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

Basic operators moving tensor variables #929

Open
otavio-silva opened this issue Oct 20, 2024 · 0 comments
Open

Basic operators moving tensor variables #929

otavio-silva opened this issue Oct 20, 2024 · 0 comments

Comments

@otavio-silva
Copy link

I'm trying to understand the basics of the library to use it to implement CUDA-accelerated evolutionary algorithms for a college assignment. I have already worked with TensorFlow and PyTorch, and have a very good understanding of these libraries. I'm still learning Rust, and also learning how this library works.
I have the below code:

use dfdx::{prelude, shapes, tensor};

fn main()
{
	let dev = prelude::AutoDevice::default();
	let t1: tensor::Tensor<shapes::Rank1<4096>, f32, _> = tensor::OnesTensor::ones(&dev);
	let t2 = tensor::OnesTensor::ones(&dev);
	let t3 = t1 + t2;
	println!("a: {:?}\nb: {:?}\na + b: {:?}", t1.as_vec(), t2.as_vec(), t3.as_vec());
}

And when I try to run it using cargo run, I get the errors below:

error[E0382]: borrow of moved value: `t1`
  --> src/main.rs:9:44
   |
6  |     let t1: tensor::Tensor<shapes::Rank1<4096>, f32, _> = tensor::OnesTensor::ones(&dev);
   |         -- move occurs because `t1` has type `Tensor<(Const<4096>,), f32, Cuda>`, which does not implement the `Copy` trait
7  |     let t2 = tensor::OnesTensor::ones(&dev);
8  |     let t3 = t1 + t2;
   |              ------- `t1` moved due to usage in operator
9  |     println!("a: {:?}\nb: {:?}\na + b: {:?}", t1.as_vec(), t2.as_vec(), t3.as_vec());
   |                                               ^^ value borrowed here after move
   |
note: calling this operator moves the left-hand side
  --> C:\Users\otavi\scoop\persist\rustup-gnu\.rustup\toolchains\stable-x86_64-pc-windows-gnu\lib/rustlib/src/rust\library\core\src\ops\arith.rs:91:12
   |
91 |     fn add(self, rhs: Rhs) -> Self::Output;
   |            ^^^^
help: consider cloning the value if the performance cost is acceptable
   |
8  |     let t3 = t1.clone() + t2;
   |                ++++++++

error[E0382]: borrow of moved value: `t2`
 --> src/main.rs:9:57
  |
7 |     let t2 = tensor::OnesTensor::ones(&dev);
  |         -- move occurs because `t2` has type `Tensor<(Const<4096>,), f32, Cuda>`, which does not implement the `Copy` trait
8 |     let t3 = t1 + t2;
  |                   -- value moved here
9 |     println!("a: {:?}\nb: {:?}\na + b: {:?}", t1.as_vec(), t2.as_vec(), t3.as_vec());
  |                                                            ^^ value borrowed here after move
  |
help: consider cloning the value if the performance cost is acceptable
  |
8 |     let t3 = t1 + t2.clone();
  |                     ++++++++

For more information about this error, try `rustc --explain E0382`.
error: could not compile `dfdx-cuda-add` (bin "dfdx-cuda-add") due to 2 previous errors

From my understanding, basic tensor operators such as +, -, * and others should be pure (in the functional sense) but the error messages indicate that, when creating t3, some side effect is happening with t1 and t2 that makes these errors appear. Why is that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant