Skip to content

feat: add a macro to directly visualize the generated mlir #1246

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/src/api/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ within_compile
@code_hlo
@code_mhlo
@code_xla
@mlir_visualize
```

## Profile XLA
Expand Down
45 changes: 45 additions & 0 deletions src/Compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2068,6 +2068,51 @@ macro jit(args...)
#! format: on
end

"""
@mlir_visualize [optimize = ...] [no_nan = <true/false>] f(args...)

Runs `@code_hlo` and visualizes the MLIR module using `model-explorer`. This expects the
`model-explorer` executable to be in your `PATH`. Installation instructions can be found
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this feels worthwhile just making into a (separate) jll of our own imo, rather than relying on path

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, though it seems to be a JS framework, not sure how to ship the binary via yggy. @giordano might know?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like https://github.com/JuliaPackaging/Yggdrasil/blob/179e92aaf902a50c7876b0d088b3de88091be228/D/D3/build_tarballs.jl? What's the executable? A script which requires an interpreter?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I was mistaken, only the UI components are present in the npm package. https://github.com/google-ai-edge/model-explorer/blob/main/src/builtin-adapter/python/pip_package/build_pip_package.sh has the bazel scripts for the python wheel

[here](https://github.com/google-ai-edge/model-explorer).
"""
macro mlir_visualize(args...)
default_options = Dict{Symbol,Any}(
:optimize => true,
:no_nan => false,
:client => nothing,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tangentially from this PR, we should consider having these options defaults in one place (rather than copied many places)

:raise => false,
:raise_first => false,
:shardy_passes => :(:to_mhlo_shardings),
:assert_nonallocating => false,
:donated_args => :(:auto),
:transpose_propagate => :(:up),
:reshape_propagate => :(:up),
:optimize_then_pad => true,
:optimize_communications => true,
:cudnn_hlo_optimize => false,
)
compile_expr, (; compiled) = compile_call_expr(
__module__, compile_mlir, default_options, args...
)
#! format: off
return esc(
:(
if Sys.which("model-explorer") === nothing
error("model-explorer is not in your PATH. Please install it from \
https://github.com/google-ai-edge/model-explorer")
end;
$(compile_expr);
mlir_mod = $(first)($(compiled));
tmpfile = tempname() * ".mlir";
open(tmpfile, "w") do io
print(io, mlir_mod)
end;
run(`model-explorer $(tmpfile)`)
)
)
#! format: on
end

function compile_call_expr(mod, compiler, options::Dict, args...)
while length(args) > 1
option, args = args[1], args[2:end]
Expand Down
11 changes: 10 additions & 1 deletion src/Reactant.jl
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,15 @@ function Enzyme.make_zero(
return res
end

using .Compiler: @compile, @code_hlo, @code_mhlo, @jit, @code_xla, traced_getfield, compile
using .Compiler:
@compile,
@code_hlo,
@code_mhlo,
@jit,
@code_xla,
@mlir_visualize,
traced_getfield,
compile
export ConcreteRArray,
ConcreteRNumber,
ConcretePJRTArray,
Expand All @@ -214,6 +222,7 @@ export ConcreteRArray,
@code_xla,
@jit,
@trace,
@mlir_visualize,
within_compile

const registry = Ref{Union{Nothing,MLIR.IR.DialectRegistry}}()
Expand Down
Loading