Skip to content
New issue

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

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

Already on GitHub? # to your account

Datatype conversion #2559

Open
wangjiawen2013 opened this issue Nov 28, 2024 · 7 comments
Open

Datatype conversion #2559

wangjiawen2013 opened this issue Nov 28, 2024 · 7 comments

Comments

@wangjiawen2013
Copy link
Contributor

Hi,
This is a snapshot from https://github.com/tracel-ai/burn/blob/main/examples/custom-image-dataset/src/data.rs
image
The conversion is too complicated and cause confusions to users who are not familiar with burn. I converted the datatype of TensorData like this and got the same results
image

@wangjiawen2013
Copy link
Contributor Author

And I also converted the TensorData datetype like this:
image

@wangjiawen2013
Copy link
Contributor Author

In both cases we can get the same conversion. Why not using the latter ? It's easy to understand and more concise than that in the example. Are are any special consideration in the example ?

@AsherJingkongChen
Copy link
Contributor

In both cases we can get the same conversion. Why not using the latter ? It's easy to understand and more concise than that in the example. Are are any special consideration in the example ?

I think it is just not updated. You can open a PR for it.

@laggui
Copy link
Member

laggui commented Nov 28, 2024

Actually the associated type is used to be compatible across backends. Not all backends are guaranteed to support the same element type for ints or float. For example, IntElem for wgpu is i32 but for tch it is i64.

So changing that to i64 for all won't work since the example can use both backends. But in practice, if you only need to support one backend or you know what element types are ok then you can simply make the changes as you did 🙂

@wangjiawen2013
Copy link
Contributor Author

I browsed the source code of IntElem and Element but have not found how burn determines the element types corresponding to each backend automatically. Could take about the mechanism ? Knowing this could make users understand the data type conversion better and could write more robust code.

@wangjiawen2013
Copy link
Contributor Author

Besides, when the ran the following code in evcxr_jupyter to construct a polars dataframe, it worked:

let tensor1: Tensor<NdArray, 1> = Tensor::from_data([1, 2, 3], &device);
let tensor2 = tensor1.into_data().to_vec::<<NdArray as Backend>::FloatElem>().unwrap();

// Construct a polars dataframe
let mut results = df![
    "targets" => tensor2,
].unwrap();

println!("{:?}", results);

image

But when I ran the same code in main.rs, It's very weired that this error occurred:
image

@laggui
Copy link
Member

laggui commented Nov 29, 2024

I browsed the source code of IntElem and Element but have not found how burn determines the element types corresponding to each backend automatically. Could take about the mechanism ? Knowing this could make users understand the data type conversion better and could write more robust code.

The element types are set in the backend implementation. Take a look at the NdArray backend. The default NdArray uses f32 and i64.

For wgpu, the default uses f32 and i32 and the element types are also in the backend implementation.

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

No branches or pull requests

3 participants