-
Notifications
You must be signed in to change notification settings - Fork 2
/
mix.exs
322 lines (259 loc) · 8.92 KB
/
mix.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
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
# Force NxIREE.MixHelpers to be available before the app is compiled
Code.eval_file("./lib/nx_iree/mix_helpers.exs")
defmodule NxIREE.MixProject do
use Mix.Project
@version File.read!(Path.join([__DIR__, "priv", "VERSION"]))
@source_url "https://github.com/elixir-nx/nx_iree"
import NxIREE.MixHelpers, only: [download!: 3, github_release_path: 2]
def project do
n_jobs = to_string(max(System.schedulers_online() - 2, 1))
[
app: :nx_iree,
version: @version,
elixir: "~> 1.15",
start_permanent: Mix.env() == :prod,
deps: deps(),
docs: docs(),
name: "NxIREE",
description: "IREE compiler and runtime facilities for Nx",
package: package(),
preferred_cli_env: [
docs: :docs,
"hex.publish": :docs
],
compilers: [:nx_iree, :elixir_make] ++ Mix.compilers(),
aliases: [
"compile.nx_iree": &compile/1,
"iree.version": &version/1
],
make_env: fn ->
priv_path = Path.join(Mix.Project.app_path(), "priv")
cwd_relative_to_priv = relative_to(__DIR__, priv_path)
%{
"MIX_BUILD_EMBEDDED" => "#{Mix.Project.config()[:build_embedded]}",
"CWD_RELATIVE_TO_PRIV_PATH" => cwd_relative_to_priv,
"MAKE_NUM_JOBS" => n_jobs,
"IREE_GIT_REV" => nx_iree_config().tag,
"NX_IREE_SOURCE_DIR" => nx_iree_config().source_dir,
"NX_IREE_CACHE_SO" => nx_iree_config().nx_iree_so_path,
"NX_IREE_PREFER_PRECOMPILED" => to_string(nx_iree_config().use_precompiled)
}
end
]
end
# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger],
mod: {NxIREE.Application, []}
]
end
# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:elixir_make, "~> 0.6", runtime: false},
{:exla, "~> 0.9"},
{:nx, "~> 0.9"},
{:ex_doc, "~> 0.34"},
{:req, "~> 0.5", runtime: false}
]
end
defp package do
[
maintainers: ["Paulo Valente"],
licenses: ["Apache-2.0"],
links: %{"GitHub" => @source_url},
files: [
"lib",
"priv",
"mix.exs",
"README.md",
"LICENSE",
"CHANGELOG.md",
"cmake",
"c_src",
"Makefile"
]
]
end
defp docs do
[
main: "NxIREE",
source_url_pattern: "#{@source_url}/blob/v#{@version}/nx_iree/%{path}#L%{line}",
extras: [
"CHANGELOG.md"
],
skip_undefined_reference_warnings_on: ["CHANGELOG.md"]
]
end
defp version(_args) do
IO.puts(nx_iree_config().tag)
end
defp compile(args) do
:ok = download_and_unzip_iree_release(args)
if nx_iree_config().use_precompiled and not File.exists?(nx_iree_config().nx_iree_so_path) do
case download_precompiled_nx_iree_lib() do
:ok ->
:ok
{:error, :not_found} ->
System.put_env("NX_IREE_PREFER_PRECOMPILED", "false")
:ok
{:error, reason} ->
raise "unable to download nx_iree: #{inspect(reason)}"
end
else
:ok
end
end
defp nx_iree_config() do
version = System.get_env("NX_IREE_VERSION", "20240822.993")
tag = System.get_env("NX_IREE_GIT_REV", "candidate-20240822.993")
env_dir = System.get_env("NX_IREE_COMPILER_DIR")
home_cache = Path.join(System.fetch_env!("HOME"), ".cache")
source_env_dir =
System.get_env("NX_IREE_SOURCE_DIR", Path.join([home_cache, "nx_iree", "iree-#{tag}"]))
dir = env_dir || Path.join(__DIR__, "cache/iree")
source_dir = source_env_dir || Path.join(__DIR__, "cache/iree-source")
use_precompiled = System.get_env("NX_IREE_PREFER_PRECOMPILED", "true") in ["1", "true"]
%{
version: version,
tag: tag,
base: "iree",
env_dir: env_dir,
dir: dir,
source_dir: source_dir,
use_precompiled: use_precompiled,
nx_iree_so_path: Path.join([__DIR__, "cache", "libnx_iree.so"]),
nx_iree_tar_gz_path: Path.join([__DIR__, "cache", "libnx_iree.tar.gz"])
}
end
defp download_and_unzip_iree_release(args) do
nx_iree_config = nx_iree_config()
cache_dir =
if dir = System.get_env("NX_IREE_CACHE") do
Path.expand(dir)
else
:filename.basedir(:user_cache, "iree")
end
if "--force" in args do
File.rm_rf(nx_iree_config.dir)
File.rm_rf(nx_iree_config.source_dir)
File.rm_rf(cache_dir)
end
priv_path = Path.join(Mix.Project.app_path(), "priv")
if File.dir?(nx_iree_config.dir) and File.exists?(Path.join(priv_path, "iree-compile")) do
:ok
else
download_and_unzip_iree_release(cache_dir, nx_iree_config)
end
end
defp download_and_unzip_iree_release(cache_dir, nx_iree_config) do
File.mkdir_p!(cache_dir)
nx_iree_zip =
Path.join(cache_dir, "iree-compiler-#{nx_iree_config.version}.zip")
unless File.exists?(nx_iree_zip) do
# Download iree release for the compiler
os = :os.type()
arch =
case List.to_string(:erlang.system_info(:system_architecture)) do
"x86_64" <> _ -> "x86_64"
"aarch64" <> _ -> "aarch64"
"amd64" <> _ -> "amd64"
end
url =
case {os, arch} do
{{:unix, :linux}, arch} ->
"https://github.com/iree-org/iree/releases/download/#{nx_iree_config.tag}/iree_compiler-#{nx_iree_config.version}-cp310-cp310-manylinux_2_27_#{arch}.manylinux_2_28_#{arch}.whl"
{{:unix, :darwin}, _} ->
# MacOS
"https://github.com/iree-org/iree/releases/download/#{nx_iree_config.tag}/iree_compiler-#{nx_iree_config.version}-cp311-cp311-macosx_13_0_universal2.whl"
os ->
Mix.raise("OS #{inspect(os)} is not supported")
end
download!("IREE", url, nx_iree_zip)
end
# Unpack iree and move to the target cache dir
parent_iree_dir = Path.dirname(nx_iree_config.dir)
File.mkdir_p!(parent_iree_dir)
# Extract to the parent directory (it will be inside the iree directory)
{:ok, _} =
nx_iree_zip
|> String.to_charlist()
|> :zip.unzip(cwd: String.to_charlist(parent_iree_dir))
# Remove stray files
File.rm_rf(Path.join(parent_iree_dir, "iree_compiler.egg-info"))
File.rm_rf(Path.join(parent_iree_dir, "iree_compiler-#{nx_iree_config.version}.dist-info"))
iree_compile_path =
Path.join([parent_iree_dir, "iree", "compiler", "_mlir_libs", "iree-compile"])
iree_lld_path =
Path.join([parent_iree_dir, "iree", "compiler", "_mlir_libs", "iree-lld"])
File.chmod!(iree_compile_path, 0o755)
File.chmod!(iree_lld_path, 0o755)
priv_path = Path.join(Mix.Project.app_path(), "priv")
File.mkdir_p!(priv_path)
link_name = Path.join(priv_path, "iree-compile")
File.rm(link_name)
File.ln_s!(iree_compile_path, link_name)
:ok
end
defp download_precompiled_nx_iree_lib() do
nx_iree_config = nx_iree_config()
arch =
case to_string(:erlang.system_info(:system_architecture)) do
"x86_64" <> _ -> "x86_64"
"aarch64" <> _ -> "aarch64"
_ -> Mix.raise("Unsupported architecture")
end
nif_version = :erlang.system_info(:nif_version)
os_name =
case :os.type() do
{:unix, :darwin} ->
"macos"
{:unix, :linux} ->
"linux"
os ->
Mix.raise("OS #{inspect(os)} is not supported")
end
# This is the precompiled path, which should match what's included in releases
# by the github actions workflows
version_path = "libnx_iree-#{os_name}-#{arch}-nif-#{nif_version}"
source_tar_path = "#{version_path}.tar.gz"
zip_name = nx_iree_config.nx_iree_tar_gz_path
result =
download!(
"NxIREE NIFs",
github_release_path(source_tar_path, @version),
zip_name
)
case result do
{:error, reason} ->
{:error, reason}
:ok ->
parent_dir = Path.dirname(zip_name)
:ok =
zip_name
|> String.to_charlist()
|> :erl_tar.extract([:compressed, cwd: String.to_charlist(parent_dir)])
File.rename(
Path.join([parent_dir, version_path, "libnx_iree.so"]),
Path.join(parent_dir, "libnx_iree.so")
)
File.rename(
Path.join([parent_dir, version_path, "iree-runtime"]),
Path.join(parent_dir, "iree-runtime")
)
File.rmdir(Path.join(parent_dir, version_path))
:ok
end
end
# Returns `path` relative to the `from` directory.
defp relative_to(path, from) do
path_parts = path |> Path.expand() |> Path.split()
from_parts = from |> Path.expand() |> Path.split()
{path_parts, from_parts} = drop_common_prefix(path_parts, from_parts)
root_relative = for _ <- from_parts, do: ".."
Path.join(root_relative ++ path_parts)
end
defp drop_common_prefix([h | left], [h | right]), do: drop_common_prefix(left, right)
defp drop_common_prefix(left, right), do: {left, right}
end