rl-post-train-artifacts / scripts /create_random_gemma3_smoke.py
myx160's picture
Add files using upload-large-folder tool
e588892 verified
Raw History Blame Contribute Delete
2.44 kB
#!/usr/bin/env python
"""Create a tiny random Gemma3-text checkpoint for wiring-only smoke tests.
The public Gemma3-270M checkpoint is gated. This artifact is intentionally
*not* a pretrained model and must never be used for a result; it only lets the
ValueCriticModel forward/backward code be tested without credentials.
"""
from __future__ import annotations
import argparse
from pathlib import Path
import torch
from transformers import Gemma3ForCausalLM, Gemma3TextConfig
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument(
"--output",
type=Path,
default=Path("/data/myx/RL_post_train/checkpoints/pretrained/gemma-3-270m-smoke-random"),
)
args = parser.parse_args()
if args.output.exists() and any(args.output.iterdir()):
raise FileExistsError(f"refusing to overwrite non-empty directory: {args.output}")
torch.manual_seed(0)
config = Gemma3TextConfig(
# The official Gemma tokenizer used by the SigLIP/Gemma checkpoint
# has a 256k vocabulary. Keeping the same size is important for a
# wiring smoke test: a smaller synthetic vocabulary would trigger an
# embedding index assertion before the model can be exercised.
vocab_size=262144,
hidden_size=640,
intermediate_size=1280,
# The value expert currently has four decoder layers and consumes the
# corresponding first four entries of the backbone DynamicCache.
# Keep at least that many layers in the synthetic backbone so cache
# indexing exercises the real two-stage path.
num_hidden_layers=4,
# Gemma-3 270M uses grouped-query attention with one KV head. The
# RECAP value experts also use one KV head, so this must match for the
# two-stage DynamicCache hand-off to be shape-compatible.
num_attention_heads=4,
num_key_value_heads=1,
head_dim=256,
max_position_embeddings=2048,
layer_types=["full_attention"] * 4,
tie_word_embeddings=True,
)
model = Gemma3ForCausalLM(config)
args.output.mkdir(parents=True, exist_ok=True)
model.save_pretrained(args.output, safe_serialization=True)
print(
f"created random Gemma3-text smoke checkpoint: {args.output} "
f"params={sum(p.numel() for p in model.parameters())}"
)
return 0
if __name__ == "__main__":
raise SystemExit(main())