Skip to content

Commit 3c69741

Browse files
committed
向外提取conv\pooling等循环中的变量
1 parent e93b6bd commit 3c69741

File tree

4 files changed

+33
-28
lines changed

4 files changed

+33
-28
lines changed

source/data/tensor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ void Tensor<float>::Fill(const std::vector<float>& values, bool row_major) {
186186
const uint32_t channels = this->data_.n_slices;
187187

188188
for (uint32_t i = 0; i < channels; ++i) {
189-
auto& channel_data = this->data_.slice(i);
189+
arma::fmat& channel_data = this->data_.slice(i);
190190
arma::fmat channel_data_t((float*)values.data() + i * planes,
191191
this->cols(), this->rows(), false, true);
192192
channel_data = channel_data_t.t();

source/layer/details/adaptive_avgpooling.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,18 +116,19 @@ InferStatus AdaptiveAveragePoolingLayer::Forward(
116116
const arma::fmat& input_channel = input_data->slice(ic);
117117
arma::fmat& output_channel = output_data->slice(ic);
118118
for (uint32_t c = 0; c < input_w - pooling_w + 1; c += stride_w) {
119+
int output_col = int(c / stride_w);
119120
for (uint32_t r = 0; r < input_h - pooling_h + 1; r += stride_h) {
121+
int output_row = int(r / stride_h);
120122
float mean_value = 0.f;
121-
float* output_channel_ptr = output_channel.colptr(int(c / stride_w));
123+
float* output_channel_ptr = output_channel.colptr(output_col);
122124
for (uint32_t w = 0; w < pooling_w; ++w) {
123125
const float* col_ptr = input_channel.colptr(c + w) + r;
124126
for (uint32_t h = 0; h < pooling_h; ++h) {
125127
float current_value = *(col_ptr + h);
126128
mean_value = mean_value + current_value;
127129
}
128130
}
129-
*(output_channel_ptr + int(r / stride_h)) =
130-
mean_value / float(pooling_size);
131+
*(output_channel_ptr + output_row) = mean_value / float(pooling_size);
131132
}
132133
}
133134
}

source/layer/details/convolution.cpp

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -195,26 +195,26 @@ arma::fmat ConvolutionLayer::Im2Col(sftensor input, uint32_t kernel_w,
195195
for (uint32_t ic = 0; ic < input_c_group; ++ic) {
196196
float* input_channel_ptr =
197197
input->matrix_raw_ptr(ic + group * input_c_group);
198-
uint32_t input_channel_height = input_h;
199-
int current_col = 0;
198+
uint32_t current_col = 0;
199+
uint32_t channel_row = ic * row_len;
200200
for (uint32_t w = 0; w < input_padded_w - kernel_w + 1; w += stride_w_) {
201201
for (uint32_t r = 0; r < input_padded_h - kernel_h + 1; r += stride_h_) {
202-
float* input_matrix_c_ptr =
203-
input_matrix.colptr(current_col) + ic * row_len;
202+
float* input_matrix_ptr =
203+
input_matrix.colptr(current_col) + channel_row;
204204
current_col += 1;
205205
for (uint32_t kw = 0; kw < kernel_w; ++kw) {
206+
const uint32_t region_w = input_h * (w + kw - padding_w_);
206207
for (uint32_t kh = 0; kh < kernel_h; ++kh) {
207208
if ((kh + r >= padding_h_ && kw + w >= padding_w_) &&
208209
(kh + r < input_h + padding_h_ &&
209210
kw + w < input_w + padding_w_)) {
210-
float* region_ptr = input_channel_ptr +
211-
input_channel_height * (w + kw - padding_w_) +
212-
r + kh - padding_h_;
213-
*input_matrix_c_ptr = *region_ptr;
211+
float* region_ptr =
212+
input_channel_ptr + region_w + (r + kh - padding_h_);
213+
*input_matrix_ptr = *region_ptr;
214214
} else {
215-
*input_matrix_c_ptr = padding_value; // only support zero mode
215+
*input_matrix_ptr = padding_value; // only support zero mode
216216
}
217-
input_matrix_c_ptr += 1;
217+
input_matrix_ptr += 1;
218218
}
219219
}
220220
}
@@ -339,7 +339,7 @@ ParseParameterAttrStatus ConvolutionLayer::GetInstance(
339339
return ParseParameterAttrStatus::kParameterMissingOutChannel;
340340
}
341341

342-
auto out_channel =
342+
auto out_channel =
343343
std::dynamic_pointer_cast<RuntimeParameterInt>(params.at("out_channels"));
344344
if (!out_channel) {
345345
LOG(ERROR) << "Can not find the out channel parameter";
@@ -362,7 +362,8 @@ ParseParameterAttrStatus ConvolutionLayer::GetInstance(
362362
LOG(ERROR) << "Can not find the bias parameter";
363363
return ParseParameterAttrStatus::kParameterMissingUseBias;
364364
}
365-
auto use_bias = std::dynamic_pointer_cast<RuntimeParameterBool>(params.at("bias"));
365+
auto use_bias =
366+
std::dynamic_pointer_cast<RuntimeParameterBool>(params.at("bias"));
366367
if (!use_bias) {
367368
LOG(ERROR) << "Can not find the bias parameter";
368369
return ParseParameterAttrStatus::kParameterMissingUseBias;
@@ -372,7 +373,7 @@ ParseParameterAttrStatus ConvolutionLayer::GetInstance(
372373
LOG(ERROR) << "Can not find the stride parameter";
373374
return ParseParameterAttrStatus::kParameterMissingStride;
374375
}
375-
auto stride =
376+
auto stride =
376377
std::dynamic_pointer_cast<RuntimeParameterIntArray>(params.at("stride"));
377378
if (!stride) {
378379
LOG(ERROR) << "Can not find the stride parameter";
@@ -383,16 +384,16 @@ ParseParameterAttrStatus ConvolutionLayer::GetInstance(
383384
LOG(ERROR) << "Can not find the kernel parameter";
384385
return ParseParameterAttrStatus::kParameterMissingKernel;
385386
}
386-
auto kernel =
387-
std::dynamic_pointer_cast<RuntimeParameterIntArray>(params.at("kernel_size"));
387+
auto kernel = std::dynamic_pointer_cast<RuntimeParameterIntArray>(
388+
params.at("kernel_size"));
388389
if (!kernel) {
389390
LOG(ERROR) << "Can not find the kernel parameter";
390391
return ParseParameterAttrStatus::kParameterMissingKernel;
391392
}
392393

393394
if (params.find("padding_mode") != params.end()) {
394-
auto padding_mode =
395-
std::dynamic_pointer_cast<RuntimeParameterString>(params.at("padding_mode"));
395+
auto padding_mode = std::dynamic_pointer_cast<RuntimeParameterString>(
396+
params.at("padding_mode"));
396397
if (padding_mode == nullptr) {
397398
LOG(ERROR) << "Can not find the padding parameter";
398399
return ParseParameterAttrStatus::kParameterMissingPaddingMode;
@@ -408,7 +409,8 @@ ParseParameterAttrStatus ConvolutionLayer::GetInstance(
408409
return ParseParameterAttrStatus::kParameterMissingPaddingMode;
409410
}
410411

411-
auto groups = std::dynamic_pointer_cast<RuntimeParameterInt>(params.at("groups"));
412+
auto groups =
413+
std::dynamic_pointer_cast<RuntimeParameterInt>(params.at("groups"));
412414
if (!groups) {
413415
LOG(ERROR) << "Can not find the groups parameter";
414416
return ParseParameterAttrStatus::kParameterMissingGroups;

source/layer/details/maxpooling.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,25 +133,27 @@ InferStatus MaxPoolingLayer::Forward(
133133
const arma::fmat& input_channel = input_data->slice(ic);
134134
arma::fmat& output_channel = output_data->slice(ic);
135135
for (uint32_t c = 0; c < input_padded_w - pooling_w + 1; c += stride_w_) {
136+
int output_col = int(c / stride_w_);
136137
for (uint32_t r = 0; r < input_padded_h - pooling_h + 1;
137138
r += stride_h_) {
138-
float* output_channel_ptr = output_channel.colptr(int(c / stride_w_));
139+
int output_row = int(r / stride_h_);
140+
float* output_channel_ptr = output_channel.colptr(output_col);
139141
float max_value = std::numeric_limits<float>::lowest();
140142
for (uint32_t w = 0; w < pooling_w; ++w) {
143+
const float* col_ptr = input_channel.colptr(c + w - padding_w_);
141144
for (uint32_t h = 0; h < pooling_h; ++h) {
142145
float current_value = 0.f;
143146
if ((h + r >= padding_h_ && w + c >= padding_w_) &&
144147
(h + r < input_h + padding_h_ &&
145148
w + c < input_w + padding_w_)) {
146-
const float* col_ptr = input_channel.colptr(c + w - padding_w_);
147149
current_value = *(col_ptr + r + h - padding_h_);
148150
} else {
149151
current_value = std::numeric_limits<float>::lowest();
150152
}
151153
max_value = max_value > current_value ? max_value : current_value;
152154
}
153155
}
154-
*(output_channel_ptr + int(r / stride_h_)) = max_value;
156+
*(output_channel_ptr + output_row) = max_value;
155157
}
156158
}
157159
}
@@ -182,7 +184,7 @@ ParseParameterAttrStatus MaxPoolingLayer::GetInstance(
182184
return ParseParameterAttrStatus::kParameterMissingPadding;
183185
}
184186

185-
auto padding =
187+
auto padding =
186188
std::dynamic_pointer_cast<RuntimeParameterIntArray>(params.at("padding"));
187189
if (!padding) {
188190
LOG(ERROR) << "Can not find the padding parameter";
@@ -194,8 +196,8 @@ ParseParameterAttrStatus MaxPoolingLayer::GetInstance(
194196
return ParseParameterAttrStatus::kParameterMissingKernel;
195197
}
196198

197-
auto kernel_size =
198-
std::dynamic_pointer_cast<RuntimeParameterIntArray>(params.at("kernel_size"));
199+
auto kernel_size = std::dynamic_pointer_cast<RuntimeParameterIntArray>(
200+
params.at("kernel_size"));
199201
if (!kernel_size) {
200202
LOG(ERROR) << "Can not find the kernel size parameter";
201203
return ParseParameterAttrStatus::kParameterMissingKernel;

0 commit comments

Comments
 (0)