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