From cf9d4554d4f70032f8c8ba8b141d6c659291068c Mon Sep 17 00:00:00 2001 From: Andrew Luo Date: Mon, 30 Jan 2023 16:05:43 -0800 Subject: [PATCH 1/8] initial commit --- include/tvm/support/file_io.h | 46 +++++++++++++++++++++++++++++++++++ src/runtime/vm/executable.cc | 5 ++-- 2 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 include/tvm/support/file_io.h diff --git a/include/tvm/support/file_io.h b/include/tvm/support/file_io.h new file mode 100644 index 000000000000..038bf4c9db05 --- /dev/null +++ b/include/tvm/support/file_io.h @@ -0,0 +1,46 @@ +#include + +#ifndef SUPPORT_FILE_IO_H_ +#define SUPPORT_FILE_IO_H_ + +namespace tvm { +namespace support { + +/*! + * \brief A dmlc stream which wraps standard file operations. + */ +struct SimpleBinaryFileStream : public dmlc::Stream { + public: + SimpleBinaryFileStream(const std::string& path, bool read) { + const char* fname = path.c_str(); + if (read) { + fp_ = std::fopen(fname, "rb"); + } else { + fp_ = std::fopen(fname, "wb"); + } + CHECK(fp_) << "Unable to open file " << path; + read_ = read; + } + virtual ~SimpleBinaryFileStream(void) { this->Close(); } + virtual size_t Read(void* ptr, size_t size) { + CHECK(read_) << "File opened in write-mode, cannot read."; + return std::fread(ptr, 1, size, fp_); + } + virtual void Write(const void* ptr, size_t size) { + CHECK(!read_) << "File opened in read-mode, cannot write."; + CHECK(std::fwrite(ptr, 1, size, fp_) == size) << "SimpleBinaryFileStream.Write incomplete"; + } + inline void Close(void) { + if (fp_ != NULL) { + std::fclose(fp_); + fp_ = NULL; + } + } + + private: + std::FILE* fp_ = nullptr; + bool read_; // if false, then in read mode. +}; // class SimpleBinaryFileStream +} // namespace support +} // namespace tvm +#endif // SUPPORT_FILE_IO_H_ diff --git a/src/runtime/vm/executable.cc b/src/runtime/vm/executable.cc index 082ff0556544..cfcb1a5db8f5 100644 --- a/src/runtime/vm/executable.cc +++ b/src/runtime/vm/executable.cc @@ -28,6 +28,7 @@ #include #include #include +#include #include #include @@ -381,9 +382,7 @@ void Executable::LoadLateBoundConstantsFromMap(Map map) { } void Executable::LoadLateBoundConstantsFromFile(const std::string& path) { - std::string bytes; - LoadBinaryFromFile(path, &bytes); - dmlc::MemoryStringStream stream(&bytes); + tvm::support::SimpleBinaryFileStream stream(path, true); LoadLateBoundConstantsFromStream(&stream); } From c7d7622f690d4f059b7fe29c512ac9477c8876a2 Mon Sep 17 00:00:00 2001 From: Andrew Luo Date: Mon, 30 Jan 2023 16:36:00 -0800 Subject: [PATCH 2/8] update additional use cases --- src/runtime/vm/executable.cc | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/src/runtime/vm/executable.cc b/src/runtime/vm/executable.cc index cfcb1a5db8f5..ce4f7d4f46a5 100644 --- a/src/runtime/vm/executable.cc +++ b/src/runtime/vm/executable.cc @@ -345,10 +345,8 @@ void Executable::MoveLateBoundConstantsToStream(dmlc::Stream* stream, size_t byt } void Executable::MoveLateBoundConstantsToFile(const std::string& path, size_t byte_limit) { - std::string bytes; - dmlc::MemoryStringStream stream(&bytes); + tvm::support::SimpleBinaryFileStream stream(path, false); MoveLateBoundConstantsToStream(&stream, byte_limit); - SaveBinaryToFile(path, bytes); } void Executable::LoadLateBoundConstantsFromStream(dmlc::Stream* stream) { @@ -1062,22 +1060,16 @@ Module ExecutableLoadBinary(void* strm) { } void Executable::SaveToFile(const std::string& path, const std::string& format) { - std::string data; - dmlc::MemoryStringStream writer(&data); - dmlc::SeekStream* strm = &writer; - SaveToBinary(strm); - SaveBinaryToFile(path, data); + tvm::support::SimpleBinaryFileStream stream(path, false); + SaveToBinary(&stream); } TVM_REGISTER_GLOBAL("runtime.module.loadbinary_VMExecutable").set_body_typed(ExecutableLoadBinary); // Load module from module. Module ExecutableLoadFile(const std::string& file_name, const std::string& format) { - std::string data; - LoadBinaryFromFile(file_name, &data); - dmlc::MemoryStringStream reader(&data); - dmlc::Stream* strm = &reader; - auto exec = ExecutableLoadBinary(reinterpret_cast(strm)); + tvm::support::SimpleBinaryFileStream stream(file_name, true); + auto exec = ExecutableLoadBinary(reinterpret_cast(&stream)); return exec; } From a439a8a43666f8549147fbaca01913f5da0e377d Mon Sep 17 00:00:00 2001 From: Andrew Luo Date: Mon, 30 Jan 2023 16:47:18 -0800 Subject: [PATCH 3/8] typo --- include/tvm/support/file_io.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/tvm/support/file_io.h b/include/tvm/support/file_io.h index 038bf4c9db05..68eef2f7f008 100644 --- a/include/tvm/support/file_io.h +++ b/include/tvm/support/file_io.h @@ -39,7 +39,7 @@ struct SimpleBinaryFileStream : public dmlc::Stream { private: std::FILE* fp_ = nullptr; - bool read_; // if false, then in read mode. + bool read_; // if false, then in write mode. }; // class SimpleBinaryFileStream } // namespace support } // namespace tvm From 4e7880764e47ba3639507e575f08eed716c0e7cc Mon Sep 17 00:00:00 2001 From: Andrew Luo Date: Mon, 30 Jan 2023 17:10:56 -0800 Subject: [PATCH 4/8] asf header, summary --- include/tvm/support/file_io.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/include/tvm/support/file_io.h b/include/tvm/support/file_io.h index 68eef2f7f008..625fcdf53d07 100644 --- a/include/tvm/support/file_io.h +++ b/include/tvm/support/file_io.h @@ -1,3 +1,26 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/*! + * \file file_io.h + * \brief Defines a simple DMLC stream for file io. + */ #include #ifndef SUPPORT_FILE_IO_H_ From 2a12aca226939d5d880041b88eee5141214fb5a8 Mon Sep 17 00:00:00 2001 From: Andrew Luo Date: Wed, 1 Feb 2023 10:27:01 -0800 Subject: [PATCH 5/8] clean up --- .../file_io.h => runtime/dmlc_file_stream.h} | 37 ++++++++++--------- src/runtime/vm/executable.cc | 10 ++--- 2 files changed, 24 insertions(+), 23 deletions(-) rename include/tvm/{support/file_io.h => runtime/dmlc_file_stream.h} (70%) diff --git a/include/tvm/support/file_io.h b/include/tvm/runtime/dmlc_file_stream.h similarity index 70% rename from include/tvm/support/file_io.h rename to include/tvm/runtime/dmlc_file_stream.h index 625fcdf53d07..da5ff3108bf7 100644 --- a/include/tvm/support/file_io.h +++ b/include/tvm/runtime/dmlc_file_stream.h @@ -18,52 +18,53 @@ */ /*! - * \file file_io.h + * \file dmlc_file_stream.h * \brief Defines a simple DMLC stream for file io. */ #include -#ifndef SUPPORT_FILE_IO_H_ -#define SUPPORT_FILE_IO_H_ +#ifndef TVM_RUNTIME_DMLC_FILE_STREAM_H_ +#define TVM_RUNTIME_DMLC_FILE_STREAM_H_ namespace tvm { -namespace support { +namespace runtime { /*! * \brief A dmlc stream which wraps standard file operations. */ struct SimpleBinaryFileStream : public dmlc::Stream { public: - SimpleBinaryFileStream(const std::string& path, bool read) { + SimpleBinaryFileStream(const std::string& path, std::string mode) { const char* fname = path.c_str(); - if (read) { - fp_ = std::fopen(fname, "rb"); - } else { - fp_ = std::fopen(fname, "wb"); - } - CHECK(fp_) << "Unable to open file " << path; - read_ = read; + + CHECK(mode == "wb" || mode == "rb") << "Only allowed modes are 'wb' and 'rb'"; + read_ = mode == "rb"; + fp_ = std::fopen(fname, mode.c_str()); + CHECK(fp_ != nullptr) << "Unable to open file " << path; } virtual ~SimpleBinaryFileStream(void) { this->Close(); } virtual size_t Read(void* ptr, size_t size) { CHECK(read_) << "File opened in write-mode, cannot read."; + CHECK(fp_ != nullptr) << "File is closed"; return std::fread(ptr, 1, size, fp_); } virtual void Write(const void* ptr, size_t size) { CHECK(!read_) << "File opened in read-mode, cannot write."; + CHECK(fp_ != nullptr) << "File is closed"; CHECK(std::fwrite(ptr, 1, size, fp_) == size) << "SimpleBinaryFileStream.Write incomplete"; } inline void Close(void) { - if (fp_ != NULL) { + if (fp_ != nullptr) { std::fclose(fp_); - fp_ = NULL; + fp_ = nullptr; } } private: std::FILE* fp_ = nullptr; - bool read_; // if false, then in write mode. -}; // class SimpleBinaryFileStream -} // namespace support + bool read_; +}; // class SimpleBinaryFileStream + +} // namespace runtime } // namespace tvm -#endif // SUPPORT_FILE_IO_H_ +#endif // TVM_RUNTIME_DMLC_FILE_STREAM_H_ diff --git a/src/runtime/vm/executable.cc b/src/runtime/vm/executable.cc index ce4f7d4f46a5..a68e484f8e51 100644 --- a/src/runtime/vm/executable.cc +++ b/src/runtime/vm/executable.cc @@ -25,10 +25,10 @@ #include #include #include +#include #include #include #include -#include #include #include @@ -345,7 +345,7 @@ void Executable::MoveLateBoundConstantsToStream(dmlc::Stream* stream, size_t byt } void Executable::MoveLateBoundConstantsToFile(const std::string& path, size_t byte_limit) { - tvm::support::SimpleBinaryFileStream stream(path, false); + tvm::runtime::SimpleBinaryFileStream stream(path, "wb"); MoveLateBoundConstantsToStream(&stream, byte_limit); } @@ -380,7 +380,7 @@ void Executable::LoadLateBoundConstantsFromMap(Map map) { } void Executable::LoadLateBoundConstantsFromFile(const std::string& path) { - tvm::support::SimpleBinaryFileStream stream(path, true); + tvm::runtime::SimpleBinaryFileStream stream(path, "rb"); LoadLateBoundConstantsFromStream(&stream); } @@ -1060,7 +1060,7 @@ Module ExecutableLoadBinary(void* strm) { } void Executable::SaveToFile(const std::string& path, const std::string& format) { - tvm::support::SimpleBinaryFileStream stream(path, false); + tvm::runtime::SimpleBinaryFileStream stream(path, "wb"); SaveToBinary(&stream); } @@ -1068,7 +1068,7 @@ TVM_REGISTER_GLOBAL("runtime.module.loadbinary_VMExecutable").set_body_typed(Exe // Load module from module. Module ExecutableLoadFile(const std::string& file_name, const std::string& format) { - tvm::support::SimpleBinaryFileStream stream(file_name, true); + tvm::runtime::SimpleBinaryFileStream stream(file_name, "rb"); auto exec = ExecutableLoadBinary(reinterpret_cast(&stream)); return exec; } From 5e92c885aa8233501164d5b0545fdfeb7135f28a Mon Sep 17 00:00:00 2001 From: Andrew Luo Date: Wed, 1 Feb 2023 10:40:11 -0800 Subject: [PATCH 6/8] lint --- include/tvm/runtime/dmlc_file_stream.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/tvm/runtime/dmlc_file_stream.h b/include/tvm/runtime/dmlc_file_stream.h index da5ff3108bf7..8edf42fef67b 100644 --- a/include/tvm/runtime/dmlc_file_stream.h +++ b/include/tvm/runtime/dmlc_file_stream.h @@ -23,6 +23,8 @@ */ #include +#include + #ifndef TVM_RUNTIME_DMLC_FILE_STREAM_H_ #define TVM_RUNTIME_DMLC_FILE_STREAM_H_ From 2c0b23fcd1d0b397affdb67cce9a43be8f1cce80 Mon Sep 17 00:00:00 2001 From: Andrew Luo Date: Wed, 1 Feb 2023 12:25:46 -0800 Subject: [PATCH 7/8] move code to src/runtime/file_utils.h --- include/tvm/runtime/dmlc_file_stream.h | 72 -------------------------- src/runtime/file_utils.h | 38 ++++++++++++++ src/runtime/vm/executable.cc | 1 - 3 files changed, 38 insertions(+), 73 deletions(-) delete mode 100644 include/tvm/runtime/dmlc_file_stream.h diff --git a/include/tvm/runtime/dmlc_file_stream.h b/include/tvm/runtime/dmlc_file_stream.h deleted file mode 100644 index 8edf42fef67b..000000000000 --- a/include/tvm/runtime/dmlc_file_stream.h +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -/*! - * \file dmlc_file_stream.h - * \brief Defines a simple DMLC stream for file io. - */ -#include - -#include - -#ifndef TVM_RUNTIME_DMLC_FILE_STREAM_H_ -#define TVM_RUNTIME_DMLC_FILE_STREAM_H_ - -namespace tvm { -namespace runtime { - -/*! - * \brief A dmlc stream which wraps standard file operations. - */ -struct SimpleBinaryFileStream : public dmlc::Stream { - public: - SimpleBinaryFileStream(const std::string& path, std::string mode) { - const char* fname = path.c_str(); - - CHECK(mode == "wb" || mode == "rb") << "Only allowed modes are 'wb' and 'rb'"; - read_ = mode == "rb"; - fp_ = std::fopen(fname, mode.c_str()); - CHECK(fp_ != nullptr) << "Unable to open file " << path; - } - virtual ~SimpleBinaryFileStream(void) { this->Close(); } - virtual size_t Read(void* ptr, size_t size) { - CHECK(read_) << "File opened in write-mode, cannot read."; - CHECK(fp_ != nullptr) << "File is closed"; - return std::fread(ptr, 1, size, fp_); - } - virtual void Write(const void* ptr, size_t size) { - CHECK(!read_) << "File opened in read-mode, cannot write."; - CHECK(fp_ != nullptr) << "File is closed"; - CHECK(std::fwrite(ptr, 1, size, fp_) == size) << "SimpleBinaryFileStream.Write incomplete"; - } - inline void Close(void) { - if (fp_ != nullptr) { - std::fclose(fp_); - fp_ = nullptr; - } - } - - private: - std::FILE* fp_ = nullptr; - bool read_; -}; // class SimpleBinaryFileStream - -} // namespace runtime -} // namespace tvm -#endif // TVM_RUNTIME_DMLC_FILE_STREAM_H_ diff --git a/src/runtime/file_utils.h b/src/runtime/file_utils.h index bd3bec903180..d52349c2aba0 100644 --- a/src/runtime/file_utils.h +++ b/src/runtime/file_utils.h @@ -129,6 +129,44 @@ std::string SaveParams(const Map& params); * \param params Parameters to save. */ void SaveParams(dmlc::Stream* strm, const Map& params); + + +/*! + * \brief A dmlc stream which wraps standard file operations. + */ +struct SimpleBinaryFileStream : public dmlc::Stream { + public: + SimpleBinaryFileStream(const std::string& path, std::string mode) { + const char* fname = path.c_str(); + + CHECK(mode == "wb" || mode == "rb") << "Only allowed modes are 'wb' and 'rb'"; + read_ = mode == "rb"; + fp_ = std::fopen(fname, mode.c_str()); + CHECK(fp_ != nullptr) << "Unable to open file " << path; + } + virtual ~SimpleBinaryFileStream(void) { this->Close(); } + virtual size_t Read(void* ptr, size_t size) { + CHECK(read_) << "File opened in write-mode, cannot read."; + CHECK(fp_ != nullptr) << "File is closed"; + return std::fread(ptr, 1, size, fp_); + } + virtual void Write(const void* ptr, size_t size) { + CHECK(!read_) << "File opened in read-mode, cannot write."; + CHECK(fp_ != nullptr) << "File is closed"; + CHECK(std::fwrite(ptr, 1, size, fp_) == size) << "SimpleBinaryFileStream.Write incomplete"; + } + inline void Close(void) { + if (fp_ != nullptr) { + std::fclose(fp_); + fp_ = nullptr; + } + } + + private: + std::FILE* fp_ = nullptr; + bool read_; +}; // class SimpleBinaryFileStream + } // namespace runtime } // namespace tvm #endif // TVM_RUNTIME_FILE_UTILS_H_ diff --git a/src/runtime/vm/executable.cc b/src/runtime/vm/executable.cc index a68e484f8e51..5696bc5314c1 100644 --- a/src/runtime/vm/executable.cc +++ b/src/runtime/vm/executable.cc @@ -25,7 +25,6 @@ #include #include #include -#include #include #include #include From 42d7a379fefb6b95c098f6f5f930dd8d26af05ab Mon Sep 17 00:00:00 2001 From: Andrew Luo Date: Wed, 1 Feb 2023 12:27:22 -0800 Subject: [PATCH 8/8] file utils is cool --- src/runtime/file_utils.h | 1 - 1 file changed, 1 deletion(-) diff --git a/src/runtime/file_utils.h b/src/runtime/file_utils.h index d52349c2aba0..20806f5ff136 100644 --- a/src/runtime/file_utils.h +++ b/src/runtime/file_utils.h @@ -130,7 +130,6 @@ std::string SaveParams(const Map& params); */ void SaveParams(dmlc::Stream* strm, const Map& params); - /*! * \brief A dmlc stream which wraps standard file operations. */