arcadiaphy edited a comment on issue #14396: mx.nd.Custom not working in 
subprocess
URL: 
https://github.com/apache/incubator-mxnet/issues/14396#issuecomment-473577236
 
 
   After #14363, the threads in custom is created after running first custom 
operator, so custom operator needs also to be executed in main process to 
reproduce the bug:
   
   ```
   from concurrent import futures
   
   import mxnet as mx
   import sys
   
   class AdditionOP(mx.operator.CustomOp):
       def __init__(self):
           super(AdditionOP, self).__init__()
       def forward(self, is_train, req, in_data, out_data, aux):
           out_data[0][:] = in_data[0] + in_data[1]
       def backward(self, req, out_grad, in_data, out_data, in_grad, aux):
           in_grad[0][:] = out_grad[0]
           in_grad[1][:] = out_grad[0]
   
   @mx.operator.register("AdditionOP")
   class AdditionOPProp(mx.operator.CustomOpProp):
       def __init__(self):
           super(AdditionOPProp, self).__init__()
       def list_arguments(self):
           return ['a', 'b']
       def list_outputs(self):
           return ['output']
       def infer_shape(self, in_shape):
           return in_shape, [in_shape[0]]
       def create_operator(self, ctx, shapes, dtypes):
           return AdditionOP()
   
   def foo():
       a = mx.nd.array([1, 2, 3])
       b = mx.nd.array([4, 5, 6])
   
       a.attach_grad()
       b.attach_grad()
   
       print("REC")
       with mx.autograd.record():
           c = mx.nd.Custom(a, b, op_type='AdditionOP')
   
       dc = mx.nd.array([7, 8, 9])
       c.backward(dc)
   
       print('Okay :-)')
       print('a + b = c \n {} + {} = {}'.format(a.asnumpy(), b.asnumpy(), 
c.asnumpy()))
   
   def main():
       foo()  # ensure custom threads created in main process
       ex = futures.ProcessPoolExecutor(1)
       r = ex.submit(foo)
       r.result()
   
   if __name__ == '__main__':
       main()
   ```

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to