@weavelogic/knowledge-graph-agent 0.7.4 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. package/dist/_virtual/__vite-browser-external.js +2 -2
  2. package/dist/_virtual/__vite-browser-external.js.map +1 -1
  3. package/dist/_virtual/browser.js +2 -3
  4. package/dist/_virtual/browser.js.map +1 -1
  5. package/dist/_virtual/index10.js +2 -4
  6. package/dist/_virtual/index10.js.map +1 -1
  7. package/dist/_virtual/index11.js +2 -2
  8. package/dist/cli/commands/hive-mind/add-frontmatter.js +2 -2
  9. package/dist/cli/commands/hive-mind/add-frontmatter.js.map +1 -1
  10. package/dist/cli/commands/hive-mind/analyze-links.js +2 -2
  11. package/dist/cli/commands/hive-mind/analyze-links.js.map +1 -1
  12. package/dist/cli/commands/hive-mind/find-connections.js +2 -2
  13. package/dist/cli/commands/hive-mind/find-connections.js.map +1 -1
  14. package/dist/cli/commands/hive-mind/validate-names.js +2 -2
  15. package/dist/cli/commands/hive-mind/validate-names.js.map +1 -1
  16. package/dist/graphql/server.js +2 -2
  17. package/dist/graphql/server.js.map +1 -1
  18. package/dist/mcp-server/tools/audit/index.d.ts +4 -0
  19. package/dist/mcp-server/tools/audit/index.d.ts.map +1 -1
  20. package/dist/node_modules/@typescript-eslint/project-service/dist/index.js +1 -1
  21. package/dist/node_modules/debug/src/browser.js +1 -1
  22. package/dist/node_modules/fdir/dist/index.js +14 -14
  23. package/dist/node_modules/fdir/dist/index.js.map +1 -1
  24. package/dist/node_modules/tinyglobby/dist/index.js +14 -14
  25. package/dist/node_modules/tinyglobby/dist/index.js.map +1 -1
  26. package/dist/node_modules/ts-api-utils/lib/index.js +1 -1
  27. package/dist/node_modules/typescript/lib/typescript.js +24 -24
  28. package/dist/node_modules/typescript/lib/typescript.js.map +1 -1
  29. package/dist/vector/services/embedding-service.js +1 -7
  30. package/dist/vector/services/embedding-service.js.map +1 -1
  31. package/package.json +2 -1
  32. package/dist/_virtual/browser2.js +0 -5
  33. package/dist/_virtual/browser2.js.map +0 -1
  34. package/dist/_virtual/index12.js +0 -5
  35. package/dist/_virtual/index12.js.map +0 -1
  36. package/dist/_virtual/ort-web.min.js +0 -8
  37. package/dist/_virtual/ort-web.min.js.map +0 -1
  38. package/dist/_virtual/ort-web.min2.js +0 -5
  39. package/dist/_virtual/ort-web.min2.js.map +0 -1
  40. package/dist/node_modules/@huggingface/jinja/dist/index.js +0 -118
  41. package/dist/node_modules/@huggingface/jinja/dist/index.js.map +0 -1
  42. package/dist/node_modules/@xenova/transformers/src/backends/onnx.js +0 -24
  43. package/dist/node_modules/@xenova/transformers/src/backends/onnx.js.map +0 -1
  44. package/dist/node_modules/@xenova/transformers/src/configs.js +0 -52
  45. package/dist/node_modules/@xenova/transformers/src/configs.js.map +0 -1
  46. package/dist/node_modules/@xenova/transformers/src/env.js +0 -35
  47. package/dist/node_modules/@xenova/transformers/src/env.js.map +0 -1
  48. package/dist/node_modules/@xenova/transformers/src/models.js +0 -3852
  49. package/dist/node_modules/@xenova/transformers/src/models.js.map +0 -1
  50. package/dist/node_modules/@xenova/transformers/src/tokenizers.js +0 -144
  51. package/dist/node_modules/@xenova/transformers/src/tokenizers.js.map +0 -1
  52. package/dist/node_modules/@xenova/transformers/src/utils/core.js +0 -52
  53. package/dist/node_modules/@xenova/transformers/src/utils/core.js.map +0 -1
  54. package/dist/node_modules/@xenova/transformers/src/utils/generation.js +0 -623
  55. package/dist/node_modules/@xenova/transformers/src/utils/generation.js.map +0 -1
  56. package/dist/node_modules/@xenova/transformers/src/utils/hub.js +0 -395
  57. package/dist/node_modules/@xenova/transformers/src/utils/hub.js.map +0 -1
  58. package/dist/node_modules/@xenova/transformers/src/utils/image.js +0 -12
  59. package/dist/node_modules/@xenova/transformers/src/utils/image.js.map +0 -1
  60. package/dist/node_modules/@xenova/transformers/src/utils/maths.js +0 -89
  61. package/dist/node_modules/@xenova/transformers/src/utils/maths.js.map +0 -1
  62. package/dist/node_modules/@xenova/transformers/src/utils/tensor.js +0 -750
  63. package/dist/node_modules/@xenova/transformers/src/utils/tensor.js.map +0 -1
  64. package/dist/node_modules/onnxruntime-common/dist/lib/backend-impl.js +0 -67
  65. package/dist/node_modules/onnxruntime-common/dist/lib/backend-impl.js.map +0 -1
  66. package/dist/node_modules/onnxruntime-common/dist/lib/env-impl.js +0 -24
  67. package/dist/node_modules/onnxruntime-common/dist/lib/env-impl.js.map +0 -1
  68. package/dist/node_modules/onnxruntime-common/dist/lib/env.js +0 -6
  69. package/dist/node_modules/onnxruntime-common/dist/lib/env.js.map +0 -1
  70. package/dist/node_modules/onnxruntime-common/dist/lib/index.js +0 -11
  71. package/dist/node_modules/onnxruntime-common/dist/lib/index.js.map +0 -1
  72. package/dist/node_modules/onnxruntime-common/dist/lib/inference-session-impl.js +0 -162
  73. package/dist/node_modules/onnxruntime-common/dist/lib/inference-session-impl.js.map +0 -1
  74. package/dist/node_modules/onnxruntime-common/dist/lib/inference-session.js +0 -6
  75. package/dist/node_modules/onnxruntime-common/dist/lib/inference-session.js.map +0 -1
  76. package/dist/node_modules/onnxruntime-common/dist/lib/tensor-impl.js +0 -393
  77. package/dist/node_modules/onnxruntime-common/dist/lib/tensor-impl.js.map +0 -1
  78. package/dist/node_modules/onnxruntime-common/dist/lib/tensor.js +0 -6
  79. package/dist/node_modules/onnxruntime-common/dist/lib/tensor.js.map +0 -1
  80. package/dist/node_modules/onnxruntime-web/dist/ort-web.min.js +0 -12919
  81. package/dist/node_modules/onnxruntime-web/dist/ort-web.min.js.map +0 -1
  82. package/dist/node_modules/ws/browser.js +0 -16
  83. package/dist/node_modules/ws/browser.js.map +0 -1
