← All Google Cloud AI guidesSection 3 · 3.1–3.3

Training, hyperparameter tuning and hardware

Section 3: choosing the model type and product, organizing training data, managed custom training (prebuilt vs custom containers, worker pools, Spot and reserved capacity), troubleshooting failed training, hyperparameter tuning with Vizier, fine-tuning foundation models, CPU vs GPU vs TPU, and distributed training strategies.

1. Choosing the model, product and approach

Model type

ProblemGood first choiceWhy
Tabular classification / regressionGradient-boosted trees (XGBoost, BQML boosted trees, AutoML tabular)Strong accuracy, little preprocessing, feature importance
Must explain decisions to regulatorsLinear/logistic regression, small trees, or boosted trees with Explainable AIInterpretable by design, or attributions per prediction
Univariate or many-series forecastingARIMA_PLUS (BQML), AutoML forecasting, TimesFMSeasonality and holidays handled; fast
Images, audio, unstructured patternsDeep neural networks (DNN, CNN, transformers), transfer learningLearns representations; start from a pretrained model
Text generation, summarization, extraction, chat, multimodal reasoningLLM (Gemini) with prompting, RAG or tuningNo task-specific training data needed to start
RecommendationsMatrix factorization, two-tower retrieval + ranking, or Recommendations AICollaborative signals at scale
Anomaly detection with no labelsk-means distance, autoencoder, ARIMA_PLUS anomaliesUnsupervised

Product

ProductChoose when
Prebuilt APIGeneric task, standard classes
BigQuery MLData in BigQuery, SQL team, batch scoring
AutoMLCustom labels, best accuracy with no ML code
Gemini / Model GardenLanguage, vision or multimodal tasks; quick prototypes
Managed (custom) trainingYour code and framework, scaled and managed
PipelinesAny of the above, repeatable and automated
GKE / RayExisting Kubernetes or Ray platform, special scheduling, multi-tenant clusters

Deployment strategy

RequirementStrategy
Predictions needed per request in under ~100 msOnline endpoint (Agent Platform, Cloud Run or GKE), features from the online Feature Store
Scores needed daily for all customersBatch inference (or BQML ML.PREDICT) writing to BigQuery; no endpoint to pay for
Precompute is possible and inputs are known in advanceBatch precompute, then serve lookups from a low-latency store
No connectivity, privacy on device, ultra-low latencyEdge: TF Lite / LiteRT on device, exported AutoML Edge models
Interpretability is a requirement, not a nice-to-have, when the question mentions regulators, adverse action, credit, healthcare or "explain to the customer". Choose interpretable models or enable Explainable AI, and say so in the design.

2. Training and tuning on Google Cloud

Organizing and ingesting training data

Managed training (custom training jobs)

OptionWhen
Prebuilt training container + your Python package or scriptYour framework is supported (TensorFlow, PyTorch, scikit-learn, XGBoost); least work
Custom container (image in Artifact Registry)Unusual dependencies, other languages, exact environment control
Worker poolsPool 0 is the chief; further pools are workers, parameter servers or a reduction server
Serverless Training vs Managed Training ClustersPay per job with on-demand capacity, or reserved, persistent clusters for large, frequent training
Spot VMs, reservations, Dynamic Workload SchedulerCheaper interruptible capacity (checkpoint!), guaranteed capacity, or queued access to scarce GPUs
Kubeflow on GKE / Ray on Agent PlatformExisting Kubernetes skills, custom operators; Ray for Python-native distributed data and training
AutoML and Tabular WorkflowsNo training code; Tabular Workflows expose the AutoML pipeline for more control
gcloud ai custom-jobs create --region=us-central1 --display-name=churn-train \
  --python-package-uris=gs://BUCKET/trainer-0.1.tar.gz \
  --worker-pool-spec=machine-type=n1-standard-8,accelerator-type=NVIDIA_TESLA_T4,accelerator-count=1,replica-count=1,executor-image-uri=PREBUILT_PYTORCH_IMAGE,python-module=trainer.task \
  --args=--epochs=10,--data=bq://PROJECT_ID.shop.train
# The job writes its model to the directory in AIP_MODEL_DIR (a Cloud Storage path).

Troubleshooting training failures

