Skip to content

Commit fb8c061

Browse files
committed
Use Blobs instead of SyncedMemorys for the bias_multiplier_'s.
1 parent d5351bc commit fb8c061

File tree

5 files changed

+14
-22
lines changed

5 files changed

+14
-22
lines changed

include/caffe/vision_layers.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class ConvolutionLayer : public Layer<Dtype> {
5555
int num_output_;
5656
int group_;
5757
Blob<Dtype> col_buffer_;
58-
shared_ptr<SyncedMemory> bias_multiplier_;
58+
shared_ptr<Blob<Dtype> > bias_multiplier_;
5959
bool bias_term_;
6060
int M_;
6161
int K_;
@@ -157,7 +157,7 @@ class InnerProductLayer : public Layer<Dtype> {
157157
int K_;
158158
int N_;
159159
bool bias_term_;
160-
shared_ptr<SyncedMemory> bias_multiplier_;
160+
shared_ptr<Blob<Dtype> > bias_multiplier_;
161161
};
162162

163163
// Forward declare PoolingLayer and SplitLayer for use in LRNLayer.

src/caffe/layers/conv_layer.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,8 @@ void ConvolutionLayer<Dtype>::SetUp(const vector<Blob<Dtype>*>& bottom,
7878
}
7979
// Set up the bias filler
8080
if (bias_term_) {
81-
bias_multiplier_.reset(new SyncedMemory(N_ * sizeof(Dtype)));
82-
Dtype* bias_multiplier_data =
83-
reinterpret_cast<Dtype*>(bias_multiplier_->mutable_cpu_data());
84-
for (int i = 0; i < N_; ++i) {
85-
bias_multiplier_data[i] = 1.;
86-
}
81+
bias_multiplier_.reset(new Blob<Dtype>(1, 1, 1, N_));
82+
caffe_set(N_, Dtype(1), bias_multiplier_->mutable_cpu_data());
8783
}
8884
this->param_propagate_down_.resize(this->blobs_.size(), true);
8985
}
@@ -114,7 +110,7 @@ Dtype ConvolutionLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
114110
if (bias_term_) {
115111
caffe_cpu_gemm<Dtype>(CblasNoTrans, CblasNoTrans, num_output_,
116112
N_, 1, (Dtype)1., this->blobs_[1]->cpu_data(),
117-
reinterpret_cast<const Dtype*>(bias_multiplier_->cpu_data()),
113+
bias_multiplier_->cpu_data(),
118114
(Dtype)1., top_data + (*top)[i]->offset(n));
119115
}
120116
}
@@ -148,7 +144,7 @@ void ConvolutionLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
148144
for (int n = 0; n < num_; ++n) {
149145
caffe_cpu_gemv<Dtype>(CblasNoTrans, num_output_, N_,
150146
1., top_diff + top[0]->offset(n),
151-
static_cast<const Dtype*>(bias_multiplier_->cpu_data()), 1.,
147+
bias_multiplier_->cpu_data(), 1.,
152148
bias_diff);
153149
}
154150
}

src/caffe/layers/conv_layer.cu

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Dtype ConvolutionLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
3535
if (bias_term_) {
3636
caffe_gpu_gemm<Dtype>(CblasNoTrans, CblasNoTrans, num_output_,
3737
N_, 1, (Dtype)1., this->blobs_[1]->gpu_data(),
38-
reinterpret_cast<const Dtype*>(bias_multiplier_->gpu_data()),
38+
bias_multiplier_->gpu_data(),
3939
(Dtype)1., top_data + (*top)[i]->offset(n));
4040
}
4141
}
@@ -69,7 +69,7 @@ void ConvolutionLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
6969
for (int n = 0; n < num_; ++n) {
7070
caffe_gpu_gemv<Dtype>(CblasNoTrans, num_output_, N_,
7171
1., top_diff + top[0]->offset(n),
72-
static_cast<const Dtype*>(bias_multiplier_->gpu_data()), 1.,
72+
bias_multiplier_->gpu_data(), 1.,
7373
bias_diff);
7474
}
7575
}

src/caffe/layers/inner_product_layer.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,8 @@ void InnerProductLayer<Dtype>::SetUp(const vector<Blob<Dtype>*>& bottom,
4747
} // parameter initialization
4848
// Setting up the bias multiplier
4949
if (bias_term_) {
50-
bias_multiplier_.reset(new SyncedMemory(M_ * sizeof(Dtype)));
51-
Dtype* bias_multiplier_data =
52-
reinterpret_cast<Dtype*>(bias_multiplier_->mutable_cpu_data());
53-
for (int i = 0; i < M_; ++i) {
54-
bias_multiplier_data[i] = 1.;
55-
}
50+
bias_multiplier_.reset(new Blob<Dtype>(1, 1, 1, M_));
51+
caffe_set(M_, Dtype(1), bias_multiplier_->mutable_cpu_data());
5652
}
5753
this->param_propagate_down_.resize(this->blobs_.size(), true);
5854
}
@@ -67,7 +63,7 @@ Dtype InnerProductLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
6763
bottom_data, weight, (Dtype)0., top_data);
6864
if (bias_term_) {
6965
caffe_cpu_gemm<Dtype>(CblasNoTrans, CblasNoTrans, M_, N_, 1, (Dtype)1.,
70-
reinterpret_cast<const Dtype*>(bias_multiplier_->cpu_data()),
66+
bias_multiplier_->cpu_data(),
7167
this->blobs_[1]->cpu_data(), (Dtype)1., top_data);
7268
}
7369
return Dtype(0);
@@ -88,7 +84,7 @@ void InnerProductLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
8884
const Dtype* top_diff = top[0]->cpu_diff();
8985
// Gradient with respect to bias
9086
caffe_cpu_gemv<Dtype>(CblasTrans, M_, N_, (Dtype)1., top_diff,
91-
reinterpret_cast<const Dtype*>(bias_multiplier_->cpu_data()), (Dtype)0.,
87+
bias_multiplier_->cpu_data(), (Dtype)0.,
9288
this->blobs_[1]->mutable_cpu_diff());
9389
}
9490
if (propagate_down[0]) {

src/caffe/layers/inner_product_layer.cu

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Dtype InnerProductLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
2323
bottom_data, weight, (Dtype)0., top_data);
2424
if (bias_term_) {
2525
caffe_gpu_gemm<Dtype>(CblasNoTrans, CblasNoTrans, M_, N_, 1, (Dtype)1.,
26-
reinterpret_cast<const Dtype*>(bias_multiplier_->gpu_data()),
26+
bias_multiplier_->gpu_data(),
2727
this->blobs_[1]->gpu_data(), (Dtype)1., top_data);
2828
}
2929
return Dtype(0);
@@ -44,7 +44,7 @@ void InnerProductLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
4444
const Dtype* top_diff = top[0]->gpu_diff();
4545
// Gradient with respect to bias
4646
caffe_gpu_gemv<Dtype>(CblasTrans, M_, N_, (Dtype)1., top_diff,
47-
reinterpret_cast<const Dtype*>(bias_multiplier_->gpu_data()), (Dtype)0.,
47+
bias_multiplier_->gpu_data(), (Dtype)0.,
4848
this->blobs_[1]->mutable_gpu_diff());
4949
}
5050
if (propagate_down[0]) {

0 commit comments

Comments
 (0)