-
Notifications
You must be signed in to change notification settings - Fork 17
[bench] enable mlir benchmark #106
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
Changes from all commits
0781b4f
93306ef
60807a8
9344308
eb4678f
d82cc81
bda491d
f1a8e10
e50b9fb
7515714
1ecf8a4
45e64c9
50c7737
fe0c118
8e144c9
44619b4
dd98c65
04db05b
4877c06
f6072f0
4dc1aa4
0bdecfe
47b6551
e86afaa
6682794
0c3ff50
7b4c2f9
72983b7
b14398c
d208ec4
fc85574
dee3d54
eb04513
212710b
9d2e336
d9e8009
7e1a4be
eeaceb7
efb6133
20d7dd0
37782b6
fb61105
f0916fc
af1bdbb
52502d2
f7a6e1f
ecf95e8
c3203b3
5b953e5
a58afd2
dfb212c
c3b9008
4746b97
26be584
401975f
abd8ff4
056a0a9
8079132
d504ce6
f6adb8e
47ea2fc
23f2e29
e2c5893
315f48d
4024ece
c33f532
7f7b042
7ff5592
8c3a5fd
7dff6a1
2399d08
4923f17
8c168e7
c47c33d
9da1002
9bdf0b5
2691727
1b8c1c6
8e15377
201f942
a58bd6f
cb66f38
b06cdda
30945d8
a7562d2
2b11201
5f12a72
e06b194
21ea72f
87e85b0
00da905
d47201d
667cd13
f113eff
3623d16
1f3e6e6
e80ac7c
2d8e40d
1f45ec1
defda06
4ff5baa
61fc0d6
5f8ac95
524bd7d
95b3cc8
fd1ad8b
aa69883
27ff72c
0c82ebd
b466415
21bec88
0407f22
271e0c3
3517083
d6fff77
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import os | ||
import sys | ||
|
||
llvm_obj_root = "@LLVM_BINARY_DIR@" | ||
llvm_lib_dir = "@LLVM_LIBRARY_DIR@" | ||
shlib_ext = "@LTDL_SHLIB_EXT@" | ||
|
||
if sys.platform.startswith("win32"): | ||
mlir_runner_utils_dir = os.path.normpath(os.path.join(llvm_obj_root, "bin")) | ||
shlib_prefix = "" | ||
else: | ||
mlir_runner_utils_dir = llvm_lib_dir | ||
shlib_prefix = "lib" | ||
|
||
MLIR_C_RUNNER_UTILS = os.path.normpath( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be better to just register a folder path here, maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed this with similar way in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can set mlir_runner_utils_dir to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried and we can not get the |
||
os.path.join( | ||
mlir_runner_utils_dir, shlib_prefix + "mlir_c_runner_utils" + shlib_ext | ||
) | ||
) | ||
MLIR_RUNNER_UTILS = os.path.normpath( | ||
os.path.join(mlir_runner_utils_dir, shlib_prefix + "mlir_runner_utils" + shlib_ext) | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# ===-- graph_compiler.py - DESC ------------------------------*- Python -*-===# | ||
# | ||
# This file is licensed under the Apache License v2.0 with LLVM Exceptions. | ||
# See https://llvm.org/LICENSE.txt for license information. | ||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
# | ||
# ===-----------------------------------------------------------------------===# | ||
|
||
from gc_mlir import execution_engine | ||
from gc_mlir import ir | ||
from gc_mlir import passmanager | ||
from gc_mlir.config import MLIR_C_RUNNER_UTILS, MLIR_RUNNER_UTILS | ||
|
||
__all__ = [ | ||
"GraphCompiler", | ||
] | ||
|
||
|
||
class GraphCompiler: | ||
def __init__( | ||
self, | ||
pipeline: str = "any(gc-cpu-pipeline)", | ||
opt_level: int = 3, | ||
): | ||
self.shared_libs = [MLIR_C_RUNNER_UTILS, MLIR_RUNNER_UTILS] | ||
self.pipeline = pipeline | ||
self.opt_level = opt_level | ||
|
||
def __call__(self, module: ir.Module, ir_printing: bool = False): | ||
self.compile(module, ir_printing) | ||
|
||
def compile(self, module: ir.Module, ir_printing: bool = False): | ||
pm = passmanager.PassManager.parse(self.pipeline) | ||
if ir_printing: | ||
pm.enable_ir_printing() | ||
pm.run(module.operation) | ||
|
||
def jit(self, module: ir.Module) -> execution_engine.ExecutionEngine: | ||
return execution_engine.ExecutionEngine( | ||
module, opt_level=self.opt_level, shared_libs=self.shared_libs | ||
) | ||
|
||
def compile_and_jit( | ||
self, module: ir.Module, ir_printing: bool = False | ||
) -> execution_engine.ExecutionEngine: | ||
self.compile(module, ir_printing) | ||
return self.jit(module) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# Python Tools | ||
## Pre-requisites | ||
### Enable python binding | ||
* Enable MLIR python binding, [README](https://github.com/intel/graph-compiler/blob/main/python/README.md) | ||
### Set env | ||
* **PYTHONPATH**=*${BUILD_DIR}*/python_packages/gc_mlir_core | ||
* **LD_PRELOAD**=path/to/libiomp5.so | ||
|
||
|
||
## Bench | ||
The tool has two different ways to calculate the time cost, and more experiments are needed to test which one is more stable and accurate. Currently, users can choose which way to use through options | ||
* Use the MLIR Python API to invoke the kernel and use Python to calculate the time cost | ||
* Modify MLIR by wrapping the kernel into a new method and calling the `nanoTime()` method before and after calling the kernel. Finally, calculate the difference as the time cost | ||
``` | ||
func.func private @nanoTime() -> i64 attributes {llvm.emit_c_interface} | ||
func.func public @wrapped_main(%arg0: memref<1xi64>, %arg1: tensor<128x512xbf16>, %arg2: tensor<512x256xbf16>) -> tensor<128x256xbf16> attributes {llvm.emit_c_interface} { | ||
%0 = call @nanoTime() : () -> i64 | ||
%1 = call @main_entry(%arg1, %arg2) : (tensor<128x512xbf16>, tensor<512x256xbf16>) -> tensor<128x256xbf16> | ||
%2 = call @nanoTime() : () -> i64 | ||
%3 = arith.subi %2, %0 : i64 | ||
%c0 = arith.constant 0 : index | ||
memref.store %3, %arg0[%c0] : memref<1xi64> | ||
return %1 : tensor<128x256xbf16> | ||
} | ||
} | ||
``` | ||
|
||
### Examples: | ||
``` | ||
# simple version | ||
python3 ./tools/main.py --driver=load_mlir --path=./tools/workloads/test.mlir | ||
|
||
# complex version | ||
python3 ./tools/main.py --type=bench --bench_kind=py --driver=load_mlir --path=./tools/workloads/test.mlir --warm_up=200 --repeat=200 --print_ir --entry=main_entry | ||
``` | ||
|
||
``` | ||
# result example | ||
===========bench result=========== | ||
{ | ||
"args": { | ||
"type": "bench", | ||
"driver": "load_mlir", | ||
"path": "./tools/workloads/test.mlir", | ||
"entry": "main_entry", | ||
"bench_kind": "py", | ||
"print_ir": false, | ||
"warm_up": 20, | ||
"repeat": 100 | ||
}, | ||
"compile_cost(ms)": 25.58841183781624, | ||
"execute_cost(ms)": 1.7501823976635933 | ||
} | ||
``` | ||
|
||
### Common Options | ||
* `--driver`: the pattern to bench, currently support `mlp` and `load_mlir` | ||
* `--bench_kind`: `py` or `wrapper`, different evaluation implementation of the benchmark | ||
* `--warm_up`: warm-up times of the execution | ||
* `--repeat`: repeat times of the execution | ||
* `--print_ir`: print the ir before execution | ||
* `--disable_results_to_params`: do not use this when using the default pipeline (gc-cpu-pipeline) | ||
|
||
### Driver Specific Options | ||
* load_mlir | ||
* `--path`: the mlir file path | ||
* `--entry`: the name of entry func | ||
``` | ||
python3 ./tools/main.py --driver=load_mlir --path=./tools/workloads/test.mlir | ||
``` | ||
|
||
|
||
* mlp | ||
* `--batch_size`: the input | ||
* `--hidden_size_list`: hidden_sizes of mlp, example: 32x16x64 | ||
* `--has_bias`: if the matmul op has bias, example: 1x0 | ||
* `--act_type`: choices=["noop", "relu", "sigmoid"] | ||
* `--dtype`: choices=["bf16", "f32"] | ||
``` | ||
python3 ./tools/main.py --driver=mlp --batch_size=32 --hidden_size_list=32x16x64 --has_bias=0x0 --act_type=noop --dtype=f32 | ||
|
||
===========bench func name: main_entry =========== | ||
module { | ||
func.func @main_entry(%arg0: tensor<32x32xf32>, %arg1: tensor<32x16xf32>, %arg2: tensor<16x64xf32>) -> tensor<32x64xf32> attributes {llvm.emit_c_interface} { | ||
%0 = tensor.empty() : tensor<32x16xf32> | ||
%1 = linalg.matmul {cast = #linalg.type_fn<cast_signed>} ins(%arg0, %arg1 : tensor<32x32xf32>, tensor<32x16xf32>) outs(%0 : tensor<32x16xf32>) -> tensor<32x16xf32> | ||
%2 = tensor.empty() : tensor<32x64xf32> | ||
%3 = linalg.matmul {cast = #linalg.type_fn<cast_signed>} ins(%1, %arg2 : tensor<32x16xf32>, tensor<16x64xf32>) outs(%2 : tensor<32x64xf32>) -> tensor<32x64xf32> | ||
return %3 : tensor<32x64xf32> | ||
} | ||
} | ||
``` |
Uh oh!
There was an error while loading. Please reload this page.