admin 发布的文章

一、cuda-driver下载安装

1.查看cuda-driver是否安装

nvidia-smi来查看驱动是否安装
如果没有安装,可通过cuda-toolkit下载,里面包含了驱动一起安装
基于cuda-toolkit下载,里面包含了驱动,主要在里面下载对应的版本。
https://developer.nvidia.com/cuda-toolkit-archive

2.问题

问题1.
安装驱动报错:
/var/log/nvidia-installer.log

ERROR: Unable to find the kernel source tree for the currently running kernel.  Please make sure you have installed the kernel source files for your kernel and that they are properly configured; on Red Hat Linux systems, for example, be sure you have the 'kernel-source' or 'kernel-devel' RPM installed.  If you know the correct kernel source files are installed, you may specify the kernel source path with the '--kernel-source-path' command line option.
ERROR: Installation has failed.  Please see the file '/var/log/nvidia-installer.log' for details.  You may find suggestions on fixing installation problems in the README available on the Linux driver download page at www.nvidia.com.

解决办法:

yum install kernel-devel-$(uname -r)

sh cuda_12.2.2_535.104.05_linux.run --kernel-source-path=/usr/src/kernels/3.10.0-1160.el7.x86_64/

二、cuda-toolkit下载安装包

此处的安装环境为离线环境,需要先下载cuda安装文件,安装文件可以去官网地址下载对应的系统版本。官网下载地址:https://developer.nvidia.com/cuda-toolkit-archive

1143511.png

驱动和cuda版本对应:
https://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html

- ./NVIDIA-Linux-x86_64-495.46.run --kernel-source-path=/usr/src/kernels/$(uname -r) -k $(uname -r) --dkms -s

-

问题 centos7

Using built-in stream user interface
-> Detected 32 CPUs online; setting concurrency level to 32.
-> The file '/tmp/.X0-lock' exists and appears to contain the process ID '2647' of a running X server.
ERROR: You appear to be running an X server; please exit X before installing.  For further details, please see the section INSTALLING THE NVIDIA DRIVER in the README available on the Linux driver download page at www.nvidia.com.
ERROR: Installation has failed.  Please see the file '/var/log/nvidia-installer.log' for details.  You may find suggestions on fixing installation problems in the README available on the Linux driver download page at www.nvidia.com.

解决:
systemctl stop gdm.service

问题 centos8

-> Detected 128 CPUs online; setting concurrency level to 32.
-> Tagging shared libraries with chcon -t textrel_shlib_t.
ERROR: An NVIDIA kernel module 'nvidia-uvm' appears to already be loaded in your kernel.  This may be because it is in use (for example, by an X server, a CUDA program, or the NVIDIA Persistence Daemon), but this may also happen if your kernel was configured without support for module unloading.  Please be sure to exit any programs that may be using the GPU(s) before attempting to upgrade your driver.  If no GPU-based programs are running, you know that your kernel supports module unloading, and you still receive this message, then an error may have occurred that has corrupted an NVIDIA kernel module's usage count, for which the simplest remedy is to reboot your computer.
ERROR: Installation has failed.  Please see the file '/var/log/nvidia-installer.log' for details.  You may find suggestions on fixing installation problems in the README available on the Linux driver download page at www.nvidia.com.
~

解决:
GPU正在使用,关闭正在使用的GPU

问题 centos 8

Using built-in stream user interface
-> Detected 128 CPUs online; setting concurrency level to 32.
-> Tagging shared libraries with chcon -t textrel_shlib_t.
ERROR: An NVIDIA kernel module 'nvidia' appears to already be loaded in your kernel.  This may be because it is in use (for example, by an X server, a CUDA program, or the NVIDIA Persistence Daemon), but this may also happen if your kernel was configured without support for module unloading.  Please be sure to exit any programs that may be using the GPU(s) before attempting to upgrade your driver.  If no GPU-based programs are running, you know that your kernel supports module unloading, and you still receive this message, then an error may have occurred that has corrupted an NVIDIA kernel module's usage count, for which the simplest remedy is to reboot your computer.
ERROR: Installation has failed.  Please see the file '/var/log/nvidia-installer.log' for details.  You may find suggestions on fixing installation problems in the README available on the Linux driver download page at www.nvidia.com.

解决:
GPU正在使用,关闭正在使用的GPU,通过命令:
sudo lsof /dev/nvidia*
kill -9 pid

问题:

sh ./cuda_11.6.0_510.39.01_linux.run
Extraction failed.
Ensure there is enough space in /tmp and that the installation package is not corrupt
Signal caught, cleaning up

没有安装解压软件
yum install tar

问题 centos

nvidia-smi 中可以显示GPU,但是torch.cuda.is_available() 出现如下错误:

UserWarning: CUDA initialization: Unexpected error from cudaGetDeviceCount(). Did you run some cuda functions before calling

Error 802: system not yet initialized 

centos8中解决办法

注意 驱动版本要对应上

wget https://developer.download.nvidia.cn/compute/cuda/repos/rhel8/x86_64/nvidia-fabric-manager-515.65.01-1.x86_64.rpm

