Skip to content

Commit 6b071be

Browse files
authored
Define all C++ model constructors explicit (#2944)
* Making all model constructors explicit. * formatting.
1 parent f95b053 commit 6b071be

File tree

10 files changed

+76
-45
lines changed

10 files changed

+76
-45
lines changed

torchvision/csrc/models/alexnet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace models {
1111
struct VISION_API AlexNetImpl : torch::nn::Module {
1212
torch::nn::Sequential features{nullptr}, classifier{nullptr};
1313

14-
AlexNetImpl(int64_t num_classes = 1000);
14+
explicit AlexNetImpl(int64_t num_classes = 1000);
1515

1616
torch::Tensor forward(torch::Tensor x);
1717
};

torchvision/csrc/models/densenet.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct VISION_API DenseNetImpl : torch::nn::Module {
2323
torch::nn::Sequential features{nullptr};
2424
torch::nn::Linear classifier{nullptr};
2525

26-
DenseNetImpl(
26+
explicit DenseNetImpl(
2727
int64_t num_classes = 1000,
2828
int64_t growth_rate = 32,
2929
const std::vector<int64_t>& block_config = {6, 12, 24, 16},
@@ -35,7 +35,7 @@ struct VISION_API DenseNetImpl : torch::nn::Module {
3535
};
3636

3737
struct VISION_API DenseNet121Impl : DenseNetImpl {
38-
DenseNet121Impl(
38+
explicit DenseNet121Impl(
3939
int64_t num_classes = 1000,
4040
int64_t growth_rate = 32,
4141
const std::vector<int64_t>& block_config = {6, 12, 24, 16},
@@ -45,7 +45,7 @@ struct VISION_API DenseNet121Impl : DenseNetImpl {
4545
};
4646

4747
struct VISION_API DenseNet169Impl : DenseNetImpl {
48-
DenseNet169Impl(
48+
explicit DenseNet169Impl(
4949
int64_t num_classes = 1000,
5050
int64_t growth_rate = 32,
5151
const std::vector<int64_t>& block_config = {6, 12, 32, 32},
@@ -55,7 +55,7 @@ struct VISION_API DenseNet169Impl : DenseNetImpl {
5555
};
5656

5757
struct VISION_API DenseNet201Impl : DenseNetImpl {
58-
DenseNet201Impl(
58+
explicit DenseNet201Impl(
5959
int64_t num_classes = 1000,
6060
int64_t growth_rate = 32,
6161
const std::vector<int64_t>& block_config = {6, 12, 48, 32},
@@ -65,7 +65,7 @@ struct VISION_API DenseNet201Impl : DenseNetImpl {
6565
};
6666

6767
struct VISION_API DenseNet161Impl : DenseNetImpl {
68-
DenseNet161Impl(
68+
explicit DenseNet161Impl(
6969
int64_t num_classes = 1000,
7070
int64_t growth_rate = 48,
7171
const std::vector<int64_t>& block_config = {6, 12, 36, 24},

torchvision/csrc/models/googlenet.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct VISION_API BasicConv2dImpl : torch::nn::Module {
1212
torch::nn::Conv2d conv{nullptr};
1313
torch::nn::BatchNorm2d bn{nullptr};
1414

15-
BasicConv2dImpl(torch::nn::Conv2dOptions options);
15+
explicit BasicConv2dImpl(torch::nn::Conv2dOptions options);
1616

1717
torch::Tensor forward(torch::Tensor x);
1818
};
@@ -71,7 +71,7 @@ struct VISION_API GoogLeNetImpl : torch::nn::Module {
7171
torch::nn::Dropout dropout{nullptr};
7272
torch::nn::Linear fc{nullptr};
7373

74-
GoogLeNetImpl(
74+
explicit GoogLeNetImpl(
7575
int64_t num_classes = 1000,
7676
bool aux_logits = true,
7777
bool transform_input = false,

torchvision/csrc/models/inception.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ struct VISION_API BasicConv2dImpl : torch::nn::Module {
1111
torch::nn::Conv2d conv{nullptr};
1212
torch::nn::BatchNorm2d bn{nullptr};
1313

14-
BasicConv2dImpl(torch::nn::Conv2dOptions options, double std_dev = 0.1);
14+
explicit BasicConv2dImpl(
15+
torch::nn::Conv2dOptions options,
16+
double std_dev = 0.1);
1517

1618
torch::Tensor forward(torch::Tensor x);
1719
};
@@ -30,7 +32,7 @@ struct VISION_API InceptionAImpl : torch::nn::Module {
3032
struct VISION_API InceptionBImpl : torch::nn::Module {
3133
BasicConv2d branch3x3, branch3x3dbl_1, branch3x3dbl_2, branch3x3dbl_3;
3234

33-
InceptionBImpl(int64_t in_channels);
35+
explicit InceptionBImpl(int64_t in_channels);
3436

3537
torch::Tensor forward(const torch::Tensor& x);
3638
};
@@ -50,7 +52,7 @@ struct VISION_API InceptionDImpl : torch::nn::Module {
5052
BasicConv2d branch3x3_1, branch3x3_2, branch7x7x3_1, branch7x7x3_2,
5153
branch7x7x3_3, branch7x7x3_4;
5254

53-
InceptionDImpl(int64_t in_channels);
55+
explicit InceptionDImpl(int64_t in_channels);
5456

5557
torch::Tensor forward(const torch::Tensor& x);
5658
};
@@ -60,7 +62,7 @@ struct VISION_API InceptionEImpl : torch::nn::Module {
6062
branch3x3dbl_1, branch3x3dbl_2, branch3x3dbl_3a, branch3x3dbl_3b,
6163
branch_pool;
6264

63-
InceptionEImpl(int64_t in_channels);
65+
explicit InceptionEImpl(int64_t in_channels);
6466

6567
torch::Tensor forward(const torch::Tensor& x);
6668
};
@@ -110,7 +112,7 @@ struct VISION_API InceptionV3Impl : torch::nn::Module {
110112

111113
_inceptionimpl::InceptionAux AuxLogits{nullptr};
112114

113-
InceptionV3Impl(
115+
explicit InceptionV3Impl(
114116
int64_t num_classes = 1000,
115117
bool aux_logits = true,
116118
bool transform_input = false);

torchvision/csrc/models/mnasnet.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,28 @@ struct VISION_API MNASNetImpl : torch::nn::Module {
1111

1212
void _initialize_weights();
1313

14-
MNASNetImpl(double alpha, int64_t num_classes = 1000, double dropout = .2);
14+
explicit MNASNetImpl(
15+
double alpha,
16+
int64_t num_classes = 1000,
17+
double dropout = .2);
1518

1619
torch::Tensor forward(torch::Tensor x);
1720
};
1821

1922
struct MNASNet0_5Impl : MNASNetImpl {
20-
MNASNet0_5Impl(int64_t num_classes = 1000, double dropout = .2);
23+
explicit MNASNet0_5Impl(int64_t num_classes = 1000, double dropout = .2);
2124
};
2225

2326
struct MNASNet0_75Impl : MNASNetImpl {
24-
MNASNet0_75Impl(int64_t num_classes = 1000, double dropout = .2);
27+
explicit MNASNet0_75Impl(int64_t num_classes = 1000, double dropout = .2);
2528
};
2629

2730
struct MNASNet1_0Impl : MNASNetImpl {
28-
MNASNet1_0Impl(int64_t num_classes = 1000, double dropout = .2);
31+
explicit MNASNet1_0Impl(int64_t num_classes = 1000, double dropout = .2);
2932
};
3033

3134
struct MNASNet1_3Impl : MNASNetImpl {
32-
MNASNet1_3Impl(int64_t num_classes = 1000, double dropout = .2);
35+
explicit MNASNet1_3Impl(int64_t num_classes = 1000, double dropout = .2);
3336
};
3437

3538
TORCH_MODULE(MNASNet);

torchvision/csrc/models/mobilenet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ struct VISION_API MobileNetV2Impl : torch::nn::Module {
1010
int64_t last_channel;
1111
torch::nn::Sequential features, classifier;
1212

13-
MobileNetV2Impl(
13+
explicit MobileNetV2Impl(
1414
int64_t num_classes = 1000,
1515
double width_mult = 1.0,
1616
std::vector<std::vector<int64_t>> inverted_residual_settings = {},

torchvision/csrc/models/resnet.h

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ struct ResNetImpl : torch::nn::Module {
8080
int64_t blocks,
8181
int64_t stride = 1);
8282

83-
ResNetImpl(
83+
explicit ResNetImpl(
8484
const std::vector<int>& layers,
8585
int64_t num_classes = 1000,
8686
bool zero_init_residual = false,
@@ -186,45 +186,55 @@ torch::Tensor ResNetImpl<Block>::forward(torch::Tensor x) {
186186
}
187187

188188
struct VISION_API ResNet18Impl : ResNetImpl<_resnetimpl::BasicBlock> {
189-
ResNet18Impl(int64_t num_classes = 1000, bool zero_init_residual = false);
189+
explicit ResNet18Impl(
190+
int64_t num_classes = 1000,
191+
bool zero_init_residual = false);
190192
};
191193

192194
struct VISION_API ResNet34Impl : ResNetImpl<_resnetimpl::BasicBlock> {
193-
ResNet34Impl(int64_t num_classes = 1000, bool zero_init_residual = false);
195+
explicit ResNet34Impl(
196+
int64_t num_classes = 1000,
197+
bool zero_init_residual = false);
194198
};
195199

196200
struct VISION_API ResNet50Impl : ResNetImpl<_resnetimpl::Bottleneck> {
197-
ResNet50Impl(int64_t num_classes = 1000, bool zero_init_residual = false);
201+
explicit ResNet50Impl(
202+
int64_t num_classes = 1000,
203+
bool zero_init_residual = false);
198204
};
199205

200206
struct VISION_API ResNet101Impl : ResNetImpl<_resnetimpl::Bottleneck> {
201-
ResNet101Impl(int64_t num_classes = 1000, bool zero_init_residual = false);
207+
explicit ResNet101Impl(
208+
int64_t num_classes = 1000,
209+
bool zero_init_residual = false);
202210
};
203211

204212
struct VISION_API ResNet152Impl : ResNetImpl<_resnetimpl::Bottleneck> {
205-
ResNet152Impl(int64_t num_classes = 1000, bool zero_init_residual = false);
213+
explicit ResNet152Impl(
214+
int64_t num_classes = 1000,
215+
bool zero_init_residual = false);
206216
};
207217

208218
struct VISION_API ResNext50_32x4dImpl : ResNetImpl<_resnetimpl::Bottleneck> {
209-
ResNext50_32x4dImpl(
219+
explicit ResNext50_32x4dImpl(
210220
int64_t num_classes = 1000,
211221
bool zero_init_residual = false);
212222
};
213223

214224
struct VISION_API ResNext101_32x8dImpl : ResNetImpl<_resnetimpl::Bottleneck> {
215-
ResNext101_32x8dImpl(
225+
explicit ResNext101_32x8dImpl(
216226
int64_t num_classes = 1000,
217227
bool zero_init_residual = false);
218228
};
219229

220230
struct VISION_API WideResNet50_2Impl : ResNetImpl<_resnetimpl::Bottleneck> {
221-
WideResNet50_2Impl(
231+
explicit WideResNet50_2Impl(
222232
int64_t num_classes = 1000,
223233
bool zero_init_residual = false);
224234
};
225235

226236
struct VISION_API WideResNet101_2Impl : ResNetImpl<_resnetimpl::Bottleneck> {
227-
WideResNet101_2Impl(
237+
explicit WideResNet101_2Impl(
228238
int64_t num_classes = 1000,
229239
bool zero_init_residual = false);
230240
};

torchvision/csrc/models/shufflenetv2.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@ struct VISION_API ShuffleNetV2Impl : torch::nn::Module {
2121
};
2222

2323
struct VISION_API ShuffleNetV2_x0_5Impl : ShuffleNetV2Impl {
24-
ShuffleNetV2_x0_5Impl(int64_t num_classes = 1000);
24+
explicit ShuffleNetV2_x0_5Impl(int64_t num_classes = 1000);
2525
};
2626

2727
struct VISION_API ShuffleNetV2_x1_0Impl : ShuffleNetV2Impl {
28-
ShuffleNetV2_x1_0Impl(int64_t num_classes = 1000);
28+
explicit ShuffleNetV2_x1_0Impl(int64_t num_classes = 1000);
2929
};
3030

3131
struct VISION_API ShuffleNetV2_x1_5Impl : ShuffleNetV2Impl {
32-
ShuffleNetV2_x1_5Impl(int64_t num_classes = 1000);
32+
explicit ShuffleNetV2_x1_5Impl(int64_t num_classes = 1000);
3333
};
3434

3535
struct VISION_API ShuffleNetV2_x2_0Impl : ShuffleNetV2Impl {
36-
ShuffleNetV2_x2_0Impl(int64_t num_classes = 1000);
36+
explicit ShuffleNetV2_x2_0Impl(int64_t num_classes = 1000);
3737
};
3838

3939
TORCH_MODULE(ShuffleNetV2);

torchvision/csrc/models/squeezenet.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ struct VISION_API SqueezeNetImpl : torch::nn::Module {
1010
int64_t num_classes;
1111
torch::nn::Sequential features{nullptr}, classifier{nullptr};
1212

13-
SqueezeNetImpl(double version = 1.0, int64_t num_classes = 1000);
13+
explicit SqueezeNetImpl(double version = 1.0, int64_t num_classes = 1000);
1414

1515
torch::Tensor forward(torch::Tensor x);
1616
};
@@ -19,15 +19,15 @@ struct VISION_API SqueezeNetImpl : torch::nn::Module {
1919
// accuracy with 50x fewer parameters and <0.5MB model size"
2020
// <https://arxiv.org/abs/1602.07360> paper.
2121
struct VISION_API SqueezeNet1_0Impl : SqueezeNetImpl {
22-
SqueezeNet1_0Impl(int64_t num_classes = 1000);
22+
explicit SqueezeNet1_0Impl(int64_t num_classes = 1000);
2323
};
2424

2525
// SqueezeNet 1.1 model from the official SqueezeNet repo
2626
// <https://github.com/DeepScale/SqueezeNet/tree/master/SqueezeNet_v1.1>.
2727
// SqueezeNet 1.1 has 2.4x less computation and slightly fewer parameters
2828
// than SqueezeNet 1.0, without sacrificing accuracy.
2929
struct VISION_API SqueezeNet1_1Impl : SqueezeNetImpl {
30-
SqueezeNet1_1Impl(int64_t num_classes = 1000);
30+
explicit SqueezeNet1_1Impl(int64_t num_classes = 1000);
3131
};
3232

3333
TORCH_MODULE(SqueezeNet);

torchvision/csrc/models/vgg.h

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ struct VISION_API VGGImpl : torch::nn::Module {
1111

1212
void _initialize_weights();
1313

14-
VGGImpl(
14+
explicit VGGImpl(
1515
const torch::nn::Sequential& features,
1616
int64_t num_classes = 1000,
1717
bool initialize_weights = true);
@@ -21,42 +21,58 @@ struct VISION_API VGGImpl : torch::nn::Module {
2121

2222
// VGG 11-layer model (configuration "A")
2323
struct VISION_API VGG11Impl : VGGImpl {
24-
VGG11Impl(int64_t num_classes = 1000, bool initialize_weights = true);
24+
explicit VGG11Impl(
25+
int64_t num_classes = 1000,
26+
bool initialize_weights = true);
2527
};
2628

2729
// VGG 13-layer model (configuration "B")
2830
struct VISION_API VGG13Impl : VGGImpl {
29-
VGG13Impl(int64_t num_classes = 1000, bool initialize_weights = true);
31+
explicit VGG13Impl(
32+
int64_t num_classes = 1000,
33+
bool initialize_weights = true);
3034
};
3135

3236
// VGG 16-layer model (configuration "D")
3337
struct VISION_API VGG16Impl : VGGImpl {
34-
VGG16Impl(int64_t num_classes = 1000, bool initialize_weights = true);
38+
explicit VGG16Impl(
39+
int64_t num_classes = 1000,
40+
bool initialize_weights = true);
3541
};
3642

3743
// VGG 19-layer model (configuration "E")
3844
struct VISION_API VGG19Impl : VGGImpl {
39-
VGG19Impl(int64_t num_classes = 1000, bool initialize_weights = true);
45+
explicit VGG19Impl(
46+
int64_t num_classes = 1000,
47+
bool initialize_weights = true);
4048
};
4149

4250
// VGG 11-layer model (configuration "A") with batch normalization
4351
struct VISION_API VGG11BNImpl : VGGImpl {
44-
VGG11BNImpl(int64_t num_classes = 1000, bool initialize_weights = true);
52+
explicit VGG11BNImpl(
53+
int64_t num_classes = 1000,
54+
bool initialize_weights = true);
4555
};
4656

4757
// VGG 13-layer model (configuration "B") with batch normalization
4858
struct VISION_API VGG13BNImpl : VGGImpl {
49-
VGG13BNImpl(int64_t num_classes = 1000, bool initialize_weights = true);
59+
explicit VGG13BNImpl(
60+
int64_t num_classes = 1000,
61+
bool initialize_weights = true);
5062
};
5163

5264
// VGG 16-layer model (configuration "D") with batch normalization
5365
struct VISION_API VGG16BNImpl : VGGImpl {
54-
VGG16BNImpl(int64_t num_classes = 1000, bool initialize_weights = true);
66+
explicit VGG16BNImpl(
67+
int64_t num_classes = 1000,
68+
bool initialize_weights = true);
5569
};
5670

5771
// VGG 19-layer model (configuration 'E') with batch normalization
5872
struct VISION_API VGG19BNImpl : VGGImpl {
59-
VGG19BNImpl(int64_t num_classes = 1000, bool initialize_weights = true);
73+
explicit VGG19BNImpl(
74+
int64_t num_classes = 1000,
75+
bool initialize_weights = true);
6076
};
6177

6278
TORCH_MODULE(VGG);

0 commit comments

Comments
 (0)