Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement dequantizeLinear #102

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 16 additions & 0 deletions src/dequantize_linear.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

import {mul, sub} from './binary.js';

/**
* Elementwise operator to scale a low precision integer (typically uint8 with a zero-point bias)
* to floating point.
* The calculation follows the expression (input - zeroPoint) * scale.
* @param {Tensor} input
* @param {Tensor} scale
* @param {Tensor} zeroPoint
* @return {Tensor}
*/
export function dequantizeLinear(input, scale, zeroPoint) {
return mul(sub(input, zeroPoint), scale);
}
147 changes: 147 additions & 0 deletions test/dequantize_linear_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
'use strict';

import {dequantizeLinear} from '../src/dequantize_linear.js';
import {Tensor} from '../src/lib/tensor.js';
import * as utils from './utils.js';

describe('test dequantizeLinear', function() {
function testDequantizeLinear(input, scale, zeroPoint, expected) {
const inputTensor = new Tensor(input.shape, input.value);
const scaleTensor = new Tensor(scale.shape, scale.value);
const zeroPointTensor = new Tensor(zeroPoint.shape, zeroPoint.value);
const outputTensor = dequantizeLinear(inputTensor, scaleTensor, zeroPointTensor);
utils.checkShape(outputTensor, expected.shape);
utils.checkValue(outputTensor, expected.value);
}


it('dequantizeLinear 1D broadcasting scale and zeroPoint', function() {
testDequantizeLinear(
BruceDai marked this conversation as resolved.
Show resolved Hide resolved
{ // input
shape: [4],
value: [0, 3, 128, 255],
},
{ // scale
shape: [1],
value: [2],
},
{ // zeroPoint of uint8
shape: [1],
value: [128],
},
{ // expected
shape: [4],
value: [-256, -250, 0, 254],
},
);
});

it('dequantizeLinear 2D', function() {
testDequantizeLinear(
{ // input
shape: [3, 4],
value: [
0, 1, 2, 3,
0, 1, 2, 3,
0, 10, 20, 30,
],
},
{ // scale
shape: [3, 4],
value: [
1, 1, 1, 1,
2, 2, 2, 2,
4, 4, 4, 4,
],
},
{ // zeroPoint
shape: [3, 4],
value: [
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
],
},
{ // expected
shape: [3, 4],
value: [
0, 1, 2, 3,
0, 2, 4, 6,
0, 40, 80, 120,
],
},
);
});

it('dequantizeLinear 2D broadcasting scale and zeroPoint', function() {
testDequantizeLinear(
{ // input
shape: [3, 4],
value: [
0, 1, 2, 3,
0, 1, 2, 3,
0, 10, 20, 30,
],
},
{ // scale
shape: [3, 1],
value: [
1,
2,
4,
],
},
{ // zeroPoint
shape: [3, 1],
value: [
0,
0,
0,
BruceDai marked this conversation as resolved.
Show resolved Hide resolved
],
},
{ // expected
shape: [3, 4],
value: [
0, 1, 2, 3,
0, 2, 4, 6,
0, 40, 80, 120,
],
},
);
});

it('dequantizeLinear 4D broadcasting scale and zeroPoint', function() {
testDequantizeLinear(
{ // input
shape: [1, 1, 3, 4],
value: [
0, 1, 2, 3,
0, 1, 2, 3,
0, 10, 20, 30,
],
},
{ // scale
shape: [3, 1],
value: [
1,
2,
4,
],
},
{ // zeroPoint
shape: [1],
value: [
0,
],
},
{ // expected
shape: [1, 1, 3, 4],
value: [
0, 1, 2, 3,
0, 2, 4, 6,
0, 40, 80, 120,
],
},
);
});
});
Loading