You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2020/12/20 04:27:04 UTC
[GitHub] [incubator-mxnet] josephevans opened a new pull request #19697: [v1.x] Add more CV models to onnxruntime inference test, add bert model test.
josephevans opened a new pull request #19697:
URL: https://github.com/apache/incubator-mxnet/pull/19697
## Description ##
Add BERT model test with onnxruntime, add additionally supported CV models to unit tests.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-mxnet] josephevans commented on a change in pull request #19697: [v1.x] Add more CV models to onnxruntime inference test, add bert model test.
Posted by GitBox <gi...@apache.org>.
josephevans commented on a change in pull request #19697:
URL: https://github.com/apache/incubator-mxnet/pull/19697#discussion_r548194878
##########
File path: tests/python-pytest/onnx/test_onnxruntime.py
##########
@@ -97,25 +102,79 @@ def download_test_images(tmpdir):
tmp_path = str(tmp_path)
- #labels = load_imgnet_labels(tmp_path)
- test_images = download_test_images(tmp_path)
- sym_file, params_file = get_gluon_cv_model(model, tmp_path)
- onnx_file = export_model_to_onnx(sym_file, params_file)
-
- # create onnxruntime session using the generated onnx file
- ses_opt = onnxruntime.SessionOptions()
- ses_opt.log_severity_level = 3
- session = onnxruntime.InferenceSession(onnx_file, ses_opt)
- input_name = session.get_inputs()[0].name
-
- for img, accepted_ids in test_images:
- img_data = normalize_image(os.path.join(tmp_path,img))
- raw_result = session.run([], {input_name: img_data})
- res = softmax(np.array(raw_result)).tolist()
- class_idx = np.argmax(res)
- assert(class_idx in accepted_ids)
-
- shutil.rmtree(tmp_path)
-
+ try:
+ #labels = load_imgnet_labels(tmp_path)
+ test_images = download_test_images(tmp_path)
+ sym_file, params_file = get_gluon_cv_model(model, tmp_path)
+ onnx_file = export_model_to_onnx(sym_file, params_file)
+
+ # create onnxruntime session using the generated onnx file
+ ses_opt = onnxruntime.SessionOptions()
+ ses_opt.log_severity_level = 3
+ session = onnxruntime.InferenceSession(onnx_file, ses_opt)
+ input_name = session.get_inputs()[0].name
+
+ for img, accepted_ids in test_images:
+ img_data = normalize_image(os.path.join(tmp_path,img))
+ raw_result = session.run([], {input_name: img_data})
+ res = softmax(np.array(raw_result)).tolist()
+ class_idx = np.argmax(res)
+ assert(class_idx in accepted_ids)
+
+ finally:
+ shutil.rmtree(tmp_path)
+
+
+@with_seed()
+@pytest.mark.parametrize('model', ['bert_12_768_12'])
+def test_bert_inference_onnxruntime(tmp_path, model):
+ tmp_path = str(tmp_path)
+ try:
+ import gluonnlp as nlp
+ dataset = 'book_corpus_wiki_en_uncased'
+ ctx = mx.cpu(0)
+ model, vocab = nlp.model.get_model(
+ name=model,
+ ctx=ctx,
+ dataset_name=dataset,
+ pretrained=False,
+ use_pooler=True,
+ use_decoder=False,
+ use_classifier=False)
+ model.initialize(ctx=ctx)
+ model.hybridize(static_alloc=True)
+
+ batch = 5
+ seq_length = 16
+ # create synthetic test data
+ inputs = mx.nd.random.uniform(0, 30522, shape=(batch, seq_length), dtype='float32')
+ token_types = mx.nd.random.uniform(0, 2, shape=(batch, seq_length), dtype='float32')
+ valid_length = mx.nd.array([seq_length] * batch, dtype='float32')
+
+ seq_encoding, cls_encoding = model(inputs, token_types, valid_length)
+
+ prefix = "%s/bert" % tmp_path
+ model.export(prefix)
+ sym_file = "%s-symbol.json" % prefix
+ params_file = "%s-0000.params" % prefix
+ onnx_file = "%s.onnx" % prefix
+
+
+ input_shapes = [(batch, seq_length), (batch, seq_length), (batch,)]
+ converted_model_path = mx.contrib.onnx.export_model(sym_file, params_file, input_shapes, np.float32, onnx_file)
+
+
+ # create onnxruntime session using the generated onnx file
+ ses_opt = onnxruntime.SessionOptions()
+ ses_opt.log_severity_level = 3
+ session = onnxruntime.InferenceSession(onnx_file, ses_opt)
+ onnx_inputs = [inputs, token_types, valid_length]
+ input_dict = dict((session.get_inputs()[i].name, onnx_inputs[i].asnumpy()) for i in range(len(onnx_inputs)))
+ pred_onx = session.run(None, input_dict)[0]
+
+ assert_almost_equal(seq_encoding, pred_onx)
Review comment:
Added!
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-mxnet] marcoabreu commented on a change in pull request #19697: [v1.x] Add more CV models to onnxruntime inference test, add bert model test.
Posted by GitBox <gi...@apache.org>.
marcoabreu commented on a change in pull request #19697:
URL: https://github.com/apache/incubator-mxnet/pull/19697#discussion_r546435689
##########
File path: tests/python-pytest/onnx/test_onnxruntime.py
##########
@@ -118,4 +121,52 @@ def download_test_images(tmpdir):
shutil.rmtree(tmp_path)
+@pytest.mark.parametrize('model', ['bert_12_768_12'])
+def test_bert_inference_onnxruntime(tmp_path, model):
+ import gluonnlp as nlp
+ dataset = 'book_corpus_wiki_en_uncased'
+ ctx = mx.cpu(0)
+ model, vocab = nlp.model.get_model(
+ name=model,
+ ctx=ctx,
+ dataset_name=dataset,
+ pretrained=False,
+ use_pooler=True,
+ use_decoder=False,
+ use_classifier=False)
+ model.initialize(ctx=ctx)
+ model.hybridize(static_alloc=True)
+
+ batch = 5
+ seq_length = 16
+ # create synthetic test data
+ inputs = mx.nd.random.uniform(0, 30522, shape=(batch, seq_length), dtype='float32')
+ token_types = mx.nd.random.uniform(0, 2, shape=(batch, seq_length), dtype='float32')
+ valid_length = mx.nd.array([seq_length] * batch, dtype='float32')
+
+ seq_encoding, cls_encoding = model(inputs, token_types, valid_length)
+
+ prefix = "%s/bert" % tmp_path
+ model.export(prefix)
+ sym_file = "%s-symbol.json" % prefix
+ params_file = "%s-0000.params" % prefix
+ onnx_file = "%s.onnx" % prefix
+
+
+ input_shapes = [(batch, seq_length), (batch, seq_length), (batch,)]
+ converted_model_path = mx.contrib.onnx.export_model(sym_file, params_file, input_shapes, np.float32, onnx_file)
+
+
+ # create onnxruntime session using the generated onnx file
+ ses_opt = onnxruntime.SessionOptions()
+ ses_opt.log_severity_level = 3
+ session = onnxruntime.InferenceSession(onnx_file, ses_opt)
+ onnx_inputs = [inputs, token_types, valid_length]
+ input_dict = dict((session.get_inputs()[i].name, onnx_inputs[i].asnumpy()) for i in range(len(onnx_inputs)))
+ pred_onx = session.run(None, input_dict)[0]
+
+ assert_almost_equal(seq_encoding, pred_onx)
+
+ shutil.rmtree(tmp_path)
Review comment:
Use try finally
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-mxnet] mxnet-bot commented on pull request #19697: [v1.x] Add more CV models to onnxruntime inference test, add bert model test.
Posted by GitBox <gi...@apache.org>.
mxnet-bot commented on pull request #19697:
URL: https://github.com/apache/incubator-mxnet/pull/19697#issuecomment-748561288
Hey @josephevans , Thanks for submitting the PR
All tests are already queued to run once. If tests fail, you can trigger one or more tests again with the following commands:
- To trigger all jobs: @mxnet-bot run ci [all]
- To trigger specific jobs: @mxnet-bot run ci [job1, job2]
***
**CI supported jobs**: [sanity, windows-cpu, centos-gpu, windows-gpu, unix-cpu, website, edge, clang, centos-cpu, miscellaneous, unix-gpu]
***
_Note_:
Only following 3 categories can trigger CI :PR Author, MXNet Committer, Jenkins Admin.
All CI tests must pass before the PR can be merged.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-mxnet] Zha0q1 merged pull request #19697: [v1.x] Add more CV models to onnxruntime inference test, add bert model test.
Posted by GitBox <gi...@apache.org>.
Zha0q1 merged pull request #19697:
URL: https://github.com/apache/incubator-mxnet/pull/19697
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-mxnet] Zha0q1 commented on a change in pull request #19697: [v1.x] Add more CV models to onnxruntime inference test, add bert model test.
Posted by GitBox <gi...@apache.org>.
Zha0q1 commented on a change in pull request #19697:
URL: https://github.com/apache/incubator-mxnet/pull/19697#discussion_r548111557
##########
File path: tests/python-pytest/onnx/test_onnxruntime.py
##########
@@ -97,25 +102,79 @@ def download_test_images(tmpdir):
tmp_path = str(tmp_path)
- #labels = load_imgnet_labels(tmp_path)
- test_images = download_test_images(tmp_path)
- sym_file, params_file = get_gluon_cv_model(model, tmp_path)
- onnx_file = export_model_to_onnx(sym_file, params_file)
-
- # create onnxruntime session using the generated onnx file
- ses_opt = onnxruntime.SessionOptions()
- ses_opt.log_severity_level = 3
- session = onnxruntime.InferenceSession(onnx_file, ses_opt)
- input_name = session.get_inputs()[0].name
-
- for img, accepted_ids in test_images:
- img_data = normalize_image(os.path.join(tmp_path,img))
- raw_result = session.run([], {input_name: img_data})
- res = softmax(np.array(raw_result)).tolist()
- class_idx = np.argmax(res)
- assert(class_idx in accepted_ids)
-
- shutil.rmtree(tmp_path)
-
+ try:
+ #labels = load_imgnet_labels(tmp_path)
+ test_images = download_test_images(tmp_path)
+ sym_file, params_file = get_gluon_cv_model(model, tmp_path)
+ onnx_file = export_model_to_onnx(sym_file, params_file)
+
+ # create onnxruntime session using the generated onnx file
+ ses_opt = onnxruntime.SessionOptions()
+ ses_opt.log_severity_level = 3
+ session = onnxruntime.InferenceSession(onnx_file, ses_opt)
+ input_name = session.get_inputs()[0].name
+
+ for img, accepted_ids in test_images:
+ img_data = normalize_image(os.path.join(tmp_path,img))
+ raw_result = session.run([], {input_name: img_data})
+ res = softmax(np.array(raw_result)).tolist()
+ class_idx = np.argmax(res)
+ assert(class_idx in accepted_ids)
+
+ finally:
+ shutil.rmtree(tmp_path)
+
+
+@with_seed()
+@pytest.mark.parametrize('model', ['bert_12_768_12'])
+def test_bert_inference_onnxruntime(tmp_path, model):
+ tmp_path = str(tmp_path)
+ try:
+ import gluonnlp as nlp
+ dataset = 'book_corpus_wiki_en_uncased'
+ ctx = mx.cpu(0)
+ model, vocab = nlp.model.get_model(
+ name=model,
+ ctx=ctx,
+ dataset_name=dataset,
+ pretrained=False,
+ use_pooler=True,
+ use_decoder=False,
+ use_classifier=False)
+ model.initialize(ctx=ctx)
+ model.hybridize(static_alloc=True)
+
+ batch = 5
+ seq_length = 16
+ # create synthetic test data
+ inputs = mx.nd.random.uniform(0, 30522, shape=(batch, seq_length), dtype='float32')
+ token_types = mx.nd.random.uniform(0, 2, shape=(batch, seq_length), dtype='float32')
+ valid_length = mx.nd.array([seq_length] * batch, dtype='float32')
+
+ seq_encoding, cls_encoding = model(inputs, token_types, valid_length)
+
+ prefix = "%s/bert" % tmp_path
+ model.export(prefix)
+ sym_file = "%s-symbol.json" % prefix
+ params_file = "%s-0000.params" % prefix
+ onnx_file = "%s.onnx" % prefix
+
+
+ input_shapes = [(batch, seq_length), (batch, seq_length), (batch,)]
+ converted_model_path = mx.contrib.onnx.export_model(sym_file, params_file, input_shapes, np.float32, onnx_file)
+
+
+ # create onnxruntime session using the generated onnx file
+ ses_opt = onnxruntime.SessionOptions()
+ ses_opt.log_severity_level = 3
+ session = onnxruntime.InferenceSession(onnx_file, ses_opt)
+ onnx_inputs = [inputs, token_types, valid_length]
+ input_dict = dict((session.get_inputs()[i].name, onnx_inputs[i].asnumpy()) for i in range(len(onnx_inputs)))
+ pred_onx = session.run(None, input_dict)[0]
+
+ assert_almost_equal(seq_encoding, pred_onx)
Review comment:
shall we also add
``` python
assert_almost_equal(cls_encoding, session.run(None, input_dict)[1])
```
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org