Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
1be6dc0
Windows: a platform layer and host builds that pass
BobClawblaw Oct 12, 2026
e5bb3bc
Windows: CUDA kernels, the native server and the GPU suites on sm_89
BobClawblaw Oct 12, 2026
1f87b27
Windows: pull into the hub cache, HOME, and the whole test step
BobClawblaw Oct 12, 2026
996a50f
Windows: build notes, strip before install, formatting
BobClawblaw Oct 12, 2026
aac93d2
Default port 8888
BobClawblaw Oct 12, 2026
81f5576
qwen3_5: serve Qwen3.5-2B on CUDA
BobClawblaw Oct 13, 2026
3ace97e
qwen3_5: serve Qwen3.8-27B on CUDA, one arena for a model's device me…
BobClawblaw Oct 14, 2026
6b85f22
qwen3_5: replay each decode shape as a CUDA graph
BobClawblaw Oct 14, 2026
b19e0d7
qwen3_5: DFlash2 drafts for Qwen3.8-27B on CUDA
BobClawblaw Oct 14, 2026
288ba03
qwen3_5: tree drafts on CUDA, the kept path replayed from a log
BobClawblaw Oct 14, 2026
61fc2bf
docs: the Qwen hybrids on CUDA in the Windows page and the README
BobClawblaw Oct 14, 2026
6aaf241
qwen3_5, platform: comments on one line
BobClawblaw Oct 14, 2026
8107ee2
Windows: checkpoint reads skip the page cache
BobClawblaw Oct 14, 2026
693533e
Windows: the signal handler type allows address 1, for aarch64
BobClawblaw Oct 14, 2026
f53ae20
docs: Windows as a target in the README and the Windows page
BobClawblaw Oct 14, 2026
e875ee2
CI: the host unit tests on Windows
BobClawblaw Oct 14, 2026
38ecce5
qwen3_5: the drafter reads TF_TIMING through the platform layer
BobClawblaw Oct 14, 2026
1817823
qwen3_5: the drafter takes a prompt longer than its tap ring
BobClawblaw Oct 14, 2026
b811a3b
qwen3_5 cuda: prompt chunks past 32 rows see the right keys, and tree…
BobClawblaw Oct 14, 2026
0a0856b
Windows: the server goldens and the Cortheon fuzz step on Windows hosts
BobClawblaw Oct 14, 2026
ccd06b9
cuda: cuBLASLt opens by its Windows DLL name
BobClawblaw Oct 14, 2026
045abbd
Windows: writes on an O_APPEND descriptor land at the end whole
BobClawblaw Oct 14, 2026
bdfeb84
golden check: a request-log mismatch names the first differing line
BobClawblaw Oct 14, 2026
39de039
lane core and native CUDA host for two-rank families with a vision to…
BobClawblaw Oct 14, 2026
153d6b4
golden check: the request log compares with LF endings on either side
BobClawblaw Oct 14, 2026
22d92e4
lane host: a warm pass that steps aside is not launched after its can…
BobClawblaw Oct 15, 2026
ef73ec3
tf_video: the includes back under its one-line header
BobClawblaw Oct 15, 2026
cc5d9d1
vision inputs: a server without --vision refuses image parts in the f…
BobClawblaw Oct 15, 2026
aaed024
qwen_image.formatSeconds: t * 10 rounded in integers, not f128, so te…
BobClawblaw Oct 15, 2026
9e6dfcd
lane host: a set-up job's kept entry stays pinned until its restore r…
BobClawblaw Oct 15, 2026
8570e06
qwen3_5: attention chunks follow each row's own keys, not the forward…
BobClawblaw Oct 15, 2026
5e1c3fe
qwen3_5: the DFlash2 config's divisors must be positive, and staging …
BobClawblaw Oct 15, 2026
52b82fc
prompt cache: an entry removed while pinned is dead to lookups and fr…
BobClawblaw Oct 15, 2026
92c9ef4
Merge branch 'pr-853' into qwen35-vision
BobClawblaw Oct 15, 2026
73f06f1
qwen3_5: image rows and M-RoPE positions in the CUDA forward
BobClawblaw Oct 15, 2026
a10b4de
qwen3_5: the vision tower on CUDA, and --vision on the Qwen engine
BobClawblaw Oct 15, 2026
ac94f8f
tf-qwen35-cuda: vision and mrope checks
BobClawblaw Oct 15, 2026
7ad45b6
native/cuda: drop the doc comment learnBytes left behind on toGib
BobClawblaw Oct 15, 2026
3217063
CI on #885: the server's image paths build on Windows; lean and a tra…
BobClawblaw Oct 15, 2026
55c47f9
tf-qwen35-cuda vision: the tower's rows bounded, exit 1 outside them
BobClawblaw Oct 15, 2026
5296eb1
tf-qwen35-cuda: one-line comments within 120 columns (the lean check)
BobClawblaw Oct 15, 2026
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
Prev Previous commit
Next Next commit
qwen3_5: the vision tower on CUDA, and --vision on the Qwen engine
The checkpoint's Qwen3_5VisionModel in bf16 (#726's tower and kernels, its
Linears on cuBLASLt): width, heads, depth and MLP read from vision_config, so
the 2B's tower (24 blocks of 1024, heads of 64) runs beside the 27B's and the
35B-A3B's. Its weights and one 16,384-patch call's scratch are one allocation
made at startup and counted as model memory. At prefill a request's images go
through the tower; their rows and get_rope_index's positions feed the prompt
chunks, and the stream keeps rope_delta. Video parts are refused by name.
  • Loading branch information
