mirror of
https://github.com/pytorch/pytorch.git
synced 2026-11-20 09:04:25 +08:00
Use c10::variant-based enums for Nonlinearity and FanMode
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/27933 Test Plan: Imported from OSS Differential Revision: D18009044 Pulled By: yf225 fbshipit-source-id: e88229ee30badf7a699f62af61d1e88debc0dc7d
This commit is contained in:
committed by
Facebook Github Bot
parent
a1e14a6626
commit
eb4bb00a9c
@@ -8,6 +8,8 @@
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
|
||||
using namespace torch::test;
|
||||
|
||||
void check_exact_values(
|
||||
const std::vector<torch::Tensor>& parameters,
|
||||
const std::vector<std::vector<torch::Tensor>>& expected_parameters) {
|
||||
@@ -127,4 +129,42 @@ TEST(InitTest, CalculateGainWithLeakyRelu) {
|
||||
TEST(InitTest, CanInitializeCnnWithOrthogonal) {
|
||||
torch::nn::Conv2d conv_layer(torch::nn::Conv2dOptions(3, 2, 3).stride(2));
|
||||
torch::nn::init::orthogonal_(conv_layer->named_parameters()["weight"]);
|
||||
}
|
||||
|
||||
#define NONLINEARITY_ENUM_LEGACY_WARNING_CHECK(func_name, enum_name, enum_torch_kname) \
|
||||
{ \
|
||||
std::stringstream buffer; \
|
||||
CerrRedirect cerr_redirect(buffer.rdbuf()); \
|
||||
std::cerr << torch::nn::init::func_name(torch::nn::init::Nonlinearity::enum_name) << std::endl; \
|
||||
ASSERT_EQ(count_substr_occurrences(buffer.str(), enum_torch_kname), 1); \
|
||||
}
|
||||
|
||||
#define FANMODE_ENUM_LEGACY_WARNING_CHECK(func_name, enum_name, enum_torch_kname) \
|
||||
{ \
|
||||
std::stringstream buffer; \
|
||||
CerrRedirect cerr_redirect(buffer.rdbuf()); \
|
||||
std::cerr << torch::nn::init::func_name(torch::randn({4, 5}), 0, torch::nn::init::FanMode::enum_name) << std::endl; \
|
||||
ASSERT_EQ(count_substr_occurrences(buffer.str(), enum_torch_kname), 1); \
|
||||
}
|
||||
|
||||
TEST(InitTest, NonlinearityLegacyEnum) {
|
||||
NONLINEARITY_ENUM_LEGACY_WARNING_CHECK(calculate_gain, Linear, "torch::kLinear")
|
||||
NONLINEARITY_ENUM_LEGACY_WARNING_CHECK(calculate_gain, Conv1D, "torch::kConv1D")
|
||||
NONLINEARITY_ENUM_LEGACY_WARNING_CHECK(calculate_gain, Conv2D, "torch::kConv2D")
|
||||
NONLINEARITY_ENUM_LEGACY_WARNING_CHECK(calculate_gain, Conv3D, "torch::kConv3D")
|
||||
NONLINEARITY_ENUM_LEGACY_WARNING_CHECK(calculate_gain, ConvTranspose1D, "torch::kConvTranspose1D")
|
||||
NONLINEARITY_ENUM_LEGACY_WARNING_CHECK(calculate_gain, ConvTranspose2D, "torch::kConvTranspose2D")
|
||||
NONLINEARITY_ENUM_LEGACY_WARNING_CHECK(calculate_gain, ConvTranspose3D, "torch::kConvTranspose3D")
|
||||
NONLINEARITY_ENUM_LEGACY_WARNING_CHECK(calculate_gain, Sigmoid, "torch::kSigmoid")
|
||||
NONLINEARITY_ENUM_LEGACY_WARNING_CHECK(calculate_gain, Tanh, "torch::kTanh")
|
||||
NONLINEARITY_ENUM_LEGACY_WARNING_CHECK(calculate_gain, ReLU, "torch::kReLU")
|
||||
NONLINEARITY_ENUM_LEGACY_WARNING_CHECK(calculate_gain, LeakyReLU, "torch::kLeakyReLU")
|
||||
}
|
||||
|
||||
TEST(InitTest, FanModeLegacyEnum) {
|
||||
FANMODE_ENUM_LEGACY_WARNING_CHECK(kaiming_normal_, FanIn, "torch::kFanIn")
|
||||
FANMODE_ENUM_LEGACY_WARNING_CHECK(kaiming_normal_, FanOut, "torch::kFanOut")
|
||||
|
||||
FANMODE_ENUM_LEGACY_WARNING_CHECK(kaiming_uniform_, FanIn, "torch::kFanIn")
|
||||
FANMODE_ENUM_LEGACY_WARNING_CHECK(kaiming_uniform_, FanOut, "torch::kFanOut")
|
||||
}
|
||||
Reference in New Issue
Block a user