Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@ ops:
- pyg_lib/csrc/ops/**/*
- pyg_lib/ops/**/*

classes:
- pyg_lib/csrc/classes/**/*
- pyg_lib/classes/**/*

utils:
- pyg_lib/csrc/utils/**/*
20 changes: 14 additions & 6 deletions pyg_lib/csrc/classes/cpu/hash_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ struct CPUHashMap : torch::CustomClassHolder {
variant<bool, uint8_t, int8_t, int16_t, int32_t, int64_t, float, double>;

CPUHashMap(const at::Tensor& key) {
// TODO Assert 1-dim
at::TensorArg key_arg{key, "key", 0};
at::CheckedFrom c{"HashMap.init"};
at::checkDeviceType(c, key, at::DeviceType::CPU);
at::checkDim(c, key_arg, 1);
at::checkContiguous(c, key_arg);

// clang-format off
AT_DISPATCH_ALL_TYPES_AND(at::ScalarType::Bool,
Expand All @@ -20,15 +24,19 @@ struct CPUHashMap : torch::CustomClassHolder {
[&] {
const auto key_data = key.data_ptr<scalar_t>();
for (int64_t i = 0; i < key.numel(); ++i) {
// TODO Check that key does not yet exist.
map_[key_data[i]] = i;
auto [iterator, inserted] = map_.insert({key_data[i], i});
TORCH_CHECK(inserted, "Found duplicated key.");
}
});
// clang-format on
};

at::Tensor get(const at::Tensor& query) {
// TODO Assert 1-dim
at::TensorArg query_arg{query, "query", 0};
at::CheckedFrom c{"HashMap.get"};
at::checkDeviceType(c, query, at::DeviceType::CPU);
at::checkDim(c, query_arg, 1);
at::checkContiguous(c, query_arg);

const auto options = at::TensorOptions().dtype(at::kLong);
const auto out = at::empty({query.numel()}, options);
Expand All @@ -42,8 +50,8 @@ struct CPUHashMap : torch::CustomClassHolder {
const auto query_data = query.data_ptr<scalar_t>();

for (size_t i = 0; i < query.numel(); ++i) {
// TODO Insert -1 if key does not exist.
out_data[i] = map_[query_data[i]];
auto it = map_.find(query_data[i]);
out_data[i] = (it != map_.end()) ? it->second : -1;
}
});
// clang-format on
Expand Down
4 changes: 2 additions & 2 deletions test/csrc/classes/test_hash_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ TEST(CPUHashMapTest, BasicAssertions) {

auto map = pyg::classes::CPUHashMap(key);

auto query = at::tensor({30, 10, 20}, options);
auto expected = at::tensor({2, 1, 3}, options);
auto query = at::tensor({30, 10, 20, 40}, options);
auto expected = at::tensor({2, 1, 3, -1}, options);
EXPECT_TRUE(at::equal(map.get(query), expected));
}
Loading