Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ tmp/
*.tar.gz
models
genhtml
.github/skills/
.github/skills/
107 changes: 106 additions & 1 deletion src/modelinstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "modelinstance.hpp"

#include <algorithm>
#include <array>
#include <cstdlib>
#include <iostream>
#include <memory>
Expand Down Expand Up @@ -82,6 +83,109 @@ enum : unsigned int {
POSTPROCESS,
TIMER_END
};

enum class PluginConfigValueType {
INT64,
BOOL,
};

struct PluginConfigNormalizationRule {
const char* key;
PluginConfigValueType valueType;
};

const std::array<PluginConfigNormalizationRule, 4> TYPED_PLUGIN_CONFIG_NORMALIZATION_RULES{{
{"NUM_STREAMS", PluginConfigValueType::INT64},
{"INFERENCE_NUM_THREADS", PluginConfigValueType::INT64},
{"AUTO_BATCH_TIMEOUT", PluginConfigValueType::INT64},
{"ENABLE_CPU_PINNING", PluginConfigValueType::BOOL},
}};

std::string pluginConfigValueToString(const ov::Any& value) {
if (value.is<std::string>()) {
return value.as<std::string>();
}
if (value.is<const char*>()) {
return value.as<const char*>();
}
if (value.is<int64_t>()) {
return std::to_string(value.as<int64_t>());
}
if (value.is<int>()) {
return std::to_string(value.as<int>());
}
if (value.is<uint32_t>()) {
return std::to_string(value.as<uint32_t>());
}
if (value.is<double>()) {
return std::to_string(value.as<double>());
}
if (value.is<bool>()) {
return value.as<bool>() ? "true" : "false";
}
return "<non-stringifiable>";
}

bool normalizePluginConfigValue(ov::Any& value, const PluginConfigNormalizationRule& rule) {
if (!value.is<std::string>()) {
return false;
}

const auto& stringValue = value.as<std::string>();
switch (rule.valueType) {
case PluginConfigValueType::INT64: {
auto parsedValue = ovms::stoi64(stringValue);
if (!parsedValue.has_value()) {
return false;
}
value = parsedValue.value();
return true;
}
case PluginConfigValueType::BOOL: {
auto normalized = ovms::toLower(stringValue);
if (normalized == "true") {
value = true;
return true;
}
if (normalized == "false") {
value = false;
return true;
}
return false;
}
}
return false;
}

template <typename MapType>
void normalizeTypedPluginConfigValues(MapType& pluginConfig) {
for (const auto& rule : TYPED_PLUGIN_CONFIG_NORMALIZATION_RULES) {
auto it = pluginConfig.find(rule.key);
if (it == pluginConfig.end()) {
continue;
}
if (!normalizePluginConfigValue(it->second, rule) && it->second.template is<std::string>()) {
SPDLOG_LOGGER_DEBUG(ovms::modelmanager_logger, "Keeping plugin config key: {} as string value: {}", rule.key, it->second.template as<std::string>());
}
}

auto devicePropertiesIt = pluginConfig.find("DEVICE_PROPERTIES");
if (devicePropertiesIt == pluginConfig.end() || !devicePropertiesIt->second.template is<ov::AnyMap>()) {
return;
}

ov::AnyMap deviceProperties = devicePropertiesIt->second.template as<ov::AnyMap>();
for (auto& devicePropertiesEntry : deviceProperties) {
auto& devicePropertiesAny = devicePropertiesEntry.second;
if (!devicePropertiesAny.is<ov::AnyMap>()) {
continue;
}
ov::AnyMap nestedDeviceProperties = devicePropertiesAny.as<ov::AnyMap>();
normalizeTypedPluginConfigValues(nestedDeviceProperties);
devicePropertiesAny = nestedDeviceProperties;
}
devicePropertiesIt->second = deviceProperties;
}
} // namespace

