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.

math_ops_vm_impl.py 6.3 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """Generate vm_impl function for math ops"""
  16. import copy
  17. import numpy as np
  18. from mindspore.common.dtype import dtype_to_nptype
  19. from mindspore.common.tensor import Tensor
  20. from mindspore.ops import operations as P
  21. from mindspore.ops.vm_impl_registry import vm_impl_registry as vm_impl_getters
  22. from .vm_interface import vm
  23. # pylint: disable=unused-argument
  24. @vm_impl_getters.register(P.TensorAdd)
  25. def vm_impl_tensor_add(self):
  26. """Generate vm_impl function for TensorAdd."""
  27. def vm_impl(x, y):
  28. x = x.asnumpy()
  29. y = y.asnumpy()
  30. return Tensor(x + y)
  31. return vm_impl
  32. # pylint: disable=used-before-assignment
  33. @vm_impl_getters.register(P.LogicalNot)
  34. def vm_impl_logical_not(self):
  35. x = x.asnumpy()
  36. out = vm.logical_not(x)
  37. return Tensor(out)
  38. @vm_impl_getters.register(P.MatMul)
  39. def vm_impl_mat_mul(self):
  40. """Generate vm_impl function for MatMul."""
  41. def vm_impl(x, w):
  42. x = x.asnumpy()
  43. w = w.asnumpy()
  44. if self.transpose_a:
  45. x = x.transpose()
  46. if self.transpose_b:
  47. w = w.transpose()
  48. z = x @ w
  49. return Tensor(z)
  50. return vm_impl
  51. @vm_impl_getters.register(P.AddN)
  52. def vm_impl_addn(self):
  53. """Generate vm_impl function for AddN."""
  54. def vm_impl(inputs):
  55. added = copy.deepcopy(inputs[0].asnumpy())
  56. for x in inputs[1:]:
  57. added += x.asnumpy()
  58. return Tensor(added)
  59. return vm_impl
  60. @vm_impl_getters.register(P.Neg)
  61. def vm_impl_neg(self):
  62. """Generate vm_impl function for Neg."""
  63. def vm_impl(x):
  64. x = x.asnumpy()
  65. return Tensor(-x)
  66. return vm_impl
  67. @vm_impl_getters.register(P.Sub)
  68. def vm_impl_Sub(self):
  69. """Generate vm_impl function for Sub."""
  70. def vm_impl(x, y):
  71. x = x.asnumpy()
  72. y = y.asnumpy()
  73. return Tensor(x - y)
  74. return vm_impl
  75. @vm_impl_getters.register(P.Mul)
  76. def vm_impl_mul(self):
  77. """Generate vm_impl function for Mul."""
  78. def vm_impl(x, y):
  79. x = x.asnumpy()
  80. y = y.asnumpy()
  81. return Tensor(x * y)
  82. return vm_impl
  83. @vm_impl_getters.register(P.Square)
  84. def vm_impl_square(self):
  85. """Generate vm_impl function for Square."""
  86. def vm_impl(x):
  87. x = x.asnumpy()
  88. return Tensor(x * x)
  89. return vm_impl
  90. @vm_impl_getters.register(P.Sqrt)
  91. def vm_impl_sqrt(self):
  92. """Generate vm_impl function for Sqrt."""
  93. def vm_impl(x):
  94. x = x.asnumpy()
  95. res = vm.sqrt(x)
  96. return Tensor(res)
  97. return vm_impl
  98. @vm_impl_getters.register(P.Pow)
  99. def vm_impl_pow(self):
  100. """Generate vm_impl function for Pow."""
  101. def vm_impl(x, y):
  102. x = x.asnumpy()
  103. y = y.asnumpy()
  104. res = vm.power(x, y)
  105. return Tensor(res)
  106. return vm_impl
  107. @vm_impl_getters.register(P.Exp)
  108. def vm_impl_exp(self):
  109. """Generate vm_impl function for Exp."""
  110. def vm_impl(x):
  111. x = x.asnumpy()
  112. res = vm.exp(x)
  113. return Tensor(res)
  114. return vm_impl
  115. @vm_impl_getters.register(P.RealDiv)
  116. def vm_impl_real_div(self):
  117. """Generate vm_impl function for RealDiv."""
  118. def vm_impl(x, y):
  119. x = x.asnumpy()
  120. y = y.asnumpy()
  121. out = x / y
  122. out = np.array(out, x.dtype)
  123. return Tensor(out)
  124. return vm_impl
  125. @vm_impl_getters.register(P.Div)
  126. def vm_impl_div(self):
  127. """Generate vm_impl function for Div."""
  128. def vm_impl(x, y):
  129. x = x.asnumpy()
  130. y = y.asnumpy()
  131. return Tensor(x / y)
  132. return vm_impl
  133. @vm_impl_getters.register(P.ReduceMean)
  134. def vm_impl_reduce_mean(self):
  135. """Generate vm_impl function for ReduceMean."""
  136. def vm_impl(x, axis):
  137. x = x.asnumpy()
  138. out = vm.mean(x, axis)
  139. return Tensor(out)
  140. return vm_impl
  141. @vm_impl_getters.register(P.Equal)
  142. def vm_impl_equal(self):
  143. """Generate vm_impl function for Equal."""
  144. def vm_impl(x, y):
  145. x = x.asnumpy()
  146. y = y.asnumpy()
  147. out = vm.equal(x, y)
  148. return Tensor(np.array(out))
  149. return vm_impl
  150. @vm_impl_getters.register(P.NotEqual)
  151. def vm_impl_not_equal(self):
  152. """Generate vm_impl function for NotEqual."""
  153. def vm_impl(x, y):
  154. x = x.asnumpy()
  155. y = y.asnumpy()
  156. out = vm.not_equal(x, y)
  157. return Tensor(np.array(out))
  158. return vm_impl
  159. @vm_impl_getters.register(P.Greater)
  160. def vm_impl_greater(self):
  161. """Generate vm_impl function for Greater."""
  162. def vm_impl(x, y):
  163. x = x.asnumpy()
  164. y = y.asnumpy()
  165. out = vm.greater(x, y)
  166. return Tensor(np.array(out))
  167. return vm_impl
  168. @vm_impl_getters.register(P.Maximum)
  169. def vm_impl_maximum(self):
  170. """Generate vm_impl function for Maximum."""
  171. def vm_impl(x, y):
  172. x = x.asnumpy()
  173. y = y.asnumpy()
  174. out = vm.maximum(x, y)
  175. return Tensor(out)
  176. return vm_impl
  177. @vm_impl_getters.register(P.Minimum)
  178. def vm_impl_minimum(self):
  179. """Generate vm_impl function for Minimum."""
  180. def vm_impl(x, y):
  181. x = x.asnumpy()
  182. y = y.asnumpy()
  183. out = vm.minimum(x, y)
  184. return Tensor(out)
  185. return vm_impl
  186. @vm_impl_getters.register(P.Less)
  187. def vm_impl_less(self):
  188. """Generate vm_impl function for Less"""
  189. def vm_impl(x, y):
  190. x = x.asnumpy()
  191. y = y.asnumpy()
  192. out = vm.less(x, y)
  193. return Tensor(np.array(out))
  194. return vm_impl
  195. @vm_impl_getters.register(P.ScalarCast)
  196. def vm_impl_scalar_cast(self):
  197. """Generate vm_impl function for ScalarCast"""
  198. def vm_impl(x, t):
  199. np_type = dtype_to_nptype(t)
  200. value = np_type(x)
  201. cast_value = value.item()
  202. return cast_value
  203. return vm_impl