Skip to content

Commit e987d1c

Browse files
authored
Static Analysis corrections on c++ model classes (#2893)
* Minor refactoring based on static analysis on the code of models: - Convert unnecessary value parameter to constant reference. - Use move to avoid unnecessary copies. - Eliminate unused include. * Replace moves with const references. * Fixing formatting. * Remove explicit declaration on constructors.
1 parent 6092764 commit e987d1c

File tree

9 files changed

+27
-28
lines changed

9 files changed

+27
-28
lines changed

torchvision/csrc/models/densenet.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ struct _TransitionImpl : torch::nn::SequentialImpl {
7676
torch::nn::Conv2d(Options(num_input_features, num_output_features, 1)
7777
.stride(1)
7878
.bias(false)));
79-
push_back("pool", torch::nn::Functional([](torch::Tensor input) {
79+
push_back("pool", torch::nn::Functional([](const torch::Tensor& input) {
8080
return torch::avg_pool2d(input, 2, 2, 0, false, true);
8181
}));
8282
}
@@ -91,7 +91,7 @@ TORCH_MODULE(_Transition);
9191
DenseNetImpl::DenseNetImpl(
9292
int64_t num_classes,
9393
int64_t growth_rate,
94-
std::vector<int64_t> block_config,
94+
const std::vector<int64_t>& block_config,
9595
int64_t num_init_features,
9696
int64_t bn_size,
9797
double drop_rate) {
@@ -157,7 +157,7 @@ torch::Tensor DenseNetImpl::forward(torch::Tensor x) {
157157
DenseNet121Impl::DenseNet121Impl(
158158
int64_t num_classes,
159159
int64_t growth_rate,
160-
std::vector<int64_t> block_config,
160+
const std::vector<int64_t>& block_config,
161161
int64_t num_init_features,
162162
int64_t bn_size,
163163
double drop_rate)
@@ -172,7 +172,7 @@ DenseNet121Impl::DenseNet121Impl(
172172
DenseNet169Impl::DenseNet169Impl(
173173
int64_t num_classes,
174174
int64_t growth_rate,
175-
std::vector<int64_t> block_config,
175+
const std::vector<int64_t>& block_config,
176176
int64_t num_init_features,
177177
int64_t bn_size,
178178
double drop_rate)
@@ -187,7 +187,7 @@ DenseNet169Impl::DenseNet169Impl(
187187
DenseNet201Impl::DenseNet201Impl(
188188
int64_t num_classes,
189189
int64_t growth_rate,
190-
std::vector<int64_t> block_config,
190+
const std::vector<int64_t>& block_config,
191191
int64_t num_init_features,
192192
int64_t bn_size,
193193
double drop_rate)
@@ -202,7 +202,7 @@ DenseNet201Impl::DenseNet201Impl(
202202
DenseNet161Impl::DenseNet161Impl(
203203
int64_t num_classes,
204204
int64_t growth_rate,
205-
std::vector<int64_t> block_config,
205+
const std::vector<int64_t>& block_config,
206206
int64_t num_init_features,
207207
int64_t bn_size,
208208
double drop_rate)

torchvision/csrc/models/densenet.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ struct VISION_API DenseNetImpl : torch::nn::Module {
2626
DenseNetImpl(
2727
int64_t num_classes = 1000,
2828
int64_t growth_rate = 32,
29-
std::vector<int64_t> block_config = {6, 12, 24, 16},
29+
const std::vector<int64_t>& block_config = {6, 12, 24, 16},
3030
int64_t num_init_features = 64,
3131
int64_t bn_size = 4,
3232
double drop_rate = 0);
@@ -38,7 +38,7 @@ struct VISION_API DenseNet121Impl : DenseNetImpl {
3838
DenseNet121Impl(
3939
int64_t num_classes = 1000,
4040
int64_t growth_rate = 32,
41-
std::vector<int64_t> block_config = {6, 12, 24, 16},
41+
const std::vector<int64_t>& block_config = {6, 12, 24, 16},
4242
int64_t num_init_features = 64,
4343
int64_t bn_size = 4,
4444
double drop_rate = 0);
@@ -48,7 +48,7 @@ struct VISION_API DenseNet169Impl : DenseNetImpl {
4848
DenseNet169Impl(
4949
int64_t num_classes = 1000,
5050
int64_t growth_rate = 32,
51-
std::vector<int64_t> block_config = {6, 12, 32, 32},
51+
const std::vector<int64_t>& block_config = {6, 12, 32, 32},
5252
int64_t num_init_features = 64,
5353
int64_t bn_size = 4,
5454
double drop_rate = 0);
@@ -58,7 +58,7 @@ struct VISION_API DenseNet201Impl : DenseNetImpl {
5858
DenseNet201Impl(
5959
int64_t num_classes = 1000,
6060
int64_t growth_rate = 32,
61-
std::vector<int64_t> block_config = {6, 12, 48, 32},
61+
const std::vector<int64_t>& block_config = {6, 12, 48, 32},
6262
int64_t num_init_features = 64,
6363
int64_t bn_size = 4,
6464
double drop_rate = 0);
@@ -68,7 +68,7 @@ struct VISION_API DenseNet161Impl : DenseNetImpl {
6868
DenseNet161Impl(
6969
int64_t num_classes = 1000,
7070
int64_t growth_rate = 48,
71-
std::vector<int64_t> block_config = {6, 12, 36, 24},
71+
const std::vector<int64_t>& block_config = {6, 12, 36, 24},
7272
int64_t num_init_features = 96,
7373
int64_t bn_size = 4,
7474
double drop_rate = 0);

torchvision/csrc/models/inception.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ InceptionAImpl::InceptionAImpl(int64_t in_channels, int64_t pool_features)
4949
register_module("branch_pool", branch_pool);
5050
}
5151

52-
torch::Tensor InceptionAImpl::forward(torch::Tensor x) {
52+
torch::Tensor InceptionAImpl::forward(const torch::Tensor& x) {
5353
auto branch1x1 = this->branch1x1->forward(x);
5454

5555
auto branch5x5 = this->branch5x5_1->forward(x);
@@ -76,7 +76,7 @@ InceptionBImpl::InceptionBImpl(int64_t in_channels)
7676
register_module("branch3x3dbl_3", branch3x3dbl_3);
7777
}
7878

79-
torch::Tensor InceptionBImpl::forward(torch::Tensor x) {
79+
torch::Tensor InceptionBImpl::forward(const torch::Tensor& x) {
8080
auto branch3x3 = this->branch3x3->forward(x);
8181

8282
auto branch3x3dbl = this->branch3x3dbl_1->forward(x);
@@ -115,7 +115,7 @@ InceptionCImpl::InceptionCImpl(int64_t in_channels, int64_t channels_7x7) {
115115
register_module("branch_pool", branch_pool);
116116
}
117117

118-
torch::Tensor InceptionCImpl::forward(torch::Tensor x) {
118+
torch::Tensor InceptionCImpl::forward(const torch::Tensor& x) {
119119
auto branch1x1 = this->branch1x1->forward(x);
120120

121121
auto branch7x7 = this->branch7x7_1->forward(x);
@@ -151,7 +151,7 @@ InceptionDImpl::InceptionDImpl(int64_t in_channels)
151151
register_module("branch7x7x3_4", branch7x7x3_4);
152152
}
153153

154-
torch::Tensor InceptionDImpl::forward(torch::Tensor x) {
154+
torch::Tensor InceptionDImpl::forward(const torch::Tensor& x) {
155155
auto branch3x3 = this->branch3x3_1->forward(x);
156156
branch3x3 = this->branch3x3_2->forward(branch3x3);
157157

@@ -185,7 +185,7 @@ InceptionEImpl::InceptionEImpl(int64_t in_channels)
185185
register_module("branch_pool", branch_pool);
186186
}
187187

188-
torch::Tensor InceptionEImpl::forward(torch::Tensor x) {
188+
torch::Tensor InceptionEImpl::forward(const torch::Tensor& x) {
189189
auto branch1x1 = this->branch1x1->forward(x);
190190

191191
auto branch3x3 = this->branch3x3_1->forward(x);

torchvision/csrc/models/inception.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ struct VISION_API InceptionAImpl : torch::nn::Module {
2424

2525
InceptionAImpl(int64_t in_channels, int64_t pool_features);
2626

27-
torch::Tensor forward(torch::Tensor x);
27+
torch::Tensor forward(const torch::Tensor& x);
2828
};
2929

3030
struct VISION_API InceptionBImpl : torch::nn::Module {
3131
BasicConv2d branch3x3, branch3x3dbl_1, branch3x3dbl_2, branch3x3dbl_3;
3232

3333
InceptionBImpl(int64_t in_channels);
3434

35-
torch::Tensor forward(torch::Tensor x);
35+
torch::Tensor forward(const torch::Tensor& x);
3636
};
3737

3838
struct VISION_API InceptionCImpl : torch::nn::Module {
@@ -43,7 +43,7 @@ struct VISION_API InceptionCImpl : torch::nn::Module {
4343

4444
InceptionCImpl(int64_t in_channels, int64_t channels_7x7);
4545

46-
torch::Tensor forward(torch::Tensor x);
46+
torch::Tensor forward(const torch::Tensor& x);
4747
};
4848

4949
struct VISION_API InceptionDImpl : torch::nn::Module {
@@ -52,7 +52,7 @@ struct VISION_API InceptionDImpl : torch::nn::Module {
5252

5353
InceptionDImpl(int64_t in_channels);
5454

55-
torch::Tensor forward(torch::Tensor x);
55+
torch::Tensor forward(const torch::Tensor& x);
5656
};
5757

5858
struct VISION_API InceptionEImpl : torch::nn::Module {
@@ -62,7 +62,7 @@ struct VISION_API InceptionEImpl : torch::nn::Module {
6262

6363
InceptionEImpl(int64_t in_channels);
6464

65-
torch::Tensor forward(torch::Tensor x);
65+
torch::Tensor forward(const torch::Tensor& x);
6666
};
6767

6868
struct VISION_API InceptionAuxImpl : torch::nn::Module {

torchvision/csrc/models/resnet.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ BasicBlock::BasicBlock(
2828
int64_t inplanes,
2929
int64_t planes,
3030
int64_t stride,
31-
torch::nn::Sequential downsample,
31+
const torch::nn::Sequential& downsample,
3232
int64_t groups,
3333
int64_t base_width)
3434
: stride(stride), downsample(downsample) {
@@ -57,7 +57,7 @@ Bottleneck::Bottleneck(
5757
int64_t inplanes,
5858
int64_t planes,
5959
int64_t stride,
60-
torch::nn::Sequential downsample,
60+
const torch::nn::Sequential& downsample,
6161
int64_t groups,
6262
int64_t base_width)
6363
: stride(stride), downsample(downsample) {

torchvision/csrc/models/resnet.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ struct VISION_API BasicBlock : torch::nn::Module {
3636
int64_t inplanes,
3737
int64_t planes,
3838
int64_t stride = 1,
39-
torch::nn::Sequential downsample = nullptr,
39+
const torch::nn::Sequential& downsample = nullptr,
4040
int64_t groups = 1,
4141
int64_t base_width = 64);
4242

@@ -59,7 +59,7 @@ struct VISION_API Bottleneck : torch::nn::Module {
5959
int64_t inplanes,
6060
int64_t planes,
6161
int64_t stride = 1,
62-
torch::nn::Sequential downsample = nullptr,
62+
const torch::nn::Sequential& downsample = nullptr,
6363
int64_t groups = 1,
6464
int64_t base_width = 64);
6565

torchvision/csrc/models/squeezenet.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "squeezenet.h"
22

3-
#include <limits>
43
#include "modelsimpl.h"
54

65
namespace vision {

torchvision/csrc/models/vgg.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void VGGImpl::_initialize_weights() {
5050
}
5151

5252
VGGImpl::VGGImpl(
53-
torch::nn::Sequential features,
53+
const torch::nn::Sequential& features,
5454
int64_t num_classes,
5555
bool initialize_weights) {
5656
classifier = torch::nn::Sequential(

torchvision/csrc/models/vgg.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct VISION_API VGGImpl : torch::nn::Module {
1212
void _initialize_weights();
1313

1414
VGGImpl(
15-
torch::nn::Sequential features,
15+
const torch::nn::Sequential& features,
1616
int64_t num_classes = 1000,
1717
bool initialize_weights = true);
1818

0 commit comments

Comments
 (0)