Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure that while caching values we don't invoke any Aten operator #99050

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 17 additions & 10 deletions torch/csrc/jit/serialization/flatbuffer_serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,18 +185,23 @@ class FlatbufferSerializer {
// but without relying on aten::nonzero operator being present in the
// binary.
bool operator()(const IValue& lhs, const IValue& rhs) const {
// The only case we don't return bool is for tensor comparison. Lets do
// pointer comparison here.
if (lhs.isTensor() || rhs.isTensor()) {
if (lhs.isTensor() && rhs.isTensor()) {
return (&lhs.toTensor()) == (&rhs.toTensor());
}
return false;
}
IValue eq = lhs.equals(rhs);
if (eq.isBool()) {
return eq.toBool();
}
// The only case we don't return bool is for tensor comparison. Lets do
// pointer comparison here.
return (&lhs.toTensor()) == (&rhs.toTensor());
return false;
}
};

std::unordered_map<IValue, uint32_t, IValueHash, IValueEqual> cached_ivalues_;

const mobile::CompilationUnit* mcu_ = nullptr;
};

Expand Down Expand Up @@ -678,18 +683,20 @@ uint32_t FlatbufferSerializer::storeIValueAndGetIndex(
if (iter != cached_ivalues_.end()) {
return iter->second;
}
} catch (const std::runtime_error&) {
// Threw if ivalue is not hashable
} catch (const c10::Error&) {
// Threw if ivalue is don't have proper operator==
} catch (...) {
// Threw if ivalue is not hashable or
// if ivalue is don't have proper operator==
// we don't care catchall because either case we want to skip hashing
}

auto offset = iValueToFB(fbb, ivalue);
uint32_t index = insertIValue(offset);
try {
cached_ivalues_[ivalue] = index;
} catch (const std::runtime_error&) {
} catch (const c10::Error&) {
} catch (...) {
// Threw if ivalue is not hashable or
// if ivalue is don't have proper operator==
// we don't care catchall because either case we want to skip hashing
}

return index;
Expand Down