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.

elemwise.py 4.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. # -*- coding: utf-8 -*-
  2. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  3. #
  4. # Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
  5. #
  6. # Unless required by applicable law or agreed to in writing,
  7. # software distributed under the License is distributed on an
  8. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. # pylint: disable=unused-argument,invalid-name,redefined-builtin,arguments-out-of-order
  10. import functools
  11. import megengine._internal as mgb
  12. from ..core.tensor import Tensor, wrap_io_tensor
  13. __all__ = [
  14. "abs",
  15. "arccos",
  16. "add",
  17. "arcsin",
  18. "ceil",
  19. "cos",
  20. "divide",
  21. "equal",
  22. "exp",
  23. "greater",
  24. "greater_equal",
  25. "floor",
  26. "less",
  27. "less_equal",
  28. "log",
  29. "maximum",
  30. "minimum",
  31. "mod",
  32. "multiply",
  33. "power",
  34. "relu",
  35. "round",
  36. "sigmoid",
  37. "sin",
  38. "subtract",
  39. "tanh",
  40. ]
  41. def _elemwise(mode): # DONT export
  42. """Decorator helps to wrap megbrain element-wise oprs"""
  43. def elemwise_decorator(func):
  44. @functools.wraps(func)
  45. @wrap_io_tensor
  46. def elemwise_func(*inputs) -> Tensor:
  47. return mgb.opr.elemwise(*inputs, mode=mode)
  48. return elemwise_func
  49. return elemwise_decorator
  50. @_elemwise("ABS")
  51. def abs(x):
  52. """Calculate the absolute value element-wise."""
  53. @_elemwise("ACOS")
  54. def arccos(x):
  55. """Inverse cosine, element-wise."""
  56. @_elemwise("ADD")
  57. def add(x, y):
  58. """Element-wise addition."""
  59. @_elemwise("ASIN")
  60. def arcsin(x):
  61. """Inverse sine, element-wise."""
  62. @_elemwise("CEIL")
  63. def ceil(x):
  64. """Return the ceil of the input, element-wise."""
  65. @_elemwise("COS")
  66. def cos(x):
  67. """Cosine, element-wise."""
  68. @_elemwise("TRUE_DIV")
  69. def divide(x, y):
  70. """Return (x / y) element-wise."""
  71. @_elemwise("EQ")
  72. def equal(x, y):
  73. """Return (x == y) element-wise."""
  74. @_elemwise("EXP")
  75. def exp(x):
  76. """Calculate the exponential element-wise"""
  77. @_elemwise("FLOOR")
  78. def floor(x):
  79. """Return the floor of the input, element-wise"""
  80. def greater(x, y):
  81. """Return (x > y) element-wise."""
  82. return less(y, x)
  83. def greater_equal(x, y):
  84. """Return (x >= y) element-wise"""
  85. return less_equal(y, x)
  86. @_elemwise("LT")
  87. def less(x, y):
  88. """Return (x < y) element-wise."""
  89. @_elemwise("LEQ")
  90. def less_equal(x, y):
  91. """Return (x =< y) element-wise."""
  92. @_elemwise("LOG")
  93. def log(x):
  94. """Natural logarithm (base `e`), element-wise."""
  95. @_elemwise("MAX")
  96. def maximum(x, y):
  97. """Element-wise maximum of array elements."""
  98. @_elemwise("MIN")
  99. def minimum(x, y):
  100. """Element-wise minimum of array elements."""
  101. @_elemwise("MOD")
  102. def mod(x, y):
  103. """Return element-wise remainder of division."""
  104. @_elemwise("MUL")
  105. def multiply(x, y):
  106. """Element-wise multiplication."""
  107. @_elemwise("POW")
  108. def power(x, y):
  109. """First tensor elements raised to powers from second tensor (x ** y), element-wise."""
  110. @_elemwise("RELU")
  111. def relu(x):
  112. """Return `max(x, 0)` element-wise."""
  113. @_elemwise("ROUND")
  114. def round(x):
  115. """Round tensor to int element-wise."""
  116. @_elemwise("SIGMOID")
  117. def sigmoid(x):
  118. """Return 1 / ( 1 + exp( -x ) ) element-wise."""
  119. @_elemwise("SIN")
  120. def sin(x):
  121. """Sine, element-wise."""
  122. @_elemwise("SUB")
  123. def subtract(x, y):
  124. """Subtract arguments element-wise"""
  125. @_elemwise("TANH")
  126. def tanh(x):
  127. """Compute hyperbolic tangent element-wise."""
  128. @wrap_io_tensor
  129. def clamp(inp: Tensor, lower=None, upper=None) -> Tensor:
  130. r"""
  131. Clamp all elements in :attr:`inp` into the range `[` :attr:`lower`, :attr:`upper` `]` and return
  132. a resulting tensor:
  133. .. math::
  134. y_i = \begin{cases}
  135. \text{lower} & \text{if } x_i < \text{lower} \\
  136. x_i & \text{if } \text{lower} \leq x_i \leq \text{upper} \\
  137. \text{upper} & \text{if } x_i > \text{upper}
  138. \end{cases}
  139. :param inp: the input tensor.
  140. :param lower: lower-bound of the range to be clamped to
  141. :param upper: upper-bound of the range to be clamped to
  142. Example:
  143. .. testcode::
  144. import numpy as np
  145. from megengine import tensor
  146. import megengine.functional as F
  147. a = tensor(np.arange(5).astype(np.int32))
  148. print(F.clamp(a, 2, 4).numpy())
  149. print(F.clamp(a, lower=3).numpy())
  150. print(F.clamp(a, upper=3).numpy())
  151. .. testoutput::
  152. [2 2 2 3 4]
  153. [3 3 3 3 4]
  154. [0 1 2 3 3]
  155. """
  156. assert lower or upper, "At least one of 'lower' or 'upper' must not be None"
  157. if lower:
  158. if upper:
  159. assert lower <= upper, "clamp lower bound is bigger that upper bound"
  160. return minimum(maximum(inp, lower), upper)
  161. else:
  162. return maximum(inp, lower)
  163. else:
  164. return minimum(inp, upper)

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台

Contributors (1)