coreylowman/dfdx

Basic operators moving tensor variables

otavio-silva opened this issue · 0 comments

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?