Stay organized with collections
Save and categorize content based on your preferences.
MaxDiffusion inference on v6e TPUs
This tutorial shows how to serve MaxDiffusion models on TPU v6e. In this
tutorial, you generate images using the Stable Diffusion XL model.
Before you begin
Prepare to provision a TPU v6e with 4 chips:
Follow Set up the Cloud TPU environment
guide to set up a Google Cloud project, configure the Google Cloud CLI,
enable the Cloud TPU API, and ensure you have access to use
Cloud TPUs.
Authenticate with Google Cloud and configure the default project and
zone for Google Cloud CLI.
When you are ready to secure TPU capacity, see Cloud TPU
Quotas for more information about the Cloud TPU quotas. If
you have additional questions about securing capacity, contact your Cloud TPU
sales or account team.
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.
Verify that your project has enough TPU quota for:
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-11 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```"]]