Buckets:
| # AMD GPU | |
| [AMD Instinct](https://www.amd.com/en/products/accelerators/instinct.html) (các dòng MI200, MI300, MI350) và GPU [Radeon](https://www.amd.com/en/products/graphics/desktops/radeon.html) chạy PyTorch thông qua [ROCm](https://rocm.docs.amd.com/), phần mềm GPU của AMD. Bản build PyTorch của ROCm sử dụng [HIP](https://rocm.docs.amd.com/projects/HIP/en/latest/what_is_hip.html), giao diện lập trình tương tự CUDA của AMD, để hiển thị các GPU AMD thông qua API `torch.cuda`. Chuỗi thiết bị là `cuda`, do đó mã được viết cho GPU NVIDIA sẽ chạy mà không cần thay đổi. Transformers kiểm tra `torch.version.hip` lúc runtime để phân biệt hai loại và định tuyến attention cùng các kernel Hub sang các bản build dành riêng cho AMD. | |
| ## Cài đặt | |
| Cài đặt ROCm từ ảnh Docker [huggingface/transformers-pytorch-amd-gpu](https://hub.docker.com/r/huggingface/transformers-pytorch-amd-gpu). Ảnh này đi kèm bản build PyTorch của ROCm, bản build `flash-attn` cho GPU MI300 và MI350 (`gfx942` và `gfx950`), cùng Transformers được cài đặt từ mã nguồn. | |
| ```bash | |
| docker run -it --device=/dev/kfd --device=/dev/dri --group-add video \ | |
| --ipc=host --shm-size 16G \ | |
| huggingface/transformers-pytorch-amd-gpu | |
| ``` | |
| `--device=/dev/kfd` và `--device=/dev/dri` cho phép container truy cập vào các GPU AMD. Cài đặt [kernels](https://github.com/huggingface/kernels) bên trong container để [use Hub kernels](#use-hub-kernels). | |
| ```bash | |
| pip install "transformers[kernels]" | |
| ``` | |
| Để cài đặt trên hệ thống của bạn, hãy chọn ROCm trong [trình chọn cài đặt PyTorch](https://pytorch.org/get-started/locally/) và đảm bảo rằng phiên bản wheel của ROCm được driver của bạn hỗ trợ. | |
| Kiểm tra xem PyTorch có nhận diện được các GPU AMD hay không. | |
| ```py | |
| import torch | |
| print(torch.version.hip, torch.cuda.is_available(), torch.cuda.device_count()) | |
| print(torch.cuda.get_device_properties(0).gcnArchName) | |
| ``` | |
| ```txt | |
| 7.2.53211 True 2 | |
| gfx942:sramecc+:xnack- | |
| ``` | |
| `torch.version.hip` là `None` trên bản build CUDA của PyTorch. `gcnArchName` báo cáo kiến trúc GPU, ví dụ `gfx942` cho MI300, `gfx950` cho MI350, và `gfx1151` cho Ryzen AI Max (Strix Halo). | |
| ## Chọn và sắp xếp GPU | |
| Sử dụng `HIP_VISIBLE_DEVICES` để chọn GPU và đặt thứ tự của chúng. Đặt nó trên cùng một dòng lệnh với lệnh chạy. | |
| ```cli | |
| HIP_VISIBLE_DEVICES=0,2 torchrun trainer-program.py ... | |
| ``` | |
| PyTorch chỉ nhìn thấy GPU 0 và 2, được ánh xạ đến `cuda:0` và `cuda:1`. `ROCR_VISIBLE_DEVICES` lọc GPU ở mức thấp hơn của stack và thường được đặt bởi các trình lập lịch cụm. Nếu cả hai đều được đặt, `HIP_VISIBLE_DEVICES` sẽ chỉ mục vào các GPU còn hiển thị do `ROCR_VISIBLE_DEVICES`. | |
| Sử dụng `rocm-smi` hoặc `amd-smi` để liệt kê các GPU và chỉ số của chúng. | |
| ## Chạy suy luận | |
| [Pipeline](/docs/transformers/main/vi/main_classes/pipelines#transformers.Pipeline) và `device_map="auto"` sẽ phát hiện GPU AMD và đặt mô hình lên `cuda` một cách tự động. Bạn cũng có thể truyền `device_map="cuda"` hoặc `device_map="cuda:1"` một cách tường minh. | |
| ```py | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen3-0.6B") | |
| model = AutoModelForCausalLM.from_pretrained( | |
| "Qwen/Qwen3-0.6B", dtype="bfloat16", device_map="auto" | |
| ) | |
| inputs = tokenizer("The French Bread Law states", return_tensors="pt").to(model.device) | |
| output = model.generate(**inputs, do_sample=False, max_new_tokens=50) | |
| print(tokenizer.decode(output[0], skip_special_tokens=True)) | |
| ``` | |
| ```txt | |
| The French Bread Law states that the French government must provide a certain amount of bread for the citizens of the country. The law is passed by the French parliament. The French parliament is a body of the French government. The French government is a body of the French state. The | |
| ``` | |
| ## Chọn một backend chú ý | |
| Đặt backend chú ý bằng `attn_implementation` khi bạn tải một mô hình, và nó áp dụng cho cả suy luận và huấn luyện. SDPA là mặc định và hoạt động trên mọi GPU AMD được hỗ trợ bởi PyTorch. Hai tùy chọn FlashAttention cũng có sẵn. | |
| | `attn_implementation` | Yêu cầu | Ghi chú | | |
| |---|---|---| | |
| | `"sdpa"` | – | Mặc định. | | |
| | `"flash_attention_2"` | [flash-attn](https://github.com/Dao-AILab/flash-attention#amd-rocm-support) được xây dựng cho ROCm, hoặc [kernels](https://github.com/huggingface/kernels) | Sử dụng gói `flash-attn` khi nó được cài đặt. Backend Composable Kernel (CK) mặc định của nó hỗ trợ các GPU MI200, MI300, MI355 và RDNA 3/4, và [ảnh Docker](#installation) bao gồm nó trên MI300 hoặc MI350. Nếu không, sẽ tải [amd/aiter-flash-attn](https://huggingface.co/kernels/amd/aiter-flash-attn) từ Hub. | | |
| | `"flash_attention_3"` | [kernels](https://github.com/huggingface/kernels) | Tải [amd/aiter-flash-attn](https://huggingface.co/kernels/amd/aiter-flash-attn), một kernel FlashAttention đã được xây dựng sẵn từ thư viện [AITER](https://github.com/ROCm/aiter) của AMD. Không cần biên dịch. | | |
| ```py | |
| from transformers import AutoModelForCausalLM | |
| model = AutoModelForCausalLM.from_pretrained( | |
| "Qwen/Qwen3-0.6B", | |
| dtype="bfloat16", | |
| device_map="auto", | |
| attn_implementation="flash_attention_3", | |
| ) | |
| ``` | |
| Trên ROCm, `flash_attention_3` được phân giải thành kernel AITER và ghi log nội dung sau. Transformers hiện đang sử dụng alias `kernels-community/aiter-flash-attn`, được chuyển hướng đến `amd/aiter-flash-attn`. Thông điệp này đề cập đến gói FlashAttention-3, do đó nó vẫn xuất hiện ngay cả khi `flash_attention_2` đã được cài đặt. | |
| ```txt | |
| You do not have `flash_attn` installed, using `kernels-community/aiter-flash-attn` from the `kernels` library instead! | |
| ``` | |
| Bạn cũng có thể truyền trực tiếp kho kernel bằng `attn_implementation="kernels-community/aiter-flash-attn"`. Xem [Các backend Attention](./attention_interface) để biết cách các backend được chọn. | |
| ## Sử dụng kernel của Hub | |
| Đặt `use_kernels=True` trong [from_pretrained()](/docs/transformers/main/vi/main_classes/model#transformers.PreTrainedModel.from_pretrained) để thay thế các lớp được hỗ trợ bằng các kernel tối ưu từ Hub. Transformers phát hiện ROCm và chọn bản build AMD cho từng lớp, chẳng hạn như [amd/aiter-rope](https://huggingface.co/kernels/amd/aiter-rope) cho rotary embeddings và [kernels-community/liger-kernels](https://huggingface.co/kernels/kernels-community/liger-kernels) cho RMSNorm. Khi bạn viết một [KernelConfig](/docs/transformers/main/vi/main_classes/kernels#transformers.KernelConfig) tùy chỉnh, hãy sử dụng `"rocm"` làm khóa thiết bị cho các ánh xạ cụ thể của AMD. | |
| ```py | |
| from transformers import AutoModelForCausalLM | |
| model = AutoModelForCausalLM.from_pretrained( | |
| "Qwen/Qwen3-0.6B", dtype="bfloat16", device_map="auto", use_kernels=True | |
| ) | |
| ``` | |
| Các kernel trên Hub chủ yếu tăng tốc prefill và các batch lớn. Hãy benchmark workload của bạn trước khi bật chúng cho việc giải mã batch nhỏ. Xem [Tải kernel](./kernel_doc/loading_kernels) để biết bản ánh xạ đầy đủ. | |
| ## Lượng tử hóa mô hình | |
| Các phương pháp được hỗ trợ trên ROCm bao gồm [AWQ](./quantization/awq), [GPT-QModel](./quantization/gptq), [compressed-tensors](./quantization/compressed_tensors) và [Quark](./quantization/quark) của AMD. [bitsandbytes](./quantization/bitsandbytes) được hỗ trợ một phần. GPU MI300 và các thế hệ mới hơn hỗ trợ FP8 một cách bản địa. Xem [bảng tương thích](./quantization/overview) để biết danh sách đầy đủ. | |
| ## Huấn luyện với Trainer | |
| [TrainingArguments](/docs/transformers/main/vi/main_classes/trainer#transformers.TrainingArguments) phát hiện GPU AMD và tự động đặt thiết bị thành `cuda`. | |
| ```py | |
| from transformers import TrainingArguments | |
| training_args = TrainingArguments( | |
| output_dir="./outputs", | |
| bf16=True, | |
| ) | |
| ``` | |
| ### Huấn luyện phân tán | |
| Bản build ROCm của PyTorch đi kèm RCCL, bản triển khai API NCCL của AMD, do đó backend `nccl`, `torchrun`, [FSDP](./fsdp), [DeepSpeed](./main_classes/deepspeed) và API phân tán của Transformers hoạt động mà không cần thay đổi. | |
| ```py | |
| import torch | |
| from transformers import AutoModelForCausalLM, DistributedConfig | |
| model = AutoModelForCausalLM.from_pretrained( | |
| "Qwen/Qwen3-1.7B", | |
| dtype=torch.bfloat16, | |
| distributed_config=DistributedConfig(tp_size=2, fsdp_size=4), | |
| ) | |
| ``` | |
| ```bash | |
| torchrun --nproc_per_node=8 train.py | |
| ``` | |
| Xem [Tensor parallelism](./tensor_parallelism), [Expert parallelism](./expert_parallelism) và [FSDP](./fsdp) để biết các tùy chọn khả dụng. | |
| ## Hạn chế | |
| - `CodeCarbonCallback` không tương thích với ROCm. Đặt `report_to` trong [TrainingArguments](/docs/transformers/main/vi/main_classes/trainer#transformers.TrainingArguments) để loại trừ `codecarbon`. | |
| - Các phương pháp lượng tử hóa được đánh dấu 🔴 cho ROCm trong [bảng tương thích](./quantization/overview) dựa trên các kernel chỉ dành cho CUDA và sẽ không thể tải lên GPU AMD. | |
| ## Các bước tiếp theo | |
| - Đọc [tài liệu ROCm](https://rocm.docs.amd.com/) để cài đặt driver và PyTorch. | |
| - Xem các bản build ROCm trong các tổ chức [AMD](https://huggingface.co/amd) và [kernels-community](https://huggingface.co/kernels-community). | |
| - Đọc [Tải kernel](./kernel_doc/loading_kernels) để tùy chỉnh kernel nào thay thế lớp nào. | |
Xet Storage Details
- Size:
- 10.1 kB
- Xet hash:
- 864481e0927978a67e95b3a6b99128a9d37d0e8e78f57746c7fb1c221676d96e
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.