Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Inférence MaxDiffusion sur les TPU v6e
Ce tutoriel explique comment diffuser des modèles MaxDiffusion sur TPU v6e. Dans ce tutoriel, vous allez générer des images à l'aide du modèle Stable Diffusion XL.
Avant de commencer
Préparez-vous à provisionner un TPU v6e avec quatre puces:
Suivez le guide Configurer l'environnement Cloud TPU pour configurer un projet Google Cloud , configurer Google Cloud CLI, activer l'API Cloud TPU et vous assurer que vous avez accès à l'utilisation des Cloud TPU.
Authentifiez-vous avec Google Cloud et configurez le projet et la zone par défaut pour Google Cloud CLI.
Lorsque vous êtes prêt à sécuriser la capacité de TPU, consultez la section Quotas Cloud TPU pour en savoir plus sur les quotas Cloud TPU. Si vous avez d'autres questions sur la sécurisation de la capacité, contactez votre équipe commerciale ou chargée de votre compte Cloud TPU.
Vérifiez que votre projet dispose d'un quota TPUS_PER_TPU_FAMILY suffisant, qui spécifie le nombre maximal de puces auxquelles vous pouvez accéder dans votre projetGoogle Cloud .
Vérifiez que votre projet dispose d'un quota TPU suffisant pour :
Pour obtenir la liste complète des états des requêtes de ressources en file d'attente, consultez la documentation sur les ressources en file d'attente.
Se connecter au TPU à l'aide de SSH
gcloudcomputetpustpu-vmsshTPU_NAME
Créer un environnement Conda
Créez un répertoire pour Miniconda:
mkdir-p~/miniconda3
Téléchargez le script d'installation de Miniconda:
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/09/04 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 2025/09/04 (UTC)."],[],[],null,["# MaxDiffusion inference on v6e TPUs\n==================================\n\nThis tutorial shows how to serve MaxDiffusion models on TPU v6e. In this\ntutorial, you generate images using the Stable Diffusion XL model.\n| **Note:** After you complete the inference benchmark, be sure to [clean up](#clean-up) the TPU resources.\n\nBefore you begin\n----------------\n\nPrepare to provision a TPU v6e with 4 chips:\n\n1. Follow [Set up the Cloud TPU environment](/tpu/docs/setup-gcp-account)\n guide to set up a Google Cloud project, configure the Google Cloud CLI,\n enable the Cloud TPU API, and ensure you have access to use\n Cloud TPUs.\n\n2. Authenticate with Google Cloud and configure the default project and\n zone for Google Cloud CLI.\n\n ```bash\n gcloud auth login\n gcloud config set project PROJECT_ID\n gcloud config set compute/zone ZONE\n ```\n\n### Secure capacity\n\nWhen you are ready to secure TPU capacity, see [Cloud TPU\nQuotas](/tpu/docs/quota) for more information about the Cloud TPU quotas. If\nyou have additional questions about securing capacity, contact your Cloud TPU\nsales or account team.\n\n### Provision the Cloud TPU environment\n\nYou can provision TPU VMs with\n[GKE](/tpu/docs/tpus-in-gke), with GKE and\n[XPK](https://github.com/google/xpk/tree/main),\nor as [queued resources](/tpu/docs/queued-resources).\n| **Note:** This document describes how to provision TPUs using queued resources. If you are provisioning your TPUs using [XPK](https://github.com/AI-Hypercomputer/xpk/blob/main/README.md) (a wrapper CLI tool over GKE), set up XPK permissions on your user account for GKE.\n\n### Prerequisites\n\n| **Note:** This tutorial has been tested with Python 3.10 or later.\n\n- Verify that your project has enough `TPUS_PER_TPU_FAMILY` quota, which specifies the maximum number of chips you can access within your Google Cloud project.\n- Verify that your project has enough TPU quota for:\n - TPU VM quota\n - IP address quota\n - Hyperdisk Balanced quota\n- User project permissions\n - If you are using GKE with XPK, see [Cloud Console Permissions on\n the user or service account](https://github.com/AI-Hypercomputer/xpk/blob/main/README.md#cloud-console-permissions-on-the-user-or-service-account-needed-to-run-xpk) for the permissions needed to run XPK.\n\nProvision a TPU v6e\n-------------------\n\n```bash\n gcloud alpha compute tpus queued-resources create QUEUED_RESOURCE_ID \\\n --node-id TPU_NAME \\\n --project PROJECT_ID \\\n --zone ZONE \\\n --accelerator-type v6e-4 \\\n --runtime-version v2-alpha-tpuv6e \\\n --service-account SERVICE_ACCOUNT\n```\n\nUse the `list` or `describe` commands\nto query the status of your queued resource. \n\n```bash\n gcloud alpha compute tpus queued-resources describe QUEUED_RESOURCE_ID \\\n --project=PROJECT_ID --zone=ZONE\n```\n\nFor a complete list of queued resource request statuses, see the\n[Queued Resources](/tpu/docs/queued-resources) documentation.\n\nConnect to the TPU using SSH\n----------------------------\n\n```bash\n gcloud compute tpus tpu-vm ssh TPU_NAME\n```\n\nCreate a Conda environment\n--------------------------\n\n1. Create a directory for Miniconda:\n\n ```bash\n mkdir -p ~/miniconda3\n ```\n2. Download the Miniconda installer script:\n\n ```bash\n wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda3/miniconda.sh\n ```\n3. Install Miniconda:\n\n ```bash\n bash ~/miniconda3/miniconda.sh -b -u -p ~/miniconda3\n ```\n4. Remove the Miniconda installer script:\n\n ```bash\n rm -rf ~/miniconda3/miniconda.sh\n ```\n5. Add Miniconda to your `PATH` variable:\n\n ```bash\n export PATH=\"$HOME/miniconda3/bin:$PATH\"\n ```\n6. Reload `~/.bashrc` to apply the changes to the `PATH` variable:\n\n ```bash\n source ~/.bashrc\n ```\n7. Create a new Conda environment:\n\n ```bash\n conda create -n tpu python=3.10\n ```\n8. Activate the Conda environment:\n\n ```bash\n source activate tpu\n ```\n\nSet up MaxDiffusion\n-------------------\n\n1. Clone the MaxDiffusion GitHub repository and navigate to the MaxDiffusion directory:\n\n ```bash\n git clone https://github.com/google/maxdiffusion.git && cd maxdiffusion\n ```\n2. Switch to the `mlperf-4.1` branch:\n\n ```bash\n git checkout mlperf4.1\n ```\n3. Install MaxDiffusion:\n\n ```bash\n pip install -e .\n ```\n4. Install dependencies:\n\n ```bash\n pip install -r requirements.txt\n ```\n5. Install JAX:\n\n ```bash\n pip install jax[tpu]==0.4.34 jaxlib==0.4.34 ml-dtypes==0.2.0 -i https://us-python.pkg.dev/ml-oss-artifacts-published/jax/simple/ -f https://storage.googleapis.com/jax-releases/libtpu_releases.html\n ```\n6. Install additional dependencies:\n\n ```bash\n pip install huggingface_hub==0.25 absl-py flax tensorboardX google-cloud-storage torch tensorflow transformers \n ```\n\nGenerate images\n---------------\n\n1. Set environment variables to configure the TPU runtime:\n\n ```bash\n LIBTPU_INIT_ARGS=\"--xla_tpu_rwb_fusion=false --xla_tpu_dot_dot_fusion_duplicated=true --xla_tpu_scoped_vmem_limit_kib=65536\"\n ```\n2. Generate images using the prompt and configurations defined in\n [`src/maxdiffusion/configs/base_xl.yml`](https://github.com/AI-Hypercomputer/maxdiffusion/blob/main/src/maxdiffusion/configs/base_xl.yml):\n\n ```bash\n python -m src.maxdiffusion.generate_sdxl src/maxdiffusion/configs/base_xl.yml run_name=\"my_run\"\n ```\n\n When the images have been generated, be sure to [clean up](#clean-up)\n the TPU resources.\n\nClean up\n--------\n\nDelete the TPU: \n\n```bash\ngcloud compute tpus queued-resources delete QUEUED_RESOURCE_ID \\\n --project PROJECT_ID \\\n --zone ZONE \\\n --force \\\n --async\n```"]]