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.

batch_matmul_activation.py 2.1 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. import numpy as np
  9. from ..functional import matmul, relu
  10. from ..tensor import Parameter
  11. from . import init
  12. from .module import Module
  13. class BatchMatMulActivation(Module):
  14. r"""Batched :func:`~.matmul` with activation(only :func:`~.relu` supported), no transpose anywhere."""
  15. def __init__(
  16. self,
  17. batch: int,
  18. in_features: int,
  19. out_features: int,
  20. bias: bool = True,
  21. nonlinear_mode="identity",
  22. **kwargs
  23. ):
  24. super().__init__(**kwargs)
  25. self.batch = batch
  26. self.out_features = out_features
  27. self.in_features = in_features
  28. w_shape = (batch, out_features, in_features)
  29. self.weight = Parameter(np.zeros(w_shape, dtype=np.float32))
  30. self.bias = None
  31. if bias:
  32. b_shape = (out_features,)
  33. self.bias = Parameter(np.zeros(b_shape, dtype=np.float32))
  34. self.nonlinear_mode = nonlinear_mode.lower()
  35. self.reset_parameters()
  36. def _get_fanin(self):
  37. return self.in_features
  38. def reset_parameters(self) -> None:
  39. fanin = self._get_fanin()
  40. std = np.sqrt(1 / fanin)
  41. init.normal_(self.weight, 0.0, std)
  42. if self.bias is not None:
  43. init.zeros_(self.bias)
  44. def _calc_linear(self, x, weight, bias):
  45. res = matmul(weight, x)
  46. if self.bias is not None:
  47. res += bias
  48. if self.nonlinear_mode == "relu":
  49. res = relu(res)
  50. return res
  51. def forward(self, x):
  52. return self._calc_linear(x, self.weight, self.bias)
  53. def _module_info_string(self) -> str:
  54. return "batch={}, in_features={}, out_features={}, bias={}".format(
  55. self.batch, self.in_features, self.out_features, self.bias is not None
  56. )