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 2.7 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  2. #
  3. # Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
  4. #
  5. # Unless required by applicable law or agreed to in writing,
  6. # software distributed under the License is distributed on an
  7. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  8. from ..core.ops._internal import param_defs as P
  9. from ..functional.elemwise import _elwise
  10. from ..tensor import Tensor
  11. from .module import Module
  12. class Elemwise(Module):
  13. r"""
  14. A :class:`~.Module` to do elemwise operator. Could be replaced with :class:`~.QATModule`
  15. version :class:`~.qat.elemwise.Elemwise` using :func:`~.quantize.quantize_qat`.
  16. :param method: the elemwise method, support the following string.
  17. It will do the normal elemwise operator for float.
  18. * "ADD": a + b
  19. * "FUSE_ADD_RELU": max(x+y, 0)
  20. * "MUL": x * y
  21. * "MIN": min(x, y)
  22. * "MAX": max(x, y)
  23. * "SUB": x - y
  24. * "TRUE_DIV": x / y
  25. * "FUSE_ADD_SIGMOID": sigmoid(x + y)
  26. * "FUSE_ADD_TANH": tanh(x + y)
  27. * "RELU": x > 0 ? x : 0
  28. * "ABS": x > 0 ? x : -x
  29. * "SIGMOID": sigmoid(x)
  30. * "EXP": exp(x)
  31. * "TANH": tanh(x)
  32. * "FUSE_MUL_ADD3": x * y + z
  33. * "FAST_TANH": x * (27. + x * x) / (27. + 9. * x * x)
  34. * "NEGATE": -x
  35. * "ACOS": acos(x)
  36. * "ASIN": asin(x)
  37. * "CEIL": ceil(x)
  38. * "COS": cos(x)
  39. * "EXPM1": expm1(x)
  40. * "FLOOR": floor(x)
  41. * "LOG": log(x)
  42. * "LOG1P": log1p(x)
  43. * "SIN": sin(x)
  44. * "ROUND": round(x)
  45. * "ERF": erf(x)
  46. * "ERFINV": erfinv(x)
  47. * "ERFC": erfc(x)
  48. * "ERFCINV": erfcinv(x)
  49. * "ABS_GRAD": abs_grad
  50. * "FLOOR_DIV": floor_div
  51. * "MOD": mod
  52. * "SIGMOID_GRAD": sigmoid_grad
  53. * "SWITCH_GT0": switch_gt0
  54. * "TANH_GRAD": tanh_grad
  55. * "LT": less
  56. * "LEQ": leq
  57. * "EQ": equal
  58. * "POW": pow
  59. * "LOG_SUM_EXP": log_sum_exp
  60. * "FAST_TANH_GRAD": fast_tanh_grad
  61. * "ATAN2": atan2
  62. * "COND_LEQ_MOV": cond_leq_mov
  63. * "H_SWISH": h_swish
  64. * "FUSE_ADD_H_SWISH": h_swish(x+y)
  65. * "H_SWISH_GRAD": h_swish_grad
  66. * "AND": bool binary: x && y
  67. * "OR": bool binary: x || y
  68. * "XOR": bool binary: x ^ y
  69. * "NOT": bool unary: ~x
  70. """
  71. _elemwise_mode_type = P.Elemwise.Mode
  72. def __init__(self, method):
  73. super().__init__()
  74. self.method = self._elemwise_mode_type.convert(method)
  75. def forward(self, *inps):
  76. return _elwise(*inps, mode=self.method)

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