@hoff97/tensor-js
Version:
PyTorch like deep learning inferrence library
67 lines • 2.65 kB
JavaScript
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());
});
};
import { Variable } from '../../autograd';
import { toCPU, toGPU, toWASM } from '../../util/convert';
import { OnnxNode } from '../node';
import { createTensor } from '../util';
export class ConstantNode extends OnnxNode {
constructor(attributes, inputs, outputs, constants, onnxVersion, mode) {
super(attributes, inputs, outputs, constants, onnxVersion, mode);
const tensor = this.getAttributeTensor('value');
if (tensor !== undefined && tensor !== null) {
this.tensor = createTensor(tensor);
if (mode === 'train' && this.tensor !== undefined) {
this.tensor = new Variable(this.tensor);
}
}
else {
throw new Error('Constant needs tensor value, but attribute "value" was not defined');
}
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
forward(inputs) {
return __awaiter(this, void 0, void 0, function* () {
if (this.tensor !== undefined) {
return [this.tensor];
}
throw new Error('Constant without tensor value doesnt work');
});
}
toCPU() {
return __awaiter(this, void 0, void 0, function* () {
if (this.tensor !== undefined) {
this.tensor = yield toCPU(this.tensor);
}
});
}
toWASM() {
return __awaiter(this, void 0, void 0, function* () {
if (this.tensor !== undefined) {
this.tensor = yield toWASM(this.tensor);
}
});
}
toGPU() {
return __awaiter(this, void 0, void 0, function* () {
if (this.tensor !== undefined) {
this.tensor = yield toGPU(this.tensor);
}
});
}
getType() {
return 'Constant';
}
delete() {
if (this.tensor !== undefined) {
this.tensor.delete();
}
}
}
//# sourceMappingURL=constant.js.map