Caching
Distributed caching of functions.
pyDVL uses memcached to cache utility values, through pymemcache. This allows sharing evaluations across processes and nodes in a cluster. You can run memcached as a service, locally or remotely, see Setting up the cache
Warning
Function evaluations are cached with a key based on the function's signature and code. This can lead to undesired cache hits, see Cache reuse.
Remember not to reuse utility objects for different datasets.
Configuration¶
Memoization is disabled by default but can be enabled easily, see Setting up the cache. When enabled, it will be added to any callable used to construct a Utility (done with the decorator @memcached). Depending on the nature of the utility you might want to enable the computation of a running average of function values, see Usage with stochastic functions. You can see all configuration options under MemcachedConfig.
Default configuration¶
default_config = dict(
server=('localhost', 11211),
connect_timeout=1.0,
timeout=0.1,
# IMPORTANT! Disable small packet consolidation:
no_delay=True,
serde=serde.PickleSerde(pickle_version=PICKLE_VERSION)
)
Usage with stochastic functions¶
In addition to standard memoization, the decorator memcached() can compute running average and standard error of repeated evaluations for the same input. This can be useful for stochastic functions with high variance (e.g. model training for small sample sizes), but drastically reduces the speed benefits of memoization.
This behaviour can be activated with the argument allow_repeated_evaluations
to memcached().
Cache reuse¶
When working directly with memcached(), it is essential to only cache pure functions. If they have any kind of state, either internal or external (e.g. a closure over some data that may change), then the cache will fail to notice this and the same value will be returned.
When a function is wrapped with memcached() for memoization, its signature (input and output names) and code are used as a key for the cache. Alternatively you can pass a custom value to be used as key with
If you are running experiments with the same Utility but different datasets, this will lead to evaluations of the utility on new data returning old values because utilities only use sample indices as arguments (so there is no way to tell the difference between '1' for dataset A and '1' for dataset 2 from the point of view of the cache). One solution is to empty the cache between runs, but the preferred one is to use a different Utility object for each dataset.
Unexpected cache misses¶
Because all arguments to a function are used as part of the key for the cache,
sometimes one must exclude some of them. For example, If a function is going to
run across multiple processes and some reporting arguments are added (like a
job_id
for logging purposes), these will be part of the signature and make the
functions distinct to the eyes of the cache. This can be avoided with the use of
ignore_args in the configuration.
CacheStats
dataclass
¶
Statistics gathered by cached functions.
ATTRIBUTE | DESCRIPTION |
---|---|
sets |
number of times a value was set in the cache
TYPE:
|
misses |
number of times a value was not found in the cache
TYPE:
|
hits |
number of times a value was found in the cache
TYPE:
|
timeouts |
number of times a timeout occurred
TYPE:
|
errors |
number of times an error occurred
TYPE:
|
reconnects |
number of times the client reconnected to the server
TYPE:
|
serialize(x)
¶
Serialize an object to bytes. Args: x: object to serialize.
RETURNS | DESCRIPTION |
---|---|
bytes
|
serialized object. |
Source code in src/pydvl/utils/caching.py
memcached(client_config=None, time_threshold=0.3, allow_repeated_evaluations=False, rtol_stderr=0.1, min_repetitions=3, ignore_args=None)
¶
Transparent, distributed memoization of function calls.
Given a function and its signature, memcached uses a distributed cache that, for each set of inputs, keeps track of the average returned value, with variance and number of times it was calculated.
If the function is deterministic, i.e. same input corresponds to the same
exact output, set allow_repeated_evaluations
to False
. If instead the
function is stochastic (like the training of a model depending on random
initializations), memcached() allows to set a minimum number of evaluations
to compute a running average, and a tolerance after which the function will
not be called anymore. In other words, the function will be recomputed
until the value has stabilized with a standard error smaller than
rtol_stderr * running average
.
Warning
Do not cache functions with state! See Cache reuse
PARAMETER | DESCRIPTION |
---|---|
client_config |
configuration for pymemcache's Client. Will be merged on top of the default configuration (see below).
TYPE:
|
time_threshold |
computations taking less time than this many seconds are not cached.
TYPE:
|
allow_repeated_evaluations |
If
TYPE:
|
rtol_stderr |
relative tolerance for repeated evaluations. More precisely,
memcached() will stop evaluating the function once the
standard deviation of the mean is smaller than
TYPE:
|
min_repetitions |
minimum number of times that a function evaluation on the same arguments is repeated before returning cached values. Useful for stochastic functions only. If the model training is very noisy, set this number to higher values to reduce variance.
TYPE:
|
ignore_args |
Do not take these keyword arguments into account when
hashing the wrapped function for usage as key in memcached. This allows
sharing the cache among different jobs for the same experiment run if
the callable happens to have "nuisance" parameters like |
RETURNS | DESCRIPTION |
---|---|
Callable[[Callable[..., T], bytes | None], Callable[..., T]]
|
A wrapped function |
Source code in src/pydvl/utils/caching.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
|
Created: 2023-09-02