Added everything to TensorAlloc

This commit is contained in:
adrien 2026-05-25 22:52:27 +02:00
parent ff21f0ac8b
commit ceff8ff1bd

View File

@ -301,6 +301,68 @@ pub fn Tensor(
return Self{ .data = vec_ptr }; return Self{ .data = vec_ptr };
} }
/// Extract sub-tensor by half-open ranges [start, end) per axis.
/// All bounds comptime. Dims and scales preserved.
/// Negative indices count from end: -1 = last element.
pub inline fn slice(
self: *const Self,
alloc: Allocator,
comptime ranges: [rank]struct { start: ?isize = null, end: ?isize = null },
) !blk: {
var ns: [rank]comptime_int = undefined;
for (0..rank) |i| {
const dim = @as(isize, @intCast(shape[i]));
const s: isize = blk2: {
const raw = ranges[i].start orelse 0;
break :blk2 if (raw < 0) raw + dim else raw;
};
const e: isize = blk2: {
const raw = ranges[i].end orelse dim;
break :blk2 if (raw < 0) raw + dim else raw;
};
if (s < 0) @compileError("slice: start out of bounds after normalization");
if (e < 0) @compileError("slice: end out of bounds after normalization");
if (s >= e) @compileError("slice: start must be < end");
if (e > dim) @compileError("slice: end exceeds shape");
ns[i] = e - s;
}
const new_shape: [rank]comptime_int = ns;
break :blk Tensor(T, dims.argsOpt(), scales.argsOpt(), &new_shape);
} {
const new_shape: [rank]comptime_int = comptime blk: {
var ns: [rank]comptime_int = undefined;
for (0..rank) |i| {
const dim = @as(isize, @intCast(shape[i]));
const raw_s = ranges[i].start orelse 0;
const raw_e = ranges[i].end orelse dim;
const s: isize = if (raw_s < 0) raw_s + dim else raw_s;
const e: isize = if (raw_e < 0) raw_e + dim else raw_e;
ns[i] = e - s;
}
break :blk ns;
};
const ResultType = Tensor(T, dims.argsOpt(), scales.argsOpt(), &new_shape);
const DestVec = @Vector(ResultType.total, T);
const src: [total]T = self.data.*;
var dst: [ResultType.total]T = undefined;
for (0..ResultType.total) |flat| {
var src_flat: usize = 0;
inline for (0..rank) |i| {
const dim = @as(isize, @intCast(shape[i]));
const raw_s = ranges[i].start orelse 0;
const s: isize = if (raw_s < 0) raw_s + dim else raw_s;
const coord = (flat / ResultType.strides_arr[i]) % new_shape[i];
src_flat += (coord + @as(usize, @intCast(s))) * strides_arr[i];
}
dst[flat] = src[src_flat];
}
const vec_ptr = try alloc.create(DestVec);
vec_ptr.* = dst;
return ResultType{ .data = vec_ptr };
}
/// Convert to a compatible Tensor type. /// Convert to a compatible Tensor type.
/// Dimension mismatch compile error. /// Dimension mismatch compile error.
/// Dest.shape must equal self.shape, or total == 1 -> splat to Dest shape (scalar pattern). /// Dest.shape must equal self.shape, or total == 1 -> splat to Dest shape (scalar pattern).
@ -1389,262 +1451,295 @@ test "TensorAlloc | Tensor strides_arr correctness" {
try std.testing.expectEqual(4, T3.strides_arr[1]); try std.testing.expectEqual(4, T3.strides_arr[1]);
try std.testing.expectEqual(1, T3.strides_arr[2]); try std.testing.expectEqual(1, T3.strides_arr[2]);
} }
//
// test "TensorAlloc | Slice 1D basic" { test "TensorAlloc | Slice 1D basic" {
// const Vec = Tensor(i32, .{}, .{}, &.{5}); var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
// var v = Vec{ .data = .{ 10, 20, 30, 40, 50 } }; defer arena.deinit();
// const s = v.slice(.{.{ .start = 1, .end = 4 }}); const alloc = arena.allocator();
// try std.testing.expectEqual(3, @TypeOf(s).total);
// try std.testing.expectEqual(20, s.data[0]); const Vec = Tensor(i32, .{}, .{}, &.{5});
// try std.testing.expectEqual(30, s.data[1]); var v = try Vec.load(alloc, &.{ 10, 20, 30, 40, 50 });
// try std.testing.expectEqual(40, s.data[2]); defer v.deinit(alloc);
// }
// const s = try v.slice(alloc, .{.{ .start = 1, .end = 4 }});
// test "TensorAlloc | Slice 1D full range" { defer s.deinit(alloc);
// const Vec = Tensor(f32, .{}, .{}, &.{4});
// const v = Vec{ .data = .{ 1.0, 2.0, 3.0, 4.0 } }; try std.testing.expectEqual(3, @TypeOf(s).total);
// const s = v.slice(.{.{ .start = 0, .end = 4 }}); try std.testing.expectEqual(20, s.data[0]);
// try std.testing.expectEqual(4, @TypeOf(s).total); try std.testing.expectEqual(30, s.data[1]);
// inline for (0..4) |i| try std.testing.expectEqual(v.data[i], s.data[i]); try std.testing.expectEqual(40, s.data[2]);
// } }
//
// test "TensorAlloc | Slice 1D single element" { test "TensorAlloc | Slice 1D full range" {
// const Vec = Tensor(i64, .{}, .{}, &.{6}); var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
// const v = Vec{ .data = .{ 5, 10, 15, 20, 25, 30 } }; defer arena.deinit();
// const s = v.slice(.{.{ .start = 3, .end = 4 }}); const alloc = arena.allocator();
// try std.testing.expectEqual(1, @TypeOf(s).total); const Vec = Tensor(f32, .{}, .{}, &.{4});
// try std.testing.expectEqual(20, s.data[0]); const v = try Vec.load(alloc, &.{ 1.0, 2.0, 3.0, 4.0 });
// } defer v.deinit(alloc);
//
// test "TensorAlloc | Slice 1D preserves dims and scales" { const s = try v.slice(alloc, .{.{ .start = 0, .end = 4 }});
// const Meter = Tensor(i128, .{ .L = 1 }, .{ .L = .k }, &.{5}); defer s.deinit(alloc);
// const v = Meter{ .data = .{ 1, 2, 3, 4, 5 } };
// const s = v.slice(.{.{ .start = 0, .end = 3 }}); try std.testing.expectEqual(4, @TypeOf(s).total);
// const S = @TypeOf(s); inline for (0..4) |i| try std.testing.expectEqual(v.data[i], s.data[i]);
// try std.testing.expectEqual(1, S.dims.get(.L)); }
// try std.testing.expectEqual(Meter.scales.get(.L), S.scales.get(.L));
// } test "TensorAlloc | Slice 1D single element" {
// var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
// test "TensorAlloc | Slice 2D rows" { defer arena.deinit();
// const Mat = Tensor(i32, .{}, .{}, &.{ 4, 3 }); const alloc = arena.allocator();
// const m = Mat{ .data = .{ const Vec = Tensor(i64, .{}, .{}, &.{6});
// 1, 2, 3, const v = try Vec.load(alloc, &.{ 5, 10, 15, 20, 25, 30 });
// 4, 5, 6, defer v.deinit(alloc);
// 7, 8, 9,
// 10, 11, 12, const s = try v.slice(alloc, .{.{ .start = 3, .end = 4 }});
// } }; defer s.deinit(alloc);
// // rows [1,3), all cols
// const s = m.slice(.{ .{ .start = 1, .end = 3 }, .{ .start = 0, .end = 3 } }); try std.testing.expectEqual(1, @TypeOf(s).total);
// try std.testing.expectEqual(6, @TypeOf(s).total); try std.testing.expectEqual(20, s.data[0]);
// try std.testing.expectEqual(4, s.data[0]); }
// try std.testing.expectEqual(5, s.data[1]);
// try std.testing.expectEqual(6, s.data[2]); test "TensorAlloc | Slice 1D preserves dims and scales" {
// try std.testing.expectEqual(7, s.data[3]); var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
// try std.testing.expectEqual(8, s.data[4]); defer arena.deinit();
// try std.testing.expectEqual(9, s.data[5]); const alloc = arena.allocator();
// } const Meter = Tensor(i128, .{ .L = 1 }, .{ .L = .k }, &.{5});
// const v = try Meter.load(alloc, &.{ 1, 2, 3, 4, 5 });
// test "TensorAlloc | Slice 2D cols" { defer v.deinit(alloc);
// const Mat = Tensor(i32, .{}, .{}, &.{ 3, 4 });
// const m = Mat{ .data = .{ const s = try v.slice(alloc, .{.{ .start = 0, .end = 3 }});
// 1, 2, 3, 4, defer s.deinit(alloc);
// 5, 6, 7, 8,
// 9, 10, 11, 12, const S = @TypeOf(s);
// } }; try std.testing.expectEqual(1, S.dims.get(.L));
// // all rows, cols [1,3) try std.testing.expectEqual(Meter.scales.get(.L), S.scales.get(.L));
// const s = m.slice(.{ .{ .start = 0, .end = 3 }, .{ .start = 1, .end = 3 } }); }
// const S = @TypeOf(s);
// try std.testing.expectEqual(3, S.shape[0]); test "TensorAlloc | Slice 2D rows" {
// try std.testing.expectEqual(2, S.shape[1]); var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
// try std.testing.expectEqual(2, s.data[0]); defer arena.deinit();
// try std.testing.expectEqual(3, s.data[1]); const alloc = arena.allocator();
// try std.testing.expectEqual(6, s.data[2]); const Mat = Tensor(i32, .{}, .{}, &.{ 4, 3 });
// try std.testing.expectEqual(7, s.data[3]); const m = try Mat.load(alloc, &.{
// try std.testing.expectEqual(10, s.data[4]); 1, 2, 3,
// try std.testing.expectEqual(11, s.data[5]); 4, 5, 6,
// } 7, 8, 9,
// 10, 11, 12,
// test "TensorAlloc | Slice 2D subblock" { });
// const Mat = Tensor(f64, .{}, .{}, &.{ 4, 4 }); defer m.deinit(alloc);
// const m = Mat{ .data = .{
// 1, 2, 3, 4, // rows [1,3), all cols
// 5, 6, 7, 8, const s = try m.slice(alloc, .{ .{ .start = 1, .end = 3 }, .{ .start = 0, .end = 3 } });
// 9, 10, 11, 12, defer s.deinit(alloc);
// 13, 14, 15, 16,
// } }; try std.testing.expectEqual(6, @TypeOf(s).total);
// // centre 2x2 try std.testing.expectEqual(4, s.data[0]);
// const s = m.slice(.{ .{ .start = 1, .end = 3 }, .{ .start = 1, .end = 3 } }); try std.testing.expectEqual(5, s.data[1]);
// try std.testing.expectEqual(4, @TypeOf(s).total); try std.testing.expectEqual(6, s.data[2]);
// try std.testing.expectApproxEqAbs(6.0, s.data[0], 1e-9); try std.testing.expectEqual(7, s.data[3]);
// try std.testing.expectApproxEqAbs(7.0, s.data[1], 1e-9); try std.testing.expectEqual(8, s.data[4]);
// try std.testing.expectApproxEqAbs(10.0, s.data[2], 1e-9); try std.testing.expectEqual(9, s.data[5]);
// try std.testing.expectApproxEqAbs(11.0, s.data[3], 1e-9); }
// }
// test "TensorAlloc | Slice 2D cols" {
// test "TensorAlloc | Slice then add" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
// const Meter = Tensor(i32, .{ .L = 1 }, .{}, &.{5}); defer arena.deinit();
// const a = Meter{ .data = .{ 1, 2, 3, 4, 5 } }; const alloc = arena.allocator();
// const b = Meter{ .data = .{ 10, 20, 30, 40, 50 } }; const Mat = Tensor(i32, .{}, .{}, &.{ 3, 4 });
// const sa = a.slice(.{.{ .start = 0, .end = 3 }}); const m = try Mat.load(alloc, &.{
// const sb = b.slice(.{.{ .start = 2, .end = 5 }}); 1, 2, 3, 4,
// const r = sa.add(sb); 5, 6, 7, 8,
// try std.testing.expectEqual(31, r.data[0]); // 1+30 9, 10, 11, 12,
// try std.testing.expectEqual(42, r.data[1]); // 2+40 });
// try std.testing.expectEqual(53, r.data[2]); // 3+50 defer m.deinit(alloc);
// }
// // all rows, cols [1,3)
// test "TensorAlloc | Slice then scale convert" { const s = try m.slice(alloc, .{ .{ .start = 0, .end = 3 }, .{ .start = 1, .end = 3 } });
// const KiloMeter = Tensor(i64, .{ .L = 1 }, .{ .L = .k }, &.{4}); defer s.deinit(alloc);
// const Meter = Tensor(i64, .{ .L = 1 }, .{}, &.{2});
// const v = KiloMeter{ .data = .{ 1, 2, 3, 4 } }; const S = @TypeOf(s);
// const s = v.slice(.{.{ .start = 1, .end = 3 }}); // {2, 3} km try std.testing.expectEqual(3, S.shape[0]);
// const converted = s.to(Meter); try std.testing.expectEqual(2, S.shape[1]);
// try std.testing.expectEqual(2000, converted.data[0]); try std.testing.expectEqual(2, s.data[0]);
// try std.testing.expectEqual(3000, converted.data[1]); try std.testing.expectEqual(3, s.data[1]);
// } try std.testing.expectEqual(6, s.data[2]);
// try std.testing.expectEqual(7, s.data[3]);
// test "TensorAlloc | Slice 1D negative start" { try std.testing.expectEqual(10, s.data[4]);
// const Vec = Tensor(i32, .{}, .{}, &.{5}); try std.testing.expectEqual(11, s.data[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); test "TensorAlloc | Slice 2D subblock" {
// try std.testing.expectEqual(30, s.data[0]); var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
// try std.testing.expectEqual(40, s.data[1]); defer arena.deinit();
// try std.testing.expectEqual(50, s.data[2]); const alloc = arena.allocator();
// } const Mat = Tensor(f64, .{}, .{}, &.{ 4, 4 });
// const m = try Mat.load(alloc, &.{
// test "TensorAlloc | Slice 1D negative end" { 1, 2, 3, 4,
// const Vec = Tensor(i32, .{}, .{}, &.{5}); 5, 6, 7, 8,
// const v = Vec{ .data = .{ 10, 20, 30, 40, 50 } }; 9, 10, 11, 12,
// const s = v.slice(.{.{ .start = 1, .end = -1 }}); // [1,4) 20,30,40 13, 14, 15, 16,
// try std.testing.expectEqual(3, @TypeOf(s).total); });
// try std.testing.expectEqual(20, s.data[0]); defer m.deinit(alloc);
// try std.testing.expectEqual(30, s.data[1]);
// try std.testing.expectEqual(40, s.data[2]); // centre 2x2
// } const s = try m.slice(alloc, .{ .{ .start = 1, .end = 3 }, .{ .start = 1, .end = 3 } });
// defer s.deinit(alloc);
// test "TensorAlloc | Slice 1D both negative" {
// const Vec = Tensor(i64, .{}, .{}, &.{6}); try std.testing.expectEqual(4, @TypeOf(s).total);
// const v = Vec{ .data = .{ 5, 10, 15, 20, 25, 30 } }; try std.testing.expectApproxEqAbs(6.0, s.data[0], 1e-9);
// const s = v.slice(.{.{ .start = -4, .end = -1 }}); // [2,5) 15,20,25 try std.testing.expectApproxEqAbs(7.0, s.data[1], 1e-9);
// try std.testing.expectEqual(3, @TypeOf(s).total); try std.testing.expectApproxEqAbs(10.0, s.data[2], 1e-9);
// try std.testing.expectEqual(15, s.data[0]); try std.testing.expectApproxEqAbs(11.0, s.data[3], 1e-9);
// try std.testing.expectEqual(20, s.data[1]); }
// try std.testing.expectEqual(25, s.data[2]);
// } test "TensorAlloc | Slice then add" {
// var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
// test "TensorAlloc | Slice 1D null start" { defer arena.deinit();
// const Vec = Tensor(i32, .{}, .{}, &.{5}); const alloc = arena.allocator();
// const v = Vec{ .data = .{ 10, 20, 30, 40, 50 } }; const Meter = Tensor(i32, .{ .L = 1 }, .{}, &.{5});
// const s = v.slice(.{.{ .end = -2 }}); // [:-2] 10,20,30 const a = try Meter.load(alloc, &.{ 1, 2, 3, 4, 5 });
// try std.testing.expectEqual(3, @TypeOf(s).total); defer a.deinit(alloc);
// try std.testing.expectEqual(10, s.data[0]); const b = try Meter.load(alloc, &.{ 10, 20, 30, 40, 50 });
// try std.testing.expectEqual(20, s.data[1]); defer b.deinit(alloc);
// try std.testing.expectEqual(30, s.data[2]);
// } const sa = try a.slice(alloc, .{.{ .start = 0, .end = 3 }});
// defer sa.deinit(alloc);
// test "TensorAlloc | Slice 1D null end" { const sb = try b.slice(alloc, .{.{ .start = 2, .end = 5 }});
// const Vec = Tensor(i32, .{}, .{}, &.{5}); defer sb.deinit(alloc);
// const v = Vec{ .data = .{ 10, 20, 30, 40, 50 } };
// const s = v.slice(.{.{ .start = -3 }}); // [-3:] 30,40,50 const r = try sa.add(alloc, sb);
// try std.testing.expectEqual(3, @TypeOf(s).total); defer r.deinit(alloc);
// try std.testing.expectEqual(30, s.data[0]);
// try std.testing.expectEqual(40, s.data[1]); try std.testing.expectEqual(31, r.data[0]); // 1+30
// try std.testing.expectEqual(50, s.data[2]); try std.testing.expectEqual(42, r.data[1]); // 2+40
// } try std.testing.expectEqual(53, r.data[2]); // 3+50
// }
// test "TensorAlloc | Slice 2D negative & null indices" {
// const Mat = Tensor(i32, .{}, .{}, &.{ 4, 4 }); test "TensorAlloc | Slice then scale convert" {
// const m = Mat{ .data = .{ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
// 1, 2, 3, 4, defer arena.deinit();
// 5, 6, 7, 8, const alloc = arena.allocator();
// 9, 10, 11, 12, const KiloMeter = Tensor(i64, .{ .L = 1 }, .{ .L = .k }, &.{4});
// 13, 14, 15, 16, const Meter = Tensor(i64, .{ .L = 1 }, .{}, &.{2});
// } }; const v = try KiloMeter.load(alloc, &.{ 1, 2, 3, 4 });
// // last 2 rows, last 2 cols same as subblock test [2,4)x[2,4) defer v.deinit(alloc);
// const s = m.slice(.{ .{ .start = -2, .end = 4 }, .{ .start = -2 } });
// try std.testing.expectEqual(4, @TypeOf(s).total); const s = try v.slice(alloc, .{.{ .start = 1, .end = 3 }}); // {2, 3} km
// try std.testing.expectEqual(11, s.data[0]); defer s.deinit(alloc);
// try std.testing.expectEqual(12, s.data[1]);
// try std.testing.expectEqual(15, s.data[2]); const converted = try s.to(alloc, Meter);
// try std.testing.expectEqual(16, s.data[3]); defer converted.deinit(alloc);
// }
// try std.testing.expectEqual(2000, converted.data[0]);
// test "TensorAlloc | RLS heap basic" { try std.testing.expectEqual(3000, converted.data[1]);
// const allocator = std.testing.allocator; }
// const Meter = Tensor(f32, .{ .L = 1 }, .{}, &.{1});
// test "TensorAlloc | Slice 1D negative start" {
// const c = try allocator.create(Meter); var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
// defer allocator.destroy(c); defer arena.deinit();
// c.* = Meter.splat(42.0); const alloc = arena.allocator();
// const Vec = Tensor(i32, .{}, .{}, &.{5});
// try std.testing.expectEqual(42.0, c.data[0]); const v = try Vec.load(alloc, &.{ 10, 20, 30, 40, 50 });
// } defer v.deinit(alloc);
//
// test "TensorAlloc | RLS heap add vec3" { const s = try v.slice(alloc, .{.{ .start = -3, .end = 5 }}); // [2,5) 30,40,50
// const allocator = std.testing.allocator; defer s.deinit(alloc);
// const Meter = Tensor(f32, .{ .L = 1 }, .{}, &.{3});
// try std.testing.expectEqual(3, @TypeOf(s).total);
// const a = Meter{ .data = .{ 1.0, 2.0, 3.0 } }; try std.testing.expectEqual(30, s.data[0]);
// const b = Meter{ .data = .{ 10.0, 20.0, 30.0 } }; try std.testing.expectEqual(40, s.data[1]);
// try std.testing.expectEqual(50, s.data[2]);
// const c = try allocator.create(@TypeOf(a.add(b))); }
// defer allocator.destroy(c);
// c.* = a.add(b); // RLS: direct into heap test "TensorAlloc | Slice 1D negative end" {
// var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
// try std.testing.expectEqual(11.0, c.data[0]); defer arena.deinit();
// try std.testing.expectEqual(22.0, c.data[1]); const alloc = arena.allocator();
// try std.testing.expectEqual(33.0, c.data[2]); const Vec = Tensor(i32, .{}, .{}, &.{5});
// } const v = try Vec.load(alloc, &.{ 10, 20, 30, 40, 50 });
// defer v.deinit(alloc);
// test "TensorAlloc | RLS heap cross-scale add" {
// const allocator = std.testing.allocator; const s = try v.slice(alloc, .{.{ .start = 1, .end = -1 }}); // [1,4) 20,30,40
// const Meter = Tensor(i64, .{ .L = 1 }, .{}, &.{1}); defer s.deinit(alloc);
// const KiloMeter = Tensor(i64, .{ .L = 1 }, .{ .L = .k }, &.{1});
// try std.testing.expectEqual(3, @TypeOf(s).total);
// const c = try allocator.create(@TypeOf(Meter.splat(0).add(KiloMeter.splat(0)))); try std.testing.expectEqual(20, s.data[0]);
// defer allocator.destroy(c); try std.testing.expectEqual(30, s.data[1]);
// c.* = Meter.splat(500).add(KiloMeter.splat(1)); // 500 + 1000 = 1500m try std.testing.expectEqual(40, s.data[2]);
// }
// try std.testing.expectEqual(1500, c.data[0]);
// } test "TensorAlloc | Slice 1D both negative" {
// var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
// test "TensorAlloc | RLS heap matmul" { defer arena.deinit();
// const allocator = std.testing.allocator; const alloc = arena.allocator();
// const A = Tensor(f32, .{}, .{}, &.{ 2, 3 }); const Vec = Tensor(i64, .{}, .{}, &.{6});
// const B = Tensor(f32, .{}, .{}, &.{ 3, 2 }); const v = try Vec.load(alloc, &.{ 5, 10, 15, 20, 25, 30 });
// defer v.deinit(alloc);
// const a = A{ .data = .{ 1, 2, 3, 4, 5, 6 } };
// const b = B{ .data = .{ 7, 8, 9, 10, 11, 12 } }; const s = try v.slice(alloc, .{.{ .start = -4, .end = -1 }}); // [2,5) 15,20,25
// defer s.deinit(alloc);
// const c = try allocator.create(@TypeOf(a.contract(b, 1, 0)));
// defer allocator.destroy(c); try std.testing.expectEqual(3, @TypeOf(s).total);
// c.* = a.contract(b, 1, 0); // RLS: direct into heap try std.testing.expectEqual(15, s.data[0]);
// try std.testing.expectEqual(20, s.data[1]);
// const R = Tensor(f32, .{}, .{}, &.{ 2, 2 }); try std.testing.expectEqual(25, s.data[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 })]); test "TensorAlloc | Slice 1D null start" {
// try std.testing.expectEqual(154.0, c.data[R.idx(.{ 1, 1 })]); var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
// } defer arena.deinit();
// const alloc = arena.allocator();
// test "TensorAlloc | RLS heap big tensor (near 1MB limit)" { const Vec = Tensor(i32, .{}, .{}, &.{5});
// const allocator = std.testing.allocator; const v = try Vec.load(alloc, &.{ 10, 20, 30, 40, 50 });
// // 31249 * 32bits = 999_968 bits just under compile error defer v.deinit(alloc);
// const BigVec = Tensor(f32, .{ .L = 1 }, .{}, &.{31249});
// const s = try v.slice(alloc, .{.{ .end = -2 }}); // [:-2] 10,20,30
// const a = try allocator.create(BigVec); defer s.deinit(alloc);
// defer allocator.destroy(a);
// a.* = BigVec.splat(1.0); try std.testing.expectEqual(3, @TypeOf(s).total);
// try std.testing.expectEqual(10, s.data[0]);
// const b = try allocator.create(BigVec); try std.testing.expectEqual(20, s.data[1]);
// defer allocator.destroy(b); try std.testing.expectEqual(30, s.data[2]);
// b.* = BigVec.splat(2.0); }
//
// const c = try allocator.create(@TypeOf(a.add(b.*))); test "TensorAlloc | Slice 1D null end" {
// defer allocator.destroy(c); var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
// c.* = a.add(b.*); // RLS: ~125KB result direct into heap defer arena.deinit();
// const alloc = arena.allocator();
// try std.testing.expectEqual(3.0, c.data[0]); const Vec = Tensor(i32, .{}, .{}, &.{5});
// try std.testing.expectEqual(3.0, c.data[31248]); const v = try Vec.load(alloc, &.{ 10, 20, 30, 40, 50 });
// } defer v.deinit(alloc);
const s = try v.slice(alloc, .{.{ .start = -3 }}); // [-3:] 30,40,50
defer s.deinit(alloc);
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" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const alloc = arena.allocator();
const Mat = Tensor(i32, .{}, .{}, &.{ 4, 4 });
const m = try Mat.load(alloc, &.{
1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16,
});
defer m.deinit(alloc);
// last 2 rows, last 2 cols same as subblock test [2,4)x[2,4)
const s = try m.slice(alloc, .{ .{ .start = -2, .end = 4 }, .{ .start = -2 } });
defer s.deinit(alloc);
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]);
}