Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid keeping track of the copy ops when not necessary. #239

Merged
merged 1 commit into from
Jul 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions candle-core/src/tensor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1434,11 +1434,16 @@ impl Tensor {
/// Compared to clone, this copies the actual storage but may fail because of running out of
/// memory.
pub fn copy(&self) -> Result<Tensor> {
let op = if self.track_op() {
Some(Op::Copy(self.clone()))
} else {
None
};
let tensor_ = Tensor_ {
id: TensorId::new(),
storage: Arc::new(RwLock::new(self.storage().try_clone(self.layout())?)),
layout: self.layout.clone(),
op: Some(Op::Copy(self.clone())),
op,
is_variable: false,
dtype: self.dtype,
device: self.device.clone(),
Expand Down Expand Up @@ -1571,12 +1576,12 @@ impl Tensor {
let mut storage = self.device().zeros(shape, self.dtype())?;
self.storage()
.copy_strided_src(&mut storage, 0, self.layout())?;
Ok(from_storage(
storage,
shape.clone(),
Some(Op::Copy(self.clone())),
false,
))
let op = if self.track_op() {
Some(Op::Copy(self.clone()))
} else {
None
};
Ok(from_storage(storage, shape.clone(), op, false))
}
}

Expand Down
Loading