Skip to content

Fix load balancing loss func for mixtral#28256

Merged
ArthurZucker merged 3 commits into
huggingface:mainfrom
liangxuZhang:fix_load_balancing_loss_func_for_mixtral
Jan 11, 2024
Merged

Fix load balancing loss func for mixtral#28256
ArthurZucker merged 3 commits into
huggingface:mainfrom
liangxuZhang:fix_load_balancing_loss_func_for_mixtral

Conversation

@liangxuZhang

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #28255

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

@ArthurZucker and @younesbelkada
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@kalomaze

kalomaze commented Dec 30, 2023

Copy link
Copy Markdown

How does this differ from #28115 ?

@ArthurZucker ArthurZucker left a comment

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.

Thanks a lot for deep diving. As discussed here this is welcome, we indeed had a bug in the implementation

Let's try to help with shapes and use something explicit like this:

    _, selected_experts = torch.topk(routing_weights, top_k, dim=-1) # [batch_size X sequence_length, top_k]

    expert_mask = torch.nn.functional.one_hot(selected_experts, num_experts) # [batch_size X sequence_length, top_k, num_experts]

    tokens_per_expert = torch.mean(expert_mask.float(), dim=0) # [top_k, num_experts]

    # Compute the average probability of routing to these experts
    router_prob_per_expert = torch.mean(routing_weights, dim=0) # [num_experts]

    overall_loss = torch.sum(tokens_per_expert * router_prob_per_expert.unsqueeze(0)) / top_k
    return overall_loss * num_experts

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.

this is not needed if we just remove selected_experts = selected_experts.reshape(-1)

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.

Suggested change
expert_mask = torch.max(expert_mask, dim=-2).values

should be removed

@codybum

codybum commented Jan 7, 2024

Copy link
Copy Markdown

What is the impact of this issue on Mixtral training? Will this fix conceivability improve the quality of training? Is it likely that previous Mixtral trainings are not as good as they could be?

It seems like an important issue for those working with Mixtral that has been waiting on merge approval for a while.

@theblackcat102

theblackcat102 commented Jan 8, 2024

Copy link
Copy Markdown
Contributor

I personally finds the loss to be much lower with the new implementation. But I wasn't sure if it has to do with the (num_experts**2) instead of just N. I'm pretty sure this is an error on original mixtral side. So far still waiting for the training result on new implemented balance loss to finish. Deepspeed also has an implementation of top-2 which we might be able to reference.

@ArthurZucker

Copy link
Copy Markdown
Collaborator

#28255 has information that could help, I am down to merge this for the release planned this week, just the comments that need to be adressed cc @liangxuZhang do you need help to finish this?

@liangxuZhang

Copy link
Copy Markdown
Contributor Author

#28255 has information that could help, I am down to merge this for the release planned this week, just the comments that need to be adressed cc @liangxuZhang do you need help to finish this?

@ArthurZucker LGTM. The new implementation is correct and concise, and I've made a new commit. In #28255, maybe we can have a deep discuss whether to concatenate gate logits of all layers.

@ArthurZucker

Copy link
Copy Markdown
Collaborator

Alright! Pretty sure the math shows it's equivalent to compute on individual layers then sum vs concate and compute, but let's merge this for now !

@ArthurZucker ArthurZucker left a comment

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.

Thanks! Failing test seems unrelated let's just rebase on main

@liangxuZhang liangxuZhang force-pushed the fix_load_balancing_loss_func_for_mixtral branch from cdd6509 to 0fe0244 Compare January 10, 2024 10:41
@liangxuZhang

Copy link
Copy Markdown
Contributor Author

Thanks! Failing test seems unrelated let's just rebase on main

@ArthurZucker I've just rebase on the main branch, but I'm not sure if I'm doing it right. Please tell me what else I need to do

@bratao

bratao commented Jan 10, 2024

Copy link
Copy Markdown

@liangxuZhang @ArthurZucker opinions about #28403 ? It looks complementary to this PR

@ArthurZucker

Copy link
Copy Markdown
Collaborator

Something like git pull upstream main if the remote is upstream, the exotic CI was fixed on main! I'll merge without it

@ArthurZucker ArthurZucker merged commit e768616 into huggingface:main Jan 11, 2024
@ArthurZucker

Copy link
Copy Markdown
Collaborator

Thanks a lot @liangxuZhang for this fix! 🤗

@dancingpipi

Copy link
Copy Markdown
Contributor

great!

@cryoco

cryoco commented Jan 12, 2024

Copy link
Copy Markdown

Impressive work!

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.

Incorrect implementation of auxiliary loss

8 participants