@hoff97/tensor-js
Version:
PyTorch like deep learning inferrence library
77 lines (75 loc) • 2.46 kB
JavaScript
import { defaultAllocator } from '../../../tensor/gpu/gl';
import { getSize } from '../../../util/shape';
import { Operation } from '../operation';
export class RepeatOperation extends Operation {
constructor(tensorConstructor, dtype, allocator) {
super(tensorConstructor, dtype, allocator);
}
getVariables() {
return `
${this.getVarModifier('repeats')} int repeats[${this.maxRank}];
`;
}
getUniformAttrs() {
return [{ name: 'repeats', length: this.maxRank }];
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getFragmentShader(info) {
return `
float process(int index[${this.maxRank}]) {
int inIndex[${this.maxRank}];
${this.initIndex('inIndex')}
for (int i = 0; i < ${this.maxRank}; i++) {
if (repeats[i] == -1) {
break;
}
int d = index[i] / shapeA[i];
inIndex[i] = index[i] - d*shapeA[i];
}
return _A(inIndex);
}
${this.getDefaultMain()}
`;
}
getOutputShape(input) {
const rank = input.A.shape.length;
const outputShape = new Array(rank);
for (let i = 0; i < rank; i++) {
outputShape[i] = input.A.shape[i] * input.repeats[i];
}
return outputShape;
}
getTextureNames() {
return ['A'];
}
calc(input) {
if (this.fullyStatic && this.outputShape !== undefined) {
return this.compute(this.outputShape, { A: input.A });
}
const outputShape = this.getOutputShape(input);
return this.compute(outputShape, { A: input.A }, { repeats: this.copyPad(input.repeats) });
}
compile(info) {
if (info.shapeA !== undefined) {
this.maxRank = info.shapeA.length;
}
super.compile(info);
}
getCompilationInfo(input) {
const outputShape = this.getOutputShape(input);
const outputSize = defaultAllocator.getAllocationDimensions(getSize(outputShape), this.dtype);
return {
shapeA: input.A.shape,
widthA: input.A.memory.width,
heightA: input.A.memory.height,
shapeOutput: outputShape,
widthOutput: outputSize.width,
heightOutput: outputSize.height,
repeats: input.repeats,
};
}
getInputInfoString(input) {
return `${input.A.shape}-${input.repeats}`;
}
}
//# sourceMappingURL=repeat.js.map