BobClawblaw committed Oct 15, 2026
commit a10b4deb5fdc06cf413588cdb1ed3bd273000a8d
16 changes: 14 additions & 2 deletions docs/vision.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Image input

This page describes image input in the Python engine, frozen at 0.6.6. Install that engine from the
[`v0.6.6` tag](https://github.com/ashhart/TensorFold/tree/v0.6.6) to serve images. The native binary refuses every
`--vision` flag at startup, reads no vision tower, and answers image parts with HTTP 400.
[`v0.6.6` tag](https://github.com/ashhart/TensorFold/tree/v0.6.6) to serve images. The native binary reads images
on its Qwen3.5/3.8 CUDA engine ([below](#native-cuda-qwen3538)); elsewhere it refuses `--vision` at startup.
Parts of it came from pull requests: GLM-5.3-Flash images by @mgoldwasser
([#101](https://github.com/ashhart/TensorFold/pull/101)), Flash Next images on CUDA by @shantanugoel
([#146](https://github.com/ashhart/TensorFold/pull/146)) from @MiaAI-Lab's patch, video by @MiaAI-Lab
Expand Down Expand Up @@ -149,3 +149,15 @@ Tests cover input validation, bounded fetching, expanded prompt accounting, cach
Hardware qualification is separate from these tests: each backend and chip needs real image understanding, drafted/serial equality, concurrency, chunked-prefill and memory checks before a release claim.
Checkpoint metadata must describe the decoder separately from MTP: the `mlp_layer_types` list must match `num_hidden_layers` for Transformers validation. Preserve the separate MTP configuration and weights.
Image input stays experimental until the hardware matrix is complete, and makes no throughput claim.

## Native CUDA: Qwen3.5/3.8

`tensorfold-native serve <checkpoint> --vision` loads the checkpoint's vision tower beside the language model on the
Qwen CUDA engine (`families/qwen3_5/cuda_vision.zig`, kernels in `qwen_vision.cu`, its Linears on cuBLASLt). The
tower and the scratch of one 16,384-patch call are allocated at startup and counted as model memory, so the
stream budget never has to make room for an image. A request's images go through the tower before its prompt;
their rows replace the `<|image_pad|>` placeholders in the prompt chunks, which rotate at M-RoPE's t, h and w
positions (interleaved sections 11, 11, 10), and the reply's rows rotate at their position plus `rope_delta`, as
transformers' `get_rope_index` lays them out. Text-only requests keep their bits. Images arrive through the native
server's image parts (decoded by the shared JPEG and PNG decoder); video parts are refused for now.

1 change: 1 addition & 0 deletions zig/build/cuda.zig
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const kernels = [_]Kernel{
.{ .name = "qwen35", .flags = &.{"-O3"} }, // ours: the Qwen3.5 hybrids' glue, the Metal kernels' rounding points
.{ .name = "qwen35_draft", .flags = &.{"-O3"} }, // ours: the DFlash2 drafter's glue (draft_ops.metal's rounding)
.{ .name = "qwen35_keep", .flags = &.{"-O3"} }, // ours: a verified window's kept path applied to its stream's state
.{ .name = "qwen_vision", .flags = &.{ "-O3", "--fmad=false" } }, // ours: the Qwen3.5 vision tower's glue and attention
.{ .name = "nemotron_norms", .flags = glue }, // ours, each with a host reference (glue_ref.zig): Nemotron's glue
.{ .name = "nemotron_route", .flags = glue },
.{ .name = "nemotron_mamba", .flags = glue },
Expand Down
202 changes: 202 additions & 0 deletions zig/kernels/cuda/qwen_vision.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
// Qwen3.5 vision tower kernels (0.6.6's --vision in bf16); epilogues add the bias to cuBLASLt's fp32, round once.
#include <cuda_bf16.h>
#include <stdint.h>

__device__ __forceinline__ float bf(__nv_bfloat16 v) { return __bfloat162float(v); }
__device__ __forceinline__ float rnd(float v) { return __bfloat162float(__float2bfloat16_rn(v)); }

// Patch embedding plus the resampled position grid: x = bf16(acc + b) + bf16(sum_t pos[idx_t] * w_t).
extern "C" __global__ void __launch_bounds__(256) tf_qv_embed(const float* __restrict__ acc,
const __nv_bfloat16* __restrict__ bias,
const __nv_bfloat16* __restrict__ pos,
const int* __restrict__ idx,
const float* __restrict__ wts,
__nv_bfloat16* __restrict__ out, int D) {
const long long row = blockIdx.x;
const int* ri = idx + row * 4;
const float* rw = wts + row * 4;
for (int i = threadIdx.x; i < D; i += blockDim.x) {
const float h = rnd(acc[row * D + i] + bf(bias[i]));
float p = 0.0f;
for (int t = 0; t < 4; ++t) p += bf(pos[(long long)ri[t] * D + i]) * rw[t];
out[row * D + i] = __float2bfloat16_rn(h + rnd(p));
}
}

// LayerNorm of bf16 rows in fp32 (mean, then the biased variance about it), bf16 out; a block a row.
extern "C" __global__ void __launch_bounds__(256) tf_qv_layernorm(const __nv_bfloat16* __restrict__ x,
const __nv_bfloat16* __restrict__ w,
const __nv_bfloat16* __restrict__ b,
__nv_bfloat16* __restrict__ y, int D, float eps) {
__shared__ float part[32];
__shared__ float stat;
const long long row = blockIdx.x;
const __nv_bfloat16* xr = x + row * D;
const int lane = threadIdx.x & 31, warp = threadIdx.x >> 5, warps = blockDim.x >> 5;
float s = 0.0f;
for (int i = threadIdx.x; i < D; i += blockDim.x) s += bf(xr[i]);
for (int o = 16; o; o >>= 1) s += __shfl_xor_sync(0xffffffffu, s, o);
if (lane == 0) part[warp] = s;
__syncthreads();
if (threadIdx.x == 0) {
float t = 0.0f;
for (int i = 0; i < warps; ++i) t += part[i];
stat = t / D;
}
__syncthreads();
const float mean = stat;
float v = 0.0f;
for (int i = threadIdx.x; i < D; i += blockDim.x) {
const float d = bf(xr[i]) - mean;
v += d * d;
}
for (int o = 16; o; o >>= 1) v += __shfl_xor_sync(0xffffffffu, v, o);
__syncthreads();
if (lane == 0) part[warp] = v;
__syncthreads();
if (threadIdx.x == 0) {
float t = 0.0f;
for (int i = 0; i < warps; ++i) t += part[i];
stat = rsqrtf(t / D + eps);
}
__syncthreads();
const float r = stat;
for (int i = threadIdx.x; i < D; i += blockDim.x)
y[row * D + i] = __float2bfloat16_rn((bf(xr[i]) - mean) * r * bf(w[i]) + bf(b[i]));
}

// qkv = bf16(acc + b) split into q, k, v [heads][rows][hd]; q and k get the axial 2-D rotary in fp32 (h, then w).
extern "C" __global__ void __launch_bounds__(128) tf_qv_qkv_rope(const float* __restrict__ acc,
const __nv_bfloat16* __restrict__ bias,
const int* __restrict__ hw,
const float* __restrict__ inv,
__nv_bfloat16* __restrict__ q,
__nv_bfloat16* __restrict__ k,
__nv_bfloat16* __restrict__ v, int rows, int heads,
int hd) {
const long long row = blockIdx.x;
const int head = blockIdx.y;
const int D = heads * hd;
const int half = hd / 2, quarter = hd / 4;
const float ph = static_cast<float>(hw[row * 2]), pw = static_cast<float>(hw[row * 2 + 1]);
const float* a = acc + row * 3 * D;
const long long o = (static_cast<long long>(head) * rows + row) * hd;
for (int j = threadIdx.x; j < hd; j += blockDim.x) {
const int c = head * hd + j;
// rotate_half pairs j with j +- hd/2; angle h * inv[jj] for the first quarter's frequencies, w * inv after
const int pj = j < half ? j + half : j - half;
const int pc = head * hd + pj;
const int jj = j % half;
const float ang = jj < quarter ? ph * inv[jj] : pw * inv[jj - quarter];
const float cs = cosf(ang), sn = sinf(ang);
const float sign = j < half ? -1.0f : 1.0f;
const float qx = rnd(a[c] + bf(bias[c])), qp = rnd(a[pc] + bf(bias[pc]));
const float kx = rnd(a[D + c] + bf(bias[D + c])), kp = rnd(a[D + pc] + bf(bias[D + pc]));
q[o + j] = __float2bfloat16_rn(qx * cs + sign * qp * sn);
k[o + j] = __float2bfloat16_rn(kx * cs + sign * kp * sn);
v[o + j] = __float2bfloat16_rn(a[2 * D + c] + bf(bias[2 * D + c]));
}
}

// Attention within one image's rows [s0, s0 + n); a thread a query row, online softmax in fp32, 64-row key tiles.
template <int HD>
__device__ void qv_attention(const __nv_bfloat16* __restrict__ q, const __nv_bfloat16* __restrict__ k,
const __nv_bfloat16* __restrict__ v, __nv_bfloat16* __restrict__ out, int rows,
int heads, int s0, int n, float scale) {
constexpr int T = 64;
__shared__ float ks[T][HD];
__shared__ float vs[T][HD];
const int head = blockIdx.y;
const int qi = blockIdx.x * T + threadIdx.x;
const bool live = qi < n;
const long long base = static_cast<long long>(head) * rows + s0;
float qr[HD], acc[HD];
float m = -INFINITY, l = 0.0f;
if (live) {
const __nv_bfloat16* qp = q + (base + qi) * HD;
for (int d = 0; d < HD; ++d) {
qr[d] = bf(qp[d]) * scale;
acc[d] = 0.0f;
}
}
for (int t0 = 0; t0 < n; t0 += T) {
const int count = min(T, n - t0);
__syncthreads();
for (int i = threadIdx.x; i < count * HD; i += blockDim.x) {
const int r = i / HD, d = i % HD;
ks[r][d] = bf(k[(base + t0 + r) * HD + d]);
vs[r][d] = bf(v[(base + t0 + r) * HD + d]);
}
__syncthreads();
if (!live) continue;
for (int r = 0; r < count; ++r) {
float s = 0.0f;
#pragma unroll
for (int d = 0; d < HD; ++d) s += qr[d] * ks[r][d];
const float mn = fmaxf(m, s);
const float corr = __expf(m - mn);
const float p = __expf(s - mn);
l = l * corr + p;
#pragma unroll
for (int d = 0; d < HD; ++d) acc[d] = acc[d] * corr + p * vs[r][d];
m = mn;
}
}
if (!live) return;
const float il = 1.0f / l;
__nv_bfloat16* op = out + static_cast<long long>(s0 + qi) * heads * HD + head * HD;
for (int d = 0; d < HD; ++d) op[d] = __float2bfloat16_rn(acc[d] * il);
}

extern "C" __global__ void __launch_bounds__(64) tf_qv_attention72(const __nv_bfloat16* __restrict__ q,
const __nv_bfloat16* __restrict__ k,
const __nv_bfloat16* __restrict__ v,
__nv_bfloat16* __restrict__ out, int rows,
int heads, int s0, int n, float scale) {
qv_attention<72>(q, k, v, out, rows, heads, s0, n, scale);
}

extern "C" __global__ void __launch_bounds__(64) tf_qv_attention64(const __nv_bfloat16* __restrict__ q,
const __nv_bfloat16* __restrict__ k,
const __nv_bfloat16* __restrict__ v,
__nv_bfloat16* __restrict__ out, int rows,
int heads, int s0, int n, float scale) {
qv_attention<64>(q, k, v, out, rows, heads, s0, n, scale);
}

// y = gelu(bf16(acc + b)), the tanh form (gelu_pytorch_tanh) or the erf form (nn.GELU), computed in fp32, bf16 out.
extern "C" __global__ void __launch_bounds__(256) tf_qv_bias_gelu(const float* __restrict__ acc,
const __nv_bfloat16* __restrict__ bias,
__nv_bfloat16* __restrict__ y, int D, int tanh_form) {
const long long row = blockIdx.x;
for (int i = threadIdx.x; i < D; i += blockDim.x) {
const float x = rnd(acc[row * D + i] + bf(bias[i]));
float g;
if (tanh_form) {
const float kBeta = 0.7978845608028654f; // sqrt(2 / pi)
g = 0.5f * x * (1.0f + tanhf(kBeta * (x + 0.044715f * x * x * x)));
} else {
g = 0.5f * x * (1.0f + erff(x * 0.7071067811865476f));
}
y[row * D + i] = __float2bfloat16_rn(g);
}
}

// x += bf16(acc + b), the residual add in bf16 (both operands bf16, the sum rounded).
extern "C" __global__ void __launch_bounds__(256) tf_qv_bias_residual(const float* __restrict__ acc,
const __nv_bfloat16* __restrict__ bias,
__nv_bfloat16* __restrict__ x, int D) {
const long long row = blockIdx.x;
for (int i = threadIdx.x; i < D; i += blockDim.x) {
const float y = rnd(acc[row * D + i] + bf(bias[i]));
x[row * D + i] = __float2bfloat16_rn(bf(x[row * D + i]) + y);
}
}

// y = bf16(acc + b): the merger's last Linear.
extern "C" __global__ void __launch_bounds__(256) tf_qv_bias_cast(const float* __restrict__ acc,
const __nv_bfloat16* __restrict__ bias,
__nv_bfloat16* __restrict__ y, int D) {
const long long row = blockIdx.x;
for (int i = threadIdx.x; i < D; i += blockDim.x) y[row * D + i] = __float2bfloat16_rn(acc[row * D + i] + bf(bias[i]));
}
1 change: 1 addition & 0 deletions zig/src/cuda/kernels.zig
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub const train: []const u8 = if (available) &Blob("fatbin_train").bytes else &.
pub const qwen35: []const u8 = if (available) &Blob("fatbin_qwen35").bytes else &.{};
pub const qwen35_draft: []const u8 = if (available) &Blob("fatbin_qwen35_draft").bytes else &.{};
pub const qwen35_keep: []const u8 = if (available) &Blob("fatbin_qwen35_keep").bytes else &.{};
pub const qwen_vision: []const u8 = if (available) &Blob("fatbin_qwen_vision").bytes else &.{};
pub const qmmf: []const u8 = if (available) &Blob("fatbin_qmmf").bytes else &.{};
pub const prompt16: []const u8 = if (available) &Blob("fatbin_prompt16").bytes else &.{};
pub const modelopt_pack: []const u8 = if (available) &Blob("fatbin_modelopt_pack").bytes else &.{};
Expand Down
3 changes: 3 additions & 0 deletions zig/src/families/qwen3_5/cuda.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ pub const geometry = @import("cuda_geometry.zig");
pub const engine = @import("cuda_engine.zig");
pub const native = @import("cuda_native.zig");
pub const draft = @import("cuda_draft.zig");
pub const vision = @import("cuda_vision.zig");

test {
_ = config;
_ = geometry;
_ = @import("draw.zig");
_ = @import("cuda_draft_weights.zig");
_ = draft;
_ = vision;
_ = @import("cuda_vision_prompt.zig");
}
32 changes: 29 additions & 3 deletions zig/src/families/qwen3_5/cuda_native.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const c = @import("config.zig");
const E = @import("cuda_engine.zig");
const draws = @import("draw.zig");
const Draft = @import("cuda_draft.zig").Draft;
const vision = @import("cuda_vision.zig");
const vision_prompt = @import("cuda_vision_prompt.zig");
const be = lanes.backend;

pub const model_type = "qwen3_5";
Expand All @@ -16,7 +18,7 @@ pub const prompt_rows: u32 = E.max_rows;
/// --drafter loads the DFlash2 draft model (openDrafter); the checkpoints have no head of their own.
pub const takes_drafter = true;

pub const Options = struct { context: usize, drafts: bool, segments: usize = 1, slide: bool = false };
pub const Options = struct { context: usize, drafts: bool, segments: usize = 1, slide: bool = false, vision: bool = false };

/// What the native server drives: the lane backend, the facts its round loop reads, and how to free it.
pub const Loaded = struct {
Expand All @@ -37,8 +39,15 @@ pub fn open(gpa: std.mem.Allocator, io: std.Io, ctx: *const cuda.Context, dir: [
const e = try E.Engine.init(gpa, io, ctx, dir);
errdefer e.deinit();
if (e.g.vocab != c.vocab) return error.UnsupportedQwenVocab; // draw.zig's masks are sized by the 2B's vocabulary
// --vision: the checkpoint's tower, its weights and one call's scratch loaded beside the model's
const tower: ?*vision.Tower = if (!o.vision) null else blk: {
const vc = try vision.Config.read(gpa, io, dir) orelse return error.QwenCheckpointHasNoVision;
if (vc.out_hidden != e.g.hidden) return error.UnsupportedQwenVision;
break :blk try vision.Tower.load(gpa, io, ctx.d, e.stream, dir, vc);
};
errdefer if (tower) |t| t.deinit();
const l = try gpa.create(Lanes);
l.* = .{ .gpa = gpa, .io = io, .e = e, .capacity = o.context };
l.* = .{ .gpa = gpa, .io = io, .e = e, .capacity = o.context, .tower = tower };
return .{
.backend = l.backend(),
.facts = .{ .exact_width = @intCast(e.window_rows), .hidden_rows = true, .batch_rows = @intCast(e.verify_rows), .max_streams = E.max_segs, .streams_exact = true },
Expand Down Expand Up @@ -107,6 +116,10 @@ fn closeDrafter(p: *anyopaque) void {
pub fn explain(_: ?*anyopaque, err: anyerror) ?[]const u8 {
return switch (err) {
error.InvalidQwenPrompt => "the prompt and its reply exceed this server's context window: shorten it or lower max_tokens",
error.QwenVideoUnsupported => "video inputs are not supported by the Qwen CUDA engine yet: send images",
error.BadImagePlaceholders => "the image placeholders do not match the prepared images",
error.BadImageGrid, error.BadImagePatches => "an image's patches do not fit the vision tower",
error.QwenServedWithoutVision => "image inputs need the server started with --vision",
error.OutOfMemory => "the GPU had no memory left for this request's caches: retry once another request ends",
else => null,
};
Expand All @@ -129,6 +142,8 @@ const Lanes = struct {
io: std.Io,
e: *E.Engine,
capacity: usize,
/// --vision's tower: an image prompt's images encoded at its prefill.
tower: ?*vision.Tower = null,
caches: std.AutoHashMapUnmanaged(*lanes.Stream, *E.Cache) = .empty,
/// Caches at the full window, kept between streams: allocated and touched once (`reserve`), reset on reuse.
pool: std.ArrayList(*E.Cache) = .empty,
Expand All @@ -139,6 +154,7 @@ const Lanes = struct {
self.caches.deinit(self.gpa);
for (self.pool.items) |cache| self.drop(cache);
self.pool.deinit(self.gpa);
if (self.tower) |t| t.deinit();
self.e.deinit();
self.gpa.destroy(self);
}
Expand Down Expand Up @@ -199,6 +215,14 @@ const Lanes = struct {
const self = cast(ptr);
const ids = stream.prompt();
if (ids.len == 0 or ids.len + stream.max_new > self.capacity or self.caches.contains(stream)) return error.InvalidQwenPrompt;
// an image prompt: its images through the tower first, then every chunk at its t, h, w positions
var media: ?vision_prompt.Media = null;
if (stream.images.len > 0) {
const tower = self.tower orelse return error.QwenServedWithoutVision;
media = try vision_prompt.Media.init(self.gpa, tower, ids, stream.images);
}
defer if (media) |*m| m.deinit();
const images: ?E.Images = if (media) |m| .{ .features = m.features.ptr, .src = m.src, .pos = m.pos } else null;
const cache = try self.take();
errdefer self.give(cache);
var at: usize = 0;
Expand All @@ -208,9 +232,11 @@ const Lanes = struct {
end = @min(end, boundary);
break;
};
try self.e.forward(&.{.{ .cache = cache, .rows = end - at }}, ids[at..end], .last);
try self.e.forward(&.{.{ .cache = cache, .rows = end - at, .images = if (images) |*im| im else null }}, ids[at..end], .last);
at = end;
}
// the reply's rows rotate past the image prompt's positions (rope_delta)
if (media) |m| cache.delta = m.delta;
try self.caches.put(self.gpa, stream, cache);
}

Expand Down
Loading