Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-22 10:42:45

0001 /*
0002  * SPDX-License-Identifier: Apache-2.0
0003  */
0004 
0005 #ifndef ONNX_DATA_TYPE_UTILS_H
0006 #define ONNX_DATA_TYPE_UTILS_H
0007 
0008 #include <mutex>
0009 #include <string>
0010 #include <unordered_map>
0011 #include <unordered_set>
0012 
0013 #include "onnx/common/common.h"
0014 #include "onnx/onnx_pb.h"
0015 
0016 namespace ONNX_NAMESPACE {
0017 // String pointer as unique TypeProto identifier.
0018 using DataType = const std::string*;
0019 
0020 namespace Utils {
0021 
0022 // Data type utility, which maintains a global type string to TypeProto map.
0023 // DataType (string pointer) is used as unique data type identifier for
0024 // efficiency.
0025 //
0026 // Grammar for data type string:
0027 // <type> ::= <data_type> |
0028 //            tensor(<data_type>) |
0029 //            seq(<type>) |
0030 //            map(<data_type>, <type>)
0031 // <data_type> :: = float | int32 | string | bool | uint8
0032 //                | int8 | uint16 | int16 | int64 | float16 | double
0033 //
0034 // NOTE: <type> ::= <data_type> means the data is scalar (zero dimension).
0035 //
0036 // Example: float, tensor(float), etc.
0037 //
0038 class DataTypeUtils final {
0039  public:
0040   // If the DataType input is invalid, this function will throw std::invalid_argument exception.
0041   // If ONNX_NO_EXCEPTIONS is set it will abort.
0042   static DataType ToType(const std::string& type_str);
0043 
0044   // If the DataType input is invalid, this function will throw std::invalid_argument exception.
0045   // If ONNX_NO_EXCEPTIONS is set it will abort.
0046   static DataType ToType(const TypeProto& type_proto);
0047 
0048   // If the DataType input is invalid, this function will throw std::invalid_argument exception.
0049   // If ONNX_NO_EXCEPTIONS is set it will abort.
0050   static const TypeProto& ToTypeProto(const DataType& data_type);
0051   static std::string ToDataTypeString(int32_t tensor_data_type);
0052 
0053  private:
0054   static void FromString(const std::string& type_str, TypeProto& type_proto);
0055 
0056   static void FromDataTypeString(const std::string& type_str, int32_t& tensor_data_type);
0057 
0058   static std::string ToString(const TypeProto& type_proto, const std::string& left = "", const std::string& right = "");
0059 
0060   // If int32_t input is invalid, this function will throw an exception.
0061   // If ONNX_NO_EXCEPTIONS is set it will abort.
0062 
0063   static bool IsValidDataTypeString(const std::string& type_str);
0064 
0065   static std::unordered_map<std::string, TypeProto>& GetTypeStrToProtoMap();
0066 
0067   // Returns lock used for concurrent updates to TypeStrToProtoMap.
0068   static std::mutex& GetTypeStrLock();
0069 };
0070 } // namespace Utils
0071 } // namespace ONNX_NAMESPACE
0072 
0073 #endif // ! ONNX_DATA_TYPE_UTILS_H