File size: 4,878 Bytes
f7ccd08 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 | { SPDX-License-Identifier: LicenseRef-Sovereign-Leviathan-SL-AGPL3-001
Licensing: see LICENSE in the repository root. }
{ NraayTensor Reference Implementation: Main program
Demonstrates the Free Pascal reference layer for semantic validation.
Usage:
- Runs comprehensive test suite
- Validates conformance with Rust implementation
- Provides oracle functions for cross-checking
Exit codes:
0 - All tests passed
1 - Some tests failed
2 - Fatal error
}
program NraayTensorRef;
{$MODE FPC}
{$APPTYPE CONSOLE}
uses
SysUtils,
descriptor,
buffer,
tensor_array,
ownership,
governance,
materialize,
tensor,
validation,
tests;
procedure PrintBanner;
begin
writeln('');
writeln('================================================================');
writeln(' NraayTensor Reference Implementation (Free Pascal)');
writeln(' Semantic validation layer for Rust tensor-core library');
writeln('================================================================');
writeln('');
end;
procedure PrintTestResults(const stats: TTestStats);
begin
writeln('');
writeln('Test Results:');
writeln(' Total: ', stats.total_tests);
writeln(' Passed: ', stats.passed);
writeln(' Failed: ', stats.failed);
writeln('');
if stats.failed = 0 then
begin
writeln('All tests PASSED!');
halt(0);
end
else
begin
writeln('Some tests FAILED.');
halt(1);
end;
end;
procedure DemoBasicOperations;
var
t: TNraayTensor;
t2: TNraayTensor;
shape: array [0..1] of uint32;
coord: array [0..1] of uint32;
begin
writeln('');
writeln('--- Basic Operations Demo ---');
writeln('');
{ Create 3x4 tensor }
shape[0] := 3;
shape[1] := 4;
writeln('Creating 3x4 tensor...');
t := CreateTensor(shape);
if IsValid(t) then
begin
writeln('Tensor created successfully');
writeln(' Rank: ', Rank(t));
writeln(' Length: ', Length(t));
writeln(' B_layout (is_contiguous): ', t.descriptor.is_contiguous);
writeln(' B_own (owns_storage): ', t.descriptor.owns_storage);
writeln(' Ownership state: ', StateToString(t.ownership.state));
end;
{ Set element }
coord[0] := 1;
coord[1] := 2;
writeln('');
writeln('Setting element at [1,2] = 42.0...');
SetElement(t, coord, 42.0);
writeln('Reading element at [1,2] = ', GetElement(t, coord):0:1);
{ Clone }
writeln('');
writeln('Cloning tensor...');
t2 := CloneTensor(t);
writeln('Refcount after clone: ', GetRefCount(t2.data));
writeln('Original state: ', StateToString(t.ownership.state));
writeln('Cloned state: ', StateToString(t2.ownership.state));
{ Materialize }
writeln('');
writeln('Materializing cloned tensor...');
Materialize(t2);
writeln('After materialization:');
writeln(' B_layout: ', t2.descriptor.is_contiguous);
writeln(' B_own: ', t2.descriptor.owns_storage);
writeln(' Ownership state: ', StateToString(t2.ownership.state));
writeln(' Offset: ', t2.descriptor.offset);
DestroyTensor(t);
DestroyTensor(t2);
writeln('');
end;
procedure PrintGovernanceSummary;
begin
writeln('');
writeln('--- Governance Lemmas ---');
writeln('');
writeln('Lemma 1 (Shape-Rank):');
writeln(' rank(t) = len(shape(t))');
writeln('');
writeln('Lemma 2 (Length Product):');
writeln(' len(t) = product of all shape dimensions');
writeln('');
writeln('Lemma 3 (Canonical Strides):');
writeln(' B_layout=1 => strides are row-major canonical');
writeln('');
writeln('Lemma 4 (Exclusive Ownership):');
writeln(' B_own=1 => Arc refcount=1');
writeln('');
writeln('Lemma 5 (Generation Safety):');
writeln(' descriptor.generation = buffer.generation');
writeln('');
end;
procedure PrintSemantics;
begin
writeln('');
writeln('--- Semantic Layer ---');
writeln('');
writeln('Physical Storage (Arc semantics):');
writeln(' - TensorArray wraps TBuffer with refcount');
writeln(' - Clone increments refcount without copying data');
writeln(' - Drop decrements refcount, frees at 0');
writeln('');
writeln('Semantic View (Descriptor):');
writeln(' - Shape: logical dimensions s_0, s_1, ...');
writeln(' - Strides: step sizes sigma_0, sigma_1, ...');
writeln(' - Offset: starting position in physical buffer');
writeln(' - Flags: B_layout (canonical?), B_own (exclusive?)');
writeln('');
writeln('Operations (matched with Rust implementation):');
writeln(' - ALLOC: CreateTensor');
writeln(' - FREE: DestroyTensor');
writeln(' - VIEW: Slice (zero-copy via strides)');
writeln(' - TRANSPOSE: axis permutation');
writeln(' - MATERIALIZE: COW with contiguity restoration');
writeln('');
end;
begin
PrintBanner;
PrintSemantics;
PrintGovernanceSummary;
DemoBasicOperations;
writeln('');
writeln('Running comprehensive test suite...');
PrintTestResults(RunAllTests);
end.
|