Repository navigation
Cast datatypes at runtime #39
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
Open
apphp
wants to merge
2
commits into
RubixML:main
Choose a base branch
from
apphp:branch-38
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,4 +47,6 @@ test.php | |
| tensor | ||
| test_img.jpg | ||
| composer.lock | ||
| vendor | ||
| vendor | ||
| docker | ||
| docker-compose.yml | ||
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
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,50 @@ | ||
| --TEST-- | ||
| NDArray::cast() / toDataType() change the dtype in place and return $this | ||
| --FILE-- | ||
| <?php | ||
| /* 1. Ticket example: a bare call changes $ndarray itself */ | ||
| $a = new NDArray([1.5, 2.5, 3.7], 'float32'); | ||
| $alias = $a; | ||
| $id = spl_object_id($a); | ||
| $a->cast('int32'); | ||
| echo $a->dataType(), ' ', json_encode($a->toArray()), "\n"; | ||
| echo spl_object_id($a) === $id ? "same object\n" : "NEW OBJECT\n"; | ||
| echo $alias->dataType(), " (alias)\n"; | ||
|
|
||
| /* 2. toDataType() alias; return value is $this */ | ||
| $ret = $a->toDataType('float64'); | ||
| echo $ret === $a ? "returns \$this\n" : "returns other\n"; | ||
| echo $a->dataType(), ' ', json_encode($a->toArray()), "\n"; | ||
|
|
||
| /* 3. Ticket example with float16; chaining */ | ||
| $h = new NDArray([1.5, 2.5, 3.7], 'float32'); | ||
| echo json_encode($h->cast('float16')->toArray()), ' ', $h->dataType(), "\n"; | ||
|
|
||
| /* 4. Same dtype: no-op, still returns $this */ | ||
| echo $h->cast('float16') === $h ? "same-type ok\n" : "same-type BAD\n"; | ||
|
|
||
| /* 5. Keep the original by casting a copy */ | ||
| $src = new NDArray([1.5, 2.5], 'float32'); | ||
| $dst = NumPower::copy($src)->cast('int8'); | ||
| echo $src->dataType(), ' ', json_encode($src->toArray()), ' | ', | ||
| $dst->dataType(), ' ', json_encode($dst->toArray()), "\n"; | ||
|
|
||
| /* 6. Invalid dtype throws, array unchanged */ | ||
| try { | ||
| $src->cast('bogus'); | ||
| } catch (Error $e) { | ||
| echo "Error caught\n"; | ||
| } | ||
| echo $src->dataType(), "\n"; | ||
| ?> | ||
| --EXPECT-- | ||
| int32 [1,2,3] | ||
| same object | ||
| int32 (alias) | ||
| returns $this | ||
| float64 [1,2,3] | ||
| [1.5,2.5,3.69921875] float16 | ||
| same-type ok | ||
| float32 [1.5,2.5] | int8 [1,2] | ||
| Error caught | ||
| float32 | ||
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,16 @@ | ||
| --TEST-- | ||
| NDArray::cast() on a GPU array converts in place and stays on the GPU | ||
| --SKIPIF-- | ||
| <?php try { (new NDArray([1.0]))->gpu(); } catch (Error $e) { die('skip ' . $e->getMessage()); } ?> | ||
| --FILE-- | ||
| <?php | ||
| $g = (new NDArray([1.5, 2.5, 3.5], 'float32'))->gpu(); | ||
| $ret = $g->cast('int32'); | ||
| echo $ret === $g ? "returns \$this\n" : "returns other\n"; | ||
| echo $g->isGPU() ? "gpu\n" : "cpu\n"; | ||
| echo $g->dataType(), ' ', json_encode($g->cpu()->toArray()), "\n"; | ||
| ?> | ||
| --EXPECT-- | ||
| returns $this | ||
| gpu | ||
| int32 [1,2,3] |
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,42 @@ | ||
| --TEST-- | ||
| NDArray::cast() / setDataType() reject invalid arguments and leave the array unchanged | ||
| --FILE-- | ||
| <?php | ||
| $cases = [ | ||
| 'no args' => fn($a) => $a->cast(), | ||
| 'two args' => fn($a) => $a->cast('int8', 'int8'), | ||
| 'array' => fn($a) => $a->cast([]), | ||
| 'object' => fn($a) => $a->cast(new stdClass()), | ||
| 'empty string' => fn($a) => $a->cast(''), | ||
| 'unknown' => fn($a) => $a->cast('float17'), | ||
| 'wrong case' => fn($a) => $a->cast('Float32'), | ||
| 'trailing space' => fn($a) => $a->cast('float16 '), | ||
| 'NUL byte' => fn($a) => $a->cast("float16\0junk"), | ||
| 'NUL byte (set)' => fn($a) => $a->setDataType("int8\0"), | ||
| 'numeric' => fn($a) => $a->cast(123), | ||
| ]; | ||
|
|
||
| foreach ($cases as $label => $fn) { | ||
| $a = new NDArray([1.5, 2.5], 'float32'); | ||
| try { | ||
| $fn($a); | ||
| $res = 'no exception'; | ||
| } catch (Throwable $e) { | ||
| $res = get_class($e); | ||
| } | ||
| $intact = $a->dataType() === 'float32' && $a->toArray() === [1.5, 2.5]; | ||
| echo str_pad($label, 15), ' ', $res, ' ', $intact ? 'intact' : 'CHANGED', "\n"; | ||
| } | ||
| ?> | ||
| --EXPECT-- | ||
| no args ArgumentCountError intact | ||
| two args ArgumentCountError intact | ||
| array TypeError intact | ||
| object TypeError intact | ||
| empty string Error intact | ||
| unknown Error intact | ||
| wrong case Error intact | ||
| trailing space Error intact | ||
| NUL byte Error intact | ||
| NUL byte (set) Error intact | ||
| numeric Error intact |
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,64 @@ | ||
| --TEST-- | ||
| NDArray::cast() boundaries: every dtype pair, 0-D, empty and N-D arrays | ||
| --FILE-- | ||
| <?php | ||
| /* 1. Every source dtype to every target dtype (14 x 14). Values 1..3 are | ||
| exactly representable in all of them, including float4/float8. | ||
| float128 and uint64 come back as PHP strings to keep full precision. */ | ||
| $types = ['float4', 'float8', 'float16', 'float32', 'float64', 'float128', | ||
| 'int8', 'uint8', 'int16', 'uint16', 'int32', 'uint32', 'int64', 'uint64']; | ||
| $bad = []; | ||
| foreach ($types as $from) { | ||
| foreach ($types as $to) { | ||
| $a = new NDArray([1, 2, 3], $from); | ||
| $a->cast($to); | ||
| $want = match (true) { | ||
| in_array($to, ['float128', 'uint64'], true) => ['1', '2', '3'], | ||
| $to[0] === 'f' => [1.0, 2.0, 3.0], | ||
| default => [1, 2, 3], | ||
| }; | ||
| if ($a->dataType() !== $to || $a->toArray() !== $want) { | ||
| $bad[] = "$from->$to"; | ||
| } | ||
| } | ||
| } | ||
| echo "pairs: ", count($types) ** 2, " bad: ", json_encode($bad), "\n"; | ||
|
|
||
| /* 2. 0-D array stays an NDArray with the new dtype */ | ||
| $z = new NDArray(2.7, 'float32'); | ||
| $z->cast('int32'); | ||
| echo "0-D: ", get_class($z), ' ', $z->dataType(), ' ', json_encode($z->toArray()), "\n"; | ||
|
|
||
| /* 3. Empty array */ | ||
| $e = NumPower::zeros([0]); | ||
| $e->cast('int8'); | ||
| echo "empty: ", $e->dataType(), ' ', json_encode($e->shape()), ' ', json_encode($e->toArray()), "\n"; | ||
|
|
||
| /* 4. N-D shape and values preserved */ | ||
| $m = NumPower::ones([2, 3, 4]); | ||
| $m->cast('int16'); | ||
| $flat = array_merge(...array_merge(...$m->toArray())); | ||
| echo "3-D: ", $m->dataType(), ' ', json_encode($m->shape()), ' all ones: ', | ||
| ($flat === array_fill(0, 24, 1) ? 'yes' : 'no'), "\n"; | ||
|
|
||
| /* 5. Truncation toward zero for float -> int */ | ||
| $t = new NDArray([1.9, -1.9, 0.5, -0.5], 'float64'); | ||
| $t->cast('int32'); | ||
| echo "truncate: ", json_encode($t->toArray()), "\n"; | ||
|
|
||
| /* 6. Repeated casts back and forth do not leak */ | ||
| $x = NumPower::ones([1000]); | ||
| $x->cast('int16'); | ||
| $m0 = memory_get_usage(); | ||
| for ($i = 0; $i < 10000; $i++) { | ||
| $x->cast($i % 2 ? 'int16' : 'float32'); | ||
| } | ||
| echo "leak: ", (memory_get_usage() - $m0 < 64 * 1024 ? 'no' : 'yes'), "\n"; | ||
| ?> | ||
| --EXPECT-- | ||
| pairs: 196 bad: [] | ||
| 0-D: NDArray int32 2 | ||
| empty: int8 [0] [] | ||
| 3-D: int16 [2,3,4] all ones: yes | ||
| truncate: [1,-1,0,0] | ||
| leak: no |
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::cast() with slices and clones: only the cast object changes | ||
| --FILE-- | ||
| <?php | ||
| /* 1. Casting a slice changes only the slice */ | ||
| $p = new NDArray([[1.5, 2.5], [3.5, 4.5]], 'float32'); | ||
| $s = $p[1]; | ||
| $s->cast('int32'); | ||
| echo "slice: ", $s->dataType(), ' ', json_encode($s->toArray()), "\n"; | ||
| echo "parent: ", $p->dataType(), ' ', json_encode($p->toArray()), "\n"; | ||
|
|
||
| /* 2. Casting the parent keeps an existing slice valid and unchanged */ | ||
| $p = new NDArray([[1.5, 2.5], [3.5, 4.5]], 'float32'); | ||
| $s = $p[0]; | ||
| $p->cast('int8'); | ||
| echo "parent: ", $p->dataType(), ' ', json_encode($p->toArray()), "\n"; | ||
| echo "slice: ", $s->dataType(), ' ', json_encode($s->toArray()), "\n"; | ||
| unset($p); | ||
| echo "slice after parent freed: ", json_encode($s->toArray()), "\n"; | ||
|
|
||
| /* 3. A clone is independent */ | ||
| $a = new NDArray([1.5, 2.5], 'float32'); | ||
| $c = clone $a; | ||
| $c->cast('int8'); | ||
| echo "orig: ", $a->dataType(), ' ', json_encode($a->toArray()), "\n"; | ||
| echo "clone: ", $c->dataType(), ' ', json_encode($c->toArray()), "\n"; | ||
|
|
||
| /* 4. Arithmetic works on a cast array */ | ||
| $x = new NDArray([1.5, 2.5], 'float32'); | ||
| $x->cast('int32'); | ||
| echo "add: ", json_encode(NumPower::add($x, new NDArray([1, 1], 'int32'))->toArray()), "\n"; | ||
| ?> | ||
| --EXPECT-- | ||
| slice: int32 [3,4] | ||
| parent: float32 [[1.5,2.5],[3.5,4.5]] | ||
| parent: int8 [[1,2],[3,4]] | ||
| slice: float32 [1.5,2.5] | ||
| slice after parent freed: [1.5,2.5] | ||
| orig: float32 [1.5,2.5] | ||
| clone: int8 [1,2] | ||
| add: [2,3] |
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.
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.
Totally unrelated, but what are your thoughts on either
A. Converting tests that only test the public API to PHP Unit
B. Only testing the extension through the public API, i.e. subsystems are tested indirectly, and dropping all these .phpt style tests
?