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

[CPU]support glm4 rope #27094

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 58 additions & 25 deletions src/plugins/intel_cpu/src/nodes/rope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,34 +244,67 @@ struct RoPE::RoPEExecutorChatGLM : public RoPE::Executor {
if (m_config.slice_stop - m_config.slice_start > 0) {
t_src = t_src.slice(2, m_config.slice_start, m_config.slice_stop);
}
auto seq_len = t_src.size(0);
auto batch_size = t_src.size(1);

auto head_cnt = m_config.head_cnt;
auto head_size = m_config.head_size;

auto rotary_dims = m_config.rotary_ndims;

parallel_for3d(seq_len, batch_size, head_cnt, [&](size_t p, size_t b, size_t h) {
auto* src = t_src.ptr<T>(p, b, h * head_size);
// [length, batch_size, ndims//2, 2]
auto* cos_sin = &t_cos_sin.at<float>({p, b, 0, 0}, true);
auto* dst = t_dst.ptr<T>(p, b, h, 0);
if (m_config.support_2d_rope) {
// src [batch, length, H x S]
auto seq_len = t_src.size(1);
auto batch_size = t_src.size(0);

auto head_cnt = m_config.head_cnt;
auto head_size = m_config.head_size;

auto rotary_dims = m_config.rotary_ndims;

parallel_for3d(batch_size, head_cnt, seq_len, [&](size_t b, size_t h, size_t p) {
// src [batch, length, H x S]
auto* src = t_src.ptr<T>(b, p, h * head_size);
// [batch_size, length, ndims//2, 2]
auto* cos_sin = &t_cos_sin.at<float>({b, p, 0, 0}, true);
auto* dst = t_dst.ptr<T>(b, h, p, 0);

if (m_rotaryKernel) {
execJitKernel(m_rotaryKernel, src, dst, cos_sin, nullptr);
} else {
size_t i = 0;
for (; i < rotary_dims; i += 2) {
auto cosv = cos_sin[i];
auto sinv = cos_sin[i + 1];
dst[i] = cosv * src[i] - sinv * src[i + 1];
dst[i + 1] = sinv * src[i] + cosv * src[i + 1];
}
}

if (m_rotaryKernel) {
execJitKernel(m_rotaryKernel, src, dst, cos_sin, nullptr);
} else {
size_t i = 0;
for (; i < rotary_dims; i += 2) {
auto cosv = cos_sin[i];
auto sinv = cos_sin[i + 1];
dst[i] = cosv * src[i] - sinv * src[i + 1];
dst[i + 1] = sinv * src[i] + cosv * src[i + 1];
memcpy(dst + rotary_dims, src + rotary_dims, (head_size - rotary_dims) * sizeof(T));
});
} else {
auto seq_len = t_src.size(0);
auto batch_size = t_src.size(1);

auto head_cnt = m_config.head_cnt;
auto head_size = m_config.head_size;

auto rotary_dims = m_config.rotary_ndims;

parallel_for3d(seq_len, batch_size, head_cnt, [&](size_t p, size_t b, size_t h) {
auto* src = t_src.ptr<T>(p, b, h * head_size);
// [length, batch_size, ndims//2, 2]
auto* cos_sin = &t_cos_sin.at<float>({p, b, 0, 0}, true);
auto* dst = t_dst.ptr<T>(p, b, h, 0);

if (m_rotaryKernel) {
execJitKernel(m_rotaryKernel, src, dst, cos_sin, nullptr);
} else {
size_t i = 0;
for (; i < rotary_dims; i += 2) {
auto cosv = cos_sin[i];
auto sinv = cos_sin[i + 1];
dst[i] = cosv * src[i] - sinv * src[i + 1];
dst[i + 1] = sinv * src[i] + cosv * src[i + 1];
}
}
}

memcpy(dst + rotary_dims, src + rotary_dims, (head_size - rotary_dims) * sizeof(T));
});
memcpy(dst + rotary_dims, src + rotary_dims, (head_size - rotary_dims) * sizeof(T));
});
}
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ void Transformations::PostLpt() {
// Execute before snippets. Otherwise FQ will be converted to Subgraph
CPU_REGISTER_PASS_X64(postLPTPassManager, ConvertFqRnnToQuantizedRnn);

CPU_REGISTER_PASS_X64(postLPTPassManager, ov::pass::RoPEFusion);
CPU_REGISTER_PASS_X64(postLPTPassManager, ov::pass::RoPEFusion, true);
CPU_REGISTER_PASS_ARM64(postLPTPassManager, ov::pass::RoPEFusion);
CPU_REGISTER_PASS_X64(postLPTPassManager, CausalMaskPreprocessFusion);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,11 @@ INSTANTIATE_TEST_SUITE_P(smoke_RoPETestGPTJSlice,
::testing::Combine(::testing::Values(true, false),
::testing::Values(ov::test::utils::DEVICE_CPU)),
RoPETestGPTJSlice::getTestCaseName);

INSTANTIATE_TEST_SUITE_P(smoke_RoPETestChatGLM,
RoPETestChatGLM2DRoPEStridedSlice,
::testing::Values(ov::test::utils::DEVICE_CPU),
RoPETestChatGLM2DRoPEStridedSlice::getTestCaseName);

} // namespace test
} // namespace ov
Loading