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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel

## Unreleased

- On M5 Macs, opt-in: `TF_ATTENTION_PV_FP32=1` keeps attention's P x V at full fp32 for Nemotron's prompt attention, closer to float64 (output cells off the nearest bf16 value fall from about 1.1M to 1.8K of 8.6M), at a large prompt-speed cost (30% to 87% slower at 2K to 32K). Unset, nothing changes (part of #1772).

- The benchmark clients refuse a nonpositive `--tokens`, `--reps` or `--levels` entry, or a malformed `--levels` list, with a clear error and exit status 2 before any request, instead of failing mid-run or writing zero-stream cells (#1869, fixes #1867). Thanks to @garvrao80.

- Qwen3.8-Flash-Next on Macs, opt-in: `TENSORFOLD_PREFILL_POLICY=fcfs` serves long prompts that arrive together oldest first, in chunks of `TENSORFOLD_PREFILL_TOKENS` rows (default 2,048) between decode rounds, with the same tokens. With eight 14K-token prompts at once on an M3 Ultra, the last user's first token comes after 93.6 s instead of 326.9 s; unset, nothing changes (#1843). Thanks to @ivanfioravanti.
Expand Down
10 changes: 10 additions & 0 deletions docs/kernel-contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,13 @@ The sum fixtures use integers with exact FP32 partials below 2^24, including poo
FP16 and BF16 simulations must fail in each of the three accumulator orders. Pooling declares its nine-term
reduction separately from the physical grid width, so the small stress grid remains a valid launch.
NaN and both infinity signs have explicit class controls. These registrations still require the device gates.

## Shared M5 attention products

The shared `attention_nax` defaults to the relaxed tensor P x V: float P is truncated to ten fraction bits.
`TF_ATTENTION_PV_FP32=1` opts into full float P with FP32 FMA accumulation on M5; unset preserves the old arithmetic.
The cut registration's independent formula includes the operand cut, while the full registration uses the full P.
A third registration executes the real cut kernel against the full FP32 bound and must fail the shapes gate.
The product fixture writes float output so BF16 rounding cannot conceal that control.
Both modes still require exceptional-class, repeat, guard, shape, accumulator and separate launch checks.
See [the mode's scope and device checks](recipes/m5-attention-precision.md); other families' rounding is unchanged.
53 changes: 53 additions & 0 deletions docs/recipes/m5-attention-precision.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Shared M5 attention precision

`TF_ATTENTION_PV_FP32=1` selects explicit float FMA products for the shared `attention_nax` P x V on M5.
Unset or `0` keeps the existing tensor product, which truncates a float probability to ten fraction bits.
Other values are refused when a library using this attention source is opened.
Set the variable before starting the process and keep it fixed for that process.
The default has not changed. This is an experimental arithmetic mode, not a family qualification.

The full mode retains Q x K, softmax, masks, the normalizer and the final BF16 output.
It preserves every normal FP32 probability bit in its products, with FP32 FMA accumulation.
It does not promise exact real sums or bit equality with the cut mode.
The extra scalar products and lane shuffles can cost throughput; measure the selected checkpoint and prompt size.
Older GPUs keep the previous simdgroup-matrix source even when the switch is set.
Nemotron disk prompt states include the mode in their identity and cannot cross it.

The shared helper serves Nemotron prompt attention, including its paged twin, and Qwen Image/MOSS Image attention.
It does not change the 27B text path's explicit half probabilities, general float GEMM or another family's attention.
The 27B's Qwen-VL tower also uses a separate float GEMM.
A teacher-forced 27B comparison is an unaffected control, not proof of a quality change in that model.
Image denoisers have velocity outputs rather than top tokens; their teacher-forced velocities need their own case.

## Checks

```sh
zig build test-kernel-contract tf-kernel-contract -Dcpu=apple_m1
./zig-out/bin/tf-kernel-contract --only 'attention PV'
```

The contract registers cut operands against a cut-operand binary64 formula and full operands against full binary64.
Both require FP32 sums, guarded buffers, repeats, exceptional classes and matrix tile edges.
The served Nemotron prompt range keeps the measured one-sided operand-cut terms when unset.
Full mode removes that operand cut and counts 32 FMA steps per key tile; its control still compiles the cut product.
The masked-key nonfinite finding remains a separate case in both modes.
Its planted control runs the actual cut helper against the full-operand bound and must fail the shapes gate.
The bound is the FP32 chain length and term magnitude, not a percentage fitted to the observed result.
Separate launch records name both probes and all four production full-mode alignments.
M1/M2 compilation records and an M5 device receipt are distinct evidence; unsupported hardware is not a pass.

The template probe injects exact dyadic probabilities into the real attention source, including a float-output copy.
It checks causal/full visibility, query/key tails, key-tile seams, five repeats, input bytes and guards:

```sh
swiftc -O tools/zig/attention_pv.swift -o attention-pv
TF_ATTENTION_PV_FP32=0 ./attention-pv . check red
TF_ATTENTION_PV_FP32=1 ./attention-pv . check
TF_ATTENTION_PV_FP32=0 ./attention-pv . bench 2048
TF_ATTENTION_PV_FP32=1 ./attention-pv . bench 2048
```

Run device work only on an isolated GPU under the machine's existing queue and lock.
A compile failure cannot stand in for the cut mode's numerical red witness.
Teacher-force identical histories and report every top-token difference and both margins; do not judge diverging replies.
No default should change before the affected family has its own accepted correctness and fidelity receipts.
200 changes: 200 additions & 0 deletions tools/zig/attention_pv.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
import Foundation
import Metal
import CryptoKit

func replaceOnce(_ source: String, _ old: String, _ new: String) -> String {
precondition(source.components(separatedBy: old).count == 2, "SourceAnchor: \(old)")
return source.replacingOccurrences(of: old, with: new)
}
func expand(_ path: URL) throws -> String {
let text = try String(contentsOf: path, encoding: .utf8)
return try text.components(separatedBy: "\n").map { line in
if line.hasPrefix("#include \"") {
let name = line.components(separatedBy: "\"")[1]
return try expand(path.deletingLastPathComponent().appendingPathComponent(name))
}
return line
}.joined(separator: "\n")
}
func json(_ value: [String: Any]) throws {
let data = try JSONSerialization.data(withJSONObject: value, options: [.sortedKeys, .prettyPrinted])
FileHandle.standardOutput.write(data)
FileHandle.standardOutput.write(Data([10]))
}
func bf16(_ value: Double) -> UInt16 {
let word = Float(value).bitPattern
return UInt16(truncatingIfNeeded: (word &+ 0x7fff &+ ((word >> 16) & 1)) >> 16)
}
func checked(_ command: MTLCommandBuffer) {
command.commit()
command.waitUntilCompleted()
precondition(command.status == .completed, "CommandFailed: \(String(describing: command.error))")
}
let args = CommandLine.arguments
precondition(args.count >= 3, "attention_pv SOURCE_ROOT check|bench [rows] [red]")
let root = URL(fileURLWithPath: args[1])
let mode = args[2]
let expectRed = args.last == "red"
let selection = ProcessInfo.processInfo.environment["TF_ATTENTION_PV_FP32"] ?? "0"
precondition(["0", "1"].contains(selection), "TF_ATTENTION_PV_FP32 must be 0 or 1")
let attention = (selection == "1" ? "#define TF_ATTN_PV_FP32 1\n" : "") +
(try expand(root.appendingPathComponent("zig/kernels/metal/prefill/attention_nax.metal")))
guard let device = MTLCreateSystemDefaultDevice(), device.supportsFamily(.apple10) else {
fatalError("RequiresM5TensorUnits")
}
let queue = device.makeCommandQueue()!
let options = MTLCompileOptions()
options.languageVersion = .version4_1
options.mathMode = .safe
options.mathFloatingPointFunctions = .fast
func library(_ source: String) throws -> MTLLibrary { try device.makeLibrary(source: source, options: options) }
func pipeline(_ library: MTLLibrary, _ name: String) throws -> MTLComputePipelineState {
try device.makeComputePipelineState(function: library.makeFunction(name: name)!)
}
func buffer<T>(_ values: [T]) -> MTLBuffer {
values.withUnsafeBytes { device.makeBuffer(bytes: $0.baseAddress!, length: $0.count, options: .storageModeShared)! }
}
func parameters(_ rows: Int, _ keys: Int) -> [Int32] {
let prefix = [1, 1, rows, keys, 1, (rows + 63) / 64, (keys + 31) / 32,
rows / 64, keys / 32, rows % 64, keys % 32, keys - rows]
return (prefix + [rows*128, rows*128, 128, keys*128, keys*128, 128,
keys*128, keys*128, 128, rows*128, rows*128, 128]).map(Int32.init)
}
func dispatch(_ pipe: MTLComputePipelineState, _ buffers: [MTLBuffer], _ groups: Int, _ command: MTLCommandBuffer) {
let enc = command.makeComputeCommandEncoder()!
enc.setComputePipelineState(pipe)
for (i, b) in buffers.enumerated() { enc.setBuffer(b, offset: 0, index: i) }
enc.dispatchThreadgroups(MTLSize(width: groups, height: 1, depth: 1),
threadsPerThreadgroup: MTLSize(width: 128, height: 1, depth: 1))
enc.endEncoding()
}
let sentinel: UInt32 = 0x13579bdf
if mode == "check" {
let body = attention.components(separatedBy: "// ---- entry points:")[0]
var instrumented = replaceOnce(body, "device T* O, const device int* P", "device float* O, const device int* P")
instrumented = replaceOnce(instrumented, "s[f][e] = fast::exp2(s[f][e] - top_new[e >> 2]);",
"s[f][e] = s[f][e] == kMasked ? 0.0f : (kb*BK + 16*f + home.x + TF_COL(e) == int(F[3]) ? F[1] : (kb*BK + 16*f + home.x + TF_COL(e) == int(F[3])+1 ? F[2] : 0.0f));")
let signature = """
kernel void NAME(const device bfloat* Q [[buffer(0)]], const device bfloat* K [[buffer(1)]],
const device bfloat* V [[buffer(2)]], const device int* P [[buffer(3)]], const device float* F [[buffer(4)]],
device float* O [[buffer(5)]], uint3 tg [[threadgroup_position_in_grid]],
uint sg [[simdgroup_index_in_threadgroup]], uint lane [[thread_index_in_simdgroup]]) {
tfp::FUNCTION<bfloat, AQ, AK, CAUSAL>(Q, K, V, O, P, F, tg, sg, lane);
}
"""
var shader = replaceOnce(instrumented, "device float* O, const device int* P", "device T* O, const device int* P")
let raw = replaceOnce(instrumented, "inline void attention_nax(", "inline void attention_raw(")
.replacingOccurrences(of: "acc[i][e] = acc[i][e] * inv[e >> 2];", with: "acc[i][e] = acc[i][e];")
let rawBody = raw.components(separatedBy: "// One 64-query block")[1]
shader += "\nnamespace tfp {\n// One 64-query block" + rawBody
for aq in [false, true] { for ak in [false, true] { for causal in [false, true] { for raw in [false, true] {
let name = "pv_\(aq ? 1 : 0)\(ak ? 1 : 0)\(causal ? 1 : 0)\(raw ? 1 : 0)"
let sig = raw ? signature : signature.replacingOccurrences(of: "device float* O", with: "device bfloat* O")
shader += sig.replacingOccurrences(of: "NAME", with: name)
.replacingOccurrences(of: "FUNCTION", with: raw ? "attention_raw" : "attention_nax")
.replacingOccurrences(of: "AQ", with: String(aq)).replacingOccurrences(of: "AK", with: String(ak))
.replacingOccurrences(of: "CAUSAL", with: String(causal))
} } } }
let lib = try library(shader)
let scalarText = try expand(root.appendingPathComponent("zig/kernels/metal/core/shared_attention.metal"))
let scalar = replaceOnce(scalarText, "weight[t] = precise::exp(score[t] - next);",
"weight[t] = first+t == 0 ? 1.0f : (first+t == 1 ? 0.999267578125f : 0.0f);")
let scalarLib = try library("#define TF_D 128\n#define TF_QH 1\n#define TF_KVH 1\n#define TF_RD 0\n#define TF_OUT float\n" + scalar)
let scalarPipe = try pipeline(scalarLib, "tf_shared_lane_attention")
var records = [[String: Any]](), mismatches = 0, comparisons = 0, repeats = 0, guards = 0, unchanged = 0
let cases: [(String, Float, Float, Float)] = [
("catch", 1, 0.999267578125, 1),
("third-part", 1, Float(bitPattern: 0x3f7f1234), 1),
("small-residual", Float(bitPattern: 0x03800001), Float(bitPattern: 0x03800000), Float(pow(2.0, 120))),
("exact-cancel", 1, 1, 1)]
for (label, a, b, v) in cases {
for (rows, keys, first) in [(1,2,0), (17,33,31), (64,64,0), (65,65,31)] {
let aq = rows % 64 == 0, ak = keys % 32 == 0
for causal in [false, true] { for raw in [false, true] {
let name = "pv_\(aq ? 1 : 0)\(ak ? 1 : 0)\(causal ? 1 : 0)\(raw ? 1 : 0)"
let pipe = try pipeline(lib, name)
let q = buffer([UInt16](repeating: 0, count: rows*128))
let k = buffer([UInt16](repeating: 0, count: keys*128))
var values = [UInt16](repeating: 0, count: keys*128)
for c in 0..<128 { values[first*128+c] = bf16(Double(v)); values[(first+1)*128+c] = bf16(-Double(v)) }
let vb = buffer(values), pb = buffer(parameters(rows, keys)), fb = buffer([Float(0),a,b,Float(first)])
let out = buffer([UInt32](repeating: sentinel, count: rows*128+16))
let inputs = [q,k,vb,pb,fb]
let saved = inputs.map { Data(bytes: $0.contents(), count: $0.length) }
var firstWords: [UInt32]? = nil, bad = 0
for _ in 0..<5 {
let command = queue.makeCommandBuffer()!
dispatch(pipe, inputs+[out], (rows+63)/64, command)
checked(command)
let bits = out.contents().bindMemory(to: UInt32.self, capacity: rows*128+16)
let bf = out.contents().bindMemory(to: UInt16.self, capacity: (rows*128+16)*2)
for row in 0..<rows {
let bound = causal ? row+keys-rows : keys-1
let pa = first <= bound ? Double(a) : 0, pbb = first+1 <= bound ? Double(b) : 0
let numerator = pa*Double(v)-pbb*Double(v)
let ref = raw ? numerator : (pa+pbb == 0 ? 0 : numerator/(pa+pbb))
for c in 0..<128 {
let equal = !raw && pa+pbb == 0 ? (bf[row*128+c] & 0x7f80 == 0x7f80 && bf[row*128+c] & 0x7f != 0) : (raw ? bits[row*128+c] == Float(ref).bitPattern : bf[row*128+c] == bf16(ref))
if !equal { bad += 1 }
comparisons += 1
}
}
let count = raw ? rows*128 : rows*64
let words = Array(UnsafeBufferPointer(start: bits, count: count))
if let prior = firstWords { precondition(words == prior, "RepeatChanged") } else { firstWords = words }
repeats += 1
for i in count..<count+16 { precondition(bits[i] == sentinel, "GuardChanged"); guards += 1 }
for (i,input) in inputs.enumerated() {
precondition(saved[i] == Data(bytes: input.contents(), count: input.length), "InputChanged")
unchanged += input.length
}
}
mismatches += bad
records.append(["case":label,"rows":rows,"keys":keys,"first":first,"causal":causal,"raw":raw,"mismatches":bad])
} }
}
}
let q = buffer([UInt16](repeating: 0, count: 128)), k = buffer([UInt16](repeating: 0, count: 256))
let v = buffer([UInt16](repeating: 0x3f80,count:128)+[UInt16](repeating:0xbf80,count:128))
let mask = buffer([UInt8](repeating:1,count:2)), meta = buffer([UInt32(1),2,2,1,0])
let out = buffer([UInt32](repeating:sentinel,count:144))
let scalarCmd = queue.makeCommandBuffer()!
let enc = scalarCmd.makeComputeCommandEncoder()!
enc.setComputePipelineState(scalarPipe)
for (i,b) in [q,k,v,mask,meta,out].enumerated() { enc.setBuffer(b,offset:0,index:i) }
enc.dispatchThreadgroups(MTLSize(width:1,height:1,depth:1),threadsPerThreadgroup:MTLSize(width:32,height:1,depth:1))
enc.endEncoding(); checked(scalarCmd)
let scalarValues = out.contents().bindMemory(to:Float.self,capacity:144)
let scalarExpected = bf16((1.0-0.999267578125)/(1.0+0.999267578125))
for i in 0..<128 { precondition(bf16(Double(scalarValues[i])) == scalarExpected,"ScalarMismatch") }
for i in 128..<144 { precondition(scalarValues[i].bitPattern==sentinel,"ScalarGuardChanged") }
try json(["mode":"check","precision":selection,"device":device.name,"os":ProcessInfo.processInfo.operatingSystemVersionString,
"comparisons":comparisons,"mismatches":mismatches,"dispatches":repeats,"guards":guards,
"unchanged_input_bytes_checked":unchanged,"scalar_bf16_hex":String(scalarExpected,radix:16),
"scalar_comparisons":128,"expected_red":expectRed,"records":records])
exit(expectRed ? (mismatches > 0 ? 0 : 1) : (mismatches == 0 ? 0 : 1))
} else {
precondition(mode == "bench" && args.count >= 4)
let rows = Int(args[3])!
precondition([2048,8192,32768].contains(rows))
let lib = try library(attention)
let name = "custom_kernel_tf_attention_nax_bf16_t_t_t_bfloat16_t_bfloat16_t_bfloat16_t_int32_t_float_bfloat16_t"
let pipe = try pipeline(lib,name)
let q = buffer((0..<rows*128).map { bf16(Double(($0*17)%31-15)/32) })
let k = buffer((0..<rows*128).map { bf16(Double(($0*11)%29-14)/32) })
let v = buffer((0..<rows*128).map { bf16(Double(($0*7)%23-11)/16) })
let p = buffer(parameters(rows,rows)), f = buffer([Float(1/sqrt(128.0))])
let out = buffer([UInt16](repeating:0,count:rows*128))
var times = [Double]()
for _ in 0..<4 {
let command = queue.makeCommandBuffer()!
dispatch(pipe,[q,k,v,p,f,out],rows/64,command)
checked(command)
times.append(command.gpuEndTime-command.gpuStartTime)
}
try json(["mode":"bench","precision":selection,"device":device.name,"os":ProcessInfo.processInfo.operatingSystemVersionString,
"rows":rows,"heads":1,"dimension":128,"causal":true,"gpu_seconds":times,
"output_sha256":SHA256.hash(data:Data(bytes:out.contents(),count:out.length)).map{String(format:"%02x",$0)}.joined(),
"max_threads":pipe.maxTotalThreadsPerThreadgroup,"static_tg_bytes":pipe.staticThreadgroupMemoryLength,
"note":"first sample warmup, remaining three timed; no model loaded"])
}
7 changes: 7 additions & 0 deletions zig/build/kernel_contract.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ pub fn hostTests(b: *std.Build, family: *std.Build.Step) void {
b.step("test-kernel-seed-host", "Named contract fixtures keep their seed when entries are inserted").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = module(b, b.graph.host, .debug), .filters = &.{"contract seed:"} })).step);
const step = b.step("test-kernel-contract", "The kernel contract's host tests: gate logic, controls, every launch against every chip");
step.dependOn(&run.step);
const precision = b.addRunArtifact(b.addTest(.{ .root_module = b.createModule(.{
.root_source_file = b.path("zig/src/core/attention_precision.zig"),
.target = b.graph.host,
.optimize = .debug,
}) }));
b.step("test-attention-precision-host", "Shared attention precision mode, source and kept-state identity").dependOn(&precision.step);
step.dependOn(&precision.step);
step.dependOn(family);
const rotary = b.addRunArtifact(b.addTest(.{ .root_module = b.createModule(.{
.root_source_file = b.path("zig/tests/contract/rotary_cache_positions.zig"),
Expand Down
Loading
Loading