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 16 #include "tensorflow/lite/tools/tool_params.h" 17 18 #include <string> 19 #include <unordered_map> 20 #include <vector> 21 22 #include "tensorflow/lite/tools/logging.h" 23 24 namespace tflite { 25 namespace tools { 26 AssertHasSameType(ToolParam::ParamType a,ToolParam::ParamType b)27void ToolParam::AssertHasSameType(ToolParam::ParamType a, 28 ToolParam::ParamType b) { 29 TFLITE_TOOLS_CHECK(a == b) << "Type mismatch while accessing parameter."; 30 } 31 32 template <> GetValueType()33ToolParam::ParamType ToolParam::GetValueType<int32_t>() { 34 return ToolParam::ParamType::TYPE_INT32; 35 } 36 37 template <> GetValueType()38ToolParam::ParamType ToolParam::GetValueType<bool>() { 39 return ToolParam::ParamType::TYPE_BOOL; 40 } 41 42 template <> GetValueType()43ToolParam::ParamType ToolParam::GetValueType<float>() { 44 return ToolParam::ParamType::TYPE_FLOAT; 45 } 46 47 template <> GetValueType()48ToolParam::ParamType ToolParam::GetValueType<std::string>() { 49 return ToolParam::ParamType::TYPE_STRING; 50 } 51 AssertParamExists(const std::string & name) const52void ToolParams::AssertParamExists(const std::string& name) const { 53 TFLITE_TOOLS_CHECK(HasParam(name)) << name << " was not found."; 54 } 55 Set(const ToolParams & other)56void ToolParams::Set(const ToolParams& other) { 57 for (const auto& param : params_) { 58 const ToolParam* other_param = other.GetParam(param.first); 59 if (other_param == nullptr) continue; 60 param.second->Set(*other_param); 61 } 62 } 63 Merge(const ToolParams & other,bool overwrite)64void ToolParams::Merge(const ToolParams& other, bool overwrite) { 65 for (const auto& one : other.params_) { 66 auto it = params_.find(one.first); 67 if (it == params_.end()) { 68 AddParam(one.first, one.second->Clone()); 69 } else if (overwrite) { 70 it->second->Set(*one.second); 71 } 72 } 73 } 74 75 } // namespace tools 76 } // namespace tflite 77