Skip to content

Commit de23d34

Browse files
Switch Tensor::full to return a contiguous tensor. (#2929)
1 parent d4bac37 commit de23d34

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

candle-core/src/tensor.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,7 @@ impl Tensor {
382382
Self::new_impl(array, shape, device, false)
383383
}
384384

385-
/// Returns a new tensor with all the elements having the same specified value. Note that
386-
/// the tensor is not contiguous so you would have to call `.contiguous()` on it if needed.
385+
/// Returns a new tensor with all the elements having the same specified value.
387386
///```rust
388387
/// use candle_core::{Tensor, Device};
389388
/// let a = Tensor::full(3.5, (2, 4), &Device::Cpu)?;
@@ -398,7 +397,12 @@ impl Tensor {
398397
shape: S,
399398
device: &Device,
400399
) -> Result<Self> {
401-
Self::from_vec_impl(vec![value], (), device, false)?.broadcast_as(shape)
400+
let none = BackpropOp::none();
401+
let shape = shape.into();
402+
let mut storage = unsafe { device.alloc_uninit(&shape, D::DTYPE)? };
403+
let layout = Layout::contiguous(shape.clone());
404+
storage.const_set(value.to_scalar(), &layout)?;
405+
Ok(from_storage(storage, shape, none, false))
402406
}
403407

404408
/// Creates a new 1D tensor from an iterator.

0 commit comments

Comments
 (0)