@hoff97/tensor-js
Version:
PyTorch like deep learning inferrence library
61 lines • 2.58 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 { CPUTensor } from '../../tensor/cpu/tensor';
import { toCPU } from '../../util/convert';
import { OnnxNode } from '../node';
export class UpsampleNode extends OnnxNode {
constructor(attributes, inputs, outputs, constants, onnxVersion, mode) {
super(attributes, inputs, outputs, constants, onnxVersion, mode);
//@ts-ignore
this.sampleMode = this.getAttributeString('mode');
if (this.sampleMode !== 'nearest') {
throw new Error('Upsampling only supported with nearest neighbor sampling');
}
if (this.onnxVersion < 9) {
const scales = this.getAttributeFloats('scales');
if (scales !== undefined && scales !== null) {
this.scales = scales;
}
else {
throw new Error(`Upsample node with onnx version ${this.onnxVersion} is missing scales attribute`);
}
}
}
getScales(scale) {
return __awaiter(this, void 0, void 0, function* () {
if (this.onnxVersion < 9) {
return this.scales;
}
if (!(scale instanceof CPUTensor)) {
console.warn('Scales tensor for upsample not on CPU, need to transfer!');
scale = yield toCPU(scale);
}
const sc = scale;
const scales = new Array(sc.size);
for (let i = 0; i < sc.size; i++) {
scales[i] = sc.get(i);
}
return scales;
});
}
forward(inputs) {
return __awaiter(this, void 0, void 0, function* () {
const x = inputs[0];
const scale = inputs[1];
const scales = yield this.getScales(scale);
return [x.upsample(scales)];
});
}
getType() {
return 'Upsample';
}
delete() { }
}
//# sourceMappingURL=upsample.js.map