Skip to content

[Contributor Welcome] Implement C++ API version of torch.nn.MultiMarginLoss #27198

@yf225

Description

@yf225

Context

We would like to add the following two APIs to the C++ frontend:

  • torch::nn::MultiMarginLoss , which is the equivalent of Python API torch.nn.MultiMarginLoss.
  • torch::nn::functional::multi_margin_loss, which is the equivalent of Python API torch.nn.functional.multi_margin_loss.

Steps

  • Add torch::nn::MultiMarginLossOptions to torch/csrc/api/include/torch/nn/options/loss.h (add this file if it doesn’t exist), which should include the following parameters (based on https://pytorch.org/docs/stable/nn.html#torch.nn.MultiMarginLoss)
    • TORCH_ARG(int64_t, p) = 1
    • TORCH_ARG(double, margin) = 1.0
    • TORCH_ARG(Tensor, weight) = Tensor()
    • TORCH_ARG(Reduction::Reduction, reduction) = Reduction::Mean
    • NOTE: please make sure to add the same comments for the parameters as in the Python version. For example, for parameter p we should say Has a default value of 1. 1 and 2 are the only supported values.
  • Add torch::nn::functional::multi_margin_loss(...) in torch/csrc/api/include/torch/nn/functional/loss.h (add this file if it doesn’t exist). The function should have the following signature:
namespace torch {
namespace nn {
namespace functional {

inline Tensor multi_margin_loss(
    const Tensor& input,
    const Tensor& target,
    const MultiMarginLossOptions& options = {}) {
  ...
}

} // namespace functional
} // namespace nn
} // namespace torch
  • Add torch::nn::MultiMarginLoss in torch/csrc/api/include/torch/nn/modules/loss.h (add this file if it doesn’t exist). The module’s constructor should take torch::nn::MultiMarginLossOptions as input (and store it in its internal options field), the module’s reset() method should register weight as a buffer (same logic as
    self.register_buffer('weight', weight)
    ), and the module’s forward method should have the following signature:
Tensor forward(
    const Tensor& input,
    const Tensor& target) {
  // Should call torch::nn::functional::multi_margin_loss with all inputs and the module's options
}
  • Add test for torch::nn::MultiMarginLoss in test/cpp/api/modules.cpp. We can just check that the output value for some simple cases are as expected.

  • Add test for torch::nn::functional::multi_margin_loss(...) in test/cpp/api/functional.cpp, using essentially the same tests as torch::nn::MultiMarginLoss .

Helpful Resources

There are quite a few PRs for adding new functionals / new modules for the C++ API (the list of PRs is in #25883), which can serve as great references. Also please ping @yf225 on this issue if you encounter any problems.

How do I claim this feature request?

Please comment in this issue if you are interested in working on it.

cc @yf225

Metadata

Metadata

Assignees

Labels

enhancementNot as big of a feature, but technically not a bug. Should be easy to fixgood first issuemodule: cppRelated to C++ APItriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions