Skip to content

Commit 1e0bec4

Browse files
committed
feat: add layer_devices field to Context for future multi-GPU pipeline parallelism
Adds the infrastructure (per-layer device map) but does not activate it yet — naive layer splitting across GPUs is slower due to cross-device transfer overhead. Proper multi-GPU requires pipeline parallelism in the forward pass where the hidden state is explicitly moved between devices at layer boundaries. Also includes parallel warmup + dequant optimizations from the flash-moe session.
1 parent 874db56 commit 1e0bec4

9 files changed

Lines changed: 16 additions & 2 deletions

File tree

cake-core/src/cake/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::{
2+
collections::HashMap,
23
fmt::{Debug, Display},
34
path::PathBuf,
45
sync::{Arc, Mutex},
@@ -58,6 +59,9 @@ pub struct Context {
5859
/// Tensor storage for expert offloading (pread from safetensors).
5960
/// None when expert_offload is disabled.
6061
pub tensor_storage: Option<Arc<dyn utils::tensor_storage::TensorStorageProvider>>,
62+
/// Per-layer device overrides for multi-GPU support.
63+
/// When set, layers are split across GPUs instead of using `self.device` for all.
64+
pub layer_devices: Option<HashMap<String, Device>>,
6165
}
6266

6367
/// Parse a dtype string ("f16", "bf16", "f32") into a candle DType.
@@ -497,6 +501,7 @@ impl Context {
497501
listener_override: Arc::new(Mutex::new(None)),
498502
backend,
499503
tensor_storage,
504+
layer_devices: None,
500505
})
501506
}
502507
}

cake-core/src/cake/sharding/api/test_helpers.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ fn dummy_context() -> Context {
282282
quant: std::sync::Arc::new(crate::utils::NoQuantization),
283283
listener_override: std::sync::Mutex::new(None).into(),
284284
tensor_storage: None,
285+
layer_devices: None,
285286
backend: std::sync::Arc::new(crate::backends::CpuBackend::new()),
286287
}
287288
}

cake-core/src/cake/sharding/worker.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,7 @@ mod tests {
647647
quant: Arc::new(crate::utils::NoQuantization),
648648
listener_override: Arc::new(std::sync::Mutex::new(None)),
649649
tensor_storage: None,
650+
layer_devices: None,
650651
backend: Arc::new(crate::backends::CpuBackend::new()),
651652
};
652653

@@ -724,6 +725,7 @@ mod tests {
724725
quant: Arc::new(crate::utils::NoQuantization),
725726
listener_override: Arc::new(std::sync::Mutex::new(None)),
726727
tensor_storage: None,
728+
layer_devices: None,
727729
backend: Arc::new(crate::backends::CpuBackend::new()),
728730
};
729731

cake-core/src/models/common/text_model.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,7 @@ mod tests {
713713
quant: Arc::new(crate::utils::NoQuantization),
714714
listener_override: Arc::new(Mutex::new(None)),
715715
tensor_storage: None,
716+
layer_devices: None,
716717
backend: Arc::new(crate::backends::CpuBackend::new()),
717718
}
718719
}

cake-core/src/models/qwen3_5_moe/block.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ impl Forwarder for Qwen3_5MoeBlock {
8686
let moe = if let Some(storage) = &ctx.tensor_storage {
8787
let layer_prefix = format!("{name}.mlp");
8888
let mlp_vb = vb.pp("mlp");
89+
let layer_device = ctx.device.clone();
8990
// Detect stacked switch_mlp format vs individual experts
9091
let switch_mlp_name = format!("{layer_prefix}.switch_mlp.gate_proj.weight");
9192
let has_switch_mlp = storage.has_tensor(&switch_mlp_name)
@@ -95,12 +96,12 @@ impl Forwarder for Qwen3_5MoeBlock {
9596
// Stacked switch_mlp format — use disk provider with switch_mlp namespace
9697
std::sync::Arc::new(crate::models::common::disk_expert_provider::DiskExpertProvider::new_stacked(
9798
storage.clone(), format!("{layer_prefix}.switch_mlp"), cfg.num_experts,
98-
ctx.device.clone(), ctx.dtype,
99+
layer_device, ctx.dtype,
99100
))
100101
} else {
101102
// Individual experts: stream from disk
102103
std::sync::Arc::new(crate::models::common::disk_expert_provider::DiskExpertProvider::new(
103-
storage.clone(), layer_prefix, cfg.num_experts, ctx.device.clone(), ctx.dtype,
104+
storage.clone(), layer_prefix, cfg.num_experts, layer_device, ctx.dtype,
104105
ctx.quant.gptq_group_size(),
105106
))
106107
};

cake-core/src/models/sd/util.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ mod tests {
314314
quant: Arc::new(crate::utils::NoQuantization),
315315
listener_override: Arc::new(Mutex::new(None)),
316316
tensor_storage: None,
317+
layer_devices: None,
317318
backend: Arc::new(crate::backends::CpuBackend::new()),
318319
}
319320
}

cake-core/src/models/vibevoice/vibevoice.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ impl VibeVoiceTTS {
163163
quant: std::sync::Arc::new(crate::utils::NoQuantization),
164164
listener_override: std::sync::Arc::new(std::sync::Mutex::new(None)),
165165
tensor_storage: None,
166+
layer_devices: None,
166167
backend: crate::backends::create_backend(device),
167168
},
168169
})

cake-core/src/models/vibevoice/vibevoice_1_5b.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ impl VibeVoice1_5B {
279279
quant: std::sync::Arc::new(crate::utils::NoQuantization),
280280
listener_override: std::sync::Arc::new(std::sync::Mutex::new(None)),
281281
tensor_storage: None,
282+
layer_devices: None,
282283
backend: crate::backends::create_backend(device),
283284
},
284285
})

cake-core/tests/unit_tests/test_blocks.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ fn make_context(cfg: Config, vb: VarBuilder<'static>) -> Context {
3636
quant: Arc::new(cake_core::utils::NoQuantization),
3737
listener_override: Arc::new(Mutex::new(None)),
3838
tensor_storage: None,
39+
layer_devices: None,
3940
backend: Arc::new(cake_core::backends::CpuBackend::new()),
4041
}
4142
}

0 commit comments

Comments
 (0)