-
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
Support dynamic indexing in ti.Vector/ti.Matrix #2590
Comments
Dynamic indexing refers to the following two cases: @ti.kernel
def dynamic_index_local_vector(i: ti.i32) -> ti.f32:
a = ti.Vector([0.0, 1.0, 2.0])
return a[i]
b = ti.Vector.field(3, ti.f32, shape=())
@ti.kernel
def dynamic_index_vector_field(i: ti.i32) -> ti.f32:
return b[None][i] We would like to gradually support this feature with
A few months ago, @squarefk made a prototype for this in #2543, #2637, #2673, #2773. He also gave a talk (https://github.com/taichi-dev/taichicon/releases/download/taichicon02/Dynamic.Indexing-Freely.Access.Vector.and.Matrix.Elements.at.Runtime-Yu.Fang.pdf) at TaichiCon02 to present his ideas - common use cases can be translated to the form of
However, there are still lots of engineering efforts to be done in order to make this idea fully fit into the language and the compiler. Recently, I've been driving the development of this feature. Related work includes #3218, #3228, #3237, #3244, #3468, #3517, #3524, #3546, #3599. Also, this feature pushes us to support frontend type check (#3301). Now we can completely use dynamic indexing of local vectors/matrices now. My next focus is to fully support dynamic indexing of elements of vector/matrix fields. A n-D vector field is currently implemented as a tuple of n scalar fields, and we will refer to them as sub-fields below. Here is the preliminary roadmap:
# assume v is an element of a vector field
for i in ti.static(range(v.n)):
if i == index:
return v[i] |
Issue: #2590 ### Brief Summary 1. Dynamic indexing in metal is supported by mimicking the implementation of that in LLVM backends. 2. `ret_type` change of `ArgLoadStmt` is moved out of the `ScalarizePointers` pass, which is designed to scalarize `alloca` and will be removed once dynamic indexing becomes default. 3. Configs of dynamic indexing tests are fixed. Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Issue: #2590 ### Brief Summary Under pure `dynamic_index` setting, `MatrixPtrStmt`s are not scalarized. It actually produces `2n` more instructions (`n` `ConstStmt`s and n `MatrixPtrStmt`s) than the scalarized setting, where `n` is the number of usages of `MatrixPtrStmt`s. This PR adds `ExtractPointers` pass to eliminate all the redundant instructions. See comments in the code for details. After this PR, the number of instructions after the `scalarize()` pass of the script in #6933 under dynamic index reduces from 49589 to 26581, and the compilation time reduces from 20.02s to 7.82s. Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Issue: taichi-dev#2590 ### Brief Summary This PR supports dynamic indexing in spirv (sister PR: taichi-dev#6985). `_test_local_matrix_non_constant_index()` is modified due to lack of `AssertStmt` support in spirv. Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Issue: taichi-dev#2590 ### Brief Summary Under pure `dynamic_index` setting, `MatrixPtrStmt`s are not scalarized. It actually produces `2n` more instructions (`n` `ConstStmt`s and n `MatrixPtrStmt`s) than the scalarized setting, where `n` is the number of usages of `MatrixPtrStmt`s. This PR adds `ExtractPointers` pass to eliminate all the redundant instructions. See comments in the code for details. After this PR, the number of instructions after the `scalarize()` pass of the script in taichi-dev#6933 under dynamic index reduces from 49589 to 26581, and the compilation time reduces from 20.02s to 7.82s. Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Issue: #2590 ### Brief Summary Previously `ScalarizePointers` (which is for allocas) takes place only when `dynamic_index=False`. In this PR, we automatically identify those allocas only indexed with constants and get them scalarized, and let the remaining allocas go through the code path for dynamic indexing. In this way, we make old user programs behave the same regardless of the `dynamic_index` option, and provide dynamic indexing support for new user programs at the same time. Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Lin Jiang <[email protected]>
Issue: #2590 ### Brief Summary Dynamic indexing is now automatically supported and no switch is needed. Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Issue: taichi-dev#2590 ### Brief Summary Dynamic indexing is now automatically supported and no switch is needed. Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Issue: #2590 ### Brief Summary The `dynamic_index` switch of `ti.init()` should be removed according to the [deprecation notice](https://github.com/taichi-dev/taichi/releases/tag/v1.4.0).
Close as this has been finished. |
Issue: taichi-dev#2590 ### Brief Summary 1. Dynamic indexing in metal is supported by mimicking the implementation of that in LLVM backends. 2. `ret_type` change of `ArgLoadStmt` is moved out of the `ScalarizePointers` pass, which is designed to scalarize `alloca` and will be removed once dynamic indexing becomes default. 3. Configs of dynamic indexing tests are fixed. Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Issue: taichi-dev#2590 ### Brief Summary This PR supports dynamic indexing in spirv (sister PR: taichi-dev#6985). `_test_local_matrix_non_constant_index()` is modified due to lack of `AssertStmt` support in spirv. Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Issue: taichi-dev#2590 ### Brief Summary Under pure `dynamic_index` setting, `MatrixPtrStmt`s are not scalarized. It actually produces `2n` more instructions (`n` `ConstStmt`s and n `MatrixPtrStmt`s) than the scalarized setting, where `n` is the number of usages of `MatrixPtrStmt`s. This PR adds `ExtractPointers` pass to eliminate all the redundant instructions. See comments in the code for details. After this PR, the number of instructions after the `scalarize()` pass of the script in taichi-dev#6933 under dynamic index reduces from 49589 to 26581, and the compilation time reduces from 20.02s to 7.82s. Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Issue: taichi-dev#2590 ### Brief Summary Previously `ScalarizePointers` (which is for allocas) takes place only when `dynamic_index=False`. In this PR, we automatically identify those allocas only indexed with constants and get them scalarized, and let the remaining allocas go through the code path for dynamic indexing. In this way, we make old user programs behave the same regardless of the `dynamic_index` option, and provide dynamic indexing support for new user programs at the same time. Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Lin Jiang <[email protected]>
Issue: taichi-dev#2590 ### Brief Summary Dynamic indexing is now automatically supported and no switch is needed. Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Issue: taichi-dev#2590 ### Brief Summary The `dynamic_index` switch of `ti.init()` should be removed according to the [deprecation notice](https://github.com/taichi-dev/taichi/releases/tag/v1.4.0).
The original issue was #1004. This issue tracks the progress of supporting the new feature. Detailed design stories are commented below.
The text was updated successfully, but these errors were encountered: