-
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
[RFC] Taichi Matrix & Vector refactor plan #5819
Comments
jim19930609
changed the title
Taichi Matrix & Vector refactor plan
[RFC] Taichi Matrix & Vector refactor plan
Aug 18, 2022
This was referenced Aug 24, 2022
jim19930609
added a commit
that referenced
this issue
Sep 15, 2022
…Stmt & GlobalStoreStmt with TensorType (#5946) Related issue = #5873, #5819 This PR is working "Part ④" in #5873. Scalarization implementation plan: ![NdarrayMatrix_Indexing (1) drawio (3)](https://user-images.githubusercontent.com/22334008/188117283-582fe6d5-2a0b-4cc3-99d7-c54a8b82c3eb.png)
jim19930609
added a commit
that referenced
this issue
Sep 16, 2022
jim19930609
added a commit
that referenced
this issue
Sep 21, 2022
jim19930609
added a commit
that referenced
this issue
Sep 26, 2022
This was referenced Sep 27, 2022
jim19930609
added a commit
that referenced
this issue
Sep 29, 2022
…#6168) Related issue = #5873, #5819 This PR is working "Part ④" in #5873. [AllocaStmt scalarization] ``` Before: TensorType<4 x i32>* addr = AllocaStmt(TensorType<4 x i32>) After: i32 addr0 = AllocaStmt(i32) i32 addr1 = AllocaStmt(i32) i32 addr2 = AllocaStmt(i32) i32 addr3 = AllocaStmt(i32) scalarized_local_tensor_map_[addr] = {addr0, addr1, addr2, addr3} ``` [Load AllocaStmt] ``` Before: TensorType<4 x i32> val = LoadStmt(TensorType<4 x i32>* alloca_src) After: i32 val0 = LoadStmt(scalarized_local_tensor_map_[stmt][0]) i32 val1 = LoadStmt(scalarized_local_tensor_map_[stmt][1]) i32 val2 = LoadStmt(scalarized_local_tensor_map_[stmt][2]) i32 val3 = LoadStmt(scalarized_local_tensor_map_[stmt][3]) tmp = MatrixInitStmt(val0, val1, val2, val3) stmt->replace_all_usages_with(tmp) ``` [Store to AllocaStmt] ``` Before: StoreStmt(TensorType<4 x i32>* alloca_dest_stmt, TensorType<4 x i32> val) After: StoreStmt(i32* scalarized_local_tensor_map_[stmt][0], i32 val->cast<MatrixInitStmt>()->val[0]) StoreStmt(i32* scalarized_local_tensor_map_[stmt][1], i32 val->cast<MatrixInitStmt>()->val[1]) StoreStmt(i32* scalarized_local_tensor_map_[stmt][2], i32 val->cast<MatrixInitStmt>()->val[2]) StoreStmt(i32* scalarized_local_tensor_map_[stmt][3], i32 val->cast<MatrixInitStmt>()->val[3]) ``` Co-authored-by: Yi Xu <xy_xuyi@foxmail.com>
jim19930609
added a commit
that referenced
this issue
Sep 30, 2022
jim19930609
added a commit
that referenced
this issue
Oct 8, 2022
…setStmt (#6189) Related issue = #5873, #5819 This PR is working "Part ④" in #5873. Fused `PtrOffsetStmt` with `base_ptr=ExternalPtrStmt` into a modified `ExternalPtrStmt` to minimized number of instructions for compilation performance purposes. Co-authored-by: Yi Xu <xy_xuyi@foxmail.com> 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
…lattened statements (taichi-dev#6749) Issue: taichi-dev#5819 Overriding the flattened statement `stmt` of an `Expression` can cause conflicts, for example: ``` @ti.kernel def test(): x = ti.Vector([1, 2, 3, 4]) tmp = x + x[0] # implicit broadcast ``` In `x + x[0]`, the `x` on the lhs serves as rvalue whereas the `x` in the `x[0]` serves as a lvalue, so the result of `flatten_rvalue()` and `flatten_lvalue()` will override each other. To avoid such conflicts, this PR refactored the `flatten_values()` functions: 1. Flattened statement `stmt` of an `Expression` will only get modified by `Expression::flatten()`, any other overriding will be forbidden. 2. `flatten_rvalue()` and `flatten_lvalue()` now returns the flattened statement as the result. External users such as `irpass::lower_ast()` will turn to use the returned statement. Co-authored-by: Yi Xu <xy_xuyi@foxmail.com>
quadpixels
pushed a commit
to quadpixels/taichi
that referenced
this issue
May 13, 2023
Issue: taichi-dev#5819 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#5819 ### Brief Summary This PR fixes invalid syntax in the test. 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#5819 ### Brief Summary Quant types are not primitive types, so we should not make assertions that types only contain primitive types and tensor type. We just need to trigger scalarization when all the operands have `TensorType`. Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Zhanlue Yang <zy2284@columbia.edu>
quadpixels
pushed a commit
to quadpixels/taichi
that referenced
this issue
May 13, 2023
Issue: taichi-dev#5819 ### Brief Summary Fixed a set of issues to make BLS tests work. 1. Modified GroupedNDRange generator to directly yield `Expr with TensorType` instead of `_IntermediateMatrix` when `real_matrix=True` 2. Added support for `rescale_index()` to handle `Expr with TensorType` 3. Added scalarization for `indices` of SNode ops Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Yi Xu <xy_xuyi@foxmail.com>
quadpixels
pushed a commit
to quadpixels/taichi
that referenced
this issue
May 13, 2023
… Matrix (taichi-dev#6795) Issue: taichi-dev#5819 ### Brief Summary
quadpixels
pushed a commit
to quadpixels/taichi
that referenced
this issue
May 13, 2023
…dev#6813) Issue: taichi-dev#5819 ### Brief Summary The background is that we would like to clearly distinguish vectors from matrices. After taichi-dev#6528, `transpose()` of a vector makes no sense so we'd better raise an error and guide users towards the current practice (`outer_product()`). 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
… operands' broadcasting (taichi-dev#6805) Issue: taichi-dev#5819 ### Brief Summary
quadpixels
pushed a commit
to quadpixels/taichi
that referenced
this issue
May 13, 2023
…sorTyped operands (taichi-dev#6817) Issue: taichi-dev#5819 ### Brief Summary Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Yi Xu <xy_xuyi@foxmail.com>
quadpixels
pushed a commit
to quadpixels/taichi
that referenced
this issue
May 13, 2023
…chi-dev#6822) Issue: taichi-dev#5819 ### Brief Summary
quadpixels
pushed a commit
to quadpixels/taichi
that referenced
this issue
May 13, 2023
…hi-dev#6839) Issue: taichi-dev#5819 ### Brief Summary Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Yi Xu <xy_xuyi@foxmail.com>
quadpixels
pushed a commit
to quadpixels/taichi
that referenced
this issue
May 13, 2023
…i-dev#6801) Issue: taichi-dev#5819 ### Brief Summary
quadpixels
pushed a commit
to quadpixels/taichi
that referenced
this issue
May 13, 2023
…rue (taichi-dev#6873) Issue: taichi-dev#5819 ### Brief Summary As these two options are enabled by default (taichi-dev#6801), we no longer need separate tests for them. 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#5819 ### Brief Summary We no longer need the switch after taichi-dev#6801. 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#5819 ### Brief Summary Now the `ti.Matrix` class is only for Python-scope matrices. 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#5819 ### Brief Summary 1. `+=` should not be used in (single-thread) matrix lib functions. It is an atomic op and will be demoted very late in the optimization passes, which is harmful to the compilation speed. 2. `__getitem__` should take only one parameter. 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#6928) Issue: taichi-dev#5819 ### Brief Summary Before this PR, matrix type inference directly takes the type of the first element, which is problematic. This PR fixes the inference by calculating the common type of all elements and removes the redundant `_MatrixEntriesInitializer`.
quadpixels
pushed a commit
to quadpixels/taichi
that referenced
this issue
May 13, 2023
…-dev#6932) Issue: taichi-dev#5819 ### Brief Summary These two intermediate classes are unnecessary now, so let's remove them for simplicity. 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#5819 ### Brief Summary There is no need to wrap some methods of `Matrix` into a separate class now. 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
…Frontend IR (taichi-dev#6934) Issue: taichi-dev#5819 ### Brief Summary 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#5819 ### Brief Summary This PR removes `StrideExpression` and `expr_init_local_tensor`, which were for the old dynamic index implementation. `impl.subscript()` is also simplified a bit by the way. 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
…end IR (taichi-dev#6972) Issue: taichi-dev#5819 ### Brief Summary
quadpixels
pushed a commit
to quadpixels/taichi
that referenced
this issue
May 13, 2023
…nsorType (taichi-dev#7043) Issue: taichi-dev#5819 ### Brief Summary
quadpixels
pushed a commit
to quadpixels/taichi
that referenced
this issue
May 13, 2023
…hon to Frontend IR (taichi-dev#6942) Issue: taichi-dev#5819 ### Brief Summary For indices of TensorType, instead of scalarizing them at Python level, it is up to the Frontend IR's consumer to decide whether TensorType'd indices are acceptable and if we should have it scalarized. This PR removes `expand_expr` in Expression subscription and migrate the scalarization logics to the following constructors: 1. MeshIndexConversionExpression::MeshIndexConversionExpression 2. IndexExpression::IndexExpression
quadpixels
pushed a commit
to quadpixels/taichi
that referenced
this issue
May 13, 2023
…o Frontend IR (taichi-dev#6946) Issue: taichi-dev#5819 ### Brief Summary
quadpixels
pushed a commit
to quadpixels/taichi
that referenced
this issue
May 13, 2023
…on code to Frontend IR (taichi-dev#6968) Issue: taichi-dev#5819 ### Brief Summary
quadpixels
pushed a commit
to quadpixels/taichi
that referenced
this issue
May 13, 2023
…n code to Frontend IR (taichi-dev#6980) Issue: taichi-dev#5819 ### Brief Summary
quadpixels
pushed a commit
to quadpixels/taichi
that referenced
this issue
May 13, 2023
…hon code to Frontend IR (taichi-dev#7127) Issue: taichi-dev#5819 ### Brief Summary
quadpixels
pushed a commit
to quadpixels/taichi
that referenced
this issue
May 13, 2023
…ichi-dev#7124) Issue: taichi-dev#5819 ### Brief Summary
quadpixels
pushed a commit
to quadpixels/taichi
that referenced
this issue
May 13, 2023
…7143) Issue: taichi-dev#5819 ### Brief Summary 1. Removed redundant interface `fill_with_scalar()` 2. Refactored `cast()` into internal interface `_instantiate()` 3. MatrixType/VectorType will only return `ti.Matrix` in python_scope, while `Expr(TensorType)` in taichi_scope After this PR, both MatrixType and VectorType should have single public interface `__call__()`
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Background and Purpose
Currently, Taichi scalarizes all the Matrix and corresponding operations at Python level. This approach has major drawbacks as follow:
To address the above mentioned drawbacks, we describe our refactor goal as follow:
Design Overview
In general there are three separate code paths with Matrix involved:
Each of the code path involves four typical use cases:
Design Details
Local Matrix
Implementation plan: #5478
1. Representation
ti.Matrix returns a Expr(MatrixExpression) with TensorType
2. IR changes
[New Frontend IR] MatrixExpression: Initializing a matrix with ti.Matrix will result in the following Frontend IR:
[New CHI IR] MatrixInitStmt: Matrix initialization
[Reuse CHI IR] PtrOffsetStmt: Represent Matrix indexing
3. Codegen (LLVM)
MatrixInitStmt -> llvm::VectorType(Init + InsertElement)
BinaryOpStmt -> Same LLVM Binary Instruction with llvm::VectorType operands
4. Lowering details
MatrixField
AOS Memory Layout
1. AOS Python Interface
Current:
ti.Matrix.field(m=2, n=2, shape=3, ti.AOS)
After SNodeTreeType:
builder.add_field(dtype=ti.vec3, name='x', shape=(4, 8))
2. Representation
3. Lowering details
SOA/Customized Memory Layout
1. SOA Python Interface
Current:
After SNodeTreeType:
2. Representation
3. Lowering details
MatrixNdarray
MatrixNdarray share similar implementations wrt MatrixField. We mainly describe the lowering details in this section.
AOS Memory Layout
Lowering details:
SOA Memory Layout
Lowering details:
Fallback & Scalarization
Roadmap
Proposed Demos
The text was updated successfully, but these errors were encountered: