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

dialects: (varith) Add varith (variadic arithmetic) dialect #3241

Merged
merged 1 commit into from
Oct 4, 2024

Conversation

AntonLydike
Copy link
Collaborator

Add a variadic arithmetic dialect.

The goal is to provide a place for nice-to-use arithmetic operations. Also makes re-writing arithmetic easier in some cases, e.g. when you want to split a summation over one set of values into two summations by selecting certain values from the set.

Coming next is two rewrites to canonicalize arith to varith, and then to de-canonicalize back.

@AntonLydike AntonLydike added dialects Changes on the dialects hackathon To be tackled at the hackathon labels Oct 3, 2024
@AntonLydike AntonLydike self-assigned this Oct 3, 2024
@AntonLydike AntonLydike force-pushed the anton/varith branch 3 times, most recently from 8aff34a to 1835d9d Compare October 3, 2024 15:42
Copy link

codecov bot commented Oct 3, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.92%. Comparing base (4e3e7a9) to head (25e65c3).
Report is 3 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3241   +/-   ##
=======================================
  Coverage   89.91%   89.92%           
=======================================
  Files         440      441    +1     
  Lines       55392    55417   +25     
  Branches     8641     8643    +2     
=======================================
+ Hits        49808    49832   +24     
- Misses       4157     4158    +1     
  Partials     1427     1427           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@superlopuh
Copy link
Member

Interesting! What's the advantage of this? Smaller IR? I'm a little worried that it might be more difficult to reason about the individual arithmetic operations that would make up a variadic one

@n-io
Copy link
Collaborator

n-io commented Oct 3, 2024

Interesting! What's the advantage of this? Smaller IR? I'm a little worried that it might be more difficult to reason about the individual arithmetic operations that would make up a variadic one

Imo it looks quite useful for restructuring larger computations.

As for specifying what arithmetic operation made up the variadic one, linalg.reduce solves this by having a region:

  %reduce = linalg.reduce
      ins(%input:tensor<16x32x64xf32>)
      outs(%init:tensor<16x64xf32>)
      dimensions = [1]
      (%in: f32, %out: f32) {
        %0 = arith.addf %out, %in: f32
        linalg.yield %0: f32
      }

Though having separate ops seems quite straightforward actually.

@AntonLydike
Copy link
Collaborator Author

Interesting! What's the advantage of this? Smaller IR? I'm a little worried that it might be more difficult to reason about the individual arithmetic operations that would make up a variadic one

In our usecase, we have a giant sum that we need to split into two. This is much harder if you have a taller def-use tree vs a shorter one:

arith:
%a  %b
 \ /  
  +--- %c
  |
  +--- %d
  |
  +--- %c
  .
  .
  .
  %res


varith:

%a %b %c &d ... %z
 |  |  |  |     |
 ----------------
        |
        %res

This allows us to basically "filter" the varith.add operands to either be "dirty" or "clean", and move them around accordingly, instead of having a weird tree-like graph.

@superlopuh
Copy link
Member

Interesting! I'm curious to see where this goes

"varith",
[
VarithAddOp,
VarithMulOp,
Copy link
Collaborator

Choose a reason for hiding this comment

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

How about VarithSumOp and VarithProductOp?

@AntonLydike AntonLydike merged commit 104245e into main Oct 4, 2024
14 checks passed
@AntonLydike AntonLydike deleted the anton/varith branch October 4, 2024 13:11
emmau678 pushed a commit that referenced this pull request Oct 8, 2024
Add a variadic arithmetic dialect.

The goal is to provide a place for nice-to-use arithmetic operations.
Also makes re-writing arithmetic easier in some cases, e.g. when you
want to split a summation over one set of values into two summations by
selecting certain values from the set.

Coming next is two rewrites to canonicalize arith to varith, and then to
de-canonicalize back.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dialects Changes on the dialects hackathon To be tackled at the hackathon
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants