Something went wrong. Try again.
A general purpose .. programming language written in zig ijadux2.tngl.sh/Rinnescript
zig language
Something went wrong. Try again.
1.8 kB · 57 lines
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758// tensor_simd.rg — @tensor runtime + apply_simd element-wise kernels// Run: rinne run examples/tensor_simd.rg//// v1 executes kernels as interpreted scalar fallbacks (one invocation per// element, like the Phase-2 @vector builtins). Real 128/256/512-bit codegen// arrives with the native backend.@import = .{ std };pub fn main() int { // f32[6] filled with -1.5 on the CPU device. var t = @tensor(f32, [6], .{ .device = .cpu, .fill = -1.5, }); // NOTE: v1 defer bodies cannot capture local variables (they compile to // standalone zero-arg chunks), so tensors are released explicitly. defer std.print("tensor released\n", .{}); // Seed a few positive values so the ReLU kernel has work to do. t[1] = 2.0; t[3] = 4.25; t[4] = 10.0; std.print("before: {}\n", .{t.data()}); // ReLU: negative values clamp to zero. The kernel is an anonymous fn // literal applied element-wise; @vector(8, f32) on the parameter is a // lane-width HINT for the future native backend. t.apply_simd(fn(x: @vector(8, f32)) f32 { if x < 0.0 { return 0.0; } return x; }); const after = t.data(); std.print("relu: {}\n", .{after}); // Scale positives by 10 to show stateful follow-up passes. t.apply_simd(fn(y: f32) f32 { return y * 10.0; }); std.print("scaled: {}\n", .{t.data()}); std.print("shape={} numel={} dtype={}\n", .{ t.shape(), t.numel(), t.dtype() }); // Sanity: relu must have zeroed the three negatives. if after[0] != 0.0 or after[2] != 0.0 or after[5] != 0.0 { std.stderr("relu kernel produced wrong output\n", .{}); t.deinit(); return 1; } t.deinit(); std.print("tensor simd transformation completed\n", .{}); return 0;}