tiny.accy.tensor.types.dim
Defined in tensor.types.
API (35)
Actions
Public operations.
Spec.initType.byteCountType.elementCountType.eqlType.extentType.findType.fromSpecType.initType.rankType.scalaraxisCountaxisNamescopyDimsdimCountdimsElementCountexpectExtentsextentsfillDimsfindDimfreeTestTypeisNameSlicesameDimssameTypespecspecDimsvalidateAuthoredDimsvalidateAuthoredNamevalidateDimsvalidateName
Types and contracts
Public types and contracts.
Values and defaults
Public values and defaults.
Source
Source: lib/accy/src/tensor/type/dim.zig
zig
const std = @import("std");const choir_abi = @import("choir_abi");const accy = @import("../../root.zig");pub const DType = choir_abi.DType;pub const generated_prefix: u8 = '#';pub const Error = error{ InvalidDimension, ShapeElementOverflow, ShapeByteOverflow, ShapeMismatch, DTypeMismatch, RankMismatch, AxisOutOfRange, AxisNotFound, AxisNameEmpty, AxisNameReserved, AxisExtentMismatch, DuplicateAxis, InvalidPermutation, ReshapeElementMismatch, BroadcastRankMismatch, BroadcastDimensionMismatch, ContractDTypeMismatch, SplitExtentMismatch, MergeExtentOverflow, ReduceInitNotScalar, PayloadLengthMismatch,};pub const Dim = struct { name: []const u8, extent: i64,};pub const Spec = struct { dtype: DType, dims: []const Dim, pub fn init(dtype: DType, dims: []const Dim) Spec { return .{ .dtype = dtype, .dims = dims }; }};pub const Type = struct { dtype: DType, dims: []const Dim, pub fn init(allocator: std.mem.Allocator, dtype: DType, dims: []const Dim) !Type { return .{ .dtype = dtype, .dims = try copyDims(allocator, dims), }; } pub fn fromSpec(allocator: std.mem.Allocator, value: Spec) !Type { return init(allocator, value.dtype, value.dims); } pub fn scalar(dtype: DType) Type { return .{ .dtype = dtype, .dims = &.{} }; } pub fn eql(self: Type, other: Type) bool { return self.dtype == other.dtype and sameDims(self.dims, other.dims); } pub fn rank(self: Type) usize { return self.dims.len; } pub fn extent(self: Type, index: usize) i64 { return self.dims[index].extent; } pub fn find(self: Type, name: []const u8) ?usize { return findDim(self.dims, name); } pub fn elementCount(self: Type) !usize { return dimsElementCount(self.dims); } pub fn byteCount(self: Type) !usize { return std.math.mul(usize, try self.elementCount(), self.dtype.sizeOf()) catch error.ShapeByteOverflow; }};pub fn spec(comptime dtype: DType, comptime dims_struct: anytype) Spec { const dims_array = comptime blk: { var array: [dimCount(@TypeOf(dims_struct))]Dim = undefined; fillDims(dims_struct, &array); break :blk array; }; return .{ .dtype = dtype, .dims = &dims_array };}pub fn specDims(dtype: DType, dims: []const Dim) Spec { return Spec.init(dtype, dims);}pub fn dimCount(comptime Dims: type) usize { const info = @typeInfo(Dims); if (info != .@"struct") { @compileError("tensor dims must be a struct literal mapping axis names to extents"); } if (info.@"struct".field_names.len == 0) return 0; if (info.@"struct".is_tuple) { @compileError("tensor dims must be a struct literal mapping axis names to extents"); } return info.@"struct".field_names.len;}pub fn fillDims(dims_struct: anytype, buffer: *[dimCount(@TypeOf(dims_struct))]Dim) void { const field_names = @typeInfo(@TypeOf(dims_struct)).@"struct".field_names; inline for (field_names, 0..) |field_name, index| { buffer[index] = .{ .name = field_name, .extent = @intCast(@field(dims_struct, field_name)), }; }}pub fn axisCount(comptime axes: anytype) usize { const Axes = @TypeOf(axes); return switch (@typeInfo(Axes)) { .enum_literal => 1, .@"struct" => |info| blk: { if (!info.is_tuple) { @compileError("tensor axes must be an enum literal or a tuple of enum literals"); } break :blk info.field_names.len; }, else => @compileError("tensor axes must be an enum literal or a tuple of enum literals"), };}pub fn axisNames(comptime axes: anytype) *const [axisCount(axes)][]const u8 { comptime var names: [axisCount(axes)][]const u8 = undefined; switch (@typeInfo(@TypeOf(axes))) { .enum_literal => names[0] = @tagName(axes), .@"struct" => |info| { inline for (info.field_names, 0..) |field_name, index| { const value = @field(axes, field_name); if (@typeInfo(@TypeOf(value)) != .enum_literal) { @compileError("tensor axes tuple entries must be enum literals"); } names[index] = @tagName(value); } }, else => unreachable, } const final = names; return &final;}pub fn isNameSlice(comptime Axes: type) bool { return Axes == []const []const u8 or Axes == [][]const u8;}pub fn validateName(name: []const u8) !void { if (name.len == 0) return error.AxisNameEmpty;}pub fn validateAuthoredName(name: []const u8) !void { try validateName(name); if (name[0] == generated_prefix) return error.AxisNameReserved;}pub fn validateDims(dims: []const Dim) !void { for (dims, 0..) |dim, index| { try validateName(dim.name); if (dim.extent < 0) return error.InvalidDimension; for (dims[0..index]) |seen| { if (std.mem.eql(u8, seen.name, dim.name)) return error.DuplicateAxis; } }}pub fn validateAuthoredDims(dims: []const Dim) !void { try validateDims(dims); for (dims) |dim| { try validateAuthoredName(dim.name); }}pub fn copyDims(allocator: std.mem.Allocator, dims: []const Dim) ![]const Dim { try validateDims(dims); const owned = try allocator.alloc(Dim, dims.len); for (dims, owned) |dim, *slot| { slot.* = .{ .name = try allocator.dupe(u8, dim.name), .extent = dim.extent, }; } return owned;}pub fn findDim(dims: []const Dim, name: []const u8) ?usize { for (dims, 0..) |dim, index| { if (std.mem.eql(u8, dim.name, name)) return index; } return null;}pub fn sameDims(lhs: []const Dim, rhs: []const Dim) bool { if (lhs.len != rhs.len) return false; for (lhs, rhs) |left, right| { if (left.extent != right.extent) return false; if (!std.mem.eql(u8, left.name, right.name)) return false; } return true;}pub fn dimsElementCount(dims: []const Dim) !usize { var count: usize = 1; for (dims) |dim| { if (dim.extent < 0) return error.InvalidDimension; count = std.math.mul(usize, count, @intCast(dim.extent)) catch return error.ShapeElementOverflow; } return count;}pub fn extents(allocator: std.mem.Allocator, dims: []const Dim) ![]const i64 { const owned = try allocator.alloc(i64, dims.len); for (dims, owned) |dim, *slot| { slot.* = dim.extent; } return owned;}pub fn sameType(lhs: Type, rhs: Type) !void { if (lhs.dtype != rhs.dtype) return error.DTypeMismatch; if (!sameDims(lhs.dims, rhs.dims)) return error.ShapeMismatch;}test "dims literal parsing preserves declaration order" { const parsed = spec(.f32, .{ .point = 16, .sample = 8 }); try std.testing.expectEqual(@as(usize, 2), parsed.dims.len); try std.testing.expectEqualStrings("point", parsed.dims[0].name); try std.testing.expectEqual(@as(i64, 16), parsed.dims[0].extent); try std.testing.expectEqualStrings("sample", parsed.dims[1].name); try std.testing.expectEqual(@as(i64, 8), parsed.dims[1].extent);}test "axis names parse from literals and tuples" { const single = axisNames(.sample); try std.testing.expectEqualStrings("sample", single[0]); const pair = axisNames(.{ .point, .sample }); try std.testing.expectEqualStrings("point", pair[0]); try std.testing.expectEqualStrings("sample", pair[1]);}test "type owns validated dims and computes byte count" { const ty = try Type.init(std.testing.allocator, .f32, &.{ .{ .name = "a", .extent = 2 }, .{ .name = "b", .extent = 3 }, .{ .name = "c", .extent = 4 }, }); defer freeTestType(std.testing.allocator, ty); try std.testing.expectEqual(@as(usize, 3), ty.rank()); try std.testing.expectEqual(@as(usize, 24), try ty.elementCount()); try std.testing.expectEqual(@as(usize, 96), try ty.byteCount()); try std.testing.expectEqual(@as(?usize, 1), ty.find("b")); try std.testing.expectEqual(@as(?usize, null), ty.find("missing"));}test "dims validation rejects bad names and extents" { try std.testing.expectError(error.InvalidDimension, Type.init( std.testing.allocator, .f32, &.{.{ .name = "a", .extent = -1 }}, )); try std.testing.expectError(error.DuplicateAxis, Type.init( std.testing.allocator, .f32, &.{ .{ .name = "a", .extent = 2 }, .{ .name = "a", .extent = 3 } }, )); try std.testing.expectError(error.AxisNameEmpty, Type.init( std.testing.allocator, .f32, &.{.{ .name = "", .extent = 2 }}, )); try std.testing.expectError(error.AxisNameReserved, validateAuthoredDims(&.{ .{ .name = "#batch", .extent = 2 }, })); try validateDims(&.{.{ .name = "#batch", .extent = 2 }});}pub fn freeTestType(allocator: std.mem.Allocator, ty: Type) void { for (ty.dims) |dim| allocator.free(dim.name); allocator.free(ty.dims);}pub fn expectExtents(expected: []const i64, ty: Type) !void { try std.testing.expectEqual(expected.len, ty.dims.len); for (expected, ty.dims) |extent, dim| { try std.testing.expectEqual(extent, dim.extent); }}Source: lib/accy/src/tensor/type/root.zig:1
zig
pub const dim = @import("dim.zig");Complete caller list for tensor.Type.init
24 direct callers.
lib.accy.src.tensor.program.cloneType[function] — private source atlib/accy/src/tensor/program.zig:470in nearest public ownertiny.accy.tensor.programlib.accy.src.tensor.program.singleConstantProgram[function] — private source atlib/accy/src/tensor/program.zig:474in nearest public ownertiny.accy.tensor.programlib.accy.src.tensor.program.test_tensor_program_identifies_zero_constants[function] — test source atlib/accy/src/tensor/program.zig:506in nearest public ownertiny.accy.tensor.programlib.accy.src.tensor.random.root.splitKey[function] — private source atlib/accy/src/tensor/random/root.zig:73in nearest public ownertiny.accy.tensor.randomlib.accy.src.tensor.random.root.uniformKey[function] — private source atlib/accy/src/tensor/random/root.zig:100in nearest public ownertiny.accy.tensor.randomlib.accy.src.tensor.random.root.uniformKeyCounter[function] — private source atlib/accy/src/tensor/random/root.zig:120in nearest public ownertiny.accy.tensor.randomtiny.accy.tensor.Builder.broadcastOp[method] atlib/accy/src/tensor/trace/builder.zig:402tiny.accy.tensor.Builder.contract[method] atlib/accy/src/tensor/trace/builder.zig:588lib.accy.src.tensor.trace.builder.Builder.copyType[method] — private source atlib/accy/src/tensor/trace/builder.zig:813in nearest public ownertiny.accy.tensor.trace.buildertiny.accy.tensor.Builder.dotGeneralOp[method] atlib/accy/src/tensor/trace/builder.zig:646tiny.accy.tensor.Builder.fullDims[method] atlib/accy/src/tensor/trace/builder.zig:165tiny.accy.tensor.Builder.reduce[method] atlib/accy/src/tensor/trace/builder.zig:447tiny.accy.tensor.Builder.transposeBy[method] atlib/accy/src/tensor/trace/builder.zig:435lib.accy.src.tensor.trace.test.test_tensor_custom_calls_align_arity_with_kernel_calls[function] — test source atlib/accy/src/tensor/trace/test.zig:137in nearest public ownerlib.accy.src.tensor.trace.testtiny.accy.tensor.types.algebra.broadcastInDim[function] atlib/accy/src/tensor/type/algebra.zig:468tiny.accy.tensor.types.algebra.gather[function] atlib/accy/src/tensor/type/algebra.zig:419tiny.accy.tensor.types.algebra.reshaped[function] atlib/accy/src/tensor/type/algebra.zig:462tiny.accy.tensor.types.algebra.scatterAdd[function] atlib/accy/src/tensor/type/algebra.zig:442tiny.accy.tensor.types.algebra.select[function] atlib/accy/src/tensor/type/algebra.zig:484tiny.accy.tensor.types.algebra.sparseCrossEntropy[function] atlib/accy/src/tensor/type/algebra.zig:451lib.accy.src.tensor.type.algebra.test_gather_inserts_the_index_shape_at_the_selected_axis[function] — test source atlib/accy/src/tensor/type/algebra.zig:673in nearest public ownertiny.accy.tensor.types.algebralib.accy.src.tensor.type.algebra.test_scatter_add_validates_updates_as_gather-shaped_and_returns_input_shape[function] — test source atlib/accy/src/tensor/type/algebra.zig:704in nearest public ownertiny.accy.tensor.types.algebralib.accy.src.tensor.type.dim.test_dims_validation_rejects_bad_names_and_extents[function] — test source atlib/accy/src/tensor/type/dim.zig:272in nearest public ownertiny.accy.tensor.types.dimlib.accy.src.tensor.type.dim.test_type_owns_validated_dims_and_computes_byte_count[function] — test source atlib/accy/src/tensor/type/dim.zig:257in nearest public ownertiny.accy.tensor.types.dim
Complete caller list for tensor.types.dim.findDim
10 direct callers.
tiny.accy.tensor.types.algebra.alignment[function] atlib/accy/src/tensor/type/algebra.zig:42tiny.accy.tensor.types.algebra.appendDims[function] atlib/accy/src/tensor/type/algebra.zig:140tiny.accy.tensor.types.algebra.axisIndices[function] atlib/accy/src/tensor/type/algebra.zig:81tiny.accy.tensor.types.algebra.contraction[function] atlib/accy/src/tensor/type/algebra.zig:283tiny.accy.tensor.types.algebra.insertDim[function] atlib/accy/src/tensor/type/algebra.zig:126tiny.accy.tensor.types.algebra.mergeDims[function] atlib/accy/src/tensor/type/algebra.zig:203tiny.accy.tensor.types.algebra.renamed[function] atlib/accy/src/tensor/type/algebra.zig:154tiny.accy.tensor.types.algebra.splitDims[function] atlib/accy/src/tensor/type/algebra.zig:165tiny.accy.tensor.types.algebra.unionDims[function] atlib/accy/src/tensor/type/algebra.zig:10tiny.accy.tensor.Type.find[method] atlib/accy/src/tensor/type/dim.zig:78
Complete caller list for tensor.types.dim.sameDims
8 direct callers.
tiny.accy.tensor.types.algebra.alignment[function] atlib/accy/src/tensor/type/algebra.zig:42tiny.accy.tensor.types.algebra.scatterAdd[function] atlib/accy/src/tensor/type/algebra.zig:442tiny.accy.tensor.types.algebra.select[function] atlib/accy/src/tensor/type/algebra.zig:484tiny.accy.tensor.types.algebra.sparseCrossEntropy[function] atlib/accy/src/tensor/type/algebra.zig:451lib.accy.src.tensor.type.algebra.test_scatter_add_validates_updates_as_gather-shaped_and_returns_input_shape[function] — test source atlib/accy/src/tensor/type/algebra.zig:704in nearest public ownertiny.accy.tensor.types.algebratiny.accy.tensor.Type.eql[method] atlib/accy/src/tensor/type/dim.zig:66tiny.accy.tensor.types.dim.sameType[function] atlib/accy/src/tensor/type/dim.zig:234lib.accy.src.tensor.type.test.test_named_type_algebra_supports_the_softmax_attention_derivation[function] — test source atlib/accy/src/tensor/type/test.zig:59in nearest public ownerlib.accy.src.tensor.type.test
Complete caller list for tensor.types.dim.spec
17 direct callers.
lib.accy.src.tensor.test.test_accy_tensor_builds_and_differentiates_a_tiny_language_model_loss[function] — test source atlib/accy/src/tensor/test.zig:1106in nearest public ownerlib.accy.src.tensor.testlib.accy.src.tensor.test.test_accy_tensor_grad_composes_with_vmap_and_lowering[function] — test source atlib/accy/src/tensor/test.zig:230in nearest public ownerlib.accy.src.tensor.testlib.accy.src.tensor.test.test_accy_tensor_grad_lowers_batched_dense_scalar_loss[function] — test source atlib/accy/src/tensor/test.zig:1043in nearest public ownerlib.accy.src.tensor.testlib.accy.src.tensor.test.test_accy_tensor_grad_lowers_dense_scalar_loss[function] — test source atlib/accy/src/tensor/test.zig:1018in nearest public ownerlib.accy.src.tensor.testlib.accy.src.tensor.test.test_accy_tensor_namespace_traces_rewrites_and_lowers_a_dense_program[function] — test source atlib/accy/src/tensor/test.zig:115in nearest public ownerlib.accy.src.tensor.testlib.accy.src.tensor.test.test_accy_tensor_pullback_composes_with_vmap_and_lowering[function] — test source atlib/accy/src/tensor/test.zig:196in nearest public ownerlib.accy.src.tensor.testlib.accy.src.tensor.test.test_accy_tensor_scheduled_sparse_cross_entropy_matches_the_expanded_lowering_on_live_CUDA[function] — test source atlib/accy/src/tensor/test.zig:720in nearest public ownerlib.accy.src.tensor.testlib.accy.src.tensor.test.test_accy_tensor_sparse_cross_entropy_gradient_matches_softmax_minus_one_hot_on_cpu[function] — test source atlib/accy/src/tensor/test.zig:635in nearest public ownerlib.accy.src.tensor.testlib.accy.src.tensor.test.test_accy_tensor_sparse_cross_entropy_losses_match_the_host_reference_on_cpu[function] — test source atlib/accy/src/tensor/test.zig:599in nearest public ownerlib.accy.src.tensor.testlib.accy.src.tensor.test.test_accy_tensor_transforms_compose_through_rewrite_linearize_rewrite_and_lower[function] — test source atlib/accy/src/tensor/test.zig:145in nearest public ownerlib.accy.src.tensor.testlib.accy.src.tensor.test.test_accy_tensor_vmap_all_batched_scatter_add_accumulates_duplicate_indices_on_cpu[function] — test source atlib/accy/src/tensor/test.zig:474in nearest public ownerlib.accy.src.tensor.testlib.accy.src.tensor.test.test_accy_tensor_vmap_batched_index_scatter_add_broadcasts_the_shared_input_on_cpu[function] — test source atlib/accy/src/tensor/test.zig:533in nearest public ownerlib.accy.src.tensor.testlib.accy.src.tensor.test.test_accy_tensor_vmap_composes_with_dense_grad_and_lowering[function] — test source atlib/accy/src/tensor/test.zig:1063in nearest public ownerlib.accy.src.tensor.testlib.accy.src.tensor.test.test_accy_tensor_vmap_composes_with_linearize_and_lowering[function] — test source atlib/accy/src/tensor/test.zig:170in nearest public ownerlib.accy.src.tensor.testlib.accy.src.tensor.test.test_accy_tensor_vmap_shared_batch_gather_matches_the_host_reference_on_cpu[function] — test source atlib/accy/src/tensor/test.zig:423in nearest public ownerlib.accy.src.tensor.testlib.accy.src.tensor.test.test_accy_tensor_vmap_sparse_cross_entropy_matches_the_host_reference_on_cpu[function] — test source atlib/accy/src/tensor/test.zig:682in nearest public ownerlib.accy.src.tensor.testlib.accy.src.tensor.type.dim.test_dims_literal_parsing_preserves_declaration_order[function] — test source atlib/accy/src/tensor/type/dim.zig:239in nearest public ownertiny.accy.tensor.types.dim
Audit
| Definitions | 36 |
|---|---|
| Public names | 97 |
| Members | 27 |
| Version | 26.7.0 |
| Revision | daab053ee433 |