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.

OnesLike.py 1.1 kB

5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. from __future__ import absolute_import
  2. import numpy as np
  3. from .Node import Op
  4. from .._base import DNNL_LIB
  5. from ..cpu_links import array_set as cpu_array_set
  6. from ..gpu_links import array_set
  7. class OnesLikeOp(Op):
  8. def __init__(self, node_A, ctx=None):
  9. super().__init__(OnesLikeOp, [node_A], ctx)
  10. def compute(self, input_vals, output_val, stream_handle=None):
  11. if self.on_cpu:
  12. if DNNL_LIB['cpu_ArraySet']:
  13. cpu_array_set(output_val, 1)
  14. else:
  15. output_val[:] = np.ones(input_vals[0].shape)
  16. else:
  17. array_set(output_val, 1, stream_handle)
  18. def gradient(self, output_grad):
  19. return [None]
  20. def infer_shape(self, input_shapes):
  21. assert len(input_shapes) == 1
  22. return input_shapes[0]
  23. def oneslike_op(node, ctx=None):
  24. """Creates a node that represents np.ones(node_A.shape).
  25. Parameters:
  26. ----
  27. node : Node
  28. The Node to pad with 1.
  29. Returns:
  30. ----
  31. A new Node instance created by Op.
  32. """
  33. return OnesLikeOp(node, ctx=ctx)