Skip to content

Commit

Permalink
fix: improvements to precompiled run
Browse files Browse the repository at this point in the history
  • Loading branch information
polvalente committed Jul 15, 2024
1 parent ecc166b commit 4594493
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
22 changes: 19 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,28 @@ SOURCES = $(wildcard c_src/*.cc)
HEADERS = $(wildcard c_src/*.h)
OBJECTS = $(patsubst c_src/%.cc,cache/objs/%.o,$(SOURCES))

ifeq ($(NX_IREE_PREFER_PRECOMPILED), true)
# If we are using precompiled libnx_iree.so, we need to make sure that
# we're not trying to compile it again, which may happen due to the file
# having been recently downloaded.
# By using different nx_iree and NX_IREE_CACHE_SO rules we can ensure
# that only the precompiled .so is used directly.
nx_iree: $(NX_IREE_SO)

.PHONY: $(NX_IREE_CACHE_SO)
$(NX_IREE_CACHE_SO):
ifdef DEBUG
@echo "Using precompiled libnx_iree.so"
endif
else

nx_iree: $(NX_IREE__IREE_RUNTIME_INCLUDE_PATH) $(NX_IREE_SO)

$(NX_IREE_CACHE_SO): $(OBJECTS)
$(CXX) -shared -o $@ $^ $(LDFLAGS)
endif

$(NX_IREE_SO): $(NX_IREE_CACHE_SO)
@ echo $(CWD_RELATIVE_TO_PRIV_PATH)
@ mkdir -p $(CWD_RELATIVE_TO_PRIV_PATH)
@ if [ "${MIX_BUILD_EMBEDDED}" = "true" ]; then \
cp -a $(abspath $(NX_IREE_RUNTIME_LIB)) $(NX_IREE_LIB_DIR) ; \
Expand All @@ -162,8 +180,6 @@ $(NX_IREE_SO): $(NX_IREE_CACHE_SO)
ln -sf $(NX_IREE_CACHE_SO_LINK_PATH) $(NX_IREE_SO) ; \
fi

$(NX_IREE_CACHE_SO): $(OBJECTS)
$(CXX) -shared -o $@ $^ $(LDFLAGS)

# This rule may be overriden by the mix.exs compiler rule
# in that it may download the .so instead of compiling it locally
Expand Down
11 changes: 6 additions & 5 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ defmodule NxIREE.MixProject do
Mix.raise("OS #{inspect(os)} is not supported")
end

download!(url, nx_iree_zip)
download!("IREE", url, nx_iree_zip)
end

# Unpack libtorch and move to the target cache dir
Expand Down Expand Up @@ -215,6 +215,7 @@ defmodule NxIREE.MixProject do
zip_name = nx_iree_config.nx_iree_tar_gz_path

download!(
"NxIREE NIFs",
"https://github.com/elixir-nx/nx_iree/releases/download/v#{@version}/#{source_tar_path}",
zip_name
)
Expand Down Expand Up @@ -247,10 +248,10 @@ defmodule NxIREE.MixProject do
end
end

defp download!(url, dest) do
defp download!(name, url, dest) do
assert_network_tool!()

case download(url, dest) do
case download(name, url, dest) do
:ok ->
:ok

Expand All @@ -259,14 +260,14 @@ defmodule NxIREE.MixProject do
end
end

defp download(url, dest) do
defp download(name, url, dest) do
{command, args} =
case network_tool() do
:curl -> {"curl", ["--fail", "-L", url, "-o", dest]}
:wget -> {"wget", ["-O", dest, url]}
end

IO.puts("Downloading iree from #{url}")
IO.puts("Downloading #{name} from #{url}")

case System.cmd(command, args) do
{_, 0} -> :ok
Expand Down
6 changes: 3 additions & 3 deletions run.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
NxIREE.list_drivers() |> IO.inspect(label: "drivers")

{:ok, [dev | _]} = NxIREE.list_devices("metal") |> IO.inspect()
{:ok, [dev | _]} = NxIREE.list_devices("local-sync") |> IO.inspect()

# Obtained by using EXLA.to_mlir_module(fn a, b -> Nx.add(Nx.cos(a), Nx.sin(b)) end, [Nx.template({4}, :f32), Nx.template({4}, :s64)])
mlir_module = """
Expand All @@ -15,8 +15,8 @@ module {
}
"""

# flags = ["--iree-hal-target-backends=cuda", "--iree-input-type=stablehlo_xla", "--iree-execution-model=async-internal"]
flags = ["--iree-hal-target-backends=metal-spirv", "--iree-input-type=stablehlo_xla", "--iree-execution-model=async-internal"]
flags = ["--iree-hal-target-backends=llvm-cpu", "--iree-input-type=stablehlo_xla", "--iree-execution-model=async-internal"]
# flags = ["--iree-hal-target-backends=metal-spirv", "--iree-input-type=stablehlo_xla", "--iree-execution-model=async-internal"]

%NxIREE.Module{} = module = NxIREE.compile(mlir_module, flags)

Expand Down

0 comments on commit 4594493

Please sign in to comment.