|
| 1 | +--- |
| 2 | +description: Deploying models to Huggingface Inference Endpoints with Hugging Face :hugging_face:. |
| 3 | +--- |
| 4 | + |
| 5 | +# Hugging Face :hugging_face: |
| 6 | + |
| 7 | +Hugging Face Inference Endpoints provides a secure production solution to easily deploy any `transformers`, `sentence-transformers`, and `diffusers` models on a dedicated and autoscaling infrastructure managed by Hugging Face. An Inference Endpoint is built from a model from the [Hub](https://huggingface.co/models). |
| 8 | + |
| 9 | +This service provides dedicated and autoscaling infrastructure managed by Hugging Face, allowing you to deploy models without dealing with containers and GPUs. |
| 10 | + |
| 11 | +## When to use it? |
| 12 | + |
| 13 | +You should use Hugging Face Model Deployer: |
| 14 | + |
| 15 | +* if you want to deploy [Transformers, Sentence-Transformers, or Diffusion models](https://huggingface.co/docs/inference-endpoints/supported_tasks) on dedicated and secure infrastructure. |
| 16 | +* if you prefer a fully-managed production solution for inference without the need to handle containers and GPUs. |
| 17 | +* if your goal is to turn your models into production-ready APIs with minimal infrastructure or MLOps involvement |
| 18 | +* Cost-effectiveness is crucial, and you want to pay only for the raw compute resources you use. |
| 19 | +* Enterprise security is a priority, and you need to deploy models into secure offline endpoints accessible only via a direct connection to your Virtual Private Cloud (VPCs). |
| 20 | + |
| 21 | +If you are looking for a more easy way to deploy your models locally, you can use the [MLflow Model Deployer](mlflow.md) flavor. |
| 22 | + |
| 23 | +## How to deploy it? |
| 24 | + |
| 25 | +The Hugging Face Model Deployer flavor is provided by the Hugging Face ZenML integration, so you need to install it on your local machine to be able to deploy your models. You can do this by running the following command: |
| 26 | + |
| 27 | +```bash |
| 28 | +zenml integration install huggingface -y |
| 29 | +``` |
| 30 | + |
| 31 | +To register the Hugging Face model deployer with ZenML you need to run the following command: |
| 32 | + |
| 33 | +```bash |
| 34 | +zenml model-deployer register <MODEL_DEPLOYER_NAME> --flavor=huggingface --token=<YOUR_HF_TOKEN> --namespace=<YOUR_HF_NAMESPACE> |
| 35 | +``` |
| 36 | + |
| 37 | +Here, |
| 38 | + |
| 39 | +* `token` parameter is the Hugging Face authentication token. It can be managed through [Hugging Face settings](https://huggingface.co/settings/tokens). |
| 40 | +* `namespace` parameter is used for listing and creating the inference endpoints. It can take any of the following values, username or organization name or `*` depending on where the inference endpoint should be created. |
| 41 | + |
| 42 | +We can now use the model deployer in our stack. |
| 43 | + |
| 44 | +```bash |
| 45 | +zenml stack update <CUSTOM_STACK_NAME> --model-deployer=<MODEL_DEPLOYER_NAME> |
| 46 | +``` |
| 47 | + |
| 48 | +See the [huggingface_model_deployer_step](https://sdkdocs.zenml.io/latest/integration_code_docs/integrations-seldon/#zenml.integrations.huggingface.steps.huggingface_deployer.huggingface_model_deployer_step) for an example of using the Hugging Face Model Deployer to deploy a model inside a ZenML pipeline step. |
| 49 | + |
| 50 | +## Configuration |
| 51 | + |
| 52 | +Within the `HuggingFaceServiceConfig` you can configure: |
| 53 | + |
| 54 | +* `model_name`: the name of the model in ZenML. |
| 55 | +* `endpoint_name`: the name of the inference endpoint. We add a prefix `zenml-` and first 8 characters of the service uuid as a suffix to the endpoint name. |
| 56 | +* `repository`: The repository name in the user’s namespace (`{username}/{model_id}`) or in the organization namespace (`{organization}/{model_id}`) from the Hugging Face hub. |
| 57 | +* `framework`: The machine learning framework used for the model (e.g. `"custom"`, `"pytorch"` ) |
| 58 | +* `accelerator`: The hardware accelerator to be used for inference. (e.g. `"cpu"`, `"gpu"`) |
| 59 | +* `instance_size`: The size of the instance to be used for hosting the model (e.g. `"large"`, `"xxlarge"`) |
| 60 | +* `instance_type`: Inference Endpoints offers a selection of curated CPU and GPU instances. (e.g. `"c6i"`, `"g5.12xlarge"`) |
| 61 | +* `region`: The cloud region in which the Inference Endpoint will be created (e.g. `"us-east-1"`, `"eu-west-1"` for `vendor = aws` and `"eastus"` for Microsoft Azure vendor.). |
| 62 | +* `vendor`: The cloud provider or vendor where the Inference Endpoint will be hosted (e.g. `"aws"`). |
| 63 | +* `token`: The Hugging Face authentication token. It can be managed through [huggingface settings](https://huggingface.co/settings/tokens). The same token can be passed used while registering the Hugging Face model deployer. |
| 64 | +* `account_id`: (Optional) The account ID used to link a VPC to a private Inference Endpoint (if applicable). |
| 65 | +* `min_replica`: (Optional) The minimum number of replicas (instances) to keep running for the Inference Endpoint. Defaults to `0`. |
| 66 | +* `max_replica`: (Optional) The maximum number of replicas (instances) to scale to for the Inference Endpoint. Defaults to `1`. |
| 67 | +* `revision`: (Optional) The specific model revision to deploy on the Inference Endpoint for the Hugging Face repository . |
| 68 | +* `task`: Select a supported [Machine Learning Task](https://huggingface.co/docs/inference-endpoints/supported_tasks). (e.g. `"text-classification"`, `"text-generation"`) |
| 69 | +* `custom_image`: (Optional) A custom Docker image to use for the Inference Endpoint. |
| 70 | +* `namespace`: The namespace where the Inference Endpoint will be created. The same namespace can be passed used while registering the Hugging Face model deployer. |
| 71 | +* `endpoint_type`: (Optional) The type of the Inference Endpoint, which can be `"protected"`, `"public"` (default) or `"private"`. |
| 72 | + |
| 73 | +For more information and a full list of configurable attributes of the Hugging Face Model Deployer, check out |
| 74 | +the [SDK Docs](https://sdkdocs.zenml.io/latest/integration_code_docs/integrations-huggingface/#zenml.integrations.huggingface.model_deployers) and Hugging Face endpoint [code](https://github.com/huggingface/huggingface_hub/blob/5e3b603ccc7cd6523d998e75f82848215abf9415/src/huggingface_hub/hf_api.py#L6957). |
| 75 | + |
| 76 | +### Run inference on a provisioned inference endpoint |
| 77 | + |
| 78 | +The following code example shows how to run inference against a provisioned inference endpoint: |
| 79 | + |
| 80 | +```python |
| 81 | +from typing import Annotated |
| 82 | +from zenml import step, pipeline |
| 83 | +from zenml.integrations.huggingface.model_deployers import HuggingFaceModelDeployer |
| 84 | +from zenml.integrations.huggingface.services import HuggingFaceDeploymentService |
| 85 | + |
| 86 | + |
| 87 | +# Load a prediction service deployed in another pipeline |
| 88 | +@step(enable_cache=False) |
| 89 | +def prediction_service_loader( |
| 90 | + pipeline_name: str, |
| 91 | + pipeline_step_name: str, |
| 92 | + running: bool = True, |
| 93 | + model_name: str = "default", |
| 94 | +) -> HuggingFaceDeploymentService: |
| 95 | + """Get the prediction service started by the deployment pipeline. |
| 96 | +
|
| 97 | + Args: |
| 98 | + pipeline_name: name of the pipeline that deployed the MLflow prediction |
| 99 | + server |
| 100 | + step_name: the name of the step that deployed the MLflow prediction |
| 101 | + server |
| 102 | + running: when this flag is set, the step only returns a running service |
| 103 | + model_name: the name of the model that is deployed |
| 104 | + """ |
| 105 | + # get the Hugging Face model deployer stack component |
| 106 | + model_deployer = HuggingFaceModelDeployer.get_active_model_deployer() |
| 107 | + |
| 108 | + # fetch existing services with same pipeline name, step name and model name |
| 109 | + existing_services = model_deployer.find_model_server( |
| 110 | + pipeline_name=pipeline_name, |
| 111 | + pipeline_step_name=pipeline_step_name, |
| 112 | + model_name=model_name, |
| 113 | + running=running, |
| 114 | + ) |
| 115 | + |
| 116 | + if not existing_services: |
| 117 | + raise RuntimeError( |
| 118 | + f"No Hugging Face inference endpoint deployed by step " |
| 119 | + f"'{pipeline_step_name}' in pipeline '{pipeline_name}' with name " |
| 120 | + f"'{model_name}' is currently running." |
| 121 | + ) |
| 122 | + |
| 123 | + return existing_services[0] |
| 124 | + |
| 125 | + |
| 126 | +# Use the service for inference |
| 127 | +@step |
| 128 | +def predictor( |
| 129 | + service: HuggingFaceDeploymentService, |
| 130 | + data: str |
| 131 | +) -> Annotated[str, "predictions"]: |
| 132 | + """Run a inference request against a prediction service""" |
| 133 | + |
| 134 | + prediction = service.predict(data) |
| 135 | + return prediction |
| 136 | + |
| 137 | + |
| 138 | +@pipeline |
| 139 | +def huggingface_deployment_inference_pipeline( |
| 140 | + pipeline_name: str, pipeline_step_name: str = "huggingface_model_deployer_step", |
| 141 | +): |
| 142 | + inference_data = ... |
| 143 | + model_deployment_service = prediction_service_loader( |
| 144 | + pipeline_name=pipeline_name, |
| 145 | + pipeline_step_name=pipeline_step_name, |
| 146 | + ) |
| 147 | + predictions = predictor(model_deployment_service, inference_data) |
| 148 | +``` |
| 149 | + |
| 150 | +For more information and a full list of configurable attributes of the Hugging Face Model Deployer, check out |
| 151 | +the [SDK Docs](https://sdkdocs.zenml.io/latest/integration_code_docs/integrations-huggingface/#zenml.integrations.huggingface.model_deployers). |
| 152 | + |
| 153 | +<!-- For scarf --> |
| 154 | +<figure><img alt="ZenML Scarf" referrerpolicy="no-referrer-when-downgrade" src="https://static.scarf.sh/a.png?x-pxid=f0b4f458-0a54-4fcd-aa95-d5ee424815bc" /></figure> |
0 commit comments