Skip to content

Laser Attention from Mind-IE support#379

Draft
OrangeRedeng wants to merge 45 commits intosgl-project:mainfrom
OrangeRedeng:laser_attention
Draft

Laser Attention from Mind-IE support#379
OrangeRedeng wants to merge 45 commits intosgl-project:mainfrom
OrangeRedeng:laser_attention

Conversation

@OrangeRedeng
Copy link

@OrangeRedeng OrangeRedeng commented Feb 18, 2026

Moving Mind-IE Laser attention kernels into sgl-kernel-npu repo

image

@OrangeRedeng OrangeRedeng marked this pull request as draft February 18, 2026 09:49
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @OrangeRedeng, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request integrates a novel 'Laser Attention' mechanism into the Mind-IE framework, specifically optimized for the Ascend AI processor. It introduces a preprocessing operator to handle tensor reshaping and padding, and a main attention operator for the core computation. The implementation includes comprehensive host-side definitions for shape and data type inference, along with dynamic tiling logic and optimized device-side kernels to maximize performance on Ascend hardware.

Highlights

  • New Operator Introduction: Two new operators, 'LaPreprocess' and 'AscendLaserAttention', have been added to support the Laser Attention mechanism within the Mind-IE framework.
  • Ascend Platform Optimization: Both operators are specifically designed and optimized for the Ascend AI processor, leveraging its tiling and kernel execution capabilities for efficient performance.
  • Dynamic Tiling Logic: The operators incorporate dynamic tiling functions for shape inference, data type inference, and tiling parameter calculation, ensuring adaptive resource utilization on Ascend hardware.
  • Kernel Implementations: Device-side kernel implementations are provided for the core computation and data manipulation, including sequence padding, data type conversions, and attention calculation.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • csrc/laser_attention/op_host/ascend_la_preprocess.cpp
    • Added the 'LaPreprocess' operator definition, including 'InferShape', 'InferDataType', and 'LaPreprocessTilingFunc' for preprocessing query, key, and value tensors.
  • csrc/laser_attention/op_host/ascend_laser_attention.cpp
    • Added the 'AscendLaserAttention' operator definition, including 'InferShape', 'InferDtype', and 'AscendLaserAttentionTilingFunc' for the main attention computation.
  • csrc/laser_attention/op_host/ascend_laser_attention_tiling.h
    • Added the 'AscendLaserAttentionTilingData' structure to define tiling parameters for the 'AscendLaserAttention' operator.
  • csrc/laser_attention/op_host/la_preprocess_tiling.h
    • Added the 'LaPreprocessTiling' structure to define tiling parameters for the 'LaPreprocess' operator.
  • csrc/laser_attention/op_kernel/ascend_laser_attention.cpp
    • Added the kernel implementation for the 'ascend_laser_attention' operator, utilizing 'CubeForward' or 'VectorForward' for computation.
  • csrc/laser_attention/op_kernel/la_preprocess.cpp
    • Added the kernel implementation for the 'la_preprocess' operator, handling data copying and padding based on data types.
  • csrc/laser_attention/op_kernel/la_preprocess.h
    • Added the templated 'LaPreprocess' kernel class, providing detailed logic for data movement, padding, and task distribution across AICores.
Activity
  • No human activity has been recorded on this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces new operators for Laser Attention preprocessing and the Laser Attention mechanism itself. The changes include defining operator shapes, data types, tiling functions, and kernel implementations. Several areas for improvement have been identified, primarily concerning maintainability, correctness, and potential runtime issues. These include inconsistent attribute access, use of magic numbers, redundant comments, and critical type handling issues in the kernel code.

