文章目录
随着技术进步与完备,现在利用Windows进行AI或者算法原型的开发和研究已经逐渐成为一种可选项,甚至是一种极其推荐的方式。如果你本人对此也有兴趣,那么下面这些内容可能会对你有所帮助。
安装VSCode,Visual Studio 或者 Jetbrain
VSCode 是比较推荐的IDE,不仅因为它免费,更重要是因为它有诸多强大的可供选择插件。如果你需要开发C程序,并且想使用VSCode作为你的主力开发工具,你还需要为此安装和准备CMake
当然 Visual Studio 也是可以考虑的选项,如果你没钱支付高昂的授权费,那么可以安装Community Edition的版本
如果预算比较充足,考虑Jetbrain全家桶也是可行的。
安装CUDA和CUDNN
CUDA已经是目标AI或者并行计算的事实上的标准,所以如果你的电脑有Nvidia的显卡,那么你应该为你的环境准备一下CUDA和CUDNN。
首先从下面的链接,安装CUDA
然后安装CUDNN
准备WSL环境
当这些都准备完毕后,可以考虑安装WSL,也就是Windows的Linux子系统,你需要依次执行下面的过程。
1. 安装WSL
如果没有安装过WSL,那么首先你需要先打开Powershell,或者CMD,然后输入
wsl --install
这个过程一般需要耗费一点时间,并为你的系统安装所需的全部组件。之后,建议你把WSL设置为版本2.
wsl --set-default-version 2
若要指定运行 Linux 发行版的 WSL 版本(1 或 2),请将 替换为发行版的名称,并将 替换为 1 或 2。 比较 WSL 1 和 WSL 2。 WSL 2 仅在 Windows 11 或 Windows 10 版本 1903、内部版本 18362 或更高版本中可用。
然后安装Linux子系统,这里推荐 Ubuntu-22.04
wsl --install-d Ubuntu-22.04
但是你依然可以执行以下命令,查看其他可选项
wsl --list--online
另外,你可使用下面的命令查看在本地安装好的Linux子系统
wsl --list--verbose
关于更多的详细操作,你也可以参考Microsoft的官网
https://learn.microsoft.com/zh-cn/windows/wsl/basic-commands#list-available-linux-distributions
2. 为WSL准备CUDA的Toolkits
安装好WSL后,建议你再次回到Nvidia的CUDA页面,下载WSL的工具包。因为你将能够在Windows直接体验到类似在Linux上编写CUDA代码的乐趣,或者运行某些模型的时候,会需要你安装WSL的CUDA工具包。
对了,安装完毕后,你有可能需要配置一下PATH环境,把下面的这段代码贴到.bashrc文件中。
exportPATH=/usr/local/cuda/bin:$PATHexportLD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
当安装完毕后,可以在子系统中编写下面的CUDA代码,并用NVCC工具编译。它可以检查你当前的系统有那些GPU计算卡可以使用,并进行一个简单的矩阵加法运算。
#include"cuda_runtime.h"#include"device_launch_parameters.h"#include<stdio.h>
cudaError_t addWithCuda(int*c,constint*a,constint*b,unsignedint size);
__global__ voidaddKernel(int*c,constint*a,constint*b){int i = threadIdx.x;
c[i]= a[i]+ b[i];}intchooseCudaDevice(){int deviceCount;cudaGetDeviceCount(&deviceCount);if(deviceCount ==0){fprintf(stderr,"No CUDA devices found!\n");return-1;}printf("Available CUDA devices:\n");for(int i =0; i < deviceCount;++i){
cudaDeviceProp prop;cudaGetDeviceProperties(&prop, i);printf("%d: %s\n", i, prop.name);}int deviceChoice;printf("Select the device number to use: ");if(scanf("%d",&deviceChoice)!=1|| deviceChoice <0|| deviceChoice >= deviceCount){fprintf(stderr,"Invalid device choice!\n");return-1;}return deviceChoice;}intmain(){constint arraySize =5;constint a[arraySize]={1,2,3,4,5};constint b[arraySize]={10,20,30,40,50};int c[arraySize]={0};int selectedDevice =chooseCudaDevice();if(selectedDevice ==-1){return1;}// Add vectors in parallel using the selected device.
cudaError_t cudaStatus =cudaSetDevice(selectedDevice);if(cudaStatus != cudaSuccess){fprintf(stderr,"cudaSetDevice failed! Do you have a CUDA-capable GPU installed?");return1;}
cudaStatus =addWithCuda(c, a, b, arraySize);if(cudaStatus != cudaSuccess){fprintf(stderr,"addWithCuda failed!");return1;}printf("{1,2,3,4,5} + {10,20,30,40,50} = {%d,%d,%d,%d,%d}\n",
c[0], c[1], c[2], c[3], c[4]);// cudaDeviceReset must be called before exiting in order for profiling// and tracing tools such as Nsight and Visual Profiler to show complete traces.
cudaStatus =cudaDeviceReset();if(cudaStatus != cudaSuccess){fprintf(stderr,"cudaDeviceReset failed!");return1;}return0;}
cudaError_t addWithCuda(int*c,constint*a,constint*b,unsignedint size){int*dev_a =0;int*dev_b =0;int*dev_c =0;
cudaError_t cudaStatus;// Allocate GPU buffers for three vectors (two input, one output).
cudaStatus =cudaMalloc((void**)&dev_c, size *sizeof(int));if(cudaStatus != cudaSuccess){fprintf(stderr,"cudaMalloc failed!");goto Error;}
cudaStatus =cudaMalloc((void**)&dev_a, size *sizeof(int));if(cudaStatus != cudaSuccess){fprintf(stderr,"cudaMalloc failed!");goto Error;}
cudaStatus =cudaMalloc((void**)&dev_b, size *sizeof(int));if(cudaStatus != cudaSuccess){fprintf(stderr,"cudaMalloc failed!");goto Error;}// Copy input vectors from host memory to GPU buffers.
cudaStatus =cudaMemcpy(dev_a, a, size *sizeof(int), cudaMemcpyHostToDevice);if(cudaStatus != cudaSuccess){fprintf(stderr,"cudaMemcpy failed!");goto Error;}
cudaStatus =cudaMemcpy(dev_b, b, size *sizeof(int), cudaMemcpyHostToDevice);if(cudaStatus != cudaSuccess){fprintf(stderr,"cudaMemcpy failed!");goto Error;}// Launch a kernel on the GPU with one thread for each element.
addKernel<<<1, size>>>(dev_c, dev_a, dev_b);// Check for any errors launching the kernel.
cudaStatus =cudaGetLastError();if(cudaStatus != cudaSuccess){fprintf(stderr,"addKernel launch failed: %s\n",cudaGetErrorString(cudaStatus));goto Error;}// cudaDeviceSynchronize waits for the kernel to finish, and returns any errors.
cudaStatus =cudaDeviceSynchronize();if(cudaStatus != cudaSuccess){fprintf(stderr,"cudaDeviceSynchronize returned error code %d after launching addKernel!\n", cudaStatus);goto Error;}// Copy output vector from GPU buffer to host memory.
cudaStatus =cudaMemcpy(c, dev_c, size *sizeof(int), cudaMemcpyDeviceToHost);if(cudaStatus != cudaSuccess){fprintf(stderr,"cudaMemcpy failed!");goto Error;}
Error:cudaFree(dev_c);cudaFree(dev_a);cudaFree(dev_b);return cudaStatus;}
安装Miniconda
之后下一步是配置Python的开发环境,推荐使用Miniconda,并且把相关环境安装到Windows中,而不是Linux子系统。究其原因是因为类似Jetbrain的全家桶,或者PyCharm之类的IDE并不支持WSL的CONDA环境。所以直接安装在Windows下会更好一些。如果在Linux子环境也存在需要运行Python程序的情况,可以依照Conda的官方说明,执行必要的安装过程。
安装完CUDA后,可以开始考虑依次安装PyTorch或者Tensorflow,具体的安装过程可以参考官方说明
Meta 家的Torch
Google家的Tensorflow
Intel家的OpenVino
https://www.intel.cn/content/www/cn/zh/developer/tools/openvino-toolkit/overview.html
百度家的paddlepaddle
由于你已经准备好了WSL,并且空间还冗余的话,那么可以考虑再装一个Docker。因为一些工具库,或者项目因为缺少维护或者其他原因,已经不能在较新版本的CUDA下运行。
一些值得安装的Dockers镜像
这里有一些比较值得你安装的Dockers镜像
1. Tensorflow-GPU
不知道为什么Google家的Tensorflow GPU版本的更新进度一直比CPU版本的要慢,因此使用Dockers镜像会更合适一些
docker pull tensorflow/tensorflow # latest stable releasedocker pull tensorflow/tensorflow:devel-gpu # nightly dev release w/ GPU supportdocker pull tensorflow/tensorflow:latest-gpu-jupyter # latest release w/ GPU support and Jupyter
我个人是比较推荐下载带Jupyter版本的镜像,主要使用起来比较方便,但是你也可以只使用GPU版本的。
运行带Jupyter版本的,可以执行下述指令:
docker run -it -p 8888:8888 tensorflow/tensorflow:latest-gpu-jupyter
然后在本地8888端口,就可以正常使用了。
2. LabelStudio
LabelStudio 是一个用来数据标记的工具,也是我比较推荐的。
docker pull heartexlabs/label-studio
关于配置Docker参数的一些信息,可以从官网找到
版权归原作者 打码的老程 所有, 如有侵权,请联系我们删除。