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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent Hash of IValue between aten/src/ATen/core/ivalue.cpp and aten/src/ATen/core/Dict_inl.h #90305

Open
HunterTracer opened this issue Dec 6, 2022 · 1 comment
Labels
oncall: jit Add this issue/PR to JIT oncall triage queue

Comments

@HunterTracer
Copy link

HunterTracer commented Dec 6, 2022

馃悰 Describe the bug

Recently, I'm using libtorch 1.13.0 using C++ in VS 2022. I want to create a torch::Dict, which maps tuple<string, string, string> to tuple<string, string, string>. After I create the tuple key, I use torch::IValue::hash(key) and this works. However, when I want to insert (key, value) into dict, the program raises error in DictKeyHash::operator()(const IValue& ivalue) (the function is in aten/src/ATen/core/Dict_inl.h). It says can't hash IValues.

The reason is that DictKeyHash::operator() only supports to hash int, string, double, complex double, bool, tensor and device. However, the funciton torch::IValue::hash in aten/src/ATen/core/ivalue.cpp supports None, Bool, Double, Tensor, Storage, Int, SymInt, SymFloat, String, Tuple, and Device. There is a conflict between two hash function. The first one cannot hash None, Storage, SymInt, SymFloat, Tuple, and the second one cannot hash complex double.

Is it a bug? Can these hash functions be consistent? Thanks!

Here's my code.

`#include
#include <torch/torch.h>

int main() {
torch::TupleTypePtr pt = torch::TupleType::create({torch::StringType::get(), torch::StringType::get(), torch::StringType::get()});
std::cout << pt->str() << "\n";

c10::Dict<torch::IValue, torch::IValue> dict(pt, pt);
torch::IValue key(std::make_tuple("one", "one", "one"));
std::cout << torch::IValue::hash(key) << "\n";
dict.insert(key, key); // <- will fail in DictKeyHash::operator()
return 0;

}
`

Versions

Windows 10, Visual Studio 2022, C++, libtorch 1.13.0 with cuda

cc @EikanWang @jgong5 @wenzhe-nrv @sanchitintel @jbschlosser

@malfet
Copy link
Contributor

malfet commented Dec 7, 2022

Why do you want/need to use Dict rather than std::unordered_map?

@samdow samdow added oncall: jit Add this issue/PR to JIT oncall triage queue and removed triage review module: cpp Related to C++ API labels Dec 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
oncall: jit Add this issue/PR to JIT oncall triage queue
Projects
None yet
3 participants