SymptomLikely causeFix
Job fails immediately with permission deniedThe training service account can't read the bucket or BigQuery tableGrant the job's service account the roles it needs (it's not your user identity)
Out of memory (host or GPU)Batch size too large, data loaded fully into memorySmaller batches, streaming input (tf.data, DataLoader), larger machine or more GPUs
Loss becomes NaNLearning rate too high, unscaled features, bad labels, log(0)Lower learning rate, normalize, gradient clipping, check data
Quota exceeded / resources unavailableGPU quota or capacity in the regionRequest quota, another region or accelerator, reservations or Dynamic Workload Scheduler
Job restarted and lost hours of workSpot preemption or maintenance without checkpointsCheckpoint to Cloud Storage regularly; resume from the latest checkpoint
GPU utilization lowInput pipeline bottleneckPrefetch, parallel reads, TFRecord shards, cache; profile with TensorBoard Profiler
Training accuracy high, validation poorOverfittingRegularization, dropout, more data, augmentation, early stopping
Both training and validation poorUnderfitting or bad featuresBigger model, better features, train longer

Hyperparameter tuning

Hyperparameter tuning jobs (powered by Vizier) run many trials of your training code with different values and optimize a metric your code reports (with the cloudml-hypertune helper or the metric reporting API).

Fine-tuning foundation models

OptionUse
Supervised tuning of Gemini on Agent Platform (or from BigQuery ML)Managed, parameter-efficient tuning on JSONL prompt/response pairs; the tuned model gets its own endpoint
Open models from Model Garden (Gemma, Llama) with LoRA / QLoRA notebooks or managed tuningFull control of weights; deploy where you want
Reinforcement learning / preference tuningAlign to human preferences
Consider tuning only when prompting and grounding fall short and you have good labelled examples. Benefits: higher quality on a narrow task, shorter prompts (cheaper, faster), consistent format. Costs: data preparation, tuning jobs, re-tuning when the base model is updated, and evaluation work.

3. Hardware and distributed training

HardwareBest forWatch out
CPUClassic ML (scikit-learn, XGBoost on modest data), preprocessing, small models, low-volume servingSlow for deep learning
GPU (NVIDIA L4, A100, H100, H200, B200...)Most deep learning; flexible across frameworks; custom CUDA ops; inference with TensorRT or vLLMQuota and capacity; cost when idle
TPU (Cloud TPU v5e, v5p, Trillium, Ironwood...)Large matrix-heavy models in JAX, TensorFlow or PyTorch/XLA; large-batch training; high-throughput LLM serving at low costCustom ops not supported by XLA, heavy dynamic shapes, high-precision (float64) math and very small models fit poorly
Edge (phones, Edge TPU, Jetson)On-device inference, offline useNeeds quantized, small models
Accelerator generations change quickly. The exam tests the trade-off (CPU vs GPU vs TPU, and when TPUs don't fit), not the newest SKU.

Distributed training strategies

StrategyHowWhen
Data parallelismCopy the model to every device; split each batch; average gradients with all-reduceThe model fits on one device and you want speed. TensorFlow MirroredStrategy (one machine), MultiWorkerMirroredStrategy (many machines), TPUStrategy; PyTorch DDP
Parameter serverWorkers push gradients to parameter servers asynchronouslyLarge sparse models (embeddings), many CPU workers
Model parallelism (tensor, pipeline)Split layers or tensors across devicesThe model doesn't fit on one device
Sharded data parallel (FSDP, ZeRO)Shard parameters, gradients and optimizer states across data-parallel workersLarge models with data-parallel simplicity
Reduction Server (Agent Platform)Extra reducer nodes speed up all-reduce for multi-node GPU data parallel jobsNetwork-bound gradient synchronization
"Fits? Data. Doesn't fit? Model." If one copy fits on one device, scale out with data parallelism; if it doesn't, split the model.
More GPUs doesn't fix an input-bound job. If GPUs sit at 30% utilization, fix the data pipeline first. And with data parallelism, scale the learning rate or batch size deliberately: global batch = per-device batch × devices.

4. Accelerator memory calculator

A rough first estimate of whether a model fits on one accelerator, and therefore whether data parallelism is enough or the model must be sharded. Real usage also depends on activations, sequence length and batch size.

Bytes per parameter: 16-bit inference ≈ 2; int8 ≈ 1; int4 ≈ 0.5; full mixed-precision training with Adam ≈ 16 (weights, gradients, two optimizer states, master weights).

5. Lab: custom training, tuning and the registry

Placeholders: PROJECT_ID, BUCKET, region us-central1.

  1. Write a scikit-learn or XGBoost script that reads from BigQuery, trains, reports a metric with cloudml-hypertune, and saves the model to AIP_MODEL_DIR.
  2. Run it as a custom training job with a prebuilt container and a dedicated, least-privilege service account. Remove its BigQuery role once and read the 403 error: which principal does it name?
  3. Run a hyperparameter tuning job with 12 trials, parallel 3, a log-scaled learning rate and a discrete max depth. Compare with parallel 12: what changes in wall-clock time and best metric?
  4. Add checkpointing to Cloud Storage and rerun on Spot VMs; confirm it resumes after a restart.
  5. Upload the best model to Model Registry as version 1 with a prebuilt prediction container. Continue in the serving lab.

Clean up: delete jobs' output you don't need and the staging bucket contents.

6. "Which training setup?" chooser

Pick an item to see what it means, why, and the tempting wrong choice.

Pick one above.

7. Sample outputs

What the tools return. Expand an item to see a typical result and what to read in it.

Illustrative output, not captured from a real project. IDs, numbers and some field names are invented or simplified, and formats change between releases. Learn the shape and the fields called out under each one.
gcloud ai hp-tuning-jobs describe JOB_ID --region=us-central1Hyperparameter tuning trials
state: JOB_STATE_SUCCEEDED
maxTrialCount: 20
parallelTrialCount: 4
studySpec:
  algorithm: ALGORITHM_UNSPECIFIED      # Bayesian optimization by default
  metrics: [{metricId: auc_pr, goal: MAXIMIZE}]
trials:
- id: '7'   parameters: [{parameterId: learning_rate, value: 0.042}, {parameterId: max_depth, value: 7}]
  finalMeasurement: {metrics: [{metricId: auc_pr, value: 0.734}]}
- id: '12'  state: INFEASIBLE
- id: '15'  parameters: [{parameterId: learning_rate, value: 0.31}, {parameterId: max_depth, value: 3}]
  finalMeasurement: {metrics: [{metricId: auc_pr, value: 0.611}]}

What to look at: The best trial (7) wins on the reported metric auc_pr, which the training code must report. Four trials ran in parallel: faster, but each wave learns less from the previous ones. An INFEASIBLE trial usually means the code crashed or didn't report the metric for those values.

Training job log (custom job)A failure you should recognize
INFO  Reading training data from bq://PROJECT_ID.shop.train
ERROR google.api_core.exceptions.Forbidden: 403 Access Denied: Table PROJECT_ID:shop.train:
      User does not have bigquery.tables.getData permission.
      principal: service-PROJECT_NUMBER@gcp-sa-aiplatform-cc.iam.gserviceaccount.com
Job failed: The replica workerpool0-0 exited with a non-zero status of 1.

What to look at: The job runs as a service account, not as you. Grant that identity (or, better, a custom service account you pass to the job) BigQuery Data Viewer on the dataset and BigQuery Job User on the project.

8. Scenarios

Exam-style situations: how to diagnose each one, the likely cause and the fix.

9. Searchable reference

ItemMeaning

10. Flashcards (click to flip)

11. Common gotchas in one list

  1. The job runs as a service account, not as you: grant it the roles.
  2. Save models to AIP_MODEL_DIR so the platform can find them.
  3. Spot VMs need checkpoints or every preemption restarts training.
  4. Fits on one device → data parallel; doesn't → shard or split the model.
  5. Low GPU utilization is usually the input pipeline, not too few GPUs.
  6. TPUs don't fit custom CUDA ops, float64 or heavy dynamic shapes.
  7. More parallel trials finish sooner but search less adaptively.
  8. Use log scale for learning rates in tuning.
  9. Full fine-tuning needs ~8× the memory of 16-bit inference; LoRA and quantization cut it.
  10. Prebuilt containers before custom containers when the framework is supported.
  11. Interpretability requirements drive model choice in regulated domains.

12. Practice quiz (19 questions)

Score: 0 / 0
← Data, Feature Store, notebooks, experimentsNext: Serving and scaling →