File size: 19,363 Bytes
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 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 | (* ========================================================================
* SOVEREIGN LEVIATHAN NODE LICENSE
* License-ID: SL-AGPL3-001 | Covenant-Version: 1.0
* Copyright (C) 2026 SnapKittyWest. Ahmad Ali Parr, Bel Esprit D'Accord Irrevocable Trust.
* ========================================================================
*
* This file is a covered work under the GNU Affero General Public License,
* version 3, together with the Sovereign Leviathan additional terms.
*
* Hark, though this node be but a spark,
* Its covenant endureth through the dark.
*
* Ignorantia juris non excusat.
* ======================================================================== *)
unit TensorCore;
{$mode objfpc}{$H+}
interface
uses
SysUtils,
Math;
const
TENSORCORE_LINE_BUDGET = 250;
type
TFloatArray = array of Double;
TIntArray = array of Integer;
{ Explicit tensor record.
Data is row-major and Shape contains each dimension. }
TTensor = record
Data: TFloatArray;
Shape: TIntArray;
end;
function TensorCreate(const Shape: array of Integer): TTensor;
function TensorScalar(const Value: Double): TTensor;
function TensorVector(const Values: array of Double): TTensor;
function TensorMatrix(const Rows, Cols: Integer): TTensor;
function TensorRank(const T: TTensor): Integer;
function TensorSize(const T: TTensor): Integer;
function TensorDim(const T: TTensor; const Axis: Integer): Integer;
procedure TensorFill(var T: TTensor; const Value: Double);
procedure TensorZeros(var T: TTensor);
procedure TensorOnes(var T: TTensor);
function TensorClone(const T: TTensor): TTensor;
function TensorGet(const T: TTensor; const Index: Integer): Double;
procedure TensorSet(var T: TTensor; const Index: Integer; const Value: Double);
function TensorOffset2D(
const T: TTensor;
const Row, Col: Integer
): Integer;
function TensorOffset3D(
const T: TTensor;
const A, B, C: Integer
): Integer;
function TensorGet2D(
const T: TTensor;
const Row, Col: Integer
): Double;
procedure TensorSet2D(
var T: TTensor;
const Row, Col: Integer;
const Value: Double
);
function TensorGet3D(
const T: TTensor;
const A, B, C: Integer
): Double;
procedure TensorSet3D(
var T: TTensor;
const A, B, C: Integer;
const Value: Double
);
function TensorAdd(
const A, B: TTensor
): TTensor;
function TensorSub(
const A, B: TTensor
): TTensor;
function TensorScale(
const A: TTensor;
const S: Double
): TTensor;
function TensorHadamard(
const A, B: TTensor
): TTensor;
function TensorAddInPlace(
var A: TTensor;
const B: TTensor
): Boolean;
function TensorScaleInPlace(
var A: TTensor;
const S: Double
): Boolean;
function TensorMatMul(
const A, B: TTensor
): TTensor;
function TensorTranspose2D(
const A: TTensor
): TTensor;
function TensorTranspose(
const A: TTensor;
const Axis0, Axis1: Integer
): TTensor;
function TensorReshape(
const A: TTensor;
const NewShape: array of Integer
): TTensor;
function TensorFlatten(
const A: TTensor
): TTensor;
function TensorDot(
const A, B: TTensor
): Double;
function TensorSum(
const A: TTensor
): Double;
function TensorMean(
const A: TTensor
): Double;
function TensorMax(
const A: TTensor
): Double;
function TensorArgMax(
const A: TTensor
): Integer;
procedure TensorRandomSeed(
const Seed: Integer
);
function TensorRandomUniform(
const Shape: array of Integer;
const MinValue, MaxValue: Double
): TTensor;
function TensorRandomNormal(
const Shape: array of Integer;
const Mean, StdDev: Double
): TTensor;
function TensorXavierUniform(
const Rows, Cols: Integer
): TTensor;
function TensorXavierNormal(
const Rows, Cols: Integer
): TTensor;
function TensorIsFinite(
const A: TTensor
): Boolean;
function TensorMaxAbs(
const A: TTensor
): Double;
function TensorL2Norm(
const A: TTensor
): Double;
function TensorSameShape(
const A, B: TTensor
): Boolean;
function TensorShapeString(
const A: TTensor
): string;
procedure TensorSave(
const FileName: string;
const A: TTensor
);
function TensorLoad(
const FileName: string
): TTensor;
procedure TensorAssert(
const Condition: Boolean;
const Message: string
);
implementation
function ProductOfShape(
const Shape: array of Integer
): Integer;
var
I: Integer;
begin
Result := 1;
for I := Low(Shape) to High(Shape) do
begin
if Shape[I] <= 0 then
raise EArgumentException.Create(
'Tensor dimensions must be positive'
);
Result := Result * Shape[I];
end;
end;
function ProductOfShapeArray(
const Shape: TIntArray
): Integer;
var
I: Integer;
begin
Result := 1;
for I := Low(Shape) to High(Shape) do
begin
if Shape[I] <= 0 then
raise EArgumentException.Create(
'Tensor dimensions must be positive'
);
Result := Result * Shape[I];
end;
end;
procedure CheckSameShape(
const A, B: TTensor
);
begin
if not TensorSameShape(A, B) then
raise EArgumentException.Create(
'Tensor shape mismatch: ' +
TensorShapeString(A) +
' versus ' +
TensorShapeString(B)
);
end;
procedure Check2D(
const T: TTensor;
const Name: string
);
begin
if TensorRank(T) <> 2 then
raise EArgumentException.Create(
Name + ' must be rank 2'
);
end;
function TensorCreate(
const Shape: array of Integer
): TTensor;
var
I: Integer;
begin
SetLength(Result.Shape, Length(Shape));
for I := Low(Shape) to High(Shape) do
Result.Shape[I] := Shape[I];
SetLength(
Result.Data,
ProductOfShape(Shape)
);
TensorZeros(Result);
end;
function TensorScalar(
const Value: Double
): TTensor;
begin
Result := TensorCreate([1]);
Result.Data[0] := Value;
end;
function TensorVector(
const Values: array of Double
): TTensor;
var
I: Integer;
begin
Result := TensorCreate([Length(Values)]);
for I := Low(Values) to High(Values) do
Result.Data[I] := Values[I];
end;
function TensorMatrix(
const Rows, Cols: Integer
): TTensor;
begin
Result := TensorCreate([Rows, Cols]);
end;
function TensorRank(
const T: TTensor
): Integer;
begin
Result := Length(T.Shape);
end;
function TensorSize(
const T: TTensor
): Integer;
begin
Result := Length(T.Data);
end;
function TensorDim(
const T: TTensor;
const Axis: Integer
): Integer;
begin
if (Axis < 0) or (Axis >= TensorRank(T)) then
raise EArgumentOutOfRangeException.Create(
'Tensor axis out of range'
);
Result := T.Shape[Axis];
end;
procedure TensorFill(
var T: TTensor;
const Value: Double
);
var
I: Integer;
begin
for I := 0 to High(T.Data) do
T.Data[I] := Value;
end;
procedure TensorZeros(
var T: TTensor
);
begin
TensorFill(T, 0.0);
end;
procedure TensorOnes(
var T: TTensor
);
begin
TensorFill(T, 1.0);
end;
function TensorClone(
const T: TTensor
): TTensor;
begin
Result.Shape := Copy(
T.Shape,
0,
Length(T.Shape)
);
Result.Data := Copy(
T.Data,
0,
Length(T.Data)
);
end;
function TensorGet(
const T: TTensor;
const Index: Integer
): Double;
begin
if (Index < 0) or
(Index >= Length(T.Data)) then
raise EArgumentOutOfRangeException.Create(
'Tensor data index out of range'
);
Result := T.Data[Index];
end;
procedure TensorSet(
var T: TTensor;
const Index: Integer;
const Value: Double
);
begin
if (Index < 0) or
(Index >= Length(T.Data)) then
raise EArgumentOutOfRangeException.Create(
'Tensor data index out of range'
);
T.Data[Index] := Value;
end;
function TensorOffset2D(
const T: TTensor;
const Row, Col: Integer
): Integer;
begin
Check2D(T, 'Tensor');
if (Row < 0) or
(Row >= T.Shape[0]) or
(Col < 0) or
(Col >= T.Shape[1]) then
raise EArgumentOutOfRangeException.Create(
'2D tensor index out of range'
);
Result :=
Row * T.Shape[1] +
Col;
end;
function TensorOffset3D(
const T: TTensor;
const A, B, C: Integer
): Integer;
begin
if TensorRank(T) <> 3 then
raise EArgumentException.Create(
'Tensor must be rank 3'
);
if (A < 0) or
(A >= T.Shape[0]) or
(B < 0) or
(B >= T.Shape[1]) or
(C < 0) or
(C >= T.Shape[2]) then
raise EArgumentOutOfRangeException.Create(
'3D tensor index out of range'
);
Result :=
(A * T.Shape[1] * T.Shape[2]) +
(B * T.Shape[2]) +
C;
end;
function TensorGet2D(
const T: TTensor;
const Row, Col: Integer
): Double;
begin
Result := T.Data[
TensorOffset2D(T, Row, Col)
];
end;
procedure TensorSet2D(
var T: TTensor;
const Row, Col: Integer;
const Value: Double
);
begin
T.Data[
TensorOffset2D(T, Row, Col)
] := Value;
end;
function TensorGet3D(
const T: TTensor;
const A, B, C: Integer
): Double;
begin
Result := T.Data[
TensorOffset3D(T, A, B, C)
];
end;
procedure TensorSet3D(
var T: TTensor;
const A, B, C: Integer;
const Value: Double
);
begin
T.Data[
TensorOffset3D(T, A, B, C)
] := Value;
end;
function TensorAdd(
const A, B: TTensor
): TTensor;
var
I: Integer;
begin
CheckSameShape(A, B);
Result := TensorClone(A);
for I := 0 to High(Result.Data) do
Result.Data[I] :=
A.Data[I] +
B.Data[I];
end;
function TensorSub(
const A, B: TTensor
): TTensor;
var
I: Integer;
begin
CheckSameShape(A, B);
Result := TensorClone(A);
for I := 0 to High(Result.Data) do
Result.Data[I] :=
A.Data[I] -
B.Data[I];
end;
function TensorScale(
const A: TTensor;
const S: Double
): TTensor;
var
I: Integer;
begin
Result := TensorClone(A);
for I := 0 to High(Result.Data) do
Result.Data[I] :=
A.Data[I] * S;
end;
function TensorHadamard(
const A, B: TTensor
): TTensor;
var
I: Integer;
begin
CheckSameShape(A, B);
Result := TensorClone(A);
for I := 0 to High(Result.Data) do
Result.Data[I] :=
A.Data[I] *
B.Data[I];
end;
function TensorAddInPlace(
var A: TTensor;
const B: TTensor
): Boolean;
var
I: Integer;
begin
Result := False;
if not TensorSameShape(A, B) then
Exit;
for I := 0 to High(A.Data) do
A.Data[I] :=
A.Data[I] +
B.Data[I];
Result := True;
end;
function TensorScaleInPlace(
var A: TTensor;
const S: Double
): Boolean;
var
I: Integer;
begin
for I := 0 to High(A.Data) do
A.Data[I] :=
A.Data[I] * S;
Result := True;
end;
function TensorMatMul(
const A, B: TTensor
): TTensor;
var
M, K, N: Integer;
I, J, P: Integer;
Sum: Double;
begin
Check2D(A, 'A');
Check2D(B, 'B');
M := A.Shape[0];
K := A.Shape[1];
if B.Shape[0] <> K then
raise EArgumentException.Create(
'MatMul inner dimensions do not match'
);
N := B.Shape[1];
Result := TensorMatrix(M, N);
for I := 0 to M - 1 do
for J := 0 to N - 1 do
begin
Sum := 0.0;
for P := 0 to K - 1 do
Sum := Sum +
A.Data[I * K + P] *
B.Data[P * N + J];
Result.Data[I * N + J] :=
Sum;
end;
end;
function TensorTranspose2D(
const A: TTensor
): TTensor;
var
I, J: Integer;
begin
Check2D(A, 'Tensor');
Result :=
TensorMatrix(
A.Shape[1],
A.Shape[0]
);
for I := 0 to A.Shape[0] - 1 do
for J := 0 to A.Shape[1] - 1 do
Result.Data[
J * A.Shape[0] + I
] :=
A.Data[
I * A.Shape[1] + J
];
end;
function TensorTranspose(
const A: TTensor;
const Axis0, Axis1: Integer
): TTensor;
var
R, I, J, K: Integer;
NewShape: TIntArray;
SourceIndex, TargetIndex: Integer;
StrideSource, StrideTarget: TIntArray;
begin
R := TensorRank(A);
if (Axis0 < 0) or
(Axis0 >= R) or
(Axis1 < 0) or
(Axis1 >= R) then
raise EArgumentOutOfRangeException.Create(
'Transpose axis out of range'
);
NewShape :=
Copy(A.Shape, 0, R);
NewShape[Axis0] := A.Shape[Axis1];
NewShape[Axis1] := A.Shape[Axis0];
Result := TensorCreate(NewShape);
SetLength(
StrideSource,
R
);
SetLength(
StrideTarget,
R
);
StrideSource[R - 1] := 1;
StrideTarget[R - 1] := 1;
for I := R - 2 downto 0 do
begin
StrideSource[I] :=
StrideSource[I + 1] *
A.Shape[I + 1];
StrideTarget[I] :=
StrideTarget[I + 1] *
Result.Shape[I + 1];
end;
for I := 0 to TensorSize(A) - 1 do
begin
SourceIndex := I;
TargetIndex := 0;
for J := 0 to R - 1 do
begin
K := SourceIndex div StrideSource[J];
SourceIndex :=
SourceIndex mod StrideSource[J];
if J = Axis0 then
TargetIndex :=
TargetIndex +
K * StrideTarget[Axis1]
else if J = Axis1 then
TargetIndex :=
TargetIndex +
K * StrideTarget[Axis0]
else
TargetIndex :=
TargetIndex +
K * StrideTarget[J];
end;
Result.Data[TargetIndex] :=
A.Data[I];
end;
end;
function TensorReshape(
const A: TTensor;
const NewShape: array of Integer
): TTensor;
var
NewSize: Integer;
begin
NewSize :=
ProductOfShape(NewShape);
if NewSize <> TensorSize(A) then
raise EArgumentException.Create(
'Reshape changes tensor element count'
);
Result.Shape :=
Copy(
NewShape,
0,
Length(NewShape)
);
Result.Data :=
Copy(
A.Data,
0,
Length(A.Data)
);
end;
function TensorFlatten(
const A: TTensor
): TTensor;
begin
Result :=
TensorReshape(
A,
[TensorSize(A)]
);
end;
function TensorDot(
const A, B: TTensor
): Double;
var
I: Integer;
begin
CheckSameShape(A, B);
Result := 0.0;
for I := 0 to High(A.Data) do
Result :=
Result +
A.Data[I] *
B.Data[I];
end;
function TensorSum(
const A: TTensor
): Double;
var
I: Integer;
begin
Result := 0.0;
for I := 0 to High(A.Data) do
Result :=
Result + A.Data[I];
end;
function TensorMean(
const A: TTensor
): Double;
begin
if TensorSize(A) = 0 then
raise EArgumentException.Create(
'Mean of empty tensor'
);
Result :=
TensorSum(A) /
TensorSize(A);
end;
function TensorMax(
const A: TTensor
): Double;
var
I: Integer;
begin
if TensorSize(A) = 0 then
raise EArgumentException.Create(
'Maximum of empty tensor'
);
Result := A.Data[0];
for I := 1 to High(A.Data) do
if A.Data[I] > Result then
Result := A.Data[I];
end;
function TensorArgMax(
const A: TTensor
): Integer;
var
I: Integer;
begin
if TensorSize(A) = 0 then
raise EArgumentException.Create(
'ArgMax of empty tensor'
);
Result := 0;
for I := 1 to High(A.Data) do
if A.Data[I] > A.Data[Result] then
Result := I;
end;
procedure TensorRandomSeed(
const Seed: Integer
);
begin
RandSeed := Seed;
end;
function RandomUnit: Double;
begin
Result := Random;
end;
function RandomGaussian: Double;
var
U1, U2: Double;
begin
U1 := RandomUnit;
if U1 <= 1E-12 then
U1 := 1E-12;
U2 := RandomUnit;
Result :=
Sqrt(
-2.0 * Ln(U1)
) *
Cos(
2.0 * Pi * U2
);
end;
function TensorRandomUniform(
const Shape: array of Integer;
const MinValue, MaxValue: Double
): TTensor;
var
I: Integer;
begin
Result :=
TensorCreate(Shape);
for I := 0 to High(Result.Data) do
Result.Data[I] :=
MinValue +
RandomUnit *
(MaxValue - MinValue);
end;
function TensorRandomNormal(
const Shape: array of Integer;
const Mean, StdDev: Double
): TTensor;
var
I: Integer;
begin
Result :=
TensorCreate(Shape);
for I := 0 to High(Result.Data) do
Result.Data[I] :=
Mean +
StdDev *
RandomGaussian;
end;
function TensorXavierUniform(
const Rows, Cols: Integer
): TTensor;
var
Limit: Double;
begin
Limit :=
Sqrt(
6.0 /
(Rows + Cols)
);
Result :=
TensorRandomUniform(
[Rows, Cols],
-Limit,
Limit
);
end;
function TensorXavierNormal(
const Rows, Cols: Integer
): TTensor;
var
StdDev: Double;
begin
StdDev :=
Sqrt(
2.0 /
(Rows + Cols)
);
Result :=
TensorRandomNormal(
[Rows, Cols],
0.0,
StdDev
);
end;
function TensorIsFinite(
const A: TTensor
): Boolean;
var
I: Integer;
begin
Result := True;
for I := 0 to High(A.Data) do
if IsNan(A.Data[I]) or
IsInfinite(A.Data[I]) then
Exit(False);
end;
function TensorMaxAbs(
const A: TTensor
): Double;
var
I: Integer;
V: Double;
begin
Result := 0.0;
for I := 0 to High(A.Data) do
begin
V := Abs(A.Data[I]);
if V > Result then
Result := V;
end;
end;
function TensorL2Norm(
const A: TTensor
): Double;
var
I: Integer;
SumSquares: Double;
begin
SumSquares := 0.0;
for I := 0 to High(A.Data) do
SumSquares :=
SumSquares +
A.Data[I] *
A.Data[I];
Result := Sqrt(SumSquares);
end;
function TensorSameShape(
const A, B: TTensor
): Boolean;
var
I: Integer;
begin
Result :=
TensorRank(A) =
TensorRank(B);
if not Result then
Exit;
for I := 0 to TensorRank(A) - 1 do
if A.Shape[I] <>
B.Shape[I] then
Exit(False);
end;
function TensorShapeString(
const A: TTensor
): string;
var
I: Integer;
begin
Result := '[';
for I := 0 to High(A.Shape) do
begin
if I > 0 then
Result := Result + 'x';
Result :=
Result +
IntToStr(A.Shape[I]);
end;
Result := Result + ']';
end;
procedure TensorSave(
const FileName: string;
const A: TTensor
);
var
F: TFileStream;
Rank: Integer;
DataCount: Integer;
begin
F :=
TFileStream.Create(
FileName,
fmCreate
);
try
Rank := TensorRank(A);
DataCount := TensorSize(A);
F.WriteBuffer(
Rank,
SizeOf(Rank)
);
if Rank > 0 then
F.WriteBuffer(
A.Shape[0],
Rank * SizeOf(Integer)
);
F.WriteBuffer(
DataCount,
SizeOf(DataCount)
);
if DataCount > 0 then
F.WriteBuffer(
A.Data[0],
DataCount * SizeOf(Double)
);
finally
F.Free;
end;
end;
function TensorLoad(
const FileName: string
): TTensor;
var
F: TFileStream;
Rank: Integer;
DataCount: Integer;
I: Integer;
begin
F :=
TFileStream.Create(
FileName,
fmOpenRead or fmShareDenyWrite
);
try
F.ReadBuffer(
Rank,
SizeOf(Rank)
);
if Rank < 0 then
raise EInvalidData.Create(
'Invalid tensor rank'
);
SetLength(
Result.Shape,
Rank
);
if Rank > 0 then
F.ReadBuffer(
Result.Shape[0],
Rank * SizeOf(Integer)
);
for I := 0 to Rank - 1 do
if Result.Shape[I] <= 0 then
raise EInvalidData.Create(
'Invalid tensor dimension'
);
F.ReadBuffer(
DataCount,
SizeOf(DataCount)
);
if DataCount <> ProductOfShapeArray(Result.Shape) then
raise EInvalidData.Create(
'Tensor data count does not match shape'
);
SetLength(
Result.Data,
DataCount
);
if DataCount > 0 then
F.ReadBuffer(
Result.Data[0],
DataCount * SizeOf(Double)
);
finally
F.Free;
end;
end;
procedure TensorAssert(
const Condition: Boolean;
const Message: string
);
begin
if not Condition then
raise EAssertionFailed.Create(
Message
);
end;
end.
|