mahesh ravishankar
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    3
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Named ops refresh The primary aim of this document is to collaborate on getting to a better state with respect to Linalg named operations. As of writing of this document we have 28 different Convolution operations, 17 different Pooling operations, 14 different Matmul-like operations for a total of 93 different Linalg operations. The list keeps growing. The reason for this increase is that the OpDSL based Linalg named ops is positioned to be a common target for "front-end" dialects like Torch-MLIR, TOSA, StableHLO, etc. The primary reason for explosion in the named ops (refered to as OpDSL-specified named ops) is that these ops are operations that implement the [LinalgStructuredInterface](https://github.com/llvm/llvm-project/blob/de2fad32513f7420988df1cf99aff90e0a067469/mlir/include/mlir/Dialect/Linalg/IR/LinalgInterfaces.td#L226) and have 1. Fixed iterator types 1. Fixed Indexing maps 1. Fixed region. Consequently, for each of the variation of the input operand "layout" a new operation is added. A further limitation is that the named operations cannot easily fold in broadcasting behaviour. For example consider ```mlir %lhs = linalg.broadcast ins(%input : tensor<?x?xf32>) outs(%empty : tensor<?x?x?xf32>) dimensions = [0] %bmm = linalg.batch_matmul ins(%lhs, %rhs : tensor<?x?x?xf32>, tensor<?x?x?xf32>) outs(%init : tensor<?x?x?xf32>) -> tensor<?x?x?xf32> ``` while this could be represented more succinctly as ```mlir %lhs = linalg.generic { indexing_maps = [affine_map<(d0, d1, d2, d3) -> (d1, d3)>, affine_map<(d0, d1, d2, d3) -> (d0, d3, d1)>, affine_map<(d0, d1, d2, d3) -> (d0, d1, d2)>], iterator_types = ["parallel", "parallel", "parallel", "reduction"]} ins(%input, %rhs : tensor<?x?xf32>, tensor<?x?x?xf32>) outs(%init : tensor<?x?x?xf32>) -> tensor<?x?x?xf32> { ... } ``` As a result front-end dialects when targeting `batch_matmul` end up introducing broadcasts, which pessimizes the performance unless the rest of the compilation stack folds it away. The only way currently this is feasible is to generalize named ops and fuse the broadcast operation with the generalized `linalg.batch_matmul` to end up with the `linalg.generic` shown above. This isnt ideal either cause now the semantic information about the region computation carried by the name of the operation is also lost. A further limitation of the current implementation of named ops is that the named ops carry a hidden region. This region represents the computation within the inner most loop when the op is lowered to loops. The presence of this hidden region makes it impossible to use PDL/PDLL to match named ops (unless those can be extended to handle operations with regions). This region is not serving any purpose apart from making the implementation of the generalization to `linalg.generic` operation easier. ## Solution ## One way out of these issues is to have a named op defined in TableGen where unlike the OpDSL specified named ops these operations have only a fixed region, i.e. the operation contain indexing maps for each operand, and the iterator types attribute list explicitly. The "name" of the operation therefore just represents the region. The region is not explicitly defined, but the operation carries enough information required to generate the region if needed (say during generalization). So the above `broadcast` + `batch_matmul` could be represented by a new TableGen defined named operation ``` %contraction = linalg.contraction { indexing_maps = [affine_map<(d0, d1, d2, d3) -> (d1, d3)>, affine_map<(d0, d1, d2, d3) -> (d0, d3, d1)>, affine_map<(d0, d1, d2, d3) -> (d0, d1, d2)>], iterator_types = ["parallel", "parallel", "parallel", "reduction"]} ins(%input, %rhs : tensor<?x?xf32>, tensor<?x?x?xf32>) outs(%init : tensor<?x?x?xf32>) -> tensor<?x?x?xf32> ``` One piece of information that these operations will need to carry that isnt there in current OpDSL based named ops or `LinalgStructuredOps` is a list of attributes that can cast from input operand element type to output operand element type. A list of enums can be used to codify the cast semantics. For example, there are two forms of casts supported today that could be represented by two enums. ```C++ enum LinalgCastEnum : int32_t { CAST_SIGNED = 0, CAST_UNSIGNED = 1, }; ``` So a contraction with mixed types would be ``` %contraction = linalg.contraction { indexing_maps = [affine_map<(d0, d1, d2, d3) -> (d1, d3)>, affine_map<(d0, d1, d2, d3) -> (d0, d3, d1)>, affine_map<(d0, d1, d2, d3) -> (d0, d1, d2)>], iterator_types = ["parallel", "parallel", "parallel", "reduction"], casting_fn = ["CAST_UNSIGNED", "CAST_UNSIGNED"]} ins(%input, %rhs : tensor<?x?xi8>, tensor<?x?x?xi8>) outs(%init : tensor<?x?x?xi32>) -> tensor<?x?x?xi32> ``` All the 14 Matrix-multiply-like operations can be lowered to a single `linalg.contraction` ## Convolutions ## The explosion of operations is particularly egriguous for convolutions ops. These are the current convolution operations defined using OpDSL ```mlir= Conv1DNcwFcwOp Conv1DNwcWcfOp Conv1DOp Conv2DNchwFchwOp Conv2DNchwFchwQOp Conv2DNgchwFgchwOp Conv2DNgchwGfchwOp Conv2DNgchwGfchwQOp Conv2DNhwcFhwcOp Conv2DNhwcFhwcQOp Conv2DNhwcHwcfOp Conv2DNhwcHwcfQOp Conv2DOp Conv3DNcdhwFcdhwOp Conv3DNdhwcDhwcfOp Conv3DNdhwcDhwcfQOp Conv3DOp DepthwiseConv1DNcwCwOp DepthwiseConv1DNwcWcOp DepthwiseConv1DNwcWcmOp DepthwiseConv2DNchwChwOp DepthwiseConv2DNhwcHwcOp DepthwiseConv2DNhwcHwcQOp DepthwiseConv2DNhwcHwcmOp DepthwiseConv2DNhwcHwcmQOp DepthwiseConv3DNcdhwCdhwOp DepthwiseConv3DNdhwcDhwcOp DepthwiseConv3DNdhwcDhwcmOp ``` All of these operations can be represented using a single convolution operation ``` %convolution = linalg.convolution { indexing_maps = [...], iterator_types = [...], casting_fn = [...]} ins(%image, %filter : ...) outs(%init : ...) -> ... ``` An analysis of the indexing maps is enough to distinguish between the different dimensions. 1. N: Batch dimension. Infered as the dimension(s) that is - used as an `AffineDimExpr` in the indexing map for `%image` - used as an `AffineDimExpr` in the indexing map for `%init` - not used in the indexing map for `%filter` 1. D, H, W : Output image dimensions. Infered as the dimension(s) - used in the expression of form `a * b + c * d` in the indexing map for `%image` - not used in the indexing map for the `%filter` - used as an `AffineDimExpr` in the indexing map for `%init` 1. C: Input channel dimension. Infered as the dimension(s) - used as an `AffineDimExpr` in the indexing map for `%image` - used as an `AffineDimExpr` in the indexing map for `%filter` - not used in the indexing map for the `%init` 1. F: Output channel dimension. Infered as the dimension(s) - not used in the indexing map for the `%image` - used as an `AffineDimExpr` in the indexing map for `%image` - used as an `AffineDimExpr` in the indexing map for `%init` 1. G: Group dimension. Infered as the dimension(s) - used as an `AffineDimExpr` in the indexing map for `%image` - used as an `AffineDimExpr` in the indexing map for `%filter` - used as an `AffineDimExpr` in the indexing map for `%init` A depthwise convolution is a grouped convolution with missing `C` based on the taxonomy above (and the `G` dimension is refered to as the channel dimension of input, filter and output). The approach described here folds all the convolutions ops into a single operation. A similar approach can be used to fold all the pooling ops defined using OpDSL into a single operation. So the 93 OpDSL based named ops can be represented using a handful (less than 10) TableGen-based named ops. ## OpDSL Named ops vs (new) TableGen defined named ops. The OpDSL named ops and TableGen-based named ops defined here are intended for different purposes. The former are more "upward" facing, i.e. interfacing with front-end dialects. The TableGen-based named ops proposed here are a more restricted set of operations that are meant for transformations within the compiler. They allow for optimizations like folding transposes/broadcasts into the operation without having to generalize the operations. As such they serve slightly complementary purpose. It would be worth considering keeping the current OpDSL based named ops as "a layer above" the new TableGen-based named ops. Lowering from the OpDSL based named ops to these TableGen-based named ops is a strict lowering. In essense the TableGen-based named ops are creating new operations that satisfy the [ContractionOpInterface](https://github.com/llvm/llvm-project/blob/82d5dd28b4de7245088f7ed40da37f8cf80461e4/mlir/include/mlir/Dialect/Linalg/IR/LinalgInterfaces.td#L21) or [ConvolutionOpInterface](https://github.com/llvm/llvm-project/blob/82d5dd28b4de7245088f7ed40da37f8cf80461e4/mlir/include/mlir/Dialect/Linalg/IR/LinalgInterfaces.td#L136). ### Using PDL/PDLL for matching to libraries ### A common use of Linalg named ops is to match a DAG of operations to a library call. Today, the community at large is relying on OpDSL named ops to match a particular sequence of operations. But the same taxonomy can be obtained by specifying the indexing maps that represent the different convolution operations (essentially the inverse of the above taxonomy), and such a match could be done using PDL/PDLL which is a more efficient way of doing such matches + offloading.

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully