# 远程推理

远程推理提供对 [Inference Endpoint](https://huggingface.co/docs/inference-endpoints/index) 的访问，以便将解码和编码的本地生成需求卸载出去。

## remote_decode[[diffusers.utils.remote_decode]]

#### diffusers.utils.remote_decode[[diffusers.utils.remote_decode]]

```python
diffusers.utils.remote_decode(endpoint: str, tensor: 'torch.Tensor', processor: 'VaeImageProcessor' | 'VideoProcessor' | None = None, do_scaling: bool = True, scaling_factor: float | None = None, shift_factor: float | None = None, output_type: Literal['mp4', 'pil', 'pt'] = 'pil', return_type: Literal['mp4', 'pil', 'pt'] = 'pil', image_format: Literal['png', 'jpg'] = 'jpg', partial_postprocess: bool = False, input_tensor_type: Literal['binary'] = 'binary', output_tensor_type: Literal['binary'] = 'binary', height: int | None = None, width: int | None = None)
```

[源代码](https://github.com/huggingface/diffusers/blob/v0.43.0/src/diffusers/utils/remote_utils.py#L190)

**参数：**

endpoint (`str`) : 远程解码的端点。

tensor (`torch.Tensor`) : 要解码的张量。

processor (`VaeImageProcessor` 或 `VideoProcessor`，*可选*)：用于 `return_type="pt"`，以及视频模型的 `return_type="pil"`。

do_scaling (`bool`，默认值为 `True`，*可选*) : **已弃用**。**请改为传入 `scaling_factor`/`shift_factor`。** **在该选项被移除之前，若无需缩放，请仍设置 do_scaling=None/do_scaling=False** 当 `True` 时，会远程应用缩放，例如 `latents / self.vae.config.scaling_factor`。如果 `False`，则传入的输入必须已应用缩放。

scaling_factor (`float`, *可选*) : 传入时应用缩放，例如 [`latents / self.vae.config.scaling_factor`](https://github.com/huggingface/diffusers/blob/7007febae5cff000d4df9059d9cf35133e8b2ca9/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion.py#L1083C37-L1083C77)。 - SD v1: 0.18215 - SD XL: 0.13025 - Flux: 0.3611 如果为 `None`，则输入必须已应用缩放。

shift_factor (`float`, *可选*) : 传入时应用偏移，例如 `latents + self.vae.config.shift_factor`。- Flux: 0.1159 如果为 `None`，则传入的输入必须已应用缩放。

output_type (`"mp4"` or `"pil"` or `"pt", default `"pil") : **Endpoint** output type. Subject to change. Report feedback on preferred type.  `"mp4": Supported by video models. Endpoint returns `bytes` of video. `"pil"`: Supported by image and video models. Image models: Endpoint returns `bytes` of an image in `image_format`. Video models: Endpoint returns `torch.Tensor` with partial `postprocessing` applied. Requires `processor` as a flag (any `None` value will work). `"pt"`: Support by image and video models. Endpoint returns `torch.Tensor`. With `partial_postprocess=True` the tensor is postprocessed `uint8` image tensor.  Recommendations: `"pt"` with `partial_postprocess=True` is the smallest transfer for full quality. `"pt"` with `partial_postprocess=False` is the most compatible with third party code. `"pil"` with `image_format="jpg"` is the smallest transfer overall.

return_type (`"mp4"` or `"pil"` or `"pt", default `"pil") : **Function** return type.  `"mp4": Function returns `bytes` of video. `"pil"`: Function returns `PIL.Image.Image`. With `output_type="pil" no further processing is applied. With `output_type="pt" a `PIL.Image.Image` is created. `partial_postprocess=False` `processor` is required. `partial_postprocess=True` `processor` is **not** required. `"pt"`: Function returns `torch.Tensor`. `processor` is **not** required. `partial_postprocess=False` tensor is `float16` or `bfloat16`, without denormalization. `partial_postprocess=True` tensor is `uint8`, denormalized.

image_format (`"png"` 或 `"jpg"`，默认值为 `jpg`)：与 `output_type="pil"` 配合使用。端点返回 `jpg` 或 `png`。

partial_postprocess (`bool`，默认值为 `False`)：与 `output_type="pt"` 配合使用。`partial_postprocess=False` 时，张量为 `float16` 或 `bfloat16`，未进行反归一化。`partial_postprocess=True` 时，张量为 `uint8`，已进行反归一化。

input_tensor_type (`"binary"`，默认值为 `"binary"`)：张量传输类型。

output_tensor_type (`"binary"`，默认值为 `"binary"`)：张量传输类型。

height (`int`，**可选**)：使用 `"packed"` 潜变量时必填。

width (`int`，**可选**)：使用 `"packed"` 潜变量时必填。

**返回值：**

output (`Image.Image` 或 `list[Image.Image]` 或 `bytes` 或 `torch.Tensor`)。

Hugging Face 混合推理，支持远程运行 VAE 解码。

## remote_encode[[diffusers.utils.remote_utils.remote_encode]]

#### diffusers.utils.remote_utils.remote_encode[[diffusers.utils.remote_utils.remote_encode]]

```python
diffusers.utils.remote_utils.remote_encode(endpoint: str, image: 'torch.Tensor' | Image.Image, scaling_factor: float | None = None, shift_factor: float | None = None)
```

[源代码](https://github.com/huggingface/diffusers/blob/v0.43.0/src/diffusers/utils/remote_utils.py#L382)

**参数：**

endpoint (`str`) : 远程解码的端点。

image （`torch.Tensor` 或 `PIL.Image.Image`）：要编码的图像。

scaling_factor (`float`, *可选*) : 传入时应用缩放，例如 `latents * self.vae.config.scaling_factor`。 - SD v1: 0.18215 - SD XL: 0.13025 - Flux: 0.3611 如果为 `None`，则传入的输入必须已应用缩放。

shift_factor (`float`, *可选*) : 传入时应用偏移，例如 `latents - self.vae.config.shift_factor`。- Flux: 0.1159 如果为 `None`，则传入的输入必须已应用缩放。

**返回值：**

output (`torch.Tensor`).

Hugging Face 混合推理，支持远程运行 VAE 编码。

