Skip to content

Conversation

@iugoood
Copy link
Contributor

@iugoood iugoood commented Nov 26, 2025

Add

1 add flex_olmo model
2 add UT

Due to excessive weight causing an out-of-memory (OOM) error on the NPU, only the first 2 layers are being tested.

Usage

from mindone.transformers import FlexOlmoForCausalLM, FlexOlmoModel
import mindspore as ms

model = FlexOlmoForCausalLM.from_pretrained("/home/ymc/FlexOlmo-7x7B-1T/",mindspore_dtype=ms.float32)
tokenizer = AutoTokenizer.from_pretrained("/home/ymc/FlexOlmo-7x7B-1T/")

prompt = "Hey, are you conscious? Can you talk to me?"
inputs = tokenizer(prompt, return_tensors="np")

# Generate
generate_ids = model.generate(ms.tensor(inputs.input_ids), max_length=30)
print(tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0])

Performance

Experiments are tested on Ascend Atlas 800T A2 machines with mindspore 2.6.0.

model mode speed
flex_olmo pynative 3.23 token/s

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @iugoood, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly expands the mindone/transformers library by incorporating the FlexOlmo model. It provides a complete implementation of the model's architecture, including its unique components like sparse Mixture-of-Experts (MoE) layers and rotary position embeddings, and integrates it seamlessly into the existing framework for configuration and usage. The changes also include a robust testing suite to ensure the model's correctness and performance.

Highlights

  • New Model Integration: Introduction of the FlexOlmo model, including its core components like FlexOlmoRMSNorm, FlexOlmoRotaryEmbedding, FlexOlmoMLP, FlexOlmoAttention, and FlexOlmoSparseMoeBlock.
  • Causal Language Model: Implementation of FlexOlmoForCausalLM for causal language modeling tasks, leveraging the new FlexOlmoModel architecture.
  • Auto-Configuration & Auto-Modeling: Integration of FlexOlmo into the mindone.transformers auto-configuration and auto-modeling utilities, allowing for easy loading and use.
  • Comprehensive Testing: Addition of a dedicated test suite (test_modeling_flex_olmo.py) to validate the FlexOlmo model's functionality and ensure consistency with PyTorch implementations, including numerical comparisons.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces the flex_olmo model, adapting it for the mindone/transformers library. The changes include adding the model implementation, registering it within the auto-modeling framework, and providing corresponding tests.

My review has identified two critical issues:

  1. The model implementation depends on FlexOlmoConfig from the transformers library, but this configuration has not been ported to mindone/transformers.
  2. There is a bug in the load_balancing_loss_func which will cause the auxiliary loss to be calculated incorrectly.

I have also included a medium-severity suggestion to improve code style by avoiding wildcard imports. Please address the critical issues before merging.

from ...modeling_rope_utils import ROPE_INIT_FUNCTIONS, dynamic_rope_update
from ...modeling_utils import ALL_ATTENTION_FUNCTIONS, PreTrainedModel
from ...processing_utils import Unpack
from ...utils import TransformersKwargs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The FlexOlmoConfig is being imported from the transformers library. To make this implementation self-contained within mindone, the configuration file for FlexOlmo should also be ported and included in this repository, preferably at mindone/transformers/models/flex_olmo/configuration_flex_olmo.py. This change is also needed in the corresponding test file (tests/transformers_tests/models/flex_olmo/test_modeling_flex_olmo.py).

Comment on lines +547 to +548
)

# Compute the average probability of routing to these experts
router_prob_per_expert = mindspore.mint.sum(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

There appears to be a bug in the calculation of overall_loss. The variable rank is assigned routing_weights.shape[1], and then used to slice tokens_per_expert as tokens_per_expert[:, rank : rank + routing_weights.shape[1]]. This slicing will result in an empty tensor, causing the auxiliary loss to be incorrectly calculated as zero.

The slicing and the rank variable should be removed. The correct calculation should be a direct multiplication, as in the original PyTorch implementation.

Suggested change
)
# Compute the average probability of routing to these experts
router_prob_per_expert = mindspore.mint.sum(
overall_loss = mindspore.mint.sum(tokens_per_expert * router_prob_per_expert.unsqueeze(0))

# 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 .modeling_flex_olmo import *
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using a wildcard import (*) can lead to namespace pollution and makes it harder to trace where classes and functions originate. It's better to import names explicitly.

Additionally, this file is missing a final newline, which is a common convention.

Suggested change
from .modeling_flex_olmo import *
from .modeling_flex_olmo import (
FlexOlmoForCausalLM,
FlexOlmoModel,
FlexOlmoPreTrainedModel,
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant