File size: 9,639 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 | { SPDX-License-Identifier: LicenseRef-Sovereign-Leviathan-SL-AGPL3-001
Licensing: see LICENSE in the repository root. }
{ ============================================================================
NraayTensor FFI ABI Bindings for Free Pascal
Provides Pascal-idiomatic bindings to the Rust tensor FFI layer.
All operations are thin wrappers around C FFI calls into tensor_ffi.rs.
The Pascal implementation remains independent for validation.
These bindings allow Pascal to invoke Rust tensor operations for
conformance testing and oracle comparison.
============================================================================ }
unit TensorABI;
{$MODE OBJFPC}
{$H+}
{$MODESWITCH ADVANCEDRECORDS}
interface
uses
Classes, SysUtils, TypInfo;
type
{ ========================================================================
Tensor Descriptor — mirrors C ABI
======================================================================== }
TTensorDescriptorC = packed record
buffer_id: UInt64;
base_addr: UInt64;
offset: UInt64;
rank: UInt32;
element_count: UInt64;
shape_ptr: UInt64;
stride_ptr: UInt64;
governance: Byte;
ownership: Byte;
materialized: Byte;
state: Byte;
generation: UInt64;
end;
PTensorDescriptorC = ^TTensorDescriptorC;
{ ========================================================================
Result Structure
======================================================================== }
TTensorResultC = packed record
success: Int32;
error_code: UInt32;
error_msg: array[0..255] of AnsiChar;
end;
{ ========================================================================
Operation Result
======================================================================== }
TTensorOpResultC = packed record
descriptor: TTensorDescriptorC;
result: TTensorResultC;
end;
{ ========================================================================
Ownership State Enumeration
======================================================================== }
TOwnershipState = (
OWNERSHIP_OWNED = 0,
OWNERSHIP_SHARED = 1,
OWNERSHIP_EXCLUSIVE = 2
);
{ ========================================================================
Governance State Enumeration
======================================================================== }
TGovernanceState = (
GOVERNANCE_SHARED = 0, { B=0: shared/non-canonical }
GOVERNANCE_CANONICAL = 1 { B=1: canonical/contiguous }
);
{ ============================================================================
FFI Declarations — map to Rust tensor_ffi.rs
============================================================================ }
{ External C function declarations }
function tensor_allocate(
rank: UInt32;
shape: PUInt64;
element_size: UInt64
): TTensorOpResultC; cdecl; external;
function tensor_deallocate(
desc: PTensorDescriptorC
): TTensorResultC; cdecl; external;
function tensor_view(
source: PTensorDescriptorC;
start_indices: PUInt64;
end_indices: PUInt64
): TTensorOpResultC; cdecl; external;
function tensor_materialize(
source: PTensorDescriptorC
): TTensorOpResultC; cdecl; external;
function tensor_validate_generation(
desc: PTensorDescriptorC
): TTensorResultC; cdecl; external;
function tensor_load(
desc: PTensorDescriptorC;
indices: PUInt64;
out_value: PSingle
): TTensorResultC; cdecl; external;
function tensor_store(
desc: PTensorDescriptorC;
indices: PUInt64;
value: Single
): TTensorResultC; cdecl; external;
function tensor_info(
desc: PTensorDescriptorC;
out_buffer: PByte;
out_len: SizeInt
): TTensorResultC; cdecl; external;
{ ============================================================================
Pascal Wrapper Functions (idiomatic error handling)
============================================================================ }
type
EFTensorError = class(Exception);
{ Allocate tensor, raise exception on failure }
procedure FTensorAllocate(
out Desc: TTensorDescriptorC;
Rank: UInt32;
const Shape: array of UInt64;
ElementSize: UInt64 = 4
);
{ Deallocate tensor, raise exception on failure }
procedure FTensorDeallocate(const Desc: TTensorDescriptorC);
{ Create view, raise exception on failure }
procedure FTensorView(
out ViewDesc: TTensorDescriptorC;
const Source: TTensorDescriptorC;
const StartIndices, EndIndices: array of UInt64
);
{ Materialize view, raise exception on failure }
procedure FTensorMaterialize(
out MatDesc: TTensorDescriptorC;
const Source: TTensorDescriptorC
);
{ Validate generation (stale descriptor check) }
function FTensorValidateGeneration(const Desc: TTensorDescriptorC): Boolean;
{ Load element value }
function FTensorLoad(
const Desc: TTensorDescriptorC;
const Indices: array of UInt64
): Single;
{ Store element value }
procedure FTensorStore(
const Desc: TTensorDescriptorC;
const Indices: array of UInt64;
Value: Single
);
{ Get info string (for debugging) }
function FTensorInfo(const Desc: TTensorDescriptorC): String;
{ Helper: check result and raise on error }
procedure CheckFTensorResult(const Result: TTensorResultC; const Context: String);
implementation
procedure CheckFTensorResult(const Result: TTensorResultC; const Context: String);
var
ErrorMsg: String;
begin
if Result.success = 0 then
begin
SetLength(ErrorMsg, 256);
ErrorMsg := PAnsiChar(@Result.error_msg[0]);
raise EFTensorError.Create(
Format('[%s] Error %d: %s', [Context, Result.error_code, ErrorMsg])
);
end;
end;
procedure FTensorAllocate(
out Desc: TTensorDescriptorC;
Rank: UInt32;
const Shape: array of UInt64;
ElementSize: UInt64 = 4
);
var
OpResult: TTensorOpResultC;
ShapeArray: array of UInt64;
I: Integer;
begin
if Rank = 0 then
raise EFTensorError.Create('Rank must be > 0');
if Rank <> High(Shape) + 1 then
raise EFTensorError.Create('Shape array length mismatch');
{ Copy shape to heap }
SetLength(ShapeArray, Rank);
for I := Low(Shape) to High(Shape) do
ShapeArray[I] := Shape[I];
{ Call FFI }
OpResult := tensor_allocate(Rank, @ShapeArray[0], ElementSize);
CheckFTensorResult(OpResult.result, 'tensor_allocate');
Desc := OpResult.descriptor;
end;
procedure FTensorDeallocate(const Desc: TTensorDescriptorC);
var
LocalDesc: TTensorDescriptorC;
Result: TTensorResultC;
begin
LocalDesc := Desc;
Result := tensor_deallocate(@LocalDesc);
CheckFTensorResult(Result, 'tensor_deallocate');
end;
procedure FTensorView(
out ViewDesc: TTensorDescriptorC;
const Source: TTensorDescriptorC;
const StartIndices, EndIndices: array of UInt64
);
var
OpResult: TTensorOpResultC;
SrcCopy: TTensorDescriptorC;
StartArray, EndArray: array of UInt64;
I: Integer;
begin
if High(StartIndices) <> High(EndIndices) then
raise EFTensorError.Create('Index array length mismatch');
if High(StartIndices) + 1 <> Source.rank then
raise EFTensorError.Create('Index count mismatch with tensor rank');
SetLength(StartArray, Source.rank);
SetLength(EndArray, Source.rank);
for I := Low(StartIndices) to High(StartIndices) do
begin
StartArray[I] := StartIndices[I];
EndArray[I] := EndIndices[I];
end;
SrcCopy := Source;
OpResult := tensor_view(@SrcCopy, @StartArray[0], @EndArray[0]);
CheckFTensorResult(OpResult.result, 'tensor_view');
ViewDesc := OpResult.descriptor;
end;
procedure FTensorMaterialize(
out MatDesc: TTensorDescriptorC;
const Source: TTensorDescriptorC
);
var
OpResult: TTensorOpResultC;
SrcCopy: TTensorDescriptorC;
begin
SrcCopy := Source;
OpResult := tensor_materialize(@SrcCopy);
CheckFTensorResult(OpResult.result, 'tensor_materialize');
MatDesc := OpResult.descriptor;
end;
function FTensorValidateGeneration(const Desc: TTensorDescriptorC): Boolean;
var
LocalDesc: TTensorDescriptorC;
Result: TTensorResultC;
begin
LocalDesc := Desc;
Result := tensor_validate_generation(@LocalDesc);
Exit(Result.success <> 0);
end;
function FTensorLoad(
const Desc: TTensorDescriptorC;
const Indices: array of UInt64
): Single;
var
LocalDesc: TTensorDescriptorC;
IndicesArray: array of UInt64;
Value: Single;
Result: TTensorResultC;
I: Integer;
begin
if High(Indices) + 1 <> Desc.rank then
raise EFTensorError.Create('Index count mismatch');
SetLength(IndicesArray, Desc.rank);
for I := Low(Indices) to High(Indices) do
IndicesArray[I] := Indices[I];
LocalDesc := Desc;
Result := tensor_load(@LocalDesc, @IndicesArray[0], @Value);
CheckFTensorResult(Result, 'tensor_load');
Exit(Value);
end;
procedure FTensorStore(
const Desc: TTensorDescriptorC;
const Indices: array of UInt64;
Value: Single
);
var
LocalDesc: TTensorDescriptorC;
IndicesArray: array of UInt64;
Result: TTensorResultC;
I: Integer;
begin
if High(Indices) + 1 <> Desc.rank then
raise EFTensorError.Create('Index count mismatch');
SetLength(IndicesArray, Desc.rank);
for I := Low(Indices) to High(Indices) do
IndicesArray[I] := Indices[I];
LocalDesc := Desc;
Result := tensor_store(@LocalDesc, @IndicesArray[0], Value);
CheckFTensorResult(Result, 'tensor_store');
end;
function FTensorInfo(const Desc: TTensorDescriptorC): String;
var
LocalDesc: TTensorDescriptorC;
Buffer: array[0..511] of AnsiChar;
Result: TTensorResultC;
begin
FillChar(Buffer, SizeOf(Buffer), 0);
LocalDesc := Desc;
Result := tensor_info(@LocalDesc, PByte(@Buffer), SizeOf(Buffer));
CheckFTensorResult(Result, 'tensor_info');
Exit(String(PAnsiChar(@Buffer)));
end;
end.
|