Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions crates/dluth-cuda/src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,33 @@ impl<'m> Ops for CudaOps<'m> {
Ok(())
}

fn embed(&mut self, cfg: &InklingConfig, table: &[u8], norm: &[f32], token: u32) -> Result<DeviceVec, ModelError> {
fn count(&self, _x: &DeviceVec) -> usize {
// One position at a time for now: see `embed`.
1
}

fn row(&mut self, x: &DeviceVec, index: usize) -> Result<DeviceVec, ModelError> {
if index != 0 {
return Err(ModelError::Capacity(format!("position {index} of a batch of 1")));
}
Ok(x.clone())
}

fn embed(
&mut self,
cfg: &InklingConfig,
table: &[u8],
norm: &[f32],
tokens: &[u32],
) -> Result<DeviceVec, ModelError> {
// The device operations do not take batches yet. Refused here, where a batch enters
// the forward pass, before anything is computed.
let &[token] = tokens else {
return Err(ModelError::Capacity(format!(
"a batch of {} tokens: the device operations take one position at a time so far",
tokens.len()
)));
};
// One row of the table, normalized on the host: 2,304 floats, once per token.
let h = cfg.hidden;
let row = &table[token as usize * h * 2..(token as usize + 1) * h * 2];
Expand Down Expand Up @@ -834,8 +860,20 @@ impl<'m> Ops for TimedOps<'m> {
fn reset_pass(&mut self, pass: &mut DevicePass) -> Result<(), ModelError> {
self.inner.reset_pass(pass)
}
fn embed(&mut self, cfg: &InklingConfig, table: &[u8], norm: &[f32], token: u32) -> Result<DeviceVec, ModelError> {
self.timed("embedding", |o| o.embed(cfg, table, norm, token))
fn count(&self, x: &DeviceVec) -> usize {
self.inner.count(x)
}
fn row(&mut self, x: &DeviceVec, index: usize) -> Result<DeviceVec, ModelError> {
self.inner.row(x, index)
}
fn embed(
&mut self,
cfg: &InklingConfig,
table: &[u8],
norm: &[f32],
tokens: &[u32],
) -> Result<DeviceVec, ModelError> {
self.timed("embedding", |o| o.embed(cfg, table, norm, tokens))
}
fn normalize(&mut self, candidates: &[&DeviceVec]) -> Result<DeviceBlend, ModelError> {
self.timed("blend: normalize", |o| o.normalize(candidates))
Expand Down
21 changes: 21 additions & 0 deletions crates/dluth-cuda/tests/inkling_golden_cuda.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,27 @@ mod on_the_device {
assert_eq!(dev.live_buffers(), before);
}

/// Until the device operations take batches, a batch is refused before anything is
/// computed, and the model goes on as if it had not been asked.
#[test]
fn a_batch_is_refused_and_changes_nothing() {
let dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../tests/fixtures/tiny-inkling");
let weights = Weights::open_dir(&dir).unwrap();
let dev = CudaDevice::open(0).expect("GPU");
let module = Module::load_embedded(&dev).unwrap();
let ops = CudaOps::new(&dev, &module).unwrap();
let mut gpu = Inkling::with_ops(load_config(&dir).unwrap(), &weights, 64, ops).unwrap();
let ops = CudaOps::new(&dev, &module).unwrap();
let mut fresh = Inkling::with_ops(load_config(&dir).unwrap(), &weights, 64, ops).unwrap();
assert_eq!(gpu.forward_token(7, 0).unwrap(), fresh.forward_token(7, 0).unwrap());
let e = gpu.forward_tokens(&[8, 9], 1).unwrap_err();
assert!(
matches!(e, dluth_model::ModelError::Capacity(_)) && e.to_string().contains("one position at a time"),
"{e}"
);
assert_eq!(gpu.forward_token(8, 1).unwrap(), fresh.forward_token(8, 1).unwrap());
}

#[test]
fn the_real_model_matches_the_reference() {
let Ok(dir) = std::env::var("DLUTH_INKLING2_MINI") else {
Expand Down
2 changes: 1 addition & 1 deletion crates/dluth-model/src/inkling/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ pub mod prompt;

pub use config::{load_config, model_kind, validate_weights, Checked, ExpertFormat, InklingConfig, ModelKind};
pub use model::{
CpuBlend, CpuOps, CpuProducts, ExpertLayer, ExpertMatrices, Gate, Inkling, InklingCpu, KvConv, Layer, Ops,
CpuBlend, CpuOps, CpuProducts, CpuVecs, ExpertLayer, ExpertMatrices, Gate, Inkling, InklingCpu, KvConv, Layer, Ops,
PassState, Products, BLEND_EPS,
};
Loading
Loading