-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathlru_cache.h
More file actions
100 lines (82 loc) · 2.73 KB
/
Copy pathlru_cache.h
File metadata and controls
100 lines (82 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// clang-format off
/*
* SPDX-FileCopyrightText: Copyright (c) 2025-present NVIDIA CORPORATION & AFFILIATES.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
*/
// clang-format on
#pragma once
#include <fusion.h>
#include <runtime/fusion_executor_cache.h>
#include <runtime/fusion_kernel_runtime.h>
#include <iostream>
#include <list>
#include <optional>
#include <unordered_map>
namespace nvfuser::python {
class LRUCache {
public:
explicit LRUCache(size_t max_fusions) : max_fusions_(max_fusions) {
NVF_ERROR_GT(max_fusions_, 0, "Max fusions must be greater than 0");
}
//! Copy and Assignment of the LRUCache is not supported
LRUCache(const LRUCache&) = delete;
LRUCache& operator=(const LRUCache&) = delete;
// Create a FusionExecutorCache for given fusion.
// If the fusion is already in the cache, it will be moved to the front of the
// cache and returned immediately.
// If the cache is full, the least recently used fusion will be evicted.
FusionExecutorCache* cacheCompile(std::unique_ptr<Fusion> fusion);
// Print stats about LRU Cache
std::string stats() const;
// Get the number of fusions cached
size_t numFusions() const {
std::lock_guard<std::mutex> guard(lru_mutex_);
return items_list.size();
}
private:
// Item is a tuple of key, value, and visits per key
struct Item {
Fusion* fusion;
std::unique_ptr<FusionExecutorCache> executor_cache;
size_t visits;
};
// Custom Hasher Functor for Fusion
struct FusionHasher {
size_t operator()(const Fusion* fusion) const {
return fusion->hash();
}
};
// Custom Equality Functor for Fusion
struct FusionEqualTo {
bool operator()(const Fusion* lhs, const Fusion* rhs) const {
return lhs->sameDefinition(*rhs);
}
};
// Cumulative number of fusions compiled
inline size_t numFusionsCompiled() const {
return num_cache_lookups_ - num_cache_hits_;
}
// The list stores key-value pairs to maintain the order of use.
// Front of the list is the most recently used.
std::list<Item> items_list;
// TODO Replace std::unordered_map with absl::flat_hash_map
// Reference: Use https://abseil.io/docs/cpp/guides/container
// The map provides O(1) access to list nodes.
// It stores keys and iterators to the corresponding pairs in the list.
std::unordered_map<
Fusion*,
typename std::list<Item>::iterator,
FusionHasher,
FusionEqualTo>
items_map;
//! The max allowed number of fusions in the cache
size_t max_fusions_ = 0;
// Number of visits to the cache
size_t num_cache_lookups_ = 0;
// Number of cache hits
size_t num_cache_hits_ = 0;
// Mutex for multi-threaded safety
mutable std::mutex lru_mutex_;
};
} // namespace nvfuser::python