From 7f77a763df96d2de5a10d601e5dde56729c42085 Mon Sep 17 00:00:00 2001 From: Masahiro Sakai Date: Tue, 3 Dec 2019 17:20:19 +0900 Subject: [PATCH] Avoid ValidationError in NStepGRU link converter test case by a hack At the moment, this hack in necessary for avoiding error like: ValidationError: Nodes in a graph must be topologically sorted, however input 'v330' of node: input: "Permutate_0_const_empty" input: "v330" input: "Permutate_0_const_range" output: "Permutate_0_tmp_0" name: "Permutate_0_tmp_0" op_type: "Scatter" is not output of any previous nodes. --- .../functions_tests/test_rnn.py | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/tests/onnx_chainer_tests/functions_tests/test_rnn.py b/tests/onnx_chainer_tests/functions_tests/test_rnn.py index 8fc2dd094cdd..1903ce9ca3f4 100644 --- a/tests/onnx_chainer_tests/functions_tests/test_rnn.py +++ b/tests/onnx_chainer_tests/functions_tests/test_rnn.py @@ -2,7 +2,9 @@ import chainer.functions as F import chainer.links as L from chainer import testing +import numpy as np +from onnx_chainer import onnx_helper from onnx_chainer.testing import input_generator from onnx_chainer_tests.helper import ONNXModelTest @@ -47,6 +49,25 @@ def __call__(self, hx, ws1, ws2, ws3, bs, xs): self.expect(model, (hx, ws1, ws2, ws3, bs, xs)) +def convert_Permutate(params): + gb = onnx_helper.GraphBuilder() + # indices_name = params.context.get_name(func.indices) + indices_name = params.context.add_const(params.func.indices, + 'indices') # XXX + if params.func.inv: + empty = params.context.add_const( + np.zeros(dtype=np.int64, shape=params.func.indices.shape), 'empty') + r = params.context.add_const( + np.arange(len(params.func.indices), dtype=np.int64), + 'range') + op = 'ScatterElements' if params.opset_version == 11 else 'Scatter' + indices_name = gb.op(op, [empty, indices_name, r]) + params.input_names.append(indices_name) + gb.op_output_named('Gather', params.input_names, params.output_names, + axis=params.func.axis) + return gb.nodes() + + @testing.parameterize( {'n_layers': 1, 'name': 'TestNStepGRU_1_layer'}, {'n_layers': 2, 'name': 'TestNStepGRU_2_layer'}, @@ -74,4 +95,16 @@ def __call__(self, *xs): model = Model() xs = [input_generator.increasing(seq_length, input_size) for i in range(batch_size)] - self.expect(model, xs, skip_opset_version=[7, 8]) + + # XXX: Replace Permutate converter for avoiding error like: + # ValidationError: Nodes in a graph must be topologically sorted, \ + # however input 'v330' of node: + # input: "Permutate_0_const_empty" input: "v330" \ + # input: "Permutate_0_const_range" output: "Permutate_0_tmp_0" \ + # name: "Permutate_0_tmp_0" op_type: "Scatter" + # is not output of any previous nodes. + addon_converters = { + 'Permutate': convert_Permutate, + } + self.expect(model, xs, skip_opset_version=[7, 8], + external_converters=addon_converters)