add reduce=True argument to MultiLabelMarginLoss#4924
add reduce=True argument to MultiLabelMarginLoss#4924soumith merged 4 commits intopytorch:masterfrom
Conversation
|
@pytorchbot retest this please |
|
@pytorchbot retest this please |
zou3519
left a comment
There was a problem hiding this comment.
Haven't reviewed the C code or kernels yet, will get to that soon. I left a few comments on the python side to start out
|
|
||
| template <typename Dtype, typename Acctype> | ||
| __global__ void cunn_MultiLabelMarginCriterion_updateGradInput_kernel(Dtype *gradInput, | ||
| Dtype *gradOutput, |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
| size_average. Default: True | ||
|
|
||
| Shape: | ||
| - Input: :math:`(N)` or :math:`(N, *)` where `*` means, any number of additional |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
| - Input: :math:`(N)` or :math:`(N, *)` where `*` means, any number of additional | ||
| dimensions | ||
| - Target: :math:`(N)` or :math:`(N, *)`, same shape as the input | ||
| - Output: scalar. If `reduce` is False, then `(N)` or `(N, *)`, same shape as |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
| @@ -565,10 +565,32 @@ class MultiLabelMarginLoss(_Loss): | |||
| The criterion only considers the first non-negative `y[j]` targets. | |||
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
|
|
||
|
|
||
| def multilabelmarginloss_no_reduce_test(): | ||
| t = Variable(torch.rand(5, 10).mul(10).floor().long()) |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
| check_no_size_average=True, | ||
| ), | ||
| dict( | ||
| module_name='MultiLabelMarginLoss', |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
| if input.dim() == 1: | ||
| n = 1 | ||
| dim = input.size()[0] | ||
| output = torch.Tensor(n).zero_() |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
| return output | ||
|
|
||
|
|
||
| def _multilabelmarginloss_reference(input, target, is_target): |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
|
|
||
| if input.dim() == 1: | ||
| n = 1 | ||
| dim = input.size()[0] |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
| def _multilabelmarginloss_reference(input, target, is_target): | ||
| sum = 0 | ||
| for i in range(0, target.size()[0]): | ||
| target_index = target[i] |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
zou3519
left a comment
There was a problem hiding this comment.
The THNN code looks good for the most part! Just a few nits here and there. Haven't looked at the cuda kernel yet.
| isTarget_data += dim; | ||
| gradInput_data += dim; | ||
| } | ||
| gradInput_data -= nframe*dim; |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
| } | ||
|
|
||
| sum /= dim; | ||
| THTensor_(set1d)(output, t, sum); |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
| else | ||
| { | ||
| THNN_CHECK_DIM_SIZE(gradOutput, 1, 0, nframe); | ||
| gradOutput = THTensor_(newContiguous)(gradOutput); |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
| THTensor_(free)(input); | ||
| THIndexTensor_(free)(target); | ||
| THTensor_(free)(isTarget); | ||
| THTensor_(free)(gradOutput); |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
| reduce (bool, optional): By default, the losses are averaged or summed over | ||
| observations for each minibatch depending on size_average. When reduce | ||
| is False, returns a loss per batch element instead and ignores | ||
| size_average. Default: True |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
| @@ -133,6 +140,11 @@ __global__ void cunn_MultiLabelMarginCriterion_updateGradInput_kernel(Dtype *gra | |||
| } | |||
| __syncthreads(); | |||
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
| reduce (bool, optional): By default, the losses are averaged or summed over | ||
| observations for each minibatch depending on size_average. When reduce | ||
| is False, returns a loss per batch element instead and ignores | ||
| size_average. Default: True |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
| size_average is set to ``False``, the losses are instead summed for | ||
| each minibatch. Default: ``True`` | ||
| reduce (bool, optional): By default, the losses are averaged or summed over | ||
| observations for each minibatch depending on size_average. When reduce |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
| THIndexTensor_(free)(target); | ||
| THTensor_(free)(isTarget); | ||
| THTensor_(free)(gradOutput); | ||
| THTensor_(free)(gradInput); |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
|
Thanks @li-roy ! |
|
I'm not sure why the CI isn't running, but... let's give this one final test |
|
@pytorchbot retest this please |
* add reduce=True argument to MultiLabelMarginLoss * Fix lint * Addressed comments * Remove unneeded syncthreads calls
As per #264. When reduce is False, MultiLabelMarginLoss outputs a loss per sample in minibatch. When reduce is True (default), the current behavior is kept.
Test Plan
test/run_test.sh
Added unit test. For the reduce=False case. Added unit tests for 1d tensors. Added a reference function.