Skip to content

Add MixtralForCausalLM in Turbomind#4623

Open
43758726 wants to merge 1 commit into
InternLM:mainfrom
43758726:add_mistralforcausallm_in_turbomind
Open

Add MixtralForCausalLM in Turbomind#4623
43758726 wants to merge 1 commit into
InternLM:mainfrom
43758726:add_mistralforcausallm_in_turbomind

Conversation

@43758726
Copy link
Copy Markdown
Collaborator

Thanks for your contribution and we appreciate it a lot. The following instructions would make your pull request more healthy and more easily receiving feedbacks. If you do not understand some items, don't worry, just make the pull request and seek help from maintainers.

Motivation

Please describe the motivation of this PR and the goal you want to achieve through this PR.

Modification

Please briefly describe what modification is made in this PR.

BC-breaking (Optional)

Does the modification introduce changes that break the backward-compatibility of the downstream repositories?
If so, please describe how it breaks the compatibility and how the downstream projects should modify their code to keep compatibility with this PR.

Use cases (Optional)

If this PR introduces a new feature, it is better to list some use cases here, and update the documentation.

Checklist

  1. Pre-commit or other linting tools are used to fix the potential lint issues.
  2. The modification is covered by complete unit tests. If not, please add more unit tests to ensure the correctness.
  3. If the modification has a dependency on downstream projects of a newer version, this PR should be tested with all supported versions of downstream projects.
  4. The documentation has been modified accordingly, like docstring or example tutorials.

Copilot AI review requested due to automatic review settings May 26, 2026 10:57
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@43758726 43758726 changed the title add_mistralforcausallm_in_turbomind Add MixtralForCausalLM in Turbomind May 26, 2026
@lvhan028 lvhan028 requested a review from Copilot May 26, 2026 14:01
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

Comment on lines +43 to +50
self._n_experts = getattr(cfg, 'num_experts', 0)

if self._n_experts > 0:
self._moe_cfg = make_moe_config(
cfg,
experts_per_token=cfg.num_experts_per_tok)
self._moe_cfg.expert_num = self._n_experts

Comment on lines +107 to +110
experts = ModuleListBuilder(ModuleListConfig(), self._ctx)
for e in range(self.cfg.num_experts):
experts[e] = self.ffn(pfx + 'experts' + e, is_expert=True)
m.experts = experts.build()
Comment on lines +121 to +124
if self._n_experts:
d.moe_ffn = self.moe(p + 'block_sparse_moe')
else:
d.feed_forward = self.ffn(p + 'block_sparse_moe')
Comment on lines +51 to +53
# ------------------------------------------------------------------
# model() — walks full hierarchy
# ------------------------------------------------------------------
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Remove such comments

def __init__(self, cfg: MixtralConfig, *, resolver):
super().__init__(cfg, resolver=resolver)

self._attn_cfg = make_attention_config(cfg)
Copy link
Copy Markdown
Collaborator

@lvhan028 lvhan028 May 26, 2026

Choose a reason for hiding this comment

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

to suit make_attention_config rather than changing make_attention_config

Comment on lines +64 to +66
lm_pfx = (pfx + 'model.embed_tokens'
if self.cfg.tie_word_embeddings
else pfx + 'lm_head')
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

mixtral has no tie_word_embeddings

self._ffn_cfg = make_ffn_config(cfg,
act_type=_act_type_id('silu'))

self._n_experts = getattr(cfg, 'num_experts', 0)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Overusing getattr(cfg, 'num_experts', 0) and similar patterns makes the code overly defensive and obscures the intent. If num_experts is a required field, just use cfg.num_experts and let the missing attribute fail explicitly. If a default value is truly appropriate, please double-check its correctness. Otherwise, reject this cargo-cult style of getattr usage.


self._n_experts = getattr(cfg, 'num_experts', 0)

if self._n_experts > 0:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We can assert self._n_experts > 0. if self._n_experts >0 can be removed

Comment on lines +121 to +124
if self._n_experts:
d.moe_ffn = self.moe(p + 'block_sparse_moe')
else:
d.feed_forward = self.ffn(p + 'block_sparse_moe')
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is there "mlp" layer in mixtral?

hidden_dim = cfg.hidden_size
head_num = cfg.num_attention_heads
head_dim = head_dim if head_dim is not None else getattr(cfg, 'head_dim', hidden_dim // head_num)
cfg_head_dim = getattr(cfg, 'head_dim', None)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

May not change this function

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants