Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multi_margin_loss: check weight shape, make contiguous on CPU, add tests #104852

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions torch/testing/_internal/common_methods_invocations.py
Original file line number Diff line number Diff line change
Expand Up @@ -1430,14 +1430,17 @@ def reference_inputs_multi_margin_loss(op_info, device, dtype, requires_grad, **
)
ps = (1, 2)
margins = (0, 7, -3.14)
weights = (None, make_weight([S], low=-10., high=10.))
weights = (False, True)
reductions = (None, "none", "mean", "sum")

for (input_shape, target), p, margin, weight, reduction in product(inputs, ps, margins, weights, reductions):
input = _make_tensor(input_shape)
weight_shape = [input.size(-1)] if input.ndim > 0 else [1]
weight = make_weight(weight_shape, low=-10., high=10.) if weight else None
kwargs = {"p": p, "margin": margin, "weight": weight}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More tests with different combinations of arguments.

if reduction is not None:
kwargs["reduction"] = reduction
yield SampleInput(_make_tensor(input_shape), args=(target,), kwargs=kwargs)
yield SampleInput(input, args=(target,), kwargs=kwargs)


def error_inputs_multi_margin_loss(op, device, **kwargs):
Expand Down Expand Up @@ -12806,6 +12809,13 @@ def reference_flatten(input, start_dim=0, end_dim=-1):
sample_inputs_func=sample_inputs_multi_margin_loss,
reference_inputs_func=reference_inputs_multi_margin_loss,
error_inputs_func=error_inputs_multi_margin_loss,
decorators=(
DecorateInfo(
toleranceOverride({torch.float32: tol(atol=1e-4, rtol=1e-4)}),
"TestJit",
"test_variant_consistency_jit",
),
),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests now multiply by weight, which affects accuracy here.

),
OpInfo(
"nn.functional.multilabel_margin_loss",
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.