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)
 
 
[email protected]('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:
[email protected]


Reply via email to