Update README.md
Browse files
README.md
CHANGED
|
@@ -1,3 +1,100 @@
|
|
| 1 |
---
|
| 2 |
license: apache-2.0
|
| 3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
license: apache-2.0
|
| 3 |
---
|
| 4 |
+
|
| 5 |
+
```python
|
| 6 |
+
import torch
|
| 7 |
+
from transformers import AutoProcessor
|
| 8 |
+
from transformers.models.gemma4_unified.configuration_gemma4_unified import (
|
| 9 |
+
Gemma4UnifiedConfig,
|
| 10 |
+
Gemma4UnifiedTextConfig,
|
| 11 |
+
Gemma4UnifiedVisionConfig,
|
| 12 |
+
)
|
| 13 |
+
from transformers.models.gemma4_unified.modeling_gemma4_unified import Gemma4UnifiedForConditionalGeneration
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
save_dir = "./tiny-random-gemma4-unified-it"
|
| 17 |
+
|
| 18 |
+
# Tiny text config mirroring the 12B gemma4_unified architecture:
|
| 19 |
+
# - PLE disabled (no hidden_size_per_layer_input field exists on unified text config)
|
| 20 |
+
# - attention_k_eq_v=True so full-attention layers fuse v_proj into k_proj
|
| 21 |
+
# - num_global_key_value_heads=1, global_head_dim larger than head_dim
|
| 22 |
+
# - num_kv_shared_layers=0 (12B does not share KV across layers)
|
| 23 |
+
# - use_bidirectional_attention="vision"
|
| 24 |
+
text_config = Gemma4UnifiedTextConfig(
|
| 25 |
+
hidden_size=32,
|
| 26 |
+
intermediate_size=64,
|
| 27 |
+
num_hidden_layers=4,
|
| 28 |
+
num_attention_heads=4,
|
| 29 |
+
num_key_value_heads=2,
|
| 30 |
+
head_dim=16,
|
| 31 |
+
global_head_dim=32,
|
| 32 |
+
num_global_key_value_heads=1,
|
| 33 |
+
vocab_size=262144,
|
| 34 |
+
max_position_embeddings=512,
|
| 35 |
+
rms_norm_eps=1e-6,
|
| 36 |
+
hidden_activation="gelu_pytorch_tanh",
|
| 37 |
+
sliding_window=64,
|
| 38 |
+
layer_types=["sliding_attention", "sliding_attention", "sliding_attention", "full_attention"],
|
| 39 |
+
num_kv_shared_layers=0,
|
| 40 |
+
attention_k_eq_v=True,
|
| 41 |
+
use_double_wide_mlp=False,
|
| 42 |
+
use_bidirectional_attention="vision",
|
| 43 |
+
final_logit_softcapping=30.0,
|
| 44 |
+
tie_word_embeddings=True,
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
# Vision is an encoder-free embedder: model_patch_size = patch_size * pooling_kernel_size.
|
| 48 |
+
# mm_embed_dim / output_proj_dims must match the text hidden_size.
|
| 49 |
+
vision_config = Gemma4UnifiedVisionConfig(
|
| 50 |
+
patch_size=16,
|
| 51 |
+
pooling_kernel_size=3,
|
| 52 |
+
mm_embed_dim=32,
|
| 53 |
+
output_proj_dims=32,
|
| 54 |
+
mm_posemb_size=128,
|
| 55 |
+
rms_norm_eps=1e-6,
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
config = Gemma4UnifiedConfig(
|
| 59 |
+
text_config=text_config.to_dict(),
|
| 60 |
+
vision_config=vision_config.to_dict(),
|
| 61 |
+
audio_config=None,
|
| 62 |
+
boi_token_id=255999,
|
| 63 |
+
eoi_token_id=258882,
|
| 64 |
+
image_token_id=258880,
|
| 65 |
+
video_token_id=258884,
|
| 66 |
+
boa_token_id=256000,
|
| 67 |
+
eoa_token_index=258883,
|
| 68 |
+
audio_token_id=258881,
|
| 69 |
+
tie_word_embeddings=True,
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
# Seed before init so the random weights are reproducible. This seed produces a fixture
|
| 73 |
+
# whose greedy generation has no near-ties, so OV-vs-transformers token equality is stable
|
| 74 |
+
# under the small (~1e-4) numerical differences of OpenVINO inference.
|
| 75 |
+
torch.manual_seed(42)
|
| 76 |
+
model = Gemma4UnifiedForConditionalGeneration(config)
|
| 77 |
+
model = model.to(dtype=torch.float32)
|
| 78 |
+
model.eval()
|
| 79 |
+
|
| 80 |
+
print(f"Model parameters: {sum(p.numel() for p in model.parameters()):,}")
|
| 81 |
+
|
| 82 |
+
model.save_pretrained(save_dir)
|
| 83 |
+
|
| 84 |
+
# Reuse the reference processor but shrink the soft-token budget so the tiny
|
| 85 |
+
# position embedding table (mm_posemb_size=64) is large enough.
|
| 86 |
+
processor = AutoProcessor.from_pretrained("google/gemma-4-12b-it")
|
| 87 |
+
processor.image_processor.max_soft_tokens = 70
|
| 88 |
+
processor.image_processor.image_seq_length = 70
|
| 89 |
+
processor.save_pretrained(save_dir)
|
| 90 |
+
|
| 91 |
+
print(f"Tiny Gemma4Unified model saved to {save_dir}")
|
| 92 |
+
|
| 93 |
+
# Sanity forward pass
|
| 94 |
+
input_ids = torch.randint(0, 262144, (1, 10))
|
| 95 |
+
with torch.no_grad():
|
| 96 |
+
out = model(input_ids=input_ids)
|
| 97 |
+
print("logits shape:", out.logits.shape)
|
| 98 |
+
print("Forward pass OK!")
|
| 99 |
+
|
| 100 |
+
```
|