Unverified Commit e987d1c0 authored by Vasilis Vryniotis's avatar Vasilis Vryniotis Committed by GitHub
Browse files

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.
parent 60927642
...@@ -76,7 +76,7 @@ struct _TransitionImpl : torch::nn::SequentialImpl { ...@@ -76,7 +76,7 @@ struct _TransitionImpl : torch::nn::SequentialImpl {
torch::nn::Conv2d(Options(num_input_features, num_output_features, 1) torch::nn::Conv2d(Options(num_input_features, num_output_features, 1)
.stride(1) .stride(1)
.bias(false))); .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); return torch::avg_pool2d(input, 2, 2, 0, false, true);
})); }));
} }
...@@ -91,7 +91,7 @@ TORCH_MODULE(_Transition); ...@@ -91,7 +91,7 @@ TORCH_MODULE(_Transition);
DenseNetImpl::DenseNetImpl( DenseNetImpl::DenseNetImpl(
int64_t num_classes, int64_t num_classes,
int64_t growth_rate, int64_t growth_rate,
std::vector<int64_t> block_config, const std::vector<int64_t>& block_config,
int64_t num_init_features, int64_t num_init_features,
int64_t bn_size, int64_t bn_size,
double drop_rate) { double drop_rate) {
...@@ -157,7 +157,7 @@ torch::Tensor DenseNetImpl::forward(torch::Tensor x) { ...@@ -157,7 +157,7 @@ torch::Tensor DenseNetImpl::forward(torch::Tensor x) {
DenseNet121Impl::DenseNet121Impl( DenseNet121Impl::DenseNet121Impl(
int64_t num_classes, int64_t num_classes,
int64_t growth_rate, int64_t growth_rate,
std::vector<int64_t> block_config, const std::vector<int64_t>& block_config,
int64_t num_init_features, int64_t num_init_features,
int64_t bn_size, int64_t bn_size,
double drop_rate) double drop_rate)
...@@ -172,7 +172,7 @@ DenseNet121Impl::DenseNet121Impl( ...@@ -172,7 +172,7 @@ DenseNet121Impl::DenseNet121Impl(
DenseNet169Impl::DenseNet169Impl( DenseNet169Impl::DenseNet169Impl(
int64_t num_classes, int64_t num_classes,
int64_t growth_rate, int64_t growth_rate,
std::vector<int64_t> block_config, const std::vector<int64_t>& block_config,
int64_t num_init_features, int64_t num_init_features,
int64_t bn_size, int64_t bn_size,
double drop_rate) double drop_rate)
...@@ -187,7 +187,7 @@ DenseNet169Impl::DenseNet169Impl( ...@@ -187,7 +187,7 @@ DenseNet169Impl::DenseNet169Impl(
DenseNet201Impl::DenseNet201Impl( DenseNet201Impl::DenseNet201Impl(
int64_t num_classes, int64_t num_classes,
int64_t growth_rate, int64_t growth_rate,
std::vector<int64_t> block_config, const std::vector<int64_t>& block_config,
int64_t num_init_features, int64_t num_init_features,
int64_t bn_size, int64_t bn_size,
double drop_rate) double drop_rate)
...@@ -202,7 +202,7 @@ DenseNet201Impl::DenseNet201Impl( ...@@ -202,7 +202,7 @@ DenseNet201Impl::DenseNet201Impl(
DenseNet161Impl::DenseNet161Impl( DenseNet161Impl::DenseNet161Impl(
int64_t num_classes, int64_t num_classes,
int64_t growth_rate, int64_t growth_rate,
std::vector<int64_t> block_config, const std::vector<int64_t>& block_config,
int64_t num_init_features, int64_t num_init_features,
int64_t bn_size, int64_t bn_size,
double drop_rate) double drop_rate)
......
...@@ -26,7 +26,7 @@ struct VISION_API DenseNetImpl : torch::nn::Module { ...@@ -26,7 +26,7 @@ struct VISION_API DenseNetImpl : torch::nn::Module {
DenseNetImpl( DenseNetImpl(
int64_t num_classes = 1000, int64_t num_classes = 1000,
int64_t growth_rate = 32, 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 num_init_features = 64,
int64_t bn_size = 4, int64_t bn_size = 4,
double drop_rate = 0); double drop_rate = 0);
...@@ -38,7 +38,7 @@ struct VISION_API DenseNet121Impl : DenseNetImpl { ...@@ -38,7 +38,7 @@ struct VISION_API DenseNet121Impl : DenseNetImpl {
DenseNet121Impl( DenseNet121Impl(
int64_t num_classes = 1000, int64_t num_classes = 1000,
int64_t growth_rate = 32, 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 num_init_features = 64,
int64_t bn_size = 4, int64_t bn_size = 4,
double drop_rate = 0); double drop_rate = 0);
...@@ -48,7 +48,7 @@ struct VISION_API DenseNet169Impl : DenseNetImpl { ...@@ -48,7 +48,7 @@ struct VISION_API DenseNet169Impl : DenseNetImpl {
DenseNet169Impl( DenseNet169Impl(
int64_t num_classes = 1000, int64_t num_classes = 1000,
int64_t growth_rate = 32, 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 num_init_features = 64,
int64_t bn_size = 4, int64_t bn_size = 4,
double drop_rate = 0); double drop_rate = 0);
...@@ -58,7 +58,7 @@ struct VISION_API DenseNet201Impl : DenseNetImpl { ...@@ -58,7 +58,7 @@ struct VISION_API DenseNet201Impl : DenseNetImpl {
DenseNet201Impl( DenseNet201Impl(
int64_t num_classes = 1000, int64_t num_classes = 1000,
int64_t growth_rate = 32, 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 num_init_features = 64,
int64_t bn_size = 4, int64_t bn_size = 4,
double drop_rate = 0); double drop_rate = 0);
...@@ -68,7 +68,7 @@ struct VISION_API DenseNet161Impl : DenseNetImpl { ...@@ -68,7 +68,7 @@ struct VISION_API DenseNet161Impl : DenseNetImpl {
DenseNet161Impl( DenseNet161Impl(
int64_t num_classes = 1000, int64_t num_classes = 1000,
int64_t growth_rate = 48, 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 num_init_features = 96,
int64_t bn_size = 4, int64_t bn_size = 4,
double drop_rate = 0); double drop_rate = 0);
......
...@@ -49,7 +49,7 @@ InceptionAImpl::InceptionAImpl(int64_t in_channels, int64_t pool_features) ...@@ -49,7 +49,7 @@ InceptionAImpl::InceptionAImpl(int64_t in_channels, int64_t pool_features)
register_module("branch_pool", branch_pool); 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 branch1x1 = this->branch1x1->forward(x);
auto branch5x5 = this->branch5x5_1->forward(x); auto branch5x5 = this->branch5x5_1->forward(x);
...@@ -76,7 +76,7 @@ InceptionBImpl::InceptionBImpl(int64_t in_channels) ...@@ -76,7 +76,7 @@ InceptionBImpl::InceptionBImpl(int64_t in_channels)
register_module("branch3x3dbl_3", branch3x3dbl_3); 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 branch3x3 = this->branch3x3->forward(x);
auto branch3x3dbl = this->branch3x3dbl_1->forward(x); auto branch3x3dbl = this->branch3x3dbl_1->forward(x);
...@@ -115,7 +115,7 @@ InceptionCImpl::InceptionCImpl(int64_t in_channels, int64_t channels_7x7) { ...@@ -115,7 +115,7 @@ InceptionCImpl::InceptionCImpl(int64_t in_channels, int64_t channels_7x7) {
register_module("branch_pool", branch_pool); 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 branch1x1 = this->branch1x1->forward(x);
auto branch7x7 = this->branch7x7_1->forward(x); auto branch7x7 = this->branch7x7_1->forward(x);
...@@ -151,7 +151,7 @@ InceptionDImpl::InceptionDImpl(int64_t in_channels) ...@@ -151,7 +151,7 @@ InceptionDImpl::InceptionDImpl(int64_t in_channels)
register_module("branch7x7x3_4", branch7x7x3_4); 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); auto branch3x3 = this->branch3x3_1->forward(x);
branch3x3 = this->branch3x3_2->forward(branch3x3); branch3x3 = this->branch3x3_2->forward(branch3x3);
...@@ -185,7 +185,7 @@ InceptionEImpl::InceptionEImpl(int64_t in_channels) ...@@ -185,7 +185,7 @@ InceptionEImpl::InceptionEImpl(int64_t in_channels)
register_module("branch_pool", branch_pool); 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 branch1x1 = this->branch1x1->forward(x);
auto branch3x3 = this->branch3x3_1->forward(x); auto branch3x3 = this->branch3x3_1->forward(x);
......
...@@ -24,7 +24,7 @@ struct VISION_API InceptionAImpl : torch::nn::Module { ...@@ -24,7 +24,7 @@ struct VISION_API InceptionAImpl : torch::nn::Module {
InceptionAImpl(int64_t in_channels, int64_t pool_features); 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 { struct VISION_API InceptionBImpl : torch::nn::Module {
...@@ -32,7 +32,7 @@ struct VISION_API InceptionBImpl : torch::nn::Module { ...@@ -32,7 +32,7 @@ struct VISION_API InceptionBImpl : torch::nn::Module {
InceptionBImpl(int64_t in_channels); 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 { struct VISION_API InceptionCImpl : torch::nn::Module {
...@@ -43,7 +43,7 @@ struct VISION_API InceptionCImpl : torch::nn::Module { ...@@ -43,7 +43,7 @@ struct VISION_API InceptionCImpl : torch::nn::Module {
InceptionCImpl(int64_t in_channels, int64_t channels_7x7); 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 { struct VISION_API InceptionDImpl : torch::nn::Module {
...@@ -52,7 +52,7 @@ struct VISION_API InceptionDImpl : torch::nn::Module { ...@@ -52,7 +52,7 @@ struct VISION_API InceptionDImpl : torch::nn::Module {
InceptionDImpl(int64_t in_channels); 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 { struct VISION_API InceptionEImpl : torch::nn::Module {
...@@ -62,7 +62,7 @@ struct VISION_API InceptionEImpl : torch::nn::Module { ...@@ -62,7 +62,7 @@ struct VISION_API InceptionEImpl : torch::nn::Module {
InceptionEImpl(int64_t in_channels); 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 { struct VISION_API InceptionAuxImpl : torch::nn::Module {
......
...@@ -28,7 +28,7 @@ BasicBlock::BasicBlock( ...@@ -28,7 +28,7 @@ BasicBlock::BasicBlock(
int64_t inplanes, int64_t inplanes,
int64_t planes, int64_t planes,
int64_t stride, int64_t stride,
torch::nn::Sequential downsample, const torch::nn::Sequential& downsample,
int64_t groups, int64_t groups,
int64_t base_width) int64_t base_width)
: stride(stride), downsample(downsample) { : stride(stride), downsample(downsample) {
...@@ -57,7 +57,7 @@ Bottleneck::Bottleneck( ...@@ -57,7 +57,7 @@ Bottleneck::Bottleneck(
int64_t inplanes, int64_t inplanes,
int64_t planes, int64_t planes,
int64_t stride, int64_t stride,
torch::nn::Sequential downsample, const torch::nn::Sequential& downsample,
int64_t groups, int64_t groups,
int64_t base_width) int64_t base_width)
: stride(stride), downsample(downsample) { : stride(stride), downsample(downsample) {
......
...@@ -36,7 +36,7 @@ struct VISION_API BasicBlock : torch::nn::Module { ...@@ -36,7 +36,7 @@ struct VISION_API BasicBlock : torch::nn::Module {
int64_t inplanes, int64_t inplanes,
int64_t planes, int64_t planes,
int64_t stride = 1, int64_t stride = 1,
torch::nn::Sequential downsample = nullptr, const torch::nn::Sequential& downsample = nullptr,
int64_t groups = 1, int64_t groups = 1,
int64_t base_width = 64); int64_t base_width = 64);
...@@ -59,7 +59,7 @@ struct VISION_API Bottleneck : torch::nn::Module { ...@@ -59,7 +59,7 @@ struct VISION_API Bottleneck : torch::nn::Module {
int64_t inplanes, int64_t inplanes,
int64_t planes, int64_t planes,
int64_t stride = 1, int64_t stride = 1,
torch::nn::Sequential downsample = nullptr, const torch::nn::Sequential& downsample = nullptr,
int64_t groups = 1, int64_t groups = 1,
int64_t base_width = 64); int64_t base_width = 64);
......
#include "squeezenet.h" #include "squeezenet.h"
#include <limits>
#include "modelsimpl.h" #include "modelsimpl.h"
namespace vision { namespace vision {
......
...@@ -50,7 +50,7 @@ void VGGImpl::_initialize_weights() { ...@@ -50,7 +50,7 @@ void VGGImpl::_initialize_weights() {
} }
VGGImpl::VGGImpl( VGGImpl::VGGImpl(
torch::nn::Sequential features, const torch::nn::Sequential& features,
int64_t num_classes, int64_t num_classes,
bool initialize_weights) { bool initialize_weights) {
classifier = torch::nn::Sequential( classifier = torch::nn::Sequential(
......
...@@ -12,7 +12,7 @@ struct VISION_API VGGImpl : torch::nn::Module { ...@@ -12,7 +12,7 @@ struct VISION_API VGGImpl : torch::nn::Module {
void _initialize_weights(); void _initialize_weights();
VGGImpl( VGGImpl(
torch::nn::Sequential features, const torch::nn::Sequential& features,
int64_t num_classes = 1000, int64_t num_classes = 1000,
bool initialize_weights = true); bool initialize_weights = true);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment