如何用 Hugging Face Inference Endpoints 为 Flux 卸载 VAE 解码(remote_decode)
如何用 Hugging Face Inference Endpoints 为 Flux 卸载 VAE 解码remote_decode【免费下载链接】diffusers Diffusers: State-of-the-art diffusion models for image, video, and audio generation in PyTorch.项目地址: https://gitcode.com/GitHub_Trending/di/diffusers在本地跑 Flux 时VAE 解码一步的显存开销可能让整条推理链吃紧。diffusers 的 Remote inference混合推理功能可以把 VAE 解码这一步整体放到 Hugging Face Inference Endpoints 上执行本地 pipeline 只产出 latent解码由远端 endpoint 完成从而放宽本地推理大模型时的显存要求。该功能基于remote_decode函数实现目前官方文档标注为experimental feature。本文聚焦一条主路径用FluxPipeline加载black-forest-labs/FLUX.1-schnell把output_type设为latent再把 latent 交给remote_decode换回可保存的图片。前提条件已安装 diffusers并能访问文档中列出的公共 Flux endpoint下表中的地址与 checkpoint 均来自项目文档的支持矩阵。Flux 对应的 endpoint 同时支持 encode/decode本文只用 decode。模型EndpointCheckpoint支持Fluxhttps://whhx50ex1aryqvw6.us-east-1.aws.endpoints.huggingface.cloudblack-forest-labs/FLUX.1-schnellencode/decode完整的模型支持矩阵见 Remote inference 文档。步骤一以 vaeNone 加载 Flux pipeline解码卸载的前提是本地不加载 VAE在from_pretrained时把vae置为None这样 pipeline 不会在本地占用 VAE 权重。文档示例使用torch.bfloat16精度device_map可按本机条件取cuda、mps、xpu或cpu。from diffusers import FluxPipeline pipeline FluxPipeline.from_pretrained( black-forest-labs/FLUX.1-schnell, dtypetorch.bfloat16, vaeNone, device_mapcuda # or mps, xpu, cpu )步骤二让 pipeline 输出 latent把 pipeline 的output_type设为latent此时调用返回的.images就是尚未解码的 latent而不是 PIL 图像prompt A photorealistic Apollo-era photograph of a cat in a small astronaut suit with a bubble helmet, standing on the Moon and holding a flagpole planted in the dusty lunar soil. latent pipeline( promptprompt, guidance_scale0.0, num_inference_steps4, output_typelatent, ).images上面这段 prompt 与guidance_scale0.0、num_inference_steps4均为文档示例值可换成你自己的提示词和步数。步骤三调用 remote_decode 完成远端解码Flux 的 latent 是 packed 形式三维张量例如 1024×1024 输出对应形状(1, 4096, 64)见 测试用例因此remote_decode必须额外传入height和width否则函数内部检查会直接抛出ValueErrorheightandwidthrequired for packed latents.。两个缩放参数按 API 参考 中给出的 Flux 取值scaling_factor0.3611、shift_factor0.1159。传入它们后远端会替你应用缩放latents / scaling_factor与移位latents shift_factor传None则要求输入已经完成缩放。from diffusers.utils import remote_decode image remote_decode( endpointhttps://whhx50ex1aryqvw6.us-east-1.aws.endpoints.huggingface.cloud/, tensorlatent, height1024, width1024, scaling_factor0.3611, shift_factor0.1159, ) image.save(image.jpg)image是一个 PIL 图像默认output_typepil、image_formatjpg。改成image_formatpng即可让 endpoint 返回 PNG 字节流。解码结果验证文档给出的判定方式很直接正常路径remote_decode返回 PIL 图像可image.save(...)落盘对 packed latent 场景输出图像尺寸应等于传入的height/width仓库测试对 1024×1024 的输入正是断言output.height 1024、output.width 1024。失败路径remote_decode在收到非 2xx 响应时抛出RuntimeError内容是该次请求返回的 JSON见 remote_utils 实现。也就是说连接或请求出错不会静默返回 None而是以异常形式暴露方便在脚本里捕获。可选分支与限制减少传输量按 API 文档的推荐output_typept配合partial_postprocessTrue是“最小传输 全质量”的组合endpoint 直接回传uint8图像张量无需本地processoroutput_typept、partial_postprocessFalse则回传未反归一化的float16/bfloat16张量对第三方后处理代码兼容性最好默认output_typepilimage_formatjpg传输量最小。废弃参数do_scaling已废弃文档要求改传scaling_factor/shift_factorinput_tensor_type/output_tensor_type的base64取值也已废弃当前只走binary传输。排队处理多请求官方文档演示了用queue 后台线程连续提交 latent 解码的做法decode 与下一轮生成重叠可参考 overview 的 Queuing 一节。编码方向同一 endpoint 也支持remote_encode图像/PIL 或张量进latent 张量出Flux 对应的scaling_factor0.3611、shift_factor0.1159与解码一致img2img 场景可复用同一 endpoint。需要注意的是整个 Remote inference 目前是实验性功能文档明确欢迎通过 issue 反馈问题本文使用的 endpoint 是文档中登记的公共地址其部署模型可能随时间更新仓库的慢速测试注释也提到参考切片会随端侧重部署发生漂移因此以实际输出为准不要把任何单一输出当成固定预期。【免费下载链接】diffusers Diffusers: State-of-the-art diffusion models for image, video, and audio generation in PyTorch.项目地址: https://gitcode.com/GitHub_Trending/di/diffusers创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考