Execution
Code execution and caching module for MLIR benchmarks.
This module handles the execution of transformed MLIR code, including bufferization, lowering, and performance measurement. It manages an execution cache to avoid redundant computations and interfaces with the MLIR execution engine to measure actual execution times.
OutputsStructure
Bases: Protocol
Placeholder for structure used as output of MLIR execution.
Note
Used for type hinting only. The actual structure is defined inside create_params().
Attributes:
| Name | Type | Description |
|---|---|---|
delta |
int
|
Execution time in nanoseconds. |
get_results()
Execution(exec_data_file=None, main_exec_data=None)
Class that deals with code execution and cache management
Attributes:
| Name | Type | Description |
|---|---|---|
exec_data_file |
str
|
Path to the local file where exec data is cached |
main_exec_data |
dict[str, dict[str, int]] | None
|
External exec data that was read at the beginning of training |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exec_data_file
|
str | None
|
Path to the local file where exec data is cached |
None
|
main_exec_data
|
dict[str, dict[str, int]] | None
|
External exec data that was read at the beginning of training |
None
|
Source code in mlir_rl_artifact/execution.py
execute_code(module, bench_name, seq)
Executes the given MLIR module and measures execution time.
Checks the execution cache first for code matching this sequence. If not found, applies bufferization and lowering transforms before executing the code.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module
|
Module
|
The MLIR module to execute. |
required |
bench_name
|
str
|
The benchmark name for cache management. |
required |
seq
|
list[list[Action]]
|
The sequence of transformations applied to reach this code. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Execution time in nanoseconds. |
bool
|
Boolean indicating if execution succeeded. |
bool
|
Boolean indicating if this is a cache miss (True if executed, False if cached). |
Source code in mlir_rl_artifact/execution.py
update_execution_cache(new_data)
Update the temp execution cache with the new data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_data
|
dict[str, dict[str, int]]
|
The new data to update. |
required |
Source code in mlir_rl_artifact/execution.py
get_code_cache_key(seq)
Get the code cache key for the given operation state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seq
|
list[list[Action]]
|
The sequence of transformations applied to reach this code. |
required |
Returns:
| Type | Description |
|---|---|
str
|
the code cache key. |
Source code in mlir_rl_artifact/execution.py
__execute_bufferized_code(module)
Lowers and runs the given MLIR code using Python bindings, then returns the execution time and assertion result (if the executed code returns the correct result).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module
|
Module
|
The MLIR module to execute. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The execution time in nanoseconds. |
bool
|
The assertion result. |
Source code in mlir_rl_artifact/execution.py
__check_execution_cache(bench_name, cache_key)
Check the execution cache for the given operation state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bench_name
|
str
|
The benchmark name to check. |
required |
cache_key
|
str
|
The cache key to check. |
required |
Returns:
| Type | Description |
|---|---|
int | None
|
the execution time in nanoseconds if the operation is found in the cache, otherwise None. |
Source code in mlir_rl_artifact/execution.py
__create_params(module)
staticmethod
Creates the input and output parameters for the given MLIR module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module
|
Module
|
The MLIR module to create the parameters for. |
required |
Returns:
| Type | Description |
|---|---|
list[ndarray]
|
The list of inputs as numpy arrays |
OutputsStructure
|
The outputs structure (output arrays + delta) |
Source code in mlir_rl_artifact/execution.py
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | |
__convert_to_args(inputs, outputs_structure)
staticmethod
Converts input arrays and output structure into ctypes arguments for MLIR execution.
Prepares arguments in the format required by the MLIR execution engine. Each argument is a double pointer (pointer to pointer) to allow proper handling in the C calling convention.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inputs
|
list[ndarray]
|
List of input numpy arrays to be passed to the MLIR kernel. |
required |
outputs_structure
|
OutputsStructure
|
ctypes Structure containing output memref descriptors and execution time. |
required |
Returns:
| Type | Description |
|---|---|
list
|
List of double pointers to ctypes Structures suitable for passing to ExecutionEngine.invoke(). |
Source code in mlir_rl_artifact/execution.py
free_pointer(ptr)
staticmethod
Free the memory pointed to by the given pointer using the C standard library.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ptr
|
c_void_p
|
The pointer to free. |
required |