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
20/*!
21 * \file file_utils.h
22 * \brief Minimum file manipulation utils for runtime.
23 */
24#ifndef TVM_RUNTIME_FILE_UTILS_H_
25#define TVM_RUNTIME_FILE_UTILS_H_
26
27#include <tvm/runtime/container/map.h>
28#include <tvm/runtime/container/string.h>
29
30#include <string>
31#include <unordered_map>
32
33#include "meta_data.h"
34
35namespace tvm {
36namespace runtime {
37/*!
38 * \brief Get file format from given file name or format argument.
39 * \param file_name The name of the file.
40 * \param format The format of the file.
41 */
42std::string GetFileFormat(const std::string& file_name, const std::string& format);
43
44/*!
45 * \return the directory in which TVM stores cached files.
46 * May be set using TVM_CACHE_DIR; defaults to system locations.
47 */
48std::string GetCacheDir();
49
50/*!
51 * \brief Get meta file path given file name and format.
52 * \param file_name The name of the file.
53 */
54std::string GetMetaFilePath(const std::string& file_name);
55
56/*!
57 * \brief Get file basename (i.e. without leading directories)
58 * \param file_name The name of the file.
59 * \return the base name
60 */
61std::string GetFileBasename(const std::string& file_name);
62
63/*!
64 * \brief Load binary file into a in-memory buffer.
65 * \param file_name The name of the file.
66 * \param data The data to be loaded.
67 */
68void LoadBinaryFromFile(const std::string& file_name, std::string* data);
69
70/*!
71 * \brief Load binary file into a in-memory buffer.
72 * \param file_name The name of the file.
73 * \param data The binary data to be saved.
74 */
75void SaveBinaryToFile(const std::string& file_name, const std::string& data);
76
77/*!
78 * \brief Save meta data to file.
79 * \param file_name The name of the file.
80 * \param fmap The function info map.
81 */
82void SaveMetaDataToFile(const std::string& file_name,
83 const std::unordered_map<std::string, FunctionInfo>& fmap);
84
85/*!
86 * \brief Load meta data to file.
87 * \param file_name The name of the file.
88 * \param fmap The function info map.
89 */
90void LoadMetaDataFromFile(const std::string& file_name,
91 std::unordered_map<std::string, FunctionInfo>* fmap);
92
93/*!
94 * \brief Copy the content of an existing file to another file.
95 * \param src_file_name Path to the source file.
96 * \param dest_file_name Path of the destination file. If this file already exists,
97 * replace its content.
98 */
99void CopyFile(const std::string& src_file_name, const std::string& dest_file_name);
100
101/*!
102 * \brief Remove (unlink) a file.
103 * \param file_name The file name.
104 */
105void RemoveFile(const std::string& file_name);
106
107constexpr uint64_t kTVMNDArrayListMagic = 0xF7E58D4F05049CB7;
108/*!
109 * \brief Load parameters from a string.
110 * \param param_blob Serialized string of parameters.
111 * \return Map of parameter name to parameter value.
112 */
113Map<String, NDArray> LoadParams(const std::string& param_blob);
114/*!
115 * \brief Load parameters from a stream.
116 * \param strm Stream to load parameters from.
117 * \return Map of parameter name to parameter value.
118 */
119Map<String, NDArray> LoadParams(dmlc::Stream* strm);
120/*!
121 * \brief Serialize parameters to a byte array.
122 * \param params Parameters to save.
123 * \return String containing binary parameter data.
124 */
125std::string SaveParams(const Map<String, NDArray>& params);
126/*!
127 * \brief Serialize parameters to a stream.
128 * \param strm Stream to write to.
129 * \param params Parameters to save.
130 */
131void SaveParams(dmlc::Stream* strm, const Map<String, NDArray>& params);
132
133/*!
134 * \brief A dmlc stream which wraps standard file operations.
135 */
136struct SimpleBinaryFileStream : public dmlc::Stream {
137 public:
138 SimpleBinaryFileStream(const std::string& path, std::string mode) {
139 const char* fname = path.c_str();
140
141 CHECK(mode == "wb" || mode == "rb") << "Only allowed modes are 'wb' and 'rb'";
142 read_ = mode == "rb";
143 fp_ = std::fopen(fname, mode.c_str());
144 CHECK(fp_ != nullptr) << "Unable to open file " << path;
145 }
146 virtual ~SimpleBinaryFileStream(void) { this->Close(); }
147 virtual size_t Read(void* ptr, size_t size) {
148 CHECK(read_) << "File opened in write-mode, cannot read.";
149 CHECK(fp_ != nullptr) << "File is closed";
150 return std::fread(ptr, 1, size, fp_);
151 }
152 virtual void Write(const void* ptr, size_t size) {
153 CHECK(!read_) << "File opened in read-mode, cannot write.";
154 CHECK(fp_ != nullptr) << "File is closed";
155 CHECK(std::fwrite(ptr, 1, size, fp_) == size) << "SimpleBinaryFileStream.Write incomplete";
156 }
157 inline void Close(void) {
158 if (fp_ != nullptr) {
159 std::fclose(fp_);
160 fp_ = nullptr;
161 }
162 }
163
164 private:
165 std::FILE* fp_ = nullptr;
166 bool read_;
167}; // class SimpleBinaryFileStream
168
169} // namespace runtime
170} // namespace tvm
171#endif // TVM_RUNTIME_FILE_UTILS_H_
172