Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tfjs-backend-cpu/src/backend_cpu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1152,7 +1152,7 @@ export class MathBackendCPU extends KernelBackend {
const v = values[i];
resultValues[i] = v > max ? max : (v < min ? min : v);
}
return this.makeOutput(resultValues, x.shape, 'float32');
return this.makeOutput(resultValues, x.shape, x.dtype);
}

abs<T extends Tensor>(x: T): T {
Expand Down
2 changes: 1 addition & 1 deletion tfjs-backend-wasm/src/kernels/ClipByValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function clip(args: {
const {x} = inputs;
const {clipValueMin, clipValueMax} = attrs;
const xId = backend.dataIdMap.get(x.dataId).id;
const out = backend.makeOutput(x.shape, 'float32');
const out = backend.makeOutput(x.shape, x.dtype);
const outId = backend.dataIdMap.get(out.dataId).id;
wasmClip(xId, clipValueMin, clipValueMax, outId);
return out;
Expand Down
9 changes: 9 additions & 0 deletions tfjs-core/src/ops/clip_by_value_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,13 @@ describeWithFlags('clipByValue', ALL_ENVS, () => {
expect(() => tf.clipByValue('q', 0, 1))
.toThrowError(/Argument 'x' passed to 'clipByValue' must be numeric/);
});

it('clip int32 tensor', async () => {
const min = -1;
const max = 50;
const tensor = tf.tensor([2, 3, 4], [3], 'int32');
const result = tf.clipByValue(tensor, min, max);
expectArraysClose(await result.data(), [2, 3, 4]);
expect(result.dtype).toEqual('int32');
});
});
4 changes: 2 additions & 2 deletions tfjs-node/src/nodejs_kernel_backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -722,8 +722,8 @@ export class NodeJSKernelBackend extends KernelBackend {
}

clip<T extends Tensor>(x: T, min: number, max: number): T {
const xMin = this.minimum(x, scalar(max));
return this.maximum(xMin, scalar(min)) as T;
const xMin = this.minimum(x, scalar(max, x.dtype));
return this.maximum(xMin, scalar(min, x.dtype)) as T;
}

abs<T extends Tensor>(x: T): T {
Expand Down