Download code/tt_embgemma/model_spec.py from changh95/embeddinggemma-2: direct link, hf CLI and curl.
- Browser
- Download file 5.11 kB
-
https://huggingface.co/changh95/embeddinggemma-2/resolve/main/code/tt_embgemma/model_spec.py
- Command line
-
hf download hf://changh95/embeddinggemma-2/code/tt_embgemma/model_spec.py
-
curl -L -o model_spec.py https://huggingface.co/changh95/embeddinggemma-2/resolve/main/code/tt_embgemma/model_spec.py
5.11 kB
| # SPDX-License-Identifier: Apache-2.0 | |
| """Architecture constants of google/embeddinggemma-2 (config.json, transformers 5.24.1 ``embedding_gemma2`` + | |
| ``gemma4_vision`` / ``gemma4_audio``). ``check_config`` validates a snapshot's config.json against them. | |
| Standard library only.""" | |
| from __future__ import annotations | |
| import json | |
| import math | |
| import os | |
| # ---- text backbone (EmbeddingGemma2TextModel: bidirectional Gemma-4 style encoder) ---- | |
| VOCAB = 262144 | |
| HIDDEN = 512 | |
| INTER = 2048 | |
| LAYERS = 24 | |
| HEADS = 4 | |
| PLE_DIM = 512 # hidden_size_per_layer_input | |
| EMB_DIM = 768 # embedding_projection output (sentence embedding width) | |
| MRL_DIMS = (768, 512, 256, 128) | |
| EPS = 1e-6 | |
| SLIDING_RADIUS = 512 # bidirectional: |q - k| <= 512 on sliding layers | |
| GLOBAL_LAYERS = (5, 11, 17, 23) | |
| LAYER_TYPES = tuple("full" if i in GLOBAL_LAYERS else "sliding" for i in range(LAYERS)) | |
| HEAD_DIM = {"sliding": 256, "full": 512} | |
| KV_HEADS = {"sliding": 2, "full": 1} | |
| ROPE_THETA = {"sliding": 10000.0, "full": 1000000.0} | |
| EMBED_SCALE = math.sqrt(HIDDEN) # fp32 22.627417 (the bf16 model rounds it to 22.625) | |
| PAD_ID, EOS_ID, BOS_ID = 0, 1, 2 | |
| # special tokens (tokenizer_config.json / config.json) | |
| BOI_ID, EOI_ID = 255999, 258882 # <|image> <image|> | |
| BOA_ID, EOA_ID = 256000, 258883 # <|audio> <audio|> | |
| IMAGE_TOKEN_ID = 258880 # <|image|> | |
| AUDIO_TOKEN_ID = 258881 # <|audio|> | |
| VIDEO_TOKEN_ID = 258884 # <|video|> | |
| IMAGE_TOKEN, AUDIO_TOKEN, VIDEO_TOKEN = "<|image|>", "<|audio|>", "<|video|>" | |
| # ---- vision tower (Gemma4VisionModel) ---- | |
| V_HIDDEN = 768 | |
| V_INTER = 3072 | |
| V_LAYERS = 16 | |
| V_HEADS = 12 | |
| V_HEAD_DIM = 64 | |
| V_PATCH = 16 | |
| V_POOL = 3 | |
| V_POS_TABLE = 10240 | |
| V_ROPE_THETA = 100.0 | |
| V_SOFT_TOKENS = (70, 140, 280, 560, 1120) # supported max_soft_tokens per image / frame | |
| V_DEFAULT_IMAGE_TOKENS = 280 | |
| V_DEFAULT_VIDEO_TOKENS = 140 # processor_config.json video_processor.max_soft_tokens | |
| # ---- audio tower (Gemma4AudioModel, USM conformer) ---- | |
| A_HIDDEN = 1024 | |
| A_LAYERS = 12 | |
| A_HEADS = 8 | |
| A_HEAD_DIM = 128 | |
| A_FF = 4096 | |
| A_CONV_K = 5 | |
| A_CHUNK = 12 | |
| A_LEFT = 12 # attention_context_left - 1 (max past horizon) | |
| A_RIGHT = 0 | |
| A_WINDOW = 12 # keys j with 0 <= i - j < 12 (sliding_window_mask_function((12, 0))) | |
| A_LOGIT_CAP = 50.0 | |
| A_INVALID = -1e9 | |
| A_OUT = 1536 # output_proj_dims | |
| A_SUB_CH = (128, 32) | |
| A_RESIDUAL_W = 0.5 | |
| A_MEL = 128 | |
| A_SR = 16000 | |
| A_FRAME = 320 | |
| A_HOP = 160 | |
| A_FFT = 512 | |
| A_MEL_FLOOR = 1e-3 | |
| A_MAX_SAMPLES = 480000 # feature extractor default max_length (30 s, truncation=True) | |
| A_PAD_MULT = 128 # pad_to_multiple_of | |
| A_MS_PER_TOKEN = 40 | |
| def audio_tokens_for_samples(n_samples: int) -> int: | |
| """Soft tokens of one clip (official ``compute_sscp`` on the frame mask) after truncation to 30 s.""" | |
| n = min(int(n_samples), A_MAX_SAMPLES) | |
| n_frames = (n + A_FRAME // 2 - (A_FRAME + 1)) // A_HOP + 1 | |
| if n_frames <= 0: | |
| return 0 | |
| m = n_frames | |
| for _ in range(2): | |
| m = (m + 2 - 3) // 2 + 1 | |
| return m | |
| def check_config(model_dir: str) -> dict: | |
| """Validate config.json of a snapshot; returns it. Raises ValueError on a mismatch.""" | |
| cfg = json.load(open(os.path.join(model_dir, "config.json"))) | |
| t = cfg["text_config"] | |
| want = {"hidden_size": HIDDEN, "intermediate_size": INTER, "num_hidden_layers": LAYERS, | |
| "num_attention_heads": HEADS, "vocab_size": VOCAB, "embedding_dim": EMB_DIM, | |
| "hidden_size_per_layer_input": PLE_DIM, "sliding_window": SLIDING_RADIUS} | |
| for k, v in want.items(): | |
| if t.get(k) != v: | |
| raise ValueError(f"config.json text_config.{k}={t.get(k)!r}, expected {v!r}") | |
| lt = tuple("full" if x == "full_attention" else "sliding" for x in t["layer_types"]) | |
| if lt != LAYER_TYPES: | |
| raise ValueError("config.json layer_types differ from the 5:1 sliding/full pattern") | |
| if t.get("hidden_activation") != "gelu_pytorch_tanh": | |
| raise ValueError("unexpected hidden_activation") | |
| v = cfg.get("vision_config") | |
| if v is not None: | |
| for k, x in {"hidden_size": V_HIDDEN, "num_hidden_layers": V_LAYERS, "num_attention_heads": V_HEADS, | |
| "head_dim": V_HEAD_DIM, "patch_size": V_PATCH, "pooling_kernel_size": V_POOL, | |
| "standardize": False, "use_clipped_linears": False}.items(): | |
| if v.get(k) != x: | |
| raise ValueError(f"config.json vision_config.{k}={v.get(k)!r}, expected {x!r}") | |
| a = cfg.get("audio_config") | |
| if a is not None: | |
| for k, x in {"hidden_size": A_HIDDEN, "num_hidden_layers": A_LAYERS, "num_attention_heads": A_HEADS, | |
| "attention_chunk_size": A_CHUNK, "attention_context_left": A_LEFT + 1, | |
| "attention_context_right": A_RIGHT, "conv_kernel_size": A_CONV_K, | |
| "output_proj_dims": A_OUT, "use_clipped_linears": True}.items(): | |
| if a.get(k) != x: | |
| raise ValueError(f"config.json audio_config.{k}={a.get(k)!r}, expected {x!r}") | |
| return cfg | |