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

[Examples] Provide end2end examples using byteir ( fronts compiler and runtime ) ,for example resnet18 #223

Closed
lyffly opened this issue Apr 28, 2024 · 6 comments
Labels
documentation Improvements or additions to documentation

Comments

@lyffly
Copy link

lyffly commented Apr 28, 2024

hello , can you provide end2end examples using byteir ( fronts compiler and runtime ) ,for example resnet18

@qingyunqu
Copy link
Collaborator

Hi, you could try like https://github.com/bytedance/byteir/tree/main/frontends/torch-frontend/examples/demo. But the demo seems to be a little outdated. If you meet any problem when you try the demo, feel free to ask me.

@qingyunqu qingyunqu added the documentation Improvements or additions to documentation label Apr 30, 2024
@recursionstudio
Copy link

Hi, you could try like https://github.com/bytedance/byteir/tree/main/frontends/torch-frontend/examples/demo. But the demo seems to be a little outdated. If you meet any problem when you try the demo, feel free to ask me.

Hi I run gpt2 under demo recently and got following errors:
/host/byteir/frontends/torch-frontend/examples/demo# python3 main.py --infer gpt2

python3: /host/byteir/frontends/torch-frontend/third_party/torch-mlir/lib/Dialect/Torch/IR/TorchDialect.cpp:176: virtual mlir::Operation* mlir::torch::Torch::Torch
Dialect::materializeConstant(mlir::OpBuilder&, mlir::Attribute, mlir::Type, mlir::Location): Assertion `isa(type) && "should be a vtensor type!"'
failed.
Aborted (core dumped)

/host/byteir/frontends/torch-frontend/examples/demo# python3 main.py gpt2

File "/host/byteir/frontends/torch-frontend/examples/demo/backend.py", line 205, in byteir_runner
byteir.compile(mlir_file_name, output_mlir_file_name, entry_func='forward', target='cuda')
File "/root/anaconda3/lib/python3.9/site-packages/byteir/compile.py", line 223, in compile
module = ir.Module.parse(open(input_file_path, "r").read(), context)
byteir._mlir_libs._site_initialize..MLIRError: Unable to parse module assembly:
error: "-":134:12: 'stablehlo.gather' op attribute 'slice_sizes' failed to satisfy constraint: 64-bit signless integer elements attribute
note: "-":134:12: see current operation: %131 = "stablehlo.gather"(%arg111, %78) {dimension_numbers = #stablehlo.gather<offset_dims = [2], collapsed_slice_dims =
[0], start_index_map = [0], index_vector_dim = 2>, indices_are_sorted = false, slice_sizes = array<i64: 1, 768>} : (tensor<50257x768xf32>, tensor<8x1024xi64>) -> t
ensor<8x1024x768xf32>

Could you take a look? My commit is f734a1f

@lyffly
Copy link
Author

lyffly commented May 10, 2024

1、use https://github.com/bytedance/byteir/blob/main/frontends/torch-frontend/examples/inference/infer_resnet.py generate resnet.stablehlo.mlir
2、then compile to cuda

from byteir import compile
compile("./resnet.stablehlo.mlir", "./out.mlir", target="cuda", verbose=True)

error shows :

compile("./resnet18.stablehlo.mlir", "./resnet18.cuda.mlir", target="cuda", verbose=True)
  File "/usr/local/lib/python3.8/dist-packages/byteir/compile.py", line 212, in compile
    module = ir.Module.parse(open(input_file_path, "r").read(), context)
byteir._mlir_libs.MLIRError: Unable to parse module assembly:
error: "-":113:12: 'stablehlo.reduce_window' op attribute 'window_dimensions' failed to satisfy constraint: 64-bit signless integer elements attribute
 note: "-":113:12: see current operation: 
  %110 = "stablehlo.reduce_window"(%109, %101) ({
  ^bb0(%arg1: tensor<f32>, %arg2: tensor<f32>):
    %183 = "stablehlo.maximum"(%arg1, %arg2) : (tensor<f32>, tensor<f32>) -> tensor<f32>
    "stablehlo.return"(%183) : (tensor<f32>) -> ()
  }) {padding = dense<[[0, 0], [0, 0], [1, 1], [1, 1]]> : tensor<4x2xi64>, window_dilations = array<i64: 1, 1, 1, 1>, window_dimensions = array<i64: 1, 1, 3, 3>, window_strides = array<i64: 1, 1, 2, 2>} : (tensor<2x64x112x112xf32>, tensor<f32>) -> tensor<2x64x56x56xf32>

@qingyunqu
Copy link
Collaborator

qingyunqu commented May 11, 2024

1、use https://github.com/bytedance/byteir/blob/main/frontends/torch-frontend/examples/inference/infer_resnet.py generate resnet.stablehlo.mlir 2、then compile to cuda

from byteir import compile
compile("./resnet.stablehlo.mlir", "./out.mlir", target="cuda", verbose=True)

error shows :

compile("./resnet18.stablehlo.mlir", "./resnet18.cuda.mlir", target="cuda", verbose=True)
  File "/usr/local/lib/python3.8/dist-packages/byteir/compile.py", line 212, in compile
    module = ir.Module.parse(open(input_file_path, "r").read(), context)
byteir._mlir_libs.MLIRError: Unable to parse module assembly:
error: "-":113:12: 'stablehlo.reduce_window' op attribute 'window_dimensions' failed to satisfy constraint: 64-bit signless integer elements attribute
 note: "-":113:12: see current operation: 
  %110 = "stablehlo.reduce_window"(%109, %101) ({
  ^bb0(%arg1: tensor<f32>, %arg2: tensor<f32>):
    %183 = "stablehlo.maximum"(%arg1, %arg2) : (tensor<f32>, tensor<f32>) -> tensor<f32>
    "stablehlo.return"(%183) : (tensor<f32>) -> ()
  }) {padding = dense<[[0, 0], [0, 0], [1, 1], [1, 1]]> : tensor<4x2xi64>, window_dilations = array<i64: 1, 1, 1, 1>, window_dimensions = array<i64: 1, 1, 3, 3>, window_strides = array<i64: 1, 1, 2, 2>} : (tensor<2x64x112x112xf32>, tensor<f32>) -> tensor<2x64x56x56xf32>

In this case, the root cause is that torch-frontend has different stablehlo version with byteir. You could use stablehlo's bytecode to fill the gap. Here is example code:

import byteir
from byteir._mlir_libs._stablehlo import get_current_version
import torch_frontend

module_bytes = torch_frontend.compile(Resnet18(), sample_inputs, "stablehlo+"+get_current_version())
with open("./resnet.mlirbc", "wb") as f:
  f.write(module_bytes)

byteir.compile("./resnet.mlirbc", "./out.mlir", entry_func="forward", target="cuda", verbose=True)

@lyffly
Copy link
Author

lyffly commented May 13, 2024

1、use https://github.com/bytedance/byteir/blob/main/frontends/torch-frontend/examples/inference/infer_resnet.py generate resnet.stablehlo.mlir 2、then compile to cuda

from byteir import compile
compile("./resnet.stablehlo.mlir", "./out.mlir", target="cuda", verbose=True)

error shows :

compile("./resnet18.stablehlo.mlir", "./resnet18.cuda.mlir", target="cuda", verbose=True)
  File "/usr/local/lib/python3.8/dist-packages/byteir/compile.py", line 212, in compile
    module = ir.Module.parse(open(input_file_path, "r").read(), context)
byteir._mlir_libs.MLIRError: Unable to parse module assembly:
error: "-":113:12: 'stablehlo.reduce_window' op attribute 'window_dimensions' failed to satisfy constraint: 64-bit signless integer elements attribute
 note: "-":113:12: see current operation: 
  %110 = "stablehlo.reduce_window"(%109, %101) ({
  ^bb0(%arg1: tensor<f32>, %arg2: tensor<f32>):
    %183 = "stablehlo.maximum"(%arg1, %arg2) : (tensor<f32>, tensor<f32>) -> tensor<f32>
    "stablehlo.return"(%183) : (tensor<f32>) -> ()
  }) {padding = dense<[[0, 0], [0, 0], [1, 1], [1, 1]]> : tensor<4x2xi64>, window_dilations = array<i64: 1, 1, 1, 1>, window_dimensions = array<i64: 1, 1, 3, 3>, window_strides = array<i64: 1, 1, 2, 2>} : (tensor<2x64x112x112xf32>, tensor<f32>) -> tensor<2x64x56x56xf32>

In this case, the root cause is that torch-frontend has different stablehlo version with byteir. You could use stablehlo's bytecode to fill the gap. Here is example code:

import byteir
from byteir._mlir_libs._stablehlo import get_current_version
import torch_frontend

module_bytes = torch_frontend.compile(Resnet18(), sample_inputs, "stablehlo+"+get_current_version())
with open("./resnet.mlirbc", "wb") as f:
  f.write(module_bytes)

byteir.compile("./resnet.mlirbc", "./out.mlir", entry_func="forward", target="cuda", verbose=True)

Get it. Thanks .

@lyffly
Copy link
Author

lyffly commented May 13, 2024

Close this issue.

@lyffly lyffly closed this as completed May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

3 participants