mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-07 00:21:07 +01:00
Summary: In the C++ API, `Sequential` currently was not refcounted itself, but stored `shared_ptr<AnyModule>` to get the reference semantics. This is unfortunate because most modules in the API are accessed via `->`, e.g. `Linear l(1, 2); l->forward(...);`. `Sequential` was different in that it had value semantics itself, thus was accessed via `.`. This PR makes `Sequential` store `AnyModule` (without extra indirection), and uses the same pImpl mechanism we use for all other modules to make `Sequential` have reference semantics itself. This makes it consistent with the rest of the library. It also removes one level of indirection inside of `Sequential`, which is cool. One thing I had to change was that the `ModuleHolder` with which the whole pImpl thing is implemented previously did some tricks to make `Linear(3, 4)` actually construct `Linear(LinearOptions(3, 4))`. This doesn't work well with `Sequential` since it takes a variadic parameter pack. Instead, I made `ModuleHolder` forward all arguments to the underlying module, and then further pushed the trick to forward parameters to modules' options types into the actual Modules. This adds one constructor per Module in the library. This is not something user modules have to do (unless they want this nice forwarding themselves). It makes the code simpler overall. ezyang ebetica apaszke Pull Request resolved: https://github.com/pytorch/pytorch/pull/9151 Reviewed By: ezyang Differential Revision: D8809298 Pulled By: goldsborough fbshipit-source-id: da68452c3de912fbc67af330ba93b5220de6909f
65 lines
1.9 KiB
C++
65 lines
1.9 KiB
C++
#include <catch.hpp>
|
|
|
|
#include <torch/detail/static.h>
|
|
#include <torch/nn/module.h>
|
|
#include <torch/nn/modules/any.h>
|
|
#include <torch/nn/modules/linear.h>
|
|
|
|
#include <torch/csrc/utils/variadic.h>
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
template <
|
|
typename T,
|
|
typename = torch::enable_if_t<!torch::detail::is_module<T>::value>>
|
|
bool f(T&& m) {
|
|
return false;
|
|
}
|
|
|
|
template <typename T>
|
|
torch::detail::enable_if_module_t<T, bool> f(T&& m) {
|
|
return true;
|
|
}
|
|
|
|
TEST_CASE("static") {
|
|
SECTION("all_of") {
|
|
REQUIRE(torch::all_of<>::value == true);
|
|
REQUIRE(torch::all_of<true>::value == true);
|
|
REQUIRE(torch::all_of<true, true, true>::value == true);
|
|
REQUIRE(torch::all_of<false>::value == false);
|
|
REQUIRE(torch::all_of<false, false, false>::value == false);
|
|
REQUIRE(torch::all_of<true, true, false>::value == false);
|
|
}
|
|
SECTION("any_of") {
|
|
REQUIRE(torch::any_of<>::value == false);
|
|
REQUIRE(torch::any_of<true>::value == true);
|
|
REQUIRE(torch::any_of<true, true, true>::value == true);
|
|
REQUIRE(torch::any_of<false>::value == false);
|
|
REQUIRE(torch::any_of<true, true, false>::value == true);
|
|
}
|
|
SECTION("enable_if_module_t") {
|
|
REQUIRE(f(torch::nn::LinearImpl(1, 2)) == true);
|
|
REQUIRE(f(5) == false);
|
|
}
|
|
SECTION("check_not_lvalue_references") {
|
|
REQUIRE(torch::detail::check_not_lvalue_references<int>() == true);
|
|
REQUIRE(
|
|
torch::detail::check_not_lvalue_references<float, int, char>() == true);
|
|
REQUIRE(
|
|
torch::detail::check_not_lvalue_references<float, int&, char>() ==
|
|
false);
|
|
REQUIRE(torch::detail::check_not_lvalue_references<std::string>() == true);
|
|
REQUIRE(
|
|
torch::detail::check_not_lvalue_references<std::string&>() == false);
|
|
}
|
|
SECTION("apply") {
|
|
std::vector<int> v;
|
|
torch::apply([&v](int x) { v.push_back(x); }, 1, 2, 3, 4, 5);
|
|
REQUIRE(v.size() == 5);
|
|
for (size_t i = 0; i < v.size(); ++i) {
|
|
REQUIRE(v.at(i) == 1 + i);
|
|
}
|
|
}
|
|
}
|