Fix derivative for mvlgamma (#15049)
authorvishwakftw <cs15btech11043@iith.ac.in>
Fri, 14 Dec 2018 04:30:40 +0000 (20:30 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Fri, 14 Dec 2018 04:32:57 +0000 (20:32 -0800)
Summary:
Fixes #15015.

Added tests to validate derivative.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/15049

Reviewed By: soumith

Differential Revision: D13434117

Pulled By: zou3519

fbshipit-source-id: 4a292600af9eb08b67c0f8b5482e9512aac95e72

test/common_methods_invocations.py
test/test_jit.py
tools/autograd/templates/Functions.cpp

index 043e874..c4e2d9e 100644 (file)
@@ -458,6 +458,10 @@ method_tests = [
      NO_ARGS, [skipIfNoLapack]),
     ('matrix_power', lambda: random_fullrank_matrix_distinct_singular_value(S, S), [-2], "n=-2",
      NO_ARGS, [skipIfNoLapack]),
+    ('mvlgamma', torch.empty(S,).uniform_(0.5, 1), [1], "p=1"),
+    ('mvlgamma', torch.empty(S,).uniform_(1, 2), [2], "p=2"),
+    ('mvlgamma', torch.empty(S, S).uniform_(1.5, 3), [3], "p=3"),
+    ('mvlgamma', torch.empty(S, S).uniform_(2.5, 5), [5], "p=5"),
     ('addcmul', (S, S), ((S, S), (S, S))),
     ('addcmul', (S, S), ((S, 1), (1, S)), 'broadcast_rhs'),
     ('addcmul', (1,), ((S, S, 1), (1, S)), 'broadcast_all'),
index 1c76d68..ae6ba8d 100644 (file)
@@ -10822,7 +10822,6 @@ def add_autograd_test(
                 args_variable, kwargs_variable = create_input(args, requires_grad=not is_inplace, call_kwargs=kwargs)
                 self_tensor = deepcopy(self_variable.data)
                 args_tensor = deepcopy(unpack_variables(args_variable))
-                output_variable = getattr(self_variable, name)(*args_variable, **kwargs_variable)
 
                 def fn(*inputs, **kwargs):
                     output = getattr(inputs[0], name)(*inputs[1:], **kwargs)
index 8d71ba6..308865f 100644 (file)
@@ -144,9 +144,9 @@ Tensor pow_backward_exponent(Tensor grad, const Scalar & base, const Tensor & ex
 }
 
 Tensor mvlgamma_backward(Tensor grad, const Tensor & self, int64_t p) {
-  Tensor args = at::arange(-p + 1, 1, -1, self.options()).div_(2.);
+  Tensor args = at::arange(-p / 2. + 0.5, 0.5, 0.5, self.options());
   args = args.add(self.unsqueeze(-1));
-  return grad * args.digamma_().sum(-1).add_(p * (p - 1) * std::log(M_PI) / 4.);
+  return grad * args.digamma_().sum(-1);
 }
 
 Tensor permute_backwards(const Tensor & grad, IntList fwd_dims) {