You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Opposite.py 1.1 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142
  1. from __future__ import absolute_import
  2. from .Node import Op
  3. from .._base import DNNL_LIB
  4. from ..cpu_links import opposite as cpu_opposite
  5. from ..gpu_links import matrix_opposite
  6. class OppositeOp(Op):
  7. def __init__(self, node_A, ctx=None):
  8. super().__init__(OppositeOp, [node_A], ctx)
  9. def compute(self, input_vals, output_val, stream_handle=None):
  10. if self.on_cpu:
  11. if DNNL_LIB['DnnlOpposite']:
  12. cpu_opposite(input_vals[0], output_val)
  13. else:
  14. output_val[:] = -input_vals[0].asnumpy()
  15. else:
  16. matrix_opposite(input_vals[0], output_val, stream_handle)
  17. def gradient(self, output_grad):
  18. return [opposite_op(output_grad, ctx=self.raw_ctx)]
  19. def infer_shape(self, input_shapes):
  20. assert len(input_shapes) == 1
  21. return input_shapes[0]
  22. def opposite_op(node, ctx=None):
  23. """Calculate the opposite of a matrix elementwisely.
  24. Parameters:
  25. ----
  26. node : Node
  27. Input variable.
  28. Returns:
  29. ----
  30. A new Node instance created by Op.
  31. """
  32. return OppositeOp(node, ctx=ctx)