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 cast #63

Merged
merged 14 commits into from
Dec 21, 2023
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = {
'chai': 'readonly',
'BigInt': 'readonly',
'BigInt64Array': 'readonly',
'BigUint64Array': 'readonly',
},
rules: {
'semi': 'error',
Expand Down
46 changes: 46 additions & 0 deletions src/cast.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';

import {Tensor} from '../src/lib/tensor.js';

/**
* Cast each element in the input tensor to the target data type.
* @param {Tensor} input
* @return {Tensor}
*/

export function cast(input, type) {
let outputArray;
switch (type) {
case 'float32':
outputArray = new Float32Array(input.data);
break;
case 'int32':
outputArray = new Int32Array(input.data);
break;
case 'uint32':
outputArray = new Uint32Array(input.data);
break;
case 'int64':
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix this error "TypeError: Cannot convert a BigInt value to a number" of CI.

outputArray = new BigInt64Array(Array.from(input.data, (num) => BigInt(Math.round(num))));
break;
case 'int8':
outputArray = new Int8Array(input.data);
break;
case 'uint8':
outputArray = new Uint8Array(input.data);
break;
case 'float16':
// todo
huningxin marked this conversation as resolved.
Show resolved Hide resolved
throw new Error('Unsupported output type: float16' );
// case 'uint64':
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There're two commented case block for uint64 type, please update it.

// // todo
// throw new Error('Unsupported output type: uint64' );
// case 'uint64':
// outputArray = new BigUint64Array(Array.from(input.data, (num) => BigInt(Math.round(num))));
// break;
default:
throw new Error('Unsupported output type: ' + type);
}
const output = new Tensor(input.shape, outputArray);
return output;
}
111 changes: 111 additions & 0 deletions test/cast_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
'use strict';

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

describe('test cast', function() {
function testCast(input, type, expected) {
const tensorInput = new Tensor(input.shape, input.data);
const outputTensor = cast(tensorInput, type);
utils.checkShape(outputTensor, expected.shape);
utils.checkValue(outputTensor, expected.data);
}

it('cast float32', function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rename test name as "cast float64 to float32", and same to others tests.

const input = {
shape: [5],
data: [
-0.25, 0.25, 3.21, 1234, -1234,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should include a case > X.5 too, like 3.7 for verify that truncation toward zero happens rather than rounding.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

],
};
const expected = {
shape: [5],
data: [
-0.25, 0.25, 3.2100000381469727, 1234, -1234,
],
};
testCast(input, 'float32', expected);
});

it('cast int32', function() {
const input = {
shape: [5],
data: [
-0.25, 0.25, 3.21, 1234, -1234,
],
};
const expected = {
shape: [5],
data: [
0, 0, 3, 1234, -1234,
],
};
testCast(input, 'int32', expected);
});


it('cast uint32', function() {
const input = {
shape: [5],
data: [
-0.25, 0.25, 3.21, 1234, -1234,
],
};
const expected = {
shape: [5],
data: [
0, 0, 3, 1234, 4294966062,
],
};
testCast(input, 'uint32', expected);
});

it('cast int64', function() {
const input = {
shape: [5],
data: [
-0.25, 0.25, 3.21, 1234, -1234,
],
};
const expected = {
shape: [5],
data: [
0n, 0n, 3n, 1234n, -1234n,
],
};
testCast(input, 'int64', expected);
});

it('cast int8', function() {
const input = {
shape: [5],
data: [
-0.25, 0.25, 3.21, 1234, -1234,
],
};
const expected = {
shape: [5],
data: [
0, 0, 3, -46, 46,
],
};
testCast(input, 'int8', expected);
});

it('cast uint8', function() {
const input = {
shape: [5],
data: [
-0.25, 0.25, 3.21, 1234, -1234,
],
};
const expected = {
shape: [5],
data: [
0, 0, 3, 210, 46,
],
};
testCast(input, 'uint8', expected);
});
});
Loading