Skip to content

CANN: optimize the rope ops #15335

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

Merged
merged 4 commits into from
Aug 19, 2025
Merged

CANN: optimize the rope ops #15335

merged 4 commits into from
Aug 19, 2025

Conversation

YangShuai52
Copy link
Contributor

@YangShuai52 YangShuai52 commented Aug 15, 2025

Optimize the performance of the rope operator by reusing sin_tensor and cos_tensor across different layers for each token.
Before Optimization
root@worker-33-138:/home/y00939322/rope_test/llama.cpp-master# ./build/bin/llama-bench -m /home/y00939322/qwen2.5-0.5b-instruct-fp16.gguf -p 5 -n 5 -b 1 -sm none -mg 0 -t 8 -fa 1

model size params backend ngl threads n_batch sm fa test t/s
qwen2 1B F16 1.17 GiB 630.17 M CANN 99 8 1 none 1 pp5 174.29 ± 0.36
qwen2 1B F16 1.17 GiB 630.17 M CANN 99 8 1 none 1 tg5 173.14 ± 0.53

Optimized
root@worker-33-138:/home/y00939322/rope_test/llama.cpp-rope_ops# ./build/bin/llama-bench -m /home/y00939322/qwen2.5-0.5b-instruct-fp16.gguf -p 5 -n 5 -b 1 -sm none -mg 0 -t 8 -fa 1

model size params backend ngl threads n_batch sm fa test t/s
qwen2 1B F16 1.17 GiB 630.17 M CANN 99 8 1 none 1 pp5 195.08 ± 0.83
qwen2 1B F16 1.17 GiB 630.17 M CANN 99 8 1 none 1 tg5 195.94 ± 0.71

Verifying the Operator Precision:
ROPE(type=f32,ne_a=[128,32,2,1],n_dims=128,mode=0,n_ctx=512,fs=1.000000,ef=0.000000,af=1.000000,ff=0,v=0): OK
ROPE(type=f32,ne_a=[128,40,2,1],n_dims=128,mode=0,n_ctx=512,fs=1.000000,ef=0.000000,af=1.000000,ff=0,v=0): OK
ROPE(type=f32,ne_a=[128,52,2,1],n_dims=128,mode=0,n_ctx=512,fs=1.000000,ef=0.000000,af=1.000000,ff=0,v=0): OK
ROPE(type=f32,ne_a=[128,64,2,1],n_dims=128,mode=0,n_ctx=512,fs=1.000000,ef=0.000000,af=1.000000,ff=0,v=0): OK
ROPE(type=f32,ne_a=[64,1,2,1],n_dims=64,mode=2,n_ctx=512,fs=1.000000,ef=0.000000,af=1.000000,ff=0,v=0): OK
ROPE(type=f32,ne_a=[64,71,2,1],n_dims=64,mode=2,n_ctx=512,fs=1.000000,ef=0.000000,af=1.000000,ff=0,v=0): OK
ROPE(type=f32,ne_a=[64,8,2,1],n_dims=64,mode=2,n_ctx=512,fs=1.000000,ef=0.000000,af=1.000000,ff=0,v=0): OK

@github-actions github-actions bot added ggml changes relating to the ggml tensor library for machine learning Ascend NPU issues specific to Ascend NPUs labels Aug 15, 2025
Copy link
Contributor

@noemotiovon noemotiovon left a comment

Choose a reason for hiding this comment

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

Thank you for your contribution! Here are some suggestions, and I’m happy to discuss them together.


if(ctx.init_ptr == nullptr || !is_attention) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a comment indicating that is_attention is a flag used for accuracy testing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for your suggestion; it has been revised.

if(ctx.init_ptr != nullptr){
ACL_CHECK(aclrtFree(ctx.init_ptr));
}
ACL_CHECK(aclrtMalloc(&ctx.init_ptr,theta_scale_length * sizeof(float_t), ACL_MEM_MALLOC_HUGE_FIRST));
Copy link
Contributor

Choose a reason for hiding this comment

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

There is a missing space after &ctx.init_ptr,.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for your suggestion; it has been revised.

void* init_ptr = nullptr;
void* sin_ptr = nullptr;
void* cos_ptr = nullptr;
int64_t max_position_length = 200000;
Copy link
Contributor

Choose a reason for hiding this comment

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

A maximum prompt length of 200,000 is a bit excessive; let's initialize it to 65,536 here. And rename it to max_prompt_length.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for your suggestion; it has been revised.

@Green-Sky Green-Sky changed the title optimize the rope ops CANN: optimize the rope ops Aug 15, 2025
@YangShuai52
Copy link
Contributor Author

@hipudding

@hipudding hipudding self-requested a review August 19, 2025 13:26
@hipudding hipudding merged commit a6d3cfe into ggml-org:master Aug 19, 2025
48 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ascend NPU issues specific to Ascend NPUs ggml changes relating to the ggml tensor library for machine learning
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants