Skip to content

[Contributor Welcome] Implement C++ API version of torch.nn.functional.normalize #27048

@yf225

Description

@yf225

Context

We would like to add torch::nn::functional::normalize to the C++ API, so that C++ users can easily find the equivalent of Python API torch.nn.functional.normalize.

Steps

  • Add torch::nn::NormalizeOptions to torch/csrc/api/include/torch/nn/options/normalization.h (add this file if it doesn’t exist), which should include the following parameters (based on https://pytorch.org/docs/stable/nn.functional.html#torch.nn.functional.normalize)
    • TORCH_ARG(double, p) = 2.0
    • TORCH_ARG(int64_t, dim) = 1
    • TORCH_ARG(double, eps) = 1e-12
    • 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 the exponent value in the norm formulation. Default: 2
  • Add torch::nn::functional::normalize(...) in torch/csrc/api/include/torch/nn/functional/normalization.h (add this file if it doesn’t exist). The function should have the following signature:
namespace torch {
namespace nn {
namespace functional {

inline Tensor normalize(
    const Tensor& input,
    const NormalizeOptions& options = {},
    c10::optional<Tensor> out = c10::nullopt) {
  ...
}

} // namespace functional
} // namespace nn
} // namespace torch
  • Add test for torch::nn::functional::normalize(...) in test/cpp/api/functional.cpp. It can just contain tests for both “out is null” and “out is non-null” cases, and makes sure that for “out is null” case the function is differentiable. (We should get the expected values from the corresponding Python version.)

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

good 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