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_VM_NDARRAY_CACHE_SUPPORT_H_
20 #define TVM_RUNTIME_VM_NDARRAY_CACHE_SUPPORT_H_
21 
22 #include <tvm/ffi/container/array.h>
23 #include <tvm/ffi/function.h>
24 #include <tvm/runtime/ndarray.h>
25 
26 #include <string>
27 #include <unordered_map>
28 #include <vector>
29 
30 namespace tvm {
31 namespace runtime {
32 namespace vm {
33 
39  struct FileRecord {
41  struct ParamRecord {
49  TVM_DLL NDArray Load(Device device, const std::string* raw_data,
50  Optional<NDArray>* staging_buffer = nullptr) const;
51 
53  std::string name;
55  ffi::Shape shape;
59  std::string format;
61  int64_t nbytes;
63  int64_t byte_offset;
64  };
65 
67  TVM_DLL Array<NDArray> Load(Device device, //
68  const std::string& path_prefix, //
69  std::string* raw_data_buffer, //
70  Optional<NDArray>* staging_buffer = nullptr) const;
71 
73  std::string data_path;
75  std::string format;
77  int64_t nbytes;
79  std::vector<ParamRecord> records;
80  };
82  std::vector<FileRecord> records;
84  std::string path;
85 
87  TVM_DLL static NDArrayCacheMetadata Load(const std::string& path);
89  static NDArrayCacheMetadata LoadFromStr(const std::string& json_str, const std::string& path);
90 };
91 
92 } // namespace vm
93 } // namespace runtime
94 } // namespace tvm
95 
96 #endif // TVM_RUNTIME_VM_NDARRAY_CACHE_SUPPORT_H_
Runtime primitive data type.
Definition: data_type.h:47
Managed NDArray. The array is backed by reference counted blocks.
Definition: ndarray.h:53
Definition: builtin.h:29
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:37
DLDevice Device
Definition: device_api.h:42
A device-independent managed NDArray abstraction.
Metadata of each parameter.
Definition: ndarray_cache_support.h:41
DataType dtype
Data type of the parameter.
Definition: ndarray_cache_support.h:57
int64_t byte_offset
Offset from the raw stream.
Definition: ndarray_cache_support.h:63
ffi::Shape shape
Shape of the parameter.
Definition: ndarray_cache_support.h:55
std::string format
Format of the parameter.
Definition: ndarray_cache_support.h:59
NDArray Load(Device device, const std::string *raw_data, Optional< NDArray > *staging_buffer=nullptr) const
Load the parameter from raw data.
std::string name
Name of the parameter.
Definition: ndarray_cache_support.h:53
int64_t nbytes
Number of bytes.
Definition: ndarray_cache_support.h:61
Each shard of NDArray cache, which by default, is named as "params_shard_x.bin".
Definition: ndarray_cache_support.h:39
std::string format
Format of the file.
Definition: ndarray_cache_support.h:75
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.
int64_t nbytes
Size of the file.
Definition: ndarray_cache_support.h:77
std::string data_path
Relative path to the bin file.
Definition: ndarray_cache_support.h:73
std::vector< ParamRecord > records
The parameters in the file.
Definition: ndarray_cache_support.h:79
Metadata for NDArray cache, which by default, is named as "ndarray-cache.json".
Definition: ndarray_cache_support.h:37
std::string path
The path to the ndarray-cache.json file.
Definition: ndarray_cache_support.h:84
static NDArrayCacheMetadata Load(const std::string &path)
Load the metadata from a specific directory.
static NDArrayCacheMetadata LoadFromStr(const std::string &json_str, const std::string &path)
Load the metadata from a given JSON string.
std::vector< FileRecord > records
The files in the NDArray cache.
Definition: ndarray_cache_support.h:82