1 #include <gtest/gtest.h> 3 #include <torch/detail/static.h> 4 #include <torch/nn/module.h> 5 #include <torch/nn/modules/any.h> 6 #include <torch/nn/modules/linear.h> 8 #include <torch/csrc/utils/variadic.h> 15 typename = torch::enable_if_t<!torch::detail::is_module<T>::value>>
21 torch::detail::enable_if_module_t<T, bool> f(
T&& m) {
25 TEST(TestStatic, AllOf) {
34 TEST(TestStatic, AnyOf) {
41 TEST(TestStatic, EnableIfModule) {
44 ASSERT_TRUE(torch::detail::check_not_lvalue_references<int>());
45 ASSERT_TRUE((torch::detail::check_not_lvalue_references<float, int, char>()));
47 (torch::detail::check_not_lvalue_references<float, int&, char>()));
48 ASSERT_TRUE(torch::detail::check_not_lvalue_references<std::string>());
49 ASSERT_FALSE(torch::detail::check_not_lvalue_references<std::string&>());
80 template <
typename Module,
typename ExpectedType,
typename... Args>
81 void assert_has_expected_type() {
84 constexpr
bool is_expected_type =
85 std::is_same<ReturnType, ExpectedType>::value;
86 ASSERT_TRUE(is_expected_type) << Module().name();
89 TEST(TestStatic, ReturnTypeOfForward) {
90 assert_has_expected_type<A, int>();
91 assert_has_expected_type<B, std::string, torch::Tensor>();
92 assert_has_expected_type<C, float, torch::Tensor&>();
93 assert_has_expected_type<D, char, torch::Tensor&&>();
94 assert_has_expected_type<E, void>();
97 TEST(TestStatic, Apply) {
99 torch::apply([&v](
int x) { v.push_back(x); }, 1, 2, 3, 4, 5);
100 ASSERT_EQ(v.size(), 5);
101 for (
size_t i = 0; i < v.size(); ++i) {
102 ASSERT_EQ(v.at(i), i + 1);
Applies a linear transformation with optional bias.
The base class for all modules in PyTorch.