# VertexAI Training and Gated model access

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fvertexai-training-and-gated-model-access%2F171123%3E%5C
**Category:** Intermediate\
**Created:** [December 3, 2025, 9:19pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fvertexai-training-and-gated-model-access%2F171123 "2025-12-03T21:19:30Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![John6666](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/john6666/32/27664_2.png) [@John6666](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FJohn6666)%5C
**Post date:** [December 3, 2025, 10:52pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fvertexai-training-and-gated-model-access%2F171123%2F2 "2025-12-03T22:52:22Z")

</div>

Assuming the token itself is correct.  
In Vertex AI, it seems you need to explicitly pass the token “as an environment variable” to the container…

* * *

Short direct answer:

- From the Hugging Face side, **yes** : a valid user access token with `write` scope (which includes `read`) is enough to access a gated model like `google/txgemma-2b-predict`, as long as the **same HF account** behind that token has accepted the TxGemma terms. ([Hugging Face](https://huggingface.co/google/txgemma-9b-chat/tree/main "google/txgemma-9b-chat at main"))
- From the Vertex AI side, that token only works if you **inject it into the training container as an environment variable** (for example `HF_TOKEN`) and your code (or `transformers`/TRL) uses it when calling `from_pretrained`. The official Vertex + TRL examples for gated models do exactly this. ([Hugging Face](https://huggingface.co/docs/google-cloud/en/examples/vertex-ai-notebooks-trl-lora-sft-fine-tuning-on-vertex-ai "Fine-tune Gemma 2B with PyTorch Training DLC using SFT + LoRA on Vertex AI"))

So: just having a valid token in your account is not enough; you must pass it to the Vertex job in the right way.

Below is the “correct way” with context and a concrete pattern.

* * *

## 1. Hugging Face side: gating + token requirements

### 1.1 TxGemma is a gated model

TxGemma models on Hugging Face (e.g. `google/txgemma-9b-chat`, `google/txgemma-9b-predict`, `google/txgemma-2b-predict`) are “gated”:

- The repo page says:

- To get access, you must:

So step 0 is verifying that the HF user who owns your token actually sees “Access granted” on the TxGemma page.

### 1.2 Token scopes: read vs write

Hugging Face “User Access Tokens” are the standard way to authenticate apps and notebooks:

- Docs: “User Access Tokens are the preferred way to authenticate… You can set the role (read, write, admin).” ([Hugging Face](https://huggingface.co/docs/hub/en/security-tokens "User access tokens"))

- For **private or gated** models, the requirement is: token must have **read or broader** scope. For example, BigQuery’s Vertex integration docs explicitly say:

Your `write` token satisfies this; write ⊇ read, so scope is not the problem.

Typical error when gating is not correctly satisfied looks like what you may have seen on Kaggle for TxGemma:

> “Access to model google/txgemma-2b-predict is restricted. You must have access to it and be authenticated to access it.” ([Kaggle](https://www.kaggle.com/code/shyhill4/txgemma-2b-predictive "TxGemma-2B-Predictive"))

That can happen if:

- Token is missing in the environment, or
- Token belongs to a different user than the one who accepted the terms.

* * *

## 2. Vertex AI side: how to specify the token correctly

The _canonical_ pattern is shown in the Hugging Face + Google Cloud examples for fine-tuning Mistral or Gemma with TRL on Vertex AI. They all do the same thing:

1. Use a Hugging Face PyTorch Training DLC image.
2. Create a `CustomContainerTrainingJob` whose command runs `trl sft`.
3. Pass the HF token via `environment_variables={"HF_TOKEN": ...}`. ([Hugging Face](https://huggingface.co/docs/google-cloud/en/examples/vertex-ai-notebooks-trl-lora-sft-fine-tuning-on-vertex-ai "Fine-tune Gemma 2B with PyTorch Training DLC using SFT + LoRA on Vertex AI"))

### 2.1. Official example pattern (Mistral / Gemma on Vertex)

From the “Fine-tune Mistral 7B v0.3 with PyTorch Training DLC using SFT on Vertex AI” guide: ([Hugging Face](https://huggingface.co/docs/google-cloud/en/examples/vertex-ai-notebooks-trl-full-sft-fine-tuning-on-vertex-ai "Fine-tune Mistral 7B v0.3 with PyTorch Training DLC using SFT on Vertex AI"))

- They define a `CustomContainerTrainingJob`:

- They build `args` for `trl sft` (model, dataset, hyperparameters). ([Hugging Face](https://huggingface.co/docs/google-cloud/en/examples/vertex-ai-notebooks-trl-full-sft-fine-tuning-on-vertex-ai "Fine-tune Mistral 7B v0.3 with PyTorch Training DLC using SFT on Vertex AI"))

- Crucially, when they call `job.submit(...)`, they pass the token via `environment_variables`:

The Gemma/LoRA TRL example is identical in structure and explicitly notes:

> “As you are fine-tuning a gated model … you need to set the `HF_TOKEN` environment variable.” ([Hugging Face](https://huggingface.co/docs/google-cloud/en/examples/vertex-ai-notebooks-trl-lora-sft-fine-tuning-on-vertex-ai "Fine-tune Gemma 2B with PyTorch Training DLC using SFT + LoRA on Vertex AI"))

The Vertex AI community samples for Gemma and Llama follow the same pattern: they read `HF_TOKEN` from the notebook/UI and add it to `env_vars` before constructing the job. ([GitHub](https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/community/model_garden/model_garden_pytorch_gemma_peft_finetuning_hf.ipynb "Vertex AI Model Garden - Gemma Finetuning (PEFT + vLLM)"))

### 2.2. How `transformers` / TRL picks up the token

Recent `huggingface_hub` / `transformers` automatically look for environment variables like:

- `HF_TOKEN`
- `HUGGING_FACE_HUB_TOKEN`

If one is set, `AutoModel.from_pretrained("google/txgemma-2b-predict")` will use it to authenticate. ([Hugging Face](https://huggingface.co/docs/hub/en/security-tokens "User access tokens"))

So as long as:

- Your TRL CLI (inside the DLC) uses standard `from_pretrained`, and
- You pass `HF_TOKEN` correctly,

you do **not** need to manually add `token=...` in your training script.

If you want to be explicit in a custom script, you can still do:

```python
import os
from transformers import AutoTokenizer, AutoModelForCausalLM

model_id = "google/txgemma-2b-predict"
token = os.environ["HF_TOKEN"]

tokenizer = AutoTokenizer.from_pretrained(model_id, token=token)
model = AutoModelForCausalLM.from_pretrained(model_id, token=token)

```

But with the DLC + TRL CLI examples, the environment variable alone is normally enough.

* * *

## 3. Concrete “correct way” for your TxGemma job

Assuming:

- You’re using the Hugging Face PyTorch Training DLC (as in the TRL examples).
- You want to fine-tune `google/txgemma-2b-predict` with `trl sft` on Vertex AI.

The steps are:

1. **Verify HF-side gating with the right account**

2. **Create or copy your HF token (write is fine)**

3. **In your notebook/script, pass the token into Vertex via env vars**

4. **Use the correct model ID**

5. **Check Cloud Logs if it still fails**

* * *

## 4. Answering your exact questions explicitly

> is using a valid token (WRITE) enough to setup a custom training job on VertexAI using a gated model (aka TxGemma)?

From Hugging Face’s perspective: **yes**. Any token with **read or broader** scope is sufficient to download a gated model, provided the associated user has accepted the model’s terms. ([Hugging Face](https://huggingface.co/docs/hub/en/models-gated "Gated models"))

From Vertex AI’s perspective: the token must be **correctly passed into the container** (as `HF_TOKEN` or `HUGGING_FACE_HUB_TOKEN`) and actually used by `transformers` / TRL. If your job “fails to retrieve the model,” the usual reasons are:

- The token is not set in `environment_variables`,
- It belongs to a user that hasn’t accepted TxGemma’s terms,
- The model id is wrong, or
- There’s a network restriction.

> Correct way to specify Hugging Face token on VertexAI?

The standard, documented way is:

- In your `CustomContainerTrainingJob.run()` (or `.submit()`), pass:

- Optionally, use `HUGGING_FACE_HUB_TOKEN` instead; recent `huggingface_hub` respects both names.

This is the “correct” and common approach used both in Hugging Face’s official docs and Google’s Vertex AI sample notebooks.

---

_[View the full topic](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fvertexai-training-and-gated-model-access%2F171123)._
