When I tanslate the following python code to cpp code with libtorch
api
class _SphericalHarmonics(torch.autograd.Function):
"""Spherical Harmonics"""
@staticmethod
def forward(
ctx, sh_degree: int, dirs: Tensor, coeffs: Tensor, masks: Tensor
) -> Tensor:
colors = _make_lazy_cuda_func("compute_sh_fwd")(sh_degree, dirs, coeffs, masks)
ctx.save_for_backward(dirs, coeffs, masks)
ctx.sh_degree = sh_degree
ctx.num_bases = coeffs.shape[-2]
return colors
@staticmethod
def backward(ctx, v_colors: Tensor):
dirs, coeffs, masks = ctx.saved_tensors
sh_degree = ctx.sh_degree
num_bases = ctx.num_bases
compute_v_dirs = ctx.needs_input_grad[1]
v_coeffs, v_dirs = _make_lazy_cuda_func("compute_sh_bwd")(
num_bases,
sh_degree,
dirs,
coeffs,
masks,
v_colors.contiguous(),
compute_v_dirs,
)
if not compute_v_dirs:
v_dirs = None
return None, v_dirs, v_coeffs, None
I can’t find the AutogradContext *ctx
has the method or attribute ctx->needs_input_grad
corressponding to the ctx.needs_input_grad
function in pytorch. Have some one know it?
I have try v_dirs.required_grad()
to sub it in cpp with libtorch, I don’t know this is right.
New contributor
Zhiguo Tang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
4