-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #102 from BruceDai/implement_dequantizeLinear
Implement dequantizeLinear
- Loading branch information
Showing
2 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
], | ||
}, | ||
); | ||
}); | ||
}); |