@@ -1,750 +0,0 @@
1
- import { ONNX } from "../backends/onnx.js";
2
- import { permute_data } from "./maths.js";
3
- const DataTypeMap = Object.freeze({
4
- float32: Float32Array,
5
- float64: Float64Array,
6
- string: Array,
7
- // string[]
8
- int8: Int8Array,
9
- uint8: Uint8Array,
10
- int16: Int16Array,
11
- uint16: Uint16Array,
12
- int32: Int32Array,
13
- uint32: Uint32Array,
14
- int64: BigInt64Array,
15
- uint64: BigUint64Array,
16
- bool: Uint8Array
17
- });
18
- const ONNXTensor = ONNX.Tensor;
19
- class Tensor {
20
- /** @type {number[]} Dimensions of the tensor. */
21
- dims;
22
- /** @type {DataType} Type of the tensor. */
23
- type;
24
- /** @type {DataArray} The data stored in the tensor. */
25
- data;
26
- /** @type {number} The number of elements in the tensor. */
27
- size;
28
- /**
29
- * Create a new Tensor or copy an existing Tensor.
30
- * @param {[DataType, DataArray, number[]]|[import('onnxruntime-common').Tensor]} args
31
- */
32
- constructor(...args) {
33
- if (args[0] instanceof ONNXTensor) {
34
- Object.assign(this, args[0]);
35
- } else {
36
- Object.assign(this, new ONNXTensor(
37
- /** @type {DataType} */
38
- args[0],
39
- /** @type {Exclude<import('./maths.js').AnyTypedArray, Uint8ClampedArray>} */
40
- args[1],
41
- args[2]
42
- ));
43
- }
44
- return new Proxy(this, {
45
- get: (obj, key) => {
46
- if (typeof key === "string") {
47
- let index = Number(key);
48
- if (Number.isInteger(index)) {
49
- return obj._getitem(index);
50
- }
51
- }
52
- return obj[key];
53
- },
54
- set: (obj, key, value) => {
55
- return obj[key] = value;
56
- }
57
- });
58
- }
59
- /**
60
- * Returns an iterator object for iterating over the tensor data in row-major order.
61
- * If the tensor has more than one dimension, the iterator will yield subarrays.
62
- * @returns {Iterator} An iterator object for iterating over the tensor data in row-major order.
63
- */
64
- *[Symbol.iterator]() {
65
- const [iterLength, ...iterDims] = this.dims;
66
- if (iterDims.length > 0) {
67
- const iterSize = iterDims.reduce((a, b) => a * b);
68
- for (let i = 0; i < iterLength; ++i) {
69
- yield this._subarray(i, iterSize, iterDims);
70
- }
71
- } else {
72
- yield* this.data;
73
- }
74
- }
75
- /**
76
- * Index into a Tensor object.
77
- * @param {number} index The index to access.
78
- * @returns {Tensor} The data at the specified index.
79
- */
80
- _getitem(index) {
81
- const [iterLength, ...iterDims] = this.dims;
82
- index = safeIndex(index, iterLength);
83
- if (iterDims.length > 0) {
84
- const iterSize = iterDims.reduce((a, b) => a * b);
85
- return this._subarray(index, iterSize, iterDims);
86
- } else {
87
- return new Tensor(this.type, [this.data[index]], iterDims);
88
- }
89
- }
90
- /**
91
- * @param {number|bigint} item The item to search for in the tensor
92
- * @returns {number} The index of the first occurrence of item in the tensor data.
93
- */
94
- indexOf(item) {
95
- for (let index = 0; index < this.data.length; ++index) {
96
- if (this.data[index] == item) {
97
- return index;
98
- }
99
- }
100
- return -1;
101
- }
102
- /**
103
- * @param {number} index
104
- * @param {number} iterSize
105
- * @param {any} iterDims
106
- * @returns {Tensor}
107
- */
108
- _subarray(index, iterSize, iterDims) {
109
- const o1 = index * iterSize;
110
- const o2 = (index + 1) * iterSize;
111
- const data = "subarray" in this.data ? this.data.subarray(o1, o2) : this.data.slice(o1, o2);
112
- return new Tensor(this.type, data, iterDims);
113
- }
114
- /**
115
- * Returns the value of this tensor as a standard JavaScript Number. This only works
116
- * for tensors with one element. For other cases, see `Tensor.tolist()`.
117
- * @returns {number|bigint} The value of this tensor as a standard JavaScript Number.
118
- * @throws {Error} If the tensor has more than one element.
119
- */
120
- item() {
121
- if (this.data.length !== 1) {
122
- throw new Error(`a Tensor with ${this.data.length} elements cannot be converted to Scalar`);
123
- }
124
- return this.data[0];
125
- }
126
- /**
127
- * Convert tensor data to a n-dimensional JS list
128
- * @returns {Array}
129
- */
130
- tolist() {
131
- return reshape(this.data, this.dims);
132
- }
133
- /**
134
- * Return a new Tensor with the sigmoid function applied to each element.
135
- * @returns {Tensor} The tensor with the sigmoid function applied.
136
- */
137
- sigmoid() {
138
- return this.clone().sigmoid_();
139
- }
140
- /**
141
- * Applies the sigmoid function to the tensor in place.
142
- * @returns {Tensor} Returns `this`.
143
- */
144
- sigmoid_() {
145
- for (let i = 0; i < this.data.length; ++i) {
146
- this.data[i] = 1 / (1 + Math.exp(-this.data[i]));
147
- }
148
- return this;
149
- }
150
- /**
151
- * Return a new Tensor with every element multiplied by a constant.
152
- * @param {number} val The value to multiply by.
153
- * @returns {Tensor} The new tensor.
154
- */
155
- mul(val) {
156
- return this.clone().mul_(val);
157
- }
158
- /**
159
- * Multiply the tensor by a constant in place.
160
- * @param {number} val The value to multiply by.
161
- * @returns {Tensor} Returns `this`.
162
- */
163
- mul_(val) {
164
- for (let i = 0; i < this.data.length; ++i) {
165
- this.data[i] *= val;
166
- }
167
- return this;
168
- }
169
- /**
170
- * Return a new Tensor with every element added by a constant.
171
- * @param {number} val The value to add by.
172
- * @returns {Tensor} The new tensor.
173
- */
174
- add(val) {
175
- return this.clone().add_(val);
176
- }
177
- /**
178
- * Add the tensor by a constant in place.
179
- * @param {number} val The value to add by.
180
- * @returns {Tensor} Returns `this`.
181
- */
182
- add_(val) {
183
- for (let i = 0; i < this.data.length; ++i) {
184
- this.data[i] += val;
185
- }
186
- return this;
187
- }
188
- clone() {
189
- return new Tensor(this.type, this.data.slice(), this.dims.slice());
190
- }
191
- slice(...slices) {
192
- let newTensorDims = [];
193
- let newOffsets = [];
194
- for (let sliceIndex = 0; sliceIndex < this.dims.length; ++sliceIndex) {
195
- let slice = slices[sliceIndex];
196
- if (slice === null || slice === void 0) {
197
- newOffsets.push([0, this.dims[sliceIndex]]);
198
- newTensorDims.push(this.dims[sliceIndex]);
199
- } else if (typeof slice === "number") {
200
- slice = safeIndex(slice, this.dims[sliceIndex], sliceIndex);
201
- newOffsets.push([slice, slice + 1]);
202
- } else if (Array.isArray(slice) && slice.length === 2) {
203
- if (slice[0] > slice[1]) {
204
- throw new Error(`Invalid slice: ${slice}`);
205
- }
206
- let offsets = [
207
- Math.max(slice[0], 0),
208
- Math.min(slice[1], this.dims[sliceIndex])
209
- ];
210
- newOffsets.push(offsets);
211
- newTensorDims.push(offsets[1] - offsets[0]);
212
- } else {
213
- throw new Error(`Invalid slice: ${slice}`);
214
- }
215
- }
216
- let newDims = newOffsets.map(([start, end]) => end - start);
217
- let newBufferSize = newDims.reduce((a, b) => a * b);
218
- let data = new this.data.constructor(newBufferSize);
219
- const stride = this.stride();
220
- for (let i = 0; i < newBufferSize; ++i) {
221
- let originalIndex = 0;
222
- for (let j = newDims.length - 1, num = i; j >= 0; --j) {
223
- const size = newDims[j];
224
- originalIndex += (num % size + newOffsets[j][0]) * stride[j];
225
- num = Math.floor(num / size);
226
- }
227
- data[i] = this.data[originalIndex];
228
- }
229
- return new Tensor(this.type, data, newTensorDims);
230
- }
231
- /**
232
- * Return a permuted version of this Tensor, according to the provided dimensions.
233
- * @param {...number} dims Dimensions to permute.
234
- * @returns {Tensor} The permuted tensor.
235
- */
236
- permute(...dims) {
237
- return permute(this, dims);
238
- }
239
- // TODO: implement transpose. For now (backwards compatibility), it's just an alias for permute()
240
- transpose(...dims) {
241
- return this.permute(...dims);
242
- }
243
- // TODO add .max() and .min() methods
244
- /**
245
- * Returns the sum of each row of the input tensor in the given dimension dim.
246
- *
247
- * @param {number} [dim=null] The dimension or dimensions to reduce. If `null`, all dimensions are reduced.
248
- * @param {boolean} keepdim Whether the output tensor has `dim` retained or not.
249
- * @returns The summed tensor
250
- */
251
- sum(dim = null, keepdim = false) {
252
- return this.norm(1, dim, keepdim);
253
- }
254
- /**
255
- * Returns the matrix norm or vector norm of a given tensor.
256
- * @param {number|string} [p='fro'] The order of norm
257
- * @param {number} [dim=null] Specifies which dimension of the tensor to calculate the norm across.
258
- * If dim is None, the norm will be calculated across all dimensions of input.
259
- * @param {boolean} [keepdim=false] Whether the output tensors have dim retained or not.
260
- * @returns {Tensor} The norm of the tensor.
261
- */
262
- norm(p = "fro", dim = null, keepdim = false) {
263
- if (p === "fro") {
264
- p = 2;
265
- } else if (typeof p === "string") {
266
- throw Error(`Unsupported norm: ${p}`);
267
- }
268
- if (dim === null) {
269
- let val = this.data.reduce((a, b) => a + b ** p, 0) ** (1 / p);
270
- return new Tensor(this.type, [val], []);
271
- }
272
- dim = safeIndex(dim, this.dims.length);
273
- const resultDims = this.dims.slice();
274
- resultDims[dim] = 1;
275
- const result = new this.data.constructor(this.data.length / this.dims[dim]);
276
- for (let i = 0; i < this.data.length; ++i) {
277
- let resultIndex = 0;
278
- for (let j = this.dims.length - 1, num = i, resultMultiplier = 1; j >= 0; --j) {
279
- const size = this.dims[j];
280
- if (j !== dim) {
281
- const index = num % size;
282
- resultIndex += index * resultMultiplier;
283
- resultMultiplier *= resultDims[j];
284
- }
285
- num = Math.floor(num / size);
286
- }
287
- result[resultIndex] += this.data[i] ** p;
288
- }
289
- if (p !== 1) {
290
- for (let i = 0; i < result.length; ++i) {
291
- result[i] = result[i] ** (1 / p);
292
- }
293
- }
294
- if (!keepdim) {
295
- resultDims.splice(dim, 1);
296
- }
297
- return new Tensor(this.type, result, resultDims);
298
- }
299
- /**
300
- * Performs `L_p` normalization of inputs over specified dimension. Operates in place.
301
- * @param {number} [p=2] The exponent value in the norm formulation
302
- * @param {number} [dim=1] The dimension to reduce
303
- * @returns {Tensor} `this` for operation chaining.
304
- */
305
- normalize_(p = 2, dim = 1) {
306
- dim = safeIndex(dim, this.dims.length);
307
- const norm = this.norm(p, dim, true);
308
- for (let i = 0; i < this.data.length; ++i) {
309
- let resultIndex = 0;
310
- for (let j = this.dims.length - 1, num = i, resultMultiplier = 1; j >= 0; --j) {
311
- const size = this.dims[j];
312
- if (j !== dim) {
313
- const index = num % size;
314
- resultIndex += index * resultMultiplier;
315
- resultMultiplier *= this.dims[j];
316
- }
317
- num = Math.floor(num / size);
318
- }
319
- this.data[i] /= norm.data[resultIndex];
320
- }
321
- return this;
322
- }
323
- /**
324
- * Performs `L_p` normalization of inputs over specified dimension.
325
- * @param {number} [p=2] The exponent value in the norm formulation
326
- * @param {number} [dim=1] The dimension to reduce
327
- * @returns {Tensor} The normalized tensor.
328
- */
329
- normalize(p = 2, dim = 1) {
330
- return this.clone().normalize_(p, dim);
331
- }
332
- /**
333
- * Compute and return the stride of this tensor.
334
- * Stride is the jump necessary to go from one element to the next one in the specified dimension dim.
335
- * @returns {number[]} The stride of this tensor.
336
- */
337
- stride() {
338
- return dimsToStride(this.dims);
339
- }
340
- /**
341
- * Returns a tensor with all specified dimensions of input of size 1 removed.
342
- *
343
- * NOTE: The returned tensor shares the storage with the input tensor, so changing the contents of one will change the contents of the other.
344
- * If you would like a copy, use `tensor.clone()` before squeezing.
345
- *
346
- * @param {number} [dim=null] If given, the input will be squeezed only in the specified dimensions.
347
- * @returns The squeezed tensor
348
- */
349
- squeeze(dim = null) {
350
- return new Tensor(
351
- this.type,
352
- this.data,
353
- calc_squeeze_dims(this.dims, dim)
354
- );
355
- }
356
- /**
357
- * In-place version of @see {@link Tensor.squeeze}
358
- */
359
- squeeze_(dim = null) {
360
- this.dims = calc_squeeze_dims(this.dims, dim);
361
- return this;
362
- }
363
- /**
364
- * Returns a new tensor with a dimension of size one inserted at the specified position.
365
- *
366
- * NOTE: The returned tensor shares the same underlying data with this tensor.
367
- *
368
- * @param {number} dim The index at which to insert the singleton dimension
369
- * @returns The unsqueezed tensor
370
- */
371
- unsqueeze(dim = null) {
372
- return new Tensor(
373
- this.type,
374
- this.data,
375
- calc_unsqueeze_dims(this.dims, dim)
376
- );
377
- }
378
- /**
379
- * In-place version of @see {@link Tensor.unsqueeze}
380
- */
381
- unsqueeze_(dim = null) {
382
- this.dims = calc_unsqueeze_dims(this.dims, dim);
383
- return this;
384
- }
385
- /**
386
- * In-place version of @see {@link Tensor.flatten}
387
- */
388
- flatten_(start_dim = 0, end_dim = -1) {
389
- end_dim = (end_dim + this.dims.length) % this.dims.length;
390
- let dimsToKeepBefore = this.dims.slice(0, start_dim);
391
- let dimsToFlatten = this.dims.slice(start_dim, end_dim + 1);
392
- let dimsToKeepAfter = this.dims.slice(end_dim + 1);
393
- this.dims = [...dimsToKeepBefore, dimsToFlatten.reduce((a, b) => a * b, 1), ...dimsToKeepAfter];
394
- return this;
395
- }
396
- /**
397
- * Flattens input by reshaping it into a one-dimensional tensor.
398
- * If `start_dim` or `end_dim` are passed, only dimensions starting with `start_dim`
399
- * and ending with `end_dim` are flattened. The order of elements in input is unchanged.
400
- * @param {number} start_dim the first dim to flatten
401
- * @param {number} end_dim the last dim to flatten
402
- * @returns The flattened tensor.
403
- */
404
- flatten(start_dim = 0, end_dim = -1) {
405
- return this.clone().flatten_(start_dim, end_dim);
406
- }
407
- /**
408
- * Returns a new tensor with the same data as the `self` tensor but of a different `shape`.
409
- * @param {...number} dims the desired size
410
- * @returns {Tensor} The tensor with the same data but different shape
411
- */
412
- view(...dims) {
413
- let inferredIndex = -1;
414
- for (let i = 0; i < dims.length; ++i) {
415
- if (dims[i] === -1) {
416
- if (inferredIndex !== -1) {
417
- throw new Error("Only one dimension can be inferred");
418
- }
419
- inferredIndex = i;
420
- }
421
- }
422
- if (inferredIndex !== -1) {
423
- const productOther = dims.reduce((product, curr, index) => {
424
- return index !== inferredIndex ? product * curr : product;
425
- }, 1);
426
- dims[inferredIndex] = this.data.length / productOther;
427
- }
428
- return new Tensor(this.type, this.data, dims);
429
- }
430
- neg_() {
431
- for (let i = 0; i < this.data.length; ++i) {
432
- this.data[i] = -this.data[i];
433
- }
434
- return this;
435
- }
436
- neg() {
437
- return this.clone().neg_();
438
- }
439
- /**
440
- * In-place version of @see {@link Tensor.clamp}
441
- */
442
- clamp_(min, max) {
443
- for (let i = 0; i < this.data.length; ++i) {
444
- this.data[i] = Math.min(Math.max(this.data[i], min), max);
445
- }
446
- return this;
447
- }
448
- /**
449
- * Clamps all elements in input into the range [ min, max ]
450
- * @param {number} min lower-bound of the range to be clamped to
451
- * @param {number} max upper-bound of the range to be clamped to
452
- * @returns the output tensor.
453
- */
454
- clamp(min, max) {
455
- return this.clone().clamp_(min, max);
456
- }
457
- /**
458
- * In-place version of @see {@link Tensor.round}
459
- */
460
- round_() {
461
- for (let i = 0; i < this.data.length; ++i) {
462
- this.data[i] = Math.round(this.data[i]);
463
- }
464
- return this;
465
- }
466
- /**
467
- * Rounds elements of input to the nearest integer.
468
- * @returns the output tensor.
469
- */
470
- round() {
471
- return this.clone().round_();
472
- }
473
- /**
474
- * Performs Tensor dtype conversion.
475
- * @param {DataType} type The desired data type.
476
- * @returns {Tensor} The converted tensor.
477
- */
478
- to(type) {
479
- if (this.type === type) return this;
480
- if (!DataTypeMap.hasOwnProperty(type)) {
481
- throw new Error(`Unsupported type: ${type}`);
482
- }
483
- return new Tensor(type, DataTypeMap[type].from(this.data), this.dims);
484
- }
485
- }
486
- function reshape(data, dimensions) {
487
- const totalElements = data.length;
488
- const dimensionSize = dimensions.reduce((a, b) => a * b);
489
- if (totalElements !== dimensionSize) {
490
- throw Error(`cannot reshape array of size ${totalElements} into shape (${dimensions})`);
491
- }
492
- let reshapedArray = data;
493
- for (let i = dimensions.length - 1; i >= 0; i--) {
494
- reshapedArray = reshapedArray.reduce((acc, val) => {
495
- let lastArray = acc[acc.length - 1];
496
- if (lastArray.length < dimensions[i]) {
497
- lastArray.push(val);
498
- } else {
499
- acc.push([val]);
500
- }
501
- return acc;
502
- }, [[]]);
503
- }
504
- return reshapedArray[0];
505
- }
506
- function permute(tensor, axes) {
507
- const [permutedData, shape] = permute_data(tensor.data, tensor.dims, axes);
508
- return new Tensor(tensor.type, permutedData, shape);
509
- }
510
- function calc_squeeze_dims(dims, dim) {
511
- dims = dims.slice();
512
- if (dim === null) {
513
- dims = dims.filter((d) => d !== 1);
514
- } else if (typeof dim === "number") {
515
- if (dims[dim] === 1) {
516
- dims.splice(dim, 1);
517
- }
518
- } else if (Array.isArray(dim)) {
519
- dims = dims.filter((x, i) => {
520
- return x !== 1 || !dim.includes(i);
521
- });
522
- }
523
- return dims;
524
- }
525
- function calc_unsqueeze_dims(dims, dim) {
526
- dim = safeIndex(dim, dims.length + 1);
527
- dims = dims.slice();
528
- dims.splice(dim, 0, 1);
529
- return dims;
530
- }
531
- function safeIndex(index, size, dimension = null) {
532
- if (index < -size || index >= size) {
533
- throw new Error(`IndexError: index ${index} is out of bounds for dimension${dimension === null ? "" : " " + dimension} with size ${size}`);
534
- }
535
- if (index < 0) {
536
- index = (index % size + size) % size;
537
- }
538
- return index;
539
- }
540
- function cat(tensors, dim = 0) {
541
- dim = safeIndex(dim, tensors[0].dims.length);
542
- const resultDims = tensors[0].dims.slice();
543
- resultDims[dim] = tensors.reduce((a, b) => a + b.dims[dim], 0);
544
- const resultSize = resultDims.reduce((a, b) => a * b, 1);
545
- const result = new tensors[0].data.constructor(resultSize);
546
- const resultType = tensors[0].type;
547
- if (dim === 0) {
548
- let offset = 0;
549
- for (let t of tensors) {
550
- result.set(t.data, offset);
551
- offset += t.data.length;
552
- }
553
- } else {
554
- let currentDim = 0;
555
- for (let t = 0; t < tensors.length; ++t) {
556
- let tensor = tensors[t];
557
- for (let i = 0; i < tensor.data.length; ++i) {
558
- let resultIndex = 0;
559
- for (let j = tensor.dims.length - 1, num = i, resultMultiplier = 1; j >= 0; --j) {
560
- const size = tensor.dims[j];
561
- let index = num % size;
562
- if (j === dim) {
563
- index += currentDim;
564
- }
565
- resultIndex += index * resultMultiplier;
566
- resultMultiplier *= resultDims[j];
567
- num = Math.floor(num / size);
568
- }
569
- result[resultIndex] = tensor.data[i];
570
- }
571
- currentDim += tensor.dims[dim];
572
- }
573
- }
574
- return new Tensor(resultType, result, resultDims);
575
- }
576
- function stack(tensors, dim = 0) {
577
- return cat(tensors.map((t) => t.unsqueeze(dim)), dim);
578
- }
579
- function std_mean(input, dim = null, correction = 1, keepdim = false) {
580
- if (dim === null) {
581
- const sum = input.data.reduce((a, b) => a + b, 0);
582
- const mean2 = sum / input.data.length;
583
- const std = Math.sqrt(input.data.reduce((a, b) => a + (b - mean2) ** 2, 0) / (input.data.length - correction));
584
- const meanTensor2 = new Tensor(input.type, [mean2], [
585
- /* scalar */
586
- ]);
587
- const stdTensor2 = new Tensor(input.type, [std], [
588
- /* scalar */
589
- ]);
590
- return [stdTensor2, meanTensor2];
591
- }
592
- dim = safeIndex(dim, input.dims.length);
593
- const meanTensor = mean(input, dim, keepdim);
594
- const resultDims = input.dims.slice();
595
- resultDims[dim] = 1;
596
- const result = new input.data.constructor(input.data.length / input.dims[dim]);
597
- for (let i = 0; i < input.data.length; ++i) {
598
- let resultIndex = 0;
599
- for (let j = input.dims.length - 1, num = i, resultMultiplier = 1; j >= 0; --j) {
600
- const size = input.dims[j];
601
- if (j !== dim) {
602
- const index = num % size;
603
- resultIndex += index * resultMultiplier;
604
- resultMultiplier *= resultDims[j];
605
- }
606
- num = Math.floor(num / size);
607
- }
608
- result[resultIndex] += (input.data[i] - meanTensor.data[resultIndex]) ** 2;
609
- }
610
- for (let i = 0; i < result.length; ++i) {
611
- result[i] = Math.sqrt(result[i] / (input.dims[dim] - correction));
612
- }
613
- if (!keepdim) {
614
- resultDims.splice(dim, 1);
615
- }
616
- const stdTensor = new Tensor(input.type, result, resultDims);
617
- return [stdTensor, meanTensor];
618
- }
619
- function mean(input, dim = null, keepdim = false) {
620
- if (dim === null) {
621
- let val = input.data.reduce((a, b) => a + b, 0);
622
- return new Tensor(input.type, [val / input.data.length], [
623
- /* scalar */
624
- ]);
625
- }
626
- dim = safeIndex(dim, input.dims.length);
627
- const resultDims = input.dims.slice();
628
- resultDims[dim] = 1;
629
- const result = new input.data.constructor(input.data.length / input.dims[dim]);
630
- for (let i = 0; i < input.data.length; ++i) {
631
- let resultIndex = 0;
632
- for (let j = input.dims.length - 1, num = i, resultMultiplier = 1; j >= 0; --j) {
633
- const size = input.dims[j];
634
- if (j !== dim) {
635
- const index = num % size;
636
- resultIndex += index * resultMultiplier;
637
- resultMultiplier *= resultDims[j];
638
- }
639
- num = Math.floor(num / size);
640
- }
641
- result[resultIndex] += input.data[i];
642
- }
643
- if (input.dims[dim] !== 1) {
644
- for (let i = 0; i < result.length; ++i) {
645
- result[i] = result[i] / input.dims[dim];
646
- }
647
- }
648
- if (!keepdim) {
649
- resultDims.splice(dim, 1);
650
- }
651
- return new Tensor(input.type, result, resultDims);
652
- }
653
- function dynamicTimeWarping(matrix) {
654
- const [output_length, input_length] = matrix.dims;
655
- const outputShape = [output_length + 1, input_length + 1];
656
- const cost = new Tensor(
657
- "float32",
658
- new Float32Array(outputShape[0] * outputShape[1]).fill(Infinity),
659
- outputShape
660
- );
661
- const trace = new Tensor(
662
- "float32",
663
- new Float32Array(outputShape[0] * outputShape[1]).fill(-1),
664
- outputShape
665
- );
666
- cost[0].data[0] = 0;
667
- for (let j2 = 1; j2 < input_length + 1; ++j2) {
668
- for (let i2 = 1; i2 < output_length + 1; ++i2) {
669
- const c0 = cost[i2 - 1][j2 - 1].item();
670
- const c1 = cost[i2 - 1][j2].item();
671
- const c2 = cost[i2][j2 - 1].item();
672
- let c, t;
673
- if (c0 < c1 && c0 < c2) {
674
- c = c0;
675
- t = 0;
676
- } else if (c1 < c0 && c1 < c2) {
677
- c = c1;
678
- t = 1;
679
- } else {
680
- c = c2;
681
- t = 2;
682
- }
683
- cost[i2].data[j2] = matrix[i2 - 1][j2 - 1].item() + c;
684
- trace[i2].data[j2] = t;
685
- }
686
- }
687
- let i = output_length;
688
- let j = input_length;
689
- trace.data.fill(2, 0, outputShape[1]);
690
- for (let i2 = 0; i2 < outputShape[0]; ++i2) {
691
- trace[i2].data[0] = 1;
692
- }
693
- let text_indices = [];
694
- let time_indices = [];
695
- while (i > 0 || j > 0) {
696
- text_indices.push(i - 1);
697
- time_indices.push(j - 1);
698
- const t = trace[i][j].item();
699
- switch (t) {
700
- case 0:
701
- --i;
702
- --j;
703
- break;
704
- case 1:
705
- --i;
706
- break;
707
- case 2:
708
- --j;
709
- break;
710
- default:
711
- throw new Error(
712
- `Internal error in dynamic time warping. Unexpected trace[${i}, ${j}]. Please file a bug report.`
713
- );
714
- }
715
- }
716
- text_indices.reverse();
717
- time_indices.reverse();
718
- return [text_indices, time_indices];
719
- }
720
- function dimsToStride(dims) {
721
- const stride = new Array(dims.length);
722
- for (let i = dims.length - 1, s2 = 1; i >= 0; --i) {
723
- stride[i] = s2;
724
- s2 *= dims[i];
725
- }
726
- return stride;
727
- }
728
- function ones(size) {
729
- const numElements = size.reduce((a, b) => a * b, 1);
730
- return new Tensor(
731
- "int64",
732
- new BigInt64Array(numElements).fill(1n),
733
- size
734
- );
735
- }
736
- function ones_like(tensor) {
737
- return ones(tensor.dims);
738
- }
739
- export {
740
- Tensor,
741
- cat,
742
- dynamicTimeWarping,
743
- mean,
744
- ones,
745
- ones_like,
746
- permute,
747
- stack,
748
- std_mean
749
- };
750
- //# sourceMappingURL=tensor.js.map