Ubospica commented on code in PR #18759:
URL: https://github.com/apache/tvm/pull/18759#discussion_r2796024196
##########
src/runtime/vm/tensor_cache_support.cc:
##########
@@ -54,89 +50,76 @@ namespace tvm {
namespace runtime {
namespace vm {
-template <typename ExpectedType>
-inline ExpectedType AsType(const picojson::value& json) {
- ICHECK(json.is<ExpectedType>());
- return json.get<ExpectedType>();
-}
-
-template <typename ValueType>
-inline ValueType GetValue(const picojson::object& json, const std::string&
key) {
- return AsType<ValueType>(json.at(key));
-}
+namespace json = tvm::ffi::json;
-TensorCacheMetadata::FileRecord::ParamRecord JSONAsParamRecord(const
picojson::object& json) {
+TensorCacheMetadata::FileRecord::ParamRecord JSONAsParamRecord(const
json::Object& json) {
std::vector<ffi::Shape::index_type> shape;
{
- picojson::array shape_json = GetValue<picojson::array>(json, "shape");
+ json::Array shape_json = json[ffi::String("shape")].cast<json::Array>();
shape.reserve(shape_json.size());
- for (const picojson::value& d : shape_json) {
- shape.push_back(AsType<int64_t>(d));
+ for (const ffi::Any& d : shape_json) {
+ shape.push_back(d.cast<int64_t>());
}
}
TensorCacheMetadata::FileRecord::ParamRecord result;
- std::string dtype = GetValue<std::string>(json, "dtype");
- result.name = GetValue<std::string>(json, "name");
+ std::string dtype =
std::string(json[ffi::String("dtype")].cast<ffi::String>());
Review Comment:
```suggestion
std::string dtype = json["dtype"].cast<ffi::String>());
```
Ditto, can we just put string inside []?
##########
src/runtime/disco/loader.cc:
##########
@@ -56,57 +54,46 @@ struct ShardInfo {
std::vector<ShardFunc> funcs;
};
-template <typename ExpectedType>
-inline ExpectedType AsType(const picojson::value& json) {
- ICHECK(json.is<ExpectedType>());
- return json.get<ExpectedType>();
-}
-
-template <typename ValueType>
-inline ValueType GetValue(const picojson::object& json, const std::string&
key) {
- return AsType<ValueType>(json.at(key));
-}
-
std::unordered_map<std::string, ShardInfo> LoadShardInfoFromStr(const
std::string& json_str);
-ShardInfo::TensorInfo LoadTensorInfoFromJSON(const picojson::array&
json_tensor_info) {
+
+ShardInfo::TensorInfo LoadTensorInfoFromJSON(const json::Array&
json_tensor_info) {
CHECK_EQ(json_tensor_info.size(), 2) << "ValueError: Invalid tensor info
JSON";
- picojson::array shape_json = AsType<picojson::array>(json_tensor_info[0]);
+ json::Array shape_json = json_tensor_info[0].cast<json::Array>();
int ndim = shape_json.size();
std::vector<int64_t> shape;
shape.reserve(ndim);
for (int i = 0; i < ndim; ++i) {
- shape.push_back(AsType<int64_t>(shape_json[i]));
+ shape.push_back(shape_json[i].cast<int64_t>());
}
- std::string dtype = AsType<std::string>(json_tensor_info[1]);
+ std::string dtype = std::string(json_tensor_info[1].cast<ffi::String>());
Review Comment:
Since we have implicit conversion from std::string to tvm::ffi::string and
vice versa, can we just use
```
std::string dtype = json_tensor_info[1].cast<ffi::String>();
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]