Last active
May 19, 2024 18:17
-
-
Save cedricvidal/12168291843393cc8d2dd15f8372259e to your computer and use it in GitHub Desktop.
Huggingface hack to prevent fingerprint issues with functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Any | |
def constant_hash(func): | |
""" | |
Wraps a function and changes its hashing behavior to a constant. | |
This allows Hugginface to only use functiona parameters for hashing and nothing else in | |
the function's captured context | |
""" | |
return ConstantHash(func) | |
class ConstantHash: | |
def __init__(self, f, id = None): | |
self.f = f | |
if not id: | |
id = f.__name__ | |
self.id = id | |
def __call__(self, *args, **kwargs): | |
return self.f(*args, **kwargs) | |
def __reduce__(self) -> str | tuple[Any, ...]: | |
return (self.__class__, (self.id,)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment