-
Notifications
You must be signed in to change notification settings - Fork 8
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
Implement cast #63
Changes from 6 commits
f56f40c
f4d78ce
a2f1074
d93ef4e
7e7ea30
73b0e4a
e0c74b6
e5ed271
1d3ff60
648dbc3
a4d9972
ca639b5
c0eacc2
431fb3c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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': | ||
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': | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} |
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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should include a case > X.5 too, like There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
}); | ||
}); |
There was a problem hiding this comment.
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.