Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix moe_normalize_expert_weights when top_k=1#87

Merged
tgale96 merged 3 commits into
databricks:mainfrom
152334H:top1-fix
Jan 10, 2024
Merged

Fix moe_normalize_expert_weights when top_k=1#87
tgale96 merged 3 commits into
databricks:mainfrom
152334H:top1-fix

Conversation

@152334H

@152334H 152334H commented Jan 8, 2024

Copy link
Copy Markdown
Contributor

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)

Comment thread megablocks/layers/router.py
@tgale96

tgale96 commented Jan 8, 2024

Copy link
Copy Markdown
Contributor

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

Comment thread megablocks/layers/router.py Outdated
@tgale96

tgale96 commented Jan 10, 2024

Copy link
Copy Markdown
Contributor

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

@tgale96

tgale96 commented Jan 10, 2024

Copy link
Copy Markdown
Contributor

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