Skip to content

Instantly share code, notes, and snippets.

@cedricvidal
Last active May 19, 2024 18:17
Show Gist options
  • Save cedricvidal/12168291843393cc8d2dd15f8372259e to your computer and use it in GitHub Desktop.
Save cedricvidal/12168291843393cc8d2dd15f8372259e to your computer and use it in GitHub Desktop.
Huggingface hack to prevent fingerprint issues with functions
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