Add Flax to PyTorch model conversion script
Browse files- flax_to_torch.py +23 -0
flax_to_torch.py
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import argparse
|
2 |
+
|
3 |
+
from transformers import RobertaForMaskedLM
|
4 |
+
|
5 |
+
|
6 |
+
def convert_flax_model_to_torch(flax_model_path: str, torch_model_path: str = "./"):
|
7 |
+
"""
|
8 |
+
Converts Flax model weights to PyTorch weights.
|
9 |
+
"""
|
10 |
+
model = RobertaForMaskedLM.from_pretrained(flax_model_path, from_flax=True)
|
11 |
+
model.save_pretrained(torch_model_path)
|
12 |
+
|
13 |
+
|
14 |
+
if __name__ == "__maim__":
|
15 |
+
parser = argparse.ArgumentParser(description="Flax to Pytorch model coversion")
|
16 |
+
parser.add_argument(
|
17 |
+
"--flax_model_path", type=str, default="flax-community/roberta-pretraining-hindi", help="Flax model path"
|
18 |
+
)
|
19 |
+
parser.add_argument("--torch_model_path", type=str, default="./", help="PyTorch model path")
|
20 |
+
|
21 |
+
args = parser.parse_args()
|
22 |
+
|
23 |
+
convert_flax_model_to_torch(args.flax_model_path, args.torch_model_path)
|