Comment on lines +145 to +146
AscendC::Cast<float, T>(
castLocal.template ReinterpretCast<float>(), castLocal[bufLen_],
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

Reinterpreting LocalTensor<T> as LocalTensor<float> using ReinterpretCast<float>() when T is bfloat16_t (2 bytes) is incorrect. This is a size mismatch and will lead to undefined behavior or memory corruption. Explicit casting from bfloat16_t to float should be used, not a reinterpret cast.

AscendC::DataCopyExtParams inCopyParams{
1, seqLen * headNum_ * HEAD_DIM * static_cast<uint32_t>(sizeof(T)), 0, 0, 0};
AscendC::DataCopyPadExtParams<T> inPadParams{false, 0, 0, 0};
AscendC::DataCopyPad(srcLocal[bufLen_], src, inCopyParams, inPadParams);
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

The expression srcLocal[bufLen_] appears to be an out-of-bounds access if bufLen_ represents the size of the buffer. Typically, srcLocal itself should be passed as the destination for DataCopyPad when copying into the local tensor. This is a critical issue that could lead to memory corruption or crashes.

            AscendC::DataCopyPad(srcLocal, src, inCopyParams, inPadParams);

Comment on lines +349 to +350
.DataType({ge::DT_FLOAT, ge::DT_FLOAT})
.Format({ge::FORMAT_ND, ge::FORMAT_ND});
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The output data type for softmax_log_max_sum is specified as DT_FLOAT, but InferDtype sets it to queryDtype, which can be DT_FLOAT16 or DT_BF16. This is a mismatch and could lead to unexpected behavior or errors. Ensure consistency between the operator definition and the InferDtype function.

for (uint32_t i = 0; i < seqLen; ++i) {
AscendC::DataCopyExtParams outCopyParams{
static_cast<uint16_t>(headNum_), HEAD_DIM * sizeof(T), 0, dstStride, 0};
AscendC::DataCopyPad(dst[i * HEAD_DIM], dstLocal[i * headNum_ * HEAD_DIM], outCopyParams);
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The outCopyParams uses HEAD_DIM * sizeof(T) for the copy size, but the destination dst is GlobalTensor<DST>. This is a type mismatch. It should use HEAD_DIM * sizeof(DST) to correctly specify the size of data being copied to the destination tensor.

                AscendC::DataCopyExtParams outCopyParams{
                    static_cast<uint16_t>(headNum_), HEAD_DIM * sizeof(DST), 0, dstStride, 0};


for (uint32_t i = 0; i < seqLen; ++i) {
AscendC::DataCopyExtParams outCopyParams{
static_cast<uint16_t>(headNum_), HEAD_DIM * sizeof(T), 0, dstStride, 0};
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The dstStride calculation uses sizeof(T) but dst is a GlobalTensor<DST>. This is a type mismatch. It should use sizeof(DST) to correctly calculate the stride for the destination tensor.

            uint32_t dstStride = (curSeqAlignLen_ - 1) * HEAD_DIM * sizeof(DST);

outKShape->SetDim(1, kShape->GetDim(INPUT_HEAD_NUM_DIM));
int32_t kPadDim = (kShape->GetDim(1) + alignLen - 1) / alignLen * alignLen;
outKShape->SetDim(SEQ_LEN_DIM, kPadDim);
outKShape->SetDim(HEAD_DIM_DIM, kShape->GetDim(INPUT_HEAD_DIM_DIM));
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Comments should be in English for consistency across the codebase. Please translate or remove this Chinese comment.

outQShape->SetDim(HEAD_DIM_DIM, qShape->GetDim(INPUT_HEAD_DIM_DIM)); // head_dim

outKShape->SetDimNum(kShape->GetDimNum());
outKShape->SetDim(0, kShape->GetDim(0));
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Comments should be in English for consistency across the codebase. Please translate or remove this Chinese comment.

outQShape->SetDim(1, qShape->GetDim(2)); // head_num (从第2维移到第1维)
int32_t qPadDim = (qShape->GetDim(1) + alignLen - 1) / alignLen * alignLen; // padded seq_len
outQShape->SetDim(SEQ_LEN_DIM, qPadDim);
outQShape->SetDim(HEAD_DIM_DIM, qShape->GetDim(INPUT_HEAD_DIM_DIM)); // head_dim
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Comments should be in English for consistency across the codebase. Please translate or remove this Chinese comment.

    outQShape->SetDim(HEAD_DIM_DIM, qShape->GetDim(INPUT_HEAD_DIM_DIM));

outQShape->SetDimNum(qShape->GetDimNum());
outQShape->SetDim(0, qShape->GetDim(0)); // batch
outQShape->SetDim(1, qShape->GetDim(2)); // head_num (从第2维移到第1维)
int32_t qPadDim = (qShape->GetDim(1) + alignLen - 1) / alignLen * alignLen; // padded seq_len
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Comments should be in English for consistency across the codebase. Please translate or remove this Chinese comment.

    int32_t qPadDim = (qShape->GetDim(1) + alignLen - 1) / alignLen * alignLen;

// 输出形状: [batch, head_num, padded_seq_len, head_dim]
outQShape->SetDimNum(qShape->GetDimNum());
outQShape->SetDim(0, qShape->GetDim(0)); // batch
outQShape->SetDim(1, qShape->GetDim(2)); // head_num (从第2维移到第1维)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Comments should be in English for consistency across the codebase. Please translate or remove this Chinese comment.

    outQShape->SetDim(1, qShape->GetDim(2));

@gemini-code-assist
Copy link
Contributor

Warning

Gemini encountered an error creating the review. You can try again by commenting /gemini review.

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.

1 participant