gemini-code-assist[bot] commented on code in PR #18784:
URL: https://github.com/apache/tvm/pull/18784#discussion_r2809766116
##########
src/runtime/contrib/dnnl/dnnl_json_runtime.cc:
##########
@@ -799,7 +799,7 @@ class DNNLJSONRuntime : public JSONRuntimeBase {
template <typename T>
const T GetNodeAttr(const json::JSONGraphNode& node, std::string name,
std::vector<std::string> def = {}) {
- auto attr = node.HasAttr(name) ?
node.GetAttr<std::vector<std::string>>(name) : def;
+ auto attr = node.HasAttr(name) ?
node.GetAttr<ffi::Array<ffi::String>>(name) : def;
Review Comment:

This change introduces a compilation error. The ternary operator on this
line has incompatible types for its branches:
`node.GetAttr<ffi::Array<ffi::String>>(name)` returns an
`ffi::Array<ffi::String>`, while `def` is a `std::vector<std::string>`.
Furthermore, `AttrConvert` is called with `attr` which would be an
`ffi::Array<ffi::String>`, but `AttrConvert` expects a
`std::vector<std::string>`.
A possible fix is to handle the two cases separately and convert the
`ffi::Array<ffi::String>` to a `std::vector<std::string>` before calling
`AttrConvert`:
```cpp
template <typename T>
const T GetNodeAttr(const json::JSONGraphNode& node, std::string name,
std::vector<std::string> def = {}) {
if (node.HasAttr(name)) {
auto attr_array = node.GetAttr<ffi::Array<ffi::String>>(name);
std::vector<std::string> attr_vec(attr_array.begin(), attr_array.end());
return AttrConvert<T>(attr_vec);
}
return AttrConvert<T>(def);
}
```
##########
include/tvm/support/serializer.h:
##########
@@ -187,6 +190,78 @@ struct Serializer<std::unordered_map<K, V>> {
}
};
+// ---- ffi::String ----
+template <>
+struct Serializer<ffi::String> {
+ static constexpr bool enabled = true;
+
+ static void Write(Stream* strm, const ffi::String& data) {
+ Serializer<std::string>::Write(strm, std::string(data));
+ }
Review Comment:

The conversion from `ffi::String` to `std::string` creates a temporary copy.
This can be avoided by directly serializing the contents of the `ffi::String`,
similar to how `Serializer<std::string>` is implemented.
```c
static void Write(Stream* strm, const ffi::String& data) {
uint64_t sz = static_cast<uint64_t>(data.size());
Serializer<uint64_t>::Write(strm, sz);
if (sz != 0) {
strm->Write(data.data(), data.size());
}
}
```
--
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]