Spaces:
Running
Running
File size: 53,177 Bytes
ca97aa9 |
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 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 |
/**
* @file Helper module for `Tensor` processing.
*
* These functions and classes are only used internally,
* meaning an end-user shouldn't need to access anything here.
*
* @module utils/tensor
*/
import {
interpolate_data,
max,
min,
permute_data
} from './maths.js';
import {
Tensor as ONNXTensor, isONNXTensor,
} from '../backends/onnx.js';
import { TensorOpRegistry } from '../ops/registry.js';
export const DataTypeMap = Object.freeze({
float32: Float32Array,
// @ts-ignore ts(2552) Limited availability of Float16Array across browsers:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float16Array
float16: typeof Float16Array !== "undefined" ? Float16Array: Uint16Array,
float64: Float64Array,
string: Array, // string[]
int8: Int8Array,
uint8: Uint8Array,
int16: Int16Array,
uint16: Uint16Array,
int32: Int32Array,
uint32: Uint32Array,
int64: BigInt64Array,
uint64: BigUint64Array,
bool: Uint8Array,
uint4: Uint8Array,
int4: Int8Array,
});
/**
* @typedef {keyof typeof DataTypeMap} DataType
* @typedef {import('./maths.js').AnyTypedArray | any[]} DataArray
*/
export class Tensor {
/** @type {number[]} Dimensions of the tensor. */
get dims() {
// @ts-ignore
return this.ort_tensor.dims;
}
set dims(value) {
// FIXME: ONNXTensor declares dims as readonly so one needs to use the constructor() if dims change.
// @ts-ignore
this.ort_tensor.dims = value;
}
/** @type {DataType} Type of the tensor. */
get type() {
return this.ort_tensor.type;
};
/** @type {DataArray} The data stored in the tensor. */
get data() {
return this.ort_tensor.data;
}
/** @type {number} The number of elements in the tensor. */
get size() {
return this.ort_tensor.size;
};
/** @type {string} The location of the tensor data. */
get location() {
return this.ort_tensor.location;
};
ort_tensor;
/**
* Create a new Tensor or copy an existing Tensor.
* @param {[DataType, DataArray, number[]]|[ONNXTensor]} args
*/
constructor(...args) {
if (isONNXTensor(args[0])) {
this.ort_tensor = /** @type {ONNXTensor} */ (args[0]);
} else {
// Create new tensor
this.ort_tensor = new ONNXTensor(
/** @type {DataType} */(args[0]),
// @ts-expect-error ts(2769) Type 'number' is not assignable to type 'bigint'.
/** @type {Exclude<import('./maths.js').AnyTypedArray, Uint8ClampedArray>} */(args[1]),
args[2],
);
}
return new Proxy(this, {
get: (obj, key) => {
if (typeof key === 'string') {
let index = Number(key);
if (Number.isInteger(index)) {
// key is an integer (i.e., index)
return obj._getitem(index);
}
}
// @ts-ignore
return obj[key];
},
set: (obj, key, value) => {
// TODO allow setting of data
// @ts-ignore
return obj[key] = value;
}
});
}
dispose() {
this.ort_tensor.dispose();
// this.ort_tensor = undefined;
}
/**
* Returns an iterator object for iterating over the tensor data in row-major order.
* If the tensor has more than one dimension, the iterator will yield subarrays.
* @returns {Iterator} An iterator object for iterating over the tensor data in row-major order.
*/
*[Symbol.iterator]() {
const [iterLength, ...iterDims] = this.dims;
if (iterDims.length > 0) {
const iterSize = iterDims.reduce((a, b) => a * b);
for (let i = 0; i < iterLength; ++i) {
yield this._subarray(i, iterSize, iterDims);
}
} else {
yield* this.data
}
}
/**
* Index into a Tensor object.
* @param {number} index The index to access.
* @returns {Tensor} The data at the specified index.
*/
_getitem(index) {
const [iterLength, ...iterDims] = this.dims;
index = safeIndex(index, iterLength);
if (iterDims.length > 0) {
const iterSize = iterDims.reduce((a, b) => a * b);
return this._subarray(index, iterSize, iterDims);
} else {
return new Tensor(this.type, [this.data[index]], iterDims);
}
}
/**
* @param {number|bigint} item The item to search for in the tensor
* @returns {number} The index of the first occurrence of item in the tensor data.
*/
indexOf(item) {
const this_data = this.data;
for (let index = 0; index < this_data.length; ++index) {
// Note: == instead of === so we can match Ints with BigInts
if (this_data[index] == item) {
return index;
}
}
return -1;
}
/**
* @param {number} index
* @param {number} iterSize
* @param {any} iterDims
* @returns {Tensor}
*/
_subarray(index, iterSize, iterDims) {
const o1 = index * iterSize;
const o2 = (index + 1) * iterSize;
// We use subarray if available (typed array), otherwise we use slice (normal array)
const data =
('subarray' in this.data)
? this.data.subarray(o1, o2)
: this.data.slice(o1, o2);
return new Tensor(this.type, data, iterDims);
}
/**
* Returns the value of this tensor as a standard JavaScript Number. This only works
* for tensors with one element. For other cases, see `Tensor.tolist()`.
* @returns {number|bigint} The value of this tensor as a standard JavaScript Number.
* @throws {Error} If the tensor has more than one element.
*/
item() {
const this_data = this.data;
if (this_data.length !== 1) {
throw new Error(`a Tensor with ${this_data.length} elements cannot be converted to Scalar`);
}
return this_data[0];
}
/**
* Convert tensor data to a n-dimensional JS list
* @returns {Array}
*/
tolist() {
return reshape(this.data, this.dims)
}
/**
* Return a new Tensor with the sigmoid function applied to each element.
* @returns {Tensor} The tensor with the sigmoid function applied.
*/
sigmoid() {
return this.clone().sigmoid_();
}
/**
* Applies the sigmoid function to the tensor in place.
* @returns {Tensor} Returns `this`.
*/
sigmoid_() {
const this_data = this.data;
for (let i = 0; i < this_data.length; ++i) {
this_data[i] = 1 / (1 + Math.exp(-this_data[i]));
}
return this;
}
/**
* Return a new Tensor with a callback function applied to each element.
* @param {Function} callback - The function to apply to each element. It should take three arguments:
* the current element, its index, and the tensor's data array.
* @returns {Tensor} A new Tensor with the callback function applied to each element.
*/
map(callback) {
return this.clone().map_(callback);
}
/**
* Apply a callback function to each element of the tensor in place.
* @param {Function} callback - The function to apply to each element. It should take three arguments:
* the current element, its index, and the tensor's data array.
* @returns {Tensor} Returns `this`.
*/
map_(callback) {
const this_data = this.data;
for (let i = 0; i < this_data.length; ++i) {
this_data[i] = callback(this_data[i], i, this_data);
}
return this;
}
/**
* Return a new Tensor with every element multiplied by a constant.
* @param {number} val The value to multiply by.
* @returns {Tensor} The new tensor.
*/
mul(val) {
return this.clone().mul_(val);
}
/**
* Multiply the tensor by a constant in place.
* @param {number} val The value to multiply by.
* @returns {Tensor} Returns `this`.
*/
mul_(val) {
const this_data = this.data;
for (let i = 0; i < this_data.length; ++i) {
this_data[i] *= val;
}
return this;
}
/**
* Return a new Tensor with every element divided by a constant.
* @param {number} val The value to divide by.
* @returns {Tensor} The new tensor.
*/
div(val) {
return this.clone().div_(val);
}
/**
* Divide the tensor by a constant in place.
* @param {number} val The value to divide by.
* @returns {Tensor} Returns `this`.
*/
div_(val) {
const this_data = this.data;
for (let i = 0; i < this_data.length; ++i) {
this_data[i] /= val;
}
return this;
}
/**
* Return a new Tensor with every element added by a constant.
* @param {number} val The value to add by.
* @returns {Tensor} The new tensor.
*/
add(val) {
return this.clone().add_(val);
}
/**
* Add the tensor by a constant in place.
* @param {number} val The value to add by.
* @returns {Tensor} Returns `this`.
*/
add_(val) {
const this_data = this.data;
for (let i = 0; i < this_data.length; ++i) {
this_data[i] += val;
}
return this;
}
/**
* Return a new Tensor with every element subtracted by a constant.
* @param {number} val The value to subtract by.
* @returns {Tensor} The new tensor.
*/
sub(val) {
return this.clone().sub_(val);
}
/**
* Subtract the tensor by a constant in place.
* @param {number} val The value to subtract by.
* @returns {Tensor} Returns `this`.
*/
sub_(val) {
const this_data = this.data;
for (let i = 0; i < this_data.length; ++i) {
this_data[i] -= val;
}
return this;
}
/**
* Creates a deep copy of the current Tensor.
* @returns {Tensor} A new Tensor with the same type, data, and dimensions as the original.
*/
clone() {
return new Tensor(this.type, this.data.slice(), this.dims.slice());
}
/**
* Performs a slice operation on the Tensor along specified dimensions.
*
* Consider a Tensor that has a dimension of [4, 7]:
* ```
* [ 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]
* ```
* We can slice against the two dims of row and column, for instance in this
* case we can start at the second element, and return to the second last,
* like this:
* ```
* tensor.slice([1, -1], [1, -1]);
* ```
* which would return:
* ```
* [ 9, 10, 11, 12, 13 ]
* [ 16, 17, 18, 19, 20 ]
* ```
*
* @param {...(number|number[]|null)} slices The slice specifications for each dimension.
* - If a number is given, then a single element is selected.
* - If an array of two numbers is given, then a range of elements [start, end (exclusive)] is selected.
* - If null is given, then the entire dimension is selected.
* @returns {Tensor} A new Tensor containing the selected elements.
* @throws {Error} If the slice input is invalid.
*/
slice(...slices) {
// This allows for slicing with ranges and numbers
const newTensorDims = [];
const newOffsets = [];
// slices is an array of numbers or arrays of numbers
// e.g., slices = [0, [1, 3], null, [0, 3]]
for (let sliceIndex = 0; sliceIndex < this.dims.length; ++sliceIndex) {
let slice = slices[sliceIndex];
if (slice === null || slice === undefined) {
// null or undefined means take the whole dimension
newOffsets.push([0, this.dims[sliceIndex]]);
newTensorDims.push(this.dims[sliceIndex]);
} else if (typeof slice === 'number') {
slice = safeIndex(slice, this.dims[sliceIndex], sliceIndex);
// A number means take a single element
newOffsets.push([slice, slice + 1]);
} else if (Array.isArray(slice) && slice.length === 2) {
// An array of length 2 means take a range of elements
let [start, end] = slice;
start = start === null
? 0
: safeIndex(start, this.dims[sliceIndex], sliceIndex, false);
end = end === null
? this.dims[sliceIndex]
: safeIndex(end, this.dims[sliceIndex], sliceIndex, false);
if (start > end) {
throw new Error(`Invalid slice: ${slice}`);
}
const offsets = [
Math.max(start, 0),
Math.min(end, this.dims[sliceIndex])
];
newOffsets.push(offsets);
newTensorDims.push(offsets[1] - offsets[0]);
} else {
throw new Error(`Invalid slice: ${slice}`);
}
}
const newDims = newOffsets.map(([start, end]) => end - start);
const newBufferSize = newDims.reduce((a, b) => a * b);
const this_data = this.data;
// Allocate memory
// @ts-ignore
const data = new this_data.constructor(newBufferSize);
// Precompute strides
const stride = this.stride();
// Detect if the slice is contiguous
let isContiguous = true;
for (let i = 1; i < newDims.length; ++i) {
if (newOffsets[i][0] !== 0 || newOffsets[i][1] !== this.dims[i]) {
isContiguous = false;
break;
}
}
if (isContiguous) {
// Perform bulk copy for contiguous slices to improve performance
const start = newOffsets[0][0] * stride[0];
const end = newOffsets[0][1] * stride[0];
if (ArrayBuffer.isView(this_data)) {
// If this.data is a TypedArray, use subarray
// @ts-ignore
data.set(this_data.subarray(start, end));
} else if (Array.isArray(this_data)) {
// If this.data is a plain array, use slice
const slicedData = this_data.slice(start, end);
for (let i = 0; i < slicedData.length; ++i) {
data[i] = slicedData[i];
}
} else {
throw new Error("Unsupported data type for slicing");
}
} else {
// Fallback to manual copying for non-contiguous slices
for (let i = 0; i < newBufferSize; ++i) {
let originalIndex = 0;
for (let j = newDims.length - 1, num = i; j >= 0; --j) {
const size = newDims[j];
originalIndex += ((num % size) + newOffsets[j][0]) * stride[j];
num = Math.floor(num / size);
}
data[i] = this_data[originalIndex];
}
}
return new Tensor(this.type, data, newTensorDims);
}
/**
* Return a permuted version of this Tensor, according to the provided dimensions.
* @param {...number} dims Dimensions to permute.
* @returns {Tensor} The permuted tensor.
*/
permute(...dims) {
return permute(this, dims);
}
// TODO: implement transpose. For now (backwards compatibility), it's just an alias for permute()
transpose(...dims) {
return this.permute(...dims);
}
/**
* Returns the sum of each row of the input tensor in the given dimension dim.
*
* @param {number} [dim=null] The dimension or dimensions to reduce. If `null`, all dimensions are reduced.
* @param {boolean} keepdim Whether the output tensor has `dim` retained or not.
* @returns The summed tensor
*/
sum(dim = null, keepdim = false) {
return this.norm(1, dim, keepdim);
}
/**
* Returns the matrix norm or vector norm of a given tensor.
* @param {number|string} [p='fro'] The order of norm
* @param {number} [dim=null] Specifies which dimension of the tensor to calculate the norm across.
* If dim is None, the norm will be calculated across all dimensions of input.
* @param {boolean} [keepdim=false] Whether the output tensors have dim retained or not.
* @returns {Tensor} The norm of the tensor.
*/
norm(p = 'fro', dim = null, keepdim = false) {
if (p === 'fro') {
// NOTE: Since we only support integer dims, Frobenius norm produces the same result as p=2.
p = 2;
} else if (typeof p === 'string') {
throw Error(`Unsupported norm: ${p}`);
}
const this_data = this.data;
const fn = (a, b) => a + (b ** p);
if (dim === null) {
// @ts-ignore
const val = this_data.reduce(fn, 0) ** (1 / p);
return new Tensor(this.type, [val], []);
}
const [type, result, resultDims] = reduce_helper(fn, this, dim, keepdim);
if (p !== 1) {
for (let i = 0; i < result.length; ++i) {
result[i] = result[i] ** (1 / p);
}
}
return new Tensor(type, result, resultDims);
}
/**
* Performs `L_p` normalization of inputs over specified dimension. Operates in place.
* @param {number} [p=2] The exponent value in the norm formulation
* @param {number} [dim=1] The dimension to reduce
* @returns {Tensor} `this` for operation chaining.
*/
normalize_(p = 2.0, dim = 1) {
dim = safeIndex(dim, this.dims.length);
const norm = this.norm(p, dim, true);
const this_data = this.data;
const norm_data = norm.data;
for (let i = 0; i < this_data.length; ++i) {
// Calculate the index in the resulting array
let resultIndex = 0;
for (let j = this.dims.length - 1, num = i, resultMultiplier = 1; j >= 0; --j) {
const size = this.dims[j];
if (j !== dim) {
const index = num % size;
resultIndex += index * resultMultiplier;
resultMultiplier *= this.dims[j];
}
num = Math.floor(num / size);
}
// Divide by normalized value
this_data[i] /= norm_data[resultIndex];
}
return this;
}
/**
* Performs `L_p` normalization of inputs over specified dimension.
* @param {number} [p=2] The exponent value in the norm formulation
* @param {number} [dim=1] The dimension to reduce
* @returns {Tensor} The normalized tensor.
*/
normalize(p = 2.0, dim = 1) {
return this.clone().normalize_(p, dim);
}
/**
* Compute and return the stride of this tensor.
* Stride is the jump necessary to go from one element to the next one in the specified dimension dim.
* @returns {number[]} The stride of this tensor.
*/
stride() {
return dimsToStride(this.dims);
}
/**
* Returns a tensor with all specified dimensions of input of size 1 removed.
*
* NOTE: The returned tensor shares the storage with the input tensor, so changing the contents of one will change the contents of the other.
* If you would like a copy, use `tensor.clone()` before squeezing.
*
* @param {number|number[]} [dim=null] If given, the input will be squeezed only in the specified dimensions.
* @returns {Tensor} The squeezed tensor
*/
squeeze(dim = null) {
return new Tensor(
this.type,
this.data,
calc_squeeze_dims(this.dims, dim)
)
}
/**
* In-place version of @see {@link Tensor.squeeze}
*/
squeeze_(dim = null) {
this.dims = calc_squeeze_dims(this.dims, dim);
return this;
}
/**
* Returns a new tensor with a dimension of size one inserted at the specified position.
*
* NOTE: The returned tensor shares the same underlying data with this tensor.
*
* @param {number} dim The index at which to insert the singleton dimension
* @returns {Tensor} The unsqueezed tensor
*/
unsqueeze(dim = null) {
return new Tensor(
this.type,
this.data,
calc_unsqueeze_dims(this.dims, dim)
);
}
/**
* In-place version of @see {@link Tensor.unsqueeze}
*/
unsqueeze_(dim = null) {
this.dims = calc_unsqueeze_dims(this.dims, dim);
return this;
}
/**
* In-place version of @see {@link Tensor.flatten}
*/
flatten_(start_dim = 0, end_dim = -1) {
// TODO validate inputs
end_dim = (end_dim + this.dims.length) % this.dims.length;
let dimsToKeepBefore = this.dims.slice(0, start_dim);
let dimsToFlatten = this.dims.slice(start_dim, end_dim + 1);
let dimsToKeepAfter = this.dims.slice(end_dim + 1);
this.dims = [...dimsToKeepBefore, dimsToFlatten.reduce((a, b) => a * b, 1), ...dimsToKeepAfter]
return this;
}
/**
* Flattens input by reshaping it into a one-dimensional tensor.
* If `start_dim` or `end_dim` are passed, only dimensions starting with `start_dim`
* and ending with `end_dim` are flattened. The order of elements in input is unchanged.
* @param {number} start_dim the first dim to flatten
* @param {number} end_dim the last dim to flatten
* @returns {Tensor} The flattened tensor.
*/
flatten(start_dim = 0, end_dim = -1) {
return this.clone().flatten_(start_dim, end_dim);
}
/**
* Returns a new tensor with the same data as the `self` tensor but of a different `shape`.
* @param {...number} dims the desired size
* @returns {Tensor} The tensor with the same data but different shape
*/
view(...dims) {
// TODO: validate dims
let inferredIndex = -1;
for (let i = 0; i < dims.length; ++i) {
if (dims[i] === -1) {
if (inferredIndex !== -1) {
throw new Error("Only one dimension can be inferred");
}
inferredIndex = i;
}
}
const this_data = this.data;
if (inferredIndex !== -1) {
// Some dimension must be inferred
const productOther = dims.reduce((product, curr, index) => {
return index !== inferredIndex ? product * curr : product
}, 1);
dims[inferredIndex] = this_data.length / productOther;
}
return new Tensor(this.type, this_data, dims); // NOTE: uses same underlying storage
}
neg_() {
const this_data = this.data;
for (let i = 0; i < this_data.length; ++i) {
this_data[i] = -this_data[i];
}
return this;
}
neg() {
return this.clone().neg_();
}
/**
* Computes input > val element-wise.
* @param {number} val The value to compare with.
* @returns {Tensor} A boolean tensor that is `true` where input is greater than other and `false` elsewhere.
*/
gt(val) {
const mask = new Uint8Array(this.data.length);
const this_data = this.data;
for (let i = 0; i < this_data.length; ++i) {
mask[i] = this_data[i] > val ? 1 : 0;
}
return new Tensor('bool', mask, this.dims);
}
/**
* Computes input < val element-wise.
* @param {number} val The value to compare with.
* @returns {Tensor} A boolean tensor that is `true` where input is less than other and `false` elsewhere.
*/
lt(val) {
const mask = new Uint8Array(this.data.length);
const this_data = this.data;
for (let i = 0; i < this_data.length; ++i) {
mask[i] = this_data[i] < val ? 1 : 0;
}
return new Tensor('bool', mask, this.dims);
}
/**
* In-place version of @see {@link Tensor.clamp}
*/
clamp_(min, max) {
const this_data = this.data;
for (let i = 0; i < this_data.length; ++i) {
this_data[i] = Math.min(Math.max(this_data[i], min), max);
}
return this;
}
/**
* Clamps all elements in input into the range [ min, max ]
* @param {number} min lower-bound of the range to be clamped to
* @param {number} max upper-bound of the range to be clamped to
* @returns {Tensor} the output tensor.
*/
clamp(min, max) {
return this.clone().clamp_(min, max);
}
/**
* In-place version of @see {@link Tensor.round}
*/
round_() {
const this_data = this.data;
for (let i = 0; i < this_data.length; ++i) {
this_data[i] = Math.round(this_data[i]);
}
return this;
}
/**
* Rounds elements of input to the nearest integer.
* @returns {Tensor} the output tensor.
*/
round() {
return this.clone().round_();
}
mean(dim = null, keepdim = false) {
return mean(this, dim, keepdim);
}
min(dim = null, keepdim = false) {
if (dim === null) {
// None to reduce over all dimensions.
const val = min(this.data)[0];
return new Tensor(this.type, [val], [/* scalar */]);
}
const [type, result, resultDims] = reduce_helper((a, b) => Math.min(a, b), this, dim, keepdim, Infinity);
return new Tensor(type, result, resultDims);
}
max(dim = null, keepdim = false) {
if (dim === null) {
// None to reduce over all dimensions.
const val = max(this.data)[0];
return new Tensor(this.type, [val], [/* scalar */]);
}
const [type, result, resultDims] = reduce_helper((a, b) => Math.max(a, b), this, dim, keepdim, -Infinity);
return new Tensor(type, result, resultDims);
}
argmin(dim = null, keepdim = false) {
if (dim !== null) {
throw new Error("`dim !== null` not yet implemented.");
}
const index = min(this.data)[1];
return new Tensor('int64', [BigInt(index)], []);
}
argmax(dim = null, keepdim = false) {
if (dim !== null) {
throw new Error("`dim !== null` not yet implemented.");
}
const index = max(this.data)[1];
return new Tensor('int64', [BigInt(index)], []);
}
/**
* Performs Tensor dtype conversion.
* @param {DataType} type The desired data type.
* @returns {Tensor} The converted tensor.
*/
to(type) {
// If the self Tensor already has the correct dtype, then self is returned.
if (this.type === type) return this;
// Otherwise, the returned tensor is a copy of self with the desired dtype.
if (!DataTypeMap.hasOwnProperty(type)) {
throw new Error(`Unsupported type: ${type}`);
}
// Handle special cases where a mapping function is needed (e.g., where one type is a bigint and the other is a number)
let map_fn;
const is_source_bigint = ['int64', 'uint64'].includes(this.type);
const is_dest_bigint = ['int64', 'uint64'].includes(type);
if (is_source_bigint && !is_dest_bigint) {
// TypeError: Cannot convert a BigInt value to a number
map_fn = Number;
} else if (!is_source_bigint && is_dest_bigint) {
// TypeError: Cannot convert [x] to a BigInt
map_fn = BigInt;
}
// @ts-ignore
return new Tensor(type, DataTypeMap[type].from(this.data, map_fn), this.dims);
}
}
/**
* This creates a nested array of a given type and depth (see examples).
*
* @example
* NestArray<string, 1>; // string[]
* @example
* NestArray<number, 2>; // number[][]
* @example
* NestArray<string, 3>; // string[][][] etc.
* @template T
* @template {number} Depth
* @template {never[]} [Acc=[]]
* @typedef {Acc['length'] extends Depth ? T : NestArray<T[], Depth, [...Acc, never]>} NestArray
*/
/**
* Reshapes a 1-dimensional array into an n-dimensional array, according to the provided dimensions.
*
* @example
* reshape([10 ], [1 ]); // Type: number[] Value: [10]
* reshape([1, 2, 3, 4 ], [2, 2 ]); // Type: number[][] Value: [[1, 2], [3, 4]]
* reshape([1, 2, 3, 4, 5, 6, 7, 8], [2, 2, 2]); // Type: number[][][] Value: [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
* reshape([1, 2, 3, 4, 5, 6, 7, 8], [4, 2 ]); // Type: number[][] Value: [[1, 2], [3, 4], [5, 6], [7, 8]]
* @param {T[]|DataArray} data The input array to reshape.
* @param {DIM} dimensions The target shape/dimensions.
* @template T
* @template {[number]|number[]} DIM
* @returns {NestArray<T, DIM["length"]>} The reshaped array.
*/
function reshape(data, dimensions) {
const totalElements = data.length;
const dimensionSize = dimensions.reduce((a, b) => a * b);
if (totalElements !== dimensionSize) {
throw Error(`cannot reshape array of size ${totalElements} into shape (${dimensions})`);
}
/** @type {any} */
let reshapedArray = data;
for (let i = dimensions.length - 1; i >= 0; i--) {
reshapedArray = reshapedArray.reduce((acc, val) => {
let lastArray = acc[acc.length - 1];
if (lastArray.length < dimensions[i]) {
lastArray.push(val);
} else {
acc.push([val]);
}
return acc;
}, [[]]);
}
return reshapedArray[0];
}
/**
* Permutes a tensor according to the provided axes.
* @param {any} tensor The input tensor to permute.
* @param {Array} axes The axes to permute the tensor along.
* @returns {Tensor} The permuted tensor.
*/
export function permute(tensor, axes) {
const [permutedData, shape] = permute_data(tensor.data, tensor.dims, axes);
return new Tensor(tensor.type, permutedData, shape);
}
/**
* Interpolates an Tensor to the given size.
* @param {Tensor} input The input tensor to interpolate. Data must be channel-first (i.e., [c, h, w])
* @param {number[]} size The output size of the image
* @param {string} mode The interpolation mode
* @param {boolean} align_corners Whether to align corners.
* @returns {Tensor} The interpolated tensor.
*/
export function interpolate(input, [out_height, out_width], mode = 'bilinear', align_corners = false) {
// Input image dimensions
const in_channels = input.dims.at(-3) ?? 1;
const in_height = input.dims.at(-2);
const in_width = input.dims.at(-1);
let output = interpolate_data(
/** @type {import('./maths.js').TypedArray}*/(input.data),
[in_channels, in_height, in_width],
[out_height, out_width],
mode,
align_corners
);
return new Tensor(input.type, output, [in_channels, out_height, out_width]);
}
/**
* Down/up samples the input.
* Inspired by https://pytorch.org/docs/stable/generated/torch.nn.functional.interpolate.html.
* @param {Tensor} input the input tensor
* @param {Object} options the options for the interpolation
* @param {[number, number]|[number, number, number]|[number, number, number, number]} [options.size=null] output spatial size.
* @param {"nearest"|"bilinear"|"bicubic"} [options.mode='bilinear'] algorithm used for upsampling
* @returns {Promise<Tensor>} The interpolated tensor.
*/
export async function interpolate_4d(input, {
size = null,
mode = 'bilinear',
} = {}) {
// Error checking
if (input.dims.length !== 4) {
throw new Error('`interpolate_4d` currently only supports 4D input.');
}
if (!size) {
// TODO: support scale_factor
throw new Error('`interpolate_4d` requires a `size` argument.');
}
// Fill in missing dimensions
let targetDims;
if (size.length === 2) {
targetDims = [...input.dims.slice(0, 2), ...size];
} else if (size.length === 3) {
targetDims = [input.dims[0], ...size];
} else if (size.length === 4) {
targetDims = size;
} else {
throw new Error('`size` must be of length 2, 3, or 4.');
}
let op;
if (mode === 'nearest') {
op = await TensorOpRegistry.nearest_interpolate_4d;
} else if (mode === 'bilinear') {
op = await TensorOpRegistry.bilinear_interpolate_4d;
} else if (mode === 'bicubic') {
op = await TensorOpRegistry.bicubic_interpolate_4d;
} else {
throw new Error(`Unsupported mode: ${mode}`);
}
const sizeTensor = new Tensor('int64', new BigInt64Array(targetDims.map(BigInt)), [targetDims.length]);
return await op({ x: input, s: sizeTensor });
}
/**
* Matrix product of two tensors.
* Inspired by https://pytorch.org/docs/stable/generated/torch.matmul.html
* @param {Tensor} a the first tensor to be multiplied
* @param {Tensor} b the second tensor to be multiplied
* @returns {Promise<Tensor>} The matrix product of the two tensors.
*/
export async function matmul(a, b) {
const op = await TensorOpRegistry.matmul;
return await op({ a, b });
}
/**
* Computes the one dimensional Fourier transform of real-valued input.
* Inspired by https://pytorch.org/docs/stable/generated/torch.fft.rfft.html
* @param {Tensor} x the real input tensor
* @param {Tensor} a The dimension along which to take the one dimensional real FFT.
* @returns {Promise<Tensor>} the output tensor.
*/
export async function rfft(x, a) {
const op = await TensorOpRegistry.rfft;
return await op({ x, a });
}
/**
* Returns the k largest elements of the given input tensor.
* Inspired by https://pytorch.org/docs/stable/generated/torch.topk.html
* @param {Tensor} x the input tensor
* @param {number} [k] the k in "top-k"
* @returns {Promise<[Tensor, Tensor]>} the output tuple of (Tensor, LongTensor) of top-k elements and their indices.
*/
export async function topk(x, k) {
const op = await TensorOpRegistry.top_k;
if (k == null) {
k = x.dims.at(-1);
} else {
k = Math.min(k, x.dims.at(-1));
}
return await op({
x,
k: new Tensor(
'int64',
[BigInt(k)],
[1]
)
});
}
const arrayToIndexTensor = (array) => new Tensor('int64', array, [array.length]);
/**
* Slice a multidimensional float32 tensor.
* @param {Tensor} data: Tensor of data to extract slices from
* @param {number[]} starts: 1-D array of starting indices of corresponding axis in axes
* @param {number[]} ends: 1-D array of ending indices (exclusive) of corresponding axis in axes
* @param {number[]} axes: 1-D array of axes that starts and ends apply to
* @param {number[]} [steps]: 1-D array of slice step of corresponding axis in axes.
* @returns {Promise<Tensor>} Sliced data tensor.
*/
export async function slice(data, starts, ends, axes, steps) {
const op = await TensorOpRegistry.slice;
return await op({
x: data,
s: arrayToIndexTensor(starts),
e: arrayToIndexTensor(ends),
a: arrayToIndexTensor(axes),
t: arrayToIndexTensor(steps ?? new Array(axes.length).fill(1)),
});
}
/**
* Perform mean pooling of the last hidden state followed by a normalization step.
* @param {Tensor} last_hidden_state Tensor of shape [batchSize, seqLength, embedDim]
* @param {Tensor} attention_mask Tensor of shape [batchSize, seqLength]
* @returns {Tensor} Returns a new Tensor of shape [batchSize, embedDim].
*/
export function mean_pooling(last_hidden_state, attention_mask) {
// last_hidden_state: [batchSize, seqLength, embedDim]
// attention_mask: [batchSize, seqLength]
const lastHiddenStateData = last_hidden_state.data;
const attentionMaskData = attention_mask.data;
const shape = [last_hidden_state.dims[0], last_hidden_state.dims[2]];
// @ts-ignore
const returnedData = new lastHiddenStateData.constructor(shape[0] * shape[1]);
const [batchSize, seqLength, embedDim] = last_hidden_state.dims;
let outIndex = 0;
for (let i = 0; i < batchSize; ++i) {
const offset = i * embedDim * seqLength;
for (let k = 0; k < embedDim; ++k) {
let sum = 0;
let count = 0;
const attnMaskOffset = i * seqLength;
const offset2 = offset + k;
// Pool over all words in sequence
for (let j = 0; j < seqLength; ++j) {
// index into attention mask
const attn = Number(attentionMaskData[attnMaskOffset + j]);
count += attn;
sum += lastHiddenStateData[offset2 + j * embedDim] * attn;
}
const avg = sum / count;
returnedData[outIndex++] = avg;
}
}
return new Tensor(
last_hidden_state.type,
returnedData,
shape
)
}
/**
* Apply Layer Normalization for last certain number of dimensions.
* @param {Tensor} input The input tensor
* @param {number[]} normalized_shape input shape from an expected input of size
* @param {Object} options The options for the layer normalization
* @param {number} [options.eps=1e-5] A value added to the denominator for numerical stability.
* @returns {Tensor} The normalized tensor.
*/
export function layer_norm(input, normalized_shape, {
eps = 1e-5,
} = {}) {
if (input.dims.length !== 2) {
throw new Error('`layer_norm` currently only supports 2D input.');
}
const [batchSize, featureDim] = input.dims;
if (normalized_shape.length !== 1 && normalized_shape[0] !== featureDim) {
throw new Error('`normalized_shape` must be a 1D array with shape `[input.dims[1]]`.');
}
const [std, mean] = std_mean(input, 1, 0, true);
const stdData = /** @type {Float32Array} */(std.data);
const meanData = /** @type {Float32Array} */(mean.data);
const inputData = /** @type {Float32Array} */(input.data);
// @ts-ignore
const returnedData = new inputData.constructor(inputData.length);
for (let i = 0; i < batchSize; ++i) {
const offset = i * featureDim;
for (let j = 0; j < featureDim; ++j) {
const offset2 = offset + j;
returnedData[offset2] = (inputData[offset2] - meanData[i]) / (stdData[i] + eps);
}
}
return new Tensor(input.type, returnedData, input.dims);
}
/**
* Helper function to calculate new dimensions when performing a squeeze operation.
* @param {number[]} dims The dimensions of the tensor.
* @param {number|number[]|null} dim The dimension(s) to squeeze.
* @returns {number[]} The new dimensions.
* @private
*/
function calc_squeeze_dims(dims, dim) {
dims = dims.slice();
if (dim === null) {
dims = dims.filter((d) => d !== 1);
} else if (typeof dim === 'number') {
if (dims[dim] === 1) {
dims.splice(dim, 1);
}
} else if (Array.isArray(dim)) {
dims = dims.filter((x, i) => {
return x !== 1 || !dim.includes(i);
});
}
return dims;
}
/**
* Helper function to calculate new dimensions when performing an unsqueeze operation.
* @param {number[]} dims The dimensions of the tensor.
* @param {number} dim The dimension to unsqueeze.
* @returns {number[]} The new dimensions.
* @private
*/
function calc_unsqueeze_dims(dims, dim) {
// Dimension out of range (e.g., "expected to be in range of [-4, 3], but got 4")
// + 1 since we allow inserting at the end (i.e. dim = -1)
dim = safeIndex(dim, dims.length + 1);
dims = dims.slice();
// Insert 1 into specified dimension
dims.splice(dim, 0, 1);
return dims;
}
/**
* Safely calculate the index for an array of a given size, allowing negative indexing.
* @param {number} index The index that will be used.
* @param {number} size The size of the array.
* @param {number} [dimension=null] The dimension that the index is for (optional).
* @returns {number} The index, guaranteed to be non-negative and less than `arrayLength`.
*
* @throws {Error} If the index is out of range.
* @private
*/
function safeIndex(index, size, dimension = null, boundsCheck = true) {
if (index < -size || index >= size) {
if (boundsCheck) {
throw new Error(`IndexError: index ${index} is out of bounds for dimension${dimension === null ? '' : ' ' + dimension} with size ${size}`);
} else {
return index < -size ? 0 : size;
}
}
if (index < 0) {
// Negative indexing, ensuring positive index
index = ((index % size) + size) % size;
}
return index;
}
/**
* Concatenates an array of tensors along a specified dimension.
* @param {Tensor[]} tensors The array of tensors to concatenate.
* @param {number} dim The dimension to concatenate along.
* @returns {Tensor} The concatenated tensor.
*/
export function cat(tensors, dim = 0) {
dim = safeIndex(dim, tensors[0].dims.length);
// TODO do validation of shapes
const resultDims = tensors[0].dims.slice();
resultDims[dim] = tensors.reduce((a, b) => a + b.dims[dim], 0);
// Create a new array to store the accumulated values
const resultSize = resultDims.reduce((a, b) => a * b, 1);
// @ts-ignore
const result = new tensors[0].data.constructor(resultSize);
// Create output tensor of same type as first
const resultType = tensors[0].type;
if (dim === 0) {
// Handle special case for performance reasons
let offset = 0;
for (const tensor of tensors) {
const tensorData = tensor.data;
result.set(tensorData, offset);
offset += tensorData.length;
}
} else {
let currentDim = 0;
for (let t = 0; t < tensors.length; ++t) {
const { data, dims } = tensors[t];
// Iterate over the data array
for (let i = 0; i < data.length; ++i) {
// Calculate the index in the resulting array
let resultIndex = 0;
for (let j = dims.length - 1, num = i, resultMultiplier = 1; j >= 0; --j) {
const size = dims[j];
let index = num % size;
if (j === dim) {
index += currentDim;
}
resultIndex += index * resultMultiplier;
resultMultiplier *= resultDims[j];
num = Math.floor(num / size);
}
// Accumulate the value at the current index
result[resultIndex] = data[i];
}
currentDim += dims[dim];
}
}
return new Tensor(resultType, result, resultDims);
}
/**
* Stack an array of tensors along a specified dimension.
* @param {Tensor[]} tensors The array of tensors to stack.
* @param {number} dim The dimension to stack along.
* @returns {Tensor} The stacked tensor.
*/
export function stack(tensors, dim = 0) {
// TODO do validation of shapes
// NOTE: stack expects each tensor to be equal size
return cat(tensors.map(t => t.unsqueeze(dim)), dim);
}
/**
* @param {(previousValue: any, currentValue: any, currentIndex?: number, resultIndex?: number) => any} callbackfn
* @param {Tensor} input the input tensor.
* @param {number|null} dim the dimension to reduce.
* @param {boolean} keepdim whether the output tensor has dim retained or not.
* @returns {[DataType, any, number[]]} The reduced tensor data.
*/
function reduce_helper(callbackfn, input, dim = null, keepdim = false, initialValue = null) {
const inputData = input.data;
const inputDims = input.dims;
// Negative indexing
dim = safeIndex(dim, inputDims.length);
// Calculate the shape of the resulting array after summation
const resultDims = inputDims.slice(); // Copy the original dimensions
resultDims[dim] = 1; // Remove the specified axis
// Create a new array to store the accumulated values
// @ts-ignore
const result = new inputData.constructor(inputData.length / inputDims[dim]);
if (initialValue !== null) {
result.fill(initialValue);
}
// Iterate over the data array
for (let i = 0; i < inputData.length; ++i) {
// Calculate the index in the resulting array
let resultIndex = 0;
for (let j = inputDims.length - 1, num = i, resultMultiplier = 1; j >= 0; --j) {
const size = inputDims[j];
if (j !== dim) {
const index = num % size;
resultIndex += index * resultMultiplier;
resultMultiplier *= resultDims[j];
}
num = Math.floor(num / size);
}
// Accumulate the value at the current index
result[resultIndex] = callbackfn(result[resultIndex], inputData[i], i, resultIndex);
}
if (!keepdim) resultDims.splice(dim, 1);
return [input.type, result, resultDims];
}
/**
* Calculates the standard deviation and mean over the dimensions specified by dim. dim can be a single dimension or `null` to reduce over all dimensions.
* @param {Tensor} input the input tenso
* @param {number|null} dim the dimension to reduce. If None, all dimensions are reduced.
* @param {number} correction difference between the sample size and sample degrees of freedom. Defaults to Bessel's correction, correction=1.
* @param {boolean} keepdim whether the output tensor has dim retained or not.
* @returns {Tensor[]} A tuple of (std, mean) tensors.
*/
export function std_mean(input, dim = null, correction = 1, keepdim = false) {
const inputData = /** @type {Float32Array} */(input.data);
const inputDims = input.dims;
if (dim === null) {
// None to reduce over all dimensions.
const sum = inputData.reduce((a, b) => a + b, 0);
const mean = sum / inputData.length;
const std = Math.sqrt(inputData.reduce((a, b) => a + (b - mean) ** 2, 0) / (inputData.length - correction));
const meanTensor = new Tensor(input.type, [mean], [/* scalar */]);
const stdTensor = new Tensor(input.type, [std], [/* scalar */]);
return [stdTensor, meanTensor];
}
dim = safeIndex(dim, inputDims.length);
const meanTensor = mean(input, dim, keepdim);
const meanTensorData = meanTensor.data;
// Compute squared sum
const [type, result, resultDims] = reduce_helper((a, b, i, j) => a + (b - meanTensorData[j]) ** 2, input, dim, keepdim);
// Square root of the squared sum
for (let i = 0; i < result.length; ++i) {
result[i] = Math.sqrt(result[i] / (inputDims[dim] - correction));
}
const stdTensor = new Tensor(type, result, resultDims);
return [stdTensor, meanTensor];
}
/**
* Returns the mean value of each row of the input tensor in the given dimension dim.
* @param {Tensor} input the input tensor.
* @param {number|null} dim the dimension to reduce.
* @param {boolean} keepdim whether the output tensor has dim retained or not.
* @returns {Tensor} A new tensor with means taken along the specified dimension.
*/
export function mean(input, dim = null, keepdim = false) {
const inputDims = input.dims;
const inputData = /** @type {Float32Array} */(input.data);
if (dim === null) {
// None to reduce over all dimensions.
const val = inputData.reduce((a, b) => a + b, 0);
return new Tensor(input.type, [val / inputData.length], [/* scalar */]);
}
dim = safeIndex(dim, inputDims.length);
// Compute sum
const [type, result, resultDims] = reduce_helper((a, b) => a + b, input, dim, keepdim);
// Divide by number of elements in the dimension
if (inputDims[dim] !== 1) {
for (let i = 0; i < result.length; ++i) {
result[i] /= inputDims[dim];
}
}
return new Tensor(type, result, resultDims);
}
function dimsToStride(dims) {
const stride = new Array(dims.length);
for (let i = dims.length - 1, s2 = 1; i >= 0; --i) {
stride[i] = s2;
s2 *= dims[i];
}
return stride;
}
function fullHelper(size, fill_value, dtype, cls) {
const numElements = size.reduce((a, b) => a * b, 1);
return new Tensor(
dtype,
new cls(numElements).fill(fill_value),
size
)
}
/**
* Creates a tensor of size size filled with fill_value. The tensor's dtype is inferred from fill_value.
* @param {number[]} size A sequence of integers defining the shape of the output tensor.
* @param {number|bigint|boolean} fill_value The value to fill the output tensor with.
* @returns {Tensor} The filled tensor.
*/
export function full(size, fill_value) {
let dtype;
let typedArrayCls;
if (typeof fill_value === 'number') {
dtype = 'float32';
typedArrayCls = Float32Array;
} else if (typeof fill_value === 'bigint') {
dtype = 'int64';
typedArrayCls = BigInt64Array;
} else if (typeof fill_value === 'boolean') {
dtype = 'bool';
typedArrayCls = Uint8Array;
} else {
// TODO: support other dtypes
throw new Error(`Unsupported data type: ${typeof fill_value}`);
}
return fullHelper(size, fill_value, dtype, typedArrayCls);
}
export function full_like(tensor, fill_value) {
return full(tensor.dims, fill_value);
}
/**
* Returns a tensor filled with the scalar value 1, with the shape defined by the variable argument size.
* @param {number[]} size A sequence of integers defining the shape of the output tensor.
* @returns {Tensor} The ones tensor.
*/
export function ones(size) {
return fullHelper(size, 1n, 'int64', BigInt64Array);
}
/**
* Returns a tensor filled with the scalar value 1, with the same size as input.
* @param {Tensor} tensor The size of input will determine size of the output tensor.
* @returns {Tensor} The ones tensor.
*/
export function ones_like(tensor) {
return ones(tensor.dims);
}
/**
* Returns a tensor filled with the scalar value 0, with the shape defined by the variable argument size.
* @param {number[]} size A sequence of integers defining the shape of the output tensor.
* @returns {Tensor} The zeros tensor.
*/
export function zeros(size) {
return fullHelper(size, 0n, 'int64', BigInt64Array);
}
/**
* Returns a tensor filled with the scalar value 0, with the same size as input.
* @param {Tensor} tensor The size of input will determine size of the output tensor.
* @returns {Tensor} The zeros tensor.
*/
export function zeros_like(tensor) {
return zeros(tensor.dims);
}
/**
* Returns a tensor filled with random numbers from a uniform distribution on the interval [0, 1)
* @param {number[]} size A sequence of integers defining the shape of the output tensor.
* @returns {Tensor} The random tensor.
*/
export function rand(size) {
const length = size.reduce((a, b) => a * b, 1);
return new Tensor(
"float32",
Float32Array.from({ length }, () => Math.random()),
size,
)
}
/**
* Quantizes the embeddings tensor to binary or unsigned binary precision.
* @param {Tensor} tensor The tensor to quantize.
* @param {'binary'|'ubinary'} precision The precision to use for quantization.
* @returns {Tensor} The quantized tensor.
*/
export function quantize_embeddings(tensor, precision) {
if (tensor.dims.length !== 2) {
throw new Error("The tensor must have 2 dimensions");
}
if (tensor.dims.at(-1) % 8 !== 0) {
throw new Error("The last dimension of the tensor must be a multiple of 8");
}
if (!['binary', 'ubinary'].includes(precision)) {
throw new Error("The precision must be either 'binary' or 'ubinary'");
}
const signed = precision === 'binary';
const dtype = signed ? 'int8' : 'uint8';
// Create a typed array to store the packed bits
const cls = signed ? Int8Array : Uint8Array;
const inputData = tensor.data;
const outputData = new cls(inputData.length / 8);
// Iterate over each number in the array
for (let i = 0; i < inputData.length; ++i) {
// Determine if the number is greater than 0
const bit = inputData[i] > 0 ? 1 : 0;
// Calculate the index in the typed array and the position within the byte
const arrayIndex = Math.floor(i / 8);
const bitPosition = i % 8;
// Pack the bit into the typed array
outputData[arrayIndex] |= bit << (7 - bitPosition);
if (signed && bitPosition === 0) {
outputData[arrayIndex] -= 128;
}
};
return new Tensor(dtype, outputData, [tensor.dims[0], tensor.dims[1] / 8]);
}
|