tvm
metadata.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
24 #ifndef TVM_RUNTIME_METADATA_H_
25 #define TVM_RUNTIME_METADATA_H_
26 
27 #include <dmlc/memory_io.h>
31 #include <tvm/runtime/object.h>
32 #include <tvm/support/span.h>
33 
34 #include <memory>
35 #include <string>
36 #include <vector>
37 
38 // Version number recorded in emitted artifacts for runtime checking.
39 #define TVM_METADATA_VERSION 1
40 
41 namespace tvm {
42 namespace runtime {
43 namespace metadata {
48 static const constexpr int64_t kMetadataVersion = TVM_METADATA_VERSION;
49 
50 class Metadata;
51 class TensorInfo;
53 
55  public:
56  explicit MetadataNode(const struct ::TVMMetadata* data) : data_{data} {}
57  static constexpr const char* _type_key = "metadata.MetadataNode";
58  const char* get_c_struct_name() const override;
59  inline int64_t version() const { return int64_t(data_->version); }
60  inline int64_t num_inputs() const { return data_->num_inputs; }
62  inline int64_t num_outputs() const { return data_->num_outputs; }
64  inline int64_t num_workspace_pools() const { return data_->num_workspace_pools; }
66  inline ::tvm::runtime::String mod_name() const { return ::tvm::runtime::String(data_->mod_name); }
67  const struct ::TVMMetadata* data() const { return data_; }
69  inline int64_t num_constant_pools() const { return data_->num_constant_pools; }
71 
72  private:
73  const struct ::TVMMetadata* data_;
74 };
75 
76 class Metadata : public MetadataBase {
77  public:
78  explicit Metadata(const struct ::TVMMetadata* data);
80 };
81 
83  public:
84  explicit TensorInfoNode(const struct ::TVMTensorInfo* data) : data_{data} {}
85  static constexpr const char* _type_key = "metadata.TensorInfoNode";
86  const char* get_c_struct_name() const override;
87  inline ::tvm::runtime::String name() const { return ::tvm::runtime::String(data_->name); }
88  inline int64_t num_shape() const { return data_->num_shape; }
89  inline ::tvm::support::Span<const int64_t, int64_t> shape() const {
90  return ::tvm::support::Span<const int64_t, int64_t>(data_->shape,
91  data_->shape + data_->num_shape);
92  }
94  const struct ::TVMTensorInfo* data() const { return data_; }
96 
97  private:
98  const struct ::TVMTensorInfo* data_;
99 };
100 
101 class TensorInfo : public MetadataBase {
102  public:
103  explicit TensorInfo(const struct ::TVMTensorInfo* data);
105 };
106 
108  public:
109  explicit ConstantInfoMetadataNode(const struct ::TVMConstantInfo* data) : data_{data} {}
110  // This name should match TVMConstantInfo after processing
111  static constexpr const char* _type_key = "metadata.ConstantInfoNode";
112  const char* get_c_struct_name() const override;
113  inline ::tvm::runtime::String name_hint() const {
114  return ::tvm::runtime::String(data_->name_hint);
115  }
116  inline size_t byte_offset() const { return data_->byte_offset; }
117  inline ::tvm::runtime::NDArray data() const {
118  ::tvm::runtime::NDArray ndarray;
119  if (data_->data_len) {
120  dmlc::MemoryFixedSizeStream bytes(const_cast<void*>(data_->data_bytes), data_->data_len);
121  ndarray.Load(&bytes);
122  }
123  return ndarray;
124  }
126 
127  protected:
128  const struct ::TVMConstantInfo* data_;
129 };
130 
132  public:
133  explicit ConstantInfoMetadata(const struct ::TVMConstantInfo* data);
136 };
137 
138 } // namespace metadata
139 } // namespace runtime
140 } // namespace tvm
141 
142 #endif // TVM_RUNTIME_METADATA_H_
struct TVMMetadata TVMMetadata
Definition: aot_executor.h:35
Managed NDArray. The array is backed by reference counted blocks.
Definition: ndarray.h:51
bool Load(dmlc::Stream *stream)
Load NDArray from stream.
Definition: ndarray.h:500
A span-like class which permits access to Array fields with complex elements. These array fields shou...
Definition: metadata_base.h:98
const char * get_c_struct_name() const override
TVM_DECLARE_FINAL_OBJECT_INFO(ConstantInfoMetadataNode, MetadataBaseNode)
static constexpr const char * _type_key
Definition: metadata.h:111
ConstantInfoMetadataNode(const struct ::TVMConstantInfo *data)
Definition: metadata.h:109
size_t byte_offset() const
Definition: metadata.h:116
const struct ::TVMConstantInfo * data_
Definition: metadata.h:128
inline ::tvm::runtime::String name_hint() const
Definition: metadata.h:113
inline ::tvm::runtime::NDArray data() const
Definition: metadata.h:117
ConstantInfoMetadata(const struct ::TVMConstantInfo *data)
TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(ConstantInfoMetadata, MetadataBase, ConstantInfoMetadataNode)
Common base class for all Metadata.
Definition: metadata_base.h:48
Reference class for the common MetadataBaseNode class.
Definition: metadata_base.h:57
Definition: metadata.h:54
const char * get_c_struct_name() const override
inline ::tvm::runtime::String mod_name() const
Definition: metadata.h:66
MetadataNode(const struct ::TVMMetadata *data)
Definition: metadata.h:56
int64_t num_constant_pools() const
Definition: metadata.h:69
int64_t num_inputs() const
Definition: metadata.h:60
static constexpr const char * _type_key
Definition: metadata.h:57
int64_t version() const
Definition: metadata.h:59
ArrayAccessor< struct TVMTensorInfo, TensorInfo > outputs()
ArrayAccessor< struct TVMConstantInfo, ConstantInfoMetadata > constant_pools()
TVM_DECLARE_FINAL_OBJECT_INFO(MetadataNode, MetadataBaseNode)
int64_t num_workspace_pools() const
Definition: metadata.h:64
int64_t num_outputs() const
Definition: metadata.h:62
const struct ::TVMMetadata * data() const
Definition: metadata.h:67
ArrayAccessor< struct TVMTensorInfo, TensorInfo > inputs()
ArrayAccessor< struct TVMTensorInfo, TensorInfo > workspace_pools()
Definition: metadata.h:76
Metadata(const struct ::TVMMetadata *data)
TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(Metadata, MetadataBase, MetadataNode)
TVM_DECLARE_FINAL_OBJECT_INFO(TensorInfoNode, MetadataBaseNode)
inline ::tvm::runtime::String name() const
Definition: metadata.h:87
static constexpr const char * _type_key
Definition: metadata.h:85
inline ::tvm::runtime::DataType dtype() const
Definition: metadata.h:93
TensorInfoNode(const struct ::TVMTensorInfo *data)
Definition: metadata.h:84
inline ::tvm::support::Span< const int64_t, int64_t > shape() const
Definition: metadata.h:89
const char * get_c_struct_name() const override
int64_t num_shape() const
Definition: metadata.h:88
const struct ::TVMTensorInfo * data() const
Definition: metadata.h:94
Definition: metadata.h:101
TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(TensorInfo, MetadataBase, TensorInfoNode)
TensorInfo(const struct ::TVMTensorInfo *data)
#define TVM_METADATA_VERSION
Definition: metadata.h:39
Defines types which can be used in Metadata.
Defines types which can be used in metadata here which are also shared between C and C++ code bases.
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
runtime::DataType DataType
Definition: data_type.h:491
A managed object in the TVM runtime.
Reimplementation of part of C++-20 style span.
const void * data_bytes
data bytes of serialized NDArray
Definition: metadata_types.h:102
int64_t byte_offset
Offset in bytes of the constant.
Definition: metadata_types.h:98
const char * name_hint
Name of the constant.
Definition: metadata_types.h:96
int64_t data_len
length of the data_bytes field
Definition: metadata_types.h:100
int64_t num_outputs
Number of elements in outputs array.
Definition: metadata_types.h:56
const char * mod_name
Name of the model, as passed to tvm.relay.build.
Definition: metadata_types.h:70
int64_t num_workspace_pools
Number of elements in workspace_pools array.
Definition: metadata_types.h:63
int64_t version
Version identifier for this metadata.
Definition: metadata_types.h:42
int64_t num_inputs
Number of elements in inputs array.
Definition: metadata_types.h:49
int64_t num_constant_pools
Number of elements in constant_pools array.
Definition: metadata_types.h:68
int64_t num_shape
Rank of this tensor.
Definition: metadata_types.h:85
DLDataType dtype
Data type of one element of this tensor.
Definition: metadata_types.h:87
const char * name
Name of the tensor, as specified in the Relay program.
Definition: metadata_types.h:81
const int64_t * shape
Shape of the tensor.
Definition: metadata_types.h:83