cudnn8编译caffe过程(保姆级图文全过程,涵盖各种报错及解决办法)

众所周知,caffe是个较老的框架,而且只支持到cudnn7,但是笔者在复现ds-slam过程中又必须编译caffe,我的cuda版本是11.4,最低只支持到8.2.4,故没办法,只能编译了

在此记录过程、报错及解决办法如下;

首先安装依赖:

sudo apt-get install git
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev
libhdf5-serial-dev protobuf-compiler
sudo apt-get install --no-install-recommends libboost-all-dev
sudo apt-get install libatlas-base-dev
sudo apt-get install python-dev
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev

然后git项目源码:

git clone https://github.com/BVLC/caffe.git

然后编译:

cd caffe
mkdir build
cd build
cmake ..
make all
sudo make install 
make runtest

哈哈哈,不会那么顺利哒!

在cmake ..过程中,报了第一个错:

Found cuDNN: ver. ??? found (include: /usr/local/cuda-11.4/include, library: /usr/local/cuda-11.4/lib64/libcudnn.so) CMake Error at cmake/Cuda.cmake:227 (message): cuDNN version >3 is required. Call Stack (most recent call first): cmake/Cuda.cmake:255 (detect_cuDNN) cmake/Dependencies.cmake:85 (include) CMakeLists.txt:49 (include)

什么原因呢,是因为CMake 找不到或者无法正确检测到 cuDNN 的版本。错误消息中提到 “cuDNN version >3 is required”,但它没有成功识别你安装的 cuDNN 版本,但是不可能啊,我们安装了啊。 

直接说解决办法;修改cmake/Cuda.cmake ,  将里面的"cudnn.h" 全部用 "cudnn_version.h"代替

然后是第二个错:找不到cublas

说找不到cuda_cublas的一系列位置,这不可能,我安装了呀,先find一下:

 

果然有,那就set一下,在caffe的编译目录里cmake,找到相应的cuda.cmake,然后找CUDA_cublas_LIBRARY,在前添加行

set(CUDA_CUBLAS_LIBRARIES /usr/local/cuda/targets/x86_64-linux/lib/libcublas.so
)

这回这个问题过了,然后在make all过程中开始出错:

对了,这个方法还能解决

../lib/libcaffe.so.1.0.0:对‘cublasSetStream_v2’未定义的引用 ../lib/libcaffe.so.1.0.0:对‘cublasDdot_v2’未定义的引用 ../lib/libcaffe.so.1.0.0:对‘cublasDaxpy_v2’未定义的引用 ../lib/libcaffe.so.1.0.0:对‘cublasDscal_v2’未定义的引用 ../lib/libcaffe.so.1.0.0:对‘cublasScopy_v2’未定义的引用 ../lib/libcaffe.so.1.0.0:对‘cublasSgemv_v2’未定义的引用 ../lib/libcaffe.so.1.0.0:对‘cublasSdot_v2’未定义的引用 ../lib/libcaffe.so.1.0.0:对‘cublasDcopy_v2’未定义的引用 ../lib/libcaffe.so.1.0.0:对‘cublasDestroy_v2’未定义的引用 ../lib/libcaffe.so.1.0.0:对‘cublasSgemm_v2’未定义的引用 ../lib/libcaffe.so.1.0.0:对‘cublasDgemv_v2’未定义的引用 ../lib/libcaffe.so.1.0.0:对‘cublasDasum_v2’未定义的引用 ../lib/libcaffe.so.1.0.0:对‘cublasGetStream_v2’未定义的引用 ../lib/libcaffe.so.1.0.0:对‘cublasSaxpy_v2’未定义的引用 ../lib/libcaffe.so.1.0.0:对‘cublasDgemm_v2’未定义的引用 ../lib/libcaffe.so.1.0.0:对‘cublasSscal_v2’未定义的引用 ../lib/libcaffe.so.1.0.0:对‘cublasSasum_v2’未定义的引用 ../lib/libcaffe.so.1.0.0:对‘cublasCreate_v2’未定义的引用
等一系列关于cublas*_v2的未定义的引用错误。

说是cudnn_conv_layer.cpp第131行报错

上网上一查,这是因为cudnn8里没有cudnnGetConvolutionForwardAlgorithm()这个函数了,改成了cudnnGetConvolutionForwardAlgorithm_v7(),也没了CUDNN_CONVOLUTION_FWD_SPECIFY_WORKSPACE_LIMIT这个宏定义

那么改呗:

将 src/caffe/layers/cudnn_conv_layer.cpp:中的相关位置reshape函数替换成下面的:

template <typename Dtype>
void CuDNNConvolutionLayer<Dtype>::Reshape(
    const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) {
  ConvolutionLayer<Dtype>::Reshape(bottom, top);
  CHECK_LE(2, this->num_spatial_axes_)
      << "CuDNNConvolution input must have 2 spatial axes "
      << "(e.g., height and width). "
      << "Use 'engine: CAFFE' for general ND convolution.";
  bottom_offset_ = this->bottom_dim_ / this->group_;
  top_offset_ = this->top_dim_ / this->group_;
  const int height = bottom[0]->shape(this->channel_axis_ + 1 );
  const int width = bottom[0]->shape(this->channel_axis_ + 2 );
  const int height_out = top[0]->shape(this->channel_axis_ + 1 );
  const int width_out = top[0]->shape(this->channel_axis_ + 2 );
  const int* pad_data = this->pad_.cpu_data();
  const int pad_h = pad_data[0];
  const int pad_w = pad_data[1];
  const int* stride_data = this->stride_.cpu_data();
  const int stride_h = stride_data[0];
  const int stride_w = stride_data[1];
  #if  CUDNN_VERSION_MIN(8, 0, 0)
  int RetCnt;
  bool found_conv_algorithm;
  size_t free_memory, total_memory;
  cudnnConvolutionFwdAlgoPerf_t     fwd_algo_pref_[4];
  cudnnConvolutionBwdDataAlgoPerf_t bwd_data_algo_pref_[4];
 
  //get memory sizes
  cudaMemGetInfo(&a
NVIDIA® cuDNN is a GPU-accelerated library of primitives for deep neural networks. cuDNN是一个对DNN的GPU加速库。他提供高度可调整的在DNN中的常用的例程实现。 It provides highly tuned implementations of routines arising frequently in DNN applications: 常用语前向后向卷积网络,包括交叉相关。Convolution forward and backward, including cross-correlation 前像后向pooling。Pooling forward and backward 前向后向softmax。Softmax forward and backward 前向后向神经元激活。Neuron activations forward and backward Rectified linear (ReLU) Hyperbolic tangent (TANH) Tensor transformation functions LRN, LCN and batch normalization forward and backward cuDNN’s convolution routines aim for performance competitive with the fastest GEMM (matrix multiply) based implementations of such routines while using significantly less memory. cuDNN突出可定制的数据布局,支持灵活的维数排序,跨步,4D子区域for 4D张量作为输入输出。 cuDNN features customizable data layouts, supporting flexible dimension ordering, striding, and subregions for the 4D tensors used as inputs and outputs to all of its routines. This flexibility allows easy integration into any neural network implementation and avoids the input/output transposition steps sometimes necessary with GEMM-based convolutions. cuDNN offers a context-based API that allows for easy multithreading and (optional) interoperability with CUDA streams. cuDNN提供一种基于上下文的API,允许简单的多线程和CDUA流的互用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值