CuPy记录
因传统的Numpy运算速度较慢,使用其镜像GPU加速版本Cupy
官网
https://docs.cupy.dev/en/stable/index.html
Project Goal
The goal of the CuPy project is to provide Python users GPU acceleration capabilities, without the in-depth knowledge of underlying GPU technologies. The CuPy team focuses on providing:
A complete NumPy and SciPy API coverage to become a full drop-in replacement, as well as advanced CUDA features to maximize the performance.
Mature and quality library as a fundamental package for all projects needing acceleration, from a lab environment to a large-scale cluster.
安装
https://docs.cupy.dev/en/stable/install.html
需要安装CUDA Toolkit,我之前安装PyTorch环境时已经安装过,在NVIDIA控制面板或者运行下列代码查看CUDA版本:
import torch
print(torch.version.cuda)
选择对应CuPy版本进行安装,我这里是11.7(我在Anaconda Prompt中安装)
pip install cupy-cuda11x
之前安装PyTorch时候CUDA还是11.7,不知什么时候升级了到了12.0好像:
验证安装
可以开始使用了!
注意:Move array from a device to the host(使用gdal输出的时候):
https://docs.cupy.dev/en/stable/user_guide/basic.html
x_gpu = cp.array([1, 2, 3]) # create an array in the current device
x_cpu = cp.asnumpy(x_gpu) # move the array to the host.
x_cpu = x_gpu.get()
numpy 和 cumpy可以相互转换,读取的时候只能被其中一个读取(下边#1 读取了 #2就读取不到内容了):
f = open(dir, 'rb')
f_int = np.fromfile(f, dtype=np.int16) # 1
f_int2 = cp.fromfile(f, dtype=cp.int16) # 2
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理