UNPKG

@hoff97/tensor-js

Version:

PyTorch like deep learning inferrence library

124 lines 4.38 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; // eslint-disable-next-line node/no-extraneous-import import Long from 'long'; import { Module } from '../model/module'; import { CPUTensor } from '../tensor/cpu/tensor'; import { toCPU } from '../util/convert'; export class OnnxNode extends Module { constructor(attributes, inputs, outputs, constants, onnxVersion, mode) { super(); this.attributes = {}; this.mode = mode; for (let i = 0; i < attributes.length; i++) { this.attributes[attributes[i].name] = attributes[i]; } this.inputs = inputs; this.outputs = outputs; this.onnxVersion = onnxVersion; this.variableInputs = 0; for (let i = 0; i < this.inputs.length; i++) { if (constants[this.inputs[i]] === undefined) { this.variableInputs++; } } } // eslint-disable-next-line @typescript-eslint/no-unused-vars initialize(resolveConstant) { } getAttribute(name) { return this.attributes[name]; } getAttributeString(name) { const attr = this.attributes[name]; if (attr !== undefined) { const str = attr.s; if (str !== undefined && str !== null) { // eslint-disable-next-line node/no-unsupported-features/node-builtins return new TextDecoder('utf-8').decode(str); } return undefined; } return undefined; } getAttributeInts(name) { const attr = this.attributes[name]; if (attr !== undefined) { const result = this.attributes[name].ints; if (result !== undefined && result !== null) { for (let i = 0; i < result.length; i++) { if (Long.isLong(result[i])) { result[i] = result[i].toNumber(); } } return result; } } return undefined; } getAttributeInt(name) { const attr = this.attributes[name]; if (attr !== undefined) { let result = attr.i; if (Long.isLong(result)) { result = result.toNumber(); } return result; } return undefined; } getAttributeFloat(name) { const attr = this.attributes[name]; if (attr !== undefined) { const result = attr.f; return result; } return undefined; } getAttributeFloats(name) { const attr = this.attributes[name]; if (attr !== undefined) { const result = attr.floats; return result; } return undefined; } getAttributeTensor(name) { const attr = this.attributes[name]; if (attr !== undefined) { const result = attr.t; return result; } return undefined; } toValues(tensor) { return __awaiter(this, void 0, void 0, function* () { if (!(tensor instanceof CPUTensor)) { console.warn('Tensor for values not on CPU, need to transfer!'); tensor = yield toCPU(tensor); } const sc = tensor; const values = new Array(sc.size); for (let i = 0; i < sc.size; i++) { values[i] = sc.get(i); } return values; }); } toCPU() { return __awaiter(this, void 0, void 0, function* () { }); } toWASM() { return __awaiter(this, void 0, void 0, function* () { }); } toGPU() { return __awaiter(this, void 0, void 0, function* () { }); } } //# sourceMappingURL=node.js.map