-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Ndarray type annotations & error message refinement #6572
Labels
P0
This issue is urgent to be solved and has highest priority
Comments
turbo0628
added a commit
that referenced
this issue
Nov 17, 2022
Issue: #6572 Changes: * Replace all the element_dim and element_shape arguments with matrix types in ndarray unit tests. * [BREAK CHANGE] raises a TypeError when external array doesn't math matrix element type shape. Before this PR we don't check the consistency and silently trigger recompilation. * Throw a warning if user specifies element_dim and element_shape Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
turbo0628
added a commit
that referenced
this issue
Nov 18, 2022
turbo0628
added a commit
that referenced
this issue
Nov 21, 2022
#6665) Issue: #6572 Changes: * Add deprecation warning when `element_shape` and `element_dim` are specified. * Internally remove `element_shape` and `element_dim` inside the type annotation. Added a helper function to create respective MatrixType in dtype for legacy code. * Add deprecation test. * Update docstring. Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Ailing <ailzhang@users.noreply.github.com>
turbo0628
added a commit
that referenced
this issue
Nov 22, 2022
Issue: #6572 * Deprecate `field_dim` in ndarray type annotation, replace with `ndim`. * Change tests accordingly. Examples are left unchanged. Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
turbo0628
added a commit
that referenced
this issue
Jan 10, 2023
) Issue: #6572 * Fix using dtype for symbolic args in AOT usages * Cleanup all the usages of `field_dim` and `element_shape` in examples and tests
Yet another confusing error message import taichi as ti
import numpy as np
ti.init(arch=ti.gpu)
@ti.kernel
def add_num(num : ti.f32, arr : ti.types.ndarray(dtype=ti.math.mat3, ndim=2)):
for i in ti.grouped(arr):
arr[i] = arr[i] + num
arr_np = np.ones((2,2,3,3), dtype=np.float32)
arr_ti = ti.ndarray(dtype=ti.math.vec3, shape=(2, 2))
arr_ti.from_numpy(arr_np)
add_num(1.12, arr_ti)
print(arr_ti.to_numpy())
The "expected shape" hint doesn't include the element shape, so it's very confusing. |
lin-hitonami
pushed a commit
to lin-hitonami/taichi
that referenced
this issue
Jan 11, 2023
…ichi-dev#7100) Issue: taichi-dev#6572 * Fix using dtype for symbolic args in AOT usages * Cleanup all the usages of `field_dim` and `element_shape` in examples and tests
lin-hitonami
pushed a commit
to lin-hitonami/taichi
that referenced
this issue
Jan 12, 2023
…ichi-dev#7100) Issue: taichi-dev#6572 * Fix using dtype for symbolic args in AOT usages * Cleanup all the usages of `field_dim` and `element_shape` in examples and tests
lin-hitonami
pushed a commit
that referenced
this issue
Jan 12, 2023
Issue: #6572 * Check external ndarray total dim against type annotation * Error out when element shape mismatch * Refine error messages Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
lin-hitonami
pushed a commit
that referenced
this issue
Jan 13, 2023
Issue: #6572 * Check external ndarray total dim against type annotation * Error out when element shape mismatch * Refine error messages Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
ailzhang
added a commit
that referenced
this issue
Apr 10, 2023
Issue: #6572 For backward compatibility reason we cannot change any positional arg and kwargs in `ti.field` but we this PR simply added support for vector and matrix dtypes. Whether to deprecate `ti.Vector.field` and `ti.Matrix.field` can be a separate decision. Testing: to avoid massive duplicated tests I just replaced all `ti.Vector.field/ti.Matrix.field` in `test_fields.py` to use this unified API. There're plenty of other files that use the old APIs so this should be fine for now. ### Brief Summary <!-- copilot:summary --> ### <samp>🤖 Generated by Copilot at 326d67e</samp> Unified the creation of vector and matrix fields with a single `field` function in `taichi.lang.impl`. Updated the tests in `test_field.py` to use the new function and removed the deprecated ones. This improves the consistency and readability of the field API. ### Walkthrough <!-- copilot:walkthrough --> ### <samp>🤖 Generated by Copilot at 326d67e</samp> * Rename and redefine `field` function to handle vector and matrix types ([link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-99744c5ae5f6a754d6f68408fdc64fb0d6097216518a7f3d1ef43ffe12599577L19-R19),[link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-99744c5ae5f6a754d6f68408fdc64fb0d6097216518a7f3d1ef43ffe12599577L698-R704),[link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-99744c5ae5f6a754d6f68408fdc64fb0d6097216518a7f3d1ef43ffe12599577R760-R805)) * Update `field` function docstring and examples in `python/taichi/lang/impl.py` ([link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-99744c5ae5f6a754d6f68408fdc64fb0d6097216518a7f3d1ef43ffe12599577R760-R805)) * Replace `Vector.field` and `Matrix.field` with `field` function in `tests/python/test_field.py` ([link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-c08dd53cc282976d42e5643ea69e8e30e390e2ebd2f4e73f2789f84ac56f2494L38-R39),[link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-c08dd53cc282976d42e5643ea69e8e30e390e2ebd2f4e73f2789f84ac56f2494L55-R57),[link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-c08dd53cc282976d42e5643ea69e8e30e390e2ebd2f4e73f2789f84ac56f2494L135-R137),[link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-c08dd53cc282976d42e5643ea69e8e30e390e2ebd2f4e73f2789f84ac56f2494L141-R144),[link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-c08dd53cc282976d42e5643ea69e8e30e390e2ebd2f4e73f2789f84ac56f2494L171-R174),[link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-c08dd53cc282976d42e5643ea69e8e30e390e2ebd2f4e73f2789f84ac56f2494L177-R181),[link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-c08dd53cc282976d42e5643ea69e8e30e390e2ebd2f4e73f2789f84ac56f2494L197-R203),[link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-c08dd53cc282976d42e5643ea69e8e30e390e2ebd2f4e73f2789f84ac56f2494L207-R212),[link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-c08dd53cc282976d42e5643ea69e8e30e390e2ebd2f4e73f2789f84ac56f2494L215-R221),[link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-c08dd53cc282976d42e5643ea69e8e30e390e2ebd2f4e73f2789f84ac56f2494L295-R300),[link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-c08dd53cc282976d42e5643ea69e8e30e390e2ebd2f4e73f2789f84ac56f2494L302-R308))
ailzhang
added a commit
to ailzhang/taichi
that referenced
this issue
Apr 12, 2023
Issue: taichi-dev#6572 For backward compatibility reason we cannot change any positional arg and kwargs in `ti.field` but we this PR simply added support for vector and matrix dtypes. Whether to deprecate `ti.Vector.field` and `ti.Matrix.field` can be a separate decision. Testing: to avoid massive duplicated tests I just replaced all `ti.Vector.field/ti.Matrix.field` in `test_fields.py` to use this unified API. There're plenty of other files that use the old APIs so this should be fine for now. ### Brief Summary <!-- copilot:summary --> ### <samp>🤖 Generated by Copilot at 326d67e</samp> Unified the creation of vector and matrix fields with a single `field` function in `taichi.lang.impl`. Updated the tests in `test_field.py` to use the new function and removed the deprecated ones. This improves the consistency and readability of the field API. ### Walkthrough <!-- copilot:walkthrough --> ### <samp>🤖 Generated by Copilot at 326d67e</samp> * Rename and redefine `field` function to handle vector and matrix types ([link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-99744c5ae5f6a754d6f68408fdc64fb0d6097216518a7f3d1ef43ffe12599577L19-R19),[link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-99744c5ae5f6a754d6f68408fdc64fb0d6097216518a7f3d1ef43ffe12599577L698-R704),[link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-99744c5ae5f6a754d6f68408fdc64fb0d6097216518a7f3d1ef43ffe12599577R760-R805)) * Update `field` function docstring and examples in `python/taichi/lang/impl.py` ([link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-99744c5ae5f6a754d6f68408fdc64fb0d6097216518a7f3d1ef43ffe12599577R760-R805)) * Replace `Vector.field` and `Matrix.field` with `field` function in `tests/python/test_field.py` ([link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-c08dd53cc282976d42e5643ea69e8e30e390e2ebd2f4e73f2789f84ac56f2494L38-R39),[link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-c08dd53cc282976d42e5643ea69e8e30e390e2ebd2f4e73f2789f84ac56f2494L55-R57),[link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-c08dd53cc282976d42e5643ea69e8e30e390e2ebd2f4e73f2789f84ac56f2494L135-R137),[link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-c08dd53cc282976d42e5643ea69e8e30e390e2ebd2f4e73f2789f84ac56f2494L141-R144),[link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-c08dd53cc282976d42e5643ea69e8e30e390e2ebd2f4e73f2789f84ac56f2494L171-R174),[link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-c08dd53cc282976d42e5643ea69e8e30e390e2ebd2f4e73f2789f84ac56f2494L177-R181),[link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-c08dd53cc282976d42e5643ea69e8e30e390e2ebd2f4e73f2789f84ac56f2494L197-R203),[link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-c08dd53cc282976d42e5643ea69e8e30e390e2ebd2f4e73f2789f84ac56f2494L207-R212),[link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-c08dd53cc282976d42e5643ea69e8e30e390e2ebd2f4e73f2789f84ac56f2494L215-R221),[link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-c08dd53cc282976d42e5643ea69e8e30e390e2ebd2f4e73f2789f84ac56f2494L295-R300),[link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-c08dd53cc282976d42e5643ea69e8e30e390e2ebd2f4e73f2789f84ac56f2494L302-R308))
ailzhang
added a commit
that referenced
this issue
May 10, 2023
…el (#7966) Issue: #6572 ### Brief Summary <!-- copilot:summary --> ### <samp>🤖 Generated by Copilot at d1aa92d</samp> Add new tests and error handling for ndarray feature. Improve `__repr__` method for `TensorType` and error message for `NdarrayType`. ### Walkthrough <!-- copilot:walkthrough --> ### <samp>🤖 Generated by Copilot at d1aa92d</samp> * Add a string representation method for tensor types ([link](https://github.com/taichi-dev/taichi/pull/7966/files?diff=unified&w=0#diff-78972e3ce6c462d977b9e713e447e8f3305899c8037a6992c37546aa0c4cb291L22-R25)) * Improve the error message for ndarray type validation ([link](https://github.com/taichi-dev/taichi/pull/7966/files?diff=unified&w=0#diff-06e0109e9fa5071f7d364306981845d410fa17425db48001e3ba69337b47c152L127-R127)) * Add tests for ndarray type mismatch scenarios ([link](https://github.com/taichi-dev/taichi/pull/7966/files?diff=unified&w=0#diff-ca3c8d1edb25b6a7f4affbb79b2e3e74f73b3757e5d465258ce42ea9eb09fbc0L6-R6), [link](https://github.com/taichi-dev/taichi/pull/7966/files?diff=unified&w=0#diff-ca3c8d1edb25b6a7f4affbb79b2e3e74f73b3757e5d465258ce42ea9eb09fbc0R870-R910))
quadpixels
pushed a commit
to quadpixels/taichi
that referenced
this issue
May 13, 2023
…i-dev#6620) Issue: taichi-dev#6572 Changes: * Replace all the element_dim and element_shape arguments with matrix types in ndarray unit tests. * [BREAK CHANGE] raises a TypeError when external array doesn't math matrix element type shape. Before this PR we don't check the consistency and silently trigger recompilation. * Throw a warning if user specifies element_dim and element_shape Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
quadpixels
pushed a commit
to quadpixels/taichi
that referenced
this issue
May 13, 2023
Issue: taichi-dev#6572 * Re-open taichi-dev#6620 which unexpectedly broke difftaichi and other user codes. Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
quadpixels
pushed a commit
to quadpixels/taichi
that referenced
this issue
May 13, 2023
taichi-dev#6665) Issue: taichi-dev#6572 Changes: * Add deprecation warning when `element_shape` and `element_dim` are specified. * Internally remove `element_shape` and `element_dim` inside the type annotation. Added a helper function to create respective MatrixType in dtype for legacy code. * Add deprecation test. * Update docstring. Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Ailing <ailzhang@users.noreply.github.com>
quadpixels
pushed a commit
to quadpixels/taichi
that referenced
this issue
May 13, 2023
Issue: taichi-dev#6572 * Deprecate `field_dim` in ndarray type annotation, replace with `ndim`. * Change tests accordingly. Examples are left unchanged. Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
quadpixels
pushed a commit
to quadpixels/taichi
that referenced
this issue
May 13, 2023
…ichi-dev#7100) Issue: taichi-dev#6572 * Fix using dtype for symbolic args in AOT usages * Cleanup all the usages of `field_dim` and `element_shape` in examples and tests
quadpixels
pushed a commit
to quadpixels/taichi
that referenced
this issue
May 13, 2023
Issue: taichi-dev#6572 * Check external ndarray total dim against type annotation * Error out when element shape mismatch * Refine error messages Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
quadpixels
pushed a commit
to quadpixels/taichi
that referenced
this issue
May 13, 2023
Issue: taichi-dev#6572 For backward compatibility reason we cannot change any positional arg and kwargs in `ti.field` but we this PR simply added support for vector and matrix dtypes. Whether to deprecate `ti.Vector.field` and `ti.Matrix.field` can be a separate decision. Testing: to avoid massive duplicated tests I just replaced all `ti.Vector.field/ti.Matrix.field` in `test_fields.py` to use this unified API. There're plenty of other files that use the old APIs so this should be fine for now. ### Brief Summary <!-- copilot:summary --> ### <samp>🤖 Generated by Copilot at 326d67e</samp> Unified the creation of vector and matrix fields with a single `field` function in `taichi.lang.impl`. Updated the tests in `test_field.py` to use the new function and removed the deprecated ones. This improves the consistency and readability of the field API. ### Walkthrough <!-- copilot:walkthrough --> ### <samp>🤖 Generated by Copilot at 326d67e</samp> * Rename and redefine `field` function to handle vector and matrix types ([link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-99744c5ae5f6a754d6f68408fdc64fb0d6097216518a7f3d1ef43ffe12599577L19-R19),[link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-99744c5ae5f6a754d6f68408fdc64fb0d6097216518a7f3d1ef43ffe12599577L698-R704),[link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-99744c5ae5f6a754d6f68408fdc64fb0d6097216518a7f3d1ef43ffe12599577R760-R805)) * Update `field` function docstring and examples in `python/taichi/lang/impl.py` ([link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-99744c5ae5f6a754d6f68408fdc64fb0d6097216518a7f3d1ef43ffe12599577R760-R805)) * Replace `Vector.field` and `Matrix.field` with `field` function in `tests/python/test_field.py` ([link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-c08dd53cc282976d42e5643ea69e8e30e390e2ebd2f4e73f2789f84ac56f2494L38-R39),[link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-c08dd53cc282976d42e5643ea69e8e30e390e2ebd2f4e73f2789f84ac56f2494L55-R57),[link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-c08dd53cc282976d42e5643ea69e8e30e390e2ebd2f4e73f2789f84ac56f2494L135-R137),[link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-c08dd53cc282976d42e5643ea69e8e30e390e2ebd2f4e73f2789f84ac56f2494L141-R144),[link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-c08dd53cc282976d42e5643ea69e8e30e390e2ebd2f4e73f2789f84ac56f2494L171-R174),[link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-c08dd53cc282976d42e5643ea69e8e30e390e2ebd2f4e73f2789f84ac56f2494L177-R181),[link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-c08dd53cc282976d42e5643ea69e8e30e390e2ebd2f4e73f2789f84ac56f2494L197-R203),[link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-c08dd53cc282976d42e5643ea69e8e30e390e2ebd2f4e73f2789f84ac56f2494L207-R212),[link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-c08dd53cc282976d42e5643ea69e8e30e390e2ebd2f4e73f2789f84ac56f2494L215-R221),[link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-c08dd53cc282976d42e5643ea69e8e30e390e2ebd2f4e73f2789f84ac56f2494L295-R300),[link](https://github.com/taichi-dev/taichi/pull/7761/files?diff=unified&w=0#diff-c08dd53cc282976d42e5643ea69e8e30e390e2ebd2f4e73f2789f84ac56f2494L302-R308))
quadpixels
pushed a commit
to quadpixels/taichi
that referenced
this issue
May 13, 2023
…el (taichi-dev#7966) Issue: taichi-dev#6572 ### Brief Summary <!-- copilot:summary --> ### <samp>🤖 Generated by Copilot at d1aa92d</samp> Add new tests and error handling for ndarray feature. Improve `__repr__` method for `TensorType` and error message for `NdarrayType`. ### Walkthrough <!-- copilot:walkthrough --> ### <samp>🤖 Generated by Copilot at d1aa92d</samp> * Add a string representation method for tensor types ([link](https://github.com/taichi-dev/taichi/pull/7966/files?diff=unified&w=0#diff-78972e3ce6c462d977b9e713e447e8f3305899c8037a6992c37546aa0c4cb291L22-R25)) * Improve the error message for ndarray type validation ([link](https://github.com/taichi-dev/taichi/pull/7966/files?diff=unified&w=0#diff-06e0109e9fa5071f7d364306981845d410fa17425db48001e3ba69337b47c152L127-R127)) * Add tests for ndarray type mismatch scenarios ([link](https://github.com/taichi-dev/taichi/pull/7966/files?diff=unified&w=0#diff-ca3c8d1edb25b6a7f4affbb79b2e3e74f73b3757e5d465258ce42ea9eb09fbc0L6-R6), [link](https://github.com/taichi-dev/taichi/pull/7966/files?diff=unified&w=0#diff-ca3c8d1edb25b6a7f4affbb79b2e3e74f73b3757e5d465258ce42ea9eb09fbc0R870-R910))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
We had a discussion about refining ndarray definition and type annotation in #4085 earlier this year and it's now a hard blocker if we want to properly release ndarray to users. Based on the discussions with @k-ye and @strongoier , we'll list out the desired APIs as well as some examples of bad error messages we wanna fix here.
Type annotation
Currently
ti.types.ndarray()
can take indtype/element_dim/element_shape/field_dim
as optional arguments, we'd like to:field_dim
tondim
(following numpy etc)element_dim
andelement_shape
in the type annotationAnd the desired ndarray type annotation API may only takes two optional argument
dtype
andndim
, which looks like below:Error message
Currently we've got a lot to improve in terms of telling users what's wrong with their ndarray usage, listing a few examples below:
A running demo can be found at https://gist.github.com/ailzhang/d207a53c26720cccb7e7c5a9687009c2
Note we'll need similar improvement when compiling kernels in AOT mode as well but those should be simple after the aforementioned issues are resolved.
Additional Cleanup
Notice the swap of
dtype
andshape
arguments here? We'd actually like to unify todtype
first order for all of them, which involves adding some deprecation warnings.ti.field()
and deprecateti.Vector.field()
andti.Matrix.field()
APIs.The text was updated successfully, but these errors were encountered: