首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >教你30分钟安装cuda环境下的torch(非Pytorch)

教你30分钟安装cuda环境下的torch(非Pytorch)

作者头像
老潘
发布2023-10-19 10:33:51
发布2023-10-19 10:33:51
1.6K00
代码可运行
举报
运行总次数:0
代码可运行

前言

Pytorch我们都熟悉,是一个优秀的深度学习的运行库,但我们可能也知道Pytorch的前身torchTorch也是一个优秀的深度学习库,运行语言是lua语言。既然我们有了Pytorch,为什么还要装torch呢?

很简单:

  • Torch框架和Pytorch框架类似,熟悉了Pytorch学习torch轻而易举
  • Torch框架的设计也很优秀,自定义化相比Pytorch更灵活
  • 很多优秀的项目,或者说很多最近的项目很多都是使用Torch编写的

说了这么多,开始安装吧!

安装

以下安装环境在Ubuntu16.04。

因为我们安装torch需要运行在GPU上,首先应该安装好Cuda9.1和cudnn。

相关安装cudacudnn教程:

https://cloud.tencent.com/developer/article/1150031

https://cloud.tencent.com/developer/article/1150185

安装搭配好环境后就可以进行安装torch了。

首先自己创建一个文件夹叫做torch,一般我创建的地址为:home/prototype/torch/

cd到当前的文件夹,从github下载源代码:

代码语言:javascript
代码运行次数:0
运行
复制
git clone https://github.com/torch/distro.git . --recursive

下载好后,安装依赖包然后,安装源文件:

代码语言:javascript
代码运行次数:0
运行
复制
./install-deps
./install.sh

因为我们使用的是cuda9.0以上,如果直接安装torch的话需要遇到这种问题:

代码语言:javascript
代码运行次数:0
运行
复制
...
[ 15%] Building NVCC (Device) object lib/THC/CMakeFiles/THC.dir/THC_generated_THCTensorMathReduce.cu.o
2 errors detected in the compilation of "/tmp/tmpxft_00002141_00000000-4_THCTensorMath.cpp4.ii".
CMake Error at THC_generated_THCTensorMath.cu.o.cmake:267 (message):
  Error generating file
  /home/ubuntu/torch/extra/cutorch/build/lib/THC/CMakeFiles/THC.dir//./THC_generated_THCTensorMath.cu.o


lib/THC/CMakeFiles/THC.dir/build.make:112: recipe for target 'lib/THC/CMakeFiles/THC.dir/THC_generated_THCTensorMath.cu.o' failed
make[2]: *** [lib/THC/CMakeFiles/THC.dir/THC_generated_THCTensorMath.cu.o] Error 1
make[2]: *** Waiting for unfinished jobs....
^Clib/THC/CMakeFiles/THC.dir/build.make:105: recipe for target 'lib/THC/CMakeFiles/THC.dir/THC_generated_THCTensorCopy.cu.o' failed
make[2]: *** [lib/THC/CMakeFiles/THC.dir/THC_generated_THCTensorCopy.cu.o] Interrupt
lib/THC/CMakeFiles/THC.dir/build.make:140: recipe for target 'lib/THC/CMakeFiles/THC.dir/THC_generated_THCTensorMathPairwise.cu.o' failed
make[2]: *** [lib/THC/CMakeFiles/THC.dir/THC_generated_THCTensorMathPairwise.cu.o] Interrupt
CMakeFiles/Makefile2:172: recipe for target 'lib/THC/CMakeFiles/THC.dir/all' failed
make[1]: *** [lib/THC/CMakeFiles/THC.dir/all] Interrupt
Makefile:127: recipe for target 'all' failed
make: *** [all] Interrupt

遇到这种错误是因为最新版的cuda和torch不兼容的问题,我们需要设置一下环境变量:

代码语言:javascript
代码运行次数:0
运行
复制
export TORCH_NVCC_FLAGS="-D__CUDA_NO_HALF_OPERATORS__"

设置好之后,执行:

代码语言:javascript
代码运行次数:0
运行
复制
 ./clean.sh 

清除之前的安装文件。

继续使用命令:./install.sh安装。

安装编译需要一会时间。

安装好后,会提示设置环境变量,输入yes就行。

然后执行source ~/.bashrc激活相应的环境变量。

这是在终端输入th就可以呼出torch的控制界面:

代码语言:javascript
代码运行次数:0
运行
复制
prototype@prototype-X299-UD4-Pro:~$ th
 
  ______             __   |  Torch7 
 /_  __/__  ________/ /   |  Scientific computing for Lua. 
  / / / _ \/ __/ __/ _ \  |  Type ? for help 
 /_/  \___/_/  \__/_//_/  |  https://github.com/torch 
                          |  http://torch.ch 
	
th> 

相关组件

配套torch的相关组件我们一般是Image图像处理库和Loadcaffe这个读取caffe预训练model的库。

安装Image图像处理库很简单:

代码语言:javascript
代码运行次数:0
运行
复制
luarocks install image

安装loadcaffe的时候可能会需要问题:

代码语言:javascript
代码运行次数:0
运行
复制
luarocks install loadcaffe

会遇到:

代码语言:javascript
代码运行次数:0
运行
复制
-- The C compiler identification is GNU 4.8.5
-- The CXX compiler identification is GNU 4.8.5
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Found Torch7 in /home/adityac/torch/install
-- Could NOT find PROTOBUF (missing: PROTOBUF_LIBRARY PROTOBUF_INCLUDE_DIR)
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
PROTOBUF_LIBRARY (ADVANCED)
linked by target "loadcaffe" in directory /tmp/luarocks_loadcaffe-1.0-0-7097/loadcaffe

-- Configuring incomplete, errors occurred!
make: *** No targets specified and no makefile found. Stop.

Error: Build error: Failed building.

这是我们缺少相应的环境包:

代码语言:javascript
代码运行次数:0
运行
复制
sudo apt-get install libprotobuf-dev protobuf-compiler

安装好这个再安装loadcaffe就没问题了。

这时就可以愉快地使用torch了。

下篇我会讲解如何已经学会Pytorch的基础上如何快速入门torch

参考资料

https://github.com/szagoruyko/loadcaffe/issues/76

https://github.com/torch/cutorch/issues/797

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-6-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 安装
    • 相关组件
  • 参考资料
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档