forked from guillaume-be/rust-bert
-
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.
Support wildcard for source files in convert utilities (guillaume-be#399
- Loading branch information
1 parent
a74d023
commit 107fb21
Showing
2 changed files
with
140 additions
and
61 deletions.
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
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 |
---|---|---|
@@ -1,35 +1,46 @@ | ||
from pathlib import Path | ||
import os | ||
import numpy as np | ||
import torch | ||
import subprocess | ||
from pathlib import Path | ||
|
||
import numpy as np | ||
import requests | ||
import torch | ||
|
||
if __name__ == "__main__": | ||
|
||
target_path = Path.home() / 'rustbert' / 'distilbert' | ||
target_path = Path.home() / "rustbert" / "distilbert" | ||
os.makedirs(str(target_path), exist_ok=True) | ||
|
||
weights_url = "https://huggingface.co/sshleifer/tiny-distilbert-base-cased/resolve/main/pytorch_model.bin" | ||
r = requests.get(weights_url, allow_redirects=True) | ||
(target_path / 'pytorch_model.bin').open('wb').write(r.content) | ||
(target_path / "pytorch_model.bin").open("wb").write(r.content) | ||
|
||
weights = torch.load(target_path / 'pytorch_model.bin', map_location='cpu') | ||
weights = torch.load(target_path / "pytorch_model.bin", map_location="cpu") | ||
nps = {} | ||
for k, v in weights.items(): | ||
nps[k] = np.ascontiguousarray(v.cpu().numpy()) | ||
|
||
np.savez(target_path / 'model.npz', **nps) | ||
np.savez(target_path / "model.npz", **nps) | ||
|
||
source = str(target_path / 'model.npz') | ||
target = str(target_path / 'model.ot') | ||
source = str(target_path / "model.npz") | ||
target = str(target_path / "model.ot") | ||
|
||
toml_location = (Path(__file__).resolve() / '..' / '..' / 'Cargo.toml').resolve() | ||
toml_location = (Path(__file__).resolve() / ".." / ".." / "Cargo.toml").resolve() | ||
|
||
subprocess.call( | ||
['cargo', 'run', '--bin=convert-tensor', '--features', 'download-libtorch', '--manifest-path=%s' % toml_location, '--', source, target]) | ||
|
||
os.remove(str(target_path / 'pytorch_model.bin')) | ||
os.remove(str(target_path / 'model.npz')) | ||
|
||
assert (target_path / 'model.ot').exists(), "Conversion of the model failed." | ||
[ | ||
"cargo", | ||
"run", | ||
"--bin=convert-tensor", | ||
"--features", | ||
"download-libtorch", | ||
"--manifest-path=%s" % toml_location, | ||
"--", | ||
source, | ||
target, | ||
] | ||
) | ||
|
||
os.remove(str(target_path / "pytorch_model.bin")) | ||
os.remove(str(target_path / "model.npz")) | ||
|
||
assert (target_path / "model.ot").exists(), "Conversion of the model failed." |