File size: 8,791 Bytes
32b4893
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29f097e
32b4893
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# NraayTensor Pascal Reference Implementation

Semantic reference layer for the Rust tensor-core library, implemented in Free Pascal (FPC). This reference implementation serves as an **oracle** for validating the Rust implementation, ensuring correctness through cross-language verification.

## Architecture

### Semantic Stack (Bottom to Top)

```
    Application (tests.pas, validation.pas)
           ↓
    Tensor Operations (tensor.pas)
           ↓
    Materialization & Governance (materialize.pas, governance.pas)
           ↓
    Ownership State Machine (ownership.pas)
           ↓
    Descriptor & Tensor Array (descriptor.pas, tensor_array.pas)
           ↓
    Buffer Management (buffer.pas)
           ↓
    Physical Storage (single[])
```

## Modules

### 1. buffer.pas (Generation-tracked allocation)
- **TBuffer**: Physical storage with refcount and generation
- `AllocateBuffer()`: Allocate with generation tracking
- `CloneBuffer()`: Increment refcount (Arc::clone)
- `ReleaseBuffer()`: Decrement refcount, free at 0
- `IncrementGeneration()`: Temporal safety tracking

**Key Invariants:**
- Refcount > 0 always while allocated
- Generation monotonically increases
- Once freed (refcount=0), generation becomes invalid

### 2. tensor_array.pas (Arc<Vec> semantics)
- **TTensorArray**: Wraps TBuffer with generation tracking
- `CreateTensorArray()`: Allocate new array
- `CloneTensorArray()`: Share buffer (refcount++)
- `DropTensorArray()`: Release (refcount--)
- `CopyOnWrite()`: Clone data if refcount > 1

**Arc Semantics:**
- Multiple references to same buffer (shared)
- Mutation only allowed if exclusively owned (refcount=1)
- Mutation triggers new generation

### 3. descriptor.pas (C-compatible view metadata)
- **TTensorDescriptor**: Packed record (C ABI compatible)
  - `rank`: dimensionality
  - `shape_ptr`: β†’ shape array
  - `strides_ptr`: β†’ strides array
  - `offset`: logical offset in buffer
  - `len`: total elements
  - `is_contiguous` (B_layout): canonical strides?
  - `owns_storage` (B_own): refcount=1?
  - `generation`: temporal safety token

**Index Mapping:** `f(x_0,...,x_{d-1}) = offset + Ξ£(x_i * Οƒ_i)`

### 4. ownership.pas (OWNED/SHARED/EXCLUSIVE states)
- **TOwnershipState**: Enumeration of ownership modes
- State transitions:
  - `OWNED` (refcount=1, mutable)
  - `SHARED` (refcount>1, immutable, COW)
  - `EXCLUSIVE` (after materialization, mutable)

**State Machine:**
```
OWNED --[clone]--> SHARED
SHARED --[materialize]--> EXCLUSIVE
EXCLUSIVE --[release]--> OWNED (if refcount becomes 1)
```

### 5. governance.pas (5 executable lemmas)
Proves correctness of tensor invariants:

1. **Lemma 1 (Shape-Rank)**: `rank(t) = len(shape)`
2. **Lemma 2 (Length Product)**: `len(t) = ∏ shape_i`
3. **Lemma 3 (Canonical Strides)**: `B_layout=1 ⟹ Οƒ_i = ∏_{j>i} s_j`
4. **Lemma 4 (Exclusive Ownership)**: `B_own=1 ⟹ refcount=1`
5. **Lemma 5 (Generation Safety)**: `descriptor.generation = buffer.generation`

`ValidateAllLemmas()`: Verify all 5 proofs hold at once.

### 6. materialize.pas (Copy-on-write barrier)
Restores contiguity and exclusive ownership:

**Algorithm:**
1. If already materialized (B_layout=1 AND B_own=1): return
2. Allocate new contiguous buffer
3. Copy viewed elements in row-major order
4. Replace Arc with new exclusive buffer
5. Reset strides to canonical
6. Update generation, set B_layout=1, B_own=1

**Property:** "Preserving order, strict and bound"

### 7. tensor.pas (Main tensor operations)
Core operations mirroring Rust implementation:

- **ALLOC**: `CreateTensor(shape)` β†’ new exclusive tensor
- **FREE**: `DestroyTensor(var t)` β†’ decrement refcount
- **VIEW**: `Slice(t, ranges)` β†’ zero-copy view (shared)
- **TRANSPOSE**: permute axes via stride rearrangement
- **MATERIALIZE**: `Materialize(var t)` β†’ restore contiguity
- **GET/SET**: element access via logical coordinates

### 8. validation.pas (Conformance oracle)
Cross-implementation validation functions:

- `CompareDescriptors()`: Check equivalence
- `ValidateAlloc()`: Verify creation invariants
- `ValidateSlice()`: Check slicing semantics
- `ValidateTranspose()`: Verify axis permutation
- `ValidateMaterialize()`: Check materialization result
- `ValidateClone()`: Verify shared ownership
- `VerifyGovernanceLemmas()`: All 5 lemmas hold?

### 9. tests.pas (Comprehensive test suite)
10 core conformance tests:

1. Allocation
2. Element access
3. Clone (refcount=2)
4. Slicing (non-contiguous)
5. Transpose (shape permuted)
6. Materialization (contiguous+exclusive)
7. Generation tracking
8. Governance lemmas
9. Reference counting
10. Composite workflow

### 10. nraay_tensor_ref.pas (Main program)
Demonstration and test runner:
```
./nraay_tensor_ref
```

Exit codes:
- 0: All tests passed
- 1: Some tests failed
- 2: Fatal error

## Operation Semantics

### Allocation (ALLOC)
```pascal
t := CreateTensor([3, 4]);
{ Result:
  - Exclusive ownership (refcount=1)
  - Canonical strides (row-major)
  - B_layout=1, B_own=1, OWNED state
  - Generation=0 }
```

### Slicing (VIEW)
```pascal
ranges[0] := [1, 4, 1];  // start=1, end=4, step=1
ranges[1] := [1, 4, 1];
sliced := Slice(t, ranges);
{ Result:
  - Shared ownership (refcount→2)
  - Non-canonical strides (B_layout=0)
  - Offset adjusted, shape reduced
  - B_own=0, SHARED state
  - Same buffer, different view }
```

### Materialization (MATERIALIZE)
```pascal
Materialize(sliced);
{ Result:
  - Exclusive ownership (new buffer)
  - Canonical strides (row-major)
  - B_layout=1, B_own=1
  - Offset=0, new generation
  - EXCLUSIVE state }
```

### Transpose
```pascal
perm := [1, 0];  // swap axes
transposed := Transpose(t, perm);
{ Result:
  - Shape permuted: [3,4] β†’ [4,3]
  - Strides permuted accordingly
  - B_layout likely=0 (non-canonical)
  - Shares buffer, different view }
```

## Governance Properties

### Binary Semantics (B_layout, B_own)

**B_layout (is_contiguous):**
- 1: Strides follow row-major canonical pattern
- 0: Strides do NOT follow canonical (from slicing/transpose)

**B_own (owns_storage):**
- 1: Exclusive ownership (refcount=1), can mutate
- 0: Shared ownership (refcount>1), immutable

### Generation Safety
Every buffer has monotonic generation counter:
```
buffer.generation = counter at allocation time
descriptor.generation = counter at view creation time

Use-after-free prevention: stale views detected by generation mismatch
```

## Compilation and Usage

### FPC Compilation
```bash
cd pascal
fpc -O2 nraay_tensor_ref.pas -o nraay_tensor_ref
./nraay_tensor_ref
```

### With gdb
```bash
fpc -g nraay_tensor_ref.pas
gdb ./nraay_tensor_ref
```

### Unit Testing in Other Code
```pascal
uses tensor, validation, tests;

procedure MyTest;
var
  t: TNraayTensor;
  report: TTestReport;
begin
  t := CreateTensor([2, 3]);
  report := ValidateAlloc(t);
  
  if report.passed then
    writeln('Test passed: ', report.details)
  else
    writeln('Test failed: ', report.details);
    
  DestroyTensor(t);
end;
```

## Cross-Language Conformance

This Pascal reference validates Rust implementation by:

1. **Operation Matching**: Each Rust op has Pascal equivalent
2. **State Verification**: Oracle functions check Rust output states
3. **Invariant Checking**: 5 governance lemmas verified independently
4. **Generation Tracking**: Temporal safety without language runtime
5. **Memory Semantics**: Arc behavior replicated with explicit refcount

### Testing Against Rust
1. Run Rust operation
2. Export resulting descriptor/buffer to Pascal format
3. Call validation oracle
4. Compare Oracle result with expected behavior

## Semantic Properties

### Invariants (Always True)
```
1. rank(t) = len(shape(t))
2. len(t) = ∏ shape_i
3. B_layout=1 β†’ canonical strides
4. B_own=1 β†’ refcount=1
5. generation(descriptor) = generation(buffer)
```

### State Safety
```
OWNED ← exclusive mutation OK, B_own=1
SHARED ← no mutation, COW barrier
EXCLUSIVE ← exclusive mutation OK, B_own=1
```

### Generation Transitions
```
- Allocation: generation_0 (global_counter++)
- Clone: inherits buffer.generation (same)
- Materialize: generation_new (global_counter++)
- Stale check: generation_mismatch β†’ error
```

## Performance Notes

**Memory:** ~1200 LOC core, ~800 LOC validation
**No runtime overhead:** Direct memory access via strides
**Materialization:** O(n) copy only on COW violation or explicit call

## License

License: Sovereign Leviathan covenant (`LicenseRef-Sovereign-Leviathan-SL-AGPL3-001`)

This ensures:
- Open source compliance (GPL-3.0)
- AI company restrictions (GPL-3.0 clause 5)

## See Also

- [FORMAL_SPEC.md](../FORMAL_SPEC.md) - Mathematical definitions
- [Rust Implementation](../nraay-tensor/src/tensor.rs) - Primary implementation
- [Lemmas](../nraay-tensor/src/lemmas.rs) - Formal proofs