Skip to content

Commit a9b9631

Browse files
committed
Fixed SHARED_BUFFER_STORAGE_MODE for ios
1 parent d0b9b26 commit a9b9631

3 files changed

Lines changed: 14 additions & 7 deletions

File tree

candle-core/src/metal_backend/device.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ use std::sync::{Arc, Mutex, RwLock};
77

88
use super::MetalError;
99

10+
// iOS and macOS have different storage modes for shared buffers.
11+
// due to the GPU/CPU management differences.
12+
#[cfg(target_os = "ios")]
13+
pub const SHARED_BUFFER_STORAGE_MODE: MTLResourceOptions = MTLResourceOptions::StorageModeShared;
14+
#[cfg(not(target_os = "ios"))]
15+
pub const SHARED_BUFFER_STORAGE_MODE: MTLResourceOptions = MTLResourceOptions::StorageModeManaged;
16+
1017
/// Unique identifier for cuda devices.
1118
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
1219
pub struct DeviceId(usize);
@@ -255,7 +262,7 @@ impl MetalDevice {
255262
/// synchronization when the CPU memory is modified
256263
/// Used as a bridge to gather data back from the GPU
257264
pub fn new_buffer_managed(&self, size: NSUInteger) -> Result<Arc<Buffer>> {
258-
self.allocate_buffer(size, MTLResourceOptions::StorageModeManaged, "managed")
265+
self.allocate_buffer(size,SHARED_BUFFER_STORAGE_MODE, "managed")
259266
}
260267

261268
/// Creates a new buffer from data.
@@ -268,12 +275,12 @@ impl MetalDevice {
268275
let new_buffer = self.device.new_buffer_with_data(
269276
data.as_ptr().cast(),
270277
size,
271-
MTLResourceOptions::StorageModeManaged,
278+
SHARED_BUFFER_STORAGE_MODE,
272279
);
273280
let mut buffers = self.buffers.write().map_err(MetalError::from)?;
274281

275282
let subbuffers = buffers
276-
.entry((size, MTLResourceOptions::StorageModeManaged))
283+
.entry((size,SHARED_BUFFER_STORAGE_MODE))
277284
.or_insert(vec![]);
278285

279286
let new_buffer = Arc::new(new_buffer);

candle-core/src/metal_backend/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ use crate::conv::{ParamsConv1D, ParamsConv2D, ParamsConvTranspose1D, ParamsConvT
55
use crate::op::{BinaryOpT, CmpOp, ReduceOp, UnaryOpT};
66
use crate::{CpuStorage, CpuStorageRef, DType, Error, Layout, Result, Shape};
77
use candle_metal_kernels::{BufferOffset, CallConvTranspose2dCfg, Kernels};
8-
use metal::{Buffer, MTLResourceOptions, NSUInteger};
8+
use metal::{Buffer, NSUInteger};
99
use std::collections::HashMap;
1010
use std::ffi::c_void;
1111
use std::sync::{Arc, Mutex, PoisonError, RwLock, TryLockError};
1212

1313
mod device;
14-
pub use device::{DeviceId, MetalDevice};
14+
pub use device::{DeviceId, MetalDevice, SHARED_BUFFER_STORAGE_MODE};
1515

1616
pub fn buffer_o<'a>(buffer: &'a Buffer, l: &Layout, dtype: DType) -> BufferOffset<'a> {
1717
BufferOffset {
@@ -2064,7 +2064,7 @@ impl BackendDevice for MetalDevice {
20642064
let seed = Arc::new(Mutex::new(device.new_buffer_with_data(
20652065
[299792458].as_ptr() as *const c_void,
20662066
4,
2067-
MTLResourceOptions::StorageModeManaged,
2067+
SHARED_BUFFER_STORAGE_MODE,
20682068
)));
20692069
let commands = device::Commands::new(command_queue)?;
20702070
Ok(Self {

candle-flash-attn/cutlass

Submodule cutlass updated 582 files

0 commit comments

Comments
 (0)