Skip to content

Commit

Permalink
Minor gradcheck update to reduce computations (#45757)
Browse files Browse the repository at this point in the history
Summary: Pull Request resolved: #45757

Test Plan: Imported from OSS

Reviewed By: glaringlee

Differential Revision: D24137143

Pulled By: anjali411

fbshipit-source-id: e0174ec03d93b1fedf27baa72c3542dac0b70058
  • Loading branch information
anjali411 authored and facebook-github-bot committed Oct 6, 2020
1 parent e154b36 commit a3662fa
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions torch/autograd/gradcheck.py
Expand Up @@ -102,13 +102,11 @@ def fn_out():
d[d_idx] = grad_out.conjugate() * conj_w_d + grad_out * w_d.conj()
elif ds_dx.is_complex(): # R -> C
# w_d = conj_w_d = 0.5 * ds_dx
dL_dz_conj = 0.5 * (grad_out.conjugate() * ds_dx + grad_out * ds_dx.conj())
# The above formula is derived for a C -> C function that's a part of
# bigger function with real valued output. From separate calculations,
# it can be verified that the gradient for R -> C function
# equals to real value of the result obtained from the generic formula for
# C -> C functions used above.
d[d_idx] = torch.real(dL_dz_conj)
# dL_dz_conj = 0.5 * [grad_out.conj() * ds_dx + grad_out * ds_dx.conj()]
# = 0.5 * [grad_out.conj() * ds_dx + (grad_out.conj() * ds_dx).conj()]
# = 0.5 * 2 * real(grad_out.conj() * ds_dx)
# = real(grad_out.conj() * ds_dx)
d[d_idx] = torch.real(grad_out.conjugate() * ds_dx)
else: # R -> R
d[d_idx] = ds_dx * grad_out

Expand Down

0 comments on commit a3662fa

Please sign in to comment.