|
| 1 | +<!--Copyright 2024 The HuggingFace Team. All rights reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 4 | +the License. You may obtain a copy of the License at |
| 5 | +
|
| 6 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +
|
| 8 | +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 9 | +an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 10 | +specific language governing permissions and limitations under the License. |
| 11 | +
|
| 12 | +⚠️ Note that this file is in Markdown but contains specific syntax for our doc-builder (similar to MDX) that may not be |
| 13 | +rendered properly in your Markdown viewer. |
| 14 | +
|
| 15 | +--> |
| 16 | + |
| 17 | +# Using TEI on AMD Instinct GPUs (ROCm) |
| 18 | + |
| 19 | +> [!WARNING] |
| 20 | +> AMD ROCm support is **experimental**. Only AMD Instinct GPUs (MI200, MI300 series) are tested. |
| 21 | +
|
| 22 | +Text Embeddings Inference can run on AMD Instinct GPUs using [ROCm](https://rocm.docs.amd.com/). The implementation uses PyTorch's built-in `scaled_dot_product_attention` as the attention backend. |
| 23 | + |
| 24 | +## Prerequisites |
| 25 | + |
| 26 | +- AMD Instinct GPU (MI200, MI300 series) with ROCm 6.x drivers on the host |
| 27 | +- Either a working ROCm PyTorch installation, **or** the `rocm/pytorch:latest` Docker image (recommended) |
| 28 | + |
| 29 | +--- |
| 30 | + |
| 31 | +The recommended way to get started is to use AMD's official ROCm PyTorch image, which ships with PyTorch and ROCm pre-installed. Alternatively, you can install ROCm PyTorch directly on the host with `pip install torch --index-url https://download.pytorch.org/whl/rocm6.2` and skip Step 1. |
| 32 | + |
| 33 | +## Step 1: Start the container |
| 34 | + |
| 35 | +```shell |
| 36 | +docker run -it --device=/dev/kfd --device=/dev/dri \ |
| 37 | + --group-add video --shm-size 8g \ |
| 38 | + -v $PWD:/workspace \ |
| 39 | + rocm/pytorch:latest bash |
| 40 | +``` |
| 41 | + |
| 42 | +Inside the container, clone the TEI repository (or mount it via `-v`) and run the remaining steps from the repo root. |
| 43 | + |
| 44 | +## Step 2: Install Rust |
| 45 | + |
| 46 | +```shell |
| 47 | +curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y |
| 48 | +source "$HOME/.cargo/env" |
| 49 | +``` |
| 50 | + |
| 51 | +## Step 3: Install Python dependencies |
| 52 | + |
| 53 | +PyTorch is already provided by the container image, so install the remaining dependencies without pulling a new torch: |
| 54 | + |
| 55 | +```shell |
| 56 | +pip install --no-deps -r backends/python/server/requirements-amd.txt |
| 57 | +pip install safetensors opentelemetry-api opentelemetry-sdk \ |
| 58 | + opentelemetry-exporter-otlp-proto-grpc grpcio-reflection \ |
| 59 | + grpc-interceptor einops packaging |
| 60 | +``` |
| 61 | + |
| 62 | +## Step 4: Generate protobuf stubs |
| 63 | + |
| 64 | +```shell |
| 65 | +pip install grpcio-tools==1.62.2 mypy-protobuf==3.6.0 types-protobuf |
| 66 | + |
| 67 | +mkdir -p backends/python/server/text_embeddings_server/pb |
| 68 | + |
| 69 | +python -m grpc_tools.protoc \ |
| 70 | + -I backends/proto \ |
| 71 | + --python_out=backends/python/server/text_embeddings_server/pb \ |
| 72 | + --grpc_python_out=backends/python/server/text_embeddings_server/pb \ |
| 73 | + --mypy_out=backends/python/server/text_embeddings_server/pb \ |
| 74 | + backends/proto/embed.proto |
| 75 | + |
| 76 | +# Fix relative imports in generated files |
| 77 | +find backends/python/server/text_embeddings_server/pb/ -name "*.py" \ |
| 78 | + -exec sed -i 's/^\(import.*pb2\)/from . \1/g' {} \; |
| 79 | + |
| 80 | +touch backends/python/server/text_embeddings_server/pb/__init__.py |
| 81 | +``` |
| 82 | + |
| 83 | +## Step 5: Install the Python server package |
| 84 | + |
| 85 | +```shell |
| 86 | +pip install -e backends/python/server |
| 87 | +``` |
| 88 | + |
| 89 | +## Step 6: Build the Rust router |
| 90 | + |
| 91 | +```shell |
| 92 | +cargo build --release \ |
| 93 | + --no-default-features \ |
| 94 | + --features python,http \ |
| 95 | + --bin text-embeddings-router |
| 96 | +``` |
| 97 | + |
| 98 | +## Step 7: Launch TEI |
| 99 | + |
| 100 | +```shell |
| 101 | +model=BAAI/bge-base-en-v1.5 |
| 102 | + |
| 103 | +./target/release/text-embeddings-router --model-id $model --dtype bfloat16 --port 8080 |
| 104 | +``` |
| 105 | + |
| 106 | +Once the server is ready, you can test it with a simple embed request: |
| 107 | + |
| 108 | +```shell |
| 109 | +curl http://localhost:8080/embed \ |
| 110 | + -X POST \ |
| 111 | + -H 'Content-Type: application/json' \ |
| 112 | + -d '{"inputs": "What is Deep Learning?"}' |
| 113 | +``` |
| 114 | + |
| 115 | +## Verifying GPU detection |
| 116 | + |
| 117 | +After launch you should see a log line confirming ROCm was detected: |
| 118 | + |
| 119 | +``` |
| 120 | +INFO text_embeddings_server::utils::device: ROCm / HIP version: X.Y.Z |
| 121 | +``` |
| 122 | + |
| 123 | +You can also verify from Python: |
| 124 | + |
| 125 | +```python |
| 126 | +import torch |
| 127 | +print(torch.cuda.is_available()) # True |
| 128 | +print(torch.version.hip) # e.g. 6.2.12345-... |
| 129 | +``` |
| 130 | + |
| 131 | +## Notes |
| 132 | + |
| 133 | +This is a work in progress — more model support and optimized operations for AMD GPUs are coming soon. |
0 commit comments