-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
[Model] Add FlexOlmo model implementation #24923
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
vllm-bot
merged 17 commits into
vllm-project:main
from
2015aroras:shanea/flex-olmo-official
Oct 10, 2025
Merged
Changes from 8 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
a00b375
Add FlexOlmoConfig
2015aroras 1e734fa
Add FlexOlmo model implementation
2015aroras f98dd62
Update supported models docs
2015aroras d1adc72
Move weight loading logic to FlexOlmoModel
2015aroras 56b3cdf
Implement get_expert_mapping
2015aroras 8942c65
Merge branch 'main' into shanea/flex-olmo-official
2015aroras 5b3c09e
Make OlmoE suitable for inheritance by FlexOlmo
2015aroras 2ba0a2d
Make FlexOlmo inherit from OlmoE
2015aroras a4a2bbb
Merge branch 'main' into shanea/flex-olmo-official
2015aroras bdec7d6
Remove deprecated(?) sampling logic
2015aroras 6bdb8a6
Remove unneeded bias from gate
2015aroras d14a596
Update supported models
2015aroras c9705f8
Update test model in registry
2015aroras 8fef613
Merge branch 'main' into shanea/flex-olmo-official
2015aroras a32b5f2
Fix formatting
2015aroras 907a8d9
Merge branch 'main' into shanea/flex-olmo-official
2015aroras acab96d
Merge branch 'main' into shanea/flex-olmo-official
2015aroras File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -215,6 +215,7 @@ def check_available_online( | |||||
| "Fairseq2LlamaForCausalLM": _HfExamplesInfo("mgleize/fairseq2-dummy-Llama-3.2-1B"), # noqa: E501 | ||||||
| "FalconForCausalLM": _HfExamplesInfo("tiiuae/falcon-7b"), | ||||||
| "FalconH1ForCausalLM":_HfExamplesInfo("tiiuae/Falcon-H1-0.5B-Base"), | ||||||
| "FlexOlmoForCausalLM": _HfExamplesInfo("shanearora/Flex-reddit-2x7B-1T"), | ||||||
|
||||||
| "FlexOlmoForCausalLM": _HfExamplesInfo("shanearora/Flex-reddit-2x7B-1T"), | |
| "FlexOlmoForCausalLM": _HfExamplesInfo(allenai/Flex-reddit-2x7B-1T"), |
Ignore this, I misunderstood
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
c9705f8 Your suggestion is valid now that I've updated the official model.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
|
|
||
| # 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. | ||
| """Inference-only FlexOlmo model compatible with HuggingFace weights.""" | ||
| from typing import Optional | ||
|
|
||
| import torch | ||
| from torch import nn | ||
|
|
||
| from vllm.config import VllmConfig | ||
| from vllm.distributed import get_tensor_model_parallel_world_size | ||
| from vllm.logger import init_logger | ||
| from vllm.model_executor.layers.fused_moe import FusedMoE | ||
| from vllm.model_executor.layers.layernorm import RMSNorm | ||
| from vllm.model_executor.layers.linear import ReplicatedLinear | ||
| from vllm.model_executor.layers.sampler import SamplerOutput, get_sampler | ||
| from vllm.model_executor.models.olmoe import OlmoeAttention, OlmoeForCausalLM | ||
| from vllm.model_executor.sampling_metadata import SamplingMetadata | ||
| from vllm.transformers_utils.configs import FlexOlmoConfig | ||
|
|
||
| logger = init_logger(__name__) | ||
|
|
||
|
|
||
| class FlexOlmoAttention(OlmoeAttention): | ||
|
|
||
| def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""): | ||
| super().__init__(vllm_config=vllm_config, prefix=prefix) | ||
|
|
||
| hf_config = vllm_config.model_config.hf_config | ||
| assert isinstance(hf_config, FlexOlmoConfig) | ||
|
|
||
| self.k_norm = RMSNorm(self.total_num_kv_heads * self.head_dim, | ||
| eps=hf_config.rms_norm_eps) | ||
| self.q_norm = RMSNorm(self.total_num_heads * self.head_dim, | ||
| eps=hf_config.rms_norm_eps) | ||
|
|
||
|
|
||
| class FlexOlmoMoE(nn.Module): | ||
| """A tensor-parallel MoE implementation for FlexOlmo that shards each expert | ||
| across all ranks. | ||
|
|
||
| Each expert's weights are sharded across all ranks and a fused MoE | ||
| kernel is used for the forward pass, and finally we reduce the outputs | ||
| across ranks. | ||
| """ | ||
|
|
||
| def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""): | ||
| super().__init__() | ||
|
|
||
| hf_config = vllm_config.model_config.hf_config | ||
| assert isinstance(hf_config, FlexOlmoConfig) | ||
|
|
||
| tp_size = get_tensor_model_parallel_world_size() | ||
|
|
||
| # Gate always runs at half / full precision for now. | ||
| self.gate = ReplicatedLinear(hf_config.hidden_size, | ||
| hf_config.num_experts, | ||
| bias=False, | ||
| quant_config=None, | ||
| prefix=f"{prefix}.gate") | ||
|
|
||
| # Gate always runs at half / full precision for now. | ||
| self.experts = FusedMoE(num_experts=hf_config.num_experts, | ||
| top_k=hf_config.num_experts_per_tok, | ||
| hidden_size=hf_config.hidden_size, | ||
| intermediate_size=hf_config.intermediate_size, | ||
| reduce_results=True, | ||
| renormalize=False, | ||
| quant_config=None, | ||
| tp_size=tp_size, | ||
| prefix=f"{prefix}.experts") | ||
|
|
||
| self.top_k = hf_config.num_experts_per_tok | ||
|
|
||
| def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: | ||
| # NOTE: hidden_states can have either 1D or 2D shape. | ||
| orig_shape = hidden_states.shape | ||
| hidden_dim = hidden_states.shape[-1] | ||
| hidden_states = hidden_states.view(-1, hidden_dim) | ||
|
|
||
| # router_logits: (num_tokens, n_experts) | ||
| router_logits, _ = self.gate(hidden_states) | ||
| # Warning: The experts mutate the hidden state input! This messes up | ||
| # basic things like the residual stream. | ||
| final_hidden_states = self.experts( | ||
| hidden_states=hidden_states.detach().clone(), | ||
| router_logits=router_logits.float()) | ||
|
|
||
| return final_hidden_states.view(orig_shape) | ||
|
|
||
|
|
||
| class FlexOlmoDecoderLayer(nn.Module): | ||
|
|
||
| def __init__(self, *, vllm_config: VllmConfig, prefix: str = "") -> None: | ||
| super().__init__() | ||
| hf_config = vllm_config.model_config.hf_config | ||
| assert isinstance(hf_config, FlexOlmoConfig) | ||
|
|
||
| self.self_attn = FlexOlmoAttention(vllm_config=vllm_config, | ||
| prefix=f"{prefix}.self_attn") | ||
| self.post_attention_layernorm = RMSNorm(hf_config.hidden_size, | ||
| eps=hf_config.rms_norm_eps) | ||
| self.post_feedforward_layernorm = RMSNorm(hf_config.hidden_size, | ||
| eps=hf_config.rms_norm_eps) | ||
|
|
||
| self.mlp = FlexOlmoMoE(vllm_config=vllm_config, prefix=f"{prefix}.mlp") | ||
|
|
||
| def forward( | ||
| self, | ||
| positions: torch.Tensor, | ||
| hidden_states: torch.Tensor, | ||
| residual: Optional[torch.Tensor], | ||
| ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: | ||
| # Attention block. | ||
| residual = hidden_states | ||
| hidden_states = self.self_attn(positions, hidden_states) | ||
| hidden_states = self.post_attention_layernorm(hidden_states) | ||
| hidden_states = hidden_states + residual | ||
|
|
||
| # MLP block. | ||
| residual = hidden_states | ||
| hidden_states = self.mlp(hidden_states) | ||
| hidden_states = self.post_feedforward_layernorm(hidden_states) | ||
| hidden_states = residual + hidden_states | ||
| return hidden_states, None | ||
|
|
||
|
|
||
| class FlexOlmoForCausalLM(OlmoeForCausalLM): | ||
|
|
||
| fall_back_to_pt_during_load = False | ||
|
|
||
| def __init__(self, | ||
| *, | ||
| vllm_config: VllmConfig, | ||
| prefix: str = "", | ||
| layer_type: type[nn.Module] = FlexOlmoDecoderLayer): | ||
| super().__init__(vllm_config=vllm_config, | ||
| prefix=prefix, | ||
| layer_type=layer_type) | ||
| self.sampler = get_sampler() | ||
|
|
||
| def sample( | ||
| self, | ||
| logits: Optional[torch.Tensor], | ||
| sampling_metadata: SamplingMetadata, | ||
| ) -> Optional[SamplerOutput]: | ||
| next_tokens = self.sampler(logits, sampling_metadata) | ||
| return next_tokens |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.