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.6 kB

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

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