Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions test/test_nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -5578,6 +5578,18 @@ def bce_with_logistic_no_reduce_scalar_test():
pickle=False)


def kldivloss_with_target_no_reduce_test():
i = torch.rand(10, 10).log()
return dict(
fullname='KLDivLoss_with_target_no_reduce',
constructor=wrap_functional(
lambda t: F.kl_div(i.type_as(t), t, reduce=False)),
input_fn=lambda: torch.rand(10, 10),
reference_fn=lambda t, _:
loss_reference_fns['KLDivLoss'](i.type_as(t), t, reduce=False),
pickle=False)


def kldivloss_no_reduce_test():
t = torch.randn(10, 10)
return dict(
Expand Down Expand Up @@ -6030,6 +6042,7 @@ def multimarginloss_weights_no_reduce_test():
bceloss_no_reduce_scalar_test(),
bceloss_weights_no_reduce_scalar_test(),
bce_with_logistic_no_reduce_scalar_test(),
kldivloss_with_target_no_reduce_test(),
kldivloss_no_reduce_test(),
kldivloss_no_reduce_scalar_test(),
l1loss_no_reduce_test(),
Expand Down
2 changes: 2 additions & 0 deletions tools/autograd/derivatives.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,7 @@

- name: kl_div_forward(Tensor self, Tensor target, bool size_average, bool reduce)
self: kl_div_backward(grad, self, target, size_average, reduce)
target: kl_div_target_backward(grad, self, target, size_average, reduce)

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.


- name: l1_loss_forward(Tensor self, Tensor target, bool size_average, bool reduce)
self: l1_loss_backward(grad, self, target, size_average, reduce)
Expand Down Expand Up @@ -963,6 +964,7 @@
- name: kl_div_backward(Tensor grad_output, Tensor self, Tensor target, bool size_average, bool reduce)
grad_output: kl_div_double_backward_grad_output(grad, self, target, size_average, reduce)
self: zeros_like(grad)
target: zeros_like(grad)

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.


- name: l1_loss_backward(Tensor grad_output, Tensor self, Tensor target, bool size_average, bool reduce)
grad_output: l1_loss_double_backward_grad_output(grad, self, target, size_average, reduce)
Expand Down
12 changes: 12 additions & 0 deletions tools/autograd/templates/Functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,18 @@ Tensor kl_div_double_backward_grad_output(const Tensor & grad, const Tensor & in
return result;
}

// Compute derivatives for targets.
// Assume targets are given as probabilities (i.e. without taking the logarithm).
Tensor kl_div_target_backward(Tensor grad_output, Tensor self, Tensor target, bool size_average, bool reduce) {
if (!reduce) {
return grad_output.mul(target.log().add_(1).sub_(self)).masked_fill_(target == 0, 0.);
}
if (size_average) {
return grad_output.mul(target.log().add_(1).sub_(self)).div_(target.numel()).masked_fill_(target == 0, 0.);
}
return grad_output.mul(target.log().add_(1).sub_(self)).masked_fill_(target == 0, 0.);
}

Tensor log_sigmoid_double_backward(const Tensor & grad, const Tensor & input) {
auto z = input.sigmoid();
return grad * (z - 1) * z;
Expand Down