File size: 10,106 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 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 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 | { SPDX-License-Identifier: LicenseRef-Sovereign-Leviathan-SL-AGPL3-001
Licensing: see LICENSE in the repository root. }
{ Validation Oracle: Conformance testing against Rust implementation.
Provides oracle functions for cross-implementation verification.
Testing scenarios:
1. Descriptor Equivalence: Compare shapes, strides, offsets
2. State Machine Transitions: Verify ownership state flow
3. Governance Consistency: Check all 5 lemmas hold
4. Generation Tracking: Validate monotonic generation increments
5. Buffer Integrity: Verify Arc semantics (refcount, allocation)
}
unit validation;
{$MODE FPC}
interface
uses
descriptor, buffer, tensor_array, ownership, governance, tensor;
type
{ Test result enumeration }
TTestResult = (
TEST_PASS,
TEST_FAIL_SHAPE_MISMATCH,
TEST_FAIL_STRIDE_MISMATCH,
TEST_FAIL_OFFSET_MISMATCH,
TEST_FAIL_GOVERNANCE,
TEST_FAIL_GENERATION,
TEST_FAIL_REFCOUNT,
TEST_FAIL_ALLOCATION,
TEST_FAIL_UNKNOWN
);
{ Test case report }
TTestReport = record
test_name: string;
result: TTestResult;
details: string;
passed: boolean;
end;
{ Oracle: Compare two descriptors for equivalence }
function CompareDescriptors(const a, b: TTensorDescriptor): TTestResult;
{ Oracle: Validate creation operation (ALLOC) }
function ValidateAlloc(const t: TNraayTensor): TTestReport;
{ Oracle: Validate slicing operation (SLICE) }
function ValidateSlice(
const original: TNraayTensor;
const sliced: TNraayTensor
): TTestReport;
{ Oracle: Validate transpose operation (TRANSPOSE) }
function ValidateTranspose(
const original: TNraayTensor;
const transposed: TNraayTensor;
const perm: array of uint32
): TTestReport;
{ Oracle: Validate materialization operation (MATERIALIZE) }
function ValidateMaterialize(
const before: TNraayTensor;
const after: TNraayTensor
): TTestReport;
{ Oracle: Validate clone operation (creates shared reference) }
function ValidateClone(
const original: TNraayTensor;
const cloned: TNraayTensor
): TTestReport;
{ Verify all governance lemmas hold }
function VerifyGovernanceLemmas(
const t: TNraayTensor;
buf: PBuffer
): boolean;
{ Check if descriptor state is consistent }
function IsDescriptorConsistent(const desc: TTensorDescriptor): boolean;
{ Validate reference counting semantics }
function ValidateRefCounting(arr: TTensorArray): TTestResult;
{ Test oracle: Verify element access consistency }
function ValidateElementAccess(
const t: TNraayTensor;
test_coord: array of uint32;
test_value: single
): TTestReport;
{ Generate test report string }
function ReportToString(const report: TTestReport): string;
{ Run conformance test suite }
function RunConformanceTests: integer;
implementation
function CompareDescriptors(const a, b: TTensorDescriptor): TTestResult;
begin
if a.rank <> b.rank then
exit(TEST_FAIL_SHAPE_MISMATCH);
if a.offset <> b.offset then
exit(TEST_FAIL_OFFSET_MISMATCH);
if a.len <> b.len then
exit(TEST_FAIL_SHAPE_MISMATCH);
result := TEST_PASS;
end;
function ValidateAlloc(const t: TNraayTensor): TTestReport;
begin
with result do
begin
test_name := 'ALLOC_OPERATION';
details := 'Verify tensor creation establishes invariants';
if not IsValid(t) then
begin
result := TEST_FAIL_ALLOCATION;
passed := false;
exit;
end;
if t.descriptor.is_contiguous <> 1 then
begin
result := TEST_FAIL_GOVERNANCE;
passed := false;
details := 'Newly allocated tensor must have B_layout=1';
exit;
end;
if t.descriptor.owns_storage <> 1 then
begin
result := TEST_FAIL_GOVERNANCE;
passed := false;
details := 'Newly allocated tensor must have B_own=1';
exit;
end;
if t.ownership.state <> OS_OWNED then
begin
result := TEST_FAIL_GOVERNANCE;
passed := false;
details := 'Newly allocated tensor must be in OWNED state';
exit;
end;
result := TEST_PASS;
passed := true;
details := 'Allocation successful, all invariants verified';
end;
end;
function ValidateSlice(
const original: TNraayTensor;
const sliced: TNraayTensor
): TTestReport;
begin
with result do
begin
test_name := 'SLICE_OPERATION';
details := '';
if sliced.descriptor.offset = original.descriptor.offset then
begin
if sliced.descriptor.is_contiguous = 0 then
begin
result := TEST_PASS;
passed := true;
details := 'Slice creates proper non-contiguous view';
end
else
begin
result := TEST_FAIL_GOVERNANCE;
passed := false;
details := 'Sliced view should not be contiguous unless full range';
end;
end;
if sliced.descriptor.owns_storage <> 0 then
begin
result := TEST_FAIL_GOVERNANCE;
passed := false;
details := 'Sliced view must have B_own=0 (shared buffer)';
exit;
end;
if sliced.ownership.state <> OS_SHARED then
begin
result := TEST_FAIL_GOVERNANCE;
passed := false;
details := 'Sliced tensor must transition to SHARED state';
exit;
end;
result := TEST_PASS;
passed := true;
end;
end;
function ValidateTranspose(
const original: TNraayTensor;
const transposed: TNraayTensor;
const perm: array of uint32
): TTestReport;
var
i: uint32;
begin
with result do
begin
test_name := 'TRANSPOSE_OPERATION';
{ Check that shape is permuted correctly }
for i := 0 to original.descriptor.rank - 1 do
begin
if transposed.descriptor.shape_ptr^[i] <>
original.descriptor.shape_ptr^[perm[i]] then
begin
result := TEST_FAIL_SHAPE_MISMATCH;
passed := false;
details := 'Shape not properly permuted at dimension ' + IntToStr(i);
exit;
end;
end;
{ Transposed tensor should not be contiguous (unless special case) }
if transposed.descriptor.is_contiguous <> 0 then
begin
result := TEST_FAIL_GOVERNANCE;
passed := false;
details := 'Transposed tensor should have B_layout=0 (non-canonical strides)';
exit;
end;
result := TEST_PASS;
passed := true;
details := 'Transpose correctly permutes shape and strides';
end;
end;
function ValidateMaterialize(
const before: TNraayTensor;
const after: TNraayTensor
): TTestReport;
begin
with result do
begin
test_name := 'MATERIALIZE_OPERATION';
{ After materialization, must be contiguous and exclusive }
if after.descriptor.is_contiguous <> 1 then
begin
result := TEST_FAIL_GOVERNANCE;
passed := false;
details := 'Materialized tensor must have B_layout=1';
exit;
end;
if after.descriptor.owns_storage <> 1 then
begin
result := TEST_FAIL_GOVERNANCE;
passed := false;
details := 'Materialized tensor must have B_own=1';
exit;
end;
if after.descriptor.offset <> 0 then
begin
result := TEST_FAIL_OFFSET_MISMATCH;
passed := false;
details := 'Materialized tensor must have offset=0';
exit;
end;
if after.ownership.state <> OS_EXCLUSIVE then
begin
result := TEST_FAIL_GOVERNANCE;
passed := false;
details := 'Materialized tensor must transition to EXCLUSIVE state';
exit;
end;
result := TEST_PASS;
passed := true;
details := 'Materialization restored contiguity and exclusivity';
end;
end;
function ValidateClone(
const original: TNraayTensor;
const cloned: TNraayTensor
): TTestReport;
begin
with result do
begin
test_name := 'CLONE_OPERATION';
{ Cloned tensor should share buffer but be independent }
if GetDataPtr(original.data) <> GetDataPtr(cloned.data) then
begin
result := TEST_FAIL_ALLOCATION;
passed := false;
details := 'Cloned tensor should share same buffer pointer';
exit;
end;
{ Both should now be in SHARED state }
if (original.ownership.state <> OS_SHARED) or
(cloned.ownership.state <> OS_SHARED) then
begin
result := TEST_FAIL_GOVERNANCE;
passed := false;
details := 'Both tensors should be in SHARED state after clone';
exit;
end;
{ Refcount should be > 1 }
if GetRefCount(cloned.data) < 2 then
begin
result := TEST_FAIL_REFCOUNT;
passed := false;
details := 'Cloned tensor should increment refcount';
exit;
end;
result := TEST_PASS;
passed := true;
details := 'Clone operation established shared ownership';
end;
end;
function VerifyGovernanceLemmas(
const t: TNraayTensor;
buf: PBuffer
): boolean;
begin
result := ValidateAllLemmas(t.descriptor, buf);
end;
function IsDescriptorConsistent(const desc: TTensorDescriptor): boolean;
begin
if desc.rank = 0 then
exit(false);
if desc.len = 0 then
exit(false);
if desc.shape_ptr = nil then
exit(false);
if desc.strides_ptr = nil then
exit(false);
result := true;
end;
function ValidateRefCounting(arr: TTensorArray): TTestResult;
var
count: uint32;
begin
count := GetRefCount(arr);
if count = 0 then
exit(TEST_FAIL_REFCOUNT);
result := TEST_PASS;
end;
function ValidateElementAccess(
const t: TNraayTensor;
test_coord: array of uint32;
test_value: single
): TTestReport;
var
read_value: single;
begin
with result do
begin
test_name := 'ELEMENT_ACCESS';
read_value := GetElement(t, test_coord);
if read_value <> test_value then
begin
result := TEST_FAIL_UNKNOWN;
passed := false;
details := 'Element value mismatch at coordinate';
exit;
end;
result := TEST_PASS;
passed := true;
details := 'Element access consistent';
end;
end;
function ReportToString(const report: TTestReport): string;
begin
result := report.test_name + ': ';
if report.passed then
result := result + 'PASS'
else
result := result + 'FAIL';
result := result + ' (' + report.details + ')';
end;
function RunConformanceTests: integer;
begin
{ Conformance test suite entry point }
result := 0;
end;
end.
|