Mantenha tudo organizado com as coleções
Salve e categorize o conteúdo com base nas suas preferências.
Inferência do MaxDiffusion em TPUs v6e
Este tutorial mostra como disponibilizar modelos MaxDiffusion na TPU v6e. Neste
tutorial, você vai gerar imagens usando o modelo Stable Diffusion XL.
Antes de começar
Prepare-se para provisionar uma TPU v6e com 4 chips:
Siga o guia Configurar o ambiente do Cloud TPU
para configurar um projeto Google Cloud , configurar a Google Cloud CLI,
ativar a API Cloud TPU e garantir que você tenha acesso para usar
o Cloud TPU.
Faça a autenticação com Google Cloud e configure o projeto e a zona padrão para a Google Cloud CLI.
Quando estiver tudo pronto para garantir a capacidade da TPU, consulte Cotas da Cloud TPU para mais informações. Se
você tiver outras dúvidas sobre como garantir a capacidade, entre em contato com a equipe de vendas ou de conta do Cloud TPU.
Provisionar o ambiente do Cloud TPU
É possível provisionar VMs do TPU com o
GKE, com o GKE e o
XPK
ou como recursos em fila.
Pré-requisitos
Verifique se o projeto tem cota de TPUS_PER_TPU_FAMILY suficiente,
que especifica o número máximo de chips que você pode acessar no
projetoGoogle Cloud .
Verifique se o projeto tem cota suficiente de TPU para:
[[["Fácil de entender","easyToUnderstand","thumb-up"],["Meu problema foi resolvido","solvedMyProblem","thumb-up"],["Outro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Informações incorretas ou exemplo de código","incorrectInformationOrSampleCode","thumb-down"],["Não contém as informações/amostras de que eu preciso","missingTheInformationSamplesINeed","thumb-down"],["Problema na tradução","translationIssue","thumb-down"],["Outro","otherDown","thumb-down"]],["Última atualização 2025-08-18 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```"]]