mirror of
https://github.com/huggingface/candle.git
synced 2025-06-16 18:48:51 +00:00
Use a reference for the device.
This commit is contained in:
@ -2,7 +2,7 @@ use anyhow::Result;
|
||||
use candle::{Device, Tensor};
|
||||
|
||||
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)?;
|
||||
println!("{:?}", y.to_vec1::<f32>()?);
|
||||
Ok(())
|
||||
|
@ -1,9 +1,12 @@
|
||||
use anyhow::Result;
|
||||
use candle::{DType, Device, Tensor};
|
||||
use candle::{Device, Tensor};
|
||||
|
||||
fn main() -> Result<()> {
|
||||
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>()?);
|
||||
let z = (x + y)?;
|
||||
println!("{:?}", z.to_vec1::<f32>()?);
|
||||
Ok(())
|
||||
}
|
||||
|
@ -84,7 +84,7 @@ impl Tensor {
|
||||
fn ones_impl<S: Into<Shape>>(
|
||||
shape: S,
|
||||
dtype: DType,
|
||||
device: Device,
|
||||
device: &Device,
|
||||
is_variable: bool,
|
||||
) -> Result<Self> {
|
||||
let shape = shape.into();
|
||||
@ -101,22 +101,22 @@ impl 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)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
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>>(
|
||||
shape: S,
|
||||
dtype: DType,
|
||||
device: Device,
|
||||
device: &Device,
|
||||
is_variable: bool,
|
||||
) -> Result<Self> {
|
||||
let shape = shape.into();
|
||||
@ -133,21 +133,21 @@ impl 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)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
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>(
|
||||
array: A,
|
||||
device: Device,
|
||||
device: &Device,
|
||||
is_variable: bool,
|
||||
) -> Result<Self> {
|
||||
let shape = array.shape()?;
|
||||
@ -164,11 +164,11 @@ impl 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)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,7 @@ use candle::{Device, Tensor};
|
||||
|
||||
#[test]
|
||||
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 grads = y.backward()?;
|
||||
let grad_x = grads.get(&x).context("no grad for x")?;
|
||||
|
@ -2,7 +2,7 @@ use candle::{DType, Device, Result, Tensor};
|
||||
|
||||
#[test]
|
||||
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()?;
|
||||
assert_eq!(dim1, 5);
|
||||
assert_eq!(dim2, 2);
|
||||
@ -11,7 +11,7 @@ fn zeros() -> Result<()> {
|
||||
|
||||
#[test]
|
||||
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()?;
|
||||
assert_eq!(dim1, 3);
|
||||
let content: Vec<f32> = tensor.to_vec1()?;
|
||||
@ -28,7 +28,7 @@ fn add_mul() -> Result<()> {
|
||||
#[test]
|
||||
fn tensor_2d() -> Result<()> {
|
||||
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()?;
|
||||
assert_eq!(dims, (2, 5));
|
||||
let content: Vec<Vec<f32>> = tensor.to_vec2()?;
|
||||
@ -39,9 +39,9 @@ fn tensor_2d() -> Result<()> {
|
||||
#[test]
|
||||
fn binary_op() -> Result<()> {
|
||||
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 tensor2 = Tensor::new(data2, Device::Cpu)?;
|
||||
let tensor2 = Tensor::new(data2, &Device::Cpu)?;
|
||||
let tensor = (&tensor + (&tensor * &tensor)? / (&tensor + &tensor2))?;
|
||||
let dims = tensor.shape().r2()?;
|
||||
assert_eq!(dims, (2, 5));
|
||||
|
Reference in New Issue
Block a user