97e6338db9706af044ce4e0d3e56cce2754d6119
[platform/upstream/dldt.git] / inference-engine / src / vpu / graph_transformer / include / vpu / parsed_config.hpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #pragma once
6
7 #include <map>
8 #include <unordered_map>
9 #include <unordered_set>
10 #include <string>
11
12 #include <vpu/vpu_plugin_config.hpp>
13 #include <vpu/private_plugin_config.hpp>
14
15 #include <vpu/graph_transformer.hpp>
16 #include <vpu/utils/perf_report.hpp>
17 #include <vpu/utils/logger.hpp>
18 #include <vpu/utils/enums.hpp>
19
20 namespace vpu {
21
22 VPU_DECLARE_ENUM(ConfigMode,
23     DEFAULT_MODE = 0,
24     RUNTIME_MODE = 1,
25     COMPILE_MODE = 2,
26 )
27
28 struct ParsedConfig {
29     CompilationConfig compileConfig;
30
31     bool printReceiveTensorTime = false;
32     bool exclusiveAsyncRequests = false;
33     bool perfCount              = false;
34
35     LogLevel deviceLogLevel = LogLevel::None;
36     LogLevel hostLogLevel = LogLevel::None;
37
38     PerfReport perfReport = PerfReport::PerLayer;
39
40     virtual std::map<std::string, std::string> getDefaultConfig() const;
41
42     virtual ~ParsedConfig() = default;
43
44 protected:
45     explicit ParsedConfig(ConfigMode configMode = ConfigMode::DEFAULT_MODE);
46
47     void checkUnknownOptions(const std::map<std::string, std::string> &config) const;
48     virtual void checkInvalidValues(const std::map<std::string, std::string> &config) const;
49     std::unordered_set<std::string> getKnownOptions() const;
50
51     std::map<std::string, std::string> parse(const std::map<std::string, std::string> &config) {
52         checkInvalidValues(config);
53         checkUnknownOptions(config);
54         checkOptionsAccordingToMode(config);
55
56         auto defaultConfig = getDefaultConfig();
57         for (auto &&entry : config) {
58             defaultConfig[entry.first] = entry.second;
59         }
60
61         return defaultConfig;
62     }
63
64     void configure(const std::map<std::string, std::string> &config);
65     void checkSupportedValues(const std::unordered_map<std::string, std::unordered_set<std::string>> &supported,
66                               const std::map<std::string, std::string> &config) const;
67
68     virtual void checkOptionsAccordingToMode(const std::map<std::string, std::string> &config) const;
69     virtual std::unordered_set<std::string> getCompileOptions() const;
70     virtual std::unordered_set<std::string> getRuntimeOptions() const;
71
72 private:
73     ConfigMode _mode = ConfigMode::DEFAULT_MODE;
74     Logger::Ptr _log;
75 };
76
77 template<typename T, typename V>
78 inline void setOption(T &dst, const V &supported, const std::map<std::string, std::string> &config, const std::string &key) {
79     auto value = config.find(key);
80     if (value != config.end()) {
81         dst = supported.at(value->second);
82     }
83 }
84
85 inline void setOption(std::string &dst, const std::map<std::string, std::string> &config, const std::string &key) {
86     auto value = config.find(key);
87     if (value != config.end()) {
88         dst = value->second;
89     }
90 }
91
92 template<typename T, typename C>
93 inline void setOption(T &dst, const std::map<std::string, std::string> &config, const std::string &key, const C &preprocess) {
94     auto value = config.find(key);
95     if (value != config.end()) {
96         dst = preprocess(value->second);
97     }
98 }
99
100 }  // namespace vpu