tvm
ndarray_cache_support.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 #ifndef TVM_RUNTIME_RELAX_VM_NDARRAY_CACHE_SUPPORT_H_
20 #define TVM_RUNTIME_RELAX_VM_NDARRAY_CACHE_SUPPORT_H_
21 
22 #include <tvm/runtime/ndarray.h>
23 #include <tvm/runtime/registry.h>
24 
25 #include <string>
26 #include <unordered_map>
27 #include <vector>
28 
29 namespace tvm {
30 namespace runtime {
31 namespace relax_vm {
32 
38  struct FileRecord {
40  struct ParamRecord {
48  TVM_DLL NDArray Load(Device device, const std::string* raw_data,
49  Optional<NDArray>* staging_buffer = nullptr) const;
50 
52  std::string name;
58  std::string format;
60  int64_t nbytes;
62  int64_t byte_offset;
63  };
64 
66  TVM_DLL Array<NDArray> Load(Device device, //
67  const std::string& path_prefix, //
68  std::string* raw_data_buffer, //
69  Optional<NDArray>* staging_buffer = nullptr) const;
70 
72  std::string data_path;
74  std::string format;
76  int64_t nbytes;
78  std::vector<ParamRecord> records;
79  };
81  std::vector<FileRecord> records;
83  std::string path;
84 
86  TVM_DLL static NDArrayCacheMetadata Load(const std::string& path);
88  static NDArrayCacheMetadata LoadFromStr(const std::string& json_str, const std::string& path);
89 };
90 
91 } // namespace relax_vm
92 } // namespace runtime
93 } // namespace tvm
94 
95 #endif // TVM_RUNTIME_RELAX_VM_NDARRAY_CACHE_SUPPORT_H_
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:289
Runtime primitive data type.
Definition: data_type.h:43
Managed NDArray. The array is backed by reference counted blocks.
Definition: ndarray.h:51
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
Reference to shape tuple objects.
Definition: shape_tuple.h:85
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
DLDevice Device
Definition: ndarray.h:43
A device-independent managed NDArray abstraction.
This file defines the TVM global function registry.
Metadata of each parameter.
Definition: ndarray_cache_support.h:40
DataType dtype
Data type of the parameter.
Definition: ndarray_cache_support.h:56
ShapeTuple shape
Shape of the parameter.
Definition: ndarray_cache_support.h:54
NDArray Load(Device device, const std::string *raw_data, Optional< NDArray > *staging_buffer=nullptr) const
Load the parameter from raw data.
std::string format
Format of the parameter.
Definition: ndarray_cache_support.h:58
int64_t nbytes
Number of bytes.
Definition: ndarray_cache_support.h:60
std::string name
Name of the parameter.
Definition: ndarray_cache_support.h:52
int64_t byte_offset
Offset from the raw stream.
Definition: ndarray_cache_support.h:62
Each shard of NDArray cache, which by default, is named as "params_shard_x.bin".
Definition: ndarray_cache_support.h:38
Array< NDArray > Load(Device device, const std::string &path_prefix, std::string *raw_data_buffer, Optional< NDArray > *staging_buffer=nullptr) const
Load a FileRecord into memory.
std::string data_path
Relative path to the bin file.
Definition: ndarray_cache_support.h:72
int64_t nbytes
Size of the file.
Definition: ndarray_cache_support.h:76
std::string format
Format of the file.
Definition: ndarray_cache_support.h:74
std::vector< ParamRecord > records
The parameters in the file.
Definition: ndarray_cache_support.h:78
Metadata for NDArray cache, which by default, is named as "ndarray-cache.json".
Definition: ndarray_cache_support.h:36
static NDArrayCacheMetadata LoadFromStr(const std::string &json_str, const std::string &path)
Load the metadata from a given JSON string.
static NDArrayCacheMetadata Load(const std::string &path)
Load the metadata from a specific directory.
std::vector< FileRecord > records
The files in the NDArray cache.
Definition: ndarray_cache_support.h:81
std::string path
The path to the ndarray-cache.json file.
Definition: ndarray_cache_support.h:83