WSL AI工具以及开发环境的准备

2024-06-24 08:36
文章标签 工具 ai 开发 环境 准备 wsl

本文主要是介绍WSL AI工具以及开发环境的准备,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 安装VSCode,Visual Studio 或者 Jetbrain
  • 安装CUDA和CUDNN
  • 准备WSL环境
    • 1. 安装WSL
    • 2. 为WSL准备CUDA的Toolkits
  • 安装Miniconda
  • 一些值得安装的Dockers镜像
    • 1. Tensorflow-GPU
    • 2. LabelStudio

随着技术进步与完备,现在利用Windows进行AI或者算法原型的开发和研究已经逐渐成为一种可选项,甚至是一种极其推荐的方式。如果你本人对此也有兴趣,那么下面这些内容可能会对你有所帮助。

安装VSCode,Visual Studio 或者 Jetbrain

VSCode 是比较推荐的IDE,不仅因为它免费,更重要是因为它有诸多强大的可供选择插件。如果你需要开发C程序,并且想使用VSCode作为你的主力开发工具,你还需要为此安装和准备CMake

https://cmake.org/download/

当然 Visual Studio 也是可以考虑的选项,如果你没钱支付高昂的授权费,那么可以安装Community Edition的版本

https://visualstudio.microsoft.com/downloads/

如果预算比较充足,考虑Jetbrain全家桶也是可行的。

https://www.jetbrains.com/zh-cn/

安装CUDA和CUDNN

CUDA已经是目标AI或者并行计算的事实上的标准,所以如果你的电脑有Nvidia的显卡,那么你应该为你的环境准备一下CUDA和CUDNN。

首先从下面的链接,安装CUDA

https://developer.nvidia.com/cuda-toolkit

然后安装CUDNN

https://developer.nvidia.cn/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文件中。

export PATH=/usr/local/cuda/bin:$PATH
export LD_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, const int *a, const int *b, unsigned int size);__global__ void addKernel(int *c, const int *a, const int *b) {int i = threadIdx.x;c[i] = a[i] + b[i];
}int chooseCudaDevice() {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;
}int main() {const int arraySize = 5;const int a[arraySize] = {1, 2, 3, 4, 5};const int b[arraySize] = {10, 20, 30, 40, 50};int c[arraySize] = {0};int selectedDevice = chooseCudaDevice();if (selectedDevice == -1) {return 1;}// 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?");return 1;}cudaStatus = addWithCuda(c, a, b, arraySize);if (cudaStatus != cudaSuccess) {fprintf(stderr, "addWithCuda failed!");return 1;}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!");return 1;}return 0;
}cudaError_t addWithCuda(int *c, const int *a, const int *b, unsigned int 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的官方说明,执行必要的安装过程。

https://docs.anaconda.com/miniconda/miniconda-install/

安装完CUDA后,可以开始考虑依次安装PyTorch或者Tensorflow,具体的安装过程可以参考官方说明

Meta 家的Torch

https://pytorch.org/

Google家的Tensorflow

https://www.tensorflow.org/

Intel家的OpenVino

https://www.intel.cn/content/www/cn/zh/developer/tools/openvino-toolkit/overview.html

百度家的paddlepaddle

https://www.paddlepaddle.org.cn/

由于你已经准备好了WSL,并且空间还冗余的话,那么可以考虑再装一个Docker。因为一些工具库,或者项目因为缺少维护或者其他原因,已经不能在较新版本的CUDA下运行。

一些值得安装的Dockers镜像

这里有一些比较值得你安装的Dockers镜像

1. Tensorflow-GPU

不知道为什么Google家的Tensorflow GPU版本的更新进度一直比CPU版本的要慢,因此使用Dockers镜像会更合适一些

docker pull tensorflow/tensorflow                     # latest stable release
docker pull tensorflow/tensorflow:devel-gpu           # nightly dev release w/ GPU support
docker 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参数的一些信息,可以从官网找到

https://labelstud.io/

这篇关于WSL AI工具以及开发环境的准备的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1089646

相关文章

Spring AI集成DeepSeek的详细步骤

《SpringAI集成DeepSeek的详细步骤》DeepSeek作为一款卓越的国产AI模型,越来越多的公司考虑在自己的应用中集成,对于Java应用来说,我们可以借助SpringAI集成DeepSe... 目录DeepSeek 介绍Spring AI 是什么?1、环境准备2、构建项目2.1、pom依赖2.2

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

Deepseek R1模型本地化部署+API接口调用详细教程(释放AI生产力)

《DeepseekR1模型本地化部署+API接口调用详细教程(释放AI生产力)》本文介绍了本地部署DeepSeekR1模型和通过API调用将其集成到VSCode中的过程,作者详细步骤展示了如何下载和... 目录前言一、deepseek R1模型与chatGPT o1系列模型对比二、本地部署步骤1.安装oll

Spring AI Alibaba接入大模型时的依赖问题小结

《SpringAIAlibaba接入大模型时的依赖问题小结》文章介绍了如何在pom.xml文件中配置SpringAIAlibaba依赖,并提供了一个示例pom.xml文件,同时,建议将Maven仓... 目录(一)pom.XML文件:(二)application.yml配置文件(一)pom.xml文件:首

Java数字转换工具类NumberUtil的使用

《Java数字转换工具类NumberUtil的使用》NumberUtil是一个功能强大的Java工具类,用于处理数字的各种操作,包括数值运算、格式化、随机数生成和数值判断,下面就来介绍一下Number... 目录一、NumberUtil类概述二、主要功能介绍1. 数值运算2. 格式化3. 数值判断4. 随机

使用Navicat工具比对两个数据库所有表结构的差异案例详解

《使用Navicat工具比对两个数据库所有表结构的差异案例详解》:本文主要介绍如何使用Navicat工具对比两个数据库test_old和test_new,并生成相应的DDLSQL语句,以便将te... 目录概要案例一、如图两个数据库test_old和test_new进行比较:二、开始比较总结概要公司存在多

SpringBoot整合DeepSeek实现AI对话功能

《SpringBoot整合DeepSeek实现AI对话功能》本文介绍了如何在SpringBoot项目中整合DeepSeekAPI和本地私有化部署DeepSeekR1模型,通过SpringAI框架简化了... 目录Spring AI版本依赖整合DeepSeek API key整合本地化部署的DeepSeek

Java中基于注解的代码生成工具MapStruct映射使用详解

《Java中基于注解的代码生成工具MapStruct映射使用详解》MapStruct作为一个基于注解的代码生成工具,为我们提供了一种更加优雅、高效的解决方案,本文主要为大家介绍了它的具体使用,感兴趣... 目录介绍优缺点优点缺点核心注解及详细使用语法说明@Mapper@Mapping@Mappings@Co

在Mysql环境下对数据进行增删改查的操作方法

《在Mysql环境下对数据进行增删改查的操作方法》本文介绍了在MySQL环境下对数据进行增删改查的基本操作,包括插入数据、修改数据、删除数据、数据查询(基本查询、连接查询、聚合函数查询、子查询)等,并... 目录一、插入数据:二、修改数据:三、删除数据:1、delete from 表名;2、truncate

基于Python开发PPTX压缩工具

《基于Python开发PPTX压缩工具》在日常办公中,PPT文件往往因为图片过大而导致文件体积过大,不便于传输和存储,所以本文将使用Python开发一个PPTX压缩工具,需要的可以了解下... 目录引言全部代码环境准备代码结构代码实现运行结果引言在日常办公中,PPT文件往往因为图片过大而导致文件体积过大,