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

Conversation

@pytorch-bot
Copy link

pytorch-bot bot commented Jul 9, 2023

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/104852

Note: Links to docs will display an error until the docs builds have been completed.

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

❌ 1 New Failure

As of commit 0966cc1:

NEW FAILURE - The following job has failed:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

nkaretnikov added a commit that referenced this pull request Jul 9, 2023
…d tests

ghstack-source-id: deae4cd19cdde371632b0f2c00addca770458579
Pull Request resolved: #104852
@nkaretnikov nkaretnikov added ciflow/trunk Trigger trunk jobs on your pull request topic: not user facing topic category ciflow/periodic Trigger jobs ran periodically on master (periodic.yml) on the PR ciflow/inductor ciflow/slow labels Jul 9, 2023
@nkaretnikov
Copy link
Collaborator Author

Summary:

  • There was no shape check for weight, so this could access arbitrary memory.
  • On CPU, weight wasn't contiguous unlike on CUDA. No one noticed because there were no tests for weight.
  • Added tests for weight.

Tensor weight_contiguous;
if (weight && weight->defined()) {
weight_contiguous = weight->contiguous();
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

For compat with multi_margin_loss_cuda_out:

  Tensor weights;
  if (weights_ && weights_->defined()) {
    weights = weights_->contiguous();
  }

@@ -103,15 +103,15 @@ void multi_margin_loss_out_cpu_template(
const Tensor& target,
int p,
const Scalar& margin,
const Tensor& weight,
const c10::optional<Tensor>& 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.

multi_margin_loss_shape_check accepts an optional Tensor because weight is an optional on CUDA. So I change this (and elsewhere) to be consistent.

// See [Note: hacky wrapper removal for optional tensor]
c10::MaybeOwned<Tensor> weight_maybe_owned = at::borrow_from_optional_tensor(weight_opt);
const Tensor& weight = *weight_maybe_owned;

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Here and elsewhere: to have a consistent API for multi_margin_loss_shape_check (it accepts optional weight on CPU and CUDA).

weight->dim() <= 1 && weight->numel() == dim,
"inconsistent weight size, expected ", dim, " but got ",
weight->sizes());
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

From the docs: "a manual rescaling weight given to each class. If given, it has to be a Tensor of size C. Otherwise, it is treated as if having all ones." But target here can be a scalar, so could it be useful to allow scalars for weight?

https://pytorch.org/docs/stable/generated/torch.nn.MultiMarginLoss.html

@@ -1405,6 +1406,7 @@ def sample_inputs_multi_margin_loss(op_info, device, dtype, requires_grad, **kwa
((S, M), make_target([S], low=0, high=M), {"margin": 1.0}),
((S, M), make_target([S], low=0, high=M), {"margin": -3.14}),
((M, S), make_target([M], low=0, high=S), {"weight": None}),
((M, S), make_target([M], low=0, high=S), {"weight": make_weight([S], low=-10., high=10.)}),
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

weight wasn't tested before at all.

for (input_shape, target), p, margin, reduction in product(inputs, ps, margins, reductions):
kwargs = {"p": p, "margin": margin}
for (input_shape, target), p, margin, weight, reduction in product(inputs, ps, margins, weights, reductions):
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.

yield ErrorInput(SampleInput(make_input(5, 4), args=(make_input(5,),), kwargs={'weight': make_input(())}),
error_type=ValueError, error_regex='weight must be one-dimensional')
yield ErrorInput(SampleInput(make_input(5, 4), args=(make_input(5,),), kwargs={'weight': make_input(5, 4)}),
error_type=ValueError, error_regex='weight must be one-dimensional')
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This "one-dimensional" error is only on the Python side. See my comment above, maybe worth removing this restriction from the Python side and allow scalars?

yield ErrorInput(SampleInput(make_input(5, 4), args=(make_input(5,),), kwargs={'weight': make_input(5, 4)}),
error_type=ValueError, error_regex='weight must be one-dimensional')
yield ErrorInput(SampleInput(make_input(5, 4), args=(make_input(5,),), kwargs={'weight': make_input(5,)}),
error_type=RuntimeError, error_regex=r'inconsistent weight size, expected 4 but got \[5\]')
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Raises an error now.

nkaretnikov added a commit that referenced this pull request Jul 12, 2023
…d tests

ghstack-source-id: 585ed2c4981b8327d1ad1301c896cf8f8a63c0ea
Pull Request resolved: #104852
"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.

@nkaretnikov nkaretnikov requested a review from ezyang July 13, 2023 01:17
@nkaretnikov nkaretnikov marked this pull request as ready for review July 13, 2023 01:18
@nkaretnikov nkaretnikov removed the ciflow/periodic Trigger jobs ran periodically on master (periodic.yml) on the PR label Jul 13, 2023
@facebook-github-bot facebook-github-bot deleted the gh/nkaretnikov/151/head branch July 18, 2023 14:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ciflow/inductor ciflow/trunk Trigger trunk jobs on your pull request Merged open source topic: not user facing topic category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants