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

Support MLIR (first e2e) #1088

Closed
wants to merge 13 commits into from
Closed

Support MLIR (first e2e) #1088

wants to merge 13 commits into from

Conversation

tucan9389
Copy link

@tucan9389 tucan9389 commented Apr 29, 2023

#1044

Please correct the table if there is wrong

mlir.js MLIR concept Note
mlir.Graph Region or Function Graph or Model
mlir.Parameter Value (Operation's Operand or Result) Edge
mlir.Argument Value (Operation's Operand or Result) Edge
mlir.Node Operation Node
mlir.Attributes Operation's Attribute Node's metadata
mlir.Tensor Constant Operation Kind of node
mlir.TensorType Edge's metadata
mlir.TensorShape Edge's metadata

image

@lutzroeder
Copy link
Owner

@tucan9389 let's review and merge this once the scenario works E2E.

@tucan9389 tucan9389 mentioned this pull request Apr 29, 2023
14 tasks
@tucan9389 tucan9389 changed the title Add empty data structure for MLIR graph Support MLIR (first e2e) Apr 30, 2023
@tucan9389
Copy link
Author

tucan9389 commented May 1, 2023

@lutzroeder cc. @chococigar

Thanks for waiting. I made the first e2e code. Please review and find some missing parts.

stablehlo_sample.mlir issue_1043.mlir
Screenshot 2023-05-01 at 2 22 50 PM Screenshot 2023-05-01 at 2 22 30 PM

How it work

  1. Parsing the .mlir text file with the existing decoder and mlir.Tokenizer and get the parsed js object(ex1, ex2)
  2. With the parsed object, create the mlir.Model (inside of mlir.Model, the constructor makes mlir.Gragh, mlir.Node, mlir.Parameter, mlir.Attribute, etc.)

Note

Done

  • Implement the initial version of mlir.Parser and mlir.Tokenizer
  • Can parse the module, function, operation, attribute(arg, function, operation) of MLIR
  • Can parse the tensor<...> type into mlir.TensorType
  • Implement the constructors(mlir.Graph, mlir.Node, mlir.TensorType, mlir.TensorShape, etc.) for View level
  • Type parsing: I'm not sure this will work with primitive types, like float32, int8
  • Support module as optional: currently, the .mlir file should have the module { ... }

TODO

  • Cannot parse the basic block(^bb). Tokenizer will just skip it.
  • There are a lot of constant expressions in MLIR, so we need to think about it more.
Detail cases of constant expression dialect by dialect
// syntax group 1: the case containing the value in the constant operation itself
// Standard Dialect (std)
%std_const_i32 = constant 42 : i32
%std_const_f32 = constant 3.14 : f32
%std_const_vec = constant dense<[1, 2, 3, 4]> : vector<4xi32>

// Affine Dialect (affine)
%affine_const_i32 = affine.constant 42 : i32
%affine_const_i64 = affine.constant 42 : i64

// SPIR-V Dialect (spirv)
%spirv_const_i32 = spirv.constant 42 : i32
%spirv_const_f32 = spirv.constant 3.14 : f32

// GPU Dialect (gpu)
%gpu_const_i32 = gpu.constant 42 : i32
%gpu_const_f32 = gpu.constant 3.14 : f32

// syntax group 2: the case specifying the value by attribute
// TensorFlow Dialect (tf)
%tf_const_i32 = "tf.Const"() {value = dense<42> : tensor<i32>} : () -> tensor<i32>
%tf_const_f32 = "tf.Const"() {value = dense<3.14> : tensor<f32>} : () -> tensor<f32>
%tf_const_vec = "tf.Const"() {value = dense<[1, 2, 3, 4]> : tensor<4xi32>} : () -> tensor<4xi32>

// TOSA Dialect (tosa)
%tosa_const_f32 = "tosa.const"() {value = dense<3.14> : tensor<f32>} : () -> tensor<f32>
%tosa_const_vec = "tosa.const"() {value = dense<[1, 2, 3, 4]> : tensor<4xi32>} : () -> tensor<4xi32>

// ONNX Dialect (onnx)
%onnx_const_i32 = "onnx.Constant"() {value = dense<42> : tensor<i32>} : () -> tensor<i32>
%onnx_const_vec = "onnx.Constant"() {value = dense<[1, 2, 3, 4]> : tensor<4xi32>} : () -> tensor<4xi32>

// XLA HLO Dialect (mhlo)
%mhlo_const_i32 = "mhlo.const"() {value = dense<42> : tensor<i32>} : () -> tensor<i32>
%mhlo_const_f32 = "mhlo.const"() {value = dense<3.14> : tensor<f32>} : () -> tensor<f32>
%mhlo_const_vec = "mhlo.const"() {value = dense<[1, 2, 3, 4]> : tensor<4xi32>} : () -> tensor<4xi32>

// syntax group 3: the case specifying with function call format
%llvm_const_i32 = llvm.mlir.constant(42 : i32) : i32
%llvm_const_f32 = llvm.mlir.constant(3.14 : f32) : f32
%llvm_const_vec = llvm.mlir.constant(dense<[1, 2, 3, 4]> : vector<4xi32>) : !llvm<"<4 x i32>">
  • Parse the value of attribute: currently, the attribute's value is all parsed string. We can parse it correctly.

Please comment if you have any questions or feedback.
Thanks.

@tucan9389 tucan9389 mentioned this pull request May 4, 2023
@lutzroeder lutzroeder force-pushed the main branch 2 times, most recently from 998aad7 to b92461c Compare May 6, 2023 02:04
@tucan9389 tucan9389 force-pushed the main branch 2 times, most recently from d9f2932 to 7fac327 Compare May 7, 2023 10:04
lutzroeder added a commit that referenced this pull request May 7, 2023
@lutzroeder lutzroeder closed this May 7, 2023
lutzroeder added a commit that referenced this pull request May 7, 2023
lutzroeder added a commit that referenced this pull request May 7, 2023
lutzroeder added a commit that referenced this pull request May 8, 2023
lutzroeder added a commit that referenced this pull request May 8, 2023
lutzroeder added a commit that referenced this pull request May 8, 2023
lutzroeder added a commit that referenced this pull request May 8, 2023
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