Skip to content
Closed
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
9 changes: 9 additions & 0 deletions THIRD_PARTY_NOTICES.md
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,15 @@ vision tower: `Inkling2VisionEmbeddings`, `Inkling2VisionRotaryEmbedding`, `Inkl
`Inkling2VisionMLP` and `Inkling2VisionMerger`. `zig/src/families/inkling2/image.zig` follows `Inkling2ImageProcessor`
in `image_processing_inkling2.py` at the same commit for its 40-pixel patches and the tower's input tables.

## Inkling 2 DSpark drafter kernels

`zig/kernels/cuda/inkling2/dspark.cu` and `zig/src/families/inkling2/dspark_ops.zig` compute the drafter in
`vllm/models/inkling2/nvidia/dspark.py` at commit `abb8194dc35e` of
[vllm-project/vllm](https://github.com/vllm-project/vllm) (Apache-2.0): the tap mix and its RMS norm in
`Inkling2DSparkDecoder.mix`, `Inkling2DraftSelfAttention`, `Inkling2DraftMLP` and the recurrent `Inkling2MarkovHead`.
The drafter's weights ship in Thinking Machines' Inkling 2 Small release under Apache-2.0. The kernels are TensorFold's
own.

## Hy4-flash NVFP4 tensor table and build scripts

`zig/src/families/hy4_flash/nvfp4_header.txt` lists names, types and shapes from the safetensors headers of the NVFP4
Expand Down
2 changes: 2 additions & 0 deletions docs/design/inkling-2-mini.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ DSpark support is part of the plan on both backends, not an extra: H4, C5 and M6
The engine slices C4 and M5 land first without it, so plain decode exists as the reference the drafter is checked against.
The drafter taps by zero-based decoder visit: 5, 23 and 41 are the inputs to those visits, and 48 is the output after the final norm, not a cache slot.

On CUDA, the Mini adapter reuses #4858's batched DSpark Q/K, attention, context, recurrence and pick kernels across the active lane streams. It advertises batched drafting to the shared lane scheduler; without that fact, the scheduler passes only one stream to each hold and cannot use the grouped kernels. The serving adapter limits requested drafts to two per stream: the eight-row head is a kernel capacity, not a reason to verify mostly rejected rows across concurrent streams. The drafter checks the full unembedding checkpoint shape but packs only the valid vocabulary rows, so the batched matmul output, softcap and picks use the same row stride. The target computes its final norm before capturing the fourth tap or unembedding; the drafter's seven recurrent contexts use mask rows 1–7, aligned with its hidden projection. The adapter verifies proposals through the ordinary target and rolls back rejected rows. Passing this device path does not waive the same-engine fidelity and speed gate below.

## Gate

The gate is agreed; the speed bar below is its threshold.
Expand Down
2 changes: 1 addition & 1 deletion tools/bench_concurrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ def main() -> None:
rep["pooled"] = bench_metrics.pooled(runs)
rep["evidence"] = [{k: r.get(k) for k in ("prompt_tokens", "cached_tokens", "tokens",
"cache_state", "complete", "token_sha", "token_sha256", "output_sha256",
"completion", "finish_reasons", "tool_calls", "tool_sha256")}
"completion", "finish_reasons", "tool_calls", "tool_sha256", "speculative")}
for r in runs]
rep["client_intervals"] = [{"start": r["sent"], "first": r.get("first"),
"last": r.get("last"), "end": r.get("completed"),
Expand Down
4 changes: 4 additions & 0 deletions tools/zig/cuda_common_allowlist.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
{"path":"zig/kernels/cuda/flashnext_gdn_tpr.cuh","kind":"asm:cp.async.wait_group","fingerprint":"f39f99cfddd23a17f459f43825e9603b36fb1dac7f464d7bec5fec0f40da73c8","count":1,"reason":"This cp.async.wait_group uses its literal pending-group count at this dependency boundary; a helper substitution needs the same immediate, scheduling and machine code."},
{"path":"zig/kernels/cuda/glm_triton.cuh","kind":"definition:block_sum","fingerprint":"c668775654a3913f9ca96e2b23f2d4be09aef3b2ecbe60e46d06f7a510cecbbb","count":1,"reason":"Triton NW-dependent butterfly and early return; different reduction order."},
{"path":"zig/kernels/cuda/glm_vision.cu","kind":"definition:block_sum","fingerprint":"679cb663bd7688332a53b69d2a4ecbf5d94ba8f0b9318aa279f21e1586fa5d41","count":1,"reason":"Tree reduction owns shared scratch and broadcasts through total; shape differs from shared tree."},
{"path":"zig/kernels/cuda/inkling2/attention.cu","kind":"definition:warpSum","fingerprint":"23eaa1ba706617a178d504e531ed1537b557d8428c2b881a3ffc6e92c07b8f76","count":1,"reason":"Inkling 2's attention keeps its xor butterfly, offsets 16 down to 1, the order tf_common::warp_sum uses; the swap needs identical SASS on the family's images."},
{"path":"zig/kernels/cuda/inkling2/dspark.cu","kind":"definition:blockSum","fingerprint":"6e1159a5a63b8da0f60b6ae6fdbccfa959c996a2d255066335d30a21b642b7c2","count":1,"reason":"Core block_sum_tree compiles to different SASS; this broadcast form keeps the DSpark kernels' recorded machine code."},
{"path":"zig/kernels/cuda/inkling2/ops.cu","kind":"definition:blockSums","fingerprint":"60bc85468608b496a7c788416a3db98223e96585e76d8842367bea5c21d1c0e2","count":1,"reason":"Sums N values a thread in one pass behind one pair of barriers; the shared block sums take one value a call."},
{"path":"zig/kernels/cuda/inkling2/ops.cu","kind":"definition:warpSum","fingerprint":"23eaa1ba706617a178d504e531ed1537b557d8428c2b881a3ffc6e92c07b8f76","count":1,"reason":"Inkling 2's glue keeps its xor butterfly, offsets 16 down to 1, the order tf_common::warp_sum uses; the swap needs identical SASS on the family's images."},
{"path":"zig/kernels/cuda/lfm2.cu","kind":"definition:block_sum","fingerprint":"d9816f70067e6b5d8b584e4a323f9aa0c84309292758719e0b2f1364e66d6c67","count":1,"reason":"Serial sum starts at red[0] with family-specific barriers; retain order and signed-zero behavior."},
{"path":"zig/kernels/cuda/minimax_m3.cuh","kind":"asm:mma.sync.aligned.m16n8k16.row.col.f32.bf16.bf16.f32","fingerprint":"edff3872cb8a0cba7404cb7e391b3fdfdabee3a8109294582e49b95953721be9","count":1,"reason":"Expert projections retain the mma.sync.aligned.m16n8k16.row.col.f32.bf16.bf16.f32 accumulator and operand constraints; a shared substitution needs machine-code identity checks."},
{"path":"zig/kernels/cuda/minimax_m3.cuh","kind":"definition:block_sum","fingerprint":"5cdf621633053e5319eafb758a82aa96a7b2716308018e821e84724de1c518dc","count":1,"reason":"Leading-sync serial block sum with no trailing sync; deliberate shape variant."},
Expand Down
2 changes: 1 addition & 1 deletion tools/zig/cuda_common_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
ALLOW = Path(__file__).with_name("cuda_common_allowlist.json")
TOKEN = re.compile(r'//[^\n]*|/\*[\s\S]*?\*/|"(?:\\.|[^"\\])*"|\'(?:\\.|[^\'\\])*\'|[A-Za-z_]\w*|\d+(?:\.\d*)?|[^\s]')
OPS = re.compile(r'\b(?:ldmatrix[.\w]*|mma\.sync[.\w]*|cp\.async[.\w]*|ld\.global\.nc[.\w]*|griddepcontrol[.\w]*)')
NAMES = re.compile(r'(?:warp_sum|block_sum\w*|fp4pair|fp8pair|e4m3f|unpack8|pack8)\Z')
NAMES = re.compile(r'(?:[wW]arp_?[sS]um|[bB]lock_?[sS]um\w*|fp4pair|fp8pair|e4m3f|unpack8|pack8)\Z')


@dataclass(frozen=True)
Expand Down
7 changes: 7 additions & 0 deletions tools/zig/cuda_common_check_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ def test_every_requested_instruction_and_definition(self):
self.unit.write_text(DEFINITION + DEFINITION.replace("warp_sum", name))
self.assertIn("definition:" + name, " ".join(self.errors()))

def test_camel_case_reductions(self):
for name in ("warpSum", "WarpSum", "blockSum", "blockSums", "BlockSumTree"):
self.unit.write_text(DEFINITION + DEFINITION.replace("warp_sum", name))
self.assertIn("definition:" + name, " ".join(self.errors()))
self.unit.write_text(DEFINITION + DEFINITION.replace("warp_sum", "warpSumOf"))
self.assertEqual(self.errors(), [])


if __name__ == "__main__":
unittest.main()
1 change: 1 addition & 0 deletions zig/build/cuda_kernels.zig
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ pub const kernels = [_]Kernel{
.{ .name = "inkling2_pattn", .src = "inkling2/prompt_attention", .flags = &.{"-O3"} }, // ours: Inkling 2's global-layer prompt attention
.{ .name = "inkling2_ops", .src = "inkling2/ops", .flags = glue }, // ours: Inkling 2's residual mix, convs, router, SwiGLU
.{ .name = "inkling2_rows", .src = "inkling2/rows", .flags = glue }, // ours: Inkling 2's router row dots on prompt rows
.{ .name = "inkling2_dspark", .src = "inkling2/dspark", .flags = glue }, // Inkling 2 DSpark kernels
.{ .name = "ricursive", .flags = glue }, // ours: Ripple's glue (embed, add-norms, output gate, FFN activation), glue.metal step for step
.{ .name = "ricursive_prep", .flags = glue }, // ours: Ripple's attention prep (q/k head norms, RoPE, K and V into rings or pages)
.{ .name = "inkling2_experts", .src = "inkling2/experts", .flags = &.{ "-O3", "--expt-relaxed-constexpr" } }, // ours: Inkling 2's grouped experts
Expand Down
38 changes: 32 additions & 6 deletions zig/kernels/cuda/act_rows.cu
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,19 @@ __device__ __forceinline__ float sigmoid(float x) { return tf_common::sigmoid_rn

using namespace tf_act;

// out[r, i] = bf16(silu(gate[r * g_ld + i]) * up[r * u_ld + i]) for i < n (n % 64 == 0); xs (or null) out's sums.
extern "C" __global__ void __launch_bounds__(NT) tf_swiglu_rows(const __nv_bfloat16* __restrict__ gate, long long g_ld,
const __nv_bfloat16* __restrict__ up, long long u_ld,
__nv_bfloat16* __restrict__ out, float* __restrict__ xs,
int n) {
namespace tf_act {

// silu(g) * u: core's g * sigmoid(g), or with Torch torch's g / (1 + expf(-g)), each step rounded as written.
template <bool Torch>
__device__ __forceinline__ float silu_mul(float g, float u) {
if (Torch) return __fmul_rn(__fdiv_rn(g, __fadd_rn(1.0f, expf(-g))), u);
return __fmul_rn(__fmul_rn(g, sigmoid(g)), u);
}

template <bool Torch>
__device__ __forceinline__ void swiglu_rows(const __nv_bfloat16* __restrict__ gate, long long g_ld,
const __nv_bfloat16* __restrict__ up, long long u_ld,
__nv_bfloat16* __restrict__ out, float* __restrict__ xs, int n) {
const int64_t r = blockIdx.x;
const int c = blockIdx.y * blockDim.x + threadIdx.x; // 8 values
const bool live = 8 * c < n;
Expand All @@ -36,7 +44,7 @@ extern "C" __global__ void __launch_bounds__(NT) tf_swiglu_rows(const __nv_bfloa
unpack8(*reinterpret_cast<const uint4*>(up + r * u_ld + 8 * c), u);
}
#pragma unroll
for (int i = 0; i < 8; ++i) o[i] = live ? __fmul_rn(__fmul_rn(g[i], sigmoid(g[i])), u[i]) : 0.f;
for (int i = 0; i < 8; ++i) o[i] = live ? silu_mul<Torch>(g[i], u[i]) : 0.f;
const uint4 op = pack8(o);
if (live) *reinterpret_cast<uint4*>(out + r * n + 8 * c) = op;
if (xs != nullptr) {
Expand All @@ -45,6 +53,24 @@ extern "C" __global__ void __launch_bounds__(NT) tf_swiglu_rows(const __nv_bfloa
}
}

} // namespace tf_act

// out[r, i] = bf16(silu(gate[r * g_ld + i]) * up[r * u_ld + i]) for i < n (n % 64 == 0); xs (or null) out's sums.
extern "C" __global__ void __launch_bounds__(NT) tf_swiglu_rows(const __nv_bfloat16* __restrict__ gate, long long g_ld,
const __nv_bfloat16* __restrict__ up, long long u_ld,
__nv_bfloat16* __restrict__ out, float* __restrict__ xs,
int n) {
tf_act::swiglu_rows<false>(gate, g_ld, up, u_ld, out, xs, n);
}

// tf_swiglu_rows with torch's silu, g / (1 + expf(-g)): Inkling 2's DSpark drafter (expf, not tf_exp, for its bits).
extern "C" __global__ void __launch_bounds__(NT) tf_swiglu_rows_expf(const __nv_bfloat16* __restrict__ gate, long long g_ld,
const __nv_bfloat16* __restrict__ up, long long u_ld,
__nv_bfloat16* __restrict__ out, float* __restrict__ xs,
int n) {
tf_act::swiglu_rows<true>(gate, g_ld, up, u_ld, out, xs, n);
}

// out[r, h * d + e] = bf16(o[r, h * d + e] * sigmoid(g[r * g_ld + h * g_hs + e])) (d % 64 == 0); xs (or null) its sums.
extern "C" __global__ void __launch_bounds__(NT) tf_gate_rows(const __nv_bfloat16* __restrict__ o,
const __nv_bfloat16* __restrict__ g, long long g_ld,
Expand Down
26 changes: 26 additions & 0 deletions zig/kernels/cuda/common/dspark_pick.cuh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// DSpark's pick merge, one body for every family: the ranks' gathered (value, id) picks to the walk's next token.
#pragma once

#include <math_constants.h>

namespace tf_common {

// Thread 0, picks stride bytes apart: the largest value, the lower id on a tie, kNone when none beats -Inf.
template <int kNone>
__device__ __forceinline__ void dspark_pick(const unsigned char* __restrict__ picks, int world, int stride,
int* __restrict__ walk, int j) {
if (threadIdx.x != 0) return;
float best = -CUDART_INF_F;
int id = kNone;
for (int q = 0; q < world; ++q) {
const float v = *reinterpret_cast<const float*>(picks + (size_t)q * stride);
const int k = *reinterpret_cast<const int*>(picks + (size_t)q * stride + 4);
if (v > best || (v == best && k < id)) {
best = v;
id = k;
}
}
walk[j + 1] = id;
}

} // namespace tf_common
Loading