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
25 changes: 24 additions & 1 deletion crates/dluth-cuda/examples/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
//! of the port did, with that step's own breakdown of a token's time.
//!
//! Greedy, reasoning effort "none". Speed is counted from the first generated token to the
//! last, so loading the weights and reading the prompt are not in it.
//! last, so loading the weights and reading the prompt are not in it. In the default mode
//! the text is then read again in batches of 1 to 16 positions, to show what a batch costs.

use dluth_cuda::inkling::CudaProducts;
use dluth_cuda::ops::{CudaOps, TimedOps};
Expand Down Expand Up @@ -107,6 +108,28 @@ fn main() {
(total.copies_out - at_first.copies_out) as f64 / decoded as f64
);
}
// The same text again, from the start, fed in batches of consecutive positions and
// not scored: how fast the model reads text it is given. Every weight it needs is
// on the device by now. The scores after the last token are fetched to make the
// device finish.
let text: Vec<u32> = ids.iter().chain(&generated).copied().collect();
let text = &text[..text.len() / 16 * 16];
if !text.is_empty() {
println!("reading the same {} tokens again, without scoring them:", text.len());
for batch in [1, 2, 4, 8, 16] {
let started = Instant::now();
for (i, chunk) in text.chunks(batch).enumerate() {
model.feed(chunk, i * batch).expect("a batch");
}
model.next_scores().expect("the scores");
let seconds = started.elapsed().as_secs_f64();
println!(
" batches of {batch:2}: {:6.1} tokens per second, {:5.1} ms per batch",
text.len() as f64 / seconds,
seconds * 1000.0 / (text.len() / batch) as f64
);
}
}
return;
}

Expand Down
194 changes: 129 additions & 65 deletions crates/dluth-cuda/kernels/attention.cu

Large diffs are not rendered by default.

23 changes: 14 additions & 9 deletions crates/dluth-cuda/kernels/expert_ops.cu
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,20 @@ extern "C" __global__ void capped_gate_pairs(const float* both, float* act, unsi
act[i] = gate * (1.0f / (1.0f + expf(-gu.x))) * up;
}

// The weighted sum of the chosen experts' outputs:
// y[r] = sum over j of weights[j] * outs[j][r]
// `outs` is count vectors of width floats, one after another. The sum runs in the order of
// j, like the CPU reference (the loop in the model's expert step).
// The weighted sum of the chosen experts' outputs, for a batch of positions:
// y[b][r] = sum over j of weights[b][j] * outs[b][j][r]
// `outs` holds, for each position, `count` vectors of `width` floats, one after another;
// `weights` holds `count` floats per position. The sum runs in the order of j, like the
// CPU reference (the loop in the model's expert step).
extern "C" __global__ void weighted_sum(const float* outs, const float* weights, float* y,
unsigned int count, unsigned int width) {
unsigned int r = blockIdx.x * blockDim.x + threadIdx.x;
if (r >= width) return;
unsigned int count, unsigned int width, unsigned int batch) {
unsigned long long index = (unsigned long long)blockIdx.x * blockDim.x + threadIdx.x;
unsigned long long b = index / width;
unsigned int r = (unsigned int)(index % width);
if (b >= batch) return;
const float* mine = outs + b * count * width;
const float* w = weights + b * count;
float total = 0.0f;
for (unsigned int j = 0; j < count; ++j) total += weights[j] * outs[(size_t)j * width + r];
y[r] = total;
for (unsigned int j = 0; j < count; ++j) total += w[j] * mine[(size_t)j * width + r];
y[index] = total;
}
65 changes: 42 additions & 23 deletions crates/dluth-cuda/kernels/small_ops.cu
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ extern "C" __global__ void vec_add(const float* a, const float* b, float* out, u
if (i < n) out[i] = a[i] + b[i];
}

// Root-mean-square normalization with learned weights, one block of threads:
// out[i] = x[i] / sqrt(mean(x^2) + eps) * w[i]
// CPU reference: cpu::rmsnorm. The squares are added in a different order.
// Root-mean-square normalization with learned weights, for a batch of vectors of n
// floats, one block of threads per vector:
// out[b][i] = x[b][i] / sqrt(mean(x[b]^2) + eps) * w[i]
// CPU reference: cpu::rmsnorm, once per vector. The squares are added in a different order.
extern "C" __global__ void rmsnorm_weighted(const float* x, const float* w, float* out, unsigned int n,
float eps) {
float eps, unsigned int batch) {
__shared__ float part[1024];
if (blockIdx.x >= batch) return;
const float* v = x + (size_t)blockIdx.x * n;
float* o = out + (size_t)blockIdx.x * n;
float squares = 0.0f;
for (unsigned int i = threadIdx.x; i < n; i += blockDim.x) squares += x[i] * x[i];
for (unsigned int i = threadIdx.x; i < n; i += blockDim.x) squares += v[i] * v[i];
part[threadIdx.x] = squares;
__syncthreads();
if (threadIdx.x == 0) {
Expand All @@ -23,7 +27,7 @@ extern "C" __global__ void rmsnorm_weighted(const float* x, const float* w, floa
}
__syncthreads();
float scale = 1.0f / sqrtf(part[0] / (float)n + eps);
for (unsigned int i = threadIdx.x; i < n; i += blockDim.x) out[i] = x[i] * scale * w[i];
for (unsigned int i = threadIdx.x; i < n; i += blockDim.x) o[i] = v[i] * scale * w[i];
}

// Scores for a blend across depth: for each of `count` candidates of `width` floats, one
Expand Down Expand Up @@ -60,22 +64,26 @@ extern "C" __global__ void softcap_inplace(float* x, unsigned int n, float cap)
if (i < n) x[i] = cap * tanhf(x[i] / cap);
}

