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.

grad_quant_ops.py 5.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """Generate bprop for quantization aware ops"""
  16. from .. import operations as P
  17. from ..operations import _quant_ops as Q
  18. from .grad_base import bprop_getters
  19. from ..composite.multitype_ops.zeros_like_impl import zeros_like
  20. from ... import context
  21. @bprop_getters.register(Q.FakeQuantPerLayer)
  22. def get_bprop_fakequant_with_minmax(self):
  23. """Generate bprop for FakeQuantPerLayer for GPU and Ascend"""
  24. op = Q.FakeQuantPerLayerGrad(
  25. num_bits=self.num_bits, quant_delay=self.quant_delay)
  26. def bprop(x, x_min, x_max, out, dout):
  27. dx = op(dout, x, x_min, x_max)
  28. return dx, zeros_like(x_min), zeros_like(x_max)
  29. return bprop
  30. @bprop_getters.register(Q.FakeQuantPerChannel)
  31. def get_bprop_fakequant_with_minmax_perchannel(self):
  32. """Generate bprop for FakeQuantPerChannel"""
  33. op = Q.FakeQuantPerChannelGrad(num_bits=self.num_bits,
  34. quant_delay=self.quant_delay,
  35. symmetric=self.symmetric,
  36. narrow_range=self.symmetric,
  37. channel_axis=self.channel_axis)
  38. def bprop(x, x_min, x_max, out, dout):
  39. dx = op(dout, x, x_min, x_max)
  40. return dx, zeros_like(x_min), zeros_like(x_max)
  41. return bprop
  42. @bprop_getters.register(Q.BatchNormFold)
  43. def get_bprop_batchnorm_fold(self):
  44. """Generate bprop for BatchNormFold for GPU"""
  45. op = Q.BatchNormFoldGrad(self.epsilon, self.is_training, self.freeze_bn)
  46. def bprop(x, mean, variance, global_step, out, dout):
  47. dx = op(dout[0], dout[1], x, out[0], out[1], global_step)
  48. return dx, zeros_like(mean), zeros_like(variance), zeros_like(global_step)
  49. return bprop
  50. @bprop_getters.register(Q.CorrectionMul)
  51. def get_bprop_correction_mul(self):
  52. """Generate bprop for CorrectionMul for Ascend and GPU"""
  53. grad_dx = Q.CorrectionMulGrad(self.channel_axis)
  54. grad_d_batch_std = Q.CorrectionMulGradReduce(self.channel_axis)
  55. def bprop(x, batch_std, running_std, out, dout):
  56. dx, d_batch_std = grad_dx(dout, x, batch_std, running_std)
  57. return dx, d_batch_std, zeros_like(running_std)
  58. def bprop_npu(x, batch_std, running_std, out, dout):
  59. dx, mul_dx = grad_dx(dout, x, batch_std, running_std)
  60. d_batch_std = grad_d_batch_std(mul_dx)
  61. return dx, d_batch_std, zeros_like(running_std)
  62. if context.get_context('device_target') == "Ascend":
  63. return bprop_npu
  64. return bprop
  65. @bprop_getters.register(Q.BatchNormFold2)
  66. def get_bprop_batchnorm_fold2(self):
  67. """Generate bprop for BatchNormFold2 for GPU"""
  68. op_f = Q.BatchNormFold2Grad(freeze_bn=self.freeze_bn)
  69. def bprop(x, beta, gamma, batch_std, batch_mean, running_std, running_mean, global_step, out, dout):
  70. d_batch_std, d_batch_mean, d_beta, d_gamma, d_x = op_f(dout, x, gamma, batch_std, batch_mean, running_std,
  71. running_mean, global_step)
  72. return d_x, d_beta, d_gamma, d_batch_std, d_batch_mean, zeros_like(running_std), zeros_like(running_mean), \
  73. zeros_like(global_step)
  74. return bprop
  75. @bprop_getters.register(Q.BatchNormFoldD)
  76. def get_bprop_BatchNormFold(self):
  77. """Generate bprop for BatchNormFold for Ascend"""
  78. op = Q.BatchNormFoldGradD(self.epsilon, self.is_training, self.freeze_bn)
  79. def bprop(x, x_sum, x_square_sum, mean, variance, out, dout):
  80. dx = op(dout[1], dout[2], x, out[1], out[2])
  81. return dx, zeros_like(x_sum), zeros_like(x_square_sum), zeros_like(mean), zeros_like(variance)
  82. return bprop
  83. @bprop_getters.register(P.BNTrainingReduce)
  84. def get_bprop_BNTrainingReduce(self):
  85. def bprop(x, out, dout):
  86. return (zeros_like(x),)
  87. return bprop
  88. @bprop_getters.register(Q.BatchNormFold2_D)
  89. def get_bprop_batchnorm_fold2_(self):
  90. """Generate bprop for BatchNormFold2 for Ascend"""
  91. op_reduce = Q.BatchNormFold2GradReduce(freeze_bn=self.freeze_bn)
  92. op_f = Q.BatchNormFold2GradD(freeze_bn=self.freeze_bn)
  93. def bprop(x, beta, gamma, batch_std, batch_mean, running_std, out, dout):
  94. dout_reduce, dout_x_reduce = op_reduce(dout, x)
  95. d_batch_std, d_batch_mean, d_gamma, d_x = op_f(dout, dout_reduce, dout_x_reduce, gamma, batch_std,
  96. batch_mean, running_std)
  97. return d_x, dout_reduce, d_gamma, d_batch_std, d_batch_mean, zeros_like(running_std)
  98. return bprop
  99. @bprop_getters.register(Q.MinMaxUpdatePerLayer)
  100. def get_bprop_fakequant_with_minmax_per_layer_update(self):
  101. """Generate bprop for MinMaxUpdatePerLayer for Ascend"""
  102. def bprop(x, x_min, x_max, out, dout):
  103. return zeros_like(x), zeros_like(x_min), zeros_like(x_max)
  104. return bprop
  105. @bprop_getters.register(Q.MinMaxUpdatePerChannel)
  106. def get_bprop_fakequant_with_minmax_per_channel_update(self):
  107. """Generate bprop for MinMaxUpdatePerChannel for Ascend"""
  108. def bprop(x, x_min, x_max, out, dout):
  109. return zeros_like(x), zeros_like(x_min), zeros_like(x_max)
  110. return bprop