mirror of
https://github.com/pytorch/pytorch.git
synced 2026-11-20 09:04:25 +08:00
[C++ API] Remove deprecated torch::nn::BatchNorm / FeatureDropout / modules_ordered_dict and torch::nn::init::Nonlinearity / FanMode (#34508)
Summary:
This PR is BC-breaking in the following way:
- The deprecated `torch::nn::BatchNorm` is removed in favor of `torch::nn::BatchNorm{1,2,3}d`
- The deprecated `torch::nn::FeatureDropout` is removed in favor of `torch::nn::Dropout{2,3}d`
- The deprecated `torch::nn::modules_ordered_dict` is removed. User should do `Sequential sequential({{"m1", MyModule(1)}, {"m2", MyModule(2)}})` instead.
- The deprecated `torch::nn::init::Nonlinearity` is removed, in favor of the following enums:
- `torch::kLinear`
- `torch::kConv1D`
- `torch::kConv2D`
- `torch::kConv3D`
- `torch::kConvTranspose1D`
- `torch::kConvTranspose2D`
- `torch::kConvTranspose3D`
- `torch::kSigmoid`
- `torch::kTanh`
- `torch::kReLU`
- `torch::kLeakyReLU`
- The deprecated `torch::nn::init::FanMode` is removed, in favor of the following enums:
- `torch::kFanIn`
- `torch::kFanOut`
Pull Request resolved: https://github.com/pytorch/pytorch/pull/34508
Differential Revision: D20351601
Pulled By: yf225
fbshipit-source-id: cca0cd112f29a31bb023e348ca8f82780e42bea3
This commit is contained in:
committed by
Facebook GitHub Bot
parent
e95657b87e
commit
a54416d208
+3
-41
@@ -111,19 +111,19 @@ TEST(InitTest, CanInitializeTensorThatRequiresGrad) {
|
||||
|
||||
TEST(InitTest, CalculateGainWithTanh) {
|
||||
double gain =
|
||||
torch::nn::init::calculate_gain(torch::nn::init::Nonlinearity::Tanh);
|
||||
torch::nn::init::calculate_gain(torch::kTanh);
|
||||
ASSERT_DOUBLE_EQ(gain, 5.0 / 3.0);
|
||||
}
|
||||
|
||||
TEST(InitTest, CalculateGainWithRelu) {
|
||||
double gain =
|
||||
torch::nn::init::calculate_gain(torch::nn::init::Nonlinearity::ReLU);
|
||||
torch::nn::init::calculate_gain(torch::kReLU);
|
||||
ASSERT_DOUBLE_EQ(gain, std::sqrt(2.0));
|
||||
}
|
||||
|
||||
TEST(InitTest, CalculateGainWithLeakyRelu) {
|
||||
double gain =
|
||||
torch::nn::init::calculate_gain(torch::nn::init::Nonlinearity::LeakyReLU);
|
||||
torch::nn::init::calculate_gain(torch::kLeakyReLU);
|
||||
ASSERT_DOUBLE_EQ(gain, std::sqrt(2.0 / (1 + pow(0.01, 2))));
|
||||
}
|
||||
|
||||
@@ -131,41 +131,3 @@ 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