Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.

implement Resize operator #228

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
6 changes: 3 additions & 3 deletions docs/operators.md

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions lib/api/tensor-impl-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ export function fromInternalTensor(internalTensor: InternalTensor): ApiTensor {
return new ApiTensor(new Float32Array(internalTensor.floatData), 'float32', internalTensor.dims);
case 'string':
return new ApiTensor(internalTensor.stringData, 'string', internalTensor.dims);
case 'int8' || 'uint8' || 'int16' || 'uint16' || 'uint32':
case 'int8':
case 'uint8':
case 'int16':
case 'uint16':
case 'uint32':
return new ApiTensor(new Int32Array(internalTensor.integerData), 'int32', internalTensor.dims);
case 'int32':
return new ApiTensor(internalTensor.integerData as Int32Array, 'int32', internalTensor.dims);
Expand Down Expand Up @@ -57,7 +61,7 @@ export function matchElementType(type: TensorInterface.Type, element: TensorInte
}

export function validateIndices(indices: ReadonlyArray<number>) {
if (indices.length < 0 || indices.length > 6) {
if (indices.length > 6) {
throw new RangeError(`Only rank 0 to 6 is supported for tensor shape.`);
}
for (const n of indices) {
Expand Down
9 changes: 6 additions & 3 deletions lib/backends/cpu/op-resolve-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {CpuTranspose} from './ops/transpose';
import * as unaryOps from './ops/unary-op';
import {CpuUnaryOp} from './ops/unary-op';
import {CpuUnsqueeze} from './ops/unsqueeze';
import {CpuUpsample, CpuUpsampleV9} from './ops/upsample';
import {CpuUpsample} from './ops/upsample';

export const CPU_OP_RESOLVE_RULES: ReadonlyArray<OpSet.ResolveRule> = [
['Abs', '', '6+', () => new CpuUnaryOp(NUMBER_TYPES, unaryOps.abs)],
Expand Down Expand Up @@ -71,6 +71,7 @@ export const CPU_OP_RESOLVE_RULES: ReadonlyArray<OpSet.ResolveRule> = [
['InstanceNormalization', '', '6+', () => new CpuInstanceNormalization()],
['IsNaN', '', '9+', () => new CpuUnaryOp(FLOAT_TYPES, unaryOps.isNan, undefined, 'bool')],
['LeakyRelu', '', '6+', () => new CpuUnaryOp(FLOAT_TYPES, unaryOps.leakyRelu, unaryOps.leakyReluInitializer)],
['Less', '', '7+', () => new CpuBinaryOp(NUMBER_TYPES, (a, b) => a < b ? 1 : 0, undefined, 'bool')],
['Log', '', '6+', () => new CpuUnaryOp(FLOAT_TYPES, unaryOps.log)],
['LRN', '', '1+', () => new CpuLrn()],
['MatMul', '', '1+', () => new CpuMatMul()],
Expand All @@ -92,6 +93,8 @@ export const CPU_OP_RESOLVE_RULES: ReadonlyArray<OpSet.ResolveRule> = [
['ReduceSumSquare', '', '1+', () => new cpuReduce.CpuReduceSumSquare()],
['Relu', '', '6+', () => new CpuUnaryOp(FLOAT_TYPES, unaryOps.relu)],
['Reshape', '', '5+', () => new CpuReshape()],
['Resize', '', '10', () => new CpuUpsample(10)],
['Resize', '', '11+', () => new CpuUpsample(11)],
['Shape', '', '1+', () => new CpuShape()],
['Sigmoid', '', '6+', () => new CpuUnaryOp(FLOAT_TYPES, unaryOps.sigmoid)],
['Sign', '', '9+', () => new CpuUnaryOp(NUMBER_TYPES, unaryOps.sign)],
Expand All @@ -109,7 +112,7 @@ export const CPU_OP_RESOLVE_RULES: ReadonlyArray<OpSet.ResolveRule> = [
['Tile', '', '6+', () => new CpuTile()],
['Transpose', '', '1+', () => new CpuTranspose()],
['Unsqueeze', '', '1+', () => new CpuUnsqueeze()],
['Upsample', '', '7-8', () => new CpuUpsample()],
['Upsample', '', '9', () => new CpuUpsampleV9()],
['Upsample', '', '7-8', () => new CpuUpsample(7)],
['Upsample', '', '9', () => new CpuUpsample(9)],
['Xor', '', '7+', () => new CpuBinaryOp(['bool'], (e1, e2) => (e1 ^ e2))],
];
2 changes: 1 addition & 1 deletion lib/backends/cpu/ops/concat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function concat(x: Tensor[], axis: number) {
// ensure all of the non-concatenated axes match each other
// along the way, calculate the shape of the output tensor
let concatAxisSize = inputShape[axis];
const outputShape = new Array<number>(inputShape.length);
const outputShape = inputShape.slice(0);

for (let i = 1; i < x.length; i++) {
const dataN = x[i];
Expand Down
4 changes: 2 additions & 2 deletions lib/backends/cpu/ops/reshape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class CpuReshape extends Reshape {
export function reshape(x: Tensor, shape: Tensor): Tensor {
const reshapedDims = ShapeUtil.calculateReshapedDims(x.dims, shape.integerData);
const output = new Tensor(reshapedDims, x.type);
const Y = output.floatData;
Y.set(x.floatData);
const Y = output.numberData;
Y.set(x.numberData);
return output;
}
Loading