|
| 1 | +import pytest |
| 2 | + |
| 3 | +from keras_hub.src.models.backbone import Backbone |
| 4 | +from keras_hub.src.models.gemma3n.gemma3n_backbone import Gemma3nBackbone |
| 5 | +from keras_hub.src.tests.test_case import TestCase |
| 6 | +from keras_hub.src.utils.transformers import convert_gemma3n |
| 7 | + |
| 8 | + |
| 9 | +class TestGemma3nConverter(TestCase): |
| 10 | + @pytest.mark.extra_large |
| 11 | + def test_convert_tiny_preset(self): |
| 12 | + model = Gemma3nBackbone.from_preset( |
| 13 | + "hf://yujiepan/gemma-3n-tiny-random-dim4", |
| 14 | + load_weights=False, |
| 15 | + ) |
| 16 | + self.assertIsInstance(model, Gemma3nBackbone) |
| 17 | + |
| 18 | + @pytest.mark.large |
| 19 | + def test_class_detection(self): |
| 20 | + model = Backbone.from_preset( |
| 21 | + "hf://yujiepan/gemma-3n-tiny-random-dim4", |
| 22 | + load_weights=False, |
| 23 | + ) |
| 24 | + self.assertIsInstance(model, Gemma3nBackbone) |
| 25 | + |
| 26 | + def test_gemma3n_rope_theta(self): |
| 27 | + # transformers < 5 format |
| 28 | + text_cfg = { |
| 29 | + "vocab_size": 100, |
| 30 | + "hidden_size": 32, |
| 31 | + "num_hidden_layers": 2, |
| 32 | + "num_attention_heads": 4, |
| 33 | + "num_key_value_heads": 2, |
| 34 | + "head_dim": 8, |
| 35 | + "intermediate_size": 48, |
| 36 | + "layer_types": ["full_attention", "full_attention"], |
| 37 | + "sliding_window": 4096, |
| 38 | + "rope_theta": 10000.0, |
| 39 | + "rms_norm_eps": 1e-5, |
| 40 | + "max_position_embeddings": 32, |
| 41 | + "vocab_size_per_layer_input": 100, |
| 42 | + "hidden_size_per_layer_input": 32, |
| 43 | + "altup_num_inputs": 1, |
| 44 | + "laurel_rank": 1, |
| 45 | + "attention_bias": False, |
| 46 | + "attention_dropout": 0.0, |
| 47 | + "altup_coef_clip": 1.0, |
| 48 | + "altup_active_idx": 0, |
| 49 | + "altup_correct_scale": False, |
| 50 | + "num_kv_shared_layers": 1, |
| 51 | + } |
| 52 | + transformers_config = { |
| 53 | + "text_config": text_cfg, |
| 54 | + "vision_config": { |
| 55 | + "hidden_size": 32, |
| 56 | + "vocab_size": 100, |
| 57 | + "vocab_offset": 0, |
| 58 | + }, |
| 59 | + "audio_config": { |
| 60 | + "hidden_size": 32, |
| 61 | + "input_feat_size": 32, |
| 62 | + "sscp_conv_channel_size": 32, |
| 63 | + "sscp_conv_kernel_size": 3, |
| 64 | + "sscp_conv_stride_size": 2, |
| 65 | + "sscp_conv_group_norm_eps": 1e-5, |
| 66 | + "conf_num_hidden_layers": 2, |
| 67 | + "rms_norm_eps": 1e-5, |
| 68 | + "gradient_clipping": 1.0, |
| 69 | + "conf_residual_weight": 0.1, |
| 70 | + "conf_num_attention_heads": 4, |
| 71 | + "conf_attention_chunk_size": 16, |
| 72 | + "conf_attention_context_right": 8, |
| 73 | + "conf_attention_context_left": 8, |
| 74 | + "conf_attention_logit_cap": 10.0, |
| 75 | + "conf_conv_kernel_size": 3, |
| 76 | + "conf_reduction_factor": 2, |
| 77 | + "vocab_size": 100, |
| 78 | + "vocab_offset": 0, |
| 79 | + }, |
| 80 | + "vision_soft_tokens_per_image": 16, |
| 81 | + "image_token_id": 1, |
| 82 | + "audio_soft_tokens_per_image": 16, |
| 83 | + "audio_token_id": 2, |
| 84 | + } |
| 85 | + keras_config = convert_gemma3n.convert_backbone_config( |
| 86 | + transformers_config |
| 87 | + ) |
| 88 | + self.assertEqual(keras_config["rope_theta"], 10000.0) |
| 89 | + |
| 90 | + # transformers >= 5 format |
| 91 | + text_cfg["rope_parameters"] = {"rope_theta": 20000.0} |
| 92 | + keras_config = convert_gemma3n.convert_backbone_config( |
| 93 | + transformers_config |
| 94 | + ) |
| 95 | + self.assertEqual(keras_config["rope_theta"], 20000.0) |
0 commit comments