Skip to content
This repository was archived by the owner on Apr 28, 2023. It is now read-only.

Commit 321f07e

Browse files
author
Theodoros Theodoridis
committed
Fix cuda cache same key different value check
An attemp to cache a kernel that already exists in the cache with the same key (options, input shapes, ...) now properly throws if the stored values (block/grid sizes, cuda code, ...) are different
1 parent 5e076a7 commit 321f07e

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

tc/core/cuda/cuda_compilation_cache.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,9 @@ void CudaCache::cacheKernel(CudaCachedEntry&& entry) {
198198
entry.key.inputs,
199199
entry.key.outputs);
200200
if (retrievedEntry) {
201-
if (retrievedEntry->values.cudaSource == entry.values.cudaSource or
202-
retrievedEntry->values.grid == entry.values.grid or
203-
retrievedEntry->values.block == entry.values.block) {
201+
if (retrievedEntry->values.cudaSource != entry.values.cudaSource or
202+
retrievedEntry->values.grid != entry.values.grid or
203+
retrievedEntry->values.block != entry.values.block) {
204204
throw CacheEntrySameKeyDifferentValue(
205205
"CudaCache::CacheKernel: a kernel matching the id, options and "
206206
"inputs was previously cached with different cuda source or block "

tc/core/cuda/cuda_mapping_options.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ class CudaDim {
105105
return view == other.view;
106106
}
107107

108+
inline bool operator!=(const CudaDim& other) const {
109+
return not(*this == other);
110+
}
111+
108112
private:
109113
CudaDimProto ownedProto_;
110114

@@ -122,6 +126,7 @@ class Block : public CudaDim {
122126
Block(std::vector<uint64_t> il) : CudaDim(il) {}
123127

124128
using CudaDim::operator=;
129+
using CudaDim::operator!=;
125130
};
126131

127132
/// Specializing CudaDim to differentiate between Block and Grid sizes.
@@ -134,6 +139,7 @@ class Grid : public CudaDim {
134139
Grid(std::vector<uint64_t> il) : CudaDim(il) {}
135140

136141
using CudaDim::operator=;
142+
using CudaDim::operator!=;
137143
};
138144

139145
class CudaMappingOptions {

test/cuda/test_compilation_cache.cc

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,42 @@ class CudaCacheTest : public ::testing::Test {
6969
}
7070
};
7171

72+
TEST_F(CudaCacheTest, EntrySameKeyDifferentValue) {
73+
auto options = tc::CudaMappingOptions::makeNaiveCudaMappingOptions();
74+
auto inputPtrs = InputPtrs();
75+
auto outputPtrs = InputPtrs();
76+
77+
tc::CudaCache::getCache()->cacheKernel(tc::CudaCachedEntry(
78+
"kernel0",
79+
"ker000",
80+
{0, 0, 1},
81+
tc::Grid(std::vector<size_t>{1, 1, 1}),
82+
tc::Block(std::vector<size_t>{1, 1, 1}),
83+
options,
84+
inputPtrs,
85+
outputPtrs,
86+
"source0",
87+
tc::CudaGPUInfo::GPUInfo().GetCudaDeviceStr()));
88+
ASSERT_THROW(
89+
tc::CudaCache::getCache()->cacheKernel(tc::CudaCachedEntry(
90+
"kernel0",
91+
"ker000",
92+
{0, 0, 1},
93+
tc::Grid(std::vector<size_t>{2, 1, 1}),
94+
tc::Block(std::vector<size_t>{1, 2, 1}),
95+
options,
96+
inputPtrs,
97+
outputPtrs,
98+
"source1",
99+
tc::CudaGPUInfo::GPUInfo().GetCudaDeviceStr())),
100+
tc::CacheEntrySameKeyDifferentValue);
101+
102+
ASSERT_EQ(tc::CudaCache::getCache()->size(), 1);
103+
ASSERT_EQ(tc::CudaCache::getCache()->numberAttemptedRetrievals, 0);
104+
ASSERT_EQ(tc::CudaCache::getCache()->numberSuccessfulRetrievals, 0);
105+
ASSERT_EQ(tc::CudaCache::getCache()->numberCacheAttemps, 2);
106+
}
107+
72108
TEST_F(CudaCacheTest, DifferentIDs) {
73109
auto options = tc::CudaMappingOptions::makeNaiveCudaMappingOptions();
74110
auto inputPtrs = InputPtrs();

0 commit comments

Comments
 (0)