:github_url: https://github.com/pytorch/pytorch .. _program_listing_file_torch_csrc_api_include_torch_nn_modules_utils.h: Program Listing for File utils.h ================================ |exhale_lsh| :ref:`Return to documentation for file ` (``torch/csrc/api/include/torch/nn/modules/utils.h``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp #pragma once #include #include #include #include namespace torch::nn::modules::utils { // Reverse the order of `t` and repeat each element for `n` times. // This can be used to translate padding arg used by Conv and Pooling modules // to the ones used by `F::pad`. // // This mirrors `_reverse_repeat_tuple` in `torch/nn/modules/utils.py`. inline std::vector _reverse_repeat_vector( c10::ArrayRef t, int64_t n) { TORCH_INTERNAL_ASSERT(n >= 0); std::vector ret; ret.reserve(t.size() * n); for (auto rit = t.rbegin(); rit != t.rend(); ++rit) { for ([[maybe_unused]] const auto i : c10::irange(n)) { ret.emplace_back(*rit); } } return ret; } inline std::vector _list_with_default( c10::ArrayRef> out_size, c10::IntArrayRef defaults) { TORCH_CHECK( defaults.size() > out_size.size(), "Input dimension should be at least ", out_size.size() + 1); std::vector ret; c10::IntArrayRef defaults_slice = defaults.slice(defaults.size() - out_size.size(), out_size.size()); for (const auto i : c10::irange(out_size.size())) { auto v = out_size.at(i); auto d = defaults_slice.at(i); ret.emplace_back(v.has_value() ? v.value() : d); } return ret; } } // namespace torch::nn::modules::utils