// A whole blend across depth in one launch, by one block of threads:
// A whole blend across depth in one launch, one block of threads per position of a batch:
// score_j = (query . candidate_j) * scale; weights = softmax(scores);
// out = sum over j of weights_j * candidate_j
// for `count` candidates of `width` floats, one after another in `candidates`.
// One launch here replaces three (scores, softmax, weighted sum); a token makes about two
// hundred blends. CPU reference: depthmix::mix_normalized.
// out[b] = sum over j of weights_j * candidate_j
// Position b's `count` candidates of `width` floats are one after another in `candidates`,
// after those of the positions before it. CPU reference: depthmix::mix_normalized, once per
// position.
#define BLEND_MAX 16
extern "C" __global__ void blend_mix(const float* query, const float* candidates, float* out,
unsigned int count, unsigned int width, float scale) {
unsigned int count, unsigned int width, float scale,
unsigned int batch) {
__shared__ float part[BLEND_MAX][256];
__shared__ float weight[BLEND_MAX];
if (blockIdx.x >= batch) return;
const float* mine = candidates + (size_t)blockIdx.x * count * width;
float* o = out + (size_t)blockIdx.x * width;
float dot[BLEND_MAX];
for (unsigned int j = 0; j < count; ++j) dot[j] = 0.0f;
for (unsigned int i = threadIdx.x; i < width; i += blockDim.x) {
float q = query[i];
for (unsigned int j = 0; j < count; ++j) dot[j] += q * candidates[(size_t)j * width + i];
for (unsigned int j = 0; j < count; ++j) dot[j] += q * mine[(size_t)j * width + i];
}
for (unsigned int j = 0; j < count; ++j) part[j][threadIdx.x] = dot[j];
__syncthreads();
Expand All @@ -97,22 +105,26 @@ extern "C" __global__ void blend_mix(const float* query, const float* candidates
__syncthreads();
for (unsigned int i = threadIdx.x; i < width; i += blockDim.x) {
float total = 0.0f;
for (unsigned int j = 0; j < count; ++j) total += weight[j] * candidates[(size_t)j * width + i];
out[i] = total;
for (unsigned int j = 0; j < count; ++j) total += weight[j] * mine[(size_t)j * width + i];
o[i] = total;
}
}

// Up to eight vectors, each wherever it lives, normalized into one buffer in one launch:
// out[j][i] = x_j[i] / sqrt(mean(x_j^2) + eps)
// One block of threads per vector. CPU reference: cpu::rmsnorm_plain, once per vector.
// Up to eight candidates, each wherever it lives and each a batch of vectors of `width`,
// normalized into one buffer in one launch:
// out[b][j][i] = x_j[b][i] / sqrt(mean(x_j[b]^2) + eps)
// so that position b's candidates end up one after another. One block of threads per
// vector. CPU reference: cpu::rmsnorm_plain, once per vector.
extern "C" __global__ void normalize_many(const float* x0, const float* x1, const float* x2, const float* x3,
const float* x4, const float* x5, const float* x6, const float* x7,
float* out, unsigned int count, unsigned int width, float eps) {
float* out, unsigned int count, unsigned int width, float eps,
unsigned int batch) {
__shared__ float part[1024];
unsigned int j = blockIdx.x;
if (j >= count) return;
unsigned int j = blockIdx.x % count;
unsigned int b = blockIdx.x / count;
if (b >= batch) return;
const float* all[8] = {x0, x1, x2, x3, x4, x5, x6, x7};
const float* x = all[j];
const float* x = all[j] + (size_t)b * width;
float squares = 0.0f;
for (unsigned int i = threadIdx.x; i < width; i += blockDim.x) squares += x[i] * x[i];
part[threadIdx.x] = squares;
Expand All @@ -123,5 +135,12 @@ extern "C" __global__ void normalize_many(const float* x0, const float* x1, cons
}
__syncthreads();
float scale = 1.0f / sqrtf(part[0] / (float)width + eps);
for (unsigned int i = threadIdx.x; i < width; i += blockDim.x) out[(size_t)j * width + i] = x[i] * scale;
float* o = out + ((size_t)b * count + j) * width;
for (unsigned int i = threadIdx.x; i < width; i += blockDim.x) o[i] = x[i] * scale;
}

// out[i] = x[i]: one position of a batch copied out as a batch of one.
extern "C" __global__ void vec_copy(const float* x, float* out, unsigned int n) {
unsigned int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < n) out[i] = x[i];
}
Loading
Loading