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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 .. import _internal as mgb
  9. from ..core import Tensor, wrap_io_tensor
  10. from ..core.graph import _use_default_if_none
  11. from .module import QATModule
  12. @wrap_io_tensor
  13. def _elemwise_func(mode, *inputs, **kwargs) -> Tensor:
  14. if all(isinstance(i, (int, float)) for i in inputs):
  15. device, comp_graph = _use_default_if_none(None, None)
  16. ret = mgb.opr.elemwise(
  17. *inputs, mode=mode, comp_node=device, comp_graph=comp_graph, **kwargs
  18. )
  19. return ret.inferred_value[0]
  20. return mgb.opr.elemwise(*inputs, mode=mode, **kwargs)
  21. class Elemwise(QATModule):
  22. r"""
  23. A :class:`~.QATModule` to do elemwise operator, should functional operator with this module,
  24. supporting ``qat`` mode and ``normal`` mode.
  25. :param method: the elemwise method, support the following string.
  26. It will do the normal elemwise operator for float.
  27. * "ADD": a + b
  28. * "FUSE_ADD_RELU": max(x+y, 0)
  29. * "MUL": x * y
  30. * "MIN": min(x, y)
  31. * "MAX": max(x, y)
  32. * "SUB": x - y
  33. * "TRUE_DIV": x / y
  34. * "FUSE_ADD_SIGMOID": sigmoid(x + y)
  35. * "FUSE_ADD_TANH": tanh(x + y)
  36. * "RELU": x > 0 ? x : 0
  37. * "ABS": x > 0 ? x : -x
  38. * "SIGMOID": sigmoid(x)
  39. * "EXP": exp(x)
  40. * "TANH": tanh(x)
  41. * "FUSE_MUL_ADD3": x * y + z
  42. * "FAST_TANH": fast_tanh(x)
  43. * "NEGATE": -x
  44. * "ACOS": acos(x)
  45. * "ASIN": asin(x)
  46. * "CEIL": ceil(x)
  47. * "COS": cos(x)
  48. * "EXPM1": expm1(x)
  49. * "FLOOR": floor(x)
  50. * "LOG": log(x)
  51. * "LOG1P": log1p(x)
  52. * "SIN": sin(x)
  53. * "ROUND": round(x)
  54. * "ERF": erf(x)
  55. * "ERFINV": erfinv(x)
  56. * "ERFC": erfc(x)
  57. * "ERFCINV": erfcinv(x)
  58. * "ABS_GRAD": abs_grad
  59. * "FLOOR_DIV": floor_div
  60. * "MOD": mod
  61. * "SIGMOID_GRAD": sigmoid_grad
  62. * "SWITCH_GT0": switch_gt0
  63. * "TANH_GRAD": tanh_grad
  64. * "LT": lt
  65. * "LEQ": leq
  66. * "EQ": eq
  67. * "POW": pow
  68. * "LOG_SUM_EXP": log_sum_exp
  69. * "FAST_TANH_GRAD": fast_tanh_grad
  70. * "ATAN2": atan2
  71. * "COND_LEQ_MOV": cond_leq_mov
  72. * "H_SWISH": h_swish
  73. * "FUSE_ADD_H_SWISH": h_swish(x+y)
  74. * "H_SWISH_GRAD": h_swish_grad
  75. """
  76. _elemwise_mode_type = mgb.opr_param_defs.Elemwise.Mode
  77. def __init__(self, method):
  78. super().__init__()
  79. self.method = self._elemwise_mode_type.convert(method)
  80. def forward(self, *inps):
  81. return _elemwise_func(self.method, *inps)
  82. def forward_qat(self, *inps):
  83. return self.apply_fakequant_with_observer(
  84. self.forward(*inps), self.act_fake_quant, self.act_observer,
  85. )

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