Skip to content

Commit

Permalink
prefix for config members and unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Vladimir Paramuzov <[email protected]>
  • Loading branch information
vladimir-paramuzov committed Dec 18, 2024
1 parent b3516cc commit 02a5571
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/inference/dev_api/openvino/runtime/plugin_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ class OPENVINO_RUNTIME_API PluginConfig {

template <typename T, PropertyMutability mutability>
T get_property(const ov::Property<T, mutability>& property) const {
if (is_set_by_user(property)) {
return user_properties.at(property.name()).template as<T>();
}
OPENVINO_ASSERT(m_options_map.find(property.name()) != m_options_map.end(), "Property not found: ", property.name());
return static_cast<ConfigOption<T>*>(m_options_map.at(property.name()))->value;
}
Expand Down
9 changes: 6 additions & 3 deletions src/inference/src/dev/plugin_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,12 @@ void PluginConfig::finalize(std::shared_ptr<IRemoteContext> context, const ov::R
}

void PluginConfig::apply_debug_options(std::shared_ptr<IRemoteContext> context) {
ov::AnyMap config_properties = read_config_file("config.json", context->get_device_name());
cleanup_unsupported(config_properties);
set_user_property(config_properties);
if (context) {
ov::AnyMap config_properties = read_config_file("config.json", context->get_device_name());
cleanup_unsupported(config_properties);
set_user_property(config_properties);
}

ov::AnyMap env_properties = read_env({"OV_"});
set_user_property(env_properties);
}
Expand Down
202 changes: 202 additions & 0 deletions src/inference/tests/unit/config_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include "openvino/core/any.hpp"
#include "openvino/runtime/plugin_config.hpp"

#include <gtest/gtest.h>
#include <string>

#include "common_test_utils/common_utils.hpp"

using namespace ::testing;
using namespace ov;

static constexpr Property<float, PropertyMutability::RW> unsupported_property{"UNSUPPORTED_PROPERTY"};
static constexpr Property<bool, PropertyMutability::RW> bool_property{"BOOL_PROPERTY"};
static constexpr Property<int32_t, PropertyMutability::RW> int_property{"INT_PROPERTY"};
static constexpr Property<std::string, PropertyMutability::RW> high_level_property{"HIGH_LEVEL_PROPERTY"};
static constexpr Property<std::string, PropertyMutability::RW> low_level_property{"LOW_LEVEL_PROPERTY"};


struct EmptyTestConfig : public ov::PluginConfig {
std::vector<std::string> get_supported_properties() const {
std::vector<std::string> supported_properties;
for (const auto& kv : m_options_map) {
supported_properties.push_back(kv.first);
}
return supported_properties;
}
};

struct NotEmptyTestConfig : public ov::PluginConfig {
NotEmptyTestConfig() {
m_options_map[bool_property.name()] = &m_bool_property;
m_options_map[int_property.name()] = &m_int_property;
m_options_map[high_level_property.name()] = &m_high_level_property;
m_options_map[low_level_property.name()] = &m_low_level_property;
}

NotEmptyTestConfig(const NotEmptyTestConfig& other) : NotEmptyTestConfig() {
user_properties = other.user_properties;
for (const auto& kv : other.m_options_map) {
m_options_map.at(kv.first)->set_any(kv.second->get_any());
}
}

ConfigOption<bool> m_bool_property = ConfigOption<bool>(true);
ConfigOption<int32_t> m_int_property = ConfigOption<int32_t>(-1);
ConfigOption<std::string> m_high_level_property = ConfigOption<std::string>("");
ConfigOption<std::string> m_low_level_property = ConfigOption<std::string>("");

std::vector<std::string> get_supported_properties() const {
std::vector<std::string> supported_properties;
for (const auto& kv : m_options_map) {
supported_properties.push_back(kv.first);
}
return supported_properties;
}

void finalize_impl(std::shared_ptr<IRemoteContext> context) override {
if (!is_set_by_user(low_level_property)) {
m_low_level_property.value = m_high_level_property.value;
}
}

void apply_rt_info(std::shared_ptr<IRemoteContext> context, const ov::RTMap& rt_info) override {
apply_rt_info_property(high_level_property, rt_info);
}

using ov::PluginConfig::is_set_by_user;
};

TEST(plugin_config, can_create_empty_config) {
ASSERT_NO_THROW(
EmptyTestConfig cfg;
ASSERT_EQ(cfg.get_supported_properties().size(), 0);
);
}

TEST(plugin_config, can_create_not_empty_config) {
ASSERT_NO_THROW(
NotEmptyTestConfig cfg;
ASSERT_EQ(cfg.get_supported_properties().size(), 4);
);
}

TEST(plugin_config, can_set_get_property) {
NotEmptyTestConfig cfg;
ASSERT_NO_THROW(cfg.get_property(bool_property));
ASSERT_EQ(cfg.get_property(bool_property), true);
ASSERT_NO_THROW(cfg.set_property(bool_property(false)));
ASSERT_EQ(cfg.get_property(bool_property), false);

ASSERT_NO_THROW(cfg.set_user_property(bool_property(true)));
ASSERT_EQ(cfg.get_property(bool_property), true);
}

TEST(plugin_config, throw_for_unsupported_property) {
NotEmptyTestConfig cfg;
ASSERT_ANY_THROW(cfg.get_property(unsupported_property));
ASSERT_ANY_THROW(cfg.set_property(unsupported_property(10.0f)));
ASSERT_ANY_THROW(cfg.set_user_property(unsupported_property(10.0f)));
}

TEST(plugin_config, can_direct_access_to_properties) {
NotEmptyTestConfig cfg;
ASSERT_EQ(cfg.m_bool_property.value, cfg.get_property(bool_property));
ASSERT_NO_THROW(cfg.set_property(bool_property(false)));
ASSERT_EQ(cfg.m_bool_property.value, cfg.get_property(bool_property));
ASSERT_EQ(cfg.m_bool_property.value, false);

ASSERT_NO_THROW(cfg.set_user_property(bool_property(true)));
ASSERT_EQ(cfg.m_bool_property.value, false); // user property doesn't impact member value until finalize() is called

cfg.m_bool_property.value = true;
ASSERT_EQ(cfg.get_property(bool_property), true);
}

TEST(plugin_config, finalization_updates_member) {
NotEmptyTestConfig cfg;
ASSERT_NO_THROW(cfg.set_user_property(bool_property(false)));
ASSERT_EQ(cfg.m_bool_property.value, true); // user property doesn't impact member value until finalize() is called

cfg.finalize(nullptr, {});

ASSERT_EQ(cfg.m_bool_property.value, false); // now the value has changed
}

TEST(plugin_config, get_property_before_finalization_returns_user_property_if_set) {
NotEmptyTestConfig cfg;

ASSERT_EQ(cfg.get_property(bool_property), true); // default value
ASSERT_EQ(cfg.m_bool_property.value, true); // default value

cfg.m_bool_property.value = false; // update member directly
ASSERT_EQ(cfg.get_property(bool_property), false); // OK, return the class member value as no user property was set

ASSERT_NO_THROW(cfg.set_user_property(bool_property(true)));
ASSERT_TRUE(cfg.is_set_by_user(bool_property));
ASSERT_EQ(cfg.get_property(bool_property), true); // now user property value is returned
ASSERT_EQ(cfg.m_bool_property.value, false); // but class member is not updated

cfg.finalize(nullptr, {});
ASSERT_EQ(cfg.get_property(bool_property), cfg.m_bool_property.value); // equal after finalization
ASSERT_FALSE(cfg.is_set_by_user(bool_property)); // and user property is cleared
}

TEST(plugin_config, finalization_updates_dependant_properties) {
NotEmptyTestConfig cfg;

cfg.set_user_property(high_level_property("value1"));
ASSERT_TRUE(cfg.is_set_by_user(high_level_property));
ASSERT_FALSE(cfg.is_set_by_user(low_level_property));

cfg.finalize(nullptr, {});
ASSERT_EQ(cfg.m_high_level_property.value, "value1");
ASSERT_EQ(cfg.m_low_level_property.value, "value1");
ASSERT_FALSE(cfg.is_set_by_user(high_level_property));
ASSERT_FALSE(cfg.is_set_by_user(low_level_property));
}

TEST(plugin_config, can_set_property_from_rt_info) {
NotEmptyTestConfig cfg;

RTMap rt_info = {
{high_level_property.name(), "value1"},
{int_property.name(), 10} // int_property is not applied from rt info
};

// default values
ASSERT_EQ(cfg.m_high_level_property.value, "");
ASSERT_EQ(cfg.m_low_level_property.value, "");
ASSERT_EQ(cfg.m_int_property.value, -1);

cfg.finalize(nullptr, rt_info);

ASSERT_EQ(cfg.m_high_level_property.value, "value1");
ASSERT_EQ(cfg.m_low_level_property.value, "value1"); // dependant is updated too
ASSERT_EQ(cfg.m_int_property.value, -1); // still default
}

TEST(plugin_config, can_copy_config) {
NotEmptyTestConfig cfg1;

cfg1.m_high_level_property.value = "value1";
cfg1.m_low_level_property.value = "value2";
cfg1.m_int_property.value = 1;
cfg1.set_user_property(bool_property(false));

NotEmptyTestConfig cfg2 = cfg1;
ASSERT_EQ(cfg2.m_high_level_property.value, "value1");
ASSERT_EQ(cfg2.m_low_level_property.value, "value2");
ASSERT_EQ(cfg2.m_int_property.value, 1);
ASSERT_EQ(cfg2.get_property(bool_property), false); // ensure user properties are copied too

// check that cfg1 modification doesn't impact a copy
cfg1.set_property(high_level_property("value3"));
cfg1.m_int_property.value = 3;
ASSERT_EQ(cfg2.m_high_level_property.value, "value1");
ASSERT_EQ(cfg2.m_int_property.value, 1);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct NewExecutionConfig : public ov::PluginConfig {
NewExecutionConfig& operator=(const NewExecutionConfig& other);

#define OV_CONFIG_OPTION(PropertyNamespace, PropertyVar, ...) \
ConfigOption<decltype(PropertyNamespace::PropertyVar)::value_type> PropertyVar = \
ConfigOption<decltype(PropertyNamespace::PropertyVar)::value_type> m_ ## PropertyVar = \
ConfigOption<decltype(PropertyNamespace::PropertyVar)::value_type>(GET_EXCEPT_LAST(__VA_ARGS__));

#include "options_release.inl"
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/intel_gpu/src/runtime/plugin_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace intel_gpu {

NewExecutionConfig::NewExecutionConfig() : ov::PluginConfig() {
#define OV_CONFIG_OPTION(PropertyNamespace, PropertyVar, ...) \
m_options_map[PropertyNamespace::PropertyVar.name()] = &PropertyVar;
m_options_map[PropertyNamespace::PropertyVar.name()] = & m_ ## PropertyVar;

#include "intel_gpu/runtime/options_release.inl"
#include "intel_gpu/runtime/options_debug.inl"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ TEST(config_test, basic) {
ov::intel_gpu::NewExecutionConfig cfg;
std::cerr << cfg.to_string();

std::cerr << cfg.get_property("PERFORMANCE_HINT").as<std::string>();
cfg.set_user_property(ov::hint::execution_mode(ov::hint::ExecutionMode::ACCURACY));
cfg.set_property(ov::hint::inference_precision(ov::element::f32));

std::cerr << "PROF: " << cfg.enable_profiling.value << std::endl;
std::cerr << "PROF: " << cfg.m_enable_profiling.value << std::endl;

std::cerr << cfg.to_string();

Expand Down

0 comments on commit 02a5571

Please sign in to comment.