HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
schema_registry.h
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License.
3 
4 #pragma once
5 #include <mutex>
6 #include <deque>
7 #include <map>
8 #include <sstream>
9 
10 #include "onnx/onnx_pb.h"
11 #include "onnx/onnx-operators_pb.h"
12 #include "onnx/defs/schema.h"
13 
14 #include "core/graph/constants.h"
15 #include "core/common/common.h"
16 #include "core/common/status.h"
18 
19 namespace onnxruntime {
20 using OpName_Domain_Version_Schema_Map = std::unordered_map<
22  std::unordered_map<std::string, std::map<ONNX_NAMESPACE::OperatorSetVersion, ONNX_NAMESPACE::OpSchema>>>;
23 
24 /**
25 @struct SchemaRegistryVersion
26 onnxruntime schema registry is a supplement to the built-in ONNX schema.
27 Every schema registry represent a collection of schema deltas from baseline_opset_version to opset_version
28 */
32 };
33 
34 using DomainToVersionMap = std::unordered_map<std::string, int>;
35 using DomainToVersionRangeMap = std::unordered_map<std::string, SchemaRegistryVersion>;
36 
37 class IOnnxRuntimeOpSchemaCollection : public ONNX_NAMESPACE::ISchemaRegistry {
38  public:
39  virtual DomainToVersionMap GetLatestOpsetVersions(bool is_onnx_only) const = 0;
40 
41  using ISchemaRegistry::GetSchema;
42 
43  const ONNX_NAMESPACE::OpSchema* GetSchema(const std::string& key, const int maxInclusiveVersion,
44  const std::string& domain) const final {
45  const ONNX_NAMESPACE::OpSchema* latest_schema = nullptr;
46  int earliest_opset_where_unchanged = std::numeric_limits<int>::max();
47  GetSchemaAndHistory(key, maxInclusiveVersion, domain, &latest_schema, &earliest_opset_where_unchanged);
48 
49  assert(latest_schema == nullptr || (latest_schema->SinceVersion() <= maxInclusiveVersion &&
50  earliest_opset_where_unchanged == latest_schema->SinceVersion()));
51 
52  return latest_schema;
53  }
54 
55  virtual void GetSchemaAndHistory(
56  const std::string& key,
57  int maxInclusiveVersion,
58  const std::string& domain,
59  const ONNX_NAMESPACE::OpSchema** latest_schema,
60  int* earliest_opset_where_unchanged) const = 0;
61 };
62 
63 /**
64 @class OnnxRuntimeOpSchemaRegistry
65 
66 OnnxRuntimeOpSchemaRegistry is used to provide supplement for built-in ONNX schemas.
67 Each OnnxRuntimeOpSchemaRegistry must register complete opsets delta from a baseline version to max opset version.
68 (Please notice that baseline opsets are not include in the delta)
69 
70 For example, ONNXRuntime is build with ONNX 1.2 which is at opset7, to use ONNX opset8 and opset9,
71 user could create a OnnxRuntimeOpSchemaRegistry and config it as {baseline_opset_version = 7, opset_version = 9}
72 it means this OnnxRuntimeOpSchemaRegistry contains the complete delta from opset7 to opset9.
73 */
75  public:
76  OnnxRuntimeOpSchemaRegistry() = default;
77 
79  const std::string& domain,
80  int baseline_opset_version,
81  int opset_version);
82 
83  DomainToVersionMap GetLatestOpsetVersions(bool is_onnx_only) const override;
84 
85  // OnnxRuntimeOpSchemaRegistry must register complete delta for a opset.
87  std::vector<ONNX_NAMESPACE::OpSchema>& schemas,
88  const std::string& domain,
89  int baseline_opset_version,
90  int opset_version);
91 
93 
94  void GetSchemaAndHistory(const std::string& key, int maxInclusiveVersion, const std::string& domain,
95  const ONNX_NAMESPACE::OpSchema** latest_schema,
96  int* earliest_opset_where_unchanged) const override;
97 
98  bool empty() const {
99  return map_.empty();
100  }
101 
102  private:
103  common::Status RegisterOpSchema(ONNX_NAMESPACE::OpSchema&& op_schema);
104 
105  common::Status RegisterOpSchemaInternal(ONNX_NAMESPACE::OpSchema&& op_schema);
106 
107  OrtMutex mutex_;
108 
110  DomainToVersionRangeMap domain_version_range_map_;
111 };
112 
113 /**
114 @class SchemaRegistryManager
115 
116 SchemaRegistryManager provides a view based on built-in ONNX schema and a list of
117 OnnxRuntimeOpSchemaRegistry as supplement.
118 
119 The user needs to make sure the customized schema registry is valid, otherwise the behavior is undefined.
120 
121 @todo We may add more consistency checks later.
122 */
124  public:
125  /**
126  Register a new schema registry instance.
127  @remarks The schema registry priority is the reverse of registration order. i.e. the last registry added will be
128  searched first for a matching OpSchema.
129  */
130  void RegisterRegistry(std::shared_ptr<IOnnxRuntimeOpSchemaCollection> registry);
131 
132  /** Gets the latest opset versions.
133  @param is_onnx_only If true, return the latest ONNX schemas. If false, return the latest schemas for all domains.
134  */
135  DomainToVersionMap GetLatestOpsetVersions(bool is_onnx_only) const override;
136 
137  /** Gets the last released opset versions.
138  @param is_onnx_only If true, return ONNX schemas only. If false, return the schemas for all domains.
139  */
140  DomainToVersionMap GetLastReleasedOpsetVersions(bool is_onnx_only) const ;
141  /**
142  Gets the OpSchema and its history.
143  Searches custom schema registries starting with the last one added. \
144  If the OpSchema is not found the default ONNX schema registry is searched.
145 
146  @param key Operator type.
147  @param max_inclusive_version Maximum opset version allowed, inclusive.
148  @param domain The domain of the operator.
149  @param[out] latest_schema Returns the latest OpSchema if found. nullptr otherwise.
150  @param[out] earliest_opset_where_unchanged The earliest opset version preceding max_inclusive_version where the
151  operator is known to be unchanged.
152  */
153  void GetSchemaAndHistory(const std::string& key, int max_inclusive_version, const std::string& domain,
154  const ONNX_NAMESPACE::OpSchema** latest_schema,
155  int* earliest_opset_where_unchanged) const override;
156 
157  private:
158  void GetDomainToVersionMapForRegistries(DomainToVersionMap& domain_version_map, bool is_onnx_only) const;
159 
160  std::deque<std::shared_ptr<IOnnxRuntimeOpSchemaCollection>> registries;
161 };
162 
163 } // namespace onnxruntime
void RegisterRegistry(std::shared_ptr< IOnnxRuntimeOpSchemaCollection > registry)
const ONNX_NAMESPACE::OpSchema * GetSchema(const std::string &key, const int maxInclusiveVersion, const std::string &domain) const final
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
common::Status SetBaselineAndOpsetVersionForDomain(const std::string &domain, int baseline_opset_version, int opset_version)
common::Status RegisterOpSet(std::vector< ONNX_NAMESPACE::OpSchema > &schemas, const std::string &domain, int baseline_opset_version, int opset_version)
DomainToVersionMap GetLatestOpsetVersions(bool is_onnx_only) const override
void GetSchemaAndHistory(const std::string &key, int maxInclusiveVersion, const std::string &domain, const ONNX_NAMESPACE::OpSchema **latest_schema, int *earliest_opset_where_unchanged) const override
void GetSchemaAndHistory(const std::string &key, int max_inclusive_version, const std::string &domain, const ONNX_NAMESPACE::OpSchema **latest_schema, int *earliest_opset_where_unchanged) const override
DomainToVersionMap GetLastReleasedOpsetVersions(bool is_onnx_only) const
virtual DomainToVersionMap GetLatestOpsetVersions(bool is_onnx_only) const =0
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
std::unordered_map< std::string, std::unordered_map< std::string, std::map< ONNX_NAMESPACE::OperatorSetVersion, ONNX_NAMESPACE::OpSchema >>> OpName_Domain_Version_Schema_Map
std::unordered_map< std::string, SchemaRegistryVersion > DomainToVersionRangeMap
std::unordered_map< std::string, int > DomainToVersionMap
DomainToVersionMap GetLatestOpsetVersions(bool is_onnx_only) const override
virtual void GetSchemaAndHistory(const std::string &key, int maxInclusiveVersion, const std::string &domain, const ONNX_NAMESPACE::OpSchema **latest_schema, int *earliest_opset_where_unchanged) const =0