|
| 1 | +name: ml_tensor |
| 2 | +version: "1.0.0" |
| 3 | +language: zig |
| 4 | +module: ml |
| 5 | + |
| 6 | +description: | |
| 7 | + Tensor Implementation for Trinity Nexus with gradient support. |
| 8 | + Based on archived implementation from archive/implementations/zig/src/ml/tensor.zig |
| 9 | + |
| 10 | + Features: |
| 11 | + - Multi-dimensional tensor with shape tracking |
| 12 | + - Automatic gradient computation |
| 13 | + - Matrix operations (matmul, add, mul) |
| 14 | + - Activation functions (ReLU, GELU) |
| 15 | + - Softmax with numerical stability |
| 16 | + - Cross-entropy loss |
| 17 | + - Xavier initialization scaled by φ (Golden Ratio) |
| 18 | + |
| 19 | + Sacred Constants: |
| 20 | + - PHI: 1.618033988749895 (Golden Ratio) |
| 21 | + - GOLDEN_IDENTITY: 3.0 (φ² + 1/φ² = 3) |
| 22 | + - PHOENIX: 999 (Rebirth cycle) |
| 23 | + |
| 24 | +types: |
| 25 | + Tensor: |
| 26 | + fields: |
| 27 | + data: List<Float> |
| 28 | + grad: Option<List<Float>> |
| 29 | + shape: List<usize> |
| 30 | + requires_grad: Bool |
| 31 | + # Note: allocator is implicit in Zig |
| 32 | + |
| 33 | +behaviors: |
| 34 | + - name: init |
| 35 | + given: Allocator, shape (list of dimensions), requires_grad flag |
| 36 | + when: Allocates memory for tensor data and optionally for gradients |
| 37 | + then: Returns initialized tensor with zeros |
| 38 | + |
| 39 | + - name: deinit |
| 40 | + given: Tensor pointer |
| 41 | + when: Frees all allocated memory (data, grad, shape) |
| 42 | + then: Memory released |
| 43 | + |
| 44 | + - name: numel |
| 45 | + given: Tensor |
| 46 | + when: Computes product of all dimensions |
| 47 | + then: Returns total number of elements |
| 48 | + |
| 49 | + - name: fill |
| 50 | + given: Tensor pointer and value |
| 51 | + when: Sets all elements to the given value |
| 52 | + then: Tensor data filled uniformly |
| 53 | + |
| 54 | + - name: fillRandom |
| 55 | + given: Tensor pointer and seed |
| 56 | + when: Fills with random values using Xavier initialization scaled by φ |
| 57 | + then: Random tensor ready for training |
| 58 | + |
| 59 | + - name: zeroGrad |
| 60 | + given: Tensor pointer |
| 61 | + when: Sets all gradient values to zero |
| 62 | + then: Gradients reset for new backward pass |
| 63 | + |
| 64 | + - name: add |
| 65 | + given: Self tensor, other tensor, output tensor pointer |
| 66 | + when: Element-wise addition of two tensors |
| 67 | + then: Output contains element-wise sum |
| 68 | + |
| 69 | + - name: mul |
| 70 | + given: Self tensor, other tensor, output tensor pointer |
| 71 | + when: Element-wise multiplication of two tensors |
| 72 | + then: Output contains element-wise product |
| 73 | + |
| 74 | + - name: matmul |
| 75 | + given: Self tensor (M×K), other tensor (K×N), output tensor (M×N) |
| 76 | + when: Matrix multiplication of 2D tensors |
| 77 | + then: Output contains matrix product |
| 78 | + |
| 79 | + - name: relu |
| 80 | + given: Self tensor, output tensor pointer |
| 81 | + when: Applies ReLU activation (max(0, x)) element-wise |
| 82 | + then: Output contains ReLU activations |
| 83 | + |
| 84 | + - name: gelu |
| 85 | + given: Self tensor, output tensor pointer |
| 86 | + when: Applies GELU activation approximation |
| 87 | + then: Output contains GELU activations (smoother than ReLU) |
| 88 | + |
| 89 | + - name: softmax |
| 90 | + given: Self tensor, output tensor pointer |
| 91 | + when: Applies softmax along last dimension with numerical stability |
| 92 | + then: Output contains probability distributions (sum to 1) |
| 93 | + |
| 94 | + - name: crossEntropyLoss |
| 95 | + given: Self tensor (logits) and targets (class indices) |
| 96 | + when: Computes cross-entropy loss for classification |
| 97 | + then: Returns scalar loss value |
| 98 | + |
| 99 | + - name: sum |
| 100 | + given: Tensor |
| 101 | + when: Sums all elements |
| 102 | + then: Returns scalar sum |
| 103 | + |
| 104 | + - name: mean |
| 105 | + given: Tensor |
| 106 | + when: Computes mean of all elements |
| 107 | + then: Returns scalar mean value |
| 108 | + |
| 109 | +test_cases: |
| 110 | + - name: tensor_basics |
| 111 | + input: shape=[2,3], requires_grad=true |
| 112 | + expected: numel=6, fill(1.0) -> sum=6.0 |
| 113 | + |
| 114 | + - name: matmul |
| 115 | + input: A=[2,3] filled with 1.0, B=[3,2] filled with 1.0 |
| 116 | + expected: C=[2,2] with all elements = 3.0 |
| 117 | + |
| 118 | + - name: softmax |
| 119 | + input: tensor=[1.0, 2.0, 3.0] |
| 120 | + expected: output sums to 1.0 (valid probability distribution) |
0 commit comments