s42chen's picture
Publish 11 evaluated Astra histories with reproducible checkpoint artifacts (part 51)
32d1b4a verified
Raw History Blame Contribute Delete
963 Bytes
"""Retain bounded compiled attention executables across global JAX cache clears."""
from collections import OrderedDict
import jax
class PersistentCompiled:
def __init__(self,function,max_executables=2):
self.function=jax.jit(function);self.executables=OrderedDict();self.max_executables=max_executables;self.compilations=0
def __call__(self,*args):
leaves,tree=jax.tree_util.tree_flatten(args)
signatures=[]
for value in leaves:
abstract=jax.core.get_aval(value)
signatures.append((abstract.shape,str(abstract.dtype),bool(abstract.weak_type)))
key=(tree,tuple(signatures))
if key not in self.executables:
self.executables[key]=self.function.lower(*args).compile();self.compilations+=1
while len(self.executables)>self.max_executables:self.executables.popitem(last=False)
self.executables.move_to_end(key)
return self.executables[key](*args)