namespace ov {
Expand Down Expand Up @@ -910,6 +1014,7 @@ void ModelInstance::loadCompiledModelPtr(const plugin_config_t& pluginConfig) {

plugin_config_t ModelInstance::prepareDefaultPluginConfig(const ModelConfig& config) {
plugin_config_t pluginConfig = config.getPluginConfig();
normalizeTypedPluginConfigValues(pluginConfig);
if ((pluginConfig.count("NUM_STREAMS") == 1) || (pluginConfig.count("PERFORMANCE_HINT") == 1)) {
return pluginConfig;
} else {
Expand Down Expand Up @@ -958,7 +1063,7 @@ Status ModelInstance::loadOVCompiledModel(const ModelConfig& config) {
for (const auto& pair : pluginConfig) {
const auto& key = pair.first;
const auto& value = pair.second;
SPDLOG_LOGGER_INFO(modelmanager_logger, "OVMS set plugin settings key: {}; value: {};", key, value.as<std::string>());
SPDLOG_LOGGER_INFO(modelmanager_logger, "OVMS set plugin settings key: {}; value: {};", key, pluginConfigValueToString(value));
}
logOVPluginConfig([this](const std::string& key) {
OV_LOGGER("ov::CompiledModel:{} get_property({})", reinterpret_cast<void*>(this->model.get()), key);
Expand Down
54 changes: 54 additions & 0 deletions src/test/modelinstance_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,60 @@ TEST(CpuThroughputStreamsNotSpecified, NotSetWhenPerfHintSpecified) {
EXPECT_EQ(pluginConfig.count("CPU_THROUGHPUT_STREAMS"), 0);
}

TEST(PluginConfigNormalization, ConvertsKnownTopLevelStringValuesToTypedValues) {
ovms::ModelConfig config;
config.setPluginConfig({{"NUM_STREAMS", "4"},
{"INFERENCE_NUM_THREADS", "2"},
{"AUTO_BATCH_TIMEOUT", "7"},
{"ENABLE_CPU_PINNING", "false"},
{"PERFORMANCE_HINT", "LATENCY"}});

ovms::plugin_config_t pluginConfig = ovms::ModelInstance::prepareDefaultPluginConfig(config);

ASSERT_EQ(pluginConfig.count("NUM_STREAMS"), 1);
ASSERT_TRUE(pluginConfig.at("NUM_STREAMS").is<int64_t>());
EXPECT_EQ(pluginConfig.at("NUM_STREAMS").as<int64_t>(), 4);
ASSERT_TRUE(pluginConfig.at("INFERENCE_NUM_THREADS").is<int64_t>());
EXPECT_EQ(pluginConfig.at("INFERENCE_NUM_THREADS").as<int64_t>(), 2);
ASSERT_TRUE(pluginConfig.at("AUTO_BATCH_TIMEOUT").is<int64_t>());
EXPECT_EQ(pluginConfig.at("AUTO_BATCH_TIMEOUT").as<int64_t>(), 7);
ASSERT_TRUE(pluginConfig.at("ENABLE_CPU_PINNING").is<bool>());
EXPECT_FALSE(pluginConfig.at("ENABLE_CPU_PINNING").as<bool>());
ASSERT_TRUE(pluginConfig.at("PERFORMANCE_HINT").is<std::string>());
EXPECT_EQ(pluginConfig.at("PERFORMANCE_HINT").as<std::string>(), "LATENCY");
}

TEST(PluginConfigNormalization, ConvertsKnownNestedDevicePropertiesStringValuesToTypedValues) {
ovms::ModelConfig config;
ov::AnyMap cpuProperties;
cpuProperties["NUM_STREAMS"] = std::string("3");
cpuProperties["INFERENCE_NUM_THREADS"] = std::string("5");
cpuProperties["ENABLE_CPU_PINNING"] = std::string("true");
cpuProperties["PERFORMANCE_HINT"] = std::string("LATENCY");

ov::AnyMap deviceProperties;
deviceProperties["CPU"] = cpuProperties;

ovms::plugin_config_t rawPluginConfig;
rawPluginConfig["DEVICE_PROPERTIES"] = deviceProperties;
config.setPluginConfig(rawPluginConfig);

ovms::plugin_config_t pluginConfig = ovms::ModelInstance::prepareDefaultPluginConfig(config);

ASSERT_TRUE(pluginConfig.at("DEVICE_PROPERTIES").is<ov::AnyMap>());
auto normalizedDeviceProperties = pluginConfig.at("DEVICE_PROPERTIES").as<ov::AnyMap>();
ASSERT_TRUE(normalizedDeviceProperties.at("CPU").is<ov::AnyMap>());
auto normalizedCpuProperties = normalizedDeviceProperties.at("CPU").as<ov::AnyMap>();
ASSERT_TRUE(normalizedCpuProperties.at("NUM_STREAMS").is<int64_t>());
EXPECT_EQ(normalizedCpuProperties.at("NUM_STREAMS").as<int64_t>(), 3);
ASSERT_TRUE(normalizedCpuProperties.at("INFERENCE_NUM_THREADS").is<int64_t>());
EXPECT_EQ(normalizedCpuProperties.at("INFERENCE_NUM_THREADS").as<int64_t>(), 5);
ASSERT_TRUE(normalizedCpuProperties.at("ENABLE_CPU_PINNING").is<bool>());
EXPECT_TRUE(normalizedCpuProperties.at("ENABLE_CPU_PINNING").as<bool>());
ASSERT_TRUE(normalizedCpuProperties.at("PERFORMANCE_HINT").is<std::string>());
EXPECT_EQ(normalizedCpuProperties.at("PERFORMANCE_HINT").as<std::string>(), "LATENCY");
}

TEST(TensorMap, TestProcessingHintFromShape) {
auto servableInputs = ovms::tensor_map_t({
{"Input_FP32_1_224_224_3_NHWC",
Expand Down
20 changes: 20 additions & 0 deletions src/test/pull_hf_model_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,27 @@ TEST_F(HfDownloadModelModule, TestInvalidProxyTimeout) {
ConstructorEnabledConfig config;
{
EnvGuard eGuard;
// prepareLibgit2Opts() in hf_pull_model_module.cpp only applies the
// GIT_OPT_SET_SERVER_CONNECT_TIMEOUT option when https_proxy is empty,
// so we always clear https_proxy here.
//
// To make the timeout actually fire we need the destination to be
// unreachable. The behavior depends on the host's network setup:
// * Host originally used a proxy (https_proxy was set in the
// environment): the host is on a proxy-only network where a
// direct connection to huggingface.co will hang and hit the
// timeout. Keep the default HF_ENDPOINT.
// * Host has no proxy configured (direct internet access): a direct
// connection to huggingface.co would succeed within the 1 s
// timeout and the assertion below would fail. Redirect the clone
// to an unroutable RFC 5737 TEST-NET-1 address so the connect
// must time out.
const char* hostHttpsProxy = std::getenv("https_proxy");
const bool hostHadProxy = (hostHttpsProxy != nullptr) && (std::string(hostHttpsProxy) != "");
eGuard.set("https_proxy", "");
if (!hostHadProxy) {
eGuard.set("HF_ENDPOINT", "https://192.0.2.1/");
}
const std::string timeoutConnectVal = "1000";
eGuard.set(ovms::HfPullModelModule::GIT_SERVER_CONNECT_TIMEOUT_ENV, timeoutConnectVal);
config.parse(arg_count, const_cast<char**>(n_argv));
Expand Down