From ba671ee486bc16309fddaf1f15820cfeb6dff0d1 Mon Sep 17 00:00:00 2001 From: adrien Date: Sun, 24 May 2026 18:40:14 +0200 Subject: [PATCH] Working basic eq between TensorAlloc --- build.zig.zon | 4 +- src/TensorAlloc.zig | 1019 ++++++++++++++++++++++++++++++++++++++++++ src/TensorStatic.zig | 4 +- src/shared.zig | 2 + src/test.zig | 1 + 5 files changed, 1026 insertions(+), 4 deletions(-) create mode 100644 src/TensorAlloc.zig diff --git a/build.zig.zon b/build.zig.zon index 02a9aa5..0a6c885 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -5,8 +5,8 @@ .minimum_zig_version = "0.16.0", .dependencies = .{ .zig_wgpu = .{ - .url = "git+https://git.bouvais.lu/adrien/zig-wgpu?ref=0.2.0#9c329a5a0657b00682958457bb118e2fc977dd63", - .hash = "zig_wgpu-0.2.0-xsLAy56X0QMafD4OTAvoQIJSo5VDBiw3B24DcB6wd1ZO", + .url = "git+https://git.bouvais.lu/adrien/zig-wgpu?ref=0.2.2#5f8da0940d77c40eacd39c268d09acbeaea0b2a5", + .hash = "zig_wgpu-0.2.0-xsLAy2-s0QPNwR2QNd8ZX2kWiVfV5oB92N3ga1V1Uwpu", }, }, .paths = .{ diff --git a/src/TensorAlloc.zig b/src/TensorAlloc.zig new file mode 100644 index 0000000..fed9f79 --- /dev/null +++ b/src/TensorAlloc.zig @@ -0,0 +1,1019 @@ +const std = @import("std"); +const Allocator = std.mem.Allocator; +const Scales = @import("Scales.zig"); +const UnitScale = Scales.UnitScale; +const Dimensions = @import("Dimensions.zig"); +const Dimension = Dimensions.Dimension; +const sh = @import("shared.zig"); + +pub fn Tensor( + comptime T: type, + comptime d_opt: Dimensions.ArgOpts, + comptime s_opt: Scales.ArgOpts, + comptime shape_: []const comptime_int, +) type { + if (shape_.len == 0) + @compileError("Tensor shape must have at least 1 dimension (rank >= 1)."); + for (shape_) |s| + if (s < 1) @compileError("Tensor shape dimensions must be strictly >= 1."); + @setEvalBranchQuota(100_000_000); + + const _total: usize = comptime sh.shapeTotal(shape_); + const _strides = comptime sh.shapeStrides(shape_); + const Vec: type = @Vector(_total, T); + + if (comptime _total * @bitSizeOf(T) > 1_000_000) + @compileError("Tensor too big, consider using a TensorGPU or TensorAlloc."); + + return struct { + data: *Vec, + + const Self = @This(); + + pub const ValueType: type = T; + pub const dims: Dimensions = Dimensions.init(d_opt); + pub const scales: Scales = Scales.init(s_opt); + pub const shape: []const comptime_int = shape_; + pub const rank: comptime_int = shape_.len; + pub const total: comptime_int = _total; + pub const strides_arr: [shape_.len]comptime_int = _strides; + pub const ISTENSOR = true; + pub const TENSORKIND: sh.TensorKind = .alloc; + + /// Broadcast a single value across all elements. + pub fn splat(alloc: Allocator, v: T) !Self { + const vec = try alloc.create(Vec); + vec.* = @splat(v); + return .{ .data = vec }; + } + + pub fn load(alloc: Allocator, data: []const T) !Self { + if (data.len != total) + return error.LenMismatch; + const vec = try alloc.create(Vec); + vec.* = data[0..total].*; + return .{ .data = vec }; + } + + pub fn deinit(self: @This(), alloc: Allocator) void { + alloc.destroy(self.data); + } + + pub fn copy(self: *const Self, alloc: Allocator) !Self { + var new = try splat(alloc, 0); + new.data = self.data; + return new; + } + + /// Convert to a compatible Tensor type. + /// • Dimension mismatch → compile error. + /// • Dest.shape must equal self.shape, or total == 1 -> splat to Dest shape (scalar pattern). + /// • Scale ratio is computed fully at comptime; only a SIMD multiply at runtime. + pub inline fn to( + self: *const Self, + comptime Dest: type, + alloc: Allocator, + ) !Dest { + if (comptime Self == Dest) return self.*; + + // Run validation checks FIRST before dealing with types + if (comptime !dims.eql(Dest.dims)) + @compileError("Dimension mismatch in to: " ++ dims.str() ++ " vs " ++ Dest.dims.str()); + if (comptime total != 1 and !sh.shapeEql(shape, Dest.shape)) + @compileError("Shape mismatch in to: destination type must have the identical shape, or be a scalar."); + + const vec = if (comptime total == 1 and Dest.total != 1) + try Dest.splat(alloc, self.data[0]) + else + try Dest.load(alloc, @ptrCast(self.data)); + + const ratio = comptime (scales.getFactor(dims) / Dest.scales.getFactor(Dest.dims)); + const DestT = Dest.ValueType; + const DestVec = @Vector(Dest.total, DestT); + + if (comptime ratio == 1.0 and T == DestT) + return self.*; + + // If ratio is 1, handle type conversion correctly based on BOTH source and dest types + if (comptime ratio == 1.0) { + const T_info = @typeInfo(T); + const Dest_info = @typeInfo(DestT); + + vec.data = if (comptime T_info == .int and Dest_info == .int) + @as(DestVec, @intCast(vec.data)) + else if (comptime T_info == .float and Dest_info == .float) + @as(DestVec, @floatCast(vec.data)) + else if (comptime T_info == .int and Dest_info == .float) + @as(DestVec, @floatFromInt(vec.data)) + else if (comptime T_info == .float and Dest_info == .int) + @as(DestVec, @intFromFloat(vec.data)) + else + unreachable; + + return vec; + } + + if (comptime T == DestT) { + if (comptime @typeInfo(T) == .float) { + vec.data = vec.data * @as(DestVec, @splat(@as(T, @floatCast(ratio)))); + return vec; + } + + if (comptime ratio >= 1.0) { + const mult: T = comptime @intFromFloat(@round(ratio)); + vec.data.* = vec.data.* *| @as(Vec, @splat(mult)); + return vec; + } else { + const div_val: T = comptime @intFromFloat(@round(1.0 / ratio)); + const half: T = comptime @divTrunc(div_val, 2); + + if (comptime @typeInfo(T).int.signedness == .unsigned) { + vec.data = @divTrunc(vec.data + @as(Vec, @splat(half)), @as(Vec, @splat(div_val))); + } else { + // Vectorized branchless negative handling + const is_pos = self.data >= @as(Vec, @splat(0)); + const offsets = @select(T, is_pos, @as(Vec, @splat(half)), @as(Vec, @splat(-half))); + vec.data = @divTrunc(vec.data + offsets, @as(Vec, @splat(div_val))); + } + return vec; + } + } + + // Cross-type fully vectorized casting with scales + const FVec = @Vector(total, f64); + const float_vec: FVec = switch (comptime @typeInfo(T)) { + .float => @floatCast(vec.data), + .int => @floatFromInt(vec.data), + else => unreachable, + }; + + const scaled = float_vec * @as(FVec, @splat(ratio)); + + vec.data = switch (comptime @typeInfo(DestT)) { + .float => @floatCast(scaled), + .int => @intFromFloat(@round(scaled)), + else => unreachable, + }; + return vec; + } + + const CmpResult = if (total == 1) bool else [total]bool; + + inline fn cmpResult(v: @Vector(total, bool)) CmpResult { + return if (comptime total == 1) @reduce(.And, v) else @as([total]bool, v); + } + + /// Resolve both sides to the finer scale, broadcasting shape {1} RHS if needed. + inline fn resolveScalePair(self: *const Self, alloc: Allocator, rhs: anytype) !struct { + l: Tensor(T, dims.argsOpt(), sh.finerScales(Self, @TypeOf(rhs)).argsOpt(), shape), + r: Tensor(T, dims.argsOpt(), sh.finerScales(Self, @TypeOf(rhs)).argsOpt(), shape), + + fn deinit(s: @This(), a: Allocator) void { + s.l.deinit(a); + s.r.deinit(a); + } + } { + const RhsType = @TypeOf(rhs); + if (comptime !sh.isTensor(RhsType)) + @compileError("rhs can only be a Tensor "); + if (comptime RhsType.total != 1 and !sh.shapeEql(shape, RhsType.shape)) + @compileError("Shape mismatch in comparison: element-wise operations require identical shapes, or a scalar RHS."); + + const TargetType = Tensor(T, dims.argsOpt(), sh.finerScales(Self, RhsType).argsOpt(), shape); + return .{ .l = try self.to(TargetType, alloc), .r = try rhs.to(TargetType, alloc) }; + } + + pub inline fn eq(self: *const Self, alloc: Allocator, rhs: anytype) !CmpResult { + if (comptime !dims.eql(@TypeOf(rhs).dims)) + @compileError("Dimension mismatch in eq."); + const p = try resolveScalePair(self, alloc, rhs); + defer p.deinit(alloc); + return cmpResult(p.l.data.* == p.r.data.*); + } + + pub inline fn ne(self: *const Self, rhs: anytype) CmpResult { + if (comptime !dims.eql(@TypeOf(rhs).dims)) + @compileError("Dimension mismatch in ne."); + const p = resolveScalePair(self, rhs); + return cmpResult(p.l != p.r); + } + + pub inline fn gt(self: *const Self, rhs: anytype) CmpResult { + if (comptime !dims.eql(@TypeOf(rhs).dims)) + @compileError("Dimension mismatch in gt."); + const p = resolveScalePair(self, rhs); + return cmpResult(p.l > p.r); + } + + pub inline fn gte(self: *const Self, rhs: anytype) CmpResult { + if (comptime !dims.eql(@TypeOf(rhs).dims)) + @compileError("Dimension mismatch in gte."); + const p = resolveScalePair(self, rhs); + return cmpResult(p.l >= p.r); + } + + pub inline fn lt(self: *const Self, rhs: anytype) CmpResult { + if (comptime !dims.eql(@TypeOf(rhs).dims)) + @compileError("Dimension mismatch in lt."); + const p = resolveScalePair(self, rhs); + return cmpResult(p.l < p.r); + } + + pub inline fn lte(self: *const Self, rhs: anytype) CmpResult { + if (comptime !dims.eql(@TypeOf(rhs).dims)) + @compileError("Dimension mismatch in lte."); + const p = resolveScalePair(self, rhs); + return cmpResult(p.l <= p.r); + } + + /// True iff every element is equal after scale resolution. + pub inline fn eqAll(self: *const Self, other: anytype) bool { + if (comptime !dims.eql(@TypeOf(other).dims)) + @compileError("Dimension mismatch in eqAll."); + const p = resolveScalePair(self, other); + return @reduce(.And, p.l == p.r); + } + + /// True iff any element differs after scale resolution. + pub inline fn neAll(self: *const Self, other: anytype) bool { + return !self.eqAll(other); + } + }; +} + +// ═════════════════════════════════════════════════════════════════════════════ +// Tests +// ───────────────────────────────────────────────────────────────────────────── + +// ─── Scalar tests ───────────────────────────────────────────────────────── + +test "TensorAlloc | Scalar initiat" { + const alloc = std.testing.allocator; + + const Meter = Tensor(i128, .{ .L = 1 }, .{ .L = @enumFromInt(-3) }, &.{1}); + const Second = Tensor(f32, .{ .T = 1 }, .{ .T = .n }, &.{1}); + + const distance = try Meter.splat(alloc, 10); + defer distance.deinit(alloc); + const time = try Second.splat(alloc, 2); + defer time.deinit(alloc); + + try std.testing.expectEqual(10, distance.data[0]); + try std.testing.expectEqual(2, time.data[0]); +} + +test "TensorAlloc | Scalar comparisons (eq, ne, gt, gte, lt, lte)" { + var arena = std.heap.ArenaAllocator.init(std.testing.allocator); + defer arena.deinit(); + const alloc = arena.allocator(); + + const Meter = Tensor(i128, .{ .L = 1 }, .{}, &.{1}); + const KiloMeter = Tensor(i128, .{ .L = 1 }, .{ .L = .k }, &.{1}); + + const m1000 = try Meter.splat(alloc, 1000); + const km1 = try KiloMeter.splat(alloc, 1); + // const km2 = try KiloMeter.splat(alloc, 2); + + try std.testing.expect(try m1000.eq(alloc, km1)); + try std.testing.expect(try km1.eq(alloc, m1000)); + // try std.testing.expect(km2.ne(m1000)); + // + // try std.testing.expect(km2.gt(m1000)); + // try std.testing.expect(km2.gt(km1)); + // try std.testing.expect(km1.gte(m1000)); + // try std.testing.expect(km2.gte(m1000)); + // + // try std.testing.expect(m1000.lt(km2)); + // try std.testing.expect(km1.lt(km2)); + // try std.testing.expect(km1.lte(m1000)); + // try std.testing.expect(m1000.lte(km2)); +} + +// test "TensorAlloc | Scalar Add" { +// const Meter = Tensor(i128, .{ .L = 1 }, .{}, &.{1}); +// const KiloMeter = Tensor(i128, .{ .L = 1 }, .{ .L = .k }, &.{1}); +// const KiloMeter_f = Tensor(f64, .{ .L = 1 }, .{ .L = .k }, &.{1}); +// +// const distance = Meter.splat(10); +// const distance2 = Meter.splat(20); +// const added = distance.add(distance2); +// try std.testing.expectEqual(30, added.data[0]); +// try std.testing.expectEqual(1, @TypeOf(added).dims.get(.L)); +// +// const distance3 = KiloMeter.splat(2); +// const added2 = distance.add(distance3); +// try std.testing.expectEqual(2010, added2.data[0]); +// +// const added3 = distance3.add(distance).to(KiloMeter); +// try std.testing.expectEqual(2, added3.data[0]); +// +// const distance4 = KiloMeter_f.splat(2); +// const added4 = distance4.add(distance).to(KiloMeter_f); +// try std.testing.expectApproxEqAbs(2.01, added4.data[0], 0.000001); +// } +// +// test "TensorAlloc | Scalar Sub" { +// const Meter = Tensor(i128, .{ .L = 1 }, .{}, &.{1}); +// const KiloMeter_f = Tensor(f64, .{ .L = 1 }, .{ .L = .k }, &.{1}); +// +// const a = Meter.splat(500); +// const b = Meter.splat(200); +// const diff = a.sub(b); +// try std.testing.expectEqual(300, diff.data[0]); +// const diff2 = b.sub(a); +// try std.testing.expectEqual(-300, diff2.data[0]); +// +// const km_f = KiloMeter_f.splat(2.5); +// const m_f = Meter.splat(500); +// const diff3 = km_f.sub(m_f); +// try std.testing.expectApproxEqAbs(2000, diff3.data[0], 1e-4); +// } +// +// test "TensorAlloc | Scalar MulBy" { +// const Meter = Tensor(i128, .{ .L = 1 }, .{}, &.{1}); +// const Second = Tensor(f32, .{ .T = 1 }, .{}, &.{1}); +// +// const d = Meter.splat(3); +// const t = Second.splat(4); +// const at = d.mul(t); +// try std.testing.expectEqual(12, at.data[0]); +// try std.testing.expectEqual(1, @TypeOf(at).dims.get(.L)); +// try std.testing.expectEqual(1, @TypeOf(at).dims.get(.T)); +// +// const d2 = Meter.splat(5); +// const area = d.mul(d2); +// try std.testing.expectEqual(15, area.data[0]); +// try std.testing.expectEqual(2, @TypeOf(area).dims.get(.L)); +// } +// +// test "TensorAlloc | Scalar MulBy with scale" { +// const KiloMeter = Tensor(f32, .{ .L = 1 }, .{ .L = .k }, &.{1}); +// const KiloGram = Tensor(f32, .{ .M = 1 }, .{ .M = .k }, &.{1}); +// +// const dist = KiloMeter.splat(2.0); +// const mass = KiloGram.splat(3.0); +// const prod = dist.mul(mass); +// try std.testing.expectEqual(1, @TypeOf(prod).dims.get(.L)); +// try std.testing.expectEqual(1, @TypeOf(prod).dims.get(.M)); +// } +// +// test "TensorAlloc | Scalar MulBy with type change" { +// const Meter = Tensor(i128, .{ .L = 1 }, .{ .L = .k }, &.{1}); +// const Second = Tensor(f64, .{ .T = 1 }, .{}, &.{1}); +// const KmSec = Tensor(i64, .{ .L = 1, .T = 1 }, .{ .L = .k }, &.{1}); +// const KmSec_f = Tensor(f32, .{ .L = 1, .T = 1 }, .{ .L = .k }, &.{1}); +// +// const d = Meter.splat(3); +// const t = Second.splat(4); +// +// try std.testing.expectEqual(12, d.mul(t).to(KmSec).data[0]); +// try std.testing.expectApproxEqAbs(12.0, d.mul(t).to(KmSec_f).data[0], 0.0001); +// } +// +// test "TensorAlloc | Scalar MulBy small" { +// const Meter = Tensor(i128, .{ .L = 1 }, .{ .L = .n }, &.{1}); +// const Second = Tensor(f32, .{ .T = 1 }, .{}, &.{1}); +// const d = Meter.splat(3); +// const t = Second.splat(4); +// try std.testing.expectEqual(12, d.mul(t).data[0]); +// } +// +// test "TensorAlloc | Scalar MulBy dimensionless" { +// const DimLess = Tensor(i128, .{}, .{}, &.{1}); +// const Meter = Tensor(i128, .{ .L = 1 }, .{}, &.{1}); +// const d = Meter.splat(7); +// const scaled = d.mul(DimLess.splat(3)); +// try std.testing.expectEqual(21, scaled.data[0]); +// } +// +// test "TensorAlloc | Scalar Sqrt" { +// const MeterSquare = Tensor(i128, .{ .L = 2 }, .{}, &.{1}); +// const MeterSquare_f = Tensor(f64, .{ .L = 2 }, .{}, &.{1}); +// +// var d = MeterSquare.splat(9); +// var scaled = d.sqrt(); +// try std.testing.expectEqual(3, scaled.data[0]); +// try std.testing.expectEqual(1, @TypeOf(scaled).dims.get(.L)); +// +// d = MeterSquare.splat(-5); +// scaled = d.sqrt(); +// try std.testing.expectEqual(0, scaled.data[0]); +// +// const d2 = MeterSquare_f.splat(20); +// const scaled2 = d2.sqrt(); +// try std.testing.expectApproxEqAbs(4.472135955, scaled2.data[0], 1e-4); +// } +// +// test "TensorAlloc | Scalar Chained: velocity and acceleration" { +// const Meter = Tensor(i128, .{ .L = 1 }, .{}, &.{1}); +// const Second = Tensor(f32, .{ .T = 1 }, .{}, &.{1}); +// +// const dist = Meter.splat(100); +// const t1 = Second.splat(5); +// const velocity = dist.div(t1); +// try std.testing.expectEqual(20, velocity.data[0]); +// +// const t2 = Second.splat(4); +// const accel = velocity.div(t2); +// try std.testing.expectEqual(5, accel.data[0]); +// } +// +// test "TensorAlloc | Scalar DivBy integer exact" { +// const Meter = Tensor(i128, .{ .L = 1 }, .{}, &.{1}); +// const Second = Tensor(f32, .{ .T = 1 }, .{}, &.{1}); +// +// const dist = Meter.splat(120); +// const time = Second.splat(4); +// const vel = dist.div(time); +// try std.testing.expectEqual(30, vel.data[0]); +// } +// +// test "TensorAlloc | Scalar Finer scales skip dim 0" { +// const Dimless = Tensor(i128, .{}, .{}, &.{1}); +// const KiloMetre = Tensor(i128, .{ .L = 1 }, .{ .L = .k }, &.{1}); +// +// const r = Dimless.splat(30); +// const km = KiloMetre.splat(4); +// const vel = r.mul(km); +// try std.testing.expectEqual(120, vel.data[0]); +// try std.testing.expectEqual(Scales.UnitScale.k, @TypeOf(vel).scales.get(.L)); +// } +// +// test "TensorAlloc | Scalar Conversion chain: km -> m -> cm" { +// const KiloMeter = Tensor(i128, .{ .L = 1 }, .{ .L = .k }, &.{1}); +// const Meter = Tensor(i128, .{ .L = 1 }, .{}, &.{1}); +// const CentiMeter = Tensor(i128, .{ .L = 1 }, .{ .L = .c }, &.{1}); +// +// const km = KiloMeter.splat(15); +// const m = km.to(Meter); +// const cm = m.to(CentiMeter); +// try std.testing.expectEqual(15_000, m.data[0]); +// try std.testing.expectEqual(1_500_000, cm.data[0]); +// } +// +// test "TensorAlloc | Scalar Conversion: hours -> minutes -> seconds" { +// const Hour = Tensor(i128, .{ .T = 1 }, .{ .T = .hour }, &.{1}); +// const Minute = Tensor(i128, .{ .T = 1 }, .{ .T = .min }, &.{1}); +// const Second = Tensor(i128, .{ .T = 1 }, .{}, &.{1}); +// +// const h = Hour.splat(1); +// const min = h.to(Minute); +// const sec = min.to(Second); +// try std.testing.expectEqual(60, min.data[0]); +// try std.testing.expectEqual(3600, sec.data[0]); +// } +// +// test "TensorAlloc | Scalar Format" { +// const MeterPerSecondSq = Tensor(f32, .{ .L = 1, .T = -2 }, .{ .T = .n }, &.{1}); +// const Meter = Tensor(f32, .{ .L = 1 }, .{}, &.{1}); +// +// const m = Meter.splat(1.23456); +// const accel = MeterPerSecondSq.splat(9.81); +// +// var buf: [64]u8 = undefined; +// var res = try std.fmt.bufPrint(&buf, "{d:.2}", .{m}); +// try std.testing.expectEqualStrings("1.23m", res); +// +// res = try std.fmt.bufPrint(&buf, "{d}", .{accel}); +// try std.testing.expectEqualStrings("9.81m.ns⁻²", res); +// } +// +// test "TensorAlloc | Scalar Abs" { +// const Meter = Tensor(i128, .{ .L = 1 }, .{}, &.{1}); +// const MeterF = Tensor(f32, .{ .L = 1 }, .{}, &.{1}); +// +// try std.testing.expectEqual(50, Meter.splat(-50).abs().data[0]); +// try std.testing.expectEqual(42.5, MeterF.splat(-42.5).abs().data[0]); +// } +// +// test "TensorAlloc | Scalar Pow" { +// const Meter = Tensor(i128, .{ .L = 1 }, .{}, &.{1}); +// const d = Meter.splat(4); +// try std.testing.expectEqual(16, d.pow(2).data[0]); +// try std.testing.expectEqual(64, d.pow(3).data[0]); +// } +// +// test "TensorAlloc | Scalar add/sub bare number on dimensionless scalar" { +// const DimLess = Tensor(i128, .{}, .{}, &.{1}); +// const a = DimLess.splat(10); +// try std.testing.expectEqual(15, a.add(DimLess.splat(5)).data[0]); +// try std.testing.expectEqual(7, a.sub(DimLess.splat(3)).data[0]); +// } +// +// test "TensorAlloc | Scalar Imperial length scales" { +// const Foot = Tensor(f64, .{ .L = 1 }, .{ .L = .ft }, &.{1}); +// const Meter = Tensor(f64, .{ .L = 1 }, .{}, &.{1}); +// const Inch = Tensor(f64, .{ .L = 1 }, .{ .L = .inch }, &.{1}); +// +// try std.testing.expectApproxEqAbs(0.3048, Foot.splat(1.0).to(Meter).data[0], 1e-9); +// try std.testing.expectApproxEqAbs(1.0, Inch.splat(12.0).to(Foot).data[0], 1e-9); +// } +// +// test "TensorAlloc | Scalar Imperial mass scales" { +// const Pound = Tensor(f64, .{ .M = 1 }, .{ .M = .lb }, &.{1}); +// const Ounce = Tensor(f64, .{ .M = 1 }, .{ .M = .oz }, &.{1}); +// +// const total = Pound.splat(2.0).add(Ounce.splat(8.0)).to(Pound); +// try std.testing.expectApproxEqAbs(2.5, total.data[0], 1e-6); +// } +// +// // ─── Vector / Tensor tests ──────────────────────────────────────────────── +// +// test "TensorAlloc | Vector initiate" { +// const Meter4 = Tensor(f32, .{ .L = 1 }, .{}, &.{4}); +// const m = Meter4.splat(1); +// try std.testing.expect(m.data[0] == 1); +// try std.testing.expect(m.data[3] == 1); +// } +// +// test "TensorAlloc | Vector format" { +// const MeterPerSecondSq = Tensor(f32, .{ .L = 1, .T = -2 }, .{ .T = .n }, &.{3}); +// const KgMeterPerSecond = Tensor(f32, .{ .M = 1, .L = 1, .T = -1 }, .{ .M = .k }, &.{3}); +// +// const accel = MeterPerSecondSq.splat(9.81); +// const momentum = KgMeterPerSecond{ .data = .{ 43, 0, 11 } }; +// +// var buf: [64]u8 = undefined; +// var res = try std.fmt.bufPrint(&buf, "{d}", .{accel}); +// try std.testing.expectEqualStrings("(9.81, 9.81, 9.81)m.ns⁻²", res); +// +// res = try std.fmt.bufPrint(&buf, "{d:.2}", .{momentum}); +// try std.testing.expectEqualStrings("(43.00, 0.00, 11.00)m.kg.s⁻¹", res); +// } +// +// test "TensorAlloc | Vector Vec3 Init and Basic Arithmetic" { +// const Meter3 = Tensor(i32, .{ .L = 1 }, .{}, &.{3}); +// +// const v_zero = Meter3.zero; +// try std.testing.expectEqual(0, v_zero.data[0]); +// try std.testing.expectEqual(0, v_zero.data[2]); +// +// const v_one = Meter3.one; +// try std.testing.expectEqual(1, v_one.data[0]); +// +// const v_def = Meter3.splat(5); +// try std.testing.expectEqual(5, v_def.data[2]); +// +// const v1 = Meter3{ .data = .{ 10, 20, 30 } }; +// const v2 = Meter3{ .data = .{ 2, 4, 6 } }; +// +// const added = v1.add(v2); +// try std.testing.expectEqual(12, added.data[0]); +// try std.testing.expectEqual(24, added.data[1]); +// try std.testing.expectEqual(36, added.data[2]); +// +// const subbed = v1.sub(v2); +// try std.testing.expectEqual(8, subbed.data[0]); +// try std.testing.expectEqual(16, subbed.data[1]); +// try std.testing.expectEqual(24, subbed.data[2]); +// +// const neg = v1.negate(); +// try std.testing.expectEqual(-10, neg.data[0]); +// try std.testing.expectEqual(-20, neg.data[1]); +// try std.testing.expectEqual(-30, neg.data[2]); +// } +// +// test "TensorAlloc | Vector Kinematics (scalar mul/div broadcast)" { +// const Meter3 = Tensor(i32, .{ .L = 1 }, .{}, &.{3}); +// const Second1 = Tensor(i32, .{ .T = 1 }, .{}, &.{1}); +// +// const pos = Meter3{ .data = .{ 100, 200, 300 } }; +// const time = Second1.splat(10); +// +// const vel = pos.div(time); +// try std.testing.expectEqual(10, vel.data[0]); +// try std.testing.expectEqual(20, vel.data[1]); +// try std.testing.expectEqual(30, vel.data[2]); +// try std.testing.expectEqual(1, @TypeOf(vel).dims.get(.L)); +// try std.testing.expectEqual(-1, @TypeOf(vel).dims.get(.T)); +// +// const new_pos = vel.mul(time); +// try std.testing.expectEqual(100, new_pos.data[0]); +// try std.testing.expectEqual(0, @TypeOf(new_pos).dims.get(.T)); +// } +// +// test "TensorAlloc | Vector Element-wise Math and Scaling" { +// const Meter3 = Tensor(i32, .{ .L = 1 }, .{}, &.{3}); +// +// const v1 = Meter3{ .data = .{ 10, 20, 30 } }; +// const v2 = Meter3{ .data = .{ 2, 5, 10 } }; +// const dv = v1.div(v2); +// try std.testing.expectEqual(5, dv.data[0]); +// try std.testing.expectEqual(4, dv.data[1]); +// try std.testing.expectEqual(3, dv.data[2]); +// try std.testing.expectEqual(0, @TypeOf(dv).dims.get(.L)); +// } +// +// test "TensorAlloc | Vector Conversions" { +// const KiloMeter3 = Tensor(i32, .{ .L = 1 }, .{ .L = .k }, &.{3}); +// const Meter3 = Tensor(i32, .{ .L = 1 }, .{}, &.{3}); +// +// const v_km = KiloMeter3{ .data = .{ 1, 2, 3 } }; +// const v_m = v_km.to(Meter3); +// try std.testing.expectEqual(1000, v_m.data[0]); +// try std.testing.expectEqual(2000, v_m.data[1]); +// try std.testing.expectEqual(3000, v_m.data[2]); +// try std.testing.expectEqual(UnitScale.none, @TypeOf(v_m).scales.get(.L)); +// } +// +// test "TensorAlloc | Vector Length" { +// const MeterInt3 = Tensor(i32, .{ .L = 1 }, .{}, &.{3}); +// const MeterFloat3 = Tensor(f32, .{ .L = 1 }, .{}, &.{3}); +// +// const v_int = MeterInt3{ .data = .{ 3, 4, 0 } }; +// try std.testing.expectEqual(25, v_int.lengthSqr()); +// try std.testing.expectEqual(5, v_int.length()); +// +// const v_float = MeterFloat3{ .data = .{ 3.0, 4.0, 0.0 } }; +// try std.testing.expectApproxEqAbs(@as(f32, 25.0), v_float.lengthSqr(), 1e-4); +// try std.testing.expectApproxEqAbs(@as(f32, 5.0), v_float.length(), 1e-4); +// } +// +// test "TensorAlloc | Vector Comparisons" { +// const Meter3 = Tensor(f32, .{ .L = 1 }, .{}, &.{3}); +// const KiloMeter3 = Tensor(f32, .{ .L = 1 }, .{ .L = .k }, &.{3}); +// +// const v1 = Meter3{ .data = .{ 1000.0, 500.0, 0.0 } }; +// const v2 = KiloMeter3{ .data = .{ 1.0, 0.5, 0.0 } }; +// const v3 = KiloMeter3{ .data = .{ 1.0, 0.6, 0.0 } }; +// +// try std.testing.expect(v1.eqAll(v2)); +// try std.testing.expect(v1.neAll(v3)); +// +// const higher = v3.gt(v1); +// try std.testing.expectEqual(false, higher[0]); +// try std.testing.expectEqual(true, higher[1]); +// try std.testing.expectEqual(false, higher[2]); +// +// const equal = v3.eq(v1); +// try std.testing.expectEqual(true, equal[0]); +// try std.testing.expectEqual(false, equal[1]); +// try std.testing.expectEqual(true, equal[2]); +// +// const low_eq = v1.lte(v3); +// try std.testing.expect(low_eq[0] and low_eq[1] and low_eq[2]); +// } +// +// test "TensorAlloc | Vector vs Scalar broadcast comparison" { +// const Meter3 = Tensor(f32, .{ .L = 1 }, .{}, &.{3}); +// const KiloMeter1 = Tensor(f32, .{ .L = 1 }, .{ .L = .k }, &.{1}); +// +// const positions = Meter3{ .data = .{ 500.0, 1200.0, 3000.0 } }; +// const threshold = KiloMeter1.splat(1); // 1 km = 1000 m +// +// const exceeded = positions.gt(threshold); +// try std.testing.expectEqual(false, exceeded[0]); +// try std.testing.expectEqual(true, exceeded[1]); +// try std.testing.expectEqual(true, exceeded[2]); +// +// const Meter1 = Tensor(f32, .{ .L = 1 }, .{}, &.{1}); +// const exact = positions.eq(Meter1.splat(500)); +// try std.testing.expect(exact[0] == true); +// try std.testing.expect(exact[1] == false); +// } +// +// test "TensorAlloc | Vector contract — dot product (rank-1 * rank-1)" { +// const Meter3 = Tensor(f32, .{ .L = 1 }, .{}, &.{3}); +// const Newton3 = Tensor(f32, .{ .M = 1, .L = 1, .T = -2 }, .{}, &.{3}); +// +// const pos = Meter3{ .data = .{ 10.0, 0.0, 0.0 } }; +// const force = Newton3{ .data = .{ 5.0, 5.0, 0.0 } }; +// +// const work = force.contract(pos, 0, 0); +// try std.testing.expectEqual(50.0, work.data[0]); +// try std.testing.expectEqual(1, @TypeOf(work).dims.get(.M)); +// try std.testing.expectEqual(2, @TypeOf(work).dims.get(.L)); +// try std.testing.expectEqual(-2, @TypeOf(work).dims.get(.T)); +// } +// +// test "TensorAlloc | Vector contract — matrix multiply (rank-2 * rank-2)" { +// const A = Tensor(f32, .{}, .{}, &.{ 2, 3 }); +// const B = Tensor(f32, .{}, .{}, &.{ 3, 2 }); +// +// const a = A{ .data = .{ 1, 2, 3, 4, 5, 6 } }; +// const b = B{ .data = .{ 7, 8, 9, 10, 11, 12 } }; +// +// const c = a.contract(b, 1, 0); +// try std.testing.expectEqual(58, c.data[Tensor(f32, .{}, .{}, &.{ 2, 2 }).idx(.{ 0, 0 })]); +// try std.testing.expectEqual(64, c.data[Tensor(f32, .{}, .{}, &.{ 2, 2 }).idx(.{ 0, 1 })]); +// try std.testing.expectEqual(139, c.data[Tensor(f32, .{}, .{}, &.{ 2, 2 }).idx(.{ 1, 0 })]); +// try std.testing.expectEqual(154, c.data[Tensor(f32, .{}, .{}, &.{ 2, 2 }).idx(.{ 1, 1 })]); +// } +// +// test "TensorAlloc | Vector Abs, Pow, Sqrt and Product" { +// const Meter3 = Tensor(f32, .{ .L = 1 }, .{}, &.{3}); +// +// const v1 = Meter3{ .data = .{ -2.0, 3.0, -4.0 } }; +// const v_abs = v1.abs(); +// try std.testing.expectEqual(2.0, v_abs.data[0]); +// try std.testing.expectEqual(4.0, v_abs.data[2]); +// +// const vol = v_abs.product(); +// try std.testing.expectEqual(24.0, vol.data[0]); +// try std.testing.expectEqual(3, @TypeOf(vol).dims.get(.L)); +// +// const area_vec = v_abs.pow(2); +// try std.testing.expectEqual(4.0, area_vec.data[0]); +// try std.testing.expectEqual(16.0, area_vec.data[2]); +// try std.testing.expectEqual(2, @TypeOf(area_vec).dims.get(.L)); +// +// const sqrted = area_vec.sqrt(); +// try std.testing.expectEqual(2, sqrted.data[0]); +// try std.testing.expectEqual(4, sqrted.data[2]); +// try std.testing.expectEqual(1, @TypeOf(sqrted).dims.get(.L)); +// } +// +// test "TensorAlloc | Vector eq broadcast on dimensionless" { +// const DimLess3 = Tensor(i32, .{}, .{}, &.{3}); +// const v = DimLess3{ .data = .{ 1, 2, 3 } }; +// +// const eq_res = v.eq(DimLess3.splat(2)); +// try std.testing.expectEqual(false, eq_res[0]); +// try std.testing.expectEqual(true, eq_res[1]); +// try std.testing.expectEqual(false, eq_res[2]); +// } +// +// test "TensorAlloc | Tensor idx helper and matrix access" { +// const Mat3x3 = Tensor(f32, .{}, .{}, &.{ 3, 3 }); +// var m: Mat3x3 = Mat3x3.zero; +// m.data[Mat3x3.idx(.{ 0, 0 })] = 1.0; +// m.data[Mat3x3.idx(.{ 1, 1 })] = 2.0; +// m.data[Mat3x3.idx(.{ 2, 2 })] = 3.0; +// +// try std.testing.expectEqual(1.0, m.data[0]); +// try std.testing.expectEqual(2.0, m.data[4]); +// try std.testing.expectEqual(3.0, m.data[8]); +// try std.testing.expectEqual(0.0, m.data[1]); +// } +// +// test "TensorAlloc | Tensor strides_arr correctness" { +// const T1 = Tensor(f32, .{}, .{}, &.{3}); +// const T2 = Tensor(f32, .{}, .{}, &.{ 3, 4 }); +// const T3 = Tensor(f32, .{}, .{}, &.{ 2, 3, 4 }); +// +// try std.testing.expectEqual(1, T1.strides_arr[0]); +// try std.testing.expectEqual(4, T2.strides_arr[0]); +// try std.testing.expectEqual(1, T2.strides_arr[1]); +// try std.testing.expectEqual(12, T3.strides_arr[0]); +// try std.testing.expectEqual(4, T3.strides_arr[1]); +// try std.testing.expectEqual(1, T3.strides_arr[2]); +// } +// +// test "TensorAlloc | Slice 1D basic" { +// const Vec = Tensor(i32, .{}, .{}, &.{5}); +// var v = Vec{ .data = .{ 10, 20, 30, 40, 50 } }; +// const s = v.slice(.{.{ .start = 1, .end = 4 }}); +// try std.testing.expectEqual(3, @TypeOf(s).total); +// try std.testing.expectEqual(20, s.data[0]); +// try std.testing.expectEqual(30, s.data[1]); +// try std.testing.expectEqual(40, s.data[2]); +// } +// +// test "TensorAlloc | Slice 1D full range" { +// const Vec = Tensor(f32, .{}, .{}, &.{4}); +// const v = Vec{ .data = .{ 1.0, 2.0, 3.0, 4.0 } }; +// const s = v.slice(.{.{ .start = 0, .end = 4 }}); +// try std.testing.expectEqual(4, @TypeOf(s).total); +// inline for (0..4) |i| try std.testing.expectEqual(v.data[i], s.data[i]); +// } +// +// test "TensorAlloc | Slice 1D single element" { +// const Vec = Tensor(i64, .{}, .{}, &.{6}); +// const v = Vec{ .data = .{ 5, 10, 15, 20, 25, 30 } }; +// const s = v.slice(.{.{ .start = 3, .end = 4 }}); +// try std.testing.expectEqual(1, @TypeOf(s).total); +// try std.testing.expectEqual(20, s.data[0]); +// } +// +// test "TensorAlloc | Slice 1D preserves dims and scales" { +// const Meter = Tensor(i128, .{ .L = 1 }, .{ .L = .k }, &.{5}); +// const v = Meter{ .data = .{ 1, 2, 3, 4, 5 } }; +// const s = v.slice(.{.{ .start = 0, .end = 3 }}); +// const S = @TypeOf(s); +// try std.testing.expectEqual(1, S.dims.get(.L)); +// try std.testing.expectEqual(Meter.scales.get(.L), S.scales.get(.L)); +// } +// +// test "TensorAlloc | Slice 2D rows" { +// const Mat = Tensor(i32, .{}, .{}, &.{ 4, 3 }); +// const m = Mat{ .data = .{ +// 1, 2, 3, +// 4, 5, 6, +// 7, 8, 9, +// 10, 11, 12, +// } }; +// // rows [1,3), all cols +// const s = m.slice(.{ .{ .start = 1, .end = 3 }, .{ .start = 0, .end = 3 } }); +// try std.testing.expectEqual(6, @TypeOf(s).total); +// try std.testing.expectEqual(4, s.data[0]); +// try std.testing.expectEqual(5, s.data[1]); +// try std.testing.expectEqual(6, s.data[2]); +// try std.testing.expectEqual(7, s.data[3]); +// try std.testing.expectEqual(8, s.data[4]); +// try std.testing.expectEqual(9, s.data[5]); +// } +// +// test "TensorAlloc | Slice 2D cols" { +// const Mat = Tensor(i32, .{}, .{}, &.{ 3, 4 }); +// const m = Mat{ .data = .{ +// 1, 2, 3, 4, +// 5, 6, 7, 8, +// 9, 10, 11, 12, +// } }; +// // all rows, cols [1,3) +// const s = m.slice(.{ .{ .start = 0, .end = 3 }, .{ .start = 1, .end = 3 } }); +// const S = @TypeOf(s); +// try std.testing.expectEqual(3, S.shape[0]); +// try std.testing.expectEqual(2, S.shape[1]); +// try std.testing.expectEqual(2, s.data[0]); +// try std.testing.expectEqual(3, s.data[1]); +// try std.testing.expectEqual(6, s.data[2]); +// try std.testing.expectEqual(7, s.data[3]); +// try std.testing.expectEqual(10, s.data[4]); +// try std.testing.expectEqual(11, s.data[5]); +// } +// +// test "TensorAlloc | Slice 2D subblock" { +// const Mat = Tensor(f64, .{}, .{}, &.{ 4, 4 }); +// const m = Mat{ .data = .{ +// 1, 2, 3, 4, +// 5, 6, 7, 8, +// 9, 10, 11, 12, +// 13, 14, 15, 16, +// } }; +// // centre 2x2 +// const s = m.slice(.{ .{ .start = 1, .end = 3 }, .{ .start = 1, .end = 3 } }); +// try std.testing.expectEqual(4, @TypeOf(s).total); +// try std.testing.expectApproxEqAbs(6.0, s.data[0], 1e-9); +// try std.testing.expectApproxEqAbs(7.0, s.data[1], 1e-9); +// try std.testing.expectApproxEqAbs(10.0, s.data[2], 1e-9); +// try std.testing.expectApproxEqAbs(11.0, s.data[3], 1e-9); +// } +// +// test "TensorAlloc | Slice then add" { +// const Meter = Tensor(i32, .{ .L = 1 }, .{}, &.{5}); +// const a = Meter{ .data = .{ 1, 2, 3, 4, 5 } }; +// const b = Meter{ .data = .{ 10, 20, 30, 40, 50 } }; +// const sa = a.slice(.{.{ .start = 0, .end = 3 }}); +// const sb = b.slice(.{.{ .start = 2, .end = 5 }}); +// const r = sa.add(sb); +// try std.testing.expectEqual(31, r.data[0]); // 1+30 +// try std.testing.expectEqual(42, r.data[1]); // 2+40 +// try std.testing.expectEqual(53, r.data[2]); // 3+50 +// } +// +// test "TensorAlloc | Slice then scale convert" { +// const KiloMeter = Tensor(i64, .{ .L = 1 }, .{ .L = .k }, &.{4}); +// const Meter = Tensor(i64, .{ .L = 1 }, .{}, &.{2}); +// const v = KiloMeter{ .data = .{ 1, 2, 3, 4 } }; +// const s = v.slice(.{.{ .start = 1, .end = 3 }}); // {2, 3} km +// const converted = s.to(Meter); +// try std.testing.expectEqual(2000, converted.data[0]); +// try std.testing.expectEqual(3000, converted.data[1]); +// } +// +// test "TensorAlloc | Slice 1D negative start" { +// const Vec = Tensor(i32, .{}, .{}, &.{5}); +// const v = Vec{ .data = .{ 10, 20, 30, 40, 50 } }; +// const s = v.slice(.{.{ .start = -3, .end = 5 }}); // [2,5) → 30,40,50 +// try std.testing.expectEqual(3, @TypeOf(s).total); +// try std.testing.expectEqual(30, s.data[0]); +// try std.testing.expectEqual(40, s.data[1]); +// try std.testing.expectEqual(50, s.data[2]); +// } +// +// test "TensorAlloc | Slice 1D negative end" { +// const Vec = Tensor(i32, .{}, .{}, &.{5}); +// const v = Vec{ .data = .{ 10, 20, 30, 40, 50 } }; +// const s = v.slice(.{.{ .start = 1, .end = -1 }}); // [1,4) → 20,30,40 +// try std.testing.expectEqual(3, @TypeOf(s).total); +// try std.testing.expectEqual(20, s.data[0]); +// try std.testing.expectEqual(30, s.data[1]); +// try std.testing.expectEqual(40, s.data[2]); +// } +// +// test "TensorAlloc | Slice 1D both negative" { +// const Vec = Tensor(i64, .{}, .{}, &.{6}); +// const v = Vec{ .data = .{ 5, 10, 15, 20, 25, 30 } }; +// const s = v.slice(.{.{ .start = -4, .end = -1 }}); // [2,5) → 15,20,25 +// try std.testing.expectEqual(3, @TypeOf(s).total); +// try std.testing.expectEqual(15, s.data[0]); +// try std.testing.expectEqual(20, s.data[1]); +// try std.testing.expectEqual(25, s.data[2]); +// } +// +// test "TensorAlloc | Slice 1D null start" { +// const Vec = Tensor(i32, .{}, .{}, &.{5}); +// const v = Vec{ .data = .{ 10, 20, 30, 40, 50 } }; +// const s = v.slice(.{.{ .end = -2 }}); // [:-2] → 10,20,30 +// try std.testing.expectEqual(3, @TypeOf(s).total); +// try std.testing.expectEqual(10, s.data[0]); +// try std.testing.expectEqual(20, s.data[1]); +// try std.testing.expectEqual(30, s.data[2]); +// } +// +// test "TensorAlloc | Slice 1D null end" { +// const Vec = Tensor(i32, .{}, .{}, &.{5}); +// const v = Vec{ .data = .{ 10, 20, 30, 40, 50 } }; +// const s = v.slice(.{.{ .start = -3 }}); // [-3:] → 30,40,50 +// try std.testing.expectEqual(3, @TypeOf(s).total); +// try std.testing.expectEqual(30, s.data[0]); +// try std.testing.expectEqual(40, s.data[1]); +// try std.testing.expectEqual(50, s.data[2]); +// } +// +// test "TensorAlloc | Slice 2D negative & null indices" { +// const Mat = Tensor(i32, .{}, .{}, &.{ 4, 4 }); +// const m = Mat{ .data = .{ +// 1, 2, 3, 4, +// 5, 6, 7, 8, +// 9, 10, 11, 12, +// 13, 14, 15, 16, +// } }; +// // last 2 rows, last 2 cols → same as subblock test [2,4)x[2,4) +// const s = m.slice(.{ .{ .start = -2, .end = 4 }, .{ .start = -2 } }); +// try std.testing.expectEqual(4, @TypeOf(s).total); +// try std.testing.expectEqual(11, s.data[0]); +// try std.testing.expectEqual(12, s.data[1]); +// try std.testing.expectEqual(15, s.data[2]); +// try std.testing.expectEqual(16, s.data[3]); +// } +// +// test "TensorAlloc | RLS heap basic" { +// const allocator = std.testing.allocator; +// const Meter = Tensor(f32, .{ .L = 1 }, .{}, &.{1}); +// +// const c = try allocator.create(Meter); +// defer allocator.destroy(c); +// c.* = Meter.splat(42.0); +// +// try std.testing.expectEqual(42.0, c.data[0]); +// } +// +// test "TensorAlloc | RLS heap add vec3" { +// const allocator = std.testing.allocator; +// const Meter = Tensor(f32, .{ .L = 1 }, .{}, &.{3}); +// +// const a = Meter{ .data = .{ 1.0, 2.0, 3.0 } }; +// const b = Meter{ .data = .{ 10.0, 20.0, 30.0 } }; +// +// const c = try allocator.create(@TypeOf(a.add(b))); +// defer allocator.destroy(c); +// c.* = a.add(b); // RLS: direct into heap +// +// try std.testing.expectEqual(11.0, c.data[0]); +// try std.testing.expectEqual(22.0, c.data[1]); +// try std.testing.expectEqual(33.0, c.data[2]); +// } +// +// test "TensorAlloc | RLS heap cross-scale add" { +// const allocator = std.testing.allocator; +// const Meter = Tensor(i64, .{ .L = 1 }, .{}, &.{1}); +// const KiloMeter = Tensor(i64, .{ .L = 1 }, .{ .L = .k }, &.{1}); +// +// const c = try allocator.create(@TypeOf(Meter.splat(0).add(KiloMeter.splat(0)))); +// defer allocator.destroy(c); +// c.* = Meter.splat(500).add(KiloMeter.splat(1)); // 500 + 1000 = 1500m +// +// try std.testing.expectEqual(1500, c.data[0]); +// } +// +// test "TensorAlloc | RLS heap matmul" { +// const allocator = std.testing.allocator; +// const A = Tensor(f32, .{}, .{}, &.{ 2, 3 }); +// const B = Tensor(f32, .{}, .{}, &.{ 3, 2 }); +// +// const a = A{ .data = .{ 1, 2, 3, 4, 5, 6 } }; +// const b = B{ .data = .{ 7, 8, 9, 10, 11, 12 } }; +// +// const c = try allocator.create(@TypeOf(a.contract(b, 1, 0))); +// defer allocator.destroy(c); +// c.* = a.contract(b, 1, 0); // RLS: direct into heap +// +// const R = Tensor(f32, .{}, .{}, &.{ 2, 2 }); +// try std.testing.expectEqual(58.0, c.data[R.idx(.{ 0, 0 })]); +// try std.testing.expectEqual(64.0, c.data[R.idx(.{ 0, 1 })]); +// try std.testing.expectEqual(139.0, c.data[R.idx(.{ 1, 0 })]); +// try std.testing.expectEqual(154.0, c.data[R.idx(.{ 1, 1 })]); +// } +// +// test "TensorAlloc | RLS heap big tensor (near 1MB limit)" { +// const allocator = std.testing.allocator; +// // 31249 * 32bits = 999_968 bits — just under compile error +// const BigVec = Tensor(f32, .{ .L = 1 }, .{}, &.{31249}); +// +// const a = try allocator.create(BigVec); +// defer allocator.destroy(a); +// a.* = BigVec.splat(1.0); +// +// const b = try allocator.create(BigVec); +// defer allocator.destroy(b); +// b.* = BigVec.splat(2.0); +// +// const c = try allocator.create(@TypeOf(a.add(b.*))); +// defer allocator.destroy(c); +// c.* = a.add(b.*); // RLS: ~125KB result direct into heap +// +// try std.testing.expectEqual(3.0, c.data[0]); +// try std.testing.expectEqual(3.0, c.data[31248]); +// } diff --git a/src/TensorStatic.zig b/src/TensorStatic.zig index 978ca78..6937c6f 100644 --- a/src/TensorStatic.zig +++ b/src/TensorStatic.zig @@ -37,7 +37,7 @@ pub fn Tensor( pub const total: comptime_int = _total; pub const strides_arr: [shape_.len]comptime_int = _strides; pub const ISTENSOR = true; - pub const TENSORSTATIC = true; + pub const TENSORKIND: sh.TensorKind = .static; /// Convert N-D coords (row-major) to flat index — fully comptime. /// Usage: Tensor.idx(.{row, col}) @@ -53,7 +53,7 @@ pub fn Tensor( } /// Broadcast a single value across all elements. - pub inline fn splat(v: T) Self { + pub fn splat(v: T) Self { return .{ .data = @splat(v) }; } diff --git a/src/shared.zig b/src/shared.zig index 4f2dfff..c0d9318 100644 --- a/src/shared.zig +++ b/src/shared.zig @@ -4,6 +4,8 @@ const UnitScale = Scales.UnitScale; const Dimensions = @import("Dimensions.zig"); const Dimension = Dimensions.Dimension; +pub const TensorKind = enum { static, alloc, gpu }; + pub fn isTensor(comptime T: type) bool { return comptime @typeInfo(T) == .@"struct" and @hasDecl(T, "ISTENSOR"); } diff --git a/src/test.zig b/src/test.zig index 94231ef..e6efb47 100644 --- a/src/test.zig +++ b/src/test.zig @@ -1,5 +1,6 @@ test { _ = @import("TensorStatic.zig"); + _ = @import("TensorAlloc.zig"); _ = @import("Dimensions.zig"); _ = @import("Scales.zig"); _ = @import("Base.zig");