Buckets:

hf-doc-build/doc / diffusers /main /vi /using-diffusers /custom_pipeline_overview.md
mishig's picture
|
download
raw
10.4 kB
# Các pipeline và thành phần của cộng đồng
Các pipeline cộng đồng là các lớp [DiffusionPipeline](/docs/diffusers/main/vi/api/pipelines/overview#diffusers.DiffusionPipeline) khác với cài đặt trong bài báo gốc. Chúng cung cấp các tính năng bổ sung hoặc mở rộng cài đặt pipeline gốc.
> [!TIP]
> Xem các pipeline cộng đồng trong [diffusers/examples/community](https://github.com/huggingface/diffusers/tree/main/examples/community) với các ví dụ suy luận và huấn luyện về cách sử dụng chúng.
Các pipeline của cộng đồng được lưu trữ trên Hub hoặc trong kho GitHub của Diffusers. Pipeline trên Hub có thể tùy chỉnh hoàn toàn (scheduler, models, mã pipeline, v.v.) trong khi pipeline trên GitHub chỉ giới hạn ở mã pipeline tùy chỉnh. So sánh hai loại pipeline trong bảng dưới đây.
| | GitHub | Hub |
|---|---|---|
| Cách sử dụng | `custom_pipeline="<filename_without_.py>"` | ID kho Hub hoặc `trust_remote_code=True` khi `_class_name` trỏ đến mã tùy chỉnh |
| Quy trình xem xét | Mở một Pull Request trên GitHub và trải qua quy trình xem xét từ đội ngũ Diffusers trước khi được hợp nhất. Tùy chọn này chậm hơn. | Tải trực tiếp lên kho Hub mà không cần xem xét. Đây là tùy chọn nhanh nhất. |
| Hiển thị | Được bao gồm trong kho Diffusers chính thức và tài liệu. | Được hiển thị trên hồ sơ Hub của bạn và phụ thuộc vào mức độ sử dụng cũng như các hoạt động quảng bá của bạn để tăng khả năng tiếp cận. |
## Tải pipeline của cộng đồng
Tải một trong hai loại pipeline cộng đồng bằng cách truyền `custom_pipeline` vào [from_pretrained()](/docs/diffusers/main/vi/api/pipelines/overview#diffusers.DiffusionPipeline.from_pretrained).
```py
import torch
from diffusers import DiffusionPipeline
pipeline = DiffusionPipeline.from_pretrained(
"stabilityai/stable-diffusion-3-medium-diffusers",
custom_pipeline="pipeline_stable_diffusion_3_instruct_pix2pix",
dtype=torch.float16,
device_map="cuda" # or "mps", "xpu", "cpu"
)
```
Thêm đối số `custom_revision` vào [from_pretrained()](/docs/diffusers/main/vi/api/pipelines/overview#diffusers.DiffusionPipeline.from_pretrained) để tải pipeline của cộng đồng từ một phiên bản cụ thể (ví dụ, `v0.30.0` hoặc `main`). Mặc định, các pipeline của cộng đồng được tải từ phiên bản ổn định mới nhất của Diffusers.
```py
import torch
from diffusers import DiffusionPipeline
pipeline = DiffusionPipeline.from_pretrained(
"stabilityai/stable-diffusion-3-medium-diffusers",
custom_pipeline="pipeline_stable_diffusion_3_instruct_pix2pix",
custom_revision="main",
dtype=torch.float16,
device_map="cuda" # or "mps", "xpu", "cpu"
)
```
> [!WARNING]
> Trong khi Hugging Face Hub [quét](https://huggingface.co/docs/hub/security-malware) các tệp, bạn vẫn nên kiểm tra mã pipeline của Hub và đảm bảo rằng nó an toàn.
Có một vài cách để tải một pipeline của cộng đồng.
- Truyền một đường dẫn đến `custom_pipeline` để tải một pipeline cộng đồng cục bộ. Thư mục phải chứa tệp `pipeline.py` có chứa lớp pipeline.
```py
import torch
from diffusers import DiffusionPipeline
pipeline = DiffusionPipeline.from_pretrained(
"stabilityai/stable-diffusion-3-medium-diffusers",
custom_pipeline="path/to/pipeline_directory",
trust_remote_code=True,
dtype=torch.float16,
device_map="cuda" # or "mps", "xpu", "cpu"
)
```
- Tham số `custom_pipeline` cũng được hỗ trợ bởi [from_pipe()](/docs/diffusers/main/vi/api/pipelines/overview#diffusers.DiffusionPipeline.from_pipe), rất hữu ích để [tái sử dụng các pipeline](./loading#reusing-models-in-multiple-pipelines) mà không tốn thêm bộ nhớ. Nó giới hạn mức sử dụng bộ nhớ chỉ bằng pipeline lớn nhất được nạp.
```py
import torch
from diffusers import DiffusionPipeline
pipeline_sd = DiffusionPipeline.from_pretrained(
"emilianJR/CyberRealistic_V3", dtype=torch.float16, device_map="cuda" # or "mps", "xpu", "cpu"
)
pipeline_lpw = DiffusionPipeline.from_pipe(
pipeline_sd, custom_pipeline="lpw_stable_diffusion"
)
```
Phương thức [from_pipe()](/docs/diffusers/main/vi/api/pipelines/overview#diffusers.DiffusionPipeline.from_pipe) đặc biệt hữu ích để nạp các pipeline của cộng đồng vì nhiều pipeline trong số đó không có trọng số được huấn luyện trước. Các pipeline của cộng đồng thường thêm một tính năng trên nền của một pipeline hiện có.
## Các thành phần cộng đồng
Các thành phần cộng đồng cho phép bạn xây dựng các pipeline với các transformer, UNet, VAE và scheduler tùy chỉnh không được hỗ trợ bởi Diffusers. Những thành phần này cần có các module Python riêng của chúng.
Phần này cho thấy người dùng có thể sử dụng các thành phần cộng đồng để xây dựng một pipeline cộng đồng, với [showlab/show-1-base](https://huggingface.co/showlab/show-1-base) làm ví dụ.
1. Tải các thành phần cần thiết, bộ lập lịch và bộ xử lý ảnh. Bộ mã hóa văn bản thường được nhập từ [Transformers](https://huggingface.co/docs/transformers/index).
```python
from transformers import T5Tokenizer, T5EncoderModel, CLIPImageProcessor
from diffusers import DPMSolverMultistepScheduler
pipeline_id = "showlab/show-1-base"
tokenizer = T5Tokenizer.from_pretrained(pipeline_id, subfolder="tokenizer")
text_encoder = T5EncoderModel.from_pretrained(pipeline_id, subfolder="text_encoder")
scheduler = DPMSolverMultistepScheduler.from_pretrained(pipeline_id, subfolder="scheduler")
feature_extractor = CLIPImageProcessor.from_pretrained(pipeline_id, subfolder="feature_extractor")
```
> [!WARNING]
> Trong bước 2 và 3, việc triển khai [UNet](https://github.com/showlab/Show-1/blob/main/showone/models/unet_3d_condition.py) và [pipeline](https://huggingface.co/sayakpaul/show-1-base-with-code/blob/main/unet/showone_unet_3d_condition.py) tùy chỉnh phải khớp với định dạng được hiển thị trong các tệp của chúng để ví dụ này hoạt động.
2. Tải một [UNet tùy chỉnh](https://github.com/showlab/Show-1/blob/main/showone/models/unet_3d_condition.py) đã được triển khai trong [showone_unet_3d_condition.py](https://huggingface.co/sayakpaul/show-1-base-with-code/blob/main/unet/showone_unet_3d_condition.py). Tên lớp [UNet3DConditionModel](/docs/diffusers/main/vi/api/models/unet3d-cond#diffusers.UNet3DConditionModel) được đổi thành tên của bản triển khai tùy chỉnh, `ShowOneUNet3DConditionModel`, vì [UNet3DConditionModel](/docs/diffusers/main/vi/api/models/unet3d-cond#diffusers.UNet3DConditionModel) đã tồn tại trong Diffusers. Mọi thành phần cần thiết cho lớp `ShowOneUNet3DConditionModel` nên được đặt trong `showone_unet_3d_condition.py`.
```python
from showone_unet_3d_condition import ShowOneUNet3DConditionModel
unet = ShowOneUNet3DConditionModel.from_pretrained(pipeline_id, subfolder="unet")
```
3. Tải mã pipeline tùy chỉnh (đã được triển khai trong [pipeline_t2v_base_pixel.py](https://huggingface.co/sayakpaul/show-1-base-with-code/blob/main/pipeline_t2v_base_pixel.py)). Script này chứa lớp `TextToVideoIFPipeline` tùy chỉnh để tạo video từ văn bản. Giống như UNet tùy chỉnh, mọi mã cần thiết cho `TextToVideoIFPipeline` nên được đặt trong `pipeline_t2v_base_pixel.py`.
Khởi tạo `TextToVideoIFPipeline` với `ShowOneUNet3DConditionModel`.
```python
import torch
from pipeline_t2v_base_pixel import TextToVideoIFPipeline
pipeline = TextToVideoIFPipeline(
unet=unet,
text_encoder=text_encoder,
tokenizer=tokenizer,
scheduler=scheduler,
feature_extractor=feature_extractor,
)
pipeline = pipeline.to("cuda") # or "mps", "xpu", "cpu"
```
4. Đẩy pipeline lên Hub để chia sẻ với cộng đồng.
```python
pipeline.push_to_hub("custom-t2v-pipeline")
```
Sau khi pipeline được đẩy thành công, hãy thực hiện các thay đổi sau.
- Thay đổi thuộc tính `_class_name` trong [model_index.json](https://huggingface.co/sayakpaul/show-1-base-with-code/blob/main/model_index.json#L2) thành `["pipeline_t2v_base_pixel", "TextToVideoIFPipeline"]`.
- Tải `showone_unet_3d_condition.py` lên thư mục con [unet](https://huggingface.co/sayakpaul/show-1-base-with-code/blob/main/unet/showone_unet_3d_condition.py).
- Tải `pipeline_t2v_base_pixel.py` lên [kho](https://huggingface.co/sayakpaul/show-1-base-with-code/tree/main) pipeline.
Để chạy suy luận, hãy thêm đối số `trust_remote_code` khi khởi tạo pipeline để xử lý mọi "phép màu" diễn ra phía sau.
```python
import torch
from diffusers import DiffusionPipeline
pipeline = DiffusionPipeline.from_pretrained(
"<change-username>/<change-id>", trust_remote_code=True, dtype=torch.float16
)
```
> [!WARNING]
> Như một biện pháp phòng ngừa bổ sung khi sử dụng `trust_remote_code=True`, hãy truyền một commit hash vào tham số `revision` trong [from_pretrained()](/docs/diffusers/main/vi/api/pipelines/overview#diffusers.DiffusionPipeline.from_pretrained) để đảm bảo rằng mã nguồn không bị cập nhật với mã độc mới (trừ khi bạn hoàn toàn tin tưởng các chủ sở hữu mô hình).
## Các bước tiếp theo
- Xem Issue [#841](https://github.com/huggingface/diffusers/issues/841) để có thêm ngữ cảnh về lý do chúng tôi thêm các pipeline cộng đồng nhằm giúp mọi người dễ dàng chia sẻ công việc của mình mà không bị chậm trễ.
- Xem thêm kho [stabilityai/japanese-stable-diffusion-xl](https://huggingface.co/stabilityai/japanese-stable-diffusion-xl/) để có thêm một ví dụ về pipeline cộng đồng cũng sử dụng tính năng `trust_remote_code`.

Xet Storage Details

Size:
10.4 kB
·
Xet hash:
f80d21f308ea32761e8b353d8dde5541557cfa66927499594bc42ee5ddd7a39d

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.