Skip to content

Commit da3442d

Browse files
authored
Fix ONNXRT example with upgraded optimum 1.14.0 (#1381)
Signed-off-by: Mengni Wang <[email protected]> Signed-off-by: yuwenzho <[email protected]>
1 parent f5167dc commit da3442d

File tree

5 files changed

+97
-18
lines changed

5 files changed

+97
-18
lines changed

examples/onnxrt/nlp/huggingface_model/text_generation/llama/quantization/ptq_static/prepare_model.py

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import argparse
22
import os
33
import subprocess
4+
import optimum.version
5+
from packaging.version import Version
6+
OPTIMUM114_VERSION = Version("1.14.0")
47

58

69
def parse_arguments():
@@ -12,20 +15,37 @@ def parse_arguments():
1215

1316
def prepare_model(input_model, output_model):
1417
print("\nexport model...")
15-
subprocess.run(
16-
[
17-
"optimum-cli",
18-
"export",
19-
"onnx",
20-
"--model",
21-
f"{input_model}",
22-
"--task",
23-
"text-generation-with-past",
24-
f"{output_model}",
25-
],
26-
stdout=subprocess.PIPE,
27-
text=True,
28-
)
18+
if Version(optimum.version.__version__) >= OPTIMUM114_VERSION:
19+
subprocess.run(
20+
[
21+
"optimum-cli",
22+
"export",
23+
"onnx",
24+
"--model",
25+
f"{input_model}",
26+
"--task",
27+
"text-generation-with-past",
28+
"--legacy",
29+
f"{output_model}",
30+
],
31+
stdout=subprocess.PIPE,
32+
text=True,
33+
)
34+
else:
35+
subprocess.run(
36+
[
37+
"optimum-cli",
38+
"export",
39+
"onnx",
40+
"--model",
41+
f"{input_model}",
42+
"--task",
43+
"text-generation-with-past",
44+
f"{output_model}",
45+
],
46+
stdout=subprocess.PIPE,
47+
text=True,
48+
)
2949

3050
assert os.path.exists(output_model), f"{output_model} doesn't exist!"
3151

examples/onnxrt/nlp/huggingface_model/text_generation/llama/quantization/weight_only/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pip install -r requirements.txt
1717
## 2. Prepare Model
1818

1919
```bash
20-
optimum-cli export onnx --model decapoda-research/llama-7b-hf --task text-generation-with-past ./llama_7b
20+
python prepare_model.py --input_model="decapoda-research/llama-7b-hf" --output_model="./llama_7b"
2121
```
2222

2323
# Run
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import argparse
2+
import os
3+
import subprocess
4+
import optimum.version
5+
from packaging.version import Version
6+
OPTIMUM114_VERSION = Version("1.14.0")
7+
8+
9+
def parse_arguments():
10+
parser = argparse.ArgumentParser()
11+
parser.add_argument("--input_model", type=str, required=False, default="")
12+
parser.add_argument("--output_model", type=str, required=True)
13+
return parser.parse_args()
14+
15+
16+
def prepare_model(input_model, output_model):
17+
print("\nexport model...")
18+
if Version(optimum.version.__version__) >= OPTIMUM114_VERSION:
19+
subprocess.run(
20+
[
21+
"optimum-cli",
22+
"export",
23+
"onnx",
24+
"--model",
25+
f"{input_model}",
26+
"--task",
27+
"text-generation-with-past",
28+
"--legacy",
29+
f"{output_model}",
30+
],
31+
stdout=subprocess.PIPE,
32+
text=True,
33+
)
34+
else:
35+
subprocess.run(
36+
[
37+
"optimum-cli",
38+
"export",
39+
"onnx",
40+
"--model",
41+
f"{input_model}",
42+
"--task",
43+
"text-generation-with-past",
44+
f"{output_model}",
45+
],
46+
stdout=subprocess.PIPE,
47+
text=True,
48+
)
49+
50+
assert os.path.exists(output_model), f"{output_model} doesn't exist!"
51+
52+
53+
if __name__ == "__main__":
54+
args = parse_arguments()
55+
prepare_model(args.input_model, args.output_model)

test/adaptor/onnxrt_adaptor/test_weight_only_adaptor.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ def __iter__(self):
3838
class TestWeightOnlyAdaptor(unittest.TestCase):
3939
@classmethod
4040
def setUpClass(self):
41-
cmd = "optimum-cli export onnx --model hf-internal-testing/tiny-random-gptj --task text-generation gptj/"
41+
cmd = (
42+
"optimum-cli export onnx --model hf-internal-testing/tiny-random-gptj --task text-generation --legacy gptj/"
43+
)
4244
p = subprocess.Popen(
4345
cmd, preexec_fn=os.setsid, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True
4446
) # nosec

test/model/test_onnx_model.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,9 @@ def setUp(self):
193193
model = onnx.helper.make_model(graph, **{"opset_imports": [onnx.helper.make_opsetid("", 14)]})
194194
self.matmul_reshape_model = model
195195

196-
cmd = "optimum-cli export onnx --model hf-internal-testing/tiny-random-gptj --task text-generation gptj/"
196+
cmd = (
197+
"optimum-cli export onnx --model hf-internal-testing/tiny-random-gptj --task text-generation --legacy gptj/"
198+
)
197199
p = subprocess.Popen(
198200
cmd, preexec_fn=os.setsid, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True
199201
) # nosec
@@ -216,7 +218,7 @@ def test_hf_model(self):
216218

217219
config = AutoConfig.from_pretrained("hf_test")
218220
sessions = ORTModelForCausalLM.load_model("hf_test/decoder_model.onnx")
219-
model = ORTModelForCausalLM(sessions[0], config, "hf_test", use_cache=False, use_io_binding=False)
221+
model = ORTModelForCausalLM(sessions, config, model_save_dir="hf_test", use_cache=False, use_io_binding=False)
220222
self.assertNotEqual(model, None)
221223

222224
def test_nodes(self):

0 commit comments

Comments
 (0)