samskalicky commented on a change in pull request #15969: Partitioning Gluon 
HybridBlocks
URL: https://github.com/apache/incubator-mxnet/pull/15969#discussion_r374263587
 
 

 ##########
 File path: tests/python/unittest/test_subgraph_op.py
 ##########
 @@ -18,351 +18,448 @@
 import os
 import ctypes
 import mxnet as mx
-from mxnet.base import SymbolHandle, check_call, _LIB, mx_uint, c_str_array, 
c_str
+from mxnet.base import SymbolHandle, check_call, _LIB, mx_uint, c_str_array, 
c_str, mx_real_t
 from mxnet.symbol import Symbol
 import numpy as np
 from mxnet.test_utils import assert_almost_equal
-
-
-def _test_subgraph_exe(subgraph_backend):
-    def _check_subgraph_exe1(sym, subgraph_backend, op_names):
-        """Use the partitioned sym to simple_bind an executor and compare the 
outputs
-        with those of the original executor"""
-        out = SymbolHandle()
-        check_call(_LIB.MXBuildSubgraphByOpNames(sym.handle, 
c_str(subgraph_backend), mx_uint(len(op_names)),
-                                                  c_str_array(op_names), 
ctypes.byref(out)))
-
-        partitioned_sym = Symbol(out)
-        assert partitioned_sym.list_inputs() == sym.list_inputs()
-        assert partitioned_sym.list_arguments() == sym.list_arguments()
-        assert partitioned_sym.list_auxiliary_states() == 
sym.list_auxiliary_states()
+from mxnet import gluon
+from mxnet.gluon import nn
+from mxnet import nd
+
+def network_structure_1():
+    data1 = mx.sym.var('data1', shape=(2, 3, 10, 10))
+    data2 = mx.sym.var('data2')
+    conv1 = mx.sym.Convolution(data=data1, weight=data2, no_bias=True, 
kernel=(2, 2), num_filter=1)
+    conv2 = mx.sym.Convolution(data=data2, no_bias=True, kernel=(1, 1), 
num_filter=1)
+    out = mx.sym.Group([conv1, conv2])
+    return (out, ['data1'], [(2, 3, 10, 10)])
+
+def network_structure_2():
+    # this tests whether the partitioning algorithm can deal with cycles
+    data = mx.sym.var('data', shape=(2, 3, 10, 10))
+    ret = mx.sym.exp(data)
+    ret1 = mx.sym.cos(ret)
+    ret2 = mx.sym.sin(ret)
+    ret = ret1 + ret2
+    return (ret, ['data'], [(2, 3, 10, 10)]) 
+
+def network_structure_3():
+    # this tests whether the partitioned sym can distinguish in_args and 
aux_states
+    data = mx.sym.var('data', shape=(2, 3, 10, 10))
+    ret = mx.sym.exp(data)
+    ret1 = mx.sym.cos(ret)
+    ret2 = mx.sym.sin(ret)
+    ret = ret1 + ret2
+    ret = mx.sym.BatchNorm(ret)
+    ret = mx.sym.BatchNorm(ret)
+    # Return the same and shape of 'data' and auxiliary states
+    return  (ret, ['data', *ret.list_auxiliary_states()], [(2, 3, 10, 10), 
(3,), (3,), (3,), (3,)])
+
+def network_structure_4():
+    # the last op has multiple duplicate outputs
+    data = mx.sym.var('data', shape=(2, 3, 10, 10))
+    ret = mx.sym.exp(data)
+    ret = mx.sym.Group([ret, ret, ret])
+    return (ret, ['data'], [(2, 3, 10, 10)])
+
+def network_structure_5():
+    # the subgraph has two duplicate input entries
+    data = mx.sym.var('data', shape=(2, 3, 10, 10))
+    ret = data + data
+    return (ret, ['data'], [(2, 3, 10, 10)])
+
+def network_structure_6():
+    data1 = mx.sym.Variable('data1', shape=(3, 3, 10, 10), dtype=np.float32)
+    data2 = mx.sym.Variable('data2', shape=(1, 0, 2, 2))
+    data3 = mx.sym.sin(data2)
+    conv = mx.sym.Convolution(data=data1, weight=data3, kernel=(2, 2), 
num_filter=1)
+    return (conv, ['data1'], [(3, 3, 10, 10)])
+        
+def network_structure_7():
+    # in this graph, the subgraph node and the other two external nodes form a 
cycle
+    data = mx.sym.Variable('data', shape=(1,))
+    ret1 = mx.sym.sin(data)
+    ret2 = mx.sym.cos(ret1)
+    for _ in range(5):
+        ret2 = mx.sym.cos(ret2)
+    ret = ret1 + ret2
+    return (ret, ['data'], [(1,)])
+
+def get_graphs(): 
+    return [
+            (network_structure_1(), ['Convolution']),
+            (network_structure_2(), ['exp', 'sin', '_Plus', 'elemwise_add', 
'_plus']),
+            (network_structure_2(), ['exp', 'cos', '_Plus', 'elemwise_add', 
'_plus']),
+            (network_structure_3(), ['exp', 'sin', '_Plus', 'elemwise_add', 
'_plus']),
+            (network_structure_3(), ['exp', 'cos', '_Plus', 'elemwise_add', 
'_plus']),
+            (network_structure_3(), ['exp', 'sin', '_Plus', 'elemwise_add', 
'_plus', 'BatchNorm']),
+            (network_structure_3(), ['exp', 'cos', '_Plus', 'elemwise_add', 
'_plus', 'BatchNorm']),
+            (network_structure_3(), ['exp', 'BatchNorm']),
+            (network_structure_3(), ['BatchNorm']),
+            (network_structure_4(), ['exp']),
+            (network_structure_5(), ['_plus', '_Plus', 'elemwise_add']),
+            (network_structure_6(), []),
+            (network_structure_6(), [mx.sym.sin.__name__]),
+            (network_structure_6(), [mx.sym.Convolution.__name__]),
+            (network_structure_6(), [mx.sym.sin.__name__, 
mx.sym.Convolution.__name__]),
+            (network_structure_7(), ['sin', 'elemwise_add', '_plus', '_Plus'])
+            ]
+
+def check_subgraph_exe1(sym, subgraph_backend, op_names):
+    """Use the partitioned sym to simple_bind an executor and compare the 
outputs
+    with those of the original executor"""
+    out = SymbolHandle()
+    check_call(_LIB.MXBuildSubgraphByOpNames(sym.handle, 
c_str(subgraph_backend), mx_uint(len(op_names)),
+                                              c_str_array(op_names), 
ctypes.byref(out)))
+
+    partitioned_sym = Symbol(out)
+    assert partitioned_sym.list_inputs() == sym.list_inputs()
+    assert partitioned_sym.list_arguments() == sym.list_arguments()
+    assert partitioned_sym.list_auxiliary_states() == 
sym.list_auxiliary_states()
+    exe = sym.simple_bind(ctx=mx.current_context(), grad_req='null')
+    partitioned_exe = partitioned_sym.simple_bind(ctx=mx.current_context(), 
grad_req='null')
+    input_names = sym.list_inputs()
+    for name in input_names:
+        if name in exe.arg_dict:
+            exe.arg_dict[name][:] = 
mx.nd.random.uniform(shape=exe.arg_dict[name].shape)
+            partitioned_exe.arg_dict[name][:] = exe.arg_dict[name]
+        else:
+            assert name in exe.aux_dict
+            exe.aux_dict[name][:] = 
mx.nd.random.uniform(shape=exe.aux_dict[name].shape)
+            partitioned_exe.aux_dict[name][:] = exe.aux_dict[name]
+    exe.forward()
+    partitioned_exe.forward()
+    assert len(exe.outputs) == len(partitioned_exe.outputs)
+    for i in range(len(exe.outputs)):
+        assert_almost_equal((exe.outputs[i] - 
partitioned_exe.outputs[i]).abs().sum().asnumpy(),
+                            np.zeros(shape=(1,)))
+
+def check_subgraph_exe2(sym, subgraph_backend, op_names):
+    """Use env var MXNET_SUBGRAPH_BACKEND=default to trigger graph 
partitioning in simple_bind
+    and compare results of the partitioned sym and the original sym."""
+    def get_executor(sym, subgraph_backend=None, op_names=None, 
original_exec=None):
+        if subgraph_backend is not None:
+            os.environ['MXNET_SUBGRAPH_BACKEND'] = subgraph_backend
 
 Review comment:
   This is old code, we're not adding it in this PR just refactoring it. So 
we'll ignore the nit for this PR

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