Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[Snippets][CPU][RV64] Make PowerStatic fallback LMUL aware (#38103)

Arseniy Obolenskiyb51d0b77431a
diff --git a/src/plugins/intel_cpu/src/emitters/plugin/riscv64/jit_eltwise_emitters.cpp b/src/plugins/intel_cpu/src/emitters/plugin/riscv64/jit_eltwise_emitters.cpp
index 330ab5cbfc8..6e1ca8db287 100644
--- a/src/plugins/intel_cpu/src/emitters/plugin/riscv64/jit_eltwise_emitters.cpp
+++ b/src/plugins/intel_cpu/src/emitters/plugin/riscv64/jit_eltwise_emitters.cpp
@@ -14,6 +14,7 @@
#include <vector>
#include "common/utils.hpp"
+#include "emitters/plugin/riscv64/jit_context_helpers.hpp"
#include "emitters/plugin/riscv64/jit_conversion_helpers.hpp"
#include "emitters/plugin/riscv64/jit_emitter.hpp"
#include "emitters/utils.hpp"
@@ -3138,14 +3139,17 @@ size_t jit_power_static_emitter::get_inputs_num() const {
}
size_t jit_power_static_emitter::aux_gprs_count() const {
- if ((power == 0) || is_scale_shift() || (!is_sqrt() && !is_int_pow())) {
+ if (power == 0 || is_scale_shift()) {
return 2;
}
+ if (!is_sqrt() && !is_int_pow()) {
+ return 3;
+ }
return 1;
}
bool jit_power_static_emitter::is_lmul_supported() const {
- return jit_emitter::is_lmul_supported() && (is_int_pow() || is_sqrt());
+ return jit_emitter::is_lmul_supported();
}
size_t jit_power_static_emitter::aux_vecs_count() const {
@@ -3235,33 +3239,94 @@ void jit_power_static_emitter::emit_isa(const std::vector<size_t>& in_vec_idxs,
Reg func_reg(aux_gpr_idxs[0]);
h->uni_li(func_reg, pow_f32_addr);
- // Before binary call we have to save caller-saver registers:
- // - all caller-saver general-purpose regs + func_reg (if it's caller-saver)
- // - all caller-saver fp general-purpose regs except aux registers
- // - all vector registers except aux, src and dst registers
- auto exclude_vec_regs = aux_vec_idxs;
- aux_vec_idxs.push_back(src.getIdx());
- aux_vec_idxs.push_back(dst.getIdx());
- call_preamble({}, aux_fp_gpr_idxs, aux_vec_idxs);
-
- const auto sp_size = rnd_up(get_vec_length(), 16);
- h->addi(sp, sp, -sp_size);
- h->vse32_v(dst, sp);
-
- // TODO: Support any LMUL here (via vl from csr + labels)
- for (size_t i = 0; i < get_vec_length(); i += sizeof(float)) {
- h->flw(fa0, sp, i);
- load_table_val("power", fa1);
-
- h->jalr(ra, func_reg);
-
- h->fsw(fa0, sp, i);
+ std::vector<size_t> exclude_vec_regs;
+ exclude_vec_regs.reserve(get_max_vecs_count());
+ for (size_t idx = 0; idx < get_max_vecs_count(); ++idx) {
+ exclude_vec_regs.push_back(idx);
+ }
+ // The active LMUL cannot be used to save individual physical registers safely.
+ call_preamble({}, aux_fp_gpr_idxs, exclude_vec_regs);
+
+ const auto vector_state_bytes = 2 * get_gpr_length();
+ const auto vector_regs_offset = vector_state_bytes + 2 * get_gpr_length();
+ const auto vector_regs_count = get_max_vecs_count();
+ const auto scalar_buffer_offset = vector_regs_offset + vector_regs_count * get_vec_length();
+ const auto scalar_buffer_size = 8 * get_vec_length();
+ const auto loop_counter_offset = scalar_buffer_offset + scalar_buffer_size;
+ const auto loop_pointer_offset = loop_counter_offset + get_gpr_length();
+ const auto frame_size = rnd_up(loop_pointer_offset + get_gpr_length(), sp_alignment);
+
+ utils::sub_sp(*h, frame_size);
+ utils::save_vector_state(*h, t0, t1, 0, get_gpr_length());
+ h->sd(func_reg, sp, static_cast<int32_t>(vector_state_bytes));
+ h->sd(p_table, sp, static_cast<int32_t>(vector_state_bytes + get_gpr_length()));
+
+ std::vector<size_t> vector_regs;
+ vector_regs.reserve(vector_regs_count);
+ for (size_t idx = 0; idx < vector_regs_count; ++idx) {
+ vector_regs.push_back(idx);
}
+ utils::save_vregs(*h, t0, t1, vector_regs_offset, vector_regs);
+ utils::restore_vector_state(*h, t0, t1, 0, get_gpr_length());
+
+ Reg vl_reg(aux_gpr_idxs[1]);
+ h->uni_li(t0, scalar_buffer_offset);
+ h->add(t0, sp, t0);
+ h->vse32_v(dst, t0);
+
+ h->uni_li(t0, loop_counter_offset);
+ h->add(t0, sp, t0);
+ h->ld(vl_reg, sp, 0);
+ h->sd(vl_reg, t0, 0);
+
+ h->uni_li(t0, loop_pointer_offset);
+ h->add(t0, sp, t0);
+ h->uni_li(t1, scalar_buffer_offset);
+ h->add(t1, sp, t1);
+ h->sd(t1, t0, 0);
+
+ Xbyak_riscv::Label pow_loop;
+ Xbyak_riscv::Label pow_done;
+ h->L(pow_loop);
+
+ h->uni_li(t0, loop_counter_offset);
+ h->add(t0, sp, t0);
+ h->ld(vl_reg, t0, 0);
+ h->blez(vl_reg, pow_done);
+
+ h->uni_li(t0, loop_pointer_offset);
+ h->add(t0, sp, t0);
+ h->ld(t1, t0, 0);
+ h->flw(fa0, t1, 0);
+ h->ld(func_reg, sp, static_cast<int32_t>(vector_state_bytes));
+ h->ld(p_table, sp, static_cast<int32_t>(vector_state_bytes + get_gpr_length()));
+ load_table_val("power", fa1);
- h->vle32_v(dst, sp);
- h->addi(sp, sp, sp_size);
+ h->jalr(ra, func_reg);
- call_postamble({}, aux_fp_gpr_idxs, aux_vec_idxs);
+ h->uni_li(t0, loop_pointer_offset);
+ h->add(t0, sp, t0);
+ h->ld(t1, t0, 0);
+ h->fsw(fa0, t1, 0);
+ h->addi(t1, t1, sizeof(float));
+ h->sd(t1, t0, 0);
+
+ h->uni_li(t0, loop_counter_offset);
+ h->add(t0, sp, t0);
+ h->ld(vl_reg, t0, 0);
+ h->addi(vl_reg, vl_reg, -1);
+ h->sd(vl_reg, t0, 0);
+ h->jal(Xbyak_riscv::zero, pow_loop);
+ h->L(pow_done);
+
+ utils::restore_vregs(*h, t0, t1, vector_regs_offset, vector_regs);
+ utils::restore_vector_state(*h, t0, t1, 0, get_gpr_length());
+ h->uni_li(t0, scalar_buffer_offset);
+ h->add(t0, sp, t0);
+ h->vle32_v(dst, t0);
+ utils::add_sp(*h, frame_size);
+
+ call_postamble({}, aux_fp_gpr_idxs, exclude_vec_regs);
}
}
diff --git a/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/instances/riscv64/eltwise.cpp b/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/instances/riscv64/eltwise.cpp
index 6cc5c31b021..b46bebcf6cf 100644
--- a/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/instances/riscv64/eltwise.cpp
+++ b/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/instances/riscv64/eltwise.cpp
@@ -3,10 +3,13 @@
//
#include "custom/single_layer_tests/classes/eltwise.hpp"
+#include "internal_properties.hpp"
#include "utils/cpu_test_utils.hpp"
#include "utils/fusing_test_utils.hpp"
#include "utils/filter_cpu_info.hpp"
#include "nodes/kernels/riscv64/cpu_isa_traits.hpp"
+#include "openvino/op/constant.hpp"
+#include "openvino/op/power.hpp"
using namespace CPUTestUtils;
@@ -65,6 +68,46 @@ const auto params_4D_jit = ::testing::Combine(
INSTANTIATE_TEST_SUITE_P(smoke_CompareWithRefs_4D_jit, EltwiseLayerCPUTest, params_4D_jit, EltwiseLayerCPUTest::getTestCaseName);
+class PowerStaticRvvLmulTest : public testing::WithParamInterface<ov::Shape>,
+ virtual public SubgraphBaseTest,
+ public CpuTestWithFusing {
+protected:
+ void SetUp() override {
+ if (!ov::intel_cpu::riscv64::mayiuse(ov::intel_cpu::riscv64::gv)) {
+ GTEST_SKIP();
+ }
+
+ targetDevice = ov::test::utils::DEVICE_CPU;
+ selectedType = makeSelectedTypeStr("jit", ov::element::f32);
+ const auto shape = ov::PartialShape{GetParam()};
+ init_input_shapes({{shape, {shape.to_shape()}}});
+
+ auto parameter = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, shape);
+ auto exponent = std::make_shared<ov::op::v0::Constant>(ov::element::f32, ov::Shape{}, 1.5F);
+ auto power = std::make_shared<ov::op::v1::Power>(parameter, exponent);
+ ov::ParameterVector parameters{parameter};
+ function = create_ov_model(ov::element::f32, parameters, power, "PowerStaticRvvLmul");
+ }
+
+ void generate_inputs(const std::vector<ov::Shape>& targetInputStaticShapes) override {
+ inputs.clear();
+ const auto& parameter = function->get_parameters().front();
+ const ov::test::utils::InputGenerateData input_data(1, 2, 1);
+ inputs.insert({parameter, ov::test::utils::create_and_fill_tensor(ov::element::f32,
+ targetInputStaticShapes.front(),
+ input_data)});
+ }
+};
+
+TEST_P(PowerStaticRvvLmulTest, CompareWithRefs) {
+ run();
+ CheckPluginRelatedResults(compiledModel, std::set<std::string>{"Eltwise", "Subgraph"});
+}
+
+INSTANTIATE_TEST_SUITE_P(smoke_CompareWithRefs_PowerStaticRvvLmul,
+ PowerStaticRvvLmulTest,
+ ::testing::Values(ov::Shape{1, 1, 1, 35}));
+
} // namespace
} // namespace Eltwise
} // namespace test