NDArray setDataType() and dataType() - #33
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a new NDArray::astype(string $dtype) method to the PHP extension, exposing existing dtype-casting functionality while preserving the array’s device (CPU/GPU) and returning a new NDArray. The PR also adds PHPT coverage for CPU and GPU casting behavior and error handling.
Changes:
- Implement
NDArray::astype()innumpower.c, including dtype canonicalization/validation and returning the cast result as anNDArray. - Register the new method in
class_NDArray_methods. - Add PHPT tests validating dtype conversion semantics on CPU and GPU, plus error cases.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
numpower.c |
Adds and registers NDArray::astype() method with dtype validation and casting via existing internal APIs. |
tests/types/029-astype-dtype-change.phpt |
Adds CPU-focused tests for value conversion, immutability (original unchanged), device preservation, scalar behavior, and errors. |
tests/types/030-astype-dtype-change-gpu.phpt |
Adds GPU-focused tests ensuring astype() preserves GPU residency and converts values, plus invalid-dtype behavior. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Comment on lines
+313
to
+317
| * `fill()`. The device (CPU or GPU) is preserved. If the target dtype | ||
| * equals the current dtype, the call is a true no-op with no allocation. | ||
| * | ||
| * Supported dtypes: float4, float8, float16, float32, float64, float128, | ||
| * int8, uint8, int16, uint16, int32, uint32, int64, uint64. |
Member
Author
There was a problem hiding this comment.
@SkibidiProduction thought this would be helpful to you
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Implement NDArray->setDataType(string $dtype) API. Also provide an accessor for the data type.