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

No compiler check for operation on different tensor type. #2115

Open
npuichigo opened this issue Apr 23, 2024 · 1 comment
Open

No compiler check for operation on different tensor type. #2115

npuichigo opened this issue Apr 23, 2024 · 1 comment

Comments

@npuichigo
Copy link
Contributor

npuichigo commented Apr 23, 2024

let a = Tensor::new(&[3u8], &candle::Device::Cpu)?;
let b = a / 4f64;

What's the value of tensor b? Wow, it's Ok(Tensor[0; u8]). Rust compiler rejects code like 3u8 / 4f64, while it's legal in tensor operation but return wrong result.

I can say these lines are wrong for cifar dataset, and all image here are just all zero u8.

let images = (Tensor::from_vec(buffer_images, (samples, 3, 32, 32), &Device::Cpu)?
.to_dtype(DType::U8)?
/ 255.)?;

Any insights on compiler check for operation on different type tensor? Or it's by design? @LaurentMazare

@LaurentMazare
Copy link
Collaborator

It's kind of by design though I agree that it's not ideal. The Tensor type does not expose the tensor dtypes so we cannot really express the constraints of a scalar multiplication or addition at run time. Still it's quite convenient to be able to say multiply the values in the tensor by a constant or add a constant, so affine operations using a f64 are supported for all tensors with "rounded" semantics.

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

2 participants