sudo yum install nvidia-fabric-manager-515.65.01-1.x86_64.rpm

systemctl enable nvidia-fabricmanager

systemctl restart nvidia-fabricmanager

systemctl status nvidia-fabricmanager

安装完成后验证:

安装后的位置: /usr/local/下面

nvcc -v

三、cudnn安装

地址:https://developer.nvidia.com/rdp/cudnn-archive

选择:
(1)根据cuda的情况选择版本,例如12.x选择
Download cuDNN v8.9.7 (December 5th, 2023), for CUDA 12.x
(2)选择下载格式,一般都选择tar包
Local Installer for Linux x86_64 (Tar)

四、cuda 版本切换

一般情况下从官网下载:https://developer.nvidia.com/cuda-toolkit-archive

注意安装的时候:不要安装cuda driver

安装完成后切换软连接:

rm -rf /usr/local/cuda  #删除之前创建的软链接 
sudo ln -s /usr/local/cuda-11.3/  /usr/local/cuda/ 
nvcc --version #查看当前 cuda 版本

如果还不行直接在环境变量中修改:

vim ~/.bashrc

#然后添加

export CUDA_HOME=/usr/local/cuda
export PATH=$PATH:/usr/local/cuda/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-11.3/lib64

遇见过一种情况还不行:

查看 which nvcc 发现指向没有变 可能就是环境变量没有改过来
查看环境变量:

echo $PATH
## 打印:/home/centos/anaconda3/bin:/home/centos/anaconda3/condabin:/home/centos/.local/bin:/home/centos/bin:/usr/local/cuda-12.2/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/usr/local/cuda/bin

##发现环境变量没有变,需要将固定指向的环境变量修改:

export PATH=/home/centos/anaconda3/bin:/home/centos/anaconda3/condabin:/home/centos/.local/bin:/home/centos/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/usr/local/cuda/bin

一、创建虚拟环境

1.1 创建 exp_detect_1 的虚拟环境:

conda create -n exp_detect_1 python=3.7

