Files
candle/examples/cuda_basics.rs
2023-06-27 09:15:46 +01:00

34 lines
1.3 KiB
Rust

use anyhow::Result;
use candle::{Device, Tensor};
fn main() -> Result<()> {
let device = Device::new_cuda(0)?;
let x = Tensor::new(&[[11f32, 22.], [33., 44.], [55., 66.], [77., 78.]], &device)?;
println!("> {:?}", x.sum(&[0])?.to_vec2::<f32>()?);
println!("> {:?}", x.sum(&[1])?.to_vec2::<f32>()?);
println!("> {:?}", x.sum(&[0, 1])?.to_vec2::<f32>()?);
let x = x.to_dtype(candle::DType::F16)?;
println!("> {:?}", x.sum(&[0])?.to_vec2::<half::f16>()?);
let x = Tensor::new(&[3f32, 1., 4., 1., 5.], &device)?;
println!("{:?}", x.to_vec1::<f32>()?);
let y = Tensor::new(&[2f32, 7., 1., 8., 2.], &device)?;
let z = (y + x * 3.)?;
println!("{:?}", z.to_vec1::<f32>()?);
println!("{:?}", z.sqrt()?.to_vec1::<f32>()?);
let x = Tensor::new(&[[11f32, 22.], [33., 44.], [55., 66.], [77., 78.]], &device)?;
let y = Tensor::new(&[[1f32, 2., 3.], [4., 5., 6.]], &device)?;
println!("{:?}", y.to_vec2::<f32>()?);
let z = x.matmul(&y)?;
println!("{:?}", z.to_vec2::<f32>()?);
let x = Tensor::new(
&[[11f32, 22.], [33., 44.], [55., 66.], [77., 78.]],
&Device::Cpu,
)?;
let y = Tensor::new(&[[1f32, 2., 3.], [4., 5., 6.]], &Device::Cpu)?;
println!("{:?}", y.to_vec2::<f32>()?);
let z = x.matmul(&y)?;
println!("{:?}", z.to_vec2::<f32>()?);
Ok(())
}