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
Merged

Implement cast #63

merged 14 commits into from
Dec 21, 2023

Conversation

mei1127
Copy link
Contributor

@mei1127 mei1127 commented Dec 13, 2023

@BruceDai @huningxin @fdwr PTAL,thanks!

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.

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.

src/cast.js Outdated
case 'float16':
// todo
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.

@mei1127 mei1127 changed the title Add cast Implement cast Dec 13, 2023
Copy link

@fdwr fdwr left a comment

Choose a reason for hiding this comment

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

Some bounds considerations and test case addition requests, but otherwise LGTM. TY.

const expected = {
shape: [5],
data: [
0, 0, 3, 210, 46,
Copy link

Choose a reason for hiding this comment

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

210, 46
Casting between values that are beyond the limit of that data type is not well defined because different hardware will have different behaviors (CPU, NPU, GPU). Some will saturate 1234 to 255. Some will coerce to integer and apply bitwise truncation. This example clearly favors the typical x86 CPU behavior, but you will get surprises when you run this via DML, depending on which GPU is used.

(same comment applies elsewhere too for casts beyond bounds)

Copy link

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, thank! I'll convert examples outside the type range into within the range.

Copy link
Contributor

Choose a reason for hiding this comment

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

+1, let's focus on the defined behavior at this stage.

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 input = {
shape: [5],
data: [
0, 1, 2, 3, 3,
Copy link

Choose a reason for hiding this comment

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

Including one negative number would be good.

test/utils.js Outdated
distance = distance >= 0 ? distance : -distance;
return assert.isTrue(distance <= nulp, message);
} else {
let distance = a-b;
Copy link

Choose a reason for hiding this comment

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

[nit] a - b consistent spacing with line 33.

Copy link

Choose a reason for hiding this comment

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

(interesting - so in Javascript, int64 is considered not a "number" 🙃)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The integer of type INT64 exceeds the range of JavaScript's Number type and belongs to the integer of BigInteger type. The JavaScript exception "can't convert BigInt to number" occurs when an arithmetic operation involves a mix of BigInt and Number values. FYI: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cant_convert_BigInt_to_number?retiredLocale=pl

src/cast.js Outdated
break;
case 'uint32':
outputArray = new Uint32Array(input.data);
outputArray = new Uint32Array(Array.from(input.data, (num) => (Math.round(num))));
Copy link

@fdwr fdwr Dec 19, 2023

Choose a reason for hiding this comment

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

Truncation toward zero is the expected casting behavior for float to int, not rounding to nearest. That's consistent with existing ML libraries I know of. e.g. float32 3.7 -> uint32 3. Isn't the default behavior of Uint32Array already the expected truncation toward zero behavior? Otherwise we need Math.trunc().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm sorry for the misunderstanding. I've revised the code :)

Copy link

@fdwr fdwr left a comment

Choose a reason for hiding this comment

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

👍 TY Mei.

src/cast.js Outdated Show resolved Hide resolved
src/cast.js Outdated Show resolved Hide resolved
@BruceDai BruceDai mentioned this pull request Dec 20, 2023
@huningxin huningxin merged commit 64c784e into webmachinelearning:main Dec 21, 2023
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants