Skip to content

Commit

Permalink
Merge pull request #102 from BruceDai/implement_dequantizeLinear
Browse files Browse the repository at this point in the history
Implement dequantizeLinear
  • Loading branch information
huningxin authored Sep 8, 2024
2 parents c386af8 + 58aba22 commit 379e00f
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 0 deletions.
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);
}
167 changes: 167 additions & 0 deletions test/dequantize_linear_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
'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 0D', function() {
testDequantizeLinear(
{ // input
shape: [],
value: [255],
},
{ // scale
shape: [],
value: [2],
},
{ // zeroPoint of uint8
shape: [],
value: [128],
},
{ // expected
shape: [],
value: [254],
},
);
});

it('dequantizeLinear 1D broadcasting scale and zeroPoint', function() {
testDequantizeLinear(
{ // 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: [
1,
2,
3,
],
},
{ // expected
shape: [3, 4],
value: [
-1, 0, 1, 2,
-4, -2, 0, 2,
-12, 28, 68, 108,
],
},
);
});

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,
],
},
);
});
});

0 comments on commit 379e00f

Please sign in to comment.