forked from tracel-ai/burn
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a workaround script for arm64 tch-rs build issue (tracel-ai#180)
- Loading branch information
Showing
4 changed files
with
96 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,5 @@ target | |
Cargo.lock | ||
# These are backup files generated by rustfmt | ||
**/*.rs.bk | ||
.DS_Store | ||
burn-tch/.cargo/config.toml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use std::env; | ||
fn main() { | ||
// Temporary workaround for https://github.com/burn-rs/burn/issues/180 | ||
// Remove this once tch-rs upgrades to Torch 2.0 at least | ||
|
||
if cfg!(all(target_arch = "aarch64", target_os = "macos")) { | ||
let message = "Run scripts/fix-tch-build-arm64.py to fix environment variables for torch"; | ||
env::var("LIBTORCH").expect(message); | ||
env::var("DYLD_LIBRARY_PATH").expect(message); | ||
} else if cfg!(all(target_arch = "aarch64", target_os = "linux")) { | ||
let message = "Libtorch for AARCH64 Linux must be manually installed and set up"; | ||
env::var("LIBTORCH").expect(message); | ||
env::var("DYLD_LIBRARY_PATH").expect(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
This is a helper script to fix burn-tch build issues on Mac M1/M2 machines. | ||
It's a temporary workaround for https://github.com/burn-rs/burn/issues/180 | ||
till tch-rs starts using Torch 2.0 libraries. | ||
This script installs torch via pip3 and creates environment variables in | ||
burn-tch/.cargo/config.toml for tch-rs to link cc libs properly. | ||
""" | ||
|
||
import os | ||
import pathlib | ||
|
||
|
||
def torch_path(): | ||
import torch | ||
return pathlib.Path(torch.__file__).parent | ||
|
||
|
||
def update_toml_config(): | ||
import tomli | ||
import tomli_w | ||
|
||
cargo_cfg_dir = pathlib.Path(__file__).parent.parent.joinpath( | ||
"burn-tch/.cargo").resolve() | ||
cargo_cfg_dir.exists() | ||
if not cargo_cfg_dir.exists(): | ||
os.makedirs(cargo_cfg_dir) | ||
|
||
toml_file_path = cargo_cfg_dir.joinpath("config.toml") | ||
|
||
# Create toml file if does not exists | ||
with open(toml_file_path, 'a') as f: | ||
pass | ||
|
||
with open(toml_file_path, 'rb') as f: | ||
config = tomli.load(f) | ||
|
||
config["env"] = config.get("env", dict()) | ||
|
||
config["env"]["LIBTORCH"] = dict( | ||
value="{}".format(torch_path()), | ||
force=True, | ||
) | ||
|
||
config["env"]["DYLD_LIBRARY_PATH"] = dict( | ||
value="{}/lib".format(torch_path()), | ||
force=True, | ||
) | ||
|
||
with open(toml_file_path, 'wb') as f: | ||
tomli_w.dump(config, f) | ||
|
||
|
||
def main(): | ||
print("Installing/Upgrading torch via pip install ...") | ||
os.system("pip3 install -U torch") | ||
os.system("pip3 install -U tomli") | ||
os.system("pip3 install -U tomli-w") | ||
|
||
print("Updating config.toml with torch library paths ... ") | ||
update_toml_config() | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |