init.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import copy
  2. import torch as t
  3. from torch.nn import init as torch_init
  4. import functools
  5. from federatedml.nn.backend.torch.base import FateTorchLayer
  6. from federatedml.nn.backend.torch.base import Sequential
  7. str_init_func_map = {
  8. "uniform": torch_init.uniform_,
  9. "normal": torch_init.normal_,
  10. "constant": torch_init.constant_,
  11. "xavier_uniform": torch_init.xavier_uniform_,
  12. "xavier_normal": torch_init.xavier_normal_,
  13. "kaiming_uniform": torch_init.kaiming_uniform_,
  14. "kaiming_normal": torch_init.kaiming_normal_,
  15. "eye": torch_init.eye_,
  16. "dirac": torch_init.dirac_,
  17. "orthogonal": torch_init.orthogonal_,
  18. "sparse": torch_init.sparse_,
  19. "zeros": torch_init.zeros_,
  20. "ones": torch_init.ones_
  21. }
  22. #
  23. # def extract_param(func):
  24. #
  25. # args = inspect.getargspec(func)
  26. # keys = args[0][1:]
  27. # if len(keys) == 0:
  28. # return {}
  29. # defaults = args[-1]
  30. # args_map = {}
  31. # if defaults is not None:
  32. # for idx, i in enumerate(keys[-len(defaults):]):
  33. # args_map[i] = defaults[idx]
  34. #
  35. # for i in keys:
  36. # if i not in args_map:
  37. # args_map[i] = Required()
  38. #
  39. # return args_map
  40. def init_weight(m, initializer):
  41. if hasattr(m, 'weight'):
  42. initializer(m.weight)
  43. # LSTM RNN
  44. if hasattr(m, 'weight_hh_l0'):
  45. initializer(m.weight_hh_l0)
  46. # LSTM RNN
  47. if hasattr(m, 'weight_ih_l0'):
  48. initializer(m.weight_ih_l0)
  49. def init_bias(m, initializer):
  50. if hasattr(
  51. m,
  52. 'bias') and not isinstance(
  53. m.bias,
  54. bool) and m.bias is not None: # LSTM, RNN .bias is bool
  55. initializer(m.bias)
  56. # LSTM RNN
  57. if hasattr(m, 'bias_hh_l0') and m.bias_hh_l0 is not None:
  58. initializer(m.bias_hh_l0)
  59. # LSTM RNN
  60. if hasattr(m, 'bias_ih_l0') and m.bias_ih_l0 is not None:
  61. initializer(m.bias_ih_l0)
  62. def get_init_func_type(init='weight'):
  63. if init == 'weight':
  64. return init_weight
  65. elif init == 'bias':
  66. return init_bias
  67. else:
  68. return None
  69. def recursive_init(m, init_func, obj):
  70. if len(list(m.children())) > 0:
  71. if m == obj:
  72. return
  73. recursive_init(m, init_func, m)
  74. else:
  75. try:
  76. init_func(m)
  77. except Exception as e:
  78. print('initialize layer {} failed, exception is :{}'.format(m, e))
  79. def make_apply_func(torch_initializer, param_dict, init_func, layer):
  80. initializer = functools.partial(torch_initializer, **param_dict)
  81. init_func = functools.partial(init_func, initializer=initializer)
  82. recursive_init_func = functools.partial(
  83. recursive_init, obj=layer, init_func=init_func)
  84. return recursive_init_func, param_dict
  85. def get_init_dict(init_func, param_dict, init_type):
  86. rev_dict = {v: k for k, v in str_init_func_map.items()}
  87. rs = {
  88. 'init_type': init_type,
  89. 'init_func': rev_dict[init_func],
  90. 'param': param_dict}
  91. return rs
  92. def record_initializer(layers, init_dict):
  93. if isinstance(layers, FateTorchLayer):
  94. if init_dict['init_type'] == 'weight':
  95. layers.initializer['weight'] = init_dict
  96. elif init_dict['init_type'] == 'bias':
  97. layers.initializer['bias'] = init_dict
  98. def run_init(torch_initializer, input_var, init, layer):
  99. # recursive init
  100. if isinstance(layer, Sequential):
  101. for sub_layer in layer:
  102. run_init(torch_initializer, input_var, init, sub_layer)
  103. # init layer
  104. elif isinstance(layer, FateTorchLayer) or isinstance(layer, t.nn.Module):
  105. recursive_init_func, param_dict = make_apply_func(
  106. torch_initializer, copy.deepcopy(input_var), get_init_func_type(init), layer)
  107. layer.apply(recursive_init_func)
  108. record_initializer(
  109. layer,
  110. get_init_dict(
  111. torch_initializer,
  112. param_dict,
  113. init))
  114. else:
  115. try:
  116. return torch_initializer(layer, **input_var)
  117. except Exception as e:
  118. print(e)
  119. print('skip initialization')
  120. """
  121. Init Func
  122. """
  123. def local_extract(local_dict):
  124. param = {}
  125. for k, v in local_dict.items():
  126. if k != 'layer' and k != 'init':
  127. param[k] = v
  128. return copy.deepcopy(param)
  129. def uniform_(layer, a=0, b=1, init='weight'):
  130. run_init(
  131. str_init_func_map['uniform'],
  132. local_extract(
  133. locals()),
  134. init,
  135. layer)
  136. def normal_(layer, mean=0, std=1, init='weight'):
  137. run_init(str_init_func_map['normal'], local_extract(locals()), init, layer)
  138. def constant_(layer, val, init='weight'):
  139. run_init(
  140. str_init_func_map['constant'],
  141. local_extract(
  142. locals()),
  143. init,
  144. layer)
  145. def ones_(layer, init='weight'):
  146. run_init(str_init_func_map['ones'], local_extract(locals()), init, layer)
  147. def zeros_(layer, init='weight'):
  148. run_init(str_init_func_map['zeros'], local_extract(locals()), init, layer)
  149. def eye_(layer, init='weight'):
  150. run_init(str_init_func_map['eye'], local_extract(locals()), init, layer)
  151. def dirac_(layer, group=1, init='weight'):
  152. run_init(str_init_func_map['dirac'], local_extract(locals()), init, layer)
  153. def xavier_uniform_(layer, gain=1.0, init='weight'):
  154. run_init(str_init_func_map['xavier_uniform'],
  155. local_extract(locals()), init, layer)
  156. def xavier_normal_(layer, gain=1.0, init='weight'):
  157. run_init(str_init_func_map['xavier_normal'],
  158. local_extract(locals()), init, layer)
  159. def kaiming_uniform_(
  160. layer,
  161. a=0,
  162. mode='fan_in',
  163. nonlinearity='leaky_relu',
  164. init='weight'):
  165. run_init(str_init_func_map['kaiming_uniform'],
  166. local_extract(locals()), init, layer)
  167. def kaiming_normal_(
  168. layer,
  169. a=0,
  170. mode='fan_in',
  171. nonlinearity='leaky_relu',
  172. init='weight'):
  173. run_init(str_init_func_map['kaiming_normal'],
  174. local_extract(locals()), init, layer)
  175. def orthogonal_(layer, gain=1, init='weight'):
  176. run_init(
  177. str_init_func_map['orthogonal'],
  178. local_extract(
  179. locals()),
  180. init,
  181. layer)
  182. def sparse_(layer, sparsity, std=0.01, init='weight'):
  183. run_init(str_init_func_map['sparse'], local_extract(locals()), init, layer)
  184. str_fate_torch_init_func_map = {
  185. "uniform": uniform_,
  186. "normal": normal_,
  187. "constant": constant_,
  188. "xavier_uniform": xavier_uniform_,
  189. "xavier_normal": xavier_normal_,
  190. "kaiming_uniform": kaiming_uniform_,
  191. "kaiming_normal": kaiming_normal_,
  192. "eye": eye_,
  193. "dirac": dirac_,
  194. "orthogonal": orthogonal_,
  195. "sparse": sparse_,
  196. "zeros": zeros_,
  197. "ones": ones_
  198. }
  199. if __name__ == '__main__':
  200. pass