Skip to content

Commit 1eb1bb5

Browse files
committed
Re-name some methods and add a new one get_user_state()
Signed-off-by: Bogdan Pereanu <[email protected]>
1 parent 7aad2eb commit 1eb1bb5

File tree

7 files changed

+78
-53
lines changed

7 files changed

+78
-53
lines changed

src/plugins/intel_npu/src/backend/include/zero_variable_state.hpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ class ZeroVariableState final : public ov::IVariableState {
3232

3333
ov::SoPtr<ov::ITensor> get_state() const override;
3434

35+
/**
36+
* @brief Get user state to not change the state of the tensor through get_state()
37+
*/
38+
ov::SoPtr<ov::ITensor> get_user_state() const;
39+
3540
std::shared_ptr<ZeroTensor> get_zero_state() const;
3641

3742
/**
@@ -46,25 +51,25 @@ class ZeroVariableState final : public ov::IVariableState {
4651
size_t get_related_tensor_index() const;
4752

4853
/**
49-
* @brief Get acknowledgment if the tensor was updated
54+
* @brief Get acknowledgment if state was updated
5055
*/
51-
bool tensor_was_updated() const;
56+
bool state_update_pending() const;
5257

5358
/**
54-
* @brief Reset tensor updated flag
59+
* @brief Reset state updated flag
5560
*/
56-
void reset_tensor_updated_flag();
61+
void clear_state_update_pending();
5762

5863
/**
59-
* @brief Get acknowledgment if the zero tensor was updated
60-
* @details In case the memory was allocated in the same level zero context update the zero tensor
64+
* @brief Get acknowledgment if the zero state was updated
65+
* @details In case the memory was allocated in the same level zero context update the zero state
6166
*/
62-
bool zero_tensor_should_be_updated() const;
67+
bool zero_state_update_pending() const;
6368

6469
/**
65-
* @brief Reset zero tensor updated flag
70+
* @brief Reset zero state updated flag
6671
*/
67-
void reset_zero_tensor_updated_flag();
72+
void clear_zero_state_update_pending();
6873

6974
~ZeroVariableState() override = default;
7075

@@ -75,8 +80,8 @@ class ZeroVariableState final : public ov::IVariableState {
7580

7681
std::shared_ptr<ZeroTensor> _zero_state;
7782

78-
bool _tensor_updated = false;
79-
bool _zero_tensor_updated = false;
83+
bool _is_state_updated = false;
84+
bool _is_zero_state_update_needed = false;
8085

8186
const Config _config;
8287
Logger _logger;

src/plugins/intel_npu/src/backend/src/zero_infer_request.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -241,19 +241,19 @@ void ZeroInferRequest::create_pipeline() {
241241
auto zeroState = std::dynamic_pointer_cast<ZeroVariableState>(variableState._ptr);
242242
OPENVINO_ASSERT(zeroState != nullptr, "State is not compatible with NPU plugin");
243243

244-
if (zeroState->tensor_was_updated()) {
244+
if (zeroState->state_update_pending()) {
245245
_logger.debug("ZeroInferRequest::create_pipeline - user state tensor should be updated");
246246

247-
get_user_input(zeroState->get_tensor_index()) = zeroState->get_state();
248-
_userOutputTensors.at(zeroState->get_related_tensor_index()) = zeroState->get_state();
249-
zeroState->reset_tensor_updated_flag();
247+
get_user_input(zeroState->get_tensor_index()) = zeroState->get_user_state();
248+
_userOutputTensors.at(zeroState->get_related_tensor_index()) = zeroState->get_user_state();
249+
zeroState->clear_state_update_pending();
250250

251-
if (zeroState->zero_tensor_should_be_updated()) {
251+
if (zeroState->zero_state_update_pending()) {
252252
_logger.debug("ZeroInferRequest::create_pipeline - level zero state tensor should be updated");
253253

254254
get_level_zero_input(zeroState->get_tensor_index()) = zeroState->get_zero_state();
255255
_levelZeroOutputTensors.at(zeroState->get_related_tensor_index()) = zeroState->get_zero_state();
256-
zeroState->reset_zero_tensor_updated_flag();
256+
zeroState->clear_zero_state_update_pending();
257257
}
258258
}
259259
}
@@ -639,15 +639,15 @@ void ZeroInferRequest::update_states_if_memory_changed() {
639639
auto zeroState = std::dynamic_pointer_cast<ZeroVariableState>(variableState._ptr);
640640
OPENVINO_ASSERT(zeroState != nullptr, "State is not compatible with NPU plugin");
641641

642-
if (zeroState->tensor_was_updated()) {
643-
get_user_input(zeroState->get_tensor_index()) = zeroState->get_state();
644-
_userOutputTensors.at(zeroState->get_related_tensor_index()) = zeroState->get_state();
645-
zeroState->reset_tensor_updated_flag();
642+
if (zeroState->state_update_pending()) {
643+
get_user_input(zeroState->get_tensor_index()) = zeroState->get_user_state();
644+
_userOutputTensors.at(zeroState->get_related_tensor_index()) = zeroState->get_user_state();
645+
zeroState->clear_state_update_pending();
646646

647-
if (zeroState->zero_tensor_should_be_updated()) {
647+
if (zeroState->zero_state_update_pending()) {
648648
get_level_zero_input(zeroState->get_tensor_index()) = zeroState->get_zero_state();
649649
_levelZeroOutputTensors.at(zeroState->get_related_tensor_index()) = zeroState->get_zero_state();
650-
zeroState->reset_zero_tensor_updated_flag();
650+
zeroState->clear_zero_state_update_pending();
651651

652652
_pipeline->update_graph_arguments(_graphInputDescriptors.at(zeroState->get_tensor_index()).idx,
653653
get_level_zero_input(zeroState->get_tensor_index())->data(),

src/plugins/intel_npu/src/backend/src/zero_variable_state.cpp

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,22 @@ ZeroVariableState::ZeroVariableState(const std::shared_ptr<ZeroInitStructsHolder
2929
}
3030

3131
void ZeroVariableState::set_state(const ov::SoPtr<ov::ITensor>& new_state) {
32+
if (m_state._ptr == new_state._ptr) {
33+
// set_tensor called with the same tensor object; no action needed
34+
_logger.debug("ZeroVariableState::set_state - got the same state, do nothing");
35+
return;
36+
}
37+
3238
m_state = new_state;
33-
_tensor_updated = true;
39+
_is_state_updated = true;
3440

3541
if (_init_structs->getMutableCommandListExtVersion() >= ZE_MAKE_VERSION(1, 0)) {
3642
try {
3743
_logger.debug("ZeroVariableState::set_state - create zero tensor");
3844
// Try to use the user tensor directly if its underlying data is already allocated in the same Level Zero
3945
// context.
4046
_zero_state = std::make_shared<ZeroTensor>(_init_structs, m_state, _config);
41-
_zero_tensor_updated = true;
47+
_is_zero_state_update_needed = true;
4248
} catch (const ZeroTensorException&) {
4349
// Check if the current Level Zero tensor was previously shared with the user. If so, it cannot be reused;
4450
// allocate a new tensor to back up the user tensor (which cannot be imported or used directly).
@@ -49,7 +55,10 @@ void ZeroVariableState::set_state(const ov::SoPtr<ov::ITensor>& new_state) {
4955
m_state->get_element_type(),
5056
m_state->get_shape(),
5157
false);
52-
_zero_tensor_updated = true;
58+
_is_zero_state_update_needed = true;
59+
} else {
60+
_logger.debug("ZeroVariableState::set_state - reusing the level zero tensor since it is not shared "
61+
"with the user");
5362
}
5463
}
5564
// If command list updates are not supported, fallback to copying tensors every time.
@@ -65,6 +74,10 @@ ov::SoPtr<ov::ITensor> ZeroVariableState::get_state() const {
6574
return m_state;
6675
}
6776

77+
ov::SoPtr<ov::ITensor> ZeroVariableState::get_user_state() const {
78+
return m_state;
79+
}
80+
6881
std::shared_ptr<ZeroTensor> ZeroVariableState::get_zero_state() const {
6982
return _zero_state;
7083
}
@@ -84,20 +97,20 @@ size_t ZeroVariableState::get_related_tensor_index() const {
8497
return _related_tensor_index;
8598
}
8699

87-
bool ZeroVariableState::tensor_was_updated() const {
88-
return _tensor_updated;
100+
bool ZeroVariableState::state_update_pending() const {
101+
return _is_state_updated;
89102
}
90103

91-
void ZeroVariableState::reset_tensor_updated_flag() {
92-
_tensor_updated = false;
104+
void ZeroVariableState::clear_state_update_pending() {
105+
_is_state_updated = false;
93106
}
94107

95-
bool ZeroVariableState::zero_tensor_should_be_updated() const {
96-
return _zero_tensor_updated;
108+
bool ZeroVariableState::zero_state_update_pending() const {
109+
return _is_zero_state_update_needed;
97110
}
98111

99-
void ZeroVariableState::reset_zero_tensor_updated_flag() {
100-
_zero_tensor_updated = false;
112+
void ZeroVariableState::clear_zero_state_update_pending() {
113+
_is_zero_state_update_needed = false;
101114
}
102115

103116
} // namespace intel_npu

src/plugins/intel_npu/tests/functional/internal/backend/zero_tensor_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ using namespace ov::test::behavior;
1313

1414
const std::vector<ov::AnyMap> configsInferRequestRunTests = {{}};
1515

16-
INSTANTIATE_TEST_SUITE_P(smoke_BehaviorTest,
16+
INSTANTIATE_TEST_SUITE_P(compatibility_smoke_BehaviorTest,
1717
ZeroTensorTests,
1818
::testing::Combine(::testing::Values(ov::test::utils::DEVICE_NPU),
1919
::testing::ValuesIn(configsInferRequestRunTests)),

src/plugins/intel_npu/tests/functional/internal/backend/zero_tensor_tests.hpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,17 @@ TEST_P(ZeroTensorTests, CheckSetSmallerShape) {
125125
auto new_shape = Shape{1, 10, 10, 10};
126126
auto new_shape_size = ov::shape_size(new_shape);
127127
// Reallocation is not required.
128-
zero_tensor->set_shape(new_shape);
129-
EXPECT_EQ(new_shape, zero_tensor->get_shape());
130-
EXPECT_EQ(new_shape_size, zero_tensor->get_size());
131-
EXPECT_EQ(new_shape_size * sizeof(ov::element::f32), zero_tensor->get_byte_size());
132-
EXPECT_EQ(data, zero_tensor->data());
133-
ASSERT_TRUE(::intel_npu::zeroUtils::memory_was_allocated_in_the_same_l0_context(init_struct->getContext(),
134-
zero_tensor->data()));
128+
if (init_struct->getMutableCommandListExtVersion() >= ZE_MAKE_VERSION(1, 0)) {
129+
zero_tensor->set_shape(new_shape);
130+
EXPECT_EQ(new_shape, zero_tensor->get_shape());
131+
EXPECT_EQ(new_shape_size, zero_tensor->get_size());
132+
EXPECT_EQ(new_shape_size * sizeof(ov::element::f32), zero_tensor->get_byte_size());
133+
EXPECT_EQ(data, zero_tensor->data());
134+
ASSERT_TRUE(::intel_npu::zeroUtils::memory_was_allocated_in_the_same_l0_context(init_struct->getContext(),
135+
zero_tensor->data()));
136+
} else {
137+
ASSERT_THROW(zero_tensor->set_shape(new_shape), ov::Exception);
138+
}
135139
}
136140

137141
TEST_P(ZeroTensorTests, CheckSetBiggerShape) {

src/plugins/intel_npu/tests/functional/internal/backend/zero_variable_state_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ using namespace ov::test::behavior;
1313

1414
const std::vector<ov::AnyMap> configsInferRequestRunTests = {{}};
1515

16-
INSTANTIATE_TEST_SUITE_P(smoke_BehaviorTest,
16+
INSTANTIATE_TEST_SUITE_P(compatibility_smoke_BehaviorTest,
1717
ZeroVariableStateTests,
1818
::testing::Combine(::testing::Values(ov::test::utils::DEVICE_NPU),
1919
::testing::ValuesIn(configsInferRequestRunTests)),

src/plugins/intel_npu/tests/functional/internal/backend/zero_variable_state_tests.hpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ TEST_P(ZeroVariableStateTests, CreateZeroStateAndUseSetStateWithZeroTensor) {
132132
ASSERT_TRUE(::intel_npu::zeroUtils::memory_was_allocated_in_the_same_l0_context(init_struct->getContext(),
133133
zero_state->get_state()->data()));
134134

135-
ASSERT_TRUE(zero_state->tensor_was_updated());
136-
ASSERT_TRUE(zero_state->zero_tensor_should_be_updated());
135+
ASSERT_TRUE(zero_state->state_update_pending());
136+
ASSERT_TRUE(zero_state->zero_state_update_pending());
137137

138138
EXPECT_NE(zero_state->get_state()->data(), zero_tensor0->data());
139139

@@ -159,6 +159,9 @@ TEST_P(ZeroVariableStateTests, CreateZeroStateAndUseSetStateWithNormalTensor) {
159159
auto zero_tensor = std::make_shared<::intel_npu::ZeroTensor>(init_struct, npu_config, element::f32, shape, true);
160160
auto zero_state =
161161
std::make_shared<::intel_npu::ZeroVariableState>(init_struct, "state", zero_tensor, 1, 1, npu_config);
162+
ASSERT_TRUE(
163+
::intel_npu::zeroUtils::memory_was_allocated_in_the_same_l0_context(init_struct->getContext(),
164+
zero_state->get_user_state()->data()));
162165

163166
// shape size is unaligned to standard page size, expect to fail
164167
auto data = static_cast<float*>(::operator new(ov::shape_size(shape) * sizeof(ov::element::f32)));
@@ -170,8 +173,8 @@ TEST_P(ZeroVariableStateTests, CreateZeroStateAndUseSetStateWithNormalTensor) {
170173
::intel_npu::zeroUtils::memory_was_allocated_in_the_same_l0_context(init_struct->getContext(),
171174
zero_state->get_zero_state()->data()));
172175

173-
ASSERT_TRUE(zero_state->tensor_was_updated());
174-
ASSERT_FALSE(zero_state->zero_tensor_should_be_updated());
176+
ASSERT_TRUE(zero_state->state_update_pending());
177+
ASSERT_FALSE(zero_state->zero_state_update_pending());
175178

176179
EXPECT_NE(zero_state->get_state()->data(), zero_tensor->data());
177180
EXPECT_EQ(zero_state->get_zero_state()->data(), zero_tensor->data());
@@ -211,8 +214,8 @@ TEST_P(ZeroVariableStateTests, CreateZeroStateAndUseSetStateWithNormalTensorAfte
211214
::intel_npu::zeroUtils::memory_was_allocated_in_the_same_l0_context(init_struct->getContext(),
212215
zero_state->get_zero_state()->data()));
213216

214-
ASSERT_TRUE(zero_state->tensor_was_updated());
215-
ASSERT_TRUE(zero_state->zero_tensor_should_be_updated());
217+
ASSERT_TRUE(zero_state->state_update_pending());
218+
ASSERT_TRUE(zero_state->zero_state_update_pending());
216219

217220
EXPECT_NE(zero_state->get_state()->data(), zero_tensor->data());
218221
EXPECT_NE(zero_state->get_zero_state()->data(), zero_tensor->data());
@@ -253,8 +256,8 @@ TEST_P(ZeroVariableStateTests, CreateZeroStateAndUseSetStateWithHostTensor) {
253256
::intel_npu::zeroUtils::memory_was_allocated_in_the_same_l0_context(init_struct->getContext(),
254257
zero_state->get_zero_state()->data()));
255258

256-
ASSERT_TRUE(zero_state->tensor_was_updated());
257-
ASSERT_TRUE(zero_state->zero_tensor_should_be_updated());
259+
ASSERT_TRUE(zero_state->state_update_pending());
260+
ASSERT_TRUE(zero_state->zero_state_update_pending());
258261

259262
EXPECT_NE(zero_state->get_state()->data(), zero_tensor->data());
260263
EXPECT_NE(zero_state->get_zero_state()->data(), zero_tensor->data());
@@ -298,8 +301,8 @@ TEST_P(ZeroVariableStateTests, CreateZeroStateAndUseSetStateWithRemoteTensor) {
298301
::intel_npu::zeroUtils::memory_was_allocated_in_the_same_l0_context(init_struct->getContext(),
299302
zero_state->get_zero_state()->data()));
300303

301-
ASSERT_TRUE(zero_state->tensor_was_updated());
302-
ASSERT_TRUE(zero_state->zero_tensor_should_be_updated());
304+
ASSERT_TRUE(zero_state->state_update_pending());
305+
ASSERT_TRUE(zero_state->zero_state_update_pending());
303306

304307
EXPECT_NE(zero_remote_tensor->get_original_memory(), zero_tensor->data());
305308
EXPECT_NE(zero_state->get_zero_state()->data(), zero_tensor->data());

0 commit comments

Comments
 (0)