Add GLM 5.2 Metal execution

This commit is contained in:
Georg Bauer
2026-07-26 12:16:30 +02:00
parent 1de954b579
commit 65c9cbfc45
6 changed files with 1819 additions and 20 deletions

View File

@@ -1,6 +1,8 @@
mod checkpoint;
mod glm;
mod gpu;
use glm::GlmExecutor;
use gpu::*;
use super::gguf::{F16, Gguf, Q8_0, Tensor as GgufTensor};
@@ -563,7 +565,7 @@ impl Session {
// and `_context` must drop before `model` unmaps memory wrapped without copying
// by native/metal/ds4_metal.m:10329. This intentionally differs from
// ../ds4/ds4.c:56287-56288; do not reorder these fields to match it.
pub(super) struct Executor {
pub(super) struct DeepSeekExecutor {
weights: Weights,
session: Session,
logits: Vec<f32>,
@@ -576,7 +578,7 @@ pub(super) struct Executor {
model: Model,
}
impl Executor {
impl DeepSeekExecutor {
pub(super) fn open(
model: Model,
context: u32,
@@ -584,7 +586,7 @@ impl Executor {
prefill_chunk: u32,
) -> Result<Self, String> {
let weights = Weights::bind(&model)?;
let context_handle = Context::open(&model, quality)?;
let context_handle = Context::open(&model, quality, false, 0)?;
let session = Session::new(
&model,
context,
@@ -849,6 +851,136 @@ impl Executor {
}
}
/// Model-family dispatch over the two Rust-owned Metal graphs.
pub(super) enum Executor {
DeepSeek(Box<DeepSeekExecutor>),
Glm(Box<GlmExecutor>),
}
impl Executor {
#[allow(dead_code)]
pub(super) fn open(
model: Model,
context: u32,
quality: bool,
prefill_chunk: u32,
) -> Result<Self, String> {
Self::open_configured(
model,
context,
quality,
prefill_chunk,
crate::settings::EngineSsdSettings {
enabled: false,
cold: false,
cache_experts: 0,
cache_bytes: 0,
full_layers: 0,
full_layers_set: false,
preload_experts: 0,
},
)
}
pub(super) fn open_configured(
model: Model,
context: u32,
quality: bool,
prefill_chunk: u32,
ssd: crate::settings::EngineSsdSettings,
) -> Result<Self, String> {
match model.shape.family {
ModelFamily::DeepSeek => DeepSeekExecutor::open(model, context, quality, prefill_chunk)
.map(Box::new)
.map(Self::DeepSeek),
ModelFamily::Glm => GlmExecutor::open(model, context, quality, ssd)
.map(Box::new)
.map(Self::Glm),
}
}
pub(super) fn eval(&mut self, token: i32) -> Result<(), String> {
match self {
Self::DeepSeek(executor) => executor.eval(token),
Self::Glm(executor) => executor.eval(token),
}
}
pub(super) fn prefill(
&mut self,
tokens: &[i32],
progress: impl FnMut(u32) -> bool,
) -> Result<usize, String> {
match self {
Self::DeepSeek(executor) => executor.prefill(tokens, progress),
Self::Glm(executor) => executor.prefill(tokens, progress),
}
}
pub(super) fn logits(&self) -> &[f32] {
match self {
Self::DeepSeek(executor) => executor.logits(),
Self::Glm(executor) => executor.logits(),
}
}
pub(super) fn model(&self) -> &Model {
match self {
Self::DeepSeek(executor) => executor.model(),
Self::Glm(executor) => executor.model(),
}
}
pub(super) fn context(&self) -> u32 {
match self {
Self::DeepSeek(executor) => executor.context(),
Self::Glm(executor) => executor.context(),
}
}
pub(super) fn position(&self) -> u32 {
match self {
Self::DeepSeek(executor) => executor.position(),
Self::Glm(executor) => executor.position(),
}
}
pub(super) fn reset(&mut self) -> Result<(), String> {
match self {
Self::DeepSeek(executor) => executor.reset(),
Self::Glm(executor) => executor.reset(),
}
}
pub(super) fn align_prompt(&mut self, tokens: &[i32]) -> Result<usize, String> {
match self {
Self::DeepSeek(executor) => executor.align_prompt(tokens),
Self::Glm(executor) => executor.align_prompt(tokens),
}
}
pub(super) fn tokens(&self) -> &[i32] {
match self {
Self::DeepSeek(executor) => executor.tokens(),
Self::Glm(executor) => executor.tokens(),
}
}
pub(super) fn checkpoint_tag(&self) -> [u8; 32] {
match self {
Self::DeepSeek(executor) => executor.checkpoint_tag(),
Self::Glm(executor) => executor.checkpoint_tag(),
}
}
pub(super) fn note_checkpoint_tag(&mut self, tag: [u8; 32]) {
match self {
Self::DeepSeek(executor) => executor.note_checkpoint_tag(tag),
Self::Glm(executor) => executor.note_checkpoint_tag(tag),
}
}
}
#[allow(clippy::too_many_arguments)]
fn refresh_ratio4_compressor_state(
s: &BatchScratch,