算法加速——GPU让降维算法UMAP再快30倍!

部署运行你感兴趣的模型镜像

一、环境安装

conda create -n gpu_umap_env -c conda-forge -c rapidsai -c nvidia \
    cuml=24.02 python=3.10 cudatoolkit=11.7 \
    -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/ \
    -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ \
    -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r/

二、激活环境

conda activate gpu_umap_env

三、安装umap库

pip install umap-learn

四、运行测试用例

import numpy as np
import time

# GPU 加速相关
try:
    from cuml import UMAP as GPU_UMAP
    import cupy as cp
    gpu_available = True
except ImportError:
    gpu_available = False

# CPU 版 UMAP
from umap import UMAP as CPU_UMAP

# 构造高维数据
n_samples = 10000
n_features = 512

print(f"\nGenerating synthetic data: shape=({n_samples}, {n_features})")
X_cpu = np.random.rand(n_samples, n_features)

# ----------------------------
# CPU 版测试
# ----------------------------
print("\nRunning CPU UMAP...")
cpu_start = time.time()
umap_cpu = CPU_UMAP(n_components=2, random_state=42)
embedding_cpu = umap_cpu.fit_transform(X_cpu)
cpu_time = time.time() - cpu_start
print(f"CPU UMAP finished in {cpu_time:.2f} seconds. Output shape: {embedding_cpu.shape}")

# ----------------------------
# GPU 版测试(如果可用)
# ----------------------------
if gpu_available:
    print("\nRunning GPU UMAP...")
    X_gpu = cp.asarray(X_cpu)  # 转为 GPU 数据
    gpu_start = time.time()
    umap_gpu = GPU_UMAP(n_components=2, random_state=42)
    embedding_gpu = umap_gpu.fit_transform(X_gpu)
    embedding_gpu_np = cp.asnumpy(embedding_gpu)  # 可选:转为 numpy 显示
    gpu_time = time.time() - gpu_start
    print(f"GPU UMAP finished in {gpu_time:.2f} seconds. Output shape: {embedding_gpu_np.shape}")
else:
    print("\n⚠️ GPU UMAP not available. Please install RAPIDS cuML.")

可以看到GPU的umap快了近30倍!
在这里插入图片描述

您可能感兴趣的与本文相关的镜像

Wan2.2-I2V-A14B

Wan2.2-I2V-A14B

图生视频
Wan2.2

Wan2.2是由通义万相开源高效文本到视频生成模型,是有​50亿参数的轻量级视频生成模型,专为快速内容创作优化。支持480P视频生成,具备优秀的时序连贯性和运动推理能力

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值