Skip to content

[WIP] Add MLA layers into fla #395

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft

[WIP] Add MLA layers into fla #395

wants to merge 5 commits into from

Conversation

toothacher17
Copy link
Collaborator

@toothacher17 toothacher17 commented May 12, 2025

Initial impl, draft for now, needs setting up tests

Relates to: #392

Summary by CodeRabbit

  • New Features
    • Introduced a new multi-headed attention mechanism with rotary positional embeddings, LoRA adapters, and efficient Flash Attention integration.
    • Supports caching for autoregressive decoding and variable-length sequence handling.
    • Includes scaling adjustments for rotary embeddings and compatibility warnings for missing dependencies.

Copy link

coderabbitai bot commented May 12, 2025

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

A new file defines the Deepseek Multi Latent Attention (MLA) module as a PyTorch class, implementing a configurable multi-headed attention mechanism. The module supports rotary positional embeddings, LoRA low-rank adapters, caching for autoregressive decoding, and integrates Flash Attention for efficient computation. It exposes a constructor, a forward method, and a helper function.

Changes

File(s) Change Summary
fla/layers/mla.py Added new file implementing the MLA class with rotary embeddings, LoRA adapters, caching, and Flash Attention. Exposes the MLA class, its forward method, and the yarn_get_mscale utility function.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant MLA_Module
    participant FlashAttention
    participant Cache

    Caller->>MLA_Module: forward(hidden_states, attention_mask, past_key_values)
    MLA_Module->>MLA_Module: Project queries, keys, values (with LoRA)
    MLA_Module->>MLA_Module: Apply rotary embeddings
    MLA_Module->>Cache: Update or retrieve cached key/values
    MLA_Module->>FlashAttention: Compute attention (causal, variable length)
    FlashAttention-->>MLA_Module: Attention output
    MLA_Module->>MLA_Module: Project output
    MLA_Module-->>Caller: Return output, updated cache
Loading

Poem

A clever new module, with heads held high,
MLA hops in, with a gleam in its eye.
Rotary spins, LoRA adapts,
Flash Attention sprints—no time to nap!
With caches and tensors, it’s ready to run,
Deepseek’s attention—efficient and fun!
🐇✨


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@toothacher17 toothacher17 marked this pull request as draft May 12, 2025 06:22
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 4

🧹 Nitpick comments (2)
fla/layers/mla.py (2)

19-21: Remove unused utilities to unblock lint gate

pad_input and unpad_input are imported but never referenced, triggering F401 errors and breaking the pipeline.
Either delete the imports or wire the helpers in (e.g. to unpad before calling Flash-Attention).

-from fla.layers.utils import pad_input, unpad_input
+# (imports removed – re-add only when the helpers are used)
🧰 Tools
🪛 Ruff (0.8.2)

20-20: fla.layers.utils.pad_input imported but unused

Remove unused import

(F401)


20-20: fla.layers.utils.unpad_input imported but unused

Remove unused import

(F401)

🪛 GitHub Actions: lint

[error] 19-19: flake8: 'fla.layers.utils.pad_input' imported but unused (F401)


[error] 19-19: flake8: 'fla.layers.utils.unpad_input' imported but unused (F401)


[error] pre-commit hooks 'trailing-whitespace' and 'isort' modified the file to fix issues


173-176: Minor: simplify kwargs access

kwargs.get("cu_seqlens", None)kwargs.get("cu_seqlens") (Ruff SIM910).
Not functional, but fixing it keeps the linter green.

🧰 Tools
🪛 Ruff (0.8.2)

173-173: Use kwargs.get("cu_seqlens") instead of kwargs.get("cu_seqlens", None)

Replace kwargs.get("cu_seqlens", None) with kwargs.get("cu_seqlens")

(SIM910)

🪛 GitHub Actions: lint

[error] pre-commit hooks 'trailing-whitespace' and 'isort' modified the file to fix issues

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bc388e8 and 75c4121.

📒 Files selected for processing (1)
  • fla/layers/mla.py (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
fla/layers/mla.py (5)
fla/modules/layernorm.py (1)
  • RMSNorm (1059-1106)
fla/modules/rotary.py (1)
  • RotaryEmbedding (303-497)
fla/ops/utils/index.py (1)
  • prepare_lens_from_mask (41-42)
fla/layers/utils.py (2)
  • pad_input (176-197)
  • unpad_input (101-173)
fla/models/utils.py (3)
  • Cache (11-148)
  • get_seq_length (122-126)
  • update (43-120)
🪛 Ruff (0.8.2)
fla/layers/mla.py

20-20: fla.layers.utils.pad_input imported but unused

Remove unused import

(F401)


20-20: fla.layers.utils.unpad_input imported but unused

Remove unused import

(F401)


26-26: No explicit stacklevel keyword argument found

(B028)


173-173: Use kwargs.get("cu_seqlens") instead of kwargs.get("cu_seqlens", None)

Replace kwargs.get("cu_seqlens", None) with kwargs.get("cu_seqlens")

(SIM910)

🪛 GitHub Actions: lint
fla/layers/mla.py

[error] 6-6: flake8: line too long (129 > 127 characters) (E501)


[error] 19-19: flake8: 'fla.layers.utils.pad_input' imported but unused (F401)


[error] 19-19: flake8: 'fla.layers.utils.unpad_input' imported but unused (F401)


[error] 54-54: flake8: at least two spaces before inline comment (E261)


[error] 56-56: flake8: at least two spaces before inline comment (E261)


[error] 59-59: flake8: at least two spaces before inline comment (E261)


[error] 66-66: flake8: too many blank lines (2) (E303)


[error] 74-74: flake8: too many blank lines (2) (E303)


[error] 78-78: flake8: too many blank lines (2) (E303)


[error] 91-91: flake8: too many blank lines (2) (E303)


[error] 180-180: flake8: block comment should start with '# ' (E265)


[error] pre-commit hooks 'trailing-whitespace' and 'isort' modified the file to fix issues

⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: test
  • GitHub Check: test
🔇 Additional comments (1)
fla/layers/mla.py (1)

203-211: Potential None inputs to Flash-Attn

cu_seqlens can be None unless the caller provides it, yet flash_attn_varlen_func expects valid cumulative length tensors.
Consider:

  1. Falling back to flash_attn_func (padded path) when cu_seqlens is None, or
  2. Call unpad_input earlier to always supply valid cu_seqlens.

Without this, the code will crash for the common “full padded batch” path.

🧰 Tools
🪛 GitHub Actions: lint

[error] pre-commit hooks 'trailing-whitespace' and 'isort' modified the file to fix issues

@yzhangcs yzhangcs changed the title [WIP][RFC] Add MLA layers into fla [WIP] Add MLA layers into fla May 12, 2025
@yzhangcs yzhangcs linked an issue May 12, 2025 that may be closed by this pull request
@toothacher17
Copy link
Collaborator Author

I resolved the comments and fixed the lint today, but the implementation logic is wrong. I'll fix the impl and add more tests later

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.

[RFC] Add MLA layers into fla
2 participants