mirror of
https://github.com/huggingface/candle.git
synced 2025-06-17 02:58:50 +00:00
Use a reference for the device.
This commit is contained in:
@ -2,7 +2,7 @@ use anyhow::Result;
|
|||||||
use candle::{Device, Tensor};
|
use candle::{Device, Tensor};
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let x = Tensor::var(&[3f32, 1., 4.], Device::Cpu)?;
|
let x = Tensor::var(&[3f32, 1., 4.], &Device::Cpu)?;
|
||||||
let y = (((&x * &x)? + &x * 5f64)? + 4f64)?;
|
let y = (((&x * &x)? + &x * 5f64)? + 4f64)?;
|
||||||
println!("{:?}", y.to_vec1::<f32>()?);
|
println!("{:?}", y.to_vec1::<f32>()?);
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -1,9 +1,12 @@
|
|||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use candle::{DType, Device, Tensor};
|
use candle::{Device, Tensor};
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let device = Device::new_cuda(0)?;
|
let device = Device::new_cuda(0)?;
|
||||||
let x = Tensor::zeros(4, DType::F32, device)?;
|
let x = Tensor::new(&[3f32, 1., 4., 1., 5.], &device)?;
|
||||||
|
let y = Tensor::new(&[2f32, 7., 1., 8., 2.], &device)?;
|
||||||
println!("{:?}", x.to_vec1::<f32>()?);
|
println!("{:?}", x.to_vec1::<f32>()?);
|
||||||
|
let z = (x + y)?;
|
||||||
|
println!("{:?}", z.to_vec1::<f32>()?);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -84,7 +84,7 @@ impl Tensor {
|
|||||||
fn ones_impl<S: Into<Shape>>(
|
fn ones_impl<S: Into<Shape>>(
|
||||||
shape: S,
|
shape: S,
|
||||||
dtype: DType,
|
dtype: DType,
|
||||||
device: Device,
|
device: &Device,
|
||||||
is_variable: bool,
|
is_variable: bool,
|
||||||
) -> Result<Self> {
|
) -> Result<Self> {
|
||||||
let shape = shape.into();
|
let shape = shape.into();
|
||||||
@ -101,22 +101,22 @@ impl Tensor {
|
|||||||
Ok(Self(Arc::new(tensor_)))
|
Ok(Self(Arc::new(tensor_)))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ones<S: Into<Shape>>(shape: S, dtype: DType, device: Device) -> Result<Self> {
|
pub fn ones<S: Into<Shape>>(shape: S, dtype: DType, device: &Device) -> Result<Self> {
|
||||||
Self::ones_impl(shape, dtype, device, false)
|
Self::ones_impl(shape, dtype, device, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ones_var<S: Into<Shape>>(shape: S, dtype: DType, device: Device) -> Result<Self> {
|
pub fn ones_var<S: Into<Shape>>(shape: S, dtype: DType, device: &Device) -> Result<Self> {
|
||||||
Self::ones_impl(shape, dtype, device, true)
|
Self::ones_impl(shape, dtype, device, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ones_like(&self) -> Result<Self> {
|
pub fn ones_like(&self) -> Result<Self> {
|
||||||
Tensor::ones(self.shape(), self.dtype(), self.device())
|
Tensor::ones(self.shape(), self.dtype(), &self.device())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn zeros_impl<S: Into<Shape>>(
|
fn zeros_impl<S: Into<Shape>>(
|
||||||
shape: S,
|
shape: S,
|
||||||
dtype: DType,
|
dtype: DType,
|
||||||
device: Device,
|
device: &Device,
|
||||||
is_variable: bool,
|
is_variable: bool,
|
||||||
) -> Result<Self> {
|
) -> Result<Self> {
|
||||||
let shape = shape.into();
|
let shape = shape.into();
|
||||||
@ -133,21 +133,21 @@ impl Tensor {
|
|||||||
Ok(Self(Arc::new(tensor_)))
|
Ok(Self(Arc::new(tensor_)))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn zeros<S: Into<Shape>>(shape: S, dtype: DType, device: Device) -> Result<Self> {
|
pub fn zeros<S: Into<Shape>>(shape: S, dtype: DType, device: &Device) -> Result<Self> {
|
||||||
Self::zeros_impl(shape, dtype, device, false)
|
Self::zeros_impl(shape, dtype, device, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn zeros_var<S: Into<Shape>>(shape: S, dtype: DType, device: Device) -> Result<Self> {
|
pub fn zeros_var<S: Into<Shape>>(shape: S, dtype: DType, device: &Device) -> Result<Self> {
|
||||||
Self::zeros_impl(shape, dtype, device, true)
|
Self::zeros_impl(shape, dtype, device, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn zeros_like(&self) -> Result<Self> {
|
pub fn zeros_like(&self) -> Result<Self> {
|
||||||
Tensor::zeros(self.shape(), self.dtype(), self.device())
|
Tensor::zeros(self.shape(), self.dtype(), &self.device())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_impl<A: crate::device::NdArray>(
|
pub fn new_impl<A: crate::device::NdArray>(
|
||||||
array: A,
|
array: A,
|
||||||
device: Device,
|
device: &Device,
|
||||||
is_variable: bool,
|
is_variable: bool,
|
||||||
) -> Result<Self> {
|
) -> Result<Self> {
|
||||||
let shape = array.shape()?;
|
let shape = array.shape()?;
|
||||||
@ -164,11 +164,11 @@ impl Tensor {
|
|||||||
Ok(Self(Arc::new(tensor_)))
|
Ok(Self(Arc::new(tensor_)))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new<A: crate::device::NdArray>(array: A, device: Device) -> Result<Self> {
|
pub fn new<A: crate::device::NdArray>(array: A, device: &Device) -> Result<Self> {
|
||||||
Self::new_impl(array, device, false)
|
Self::new_impl(array, device, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn var<A: crate::device::NdArray>(array: A, device: Device) -> Result<Self> {
|
pub fn var<A: crate::device::NdArray>(array: A, device: &Device) -> Result<Self> {
|
||||||
Self::new_impl(array, device, true)
|
Self::new_impl(array, device, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@ use candle::{Device, Tensor};
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn simple_grad() -> Result<()> {
|
fn simple_grad() -> Result<()> {
|
||||||
let x = Tensor::var(&[3f32, 1., 4.], Device::Cpu)?;
|
let x = Tensor::var(&[3f32, 1., 4.], &Device::Cpu)?;
|
||||||
let y = (((&x * &x)? + &x * 5f64)? + 4f64)?;
|
let y = (((&x * &x)? + &x * 5f64)? + 4f64)?;
|
||||||
let grads = y.backward()?;
|
let grads = y.backward()?;
|
||||||
let grad_x = grads.get(&x).context("no grad for x")?;
|
let grad_x = grads.get(&x).context("no grad for x")?;
|
||||||
|
@ -2,7 +2,7 @@ use candle::{DType, Device, Result, Tensor};
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn zeros() -> Result<()> {
|
fn zeros() -> Result<()> {
|
||||||
let tensor = Tensor::zeros((5, 2), DType::F32, Device::Cpu)?;
|
let tensor = Tensor::zeros((5, 2), DType::F32, &Device::Cpu)?;
|
||||||
let (dim1, dim2) = tensor.shape().r2()?;
|
let (dim1, dim2) = tensor.shape().r2()?;
|
||||||
assert_eq!(dim1, 5);
|
assert_eq!(dim1, 5);
|
||||||
assert_eq!(dim2, 2);
|
assert_eq!(dim2, 2);
|
||||||
@ -11,7 +11,7 @@ fn zeros() -> Result<()> {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn add_mul() -> Result<()> {
|
fn add_mul() -> Result<()> {
|
||||||
let tensor = Tensor::new(&[3f32, 1., 4.], Device::Cpu)?;
|
let tensor = Tensor::new(&[3f32, 1., 4.], &Device::Cpu)?;
|
||||||
let dim1 = tensor.shape().r1()?;
|
let dim1 = tensor.shape().r1()?;
|
||||||
assert_eq!(dim1, 3);
|
assert_eq!(dim1, 3);
|
||||||
let content: Vec<f32> = tensor.to_vec1()?;
|
let content: Vec<f32> = tensor.to_vec1()?;
|
||||||
@ -28,7 +28,7 @@ fn add_mul() -> Result<()> {
|
|||||||
#[test]
|
#[test]
|
||||||
fn tensor_2d() -> Result<()> {
|
fn tensor_2d() -> Result<()> {
|
||||||
let data = &[[3f32, 1., 4., 1., 5.], [2., 1., 7., 8., 2.]];
|
let data = &[[3f32, 1., 4., 1., 5.], [2., 1., 7., 8., 2.]];
|
||||||
let tensor = Tensor::new(data, Device::Cpu)?;
|
let tensor = Tensor::new(data, &Device::Cpu)?;
|
||||||
let dims = tensor.shape().r2()?;
|
let dims = tensor.shape().r2()?;
|
||||||
assert_eq!(dims, (2, 5));
|
assert_eq!(dims, (2, 5));
|
||||||
let content: Vec<Vec<f32>> = tensor.to_vec2()?;
|
let content: Vec<Vec<f32>> = tensor.to_vec2()?;
|
||||||
@ -39,9 +39,9 @@ fn tensor_2d() -> Result<()> {
|
|||||||
#[test]
|
#[test]
|
||||||
fn binary_op() -> Result<()> {
|
fn binary_op() -> Result<()> {
|
||||||
let data = &[[3f32, 1., 4., 1., 5.], [2., 1., 7., 8., 2.]];
|
let data = &[[3f32, 1., 4., 1., 5.], [2., 1., 7., 8., 2.]];
|
||||||
let tensor = Tensor::new(data, Device::Cpu)?;
|
let tensor = Tensor::new(data, &Device::Cpu)?;
|
||||||
let data2 = &[[5f32, 5., 5., 5., 5.], [2., 1., 7., 8., 2.]];
|
let data2 = &[[5f32, 5., 5., 5., 5.], [2., 1., 7., 8., 2.]];
|
||||||
let tensor2 = Tensor::new(data2, Device::Cpu)?;
|
let tensor2 = Tensor::new(data2, &Device::Cpu)?;
|
||||||
let tensor = (&tensor + (&tensor * &tensor)? / (&tensor + &tensor2))?;
|
let tensor = (&tensor + (&tensor * &tensor)? / (&tensor + &tensor2))?;
|
||||||
let dims = tensor.shape().r2()?;
|
let dims = tensor.shape().r2()?;
|
||||||
assert_eq!(dims, (2, 5));
|
assert_eq!(dims, (2, 5));
|
||||||
|
Reference in New Issue
Block a user