-
Notifications
You must be signed in to change notification settings - Fork 4
NDArray setDataType() and dataType() #33
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
35aba71
Intial commit
andrewdalpino b335d3e
Potential fix for pull request finding
andrewdalpino d05d3f1
Potential fix for pull request finding
andrewdalpino ec48e99
Modify buffer in place
andrewdalpino bbab8a0
Add accessor
andrewdalpino e063d04
Test that object ids are the same
andrewdalpino File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,75 @@ | ||
| --TEST-- | ||
| NDArray::astype() returns a new NDArray with the target dtype, preserving device | ||
| --FILE-- | ||
| <?php | ||
| /* astype() must: | ||
| - return a NEW NDArray (original unchanged) | ||
| - convert element values and PHP types to the target dtype | ||
| - throw on an unknown dtype | ||
| - be a no-op copy when the target equals the source dtype */ | ||
|
|
||
| /* 1. float32 -> int32: values truncate, elements become ints. | ||
| Capture the original's values BEFORE the cast for an "unchanged" check. */ | ||
| $a = new NDArray([1.5, 2.5, 3.7], 'float32'); | ||
| $original_vals = $a->toArray(); | ||
| $r = $a->astype('int32'); | ||
| $php = $r->toArray(); | ||
| $ok = $r instanceof NDArray && $php === [1, 2, 3] | ||
| && array_map('gettype', $php) === ['integer', 'integer', 'integer']; | ||
| echo 'float32->int32: ', ($ok ? 'OK' : 'BAD'), ' ', json_encode($php), "\n"; | ||
|
|
||
| /* 2. int32 -> float64: elements become floats, values preserved */ | ||
| $b = new NDArray([1, 2, 3], 'int32'); | ||
| $r = $b->astype('float64'); | ||
| $php = $r->toArray(); | ||
| $ok = $r instanceof NDArray && $php === [1.0, 2.0, 3.0] | ||
| && array_map('gettype', $php) === ['double', 'double', 'double']; | ||
| echo 'int32->float64: ', ($ok ? 'OK' : 'BAD'), ' ', json_encode($php), "\n"; | ||
|
|
||
| /* 3. original array is unchanged by the cast (still the same float32 values) */ | ||
| $ok = $a->toArray() === $original_vals; | ||
| echo 'original-unchanged: ', ($ok ? 'OK' : 'BAD'), ' ', json_encode($a->toArray()), "\n"; | ||
|
|
||
| /* 4. device preserved: a CPU array stays CPU after astype */ | ||
| $c = new NDArray([1.0, 2.0, 3.0], 'float32'); | ||
| $rc = $c->astype('float64'); | ||
| $ok = $rc instanceof NDArray && !$rc->isGPU() && $rc->toArray() === [1.0, 2.0, 3.0]; | ||
| echo 'device-stays-CPU: ', ($ok ? 'OK' : 'BAD'), ' isGPU=', ($rc->isGPU() ? 1 : 0), "\n"; | ||
|
|
||
| /* 5. same-dtype cast returns a new, equal array */ | ||
| $d = new NDArray([4, 5, 6], 'int32'); | ||
| $rd = $d->astype('int32'); | ||
| $ok = ($rd !== $d) && $rd->toArray() === [4, 5, 6] && !$rd->isGPU(); | ||
| echo 'same-dtype-copy: ', ($ok ? 'OK' : 'BAD'), "\n"; | ||
|
|
||
| /* 6. 0-D scalar (shape []) stays an NDArray after astype; __toString is "7\n" */ | ||
| $e = new NDArray(7.0); | ||
| $re = $e->astype('int32'); | ||
| $ok = $re instanceof NDArray && $re->shape() === [] && trim((string)$re) === '7'; | ||
| echo 'zero-d: ', ($ok ? 'OK' : 'BAD'), ' shape=', json_encode($re->shape()), "\n"; | ||
|
|
||
| /* 7. unknown dtype throws, message matches the canonical list */ | ||
| try { | ||
| (new NDArray([1, 2, 3], 'float32'))->astype('badtype'); | ||
| echo "badtype: NO-THROW\n"; | ||
| } catch (Throwable $t) { | ||
| echo "badtype threw: ", get_class($t), " | ", $t->getMessage(), "\n"; | ||
| } | ||
|
|
||
| /* 8. wrong argument count rejects */ | ||
| try { | ||
| (new NDArray([1, 2, 3], 'float32'))->astype(); | ||
| echo "no-arg: NO-THROW\n"; | ||
| } catch (Throwable $t) { | ||
| echo "no-arg threw: ", get_class($t), "\n"; | ||
| } | ||
| ?> | ||
| --EXPECT-- | ||
| float32->int32: OK [1,2,3] | ||
| int32->float64: OK [1,2,3] | ||
| original-unchanged: OK [1.5,2.5,3.700000047683716] | ||
| device-stays-CPU: OK isGPU=0 | ||
| same-dtype-copy: OK | ||
| zero-d: OK shape=[] | ||
| badtype threw: Error | Invalid data type 'badtype'. Supported: float4, float8, float16, float32, float64, float128, int8, uint8, int16, uint16, int32, uint32, int64, uint64 | ||
| no-arg threw: ArgumentCountError |
This file contains hidden or 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,41 @@ | ||
| --TEST-- | ||
| NDArray::astype() on a GPU array keeps the array on GPU and converts values | ||
| --SKIPIF-- | ||
| <?php try { (new NDArray([1.0]))->gpu(); } catch (Error $e) { die('skip ' . $e->getMessage()); } ?> | ||
| --FILE-- | ||
| <?php | ||
| /* When the source array is on the GPU, astype() must keep the result on the | ||
| GPU and convert the values. Cast-back via ->cpu() to inspect values. */ | ||
|
|
||
| $cases = [ | ||
| 'float32' => ['int32', [1.5, 2.5, 3.5], [1, 2, 3]], | ||
| 'int32' => ['float64', [1, 2, 3], [1.0, 2.0, 3.0]], | ||
| 'float64' => ['float32', [1.5, 2.5, 0.5], [1.5, 2.5, 0.5]], | ||
| 'int32' => ['int16', [1, 2, 3], [1, 2, 3]], | ||
| ]; | ||
|
|
||
| foreach ($cases as $src => [$dst, $vals, $expect]) { | ||
|
Copilot marked this conversation as resolved.
Outdated
|
||
| $g = (new NDArray($vals, $src))->gpu(); | ||
| $r = $g->astype($dst); | ||
| $on_gpu = $r->isGPU(); | ||
| $back = $r->cpu()->toArray(); | ||
| $ok = ($r instanceof NDArray) && $on_gpu && $back === $expect; | ||
| echo "$src->$dst: ", ($ok ? 'OK' : 'BAD'), | ||
| ' isGPU=', ($r->isGPU() ? 1 : 0), | ||
| ' vals=', json_encode($back), "\n"; | ||
| } | ||
|
|
||
| /* unknown dtype on GPU throws too */ | ||
| try { | ||
| (new NDArray([1.0], 'float32'))->gpu()->astype('nope'); | ||
| echo "badtype: NO-THROW\n"; | ||
| } catch (Throwable $t) { | ||
| echo "badtype threw: ", get_class($t), "\n"; | ||
| } | ||
| ?> | ||
| --EXPECT-- | ||
| float32->int32: OK isGPU=1 vals=[1,2,3] | ||
| int32->float64: OK isGPU=1 vals=[1,2,3] | ||
| float64->float32: OK isGPU=1 vals=[1.5,2.5,0.5] | ||
| int32->int16: OK isGPU=1 vals=[1,2,3] | ||
| badtype threw: Error | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.