-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcudnn_lcn_layer.cpp
More file actions
73 lines (56 loc) · 2.1 KB
/
cudnn_lcn_layer.cpp
File metadata and controls
73 lines (56 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#ifdef USE_CUDNN
#include <vector>
#include "caffe/layers/cudnn_lcn_layer.hpp"
namespace caffe {
template <typename Dtype>
void CuDNNLCNLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
LRNLayer<Dtype>::LayerSetUp(bottom, top);
CUDNN_CHECK(cudnnCreate(&handle_));
CUDNN_CHECK(cudnnCreateLRNDescriptor(&norm_desc_));
cudnn::createTensor4dDesc<Dtype>(&bottom_desc_);
cudnn::createTensor4dDesc<Dtype>(&top_desc_);
// create a LRN handle
handles_setup_ = true;
size_ = this->layer_param().lrn_param().local_size();
pre_pad_ = (size_ - 1) / 2;
alpha_ = this->layer_param().lrn_param().alpha();
beta_ = this->layer_param().lrn_param().beta();
k_ = this->layer_param().lrn_param().k();
}
template <typename Dtype>
void CuDNNLCNLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
LRNLayer<Dtype>::Reshape(bottom, top);
cudnn::setTensor4dDesc<Dtype>(&bottom_desc_, bottom[0]->num(),
this->channels_, this->height_, this->width_);
cudnn::setTensor4dDesc<Dtype>(&top_desc_, bottom[0]->num(),
this->channels_, this->height_, this->width_);
CUDNN_CHECK(cudnnSetLRNDescriptor(norm_desc_, size_, alpha_, beta_, k_));
// allocate / reallocate tempData buffers
size_t totalSizeInBytes = sizeof(Dtype)*bottom[0]->num()* \
this->channels_*this->height_*this->width_;
if (totalSizeInBytes > tempDataSize) {
tempDataSize = totalSizeInBytes;
cudaFree(tempData1);
cudaFree(tempData2);
// allocate new buffers
CUDA_CHECK(cudaMalloc(&tempData1, totalSizeInBytes));
CUDA_CHECK(cudaMalloc(&tempData2, totalSizeInBytes));
}
}
template <typename Dtype>
CuDNNLCNLayer<Dtype>::~CuDNNLCNLayer() {
// Check that handles have been setup before destroying.
if (!handles_setup_) { return; }
cudnnDestroyTensorDescriptor(bottom_desc_);
cudnnDestroyTensorDescriptor(top_desc_);
// destroy LRN handle
cudnnDestroy(handle_);
// free temp buffers
cudaFree(tempData1);
cudaFree(tempData2);
}
INSTANTIATE_CLASS(CuDNNLCNLayer);
} // namespace caffe
#endif