OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[CPU] Add JIT kernel for the LTX-Video RoPE executor (#37519)
Aditya Goyalf5f594dc0c9e
diff --git a/src/plugins/intel_cpu/src/nodes/kernels/x64/rope_kernel.cpp b/src/plugins/intel_cpu/src/nodes/kernels/x64/rope_kernel.cppindex 9980286e141..dcaf6e27b91 100644--- a/src/plugins/intel_cpu/src/nodes/kernels/x64/rope_kernel.cpp+++ b/src/plugins/intel_cpu/src/nodes/kernels/x64/rope_kernel.cpp@@ -31,20 +31,25 @@ void jit_rotary_kernel<isa>::generate() {uni_vpxor(vmm_src1, vmm_src1, vmm_src1);uni_vpxor(vmm_cos, vmm_cos, vmm_cos);uni_vpxor(vmm_sin, vmm_sin, vmm_sin);- if (m_jcp.interleave) {+ using Mode = jit_rotary_compile_params::Mode;+ if (m_jcp.mode != Mode::ROTATE_HALF) {// dst: 0-2 4-6 8-10 12-14 16-18 20-22 24-26 28-30 ->// lower 64bit/128 lane// 0-2 4-6 8-10 12-14// higher 64bit/128 lane// 16-18 20-22 24-26 28-30static const uint64_t mask_zmm[] = {0, 4, 1, 5, 2, 6, 3, 7};- if (isa == cpu_isa_t::avx512_core) {+ if (m_jcp.mode == Mode::INTERLEAVE && isa == cpu_isa_t::avx512_core) {mov(reg_tmp, reinterpret_cast<uintptr_t>(mask_zmm));uni_vmovups(vmm_idx, ptr[reg_tmp]);}auto half_rotary_ndims = m_jcp.rotary_ndims / 2;for (size_t i = 0; i < half_rotary_ndims / vec_size; i++) {- rotary_interleave(vec_size);+ if (m_jcp.mode == Mode::LTX_VIDEO) {+ rotary_ltx_video(vec_size);+ } else {+ rotary_interleave(vec_size);+ }}} else {auto half_rotary_ndims = m_jcp.rotary_ndims / 2;@@ -119,31 +124,6 @@ void jit_rotary_kernel<isa>::rotary_interleave(size_t step) {// }load(vmm_src0, reg_src, m_jcp.src_prc, step, false);load(vmm_src1, reg_src, m_jcp.src_prc, step, false, step * m_jcp.src_prc.size());- auto deinterlace = [&](const Vmm& src0, const Vmm& src1, const Vmm& tmp0, const Vmm& tmp1) {- if (isa == cpu_isa_t::avx2) {- // src0: 0 1 2 3 4 5 6 7- // src1: 8 9 10 11 12 13 14 15- // 0 1 2 3 8 9 10 11- vperm2i128(tmp0, src0, src1, 0x20);- // 4 5 6 7 12 13 14 15- vperm2i128(tmp1, src0, src1, 0x31);- // src0 x[i]: 0 2 4 6 8 10 12 14- vshufps(src0, tmp0, tmp1, 0x88);- // src1 x[i + 1]: 1 3 5 7 9 11 13 15- vshufps(src1, tmp0, tmp1, 0xdd);- } else {- // src0: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15- // src1: 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31- // 0 1 2 3 8 9 10 11 16 17 18 19 24 25 26 27- vshuff32x4(tmp0, src0, src1, 0x88);- // 4 5 6 7 12 13 14 15 20 21 22 23 28 29 30 31- vshuff32x4(tmp1, src0, src1, 0xdd);- // src0 x[i]: 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30- vshufps(src0, tmp0, tmp1, 0x88);- // src1 x[i + 1]: 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31- vshufps(src1, tmp0, tmp1, 0xdd);- }- };deinterlace(vmm_src0, vmm_src1, vmm_dst0, vmm_dst1);// cos[j]load(vmm_cos, reg_cos, ov::element::f32, step, false);@@ -198,6 +178,61 @@ void jit_rotary_kernel<isa>::rotary_interleave(size_t step) {}}+template <cpu_isa_t isa>+void jit_rotary_kernel<isa>::deinterlace(const Vmm& src0, const Vmm& src1, const Vmm& tmp0, const Vmm& tmp1) {+ if (isa == cpu_isa_t::avx2) {+ // src0: 0 1 2 3 4 5 6 7+ // src1: 8 9 10 11 12 13 14 15+ // 0 1 2 3 8 9 10 11+ vperm2i128(tmp0, src0, src1, 0x20);+ // 4 5 6 7 12 13 14 15+ vperm2i128(tmp1, src0, src1, 0x31);+ // src0 x[i]: 0 2 4 6 8 10 12 14+ vshufps(src0, tmp0, tmp1, 0x88);+ // src1 x[i + 1]: 1 3 5 7 9 11 13 15+ vshufps(src1, tmp0, tmp1, 0xdd);+ } else {+ // src0: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15+ // src1: 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31+ // 0 1 2 3 8 9 10 11 16 17 18 19 24 25 26 27+ vshuff32x4(tmp0, src0, src1, 0x88);+ // 4 5 6 7 12 13 14 15 20 21 22 23 28 29 30 31+ vshuff32x4(tmp1, src0, src1, 0xdd);+ // src0 x[i]: 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30+ vshufps(src0, tmp0, tmp1, 0x88);+ // src1 x[i + 1]: 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31+ vshufps(src1, tmp0, tmp1, 0xdd);+ }+}++template <cpu_isa_t isa>+void jit_rotary_kernel<isa>::rotary_ltx_video(size_t step) {+ // for (size_t r = 0; r < rotary_dims; r += 2) {+ // dst[r] = cos[r] * x[r] - sin[r] * x[r + 1];+ // dst[r + 1] = sin[r + 1] * x[r] + cos[r + 1] * x[r + 1];+ // }+ // with xs[i] = x[i ^ 1] both lines collapse to dst[i] = cos[i] * x[i] -/+ sin[i] * xs[i]+ // (sub on even lanes, add on odd), which is exactly vfmaddsub - no deinterleaving needed+ for (size_t half = 0; half < 2; half++) {+ const size_t off = half * step;+ load(vmm_src0, reg_src, m_jcp.src_prc, step, false, off * m_jcp.src_prc.size());+ load(vmm_cos, reg_cos, ov::element::f32, step, false, off * sizeof(float));+ load(vmm_sin, reg_sin, ov::element::f32, step, false, off * sizeof(float));+ // xs: swap adjacent pair elements, pairs never cross 128-bit lanes+ vpermilps(vmm_src1, vmm_src0, 0xB1);+ // sin[i] * xs[i]+ uni_vmulps(vmm_dst0, vmm_sin, vmm_src1);+ // cos[i] * x[i] -/+ sin[i] * xs[i]+ vfmaddsub231ps(vmm_dst0, vmm_cos, vmm_src0);+ store(reg_dst, vmm_dst0, m_jcp.dst_prc, step, off * m_jcp.dst_prc.size());+ }++ add(reg_src, m_jcp.src_prc.size() * step * 2);+ add(reg_dst, m_jcp.dst_prc.size() * step * 2);+ add(reg_cos, 2 * sizeof(float) * step);+ add(reg_sin, 2 * sizeof(float) * step);+}+template <cpu_isa_t isa>void jit_rotary_kernel<isa>::load(const Vmm& vmm_dst,const Xbyak::Reg64& reg_src,diff --git a/src/plugins/intel_cpu/src/nodes/kernels/x64/rope_kernel.hpp b/src/plugins/intel_cpu/src/nodes/kernels/x64/rope_kernel.hppindex a4b0bc75f5a..b17427a3fba 100644--- a/src/plugins/intel_cpu/src/nodes/kernels/x64/rope_kernel.hpp+++ b/src/plugins/intel_cpu/src/nodes/kernels/x64/rope_kernel.hpp@@ -5,6 +5,7 @@#pragma once#include <cstddef>+#include <cstdint>#include <memory>#include <unordered_map>#include <vector>@@ -26,11 +27,16 @@namespace ov::intel_cpu::kernel {struct jit_rotary_compile_params {+ enum class Mode : uint8_t {+ ROTATE_HALF,+ INTERLEAVE,+ LTX_VIDEO,+ };ov::element::Type src_prc;ov::element::Type dst_prc;size_t rotary_ndims = 0UL;size_t cos_sin_ndims = 0UL;- bool interleave = false;+ Mode mode = Mode::ROTATE_HALF;bool mix_cos_sin = false;};@@ -61,6 +67,8 @@ private:void generate() override;void rotary_half(size_t step);void rotary_interleave(size_t step);+ void rotary_ltx_video(size_t step);+ void deinterlace(const Vmm& src0, const Vmm& src1, const Vmm& tmp0, const Vmm& tmp1);void load(const Vmm& vmm_dst,const Xbyak::Reg64& reg_src,ov::element::Type src_prc,diff --git a/src/plugins/intel_cpu/src/nodes/rope.cpp b/src/plugins/intel_cpu/src/nodes/rope.cppindex d41d72f446c..c6836390daf 100644--- a/src/plugins/intel_cpu/src/nodes/rope.cpp+++ b/src/plugins/intel_cpu/src/nodes/rope.cpp@@ -110,7 +110,7 @@ struct RoPE::RoPEExecutorRotateHalf : public RoPE::Executor {jcp.src_prc = precision_of<T>::value;jcp.dst_prc = precision_of<T>::value;jcp.rotary_ndims = config.rotary_ndims;- jcp.interleave = false;+ jcp.mode = jit_rotary_compile_params::Mode::ROTATE_HALF;jcp.cos_sin_ndims = config.cos_sin_ndims;m_rotaryKernel = createJitKernel(jcp);}@@ -203,7 +203,7 @@ struct RoPE::RoPEExecutorInterleaved : public RoPE::Executor {jcp.src_prc = precision_of<T>::value;jcp.dst_prc = precision_of<T>::value;jcp.rotary_ndims = config.rotary_ndims;- jcp.interleave = true;+ jcp.mode = jit_rotary_compile_params::Mode::INTERLEAVE;jcp.mix_cos_sin = false;m_rotaryKernel = createJitKernel(jcp, true);}@@ -251,8 +251,16 @@ struct RoPE::RoPEExecutorInterleaved : public RoPE::Executor {template <typename T>struct RoPE::RoPEExecutorLtxVideo : public RoPE::Executor {const op::internal::RoPE::Config& m_config;+ std::shared_ptr<kernel::JitKernelBase> m_rotaryKernel;- explicit RoPEExecutorLtxVideo(const op::internal::RoPE::Config& config) : m_config(config) {}+ explicit RoPEExecutorLtxVideo(const op::internal::RoPE::Config& config) : m_config(config) {+ jit_rotary_compile_params jcp;+ jcp.src_prc = precision_of<T>::value;+ jcp.dst_prc = precision_of<T>::value;+ jcp.rotary_ndims = config.rotary_ndims;+ jcp.mode = jit_rotary_compile_params::Mode::LTX_VIDEO;+ m_rotaryKernel = createJitKernel(jcp, true);+ }void execute([[maybe_unused]] const dnnl::stream& strm,const std::vector<MemoryPtr>& inputs,@@ -274,11 +282,15 @@ struct RoPE::RoPEExecutorLtxVideo : public RoPE::Executor {const float* sin = &t_sin.at<float>({b, p, 0}, true);auto* dst = t_dst.ptr<T>(b, p);- for (size_t r = 0; r < rotary_dims; r += 2) {- auto real = static_cast<float>(x[r]);- auto imag = static_cast<float>(x[r + 1]);- dst[r] = static_cast<T>(cos[r] * real - sin[r] * imag);- dst[r + 1] = static_cast<T>(sin[r + 1] * real + cos[r + 1] * imag);+ if (m_rotaryKernel) {+ execJitKernel(m_rotaryKernel, x, dst, cos, sin);+ } else {+ for (size_t r = 0; r < rotary_dims; r += 2) {+ auto real = static_cast<float>(x[r]);+ auto imag = static_cast<float>(x[r + 1]);+ dst[r] = static_cast<T>(cos[r] * real - sin[r] * imag);+ dst[r + 1] = static_cast<T>(sin[r + 1] * real + cos[r + 1] * imag);+ }}});}@@ -294,7 +306,7 @@ struct RoPE::RoPEExecutorChatGLM : public RoPE::Executor {jcp.src_prc = precision_of<T>::value;jcp.dst_prc = precision_of<T>::value;jcp.rotary_ndims = config.rotary_ndims;- jcp.interleave = true;+ jcp.mode = jit_rotary_compile_params::Mode::INTERLEAVE;// if use precomputed rope cache then it's mixed// otherwise rope has separate cos/sin inputsjcp.mix_cos_sin = config.use_rope_cache;@@ -409,7 +421,7 @@ struct RoPE::RoPEExecutorQwen : public RoPE::Executor {jcp.src_prc = precision_of<T>::value;jcp.dst_prc = precision_of<T>::value;jcp.rotary_ndims = config.rotary_ndims;- jcp.interleave = false;+ jcp.mode = jit_rotary_compile_params::Mode::ROTATE_HALF;m_rotaryKernel = createJitKernel(jcp);}diff --git a/src/plugins/intel_cpu/tests/functional/shared_tests_instances/subgraph_tests/rotary_pos_emb.cpp b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/subgraph_tests/rotary_pos_emb.cppindex ea0e20f4a1e..b7d6035f3f5 100644--- a/src/plugins/intel_cpu/tests/functional/shared_tests_instances/subgraph_tests/rotary_pos_emb.cpp+++ b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/subgraph_tests/rotary_pos_emb.cpp@@ -9,16 +9,14 @@ namespace test {INSTANTIATE_TEST_SUITE_P(smoke_RoPETestLlama2StridedSlice,RoPETestLlama2StridedSlice,- ::testing::Combine(- ::testing::Values(ov::element::f32),- ::testing::Values(ov::test::utils::DEVICE_CPU)),+ ::testing::Combine(::testing::Values(ov::element::f32),+ ::testing::Values(ov::test::utils::DEVICE_CPU)),RoPETestLlama2StridedSlice::getTestCaseName);INSTANTIATE_TEST_SUITE_P(smoke_RoPETestChatGLMStridedSlice,RoPETestChatGLMStridedSlice,- ::testing::Combine(- ::testing::Values(ov::element::f32),- ::testing::Values(ov::test::utils::DEVICE_CPU)),+ ::testing::Combine(::testing::Values(ov::element::f32),+ ::testing::Values(ov::test::utils::DEVICE_CPU)),RoPETestChatGLMStridedSlice::getTestCaseName);INSTANTIATE_TEST_SUITE_P(smoke_RoPETestQwen7bStridedSlice,@@ -37,16 +35,14 @@ INSTANTIATE_TEST_SUITE_P(smoke_RoPETestGPTJStridedSlice,INSTANTIATE_TEST_SUITE_P(smoke_RoPETestLlama2Slice,RoPETestLlama2Slice,- ::testing::Combine(- ::testing::Values(ov::element::f32),- ::testing::Values(ov::test::utils::DEVICE_CPU)),+ ::testing::Combine(::testing::Values(ov::element::f32),+ ::testing::Values(ov::test::utils::DEVICE_CPU)),RoPETestLlama2Slice::getTestCaseName);INSTANTIATE_TEST_SUITE_P(smoke_RoPETestChatGLMSlice,RoPETestChatGLMSlice,- ::testing::Combine(- ::testing::Values(ov::element::f32),- ::testing::Values(ov::test::utils::DEVICE_CPU)),+ ::testing::Combine(::testing::Values(ov::element::f32),+ ::testing::Values(ov::test::utils::DEVICE_CPU)),RoPETestChatGLMSlice::getTestCaseName);INSTANTIATE_TEST_SUITE_P(smoke_RoPETestQwen7bSlice,@@ -65,18 +61,16 @@ INSTANTIATE_TEST_SUITE_P(smoke_RoPETestGPTJSlice,INSTANTIATE_TEST_SUITE_P(smoke_RoPETestChatGLM,RoPETestChatGLM2DRoPEStridedSlice,- ::testing::Combine(- ::testing::Values(ov::element::f32),- ::testing::Values(ov::test::utils::DEVICE_CPU)),+ ::testing::Combine(::testing::Values(ov::element::f32),+ ::testing::Values(ov::test::utils::DEVICE_CPU)),RoPETestChatGLM2DRoPEStridedSlice::getTestCaseName);const std::vector<std::string> vit_param = {"VariadicSplit", "Slice", "StridedSlice"};INSTANTIATE_TEST_SUITE_P(smoke_RoPETestQwenVL,RoPETestQwenVL,- ::testing::Combine(- ::testing::Values(ov::element::f32),- ::testing::Values(ov::test::utils::DEVICE_CPU),- ::testing::ValuesIn(vit_param)),+ ::testing::Combine(::testing::Values(ov::element::f32),+ ::testing::Values(ov::test::utils::DEVICE_CPU),+ ::testing::ValuesIn(vit_param)),RoPETestQwenVL::getTestCaseName);INSTANTIATE_TEST_SUITE_P(smoke_RoPETestChatGLM,@@ -88,14 +82,13 @@ INSTANTIATE_TEST_SUITE_P(smoke_RoPETestChatGLM,INSTANTIATE_TEST_SUITE_P(smoke_RoPETestGPTOSS,RoPETestGPTOSS,- ::testing::Combine(- ::testing::Values(ov::element::f32),- ::testing::Values(ov::test::utils::DEVICE_CPU)),+ ::testing::Combine(::testing::Values(ov::element::f32),+ ::testing::Values(ov::test::utils::DEVICE_CPU)),RoPETestGPTOSS::getTestCaseName);INSTANTIATE_TEST_SUITE_P(smoke_RoPETestLtxVideo,RoPETestLtxVideo,- ::testing::Combine(::testing::Values(ov::element::f32),+ ::testing::Combine(::testing::Values(ov::element::f32, ov::element::bf16, ov::element::f16),::testing::Values(ov::test::utils::DEVICE_CPU)),RoPETestLtxVideo::getTestCaseName);