Skip to content

Commit

Permalink
Merge pull request #112 from drnikolaev/nvcaffe-0.14-memory-opt
Browse files Browse the repository at this point in the history
Memory consuming optimization for BN cuDNN algos
  • Loading branch information
lukeyeager committed Mar 15, 2016
2 parents 192d349 + e8dab0e commit a8d1b9d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
21 changes: 20 additions & 1 deletion include/caffe/layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ class Layer {
* layer.
*/
explicit Layer(const LayerParameter& param)
: layer_param_(param), is_shared_(false) {
: layer_param_(param), is_shared_(false),
forward_passed_(false), backward_passed_(false) {
// Set phase and copy blobs (if there are any).
phase_ = param.phase();
if (layer_param_.blobs_size() > 0) {
Expand Down Expand Up @@ -316,6 +317,21 @@ class Layer {
param_propagate_down_[param_id] = value;
}

bool IsForwardPassed() const {
return forward_passed_;
}

void ForwardPassed(bool passed) {
forward_passed_ = passed;
}

bool IsBackwardPassed() const {
return backward_passed_;
}

void BackwardPassed(bool passed) {
backward_passed_ = passed;
}

protected:
/** The protobuf that stores the layer parameters */
Expand Down Expand Up @@ -431,6 +447,9 @@ class Layer {
/** Whether this layer is actually shared by other nets*/
bool is_shared_;

bool forward_passed_;
bool backward_passed_;

/** The mutex for sequential forward if this layer is shared */
shared_ptr<boost::mutex> forward_mutex_;

Expand Down
8 changes: 6 additions & 2 deletions src/caffe/layers/cudnn_conv_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,12 @@ void CuDNNConvolutionLayer<Dtype>::Reshape(
cudnn::setConvolutionDesc<Dtype>(&conv_descs_[i], bottom_descs_[i],
filter_desc_, pad_h, pad_w, stride_h, stride_w);

// choose forward and backward algorithms + workspace(s)
CUDNN_CHECK(cudnnGetConvolutionForwardAlgorithm(Caffe::cudnn_handle(),
if (!this->IsForwardPassed() || !this->IsBackwardPassed()) {
continue;
}

// choose forward and backward algorithms + workspace(s)
CUDNN_CHECK(cudnnGetConvolutionForwardAlgorithm(Caffe::cudnn_handle(),
bottom_descs_[i],
filter_desc_,
conv_descs_[i],
Expand Down
6 changes: 6 additions & 0 deletions src/caffe/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,9 @@ Dtype Net<Dtype>::ForwardFromTo(int start, int end) {
loss += layer_loss;
if (debug_info_) { ForwardDebugInfo(i); }
}
for (int i = start; i <= end; ++i) {
layers_[i]->ForwardPassed(true);
}
return loss;
}

Expand Down Expand Up @@ -645,6 +648,9 @@ void Net<Dtype>::BackwardFromTo(int start, int end) {
if (debug_info_) { BackwardDebugInfo(i); }
}
}
for (int i = start; i >= end; --i) {
layers_[i]->BackwardPassed(true);
}
}

template <typename Dtype>
Expand Down

0 comments on commit a8d1b9d

Please sign in to comment.