grad_multiply.py 442 B

123456789101112131415161718
  1. # Copyright (c) Facebook, Inc. and its affiliates.
  2. #
  3. # This source code is licensed under the MIT license found in the
  4. # LICENSE file in the root directory of this source tree.
  5. import torch
  6. class GradMultiply(torch.autograd.Function):
  7. @staticmethod
  8. def forward(ctx, x, scale):
  9. ctx.scale = scale
  10. res = x.new(x)
  11. return res
  12. @staticmethod
  13. def backward(ctx, grad):
  14. return grad * ctx.scale, None