Install Nvidia Apex
若第一次安装需要把项目从github克隆到本地
Clean the old install before rebuilding:
pip uninstall apex
cd apex_repo_dir
rm -rf build (if present)
rm -rf apex.egg-info (if present)
Install package:
注:–no-cache-dir功能: 1. 不使用缓存在pip目录下的cache中的文件;2.也不保存在安装过程中需要的或者下次重新安装时需要的一些文件
pip install -v --no-cache-dir --global-option=“–cpp_ext” --global-option=“–cuda_ext” ./
或者
python setup.py install --cuda_ext --cpp_ext
扩展1. pip install --editable .
vs python setup.py develop
转载自
Try to avoid calling setup.py directly, it will not properly tell pip that you’ve installed your package.
With pip install -e:
其中 -e 选项全称是–editable. For local projects, the “SomeProject.egg-info” directory is created relative to the project path (相对于此项目目录的路径). This is one advantage over just using setup.py develop, which creates the “egg-info” directly relative the current working directory (相对于当前工作环境目录的路径).
扩展2. 弄懂一个命令 pip3 install --editable '.[train,test]'
例子在这里
When you then did pip install --editable .
, the command installs the Python package in the current directory
(signified by the dot .) with the optional dependencies needed for training and
testing (‘[train,test]’). 上面的安装命令中,-e选项全称是–editable,也就是可编辑的意思,以可继续开发的模式进行安装, ‘.’ 表示当前目录,也就是setup.py存在的
那个目录,此时pip install将会把包安装在当前文件目录下,而不是安装到所使用的python环境中的-site-packages。
[train,test] 只是我们举的一个例字,是可选参数,在setup.py中可以找到这两个选项(也可能叫其他名字或者根本就没有)之下包含了哪些第三方包。
扩展3. 别忘了pip其他丰富的功能
例如把clone和安装某个github上项目的命令压缩成一个
pip install git+http://127.0.0.1/xxx/demo.git
pip命令下直接制定所用的本地代理和镜像源
pip install --proxy http://127.0.0.1:8003 --user --index-url https://pypi.mirrors.ustc.edu.cn/simple/ einops
扩展4. 关于egg-info
注意⚠️:选则本地安装pip install .
成功安装完成后,apex.egg-info文件夹可以只处于当前项目文件夹下而不是安装在系统环境中,只需要在当前使用的python虚拟环境-site-packages中一个指向该egg-info文件的超链接即可(这个是在本地安装自动的行为,不需要我们关心操作),这样就能找到使用Apex包时所需的apex.egg-info文件夹里的信息。
PS: 如果遇到Cuda版本不兼容的问题,解决办法见(升级pytoch 1.3后 cuda10.1不匹配版本的警告已经消失):
https://github.com/NVIDIA/apex/issues/350#issuecomment-500390952
如果没有出现其他error,可以直接使用上面链接的建议,删除版本检查抛出的报错。见以下讨论:
https://github.com/NVIDIA/apex/issues/350
https://github.com/NVIDIA/apex/pull/323
Apex的使用
命令行启动训练
—也是如何Pycharm运行时添加命令行参数的例子
python -m torch.distributed.launch --nproc_per_node=4 train_distributed.py