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 平台