# 共享管道和模型

使用 [PushToHubMixin](/docs/diffusers/v0.43.0/zh/api/schedulers/overview#diffusers.utils.PushToHubMixin) 在 Hub 上分享您的 pipelines、models 和 schedulers。此 mixin：

1. 在 Hub 上创建一个仓库
2. 保存您的模型、调度器或流水线文件，以便稍后重新加载
3. 将包含这些文件的文件夹上传到 Hub

使用您的访问 [token](https://huggingface.co/settings/tokens) 登录您的 Hugging Face 账户。

```py
from huggingface_hub import notebook_login

notebook_login()
```

```bash
hf auth login
```

使用简短的 id（`"my-controlnet-model"`）推送到您的用户命名空间，或使用 `"your-org/my-controlnet-model"` 推送到组织。

## 模型

要将模型推送到 Hub，请调用 [push_to_hub()](/docs/diffusers/v0.43.0/zh/api/schedulers/overview#diffusers.utils.PushToHubMixin.push_to_hub) 并指定模型的仓库 id。

```py
from diffusers import ControlNetModel

controlnet = ControlNetModel(
    block_out_channels=(32, 64),
    layers_per_block=2,
    in_channels=4,
    down_block_types=("DownBlock2D", "CrossAttnDownBlock2D"),
    cross_attention_dim=32,
    conditioning_embedding_out_channels=(16, 32),
)
controlnet.push_to_hub("my-controlnet-model")
```

[push_to_hub()](/docs/diffusers/v0.43.0/zh/api/schedulers/overview#diffusers.utils.PushToHubMixin.push_to_hub) 方法会保存模型的 `config.json` 文件，权重会自动保存为 [safetensors 文件](./other-formats#safetensors)。

再次使用 [ControlNetModel.from_pretrained()](/docs/diffusers/v0.43.0/zh/api/models/overview#diffusers.ModelMixin.from_pretrained) 加载模型。

```py
model = ControlNetModel.from_pretrained("your-namespace/my-controlnet-model")
```

## 调度器

要将调度器推送到 Hub，请调用 [push_to_hub()](/docs/diffusers/v0.43.0/zh/api/schedulers/overview#diffusers.utils.PushToHubMixin.push_to_hub) 并指定调度器的仓库 id。

```py
from diffusers import DDIMScheduler

scheduler = DDIMScheduler(
    beta_start=0.00085,
    beta_end=0.012,
    beta_schedule="scaled_linear",
    clip_sample=False,
    set_alpha_to_one=False,
)
scheduler.push_to_hub("my-ddim-scheduler")
```

[push_to_hub()](/docs/diffusers/v0.43.0/zh/api/schedulers/overview#diffusers.utils.PushToHubMixin.push_to_hub) 方法会将调度器的 `scheduler_config.json` 文件保存到指定的仓库中。

再次使用 [from_pretrained()](/docs/diffusers/v0.43.0/zh/api/schedulers/overview#diffusers.SchedulerMixin.from_pretrained) 加载调度器。

```py
scheduler = DDIMScheduler.from_pretrained("your-namespace/my-ddim-scheduler")
```

## Pipeline

To push a pipeline to the Hub, load it with [from_pretrained()](/docs/diffusers/v0.43.0/zh/api/pipelines/overview#diffusers.DiffusionPipeline.from_pretrained), then call [push_to_hub()](/docs/diffusers/v0.43.0/zh/api/schedulers/overview#diffusers.utils.PushToHubMixin.push_to_hub) with a repository id.

```py
import torch
from diffusers import DiffusionPipeline

pipeline = DiffusionPipeline.from_pretrained(
    "Qwen/Qwen-Image", dtype=torch.bfloat16, device_map="cuda"  # or "mps", "xpu", "cpu"
)
pipeline.push_to_hub("your-namespace/my-qwen-image")
```

The [push_to_hub()](/docs/diffusers/v0.43.0/zh/api/schedulers/overview#diffusers.utils.PushToHubMixin.push_to_hub) method saves each component to a subfolder in the repository. Load the pipeline again with [DiffusionPipeline.from_pretrained()](/docs/diffusers/v0.43.0/zh/api/pipelines/overview#diffusers.DiffusionPipeline.from_pretrained).

```py
pipeline = DiffusionPipeline.from_pretrained("your-namespace/my-qwen-image")
```

## 隐私

在 [push_to_hub()](/docs/diffusers/v0.43.0/zh/api/schedulers/overview#diffusers.utils.PushToHubMixin.push_to_hub) 中设置 `private=True`，可将模型、调度器或流水线文件设为私有。

```py
controlnet.push_to_hub("my-controlnet-model-private", private=True)
```

传入 `create_pr=True` 可在现有的 Hub 仓库上创建一个 pull request，而不是直接推送到默认分支。

模型和流水线在推送时同样接受 `variant=` 参数，以便指定诸如 `fp16` 这样的命名权重文件。调度器不使用 `variant`。

私有仓库仅对您可见。其他用户无法克隆该仓库，且该仓库不会出现在搜索结果中。即使用户拥有您私有仓库的 URL，他们也会收到 `404 - Sorry, we can't find the page you are looking for`。您必须 [登录](https://huggingface.co/docs/huggingface_hub/quick-start#login) 才能从私有仓库加载模型。

