|
| 1 | +# Copyright 2025 The AI Edge Torch Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# ============================================================================== |
| 15 | + |
| 16 | +"""Example of building decoder for Qwen 2.5 VL models.""" |
| 17 | + |
| 18 | +import ai_edge_torch.generative.layers.model_config as cfg |
| 19 | +from ai_edge_torch.generative.utilities import model_builder |
| 20 | +from torch import nn |
| 21 | + |
| 22 | +TENSOR_NAMES = model_builder.TENSOR_NAMES |
| 23 | + |
| 24 | + |
| 25 | +class Decoder(model_builder.DecoderOnlyModel): |
| 26 | + """A decoder for Qwen-VL model built from the Edge Generative API layers.""" |
| 27 | + pass |
| 28 | + |
| 29 | + |
| 30 | +def get_decoder_config(kv_cache_max_len: int = 1024) -> cfg.ModelConfig: |
| 31 | + """Returns the model config for a Qwen 2.5 VL 3B model. |
| 32 | +
|
| 33 | + Args: |
| 34 | + kv_cache_max_len (int): The maximum sequence length of the KV cache. Default |
| 35 | + is 1024. |
| 36 | +
|
| 37 | + Returns: |
| 38 | + The model config for a Qwen 2.5 VL 3B model. |
| 39 | + """ |
| 40 | + attn_config = cfg.AttentionConfig( |
| 41 | + num_heads=16, |
| 42 | + head_dim=128, |
| 43 | + num_query_groups=2, |
| 44 | + rotary_base=1000000, |
| 45 | + rotary_percentage=1.0, |
| 46 | + qkv_use_bias=True, |
| 47 | + ) |
| 48 | + ff_config = cfg.FeedForwardConfig( |
| 49 | + type=cfg.FeedForwardType.GATED, |
| 50 | + activation=cfg.ActivationConfig(cfg.ActivationType.SILU), |
| 51 | + intermediate_size=11008, |
| 52 | + ) |
| 53 | + norm_config = cfg.NormalizationConfig( |
| 54 | + type=cfg.NormalizationType.RMS_NORM, |
| 55 | + epsilon=1e-06, |
| 56 | + ) |
| 57 | + block_config = cfg.TransformerBlockConfig( |
| 58 | + attn_config=attn_config, |
| 59 | + ff_config=ff_config, |
| 60 | + pre_attention_norm_config=norm_config, |
| 61 | + post_attention_norm_config=norm_config, |
| 62 | + ) |
| 63 | + config = cfg.ModelConfig( |
| 64 | + vocab_size=151936, |
| 65 | + num_layers=36, |
| 66 | + max_seq_len=32768, |
| 67 | + embedding_dim=2048, |
| 68 | + kv_cache_max_len=kv_cache_max_len, |
| 69 | + block_configs=block_config, |
| 70 | + final_norm_config=norm_config, |
| 71 | + enable_hlfb=True, |
| 72 | + ) |
| 73 | + return config |
| 74 | + |
| 75 | + |
| 76 | +def get_fake_decoder_config(**kwargs) -> cfg.ModelConfig: |
| 77 | + config = get_decoder_config(**kwargs) |
| 78 | + config.vocab_size = 128 |
| 79 | + config.num_layers = 2 |
| 80 | + # Decoder has only one block config. |
| 81 | + config.block_config(0).ff_config.intermediate_size = 64 |
| 82 | + return config |
| 83 | + |
| 84 | + |
| 85 | +def build_decoder(checkpoint_path: str, **kwargs) -> nn.Module: |
| 86 | + return model_builder.build_decoder_only_model( |
| 87 | + checkpoint_path=checkpoint_path, |
| 88 | + config=get_decoder_config(**kwargs), |
| 89 | + tensor_names=TENSOR_NAMES, |
| 90 | + model_class=Decoder, |
| 91 | + ) |
0 commit comments