@hoff97/tensor-js
Version:
PyTorch like deep learning inferrence library
82 lines (81 loc) • 2.58 kB
JavaScript
import { defaultAllocator } from '../../../tensor/gpu/gl';
import { getSize } from '../../../util/shape';
import { Operation } from '../operation';
export class ConcatOperation extends Operation {
constructor(tensorConstructor, dtype, allocator) {
super(tensorConstructor, dtype, allocator);
}
getVariables() {
return `
${this.getVarModifier('axis')} int axis;
`;
}
getUniformAttrs() {
return [{ name: 'axis' }];
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getFragmentShader(info) {
return `
float process(int index[${this.maxRank}]) {
float res = 0.0;
for (int i = 0; i < ${this.maxRank}; i++) {
if (i == axis) {
if (index[i] >= shapeA[i]) {
index[i] = index[i] - shapeA[i];
res = _B(index);
} else {
res = _A(index);
}
break;
}
}
return res;
}
${this.getDefaultMain()}
`;
}
getTextureNames() {
return ['A', 'B'];
}
calc(input) {
if (this.fullyStatic && this.outputShape !== undefined) {
return this.compute(this.outputShape, { A: input.A, B: input.B });
}
const outputShape = this.getOutputShape(input);
return this.compute(outputShape, { A: input.A, B: input.B }, { axis: input.axis });
}
getOutputShape(input) {
const outputShape = [...input.A.shape];
outputShape[input.axis] += input.B.shape[input.axis];
return outputShape;
}
compile(info) {
if (info.shapeA !== undefined) {
this.maxRank = info.shapeA.length;
}
if (info.shapeB !== undefined) {
this.maxRank = info.shapeB.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,
shapeB: input.B.shape,
widthB: input.B.memory.width,
heightB: input.B.memory.height,
shapeOutput: outputShape,
widthOutput: outputSize.width,
heightOutput: outputSize.height,
axis: input.axis,
};
}
getInputInfoString(input) {
return `${input.A.shape}-${input.B.shape}-${input.axis}`;
}
}
//# sourceMappingURL=concat.js.map