或者 直接激活虚拟环境 exp_detect_1:
conda activate exp_detect_1
(注意:退出环境的命令为:#conda deactivate)

二、虚拟环境打包

1.3 打包压缩虚拟环境 exp_detect_1
go to 此目录
cd /home/liu/miniconda3/envs/
打包压缩
tar -czvf exp_detect_1.tar.gz ./exp_detect_1

三、打包上传,解压

解压 exp_detect_1.tar.gz 到 ~/miniconda3/envs 环境目录。
cd ~/miniconda3/envs
tar -xzvf exp_detect_1.tar.gz
激活 conda 虚拟环境> exp_detect_1
conda activate exp_detect_1
python xxx

OK, run the source package

四、注意事项

怎么快速迁移 “训练环境”补丁-1

一、存在的问题
在使用文档“怎么快速迁移训练环境.pdf”中存在两个问题:
1.直接拷贝虚拟环境,部分文件路径没有修改
2.cudatoolkit包没有拷贝

二、对应的解决办法如下:
1.直接拷贝虚拟环境,部分文件路径没有修改
需要修改的文件所在位置:/home/anaconda3/envs/pretrain/bin (注意不同服务器conda安装位置可能不太一样)

图片1.png

修改文件:此目录下会用到的文件例如:pip、deepspeed(根据自己实际需求)
图片2.png

修改方式:将文件首行修改为当前文件实际路径

2.cudatoolkit包没有拷贝
如果虚拟环境的cudatoolkit是用过conda install 方式安装,例如下面的方式:
conda install pytorch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1 cudatoolkit=11.3 -c pytorch
实际上cudatoolkit的安装位置不在拷贝的虚拟环境中,而是在conda公用目录pkgs文件夹下(/home/anaconda3/pkgs/),注意:此目录下可能共存多个cudatoolkit包,通过如下方式选择拷贝哪一个。

在原主机中激活待拷贝的虚拟环境:
conda activate pretrain6
查看cudatoolkit的build id:
conda list cudatoolkit

图片3.png

到pkgs目前查找对应的cudatoolkit包:
cd /home/anaconda3/pkgs/
ls -hl|grep “cudatoolkit”
图片4.png

打包,压缩到:
zip -r cudatoolkit-11.3.1-h9edb442_10.zip cudatoolkit-11.3.1-h9edb442_10
拷贝到目标虚拟环境对应位置(/home/anaconda3/pkgs/)解压缩:
unzip cudatoolkit-11.3.1-h9edb442_10.zip

三、torch安装的两种方式和推荐方式
安装方式官方链接:
https://pytorch.org/get-started/previous-versions/#installing-previous-versions-of-pytorch
1.linux环境下两种安装方式
官方文档中有两种安装方式:
第一种是通过conda安装,例如:
conda install pytorch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1 cudatoolkit=11.3 -c pytorch
第二种是通过pip安装,例如;
pip install torch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1

这两种安装方式的区别在于:
通过conda安装会安装cudatoolkit,而通过pip安装则不会。

2.推荐安装方式:conda install
通过conda install 安装方式会适配pytorch,torchvision,torchaudio,cudatoolkit的版本,如果版本不对应,就安装不成功。

用pip install 不能安装cudatoolkit,只能安装pytorch,torchvision,torchaudio,调用的是主机的cuda,可能存在cuda和torch不匹配问题。

可能遇见的问题

1.openssl

OpenSSL 3.0's legacy provider failed to load. This is a fatal error by default, but cryptography supports running without legacy algorithms by setting the environment variable CRYPTOGRAPHY_OPENSSL_NO_LEGACY. If you did not expect this error, you have likely made a mistake with your OpenSSL configuration. 

修改方式:
在虚拟环境下直接执行:export CRYPTOGRAPHY_OPENSSL_NO_LEGACY=1

一、题目列表:
题目1、

二、题目
题目1、
给你一根长度为 n 的绳子,请把绳子剪成整数长的 m 段( m 、 n 都是整数, n > 1 并且 m > 1 , m <= n ),每段绳子的长度记为 k[1],...,k[m] 。请问 k[1]k[2]...*k[m] 可能的最大乘积是多少?例如,当绳子的长度是 8 时,我们把它剪成长度分别为 2、3、3 的三段,此时得到的最大乘积是 18 。

数据范围: 2 le n le 602≤n≤60
进阶:空间复杂度 O(1)O(1) ,时间复杂度 O(n)O(n)

思路:
为了解决这个问题,我们可以采用一个贪心策略,尽量将绳子剪成尽可能多的3(除了最后可能剩下1或2的情况)。这种策略基于数学上的观察:在相同的总和下,将数字拆分成更多的3(或其他大于2的数)通常能得到更大的乘积,因为乘法的增长是指数级的。

2.代码

# -*- coding:utf-8 -*-
class Solution:
    def cutRope(self, number):
        # write code here
        if number == 1:
            return 1
        if number == 2:
            return 1
        if number == 3:
            return 2
        n = number % 3
        b = number // 3
        if n == 0:
            return 3 ** b
        if n == 1:
            return 3 ** (b - 1) * 4
        if n == 2:
            return 3 ** b * 2

生成模型(Generative Model)与判别模型(Discriminative Model)在机器学习领域是两种不同类型的模型,它们之间存在着显著的差异。以下是对这两种模型区别的详细分析:

一、定义与目的

生成模型:生成模型是概率统计和机器学习中的一类重要模型,指一系列用于随机生成可观测数据的模型。在给定某些隐含参数的条件下,它能够随机生成观测数据,并给观测值和标注数据序列指定一个联合概率分布。生成模型的应用十分广泛,可以用来对不同的数据进行建模,如图像、文本、声音等。
判别模型:判别模型是一种对未知数据y与已知数据x之间关系进行建模的方法,直接对条件概率p(y|x;θ)建模。在机器学习领域,判别模型是一种基于概率理论的方法,已知输入变量x,通过构建条件概率分布P(y|x)来预测y。

二、主要区别

1、优化准则不同
生成模型:优化训练数据的联合分布概率P(X,Y)。
判别模型:优化训练数据的条件分布概率P(Y|X)。
2、对观察序列的处理不同
生成模型:将观察序列作为模型的一部分。
判别模型:将观察序列仅作为条件。
3、训练复杂度
由于需要归一化,判别模型的训练复杂度通常较高。
生成模型的训练复杂度相对较低。
4、是否支持无指导训练
生成模型支持无指导训练,即可以在没有标签数据的情况下进行训练。
判别模型通常不支持无指导训练,它依赖于标签数据来构建条件概率分布。
5、本质区别
判别模型:估计的是条件概率分布p(class|context),即给定观测数据x,模型预测目标变量y的条件概率。
生成模型:估计的是联合概率分布p(x,y),即同时考虑观测数据x和目标变量y的联合分布。
6、模型应用
生成模型:由于能够模拟数据的生成过程,因此在无监督学习任务(如聚类、异常检测)中表现较好。同时,生成模型在刻画复杂学习任务中的依赖关系方面也更加灵活。
判别模型:由于直接对条件概率建模,因此在有监督学习任务(如分类、回归)中通常能够取得更好的效果。特别是当不考虑x与y之间的联合分布时,判别模型可以更加专注于学习x到y的映射关系。
三、典型模型
生成模型:高斯混合模型(Gaussian Mixture Model, GMM)、隐马尔可夫模型(Hidden Markov Model, HMM)、朴素贝叶斯分类器(Naive Bayes Classifier)等。
判别模型:线性回归模型、线性判别分析(Linear Discriminant Analysis, LDA)、支持向量机(Support Vector Machine, SVM)、神经网络(Neural Network, NN)等。
综上所述,生成模型和判别模型在定义、优化准则、对观察序列的处理、训练复杂度、是否支持无指导训练以及模型应用等方面都存在显著的差异。选择哪种模型取决于具体的应用场景和需求。