From 280ec289cba7bfd6b36bef3e7fad37143d84463e Mon Sep 17 00:00:00 2001 From: Edward Chen <18449977+edgchen1@users.noreply.github.com> Date: Mon, 19 Dec 2022 15:45:07 -0800 Subject: [PATCH] Use `object` instead of `np.object` and `str` instead of `np.str`. (#337) --- docs/custom_ops.md | 10 +++++----- test/test_pyops.py | 6 +++--- test/test_sentencepiece_ops.py | 10 +++++----- test/test_string_ops.py | 8 ++++---- tutorials/tf2onnx_custom_ops_tutorial.ipynb | 9 +++++---- 5 files changed, 22 insertions(+), 21 deletions(-) diff --git a/docs/custom_ops.md b/docs/custom_ops.md index d30e7db6..f5581d16 100644 --- a/docs/custom_ops.md +++ b/docs/custom_ops.md @@ -102,7 +102,7 @@ node = onnx.helper.make_node( ) text = "Hello world louder" -inputs = np.array([text], dtype=np.object), +inputs = np.array([text], dtype=object), bert_tokenize_result = bert_cased_tokenizer.tokenize(text) @@ -207,7 +207,7 @@ node = onnx.helper.make_node( ) text = "Hello world louder" -token_ids = np.array([bert_cased_tokenizer.tokenize(text)], dtype=np.object), +token_ids = np.array([bert_cased_tokenizer.tokenize(text)], dtype=object), sentences = np.array(text) @@ -383,7 +383,7 @@ graph = helper.make_graph( model = helper.make_model( graph, opset_imports=[helper.make_operatorsetid(domain, 1)]) -text = np.array(["unwanted running", "unwantedX running"], dtype=np.object) +text = np.array(["unwanted running", "unwantedX running"], dtype=object) tokens = np.array(['un', '##want', '##ed', 'runn', '##ing', 'un', '##want', '##ed', '[UNK]', 'runn', '##ing'], dtype=object), indices = np.array([14, 11, 12, 15, 16, 14, 11, 12, -1, 15, 16], dtype=int32) @@ -452,7 +452,7 @@ node = onnx.helper.make_node( model=model ) -inputs = np.array(["Hello world", "Hello world louder"], dtype=np.object), +inputs = np.array(["Hello world", "Hello world louder"], dtype=object), nbest_size = np.array([0], dtype=np.float32), alpha = np.array([0], dtype=np.float32), add_bos = np.array([0], dtype=np.bool_), @@ -521,7 +521,7 @@ node = onnx.helper.make_node( outputs=['tokens'], ) -inputs = np.array([ "Hello world louder"], dtype=np.object), +inputs = np.array([ "Hello world louder"], dtype=object), tokens = np.array(tokenizer(inputs), dtype=int32) expect(node, inputs=[inputs], diff --git a/test/test_pyops.py b/test/test_pyops.py index 7193b322..00dd2d73 100644 --- a/test/test_pyops.py +++ b/test/test_pyops.py @@ -147,7 +147,7 @@ class TestPythonOp(unittest.TestCase): res = [] for x in xs: res.append(sep.join(x)) - return np.array(res, dtype=np.object) + return np.array(res, dtype=object) def test_python_operator(self): so = _ort.SessionOptions() @@ -222,9 +222,9 @@ class TestPythonOp(unittest.TestCase): onnx_model = _create_test_join() self.assertIn('op_type: "PyOpJoin"', str(onnx_model)) sess = _ort.InferenceSession(onnx_model.SerializeToString(), so) - arr = np.array([["a", "b"]], dtype=np.object) + arr = np.array([["a", "b"]], dtype=object) txout = sess.run(None, {'input_1': arr}) - exp = np.array(["a;b"], dtype=np.object) + exp = np.array(["a;b"], dtype=object) assert txout[0][0] == exp[0] diff --git a/test/test_sentencepiece_ops.py b/test/test_sentencepiece_ops.py index a8e429d4..bf623038 100644 --- a/test/test_sentencepiece_ops.py +++ b/test/test_sentencepiece_ops.py @@ -298,7 +298,7 @@ class TestPythonOpSentencePiece(unittest.TestCase): inputs = dict( model=model, inputs=np.array( - ["Hello world", "Hello world louder"], dtype=np.object), + ["Hello world", "Hello world louder"], dtype=object), nbest_size=np.array([0], dtype=np.int64), alpha=np.array([0], dtype=np.float32), add_bos=np.array([0], dtype=np.bool_), @@ -321,7 +321,7 @@ class TestPythonOpSentencePiece(unittest.TestCase): inputs = dict( model=model, inputs=np.array( - ["Hello world", "Hello world louder"], dtype=np.object), + ["Hello world", "Hello world louder"], dtype=object), nbest_size=np.array([0], dtype=np.int64), alpha=np.array([0], dtype=np.float32), add_bos=np.array([0], dtype=np.bool_), @@ -346,7 +346,7 @@ class TestPythonOpSentencePiece(unittest.TestCase): inputs = dict( model=model, inputs=np.array( - ["Hello world", "Hello world louder"], dtype=np.object), + ["Hello world", "Hello world louder"], dtype=object), nbest_size=np.array([0], dtype=np.int64), alpha=np.array([0], dtype=np.float32), add_bos=np.array([0], dtype=np.bool_), @@ -380,7 +380,7 @@ class TestPythonOpSentencePiece(unittest.TestCase): model=model, inputs=np.array( ["Hello world", "Hello world louder"], - dtype=np.object), + dtype=object), nbest_size=np.array( [nbest_size], dtype=np.int64), alpha=np.array([alpha], dtype=np.float32), @@ -415,7 +415,7 @@ class TestPythonOpSentencePiece(unittest.TestCase): model=model, inputs=np.array( ["Hello world", "Hello world louder"], - dtype=np.object), + dtype=object), nbest_size=np.array( [nbest_size], dtype=np.int64), alpha=np.array([alpha], dtype=np.float32), diff --git a/test/test_string_ops.py b/test/test_string_ops.py index 6260c249..c6de5ddd 100644 --- a/test/test_string_ops.py +++ b/test/test_string_ops.py @@ -830,19 +830,19 @@ class TestPythonOpString(unittest.TestCase): def enumerate_matrix_couples(self): for i in range(1, 5): shape = (3,) * i - a = (np.random.rand(*shape) * 10).astype(np.int32).astype(np.str) + a = (np.random.rand(*shape) * 10).astype(np.int32).astype(str) yield a, a for j in range(i): shape2 = list(shape) shape2[j] = 1 b = (np.random.rand(*shape2) * 10).astype( - np.int32).astype(np.str) + np.int32).astype(str) yield a, b for k in range(j+1, i): shape3 = list(shape2) shape3[k] = 1 b = (np.random.rand(*shape3) * 10).astype( - np.int32).astype(np.str) + np.int32).astype(str) yield a, b def test_string_equal_python(self): @@ -1117,7 +1117,7 @@ class TestPythonOpString(unittest.TestCase): cc_sess = _ort.InferenceSession(cc_onnx_model.SerializeToString(), so) inputs = dict(text=np.array(["unwanted running", - "unwantedX running"], dtype=np.object)) + "unwantedX running"], dtype=object)) cc_txout = cc_sess.run(None, inputs) exp = [np.array(['un', '##want', '##ed', 'runn', '##ing', 'un', '##want', '##ed', '[UNK]', 'runn', '##ing']), diff --git a/tutorials/tf2onnx_custom_ops_tutorial.ipynb b/tutorials/tf2onnx_custom_ops_tutorial.ipynb index cc6d9e73..ef76136e 100644 --- a/tutorials/tf2onnx_custom_ops_tutorial.ipynb +++ b/tutorials/tf2onnx_custom_ops_tutorial.ipynb @@ -365,7 +365,7 @@ " for a in list(node.attr.keys()):\n", " del node.attr[a]\n", " # Add the separator as an additional string input\n", - " separator_const = ctx.make_const(utils.make_name('separator_const'), np.array([separator], dtype=np.object))\n", + " separator_const = ctx.make_const(utils.make_name('separator_const'), np.array([separator], dtype=object))\n", " ctx.replace_inputs(node, node.input + [separator_const.output[0]])" ], "cell_type": "code", @@ -459,7 +459,7 @@ " outputs=[PyCustomOpDef.dt_string])\n", "def unsorted_segment_join(x, segment_ids, num_segments):\n", " # The custom op implementation.\n", - " result = np.full([num_segments], '', dtype=np.object)\n", + " result = np.full([num_segments], '', dtype=object)\n", " for s, seg_id in zip(x, segment_ids):\n", " result[seg_id] += s\n", " return result\n", @@ -473,7 +473,7 @@ " for s, seg_id in zip(x, segment_ids):\n", " result[seg_id].append(s)\n", " result_joined = [separator.join(l) for l in result]\n", - " return np.array(result_joined, dtype=np.object)" + " return np.array(result_joined, dtype=object)" ] }, { @@ -485,7 +485,8 @@ "output_type": "stream", "name": "stdout", "text": [ - "[array(['javascript', 'carpet'], dtype=object)]\n[array(['java-script', 'car-pet'], dtype=object)]\n" + "[array(['javascript', 'carpet'], dtype=object)]\n", + "[array(['java-script', 'car-pet'], dtype=object)]\n" ] } ],