QueensGambit opened a new pull request #17827: [Numpy] Bugfix of MXNet to ONNX 
export of slice operator
URL: https://github.com/apache/incubator-mxnet/pull/17827
 
 
   ## Description ##
   This PR fixes the slice operator export from MXNet into ONNX.
   
   @MoritzMaxeiner reported this problem already: **Incorrect ONNX export of 
SliceChannel** https://github.com/apache/incubator-mxnet/issues/13061.
   The corresponding pull request **ONNX export: Support equal length splits** 
https://github.com/apache/incubator-mxnet/pull/14121 however, only partially 
solved the problem.
   
   The output of the slice operator was corrected but it is still not properly 
handled in the `get_inputs()` function.
   
   The following unit test demonstrates this:
   
   ```python
   # imports
   
   class SplitConcatBlock(HybridBlock):
       """Block which creates two splits and later concatenates them"""
       def __init__(self, name):
           super(SplitConcatBlock, self).__init__(name)
   
       def hybrid_forward(self, F, x):
           splits = F.split(x, axis=1, num_outputs=2)
           return F.concat(*splits)
   
   def test_onnx_export_slice(self):
        net = nn.HybridSequential(prefix='slice_net')
        with net.name_scope():
            net.add(nn.Dense(100, activation='relu'), 
SplitConcatBlock("splitConcat"), nn.Dense(10))
        _check_onnx_export(net)
   ```
   
   Before this PR the conversion of the model from MXNet into ONNX resulted in 
the following error:
   ```python
   Traceback (most recent call last):
     File "mxnet_export_test.py", line 134, in test_onnx_export_slice
       _check_onnx_export(net)
     File "mxnet_export_test.py", line 66, in _check_onnx_export
       onnx_file_path=onnx_file_path)
     File "/opt/mxnet/python/mxnet/contrib/onnx/mx2onnx/export_model.py", line 
87, in export_model
       verbose=verbose)
     File "/opt/mxnet/python/mxnet/contrib/onnx/mx2onnx/export_onnx.py", line 
312, in create_onnx_graph_proto
       checker.check_graph(graph)
     File "/usr/local/lib/python3.6/dist-packages/onnx/checker.py", line 51, in 
checker
       proto.SerializeToString(), ctx)
   onnx.onnx_cpp2py_export.checker.ValidationError: Nodes in a graph must be 
topologically sorted, however input 'slice_netsplitConcatsplit0' of node: 
   input: "slice_netsplitConcatsplit0" input: "slice_netsplitConcatsplit0" 
output: "slice_netsplitConcatconcat0" name: "slice_netsplitConcatconcat0" 
op_type: "Concat" attribute { name: "axis" i: 1 type: INT }
    is not output of any previous nodes.
   ```
   
   Now, this is resolved by replacing:
   ```python
           input_nodes.append(proc_nodes[input_node_id].name)
   ```
   with:
   ```python
           try:
               # ip[1] defines which output index to use
               input_nodes.append(proc_nodes[input_node_id].output[ip[1]])
           except AttributeError:
               # fallback to the name attribute as output if the output 
attribute does not exist (e.g. for data nodes)
               input_nodes.append(proc_nodes[input_node_id].name)
   ```
   
   Ping: @Roshrini @vandanavk 
   
   ## Checklist ##
   ### Essentials ###
   Please feel free to remove inapplicable items for your PR.
   - [ ] The PR title starts with [MXNET-$JIRA_ID], where $JIRA_ID refers to 
the relevant [JIRA issue](https://issues.apache.org/jira/projects/MXNET/issues) 
created (except PRs with tiny changes)
   - [x] Changes are complete (i.e. I finished coding on this PR)
   - [x] All changes have test coverage:
   - Unit tests are added for small changes to verify correctness (e.g. adding 
a new operator)
   - Nightly tests are added for complicated/long-running ones (e.g. changing 
distributed kvstore)
   - Build tests will be added for build configuration changes (e.g. adding a 
new build option with NCCL)
   - [x] Code is well-documented: 
   - For user-facing API changes, API doc string has been updated. 
   - For new C++ functions in header files, their functionalities and arguments 
are documented. 
   - For new examples, README.md is added to explain the what the example does, 
the source of the dataset, expected performance on test set and reference to 
the original paper if applicable
   - Check the API doc at 
https://mxnet-ci-doc.s3-accelerate.dualstack.amazonaws.com/PR-$PR_ID/$BUILD_ID/index.html
   - [x] To the best of my knowledge, examples are either not affected by this 
change, or have been fixed to be compatible with this change
   
   ### Changes ###
   - [ ] Feature1, tests, (and when applicable, API doc)
   - [ ] Feature2, tests, (and when applicable, API doc)
   
   ## Comments ##
   - If this change is a backward incompatible change, why must this change be 
made.
   - Interesting edge cases to note here
   

----------------------------------------------------------------
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]


With regards,
Apache Git Services

Reply via email to