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

## Unreleased

- Templates: `str.format()` renders positional fields (`{}`, `{N}`) and the `{{` `}}` escapes as Python does; signed or underscored indexes, keyword, attribute, conversion and spec fields are refused by name (#3816, refs #3801). Thanks to @atakann.

- Qwen3.8-27B on Metal: on Macs with tensor units (M5), drafted decode rounds run two 16-row query tiles in each prefix-attention threadgroup instead of up to six, with the same kernel and the same tokens; Macs without tensor units keep six on a long cache and four otherwise (#3704). Thanks to @mrv777.

- CUDA: `nccl.Library.open()` and the tensor-parallel config now resolve `TF_NCCL_LIB` through the shared path rule, so GLM, Flash Next's `--tp 2` collective, MiniMax M3 and TP sessions open the same library; unset, empty or blank values keep `libnccl.so.2`, and a value with control characters is refused (#3106). Thanks to @vdalesandro.

- Server: replies in recipient-addressed messages (`to=self` … `<|eom|>`, `to=user` … `<|eot|>`, as Muse Spark Open's chat template writes them) are split into reasoning, answer and tool calls, where the whole reply, markers included, ended up in `content` (#1336). Thanks to @ajensenwaud.
Expand Down
9 changes: 9 additions & 0 deletions tools/zig/template_parity.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,15 @@ def assistant(text, **extra):
"{{ 1|float(2, 3) }}",
"{{ 1|float(boolean=true) }}",
"{{ undefined_name|float }}",
"{{ '<a{}b>'.format('X') }}|{{ '{}{}'.format(1, 'z') }}|{{ '{1}{0}'.format('a','b') }}|{{ '{{}}{}'.format(none) }}|{{ 'x'.format() }}",
"{{ '{}'.format(true) }}|{{ '{}'.format(1.5) }}|{{ '{0}{0}'.format('q') }}|{{ '{00}'.format('z') }}",
"{{ '{}{}'.format(1) }}",
"{{ '{0}{}'.format(1,2) }}",
"{{ '{'.format() }}",
"{{ '}'.format() }}",
"{{ '{+0}'.format(1) }}",
"{{ '{-0}'.format(1) }}",
"{{ '{0_0}'.format(1) }}",
]
# Jinja2 features the native engine refuses on purpose; parity here means the native render fails loudly.
UNSUPPORTED = [
Expand Down
1 change: 1 addition & 0 deletions zig/src/core/template/methods.zig
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ fn strMethod(rt: *Rt, s: v.Str, name: []const u8, pos: []const Value, kw: []cons
}
return .{ .str = out.str() };
}
if (eq(u8, name, "format")) return @import("str_format.zig").format(rt, s, pos, kw.len);
return rt.fail("str.{s}() is not supported", .{name});
}

Expand Down
2 changes: 1 addition & 1 deletion zig/src/core/template/provenance_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ test "provenance property covers every implemented filter" {
try t.expectEqual(@as(usize, 31), filters.implemented.len);
}

const str_supported = [_][]const u8{ "capitalize", "count", "endswith", "find", "index", "join", "lower", "lstrip", "removeprefix", "removesuffix", "replace", "rfind", "rindex", "rsplit", "rstrip", "split", "startswith", "strip", "upper" };
const str_supported = [_][]const u8{ "capitalize", "count", "endswith", "find", "format", "index", "join", "lower", "lstrip", "removeprefix", "removesuffix", "replace", "rfind", "rindex", "rsplit", "rstrip", "split", "startswith", "strip", "upper" };

test "provenance property covers every exposed string dict and list method including refusals" {
var arena = std.heap.ArenaAllocator.init(t.allocator);
Expand Down
50 changes: 50 additions & 0 deletions zig/src/core/template/str_format.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//! str.format() for templates: "{}" and "{N}" fields and the "{{" "}}" escapes; every other field is refused.
const std = @import("std");
const v = @import("value.zig");
const Value = v.Value;
const Rt = v.Rt;
const Error = v.Error;

const Numbering = enum { unset, auto, manual };

/// Python's str.format on `s` with positional `args`; a keyword, attribute, conversion or spec field fails by name.
pub fn format(rt: *Rt, s: v.Str, args: []const Value, keywords: usize) Error!Value {
if (keywords > 0) return rt.fail("str.format() keyword fields are not supported", .{});
const t = s.s;
var out: v.Buffer = .{};
var numbering: Numbering = .unset;
var next: usize = 0;
var from: usize = 0;
var i: usize = 0;
while (i < t.len) : (i += 1) {
if (t[i] != '{' and t[i] != '}') continue;
try out.append(rt.a, try s.part(rt.a, t[from..i]));
if (i + 1 < t.len and t[i + 1] == t[i]) {
try out.append(rt.a, try s.part(rt.a, t[i .. i + 1]));
i += 1;
from = i + 1;
continue;
}
if (t[i] == '}') return rt.fail("Single '}}' encountered in format string", .{});
const close = std.mem.indexOfScalarPos(u8, t, i + 1, '}') orelse return rt.fail("Single '{{' encountered in format string", .{});
const field = t[i + 1 .. close];
var at = next;
if (field.len == 0) {
if (numbering == .manual) return rt.fail("cannot switch from manual field specification to automatic field numbering", .{});
numbering = .auto;
next += 1;
} else {
// Python's index is decimal digits alone: '+0', '-0' and '0_0' are keyword names (parseInt takes them)
for (field) |ch| if (!std.ascii.isDigit(ch)) return rt.fail("str.format() field '{s}' is not supported", .{field});
at = std.fmt.parseInt(usize, field, 10) catch return rt.fail("Too many decimal digits in format string", .{});
if (numbering == .auto) return rt.fail("cannot switch from automatic field numbering to manual field specification", .{});
numbering = .manual;
}
if (at >= args.len) return rt.fail("Replacement index {d} out of range for positional args tuple", .{at});
try out.append(rt.a, try v.asStr(rt, args[at]));
i = close;
from = close + 1;
}
try out.append(rt.a, try s.part(rt.a, t[from..]));
return .{ .str = out.str() };
}
12 changes: 12 additions & 0 deletions zig/src/core/template/tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@ const probes = [_]struct { []const u8, ?[]const u8, ?[]const u8 }{
.{ "{{ undefined_name|float }}", null, null },
};

test "str.format: positional fields and escapes match Python, the fields it does not run are refused" {
const fields = "{{ '<a{}b>'.format('X') }}|{{ '{}{}'.format(1, 'z') }}|{{ '{1}{0}'.format('a','b') }}|{{ '{{}}{}'.format(none) }}|{{ 'x'.format() }}";
try expect(fields, shared, .{ .text = "<aXb>|1z|ba|{}None|x", .raised = null });
try expect("{{ '{}'.format(true) }}|{{ '{}'.format(1.5) }}|{{ '{0}{0}'.format('q') }}|{{ '{00}'.format('z') }}", shared, .{ .text = "True|1.5|qq|z", .raised = null });
// Python raises on these (the signed and underscored fields are keyword names it does not find)
for ([_][]const u8{ "{{ '{}{}'.format(1) }}", "{{ '{0}{}'.format(1,2) }}", "{{ '{'.format() }}", "{{ '}'.format() }}", "{{ '{+0}'.format(1) }}", "{{ '{-0}'.format(1) }}", "{{ '{0_0}'.format(1) }}" }) |source|
try expect(source, shared, .{ .text = null, .raised = null });
// Python renders these; the engine refuses a spec, a keyword field and a conversion by name
for ([_][]const u8{ "{{ '{:>4}'.format(1) }}", "{{ '{a}'.format(a=1) }}", "{{ '{!r}'.format('s') }}" }) |source|
try expect(source, shared, .{ .text = null, .raised = null });
}

test "feature probes match Python jinja2" {
for (probes) |p| try expect(p[0], shared, .{ .text = p[1], .raised = p[2] });
}
Expand Down
2 changes: 1 addition & 1 deletion zig/src/families/qwen27/attention.zig
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ pub const Attention = struct {
e.barrier();
const scale: f32 = 0.0625;
if (plan.prefix_chunks > 0) {
const sg: u32 = @min(plan.tiles, if (plan.prefix_chunks >= 64) @as(u32, 6) else 4); // a pair a tile; a long cache reads each chunk once for six tiles
const sg: u32 = @min(plan.tiles, if (a.units) @as(u32, 2) else if (plan.prefix_chunks >= 64) @as(u32, 6) else 4); // a pair a tile; tensor units take two a group, older chips six on a long cache
// a stream of one row sees every key its tile reads; only a stream of rows can mask a value
e.setPipeline(a.prefix[if (plan.rows > plan.streams) 16 + sg - 1 else sg - 1]);
bind(e, .{ scratch.get(.qa), binding.get(.caches) });
Expand Down