Skip to content

huggingface

huggingface

ID_CACHE module-attribute

ID_CACHE = {}

HuggingFaceModel

HuggingFaceModel(repo_id: str, *args, revision: Optional[str] = None, import_edits: Union[bool, str] = False, **kwargs)

Bases: RemoteableMixin

Base class for NNsight wrappers around HuggingFace Hub models.

Adds HuggingFace repository handling (repo ID, revision) and persistent edit export/import on top of :class:RemoteableMixin.

PARAMETER DESCRIPTION
repo_id

HuggingFace repository ID (e.g. "openai-community/gpt2") or a pre-loaded torch.nn.Module.

TYPE: str

*args

Forwarded to the parent mixin chain.

DEFAULT: ()

revision

Git revision (branch, tag, or commit hash) of the model repository. Defaults to None (latest).

TYPE: Optional[str] DEFAULT: None

import_edits

If True, import previously exported edits using the default variant. If a string, use it as the variant name. Defaults to False.

TYPE: Union[bool, str] DEFAULT: False

**kwargs

Forwarded to the parent mixin chain and ultimately to the model loading function.

DEFAULT: {}

ATTRIBUTE DESCRIPTION
repo_id

The HuggingFace repository ID.

TYPE: str

revision

The repository revision.

TYPE: Optional[str]

repo_id instance-attribute

repo_id = repo_id if isinstance(repo_id, str) else getattr(repo_id, 'name_or_path', None)

revision instance-attribute

revision = revision

export_edits

export_edits(name: Optional[str] = None, export_dir: Optional[str] = None, variant: str = '__default__')

Export persistent model edits to disk.

Edits created via model.edit(inplace=True) are serialized and saved so they can be reloaded later with :meth:import_edits.

PARAMETER DESCRIPTION
name

Export name. Defaults to a name derived from the HuggingFace repo ID.

TYPE: Optional[str] DEFAULT: None

export_dir

Directory to save exports. Defaults to the HuggingFace cache under nnsight/exports.

TYPE: Optional[str] DEFAULT: None

variant

Named variant for this set of edits. Defaults to '__default__'.

TYPE: str DEFAULT: '__default__'

import_edits

import_edits(name: Optional[str] = None, export_dir: Optional[str] = None, variant: str = '__default__')

Import previously exported model edits from disk.

Loads edits that were saved with :meth:export_edits and applies them as persistent in-place edits on this model.

PARAMETER DESCRIPTION
name

Export name. Defaults to a name derived from the HuggingFace repo ID.

TYPE: Optional[str] DEFAULT: None

export_dir

Directory to load exports from. Defaults to the HuggingFace cache under nnsight/exports.

TYPE: Optional[str] DEFAULT: None

variant

Named variant to load. Defaults to '__default__'.

TYPE: str DEFAULT: '__default__'