The 80B Qwen3-Coder-Next on a 16 GB Mac, about 13 tok/s, fully offline. A mixture-of-experts inference engine for Apple silicon.
Rust
use super::{BlitCommandEncoder, ComputeCommandEncoder, Device, Fence, PrevCeOutputs};
use objc2::{rc::Retained, runtime::ProtocolObject};
use objc2_foundation::NSString;
use objc2_metal::{MTLCommandBuffer, MTLCommandBufferStatus, MTLDispatchType};
use std::borrow::Cow;
use std::sync::Arc;
#[derive(Clone, Debug)]
pub struct CommandBuffer {
raw: Retained<ProtocolObject<dyn MTLCommandBuffer>>,
}
unsafe impl Send for CommandBuffer {}
unsafe impl Sync for CommandBuffer {}
impl CommandBuffer {
pub fn new(raw: Retained<ProtocolObject<dyn MTLCommandBuffer>>) -> Self {
Self { raw }
}
/// Create a compute command encoder with the provided per-encoder fence and global output map.
pub fn compute_command_encoder(&self, fence: &Arc<Fence>) -> ComputeCommandEncoder {
self.as_ref()
.computeCommandEncoderWithDispatchType(MTLDispatchType::Concurrent)
.map(|raw| ComputeCommandEncoder::new(raw, self.raw.clone(), Arc::clone(fence)))
.unwrap()
}
/// Create a compute command encoder with freshly allocated fence and a standalone output map.
/// Used by tests and `EncoderProvider` implementations that don't share a global fence map.
pub fn compute_command_encoder_no_fence(&self) -> ComputeCommandEncoder {
let device = Device::new(self.raw.device());
let fence = Arc::new(Fence::new(&device));
self.as_ref()
.computeCommandEncoderWithDispatchType(MTLDispatchType::Concurrent)
.map(|raw| ComputeCommandEncoder::new(raw, self.raw.clone(), fence))
.unwrap()
}
pub fn blit_command_encoder(
&self,
fence: &Arc<Fence>,
prev_ce_outputs: &PrevCeOutputs,
) -> BlitCommandEncoder {
self.as_ref()
.blitCommandEncoder()
.map(|raw| BlitCommandEncoder::new(raw, Arc::clone(fence), Arc::clone(prev_ce_outputs)))
.unwrap()
}
pub fn commit(&self) {
self.raw.commit()
}
/// Coachwhip: everything encoded into this buffer after this point waits until the event
/// reaches `value`. Must be called while no encoder is open on the buffer.
pub fn encode_wait_for_event(&self, event: &objc2::runtime::ProtocolObject<dyn objc2_metal::MTLEvent>, value: u64) {
self.raw.encodeWaitForEvent_value(event, value)
}
pub fn enqueue(&self) {
self.raw.enqueue()
}
pub fn set_label(&self, label: &str) {
self.as_ref().setLabel(Some(&NSString::from_str(label)))
}
pub fn status(&self) -> MTLCommandBufferStatus {
self.raw.status()
}
pub fn error(&self) -> Option<Cow<'_, str>> {
unsafe {
self.raw.error().map(|error| {
let description = error.localizedDescription();
let c_str = core::ffi::CStr::from_ptr(description.UTF8String());
c_str.to_string_lossy()
})
}
}
pub fn wait_until_completed(&self) {
self.raw.waitUntilCompleted();
}
}
impl AsRef<ProtocolObject<dyn MTLCommandBuffer>> for CommandBuffer {
fn as_ref(&self) -> &ProtocolObject<dyn MTLCommandBuffer> {
&self.raw
}
}