Skip to content

Commit 37a237e

Browse files
committed
Metal updates
1 parent 898dd8a commit 37a237e

3 files changed

Lines changed: 40 additions & 5 deletions

File tree

candle-core/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ pub use shape::{Shape, D};
102102
pub use storage::Storage;
103103
pub use streaming::{StreamTensor, StreamingBinOp, StreamingModule};
104104
pub use strided_index::{StridedBlocks, StridedIndex};
105-
pub use tensor::{Tensor, TensorId};
105+
pub use tensor::{from_storage_no_op, Tensor, TensorId};
106106
pub use variable::Var;
107107

108108
#[cfg(feature = "cuda")]

candle-core/src/metal_backend/device.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,21 @@ impl Commands {
5858
})
5959
}
6060

61+
pub fn flush_command_buffer(&mut self) -> Result<()> {
62+
self.command_buffer.commit();
63+
let command_buffer = self.command_queue.new_command_buffer().to_owned();
64+
self.command_buffer = command_buffer.clone();
65+
self.command_buffer_index = 0;
66+
67+
Ok(())
68+
}
69+
6170
pub fn command_buffer(&mut self) -> Result<(bool, CommandBuffer)> {
6271
let mut command_buffer = self.command_buffer.to_owned();
6372
let mut flushed = false;
6473
if self.command_buffer_index > self.compute_per_buffer {
65-
self.command_buffer.commit();
66-
command_buffer = self.command_queue.new_command_buffer().to_owned();
67-
self.command_buffer = command_buffer.clone();
68-
self.command_buffer_index = 0;
74+
self.flush_command_buffer()?;
75+
command_buffer = self.command_buffer.to_owned();
6976
flushed = true;
7077
}
7178
self.command_buffer_index += 1;
@@ -181,6 +188,13 @@ impl MetalDevice {
181188
Ok(())
182189
}
183190

191+
pub fn flush_command_buffer(&self) -> Result<()> {
192+
let mut commands = self.commands.write().map_err(MetalError::from)?;
193+
commands.flush_command_buffer()?;
194+
195+
Ok(())
196+
}
197+
184198
pub fn command_buffer(&self) -> Result<CommandBuffer> {
185199
let mut commands = self.commands.write().map_err(MetalError::from)?;
186200
let (flushed, command_buffer) = commands.command_buffer()?;
@@ -190,6 +204,11 @@ impl MetalDevice {
190204
Ok(command_buffer)
191205
}
192206

207+
pub fn new_command_buffer(&self) -> Result<CommandBuffer> {
208+
let commands = self.commands.write().map_err(MetalError::from)?;
209+
Ok(commands.command_queue.new_command_buffer().to_owned())
210+
}
211+
193212
pub fn wait_until_completed(&self) -> Result<()> {
194213
let mut commands = self.commands.write().map_err(MetalError::from)?;
195214
commands.wait_until_completed()

candle-core/src/tensor.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,22 @@ pub(crate) fn from_storage<S: Into<Shape>>(
176176
Tensor(Arc::new(tensor_))
177177
}
178178

179+
/// Creates a fresh tensor structure based on a storage and a shape, this uses contiguous strides. This has a BackpropOp:none().
180+
pub fn from_storage_no_op<S: Into<Shape>>(storage: Storage, shape: S, is_variable: bool) -> Tensor {
181+
let dtype = storage.dtype();
182+
let device = storage.device();
183+
let tensor_ = Tensor_ {
184+
id: TensorId::new(),
185+
storage: Arc::new(RwLock::new(storage)),
186+
layout: Layout::contiguous(shape),
187+
op: BackpropOp::none(),
188+
is_variable,
189+
dtype,
190+
device,
191+
};
192+
Tensor(Arc::new(tensor_))
193+
}
194+
179195
impl Tensor {
180196
pub(crate) fn ones_impl<S: Into<Shape>>(
181197
shape: S,

0 commit comments

Comments
 (0)