Optimize Qwen3.8 inference on Apple silicon

This commit is contained in:
Georg Bauer committed 2026-09-04 23:27:09 +02:00
1 parent bf82df77cb
commit bd6353804b
14 files changed
+10149 -1145

No files matched your search

+13
View File
@@ -8,6 +8,7 @@ pub(super) struct GpuTensor {
#[derive(Clone, Copy)]
#[repr(C)]
pub(super) struct QwenWeightView {
pub(super) tensor: *const GpuTensor,
pub(super) map: *const c_void,
pub(super) size: u64,
pub(super) offset: u64,
@@ -90,6 +91,7 @@ pub(super) struct StreamExpertCacheStats {
unsafe extern "C" {
pub(super) fn ds4_gpu_init() -> i32;
pub(super) fn ds4_gpu_cleanup();
pub(super) fn ds4_gpu_metal4_tensor_api_enabled() -> i32;
pub(super) fn ds4_gpu_set_model_map_range(
model_map: *const c_void,
model_size: u64,
@@ -103,6 +105,7 @@ unsafe extern "C" {
a: *const GpuTensor,
b: *const GpuTensor,
c: *const GpuTensor,
d: *const GpuTensor,
weights: *const QwenWeightView,
weight_count: u32,
args: *const QwenKernelArgs,
@@ -132,6 +135,7 @@ unsafe extern "C" {
) -> i32;
pub(super) fn ds4_gpu_set_quality(quality: bool);
pub(super) fn ds4_gpu_set_glm_model(enabled: bool);
pub(super) fn ds4_gpu_set_qwen_model(enabled: bool);
pub(super) fn ds4_gpu_set_ssd_streaming(enabled: bool);
pub(super) fn ds4_gpu_set_model_fd(fd: i32) -> i32;
pub(super) fn ds4_gpu_set_streaming_expert_cache_budget(experts: u32);
@@ -199,6 +203,7 @@ unsafe extern "C" {
pub(super) fn ds4_gpu_parallel_ffn_finish() -> i32;
pub(super) fn ds4_gpu_parallel_ffn_abort();
pub(super) fn ds4_gpu_tensor_alloc(bytes: u64) -> *mut GpuTensor;
pub(super) fn ds4_gpu_tensor_alloc_untracked(bytes: u64) -> *mut GpuTensor;
pub(super) fn ds4_gpu_tensor_view(
base: *const GpuTensor,
offset: u64,
@@ -1847,6 +1852,7 @@ impl Context {
check(unsafe { ds4_gpu_init() }, "Metal initialization")?;
unsafe {
ds4_gpu_set_glm_model(model.shape.family == ModelFamily::Glm);
ds4_gpu_set_qwen_model(false);
ds4_gpu_set_ssd_streaming(ssd_streaming);
// Decode enables this only around DS4's eligible resident pre-M5
// MXFP4 token path; all other work starts from the portable path.
@@ -1944,6 +1950,7 @@ impl Context {
check(unsafe { ds4_gpu_init() }, "Metal initialization")?;
unsafe {
ds4_gpu_set_glm_model(false);
ds4_gpu_set_qwen_model(true);
ds4_gpu_set_ssd_streaming(false);
ds4_gpu_set_decode_pipeline_fast_lookup(0);
}
@@ -2071,6 +2078,12 @@ impl Buffer {
.ok_or_else(|| format!("Metal could not allocate {bytes} bytes"))
}
pub(super) fn untracked(bytes: u64) -> Result<Self, String> {
NonNull::new(unsafe { ds4_gpu_tensor_alloc_untracked(bytes) })
.map(Self)
.ok_or_else(|| format!("Metal could not allocate {bytes} untracked bytes"))
}
pub(super) fn view(&self, offset: u64, bytes: u64) -> Result<Self, String> {
// SAFETY: `ds4_gpu_tensor_view` in native/metal/ds4_metal.m bounds-checks
// the view, ARC-retains base_obj.buffer, and marks the view non-owning, so
+6015 -829
View File
File diff suppressed because it is too large. Load diff