Skip to content
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

Fix moe_normalize_expert_weights when top_k=1 #87

Merged
merged 3 commits into from
Jan 10, 2024
Merged

Fix moe_normalize_expert_weights when top_k=1 #87

merged 3 commits into from
Jan 10, 2024

Conversation

152334H
Copy link
Contributor

@152334H 152334H commented Jan 8, 2024

The router.py function,

    def _top_k(self, scores):
        if self.args.moe_top_k == 1:
            return scores.max(dim=-1) # <-- causes weight shape to become [S]
        return torch.topk(scores, self.args.moe_top_k, dim=-1) # <-- shape is normally [S,K]

caused expert weight norm to be calculated wrong:

        expert_weights, expert_indices = self._top_k(scores)
        if self.args.moe_normalize_expert_weights:
            # this function expects dim=-1 to only contain a single token's weights
            expert_weights = expert_weights / torch.norm(
                expert_weights, p=self.args.moe_normalize_expert_weights,dim=-1, keepdim=True)

After this PR, top-1 models with moe_normalize_expert_weights=1 should always have the final weights become 1 (where previously they would be divided weirdly)

@tgale96
Copy link
Contributor

tgale96 commented Jan 8, 2024

Thanks for the PR! And great catch on this bug!

@tgale96
Copy link
Contributor

tgale96 commented Jan 10, 2024

Thanks for the update! One last small comment and then I think we're ok to merge!

@tgale96
Copy link
Contributor

tgale96 commented Jan 10, 2024

Thanks for the contribution!

@tgale96 tgale96 merged commit 04e4f1f into databricks:main Jan 10, 2024
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.

2 participants