Skip to content
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

[Lang] MatrixType refactor: Support matrix slice #6430

Merged
merged 6 commits into from
Oct 26, 2022

Conversation

strongoier
Copy link
Contributor

Issue: #5819

Brief Summary

A slice actually corresponds to several indices. Let's take a look at the following example:

import taichi as ti

ti.init(real_matrix=True, real_matrix_scalarize=True, print_ir=True)

@ti.kernel
def foo():
    c = ti.Vector([1, 2, 3, 4, 5])
    print(c[:4:2])

foo()

c[:4:2] is a "partially selected vector with length 2" which contains references to c[0] and c[2]. Let's represent it with c[(0), (2), shape=2].

This PR does the following:

  • expands slices into groups of indices in Python;
  • extends IndexExpression to accept a group of indices;
  • creates MatrixOfMatrixPtrStmt to represent a "partially selected matrix" in CHI IR;
  • eliminates MatrixOfMatrixPtrStmt in the lower_matrix_ptr() pass.

Frontend IR and CHI IR of the above example:

[I 10/25/22 19:54:51.885 816603] [compile_to_offloads.cpp:operator()@22] [foo_c80_0] Initial IR:
kernel {
  $0 = alloca @tmp0
  @tmp0 = [1, 2, 3, 4, 5] (dt=[Tensor (5) i32])
  $2 = alloca @tmp1
  @tmp1 = @tmp0
  print @tmp1[(0), (2), shape=(2)], "\n"
}
[I 10/25/22 19:54:51.885 816603] [compile_to_offloads.cpp:operator()@22] [foo_c80_0] Lowered:
kernel {
  <[Tensor (5) i32]> $0 = alloca
  <i32> $1 = const 1
  <i32> $2 = const 2
  <i32> $3 = const 3
  <i32> $4 = const 4
  <i32> $5 = const 5
  <[Tensor (5) i32]> $6 = [$1, $2, $3, $4, $5]
  $7 : local store [$0 <- $6]
  <[Tensor (5) i32]> $8 = alloca
  $9 = local load [$0]
  $10 : local store [$8 <- $9]
  <i32> $11 = const 0
  <*i32> $12 = shift ptr [$8 + $11]
  <i32> $13 = const 2
  <*i32> $14 = shift ptr [$8 + $13]
  <[Tensor (2) i32]> $15 = matrix of matrix ptr [$12, $14]
  $16 = local load [$15]
  print $16, "\n"
}

Some tests of matrix slice haven't been enabled yet because they require x.transpose(), which depends on #6425.

@netlify
Copy link

netlify bot commented Oct 25, 2022

Deploy Preview for docsite-preview ready!

Name Link
🔨 Latest commit ebbb43c
🔍 Latest deploy log https://app.netlify.com/sites/docsite-preview/deploys/6358c694f0b6cc000a553a07
😎 Deploy Preview https://deploy-preview-6430--docsite-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

taichi/ir/statements.h Outdated Show resolved Hide resolved
Copy link
Contributor

@jim19930609 jim19930609 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@strongoier strongoier merged commit c273503 into taichi-dev:master Oct 26, 2022
@strongoier strongoier deleted the mat-16 branch October 26, 2022 06:42
strongoier added a commit that referenced this pull request Oct 31, 2022
)

Issue: #5819

### Brief Summary

This is a follow-up PR of #6430.

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants