This is an automated email from the ASF dual-hosted git repository.

tqchen pushed a commit to branch unity
in repository https://gitbox.apache.org/repos/asf/tvm.git


The following commit(s) were added to refs/heads/unity by this push:
     new 1d56770213 [Runtime] Refactor NDArrayCache Support (#15659)
1d56770213 is described below

commit 1d56770213f0e0311b8aa12849d0e0fe1fdcba8d
Author: Junru Shao <[email protected]>
AuthorDate: Sat Sep 2 04:09:26 2023 -0700

    [Runtime] Refactor NDArrayCache Support (#15659)
    
    This PR refactors the NDArrayCache support with the following changes:
    
    - Support loading metadata from a string rather than a concrete JSON
      file on disc;
    - Remove dependency to std::filesystem as it is not well supported in
      certain platforms yet. Details:
      https://github.com/apache/tvm/pull/15654#issuecomment-1703282818.
    - Simplify format used in `shard-info.json` as detailed below:
    
    ```
    // Existing format
    [
      {
        "name": "x0",
        "shard_dim": 0,
      },
      {
        "name": "x1",
        "shard_dim": 1,
      }
    ]
    
    // New format
    {
      "x0": 0,
      "x1": 1,
    }
    ```
---
 src/runtime/relax_vm/ndarray_cache_support.cc | 29 +++++++++++----------------
 src/runtime/relax_vm/ndarray_cache_support.h  |  9 +++++----
 2 files changed, 17 insertions(+), 21 deletions(-)

diff --git a/src/runtime/relax_vm/ndarray_cache_support.cc 
b/src/runtime/relax_vm/ndarray_cache_support.cc
index a19cee42d7..da24a1392d 100644
--- a/src/runtime/relax_vm/ndarray_cache_support.cc
+++ b/src/runtime/relax_vm/ndarray_cache_support.cc
@@ -93,28 +93,23 @@ NDArrayCacheMetadata JSONAsNDArrayCacheMetadata(const 
picojson::object& json) {
   return result;
 }
 
-NDArrayCacheMetadata NDArrayCacheMetadata::LoadFromFile(const 
std::filesystem::path& path) {
-  std::string json_str;
-  LoadBinaryFromFile(path, &json_str);
+NDArrayCacheMetadata NDArrayCacheMetadata::LoadFromStr(const std::string& 
json_str,
+                                                       const std::string& 
path) {
   picojson::value json_info;
   picojson::parse(json_info, json_str);
   NDArrayCacheMetadata result = 
JSONAsNDArrayCacheMetadata(json_info.get<picojson::object>());
-  for (auto& file : result.records) {
-    file.data_path = path.parent_path() / file.data_path;
-  }
+  result.path = path;
   return result;
 }
 
-std::unordered_map<std::string, int> LoadShardInfoFromFile(const 
std::filesystem::path& path) {
-  std::string json_str;
-  LoadBinaryFromFile(path, &json_str);
+std::unordered_map<std::string, int> LoadShardInfoFromStr(const std::string& 
json_str) {
   picojson::value json_info;
   picojson::parse(json_info, json_str);
+  picojson::object json_obj = json_info.get<picojson::object>();
   std::unordered_map<std::string, int> result;
-  for (const auto& item : json_info.get<picojson::array>()) {
-    picojson::object kv_pair = item.get<picojson::object>();
-    std::string name = kv_pair["name"].get<std::string>();
-    int shard_dim = kv_pair["shard_dim"].get<int64_t>();
+  for (const auto& kv : json_obj) {
+    std::string name = kv.first;
+    int64_t shard_dim = kv.second.get<int64_t>();
     result[name] = shard_dim;
   }
   return result;
@@ -183,9 +178,9 @@ class NDArrayCache {
    */
   static void Load(const std::string& cache_path, int device_type, int 
device_id) {
     DLDevice device{static_cast<DLDeviceType>(device_type), device_id};
-    NDArrayCacheMetadata metadata =
-        NDArrayCacheMetadata::LoadFromFile(cache_path + "/ndarray-cache.json");
-
+    std::string json_str;
+    LoadBinaryFromFile(cache_path + "/ndarray-cache.json", &json_str);
+    NDArrayCacheMetadata metadata = 
NDArrayCacheMetadata::LoadFromStr(json_str, cache_path);
     Optional<NDArray> staging_buffer;
     auto fcopy_param_from_bytes = [&](NDArray param, const void* data, size_t 
nbytes) {
       if (device_type != kDLOpenCL) {
@@ -216,7 +211,7 @@ class NDArrayCache {
     Map<String, NDArray> result;
     std::string raw_data;
     for (const auto& shard_rec : metadata.records) {
-      LoadBinaryFromFile(shard_rec.data_path, &raw_data);
+      LoadBinaryFromFile(cache_path + "/" + shard_rec.data_path, &raw_data);
       CHECK_EQ(shard_rec.format, "raw-shard") << "ValueError: Only `raw-shard` 
format is supported";
       CHECK_EQ(shard_rec.nbytes, raw_data.length())
           << "ValueError: Parameters are not loaded properly. Please check 
your parameter shards "
diff --git a/src/runtime/relax_vm/ndarray_cache_support.h 
b/src/runtime/relax_vm/ndarray_cache_support.h
index 8ae93ce249..2bc638d6e3 100644
--- a/src/runtime/relax_vm/ndarray_cache_support.h
+++ b/src/runtime/relax_vm/ndarray_cache_support.h
@@ -22,7 +22,6 @@
 #include <tvm/runtime/ndarray.h>
 #include <tvm/runtime/registry.h>
 
-#include <filesystem>
 #include <string>
 #include <unordered_map>
 #include <vector>
@@ -63,7 +62,7 @@ struct NDArrayCacheMetadata {
     };
 
     /*! \brief Relative path to the bin file */
-    std::filesystem::path data_path;
+    std::string data_path;
     /*! \brief Format of the file */
     std::string format;
     /*! \brief Size of the file */
@@ -73,9 +72,11 @@ struct NDArrayCacheMetadata {
   };
   /*! \brief The files in the NDArray cache */
   std::vector<FileRecord> records;
+  /*! \brief The path to the `ndarray-cache.json` file */
+  std::string path;
 
   /*! \brief Load the metadata from a specific path */
-  static NDArrayCacheMetadata LoadFromFile(const std::filesystem::path& path);
+  static NDArrayCacheMetadata LoadFromStr(const std::string& json_str, const 
std::string& path);
 };
 
 /*!
@@ -83,7 +84,7 @@ struct NDArrayCacheMetadata {
  * \param path Path to the file to be loaded
  * \return Mapping from parameter name to its shard dim
  */
-std::unordered_map<std::string, int> LoadShardInfoFromFile(const 
std::filesystem::path& path);
+std::unordered_map<std::string, int> LoadShardInfoFromStr(const std::string& 
json_str);
 
 }  // namespace relax_vm
 }  // namespace runtime

Reply via email to