Skip to content

Commit

Permalink
Static Analysis corrections on c++ model classes (pytorch#2893)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
datumbox authored and bryant1410 committed Nov 22, 2020
1 parent 0178bbd commit e77e9b2
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 28 deletions.
12 changes: 6 additions & 6 deletions torchvision/csrc/models/densenet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ struct _TransitionImpl : torch::nn::SequentialImpl {
torch::nn::Conv2d(Options(num_input_features, num_output_features, 1)
.stride(1)
.bias(false)));
push_back("pool", torch::nn::Functional([](torch::Tensor input) {
push_back("pool", torch::nn::Functional([](const torch::Tensor& input) {
return torch::avg_pool2d(input, 2, 2, 0, false, true);
}));
}
Expand All @@ -91,7 +91,7 @@ TORCH_MODULE(_Transition);
DenseNetImpl::DenseNetImpl(
int64_t num_classes,
int64_t growth_rate,
std::vector<int64_t> block_config,
const std::vector<int64_t>& block_config,
int64_t num_init_features,
int64_t bn_size,
double drop_rate) {
Expand Down Expand Up @@ -157,7 +157,7 @@ torch::Tensor DenseNetImpl::forward(torch::Tensor x) {
DenseNet121Impl::DenseNet121Impl(
int64_t num_classes,
int64_t growth_rate,
std::vector<int64_t> block_config,
const std::vector<int64_t>& block_config,
int64_t num_init_features,
int64_t bn_size,
double drop_rate)
Expand All @@ -172,7 +172,7 @@ DenseNet121Impl::DenseNet121Impl(
DenseNet169Impl::DenseNet169Impl(
int64_t num_classes,
int64_t growth_rate,
std::vector<int64_t> block_config,
const std::vector<int64_t>& block_config,
int64_t num_init_features,
int64_t bn_size,
double drop_rate)
Expand All @@ -187,7 +187,7 @@ DenseNet169Impl::DenseNet169Impl(
DenseNet201Impl::DenseNet201Impl(
int64_t num_classes,
int64_t growth_rate,
std::vector<int64_t> block_config,
const std::vector<int64_t>& block_config,
int64_t num_init_features,
int64_t bn_size,
double drop_rate)
Expand All @@ -202,7 +202,7 @@ DenseNet201Impl::DenseNet201Impl(
DenseNet161Impl::DenseNet161Impl(
int64_t num_classes,
int64_t growth_rate,
std::vector<int64_t> block_config,
const std::vector<int64_t>& block_config,
int64_t num_init_features,
int64_t bn_size,
double drop_rate)
Expand Down
10 changes: 5 additions & 5 deletions torchvision/csrc/models/densenet.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct VISION_API DenseNetImpl : torch::nn::Module {
DenseNetImpl(
int64_t num_classes = 1000,
int64_t growth_rate = 32,
std::vector<int64_t> block_config = {6, 12, 24, 16},
const std::vector<int64_t>& block_config = {6, 12, 24, 16},
int64_t num_init_features = 64,
int64_t bn_size = 4,
double drop_rate = 0);
Expand All @@ -38,7 +38,7 @@ struct VISION_API DenseNet121Impl : DenseNetImpl {
DenseNet121Impl(
int64_t num_classes = 1000,
int64_t growth_rate = 32,
std::vector<int64_t> block_config = {6, 12, 24, 16},
const std::vector<int64_t>& block_config = {6, 12, 24, 16},
int64_t num_init_features = 64,
int64_t bn_size = 4,
double drop_rate = 0);
Expand All @@ -48,7 +48,7 @@ struct VISION_API DenseNet169Impl : DenseNetImpl {
DenseNet169Impl(
int64_t num_classes = 1000,
int64_t growth_rate = 32,
std::vector<int64_t> block_config = {6, 12, 32, 32},
const std::vector<int64_t>& block_config = {6, 12, 32, 32},
int64_t num_init_features = 64,
int64_t bn_size = 4,
double drop_rate = 0);
Expand All @@ -58,7 +58,7 @@ struct VISION_API DenseNet201Impl : DenseNetImpl {
DenseNet201Impl(
int64_t num_classes = 1000,
int64_t growth_rate = 32,
std::vector<int64_t> block_config = {6, 12, 48, 32},
const std::vector<int64_t>& block_config = {6, 12, 48, 32},
int64_t num_init_features = 64,
int64_t bn_size = 4,
double drop_rate = 0);
Expand All @@ -68,7 +68,7 @@ struct VISION_API DenseNet161Impl : DenseNetImpl {
DenseNet161Impl(
int64_t num_classes = 1000,
int64_t growth_rate = 48,
std::vector<int64_t> block_config = {6, 12, 36, 24},
const std::vector<int64_t>& block_config = {6, 12, 36, 24},
int64_t num_init_features = 96,
int64_t bn_size = 4,
double drop_rate = 0);
Expand Down
10 changes: 5 additions & 5 deletions torchvision/csrc/models/inception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ InceptionAImpl::InceptionAImpl(int64_t in_channels, int64_t pool_features)
register_module("branch_pool", branch_pool);
}

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

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

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

auto branch3x3dbl = this->branch3x3dbl_1->forward(x);
Expand Down Expand Up @@ -115,7 +115,7 @@ InceptionCImpl::InceptionCImpl(int64_t in_channels, int64_t channels_7x7) {
register_module("branch_pool", branch_pool);
}

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

auto branch7x7 = this->branch7x7_1->forward(x);
Expand Down Expand Up @@ -151,7 +151,7 @@ InceptionDImpl::InceptionDImpl(int64_t in_channels)
register_module("branch7x7x3_4", branch7x7x3_4);
}

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

Expand Down Expand Up @@ -185,7 +185,7 @@ InceptionEImpl::InceptionEImpl(int64_t in_channels)
register_module("branch_pool", branch_pool);
}

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

auto branch3x3 = this->branch3x3_1->forward(x);
Expand Down
10 changes: 5 additions & 5 deletions torchvision/csrc/models/inception.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ struct VISION_API InceptionAImpl : torch::nn::Module {

InceptionAImpl(int64_t in_channels, int64_t pool_features);

torch::Tensor forward(torch::Tensor x);
torch::Tensor forward(const torch::Tensor& x);
};

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

InceptionBImpl(int64_t in_channels);

torch::Tensor forward(torch::Tensor x);
torch::Tensor forward(const torch::Tensor& x);
};

struct VISION_API InceptionCImpl : torch::nn::Module {
Expand All @@ -43,7 +43,7 @@ struct VISION_API InceptionCImpl : torch::nn::Module {

InceptionCImpl(int64_t in_channels, int64_t channels_7x7);

torch::Tensor forward(torch::Tensor x);
torch::Tensor forward(const torch::Tensor& x);
};

struct VISION_API InceptionDImpl : torch::nn::Module {
Expand All @@ -52,7 +52,7 @@ struct VISION_API InceptionDImpl : torch::nn::Module {

InceptionDImpl(int64_t in_channels);

torch::Tensor forward(torch::Tensor x);
torch::Tensor forward(const torch::Tensor& x);
};

struct VISION_API InceptionEImpl : torch::nn::Module {
Expand All @@ -62,7 +62,7 @@ struct VISION_API InceptionEImpl : torch::nn::Module {

InceptionEImpl(int64_t in_channels);

torch::Tensor forward(torch::Tensor x);
torch::Tensor forward(const torch::Tensor& x);
};

struct VISION_API InceptionAuxImpl : torch::nn::Module {
Expand Down
4 changes: 2 additions & 2 deletions torchvision/csrc/models/resnet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ BasicBlock::BasicBlock(
int64_t inplanes,
int64_t planes,
int64_t stride,
torch::nn::Sequential downsample,
const torch::nn::Sequential& downsample,
int64_t groups,
int64_t base_width)
: stride(stride), downsample(downsample) {
Expand Down Expand Up @@ -57,7 +57,7 @@ Bottleneck::Bottleneck(
int64_t inplanes,
int64_t planes,
int64_t stride,
torch::nn::Sequential downsample,
const torch::nn::Sequential& downsample,
int64_t groups,
int64_t base_width)
: stride(stride), downsample(downsample) {
Expand Down
4 changes: 2 additions & 2 deletions torchvision/csrc/models/resnet.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct VISION_API BasicBlock : torch::nn::Module {
int64_t inplanes,
int64_t planes,
int64_t stride = 1,
torch::nn::Sequential downsample = nullptr,
const torch::nn::Sequential& downsample = nullptr,
int64_t groups = 1,
int64_t base_width = 64);

Expand All @@ -59,7 +59,7 @@ struct VISION_API Bottleneck : torch::nn::Module {
int64_t inplanes,
int64_t planes,
int64_t stride = 1,
torch::nn::Sequential downsample = nullptr,
const torch::nn::Sequential& downsample = nullptr,
int64_t groups = 1,
int64_t base_width = 64);

Expand Down
1 change: 0 additions & 1 deletion torchvision/csrc/models/squeezenet.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "squeezenet.h"

#include <limits>
#include "modelsimpl.h"

namespace vision {
Expand Down
2 changes: 1 addition & 1 deletion torchvision/csrc/models/vgg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void VGGImpl::_initialize_weights() {
}

VGGImpl::VGGImpl(
torch::nn::Sequential features,
const torch::nn::Sequential& features,
int64_t num_classes,
bool initialize_weights) {
classifier = torch::nn::Sequential(
Expand Down
2 changes: 1 addition & 1 deletion torchvision/csrc/models/vgg.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct VISION_API VGGImpl : torch::nn::Module {
void _initialize_weights();

VGGImpl(
torch::nn::Sequential features,
const torch::nn::Sequential& features,
int64_t num_classes = 1000,
bool initialize_weights = true);

Expand Down

0 comments on commit e77e9b2

Please sign in to comment.