Video-Text-to-Text
Transformers
Safetensors
English
KeyeVL
text-generation
multimodal
custom_code
fp8
Instructions to use Kwai-Keye/Keye-VL-671B-A37B with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Kwai-Keye/Keye-VL-671B-A37B with Transformers:
# Load model directly from transformers import AutoModelForSeq2SeqLM model = AutoModelForSeq2SeqLM.from_pretrained("Kwai-Keye/Keye-VL-671B-A37B", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
| # coding=utf-8 | |
| # Copyright 2025 The Keye Team and The HuggingFace Inc. team. All rights reserved. | |
| # | |
| # This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX | |
| # and OPT implementations in this library. It has been modified from its | |
| # original forms to accommodate minor architectural differences compared | |
| # to GPT-NeoX and OPT used by the Meta AI team that trained the model. | |
| # | |
| # Licensed under the Apache License, Version 2.0 (the "License"); | |
| # you may not use this file except in compliance with the License. | |
| # You may obtain a copy of the License at | |
| # | |
| # http://www.apache.org/licenses/LICENSE-2.0 | |
| # | |
| # Unless required by applicable law or agreed to in writing, software | |
| # distributed under the License is distributed on an "AS IS" BASIS, | |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| # See the License for the specific language governing permissions and | |
| # limitations under the License. | |
| from transformers.configuration_utils import PretrainedConfig | |
| from transformers.modeling_rope_utils import rope_config_validation | |
| class KeyeVisionConfig(PretrainedConfig): | |
| model_type = "Keye" | |
| base_config_key = "vision_config" | |
| def __init__( | |
| self, | |
| hidden_size=768, | |
| intermediate_size=3072, | |
| num_hidden_layers=12, | |
| num_attention_heads=12, | |
| num_channels=3, | |
| image_size=224, | |
| patch_size=14, | |
| hidden_act="gelu_pytorch_tanh", | |
| layer_norm_eps=1e-6, | |
| attention_dropout=0.0, | |
| spatial_merge_size=2, | |
| temporal_patch_size=2, | |
| tokens_per_second=2, | |
| **kwargs, | |
| ): | |
| super().__init__(**kwargs) | |
| self.hidden_size = hidden_size | |
| self.intermediate_size = intermediate_size | |
| self.num_hidden_layers = num_hidden_layers | |
| self.num_attention_heads = num_attention_heads | |
| self.num_channels = num_channels | |
| self.patch_size = patch_size | |
| self.image_size = image_size | |
| self.attention_dropout = attention_dropout | |
| self.layer_norm_eps = layer_norm_eps | |
| self.hidden_act = hidden_act | |
| self.spatial_merge_size = spatial_merge_size | |
| self.temporal_patch_size = temporal_patch_size | |
| self.tokens_per_second = tokens_per_second | |
| class KeyeVLMoeConfig(PretrainedConfig): | |
| r""" | |
| This is the configuration class to store the configuration of a [`KeyeModel`]. It is used to instantiate a | |
| KeyeVLMoeForConditionalGeneration model according to the specified arguments, defining the model architecture. | |
| Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the | |
| documentation from [`PretrainedConfig`] for more information. | |
| Args: | |
| vocab_size (`int`, *optional*, defaults to 152064): | |
| Vocabulary size of the Keye model. Defines the number of different tokens that can be represented by the | |
| `inputs_ids` passed when calling [`KeyeModel`] | |
| hidden_size (`int`, *optional*, defaults to 8192): | |
| Dimension of the hidden representations. | |
| intermediate_size (`int`, *optional*, defaults to 29568): | |
| Dimension of the MLP representations. | |
| num_hidden_layers (`int`, *optional*, defaults to 80): | |
| Number of hidden layers in the Transformer encoder. | |
| num_attention_heads (`int`, *optional*, defaults to 64): | |
| Number of attention heads for each attention layer in the Transformer encoder. | |
| num_key_value_heads (`int`, *optional*, defaults to 8): | |
| This is the number of key_value heads that should be used to implement Grouped Query Attention. If | |
| `num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if | |
| `num_key_value_heads=1` the model will use Multi Query Attention (MQA) otherwise GQA is used. When | |
| converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed | |
| by meanpooling all the original heads within that group. For more details checkout [this | |
| paper](https://arxiv.org/pdf/2305.13245.pdf). If it is not specified, will default to `32`. | |
| hidden_act (`str` or `function`, *optional*, defaults to `"silu"`): | |
| The non-linear activation function (function or string) in the decoder. | |
| max_position_embeddings (`int`, *optional*, defaults to 32768): | |
| The maximum sequence length that this model might ever be used with. | |
| initializer_range (`float`, *optional*, defaults to 0.02): | |
| The standard deviation of the truncated_normal_initializer for initializing all weight matrices. | |
| rms_norm_eps (`float`, *optional*, defaults to 1e-05): | |
| The epsilon used by the rms normalization layers. | |
| use_cache (`bool`, *optional*, defaults to `True`): | |
| Whether or not the model should return the last key/values attentions (not used by all models). Only | |
| relevant if `config.is_decoder=True`. | |
| tie_word_embeddings (`bool`, *optional*, defaults to `False`): | |
| Whether the model's input and output word embeddings should be tied. | |
| rope_theta (`float`, *optional*, defaults to 1000000.0): | |
| The base period of the RoPE embeddings. | |
| use_sliding_window (`bool`, *optional*, defaults to `False`): | |
| Whether to use sliding window attention. | |
| sliding_window (`int`, *optional*, defaults to 4096): | |
| Sliding window attention (SWA) window size. If not specified, will default to `4096`. | |
| max_window_layers (`int`, *optional*, defaults to 80): | |
| The number of layers that use SWA (Sliding Window Attention). The bottom layers use SWA while the top use full attention. | |
| attention_dropout (`float`, *optional*, defaults to 0.0): | |
| The dropout ratio for the attention probabilities. | |
| vision_config (`Dict`, *optional*): | |
| The config for the visual encoder initialization. | |
| rope_scaling (`Dict`, *optional*): | |
| Dictionary containing the scaling configuration for the RoPE embeddings. NOTE: if you apply new rope type | |
| and you expect the model to work on longer `max_position_embeddings`, we recommend you to update this value | |
| accordingly. | |
| Expected contents: | |
| `rope_type` (`str`): | |
| The sub-variant of RoPE to use. Can be one of ['default', 'linear', 'dynamic', 'yarn', 'longrope', | |
| 'llama3'], with 'default' being the original RoPE implementation. | |
| `factor` (`float`, *optional*): | |
| Used with all rope types except 'default'. The scaling factor to apply to the RoPE embeddings. In | |
| most scaling types, a `factor` of x will enable the model to handle sequences of length x * | |
| original maximum pre-trained length. | |
| `original_max_position_embeddings` (`int`, *optional*): | |
| Used with 'dynamic', 'longrope' and 'llama3'. The original max position embeddings used during | |
| pretraining. | |
| `attention_factor` (`float`, *optional*): | |
| Used with 'yarn' and 'longrope'. The scaling factor to be applied on the attention | |
| computation. If unspecified, it defaults to value recommended by the implementation, using the | |
| `factor` field to infer the suggested value. | |
| `beta_fast` (`float`, *optional*): | |
| Only used with 'yarn'. Parameter to set the boundary for extrapolation (only) in the linear | |
| ramp function. If unspecified, it defaults to 32. | |
| `beta_slow` (`float`, *optional*): | |
| Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear | |
| ramp function. If unspecified, it defaults to 1. | |
| `short_factor` (`List[float]`, *optional*): | |
| Only used with 'longrope'. The scaling factor to be applied to short contexts (< | |
| `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden | |
| size divided by the number of attention heads divided by 2 | |
| `long_factor` (`List[float]`, *optional*): | |
| Only used with 'longrope'. The scaling factor to be applied to long contexts (< | |
| `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden | |
| size divided by the number of attention heads divided by 2 | |
| `low_freq_factor` (`float`, *optional*): | |
| Only used with 'llama3'. Scaling factor applied to low frequency components of the RoPE | |
| `high_freq_factor` (`float`, *optional*): | |
| Only used with 'llama3'. Scaling factor applied to high frequency components of the RoPE | |
| ```python | |
| >>> from transformers import KeyeForConditionalGeneration, KeyeConfig | |
| >>> # Initializing a Keye style configuration | |
| >>> configuration = KeyeConfig() | |
| >>> # Initializing a model from the Keye-VL-671B-A37B style configuration | |
| >>> model = KeyeForConditionalGeneration(configuration) | |
| >>> # Accessing the model configuration | |
| >>> configuration = model.config | |
| ```""" | |
| model_type = "KeyeVL" | |
| sub_configs = {"vision_config": KeyeVisionConfig} | |
| keys_to_ignore_at_inference = ["past_key_values"] | |
| def __init__( | |
| self, | |
| vocab_size=129280, | |
| hidden_size=7168, | |
| intermediate_size=18432, | |
| moe_intermediate_size = 2048, | |
| num_hidden_layers=61, | |
| num_nextn_predict_layers=1, | |
| num_attention_heads=128, | |
| num_key_value_heads=128, | |
| n_shared_experts = 1, | |
| n_routed_experts = 256, | |
| ep_size = 1, | |
| routed_scaling_factor = 2.5, | |
| kv_lora_rank = 512, | |
| q_lora_rank = 1536, | |
| qk_rope_head_dim = 64, | |
| v_head_dim = 128, | |
| qk_nope_head_dim = 128, | |
| topk_method = 'noaux_tc', | |
| n_group = 8, | |
| topk_group = 4, | |
| num_experts_per_tok = 8, | |
| moe_layer_freq = 1, | |
| first_k_dense_replace = 3, | |
| norm_topk_prob = True, | |
| scoring_func = 'sigmoid', | |
| hidden_act="silu", | |
| max_position_embeddings=4096, | |
| initializer_range=0.02, | |
| rms_norm_eps=1e-6, | |
| use_cache=True, | |
| pad_token_id=None, | |
| bos_token_id=0, | |
| eos_token_id=1, | |
| tie_word_embeddings=False, | |
| rope_theta=10000.0, | |
| rope_scaling=None, | |
| attention_bias=False, | |
| attention_dropout=0.0, | |
| vision_config=None, | |
| **kwargs, | |
| ): | |
| if isinstance(vision_config, dict): | |
| self.vision_config = self.sub_configs["vision_config"](**vision_config) | |
| elif vision_config is None: | |
| self.vision_config = self.sub_configs["vision_config"]() | |
| self.vocab_size = vocab_size | |
| self.max_position_embeddings = max_position_embeddings | |
| self.hidden_size = hidden_size | |
| self.intermediate_size = intermediate_size | |
| self.moe_intermediate_size = moe_intermediate_size | |
| self.num_hidden_layers = num_hidden_layers | |
| self.num_nextn_predict_layers = num_nextn_predict_layers | |
| self.num_attention_heads = num_attention_heads | |
| self.n_shared_experts = n_shared_experts | |
| self.n_routed_experts = n_routed_experts | |
| self.ep_size = ep_size | |
| self.routed_scaling_factor = routed_scaling_factor | |
| self.kv_lora_rank = kv_lora_rank | |
| self.q_lora_rank = q_lora_rank | |
| self.qk_rope_head_dim = qk_rope_head_dim | |
| self.v_head_dim = v_head_dim | |
| self.qk_nope_head_dim = qk_nope_head_dim | |
| self.topk_method = topk_method | |
| self.n_group = n_group | |
| self.topk_group = topk_group | |
| self.num_experts_per_tok = num_experts_per_tok | |
| self.moe_layer_freq = moe_layer_freq | |
| self.first_k_dense_replace = first_k_dense_replace | |
| self.norm_topk_prob = norm_topk_prob | |
| self.scoring_func = scoring_func | |
| # for backward compatibility | |
| if num_key_value_heads is None: | |
| num_key_value_heads = num_attention_heads | |
| self.num_key_value_heads = num_key_value_heads | |
| self.hidden_act = hidden_act | |
| self.initializer_range = initializer_range | |
| self.rms_norm_eps = rms_norm_eps | |
| self.use_cache = use_cache | |
| self.rope_theta = rope_theta | |
| self.rope_scaling = rope_scaling | |
| self.attention_bias = attention_bias | |
| self.attention_dropout = attention_dropout | |
| super().__init__( | |
| pad_token_id=pad_token_id, | |
| bos_token_id=bos_token_id, | |
| eos_token_id=eos_token_id, | |
| tie_word_embeddings=tie_word_embeddings, | |
| **kwargs, | |
| ) | |
| __all__ = ["KeyeVLMoeConfig"] | |