1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #include "tensorflow/lite/experimental/acceleration/compatibility/devicedb.h"
16
17 #include <map>
18 #include <string>
19 #include <vector>
20
21 #include "tensorflow/lite/experimental/acceleration/compatibility/database_generated.h"
22
23 namespace tflite {
24 namespace acceleration {
25 namespace {
26
Find(const DeviceDecisionTreeNode * root,const std::string & value)27 std::vector<const DeviceDecisionTreeEdge*> Find(
28 const DeviceDecisionTreeNode* root, const std::string& value) {
29 std::vector<const DeviceDecisionTreeEdge*> found;
30 if (root->comparison() == Comparison_EQUAL) {
31 // Exact match.
32 const DeviceDecisionTreeEdge* possible =
33 root->items()->LookupByKey(value.c_str());
34 if (possible) {
35 found.push_back(possible);
36 }
37 } else {
38 // Minimum: value should be at least item's value.
39 for (const DeviceDecisionTreeEdge* item : *(root->items())) {
40 if (value >= item->value()->str()) {
41 found.push_back(item);
42 }
43 }
44 }
45 return found;
46 }
47
UpdateVariablesFromDeviceDecisionTreeEdges(std::map<std::string,std::string> * variable_values,const DeviceDecisionTreeEdge & item)48 void UpdateVariablesFromDeviceDecisionTreeEdges(
49 std::map<std::string, std::string>* variable_values,
50 const DeviceDecisionTreeEdge& item) {
51 if (item.derived_properties()) {
52 for (const DerivedProperty* p : *(item.derived_properties())) {
53 (*variable_values)[p->variable()->str()] = p->value()->str();
54 }
55 }
56 }
57
Follow(const DeviceDecisionTreeNode * root,std::map<std::string,std::string> * variable_values)58 void Follow(const DeviceDecisionTreeNode* root,
59 std::map<std::string, std::string>* variable_values) {
60 if (!root->variable()) {
61 return;
62 }
63 auto possible_value = variable_values->find(root->variable()->str());
64 if (possible_value == variable_values->end()) {
65 return;
66 }
67 std::vector<const DeviceDecisionTreeEdge*> edges =
68 Find(root, possible_value->second);
69 for (const DeviceDecisionTreeEdge* edge : edges) {
70 UpdateVariablesFromDeviceDecisionTreeEdges(variable_values, *edge);
71 if (edge->children()) {
72 for (const DeviceDecisionTreeNode* root : *(edge->children())) {
73 Follow(root, variable_values);
74 }
75 }
76 }
77 }
78
79 } // namespace
80
UpdateVariablesFromDatabase(std::map<std::string,std::string> * variable_values,const DeviceDatabase & database)81 void UpdateVariablesFromDatabase(
82 std::map<std::string, std::string>* variable_values,
83 const DeviceDatabase& database) {
84 if (!database.root()) return;
85 for (const DeviceDecisionTreeNode* root : *(database.root())) {
86 Follow(root, variable_values);
87 }
88 }
89
90 } // namespace acceleration
91 } // namespace tflite
92