使用 RunPod GPU
RunPod 是提供 GPU 资源的一个平台,可以在平台上租 GPU 按小时计费。支持所有主流的 GPU,价格也还好。本文将介绍如何在 RunPod 启动 Notebook 运行模型,模型使用 Flux UpScaler,显存需求大于 24 G。登陆网站,注册并充值,https://www.runpod.io/
创建 POD
选择 GPU 和 Docker 镜像,镜像选择 runpod/pytorch:2.4.0-py3.11-cuda12.4.1-devel-ubuntu22.04。
默认硬盘只有 20 G,如果模型很大,需要进行修改存储尺寸,点击 Edit Template,存储是要收费的,公用就可以。
启动Pod
安装需要的依赖
!pip install transformers diffusers accelerate protobuf sentencepiece matplotlib
UpScale
代码来自huggging face 的例子,https://huggingface.co/jasperai/Flux.1-dev-Controlnet-Upscaler
import torch
from diffusers.utils import load_image
from diffusers import FluxControlNetModel
from diffusers.pipelines import FluxControlNetPipeline
from huggingface_hub import loginlogin(token = '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')
from huggingface_hub import snapshot_download
flux = snapshot_download("black-forest-labs/FLUX.1-dev",local_dir="./flux")
upscaler = snapshot_download("jasperai/Flux.1-dev-Controlnet-Upscaler",local_dir="./flux")
# Load pipeline
controlnet = FluxControlNetModel.from_pretrained(upscaler,torch_dtype=torch.bfloat16
)
pipe = FluxControlNetPipeline.from_pretrained(flux,controlnet=controlnet,torch_dtype=torch.bfloat16
)
pipe.to("cuda")# Load a control image
control_image = load_image("https://huggingface.co/jasperai/Flux.1-dev-Controlnet-Upscaler/resolve/main/examples/input.jpg"
)w, h = control_image.size# Upscale x4
control_image = control_image.resize((w * 4, h * 4))image = pipe(prompt="", control_image=control_image,controlnet_conditioning_scale=0.6,num_inference_steps=28, guidance_scale=3.5,height=control_image.size[1],width=control_image.size[0]
).images[0]
image
原始图片
import requests
from PIL import Image
from io import BytesIO
import matplotlib.pyplot as plt# URL of the image
url = "https://huggingface.co/jasperai/Flux.1-dev-Controlnet-Upscaler/resolve/main/examples/input.jpg"# Send a GET request to the URL
response = requests.get(url)# Check if the request was successful
if response.status_code == 200:# Open the image from the response contentimage = Image.open(BytesIO(response.content))# Display the imageplt.imshow(image)plt.axis('off') # Hide axesplt.show()
else:print(f"Failed to retrieve image. Status code: {response.status_code}")
总结
RunPod 可以支持大显存需求的模型,将 Runpod 镜像下载到本地先调试好,例如需要的类库等等,这样可以省时间,有很多平台的都 提供 GPU 租赁,原理都是相似的,就看哪家更便宜些,国内的算力平台更便宜,就是英伟达的卡不好租。