clams.backends package¶
Optional model-backend helpers for CLAMS apps.
Each backend is a separate submodule. Heavy dependencies (e.g.,
torch, transformers) are NOT pulled in by the base
clams-python install; users opt in via pip extras such as
pip install clams-python[hf] for the HuggingFace transformers
backend.
Submodules¶
clams.backends.hf¶
HuggingFace transformers backend helpers.
Two general loaders that wrap the device / kwargs / inference-mode boilerplate every HF-backed CLAMS app does identically:
load_hf_model()–from_pretrained()flow for any model class (instruction-tuned LLMs/VLMs, encoder-only classifiers, vision/audio feature extractors, etc.). Use when the app needs raw access to the underlying model and processor.load_hf_pipeline()– task-leveltransformers.pipeline()flow (ASR, NER, text classification, zero-shot, etc.). Use when pipeline-level inference is sufficient.
torch and transformers are optional dependencies. Install them
via the [hf] extra:
pip install clams-python[hf]
Imports are lazy: this module can be referenced from
clams.app without triggering an ImportError on a base
clams-python install. The ImportError only fires when a
loader is actually called without the extras.
- clams.backends.hf.load_hf_model(model_id: str, model_cls, processor_cls=None, dtype=None, device: str | None = None, padding_side: str | None = None, revision: str | None = None, model_kwargs: dict | None = None, processor_kwargs: dict | None = None, move_to_device: bool = True) Tuple[Any, Any, str][source]¶
Load a HuggingFace
transformersmodel viafrom_pretrainedand return it ready for inference.- Parameters:
model_id – HuggingFace model identifier (e.g., a Hub repo name or a local path) forwarded to
from_pretrained.model_cls – a
transformersmodel class (e.g.,AutoModelForCausalLM,AutoModelForImageTextToText,ConvNextV2Model,ViTModel, …). Whatever supportsfrom_pretrained().processor_cls – a processor / tokenizer / feature-extractor class with
from_pretrained(). Defaults totransformers.AutoProcessor. Passtransformers.AutoTokenizer,transformers.AutoImageProcessor, etc. for narrower cases. PassNoneexplicitly to skip processor loading entirely (the returnedprocessorin that case isNone).dtype – torch dtype for the model (e.g.,
torch.bfloat16). WhenNone(default), notorch_dtypekwarg is forwarded tofrom_pretrained– the model class uses its own default (typically float32). Set explicitly for low-precision LLM inference.device – target device string (e.g.,
'cuda','cpu','cuda:0'). WhenNone(default), the helper auto-detects cuda availability and falls back to cpu.padding_side – if set (typically
'left'for decoder-only models doing batched generation), the helper configures the underlying tokenizer’spadding_sideand – when no pad token is set – uses the EOS token as the pad token. LeaveNonefor encoder / non-batched cases (the tokenizer’s own default is preserved).revision – optional Git revision (commit hash, branch name, or tag) on the Hub repository to pin the download to. When set, forwarded as
revision=...to bothmodel_cls.from_pretrainedandprocessor_cls.from_pretrained, ensuring the model and processor are loaded from the same commit. Strongly recommended for production: pinning a commit hash makes the analyzer artifact reproducible and immune to upstream silent updates. Apps calling this helper directly should record the same hash onanalyzer_version(oranalyzer_versions) inmetadata.pyso the output MMIF identifies the exact artifact. Apps inheriting fromClamsHFPromptableAppdo not call this helper – the base class readsanalyzer_versionsfrom the app metadata and forwards the resolved revision automatically.model_kwargs – extra kwargs forwarded to
model_cls.from_pretrained()(e.g.,{'use_safetensors': True, 'add_pooling_layer': False}).processor_kwargs – extra kwargs forwarded to
processor_cls.from_pretrained()(e.g.,{'use_safetensors': True, 'use_fast': True}).move_to_device – when
True(default), the helper moves the loaded model to the resolved device and switches it toeval()mode – the right behavior for a “ready for inference” app loader. WhenFalse, both steps are skipped; the model is returned in the statefrom_pretrainedleft it (on CPU, in train mode). UseFalsefor library-style HF wrappers that defer device placement and inference-mode switching to a downstream consumer (e.g. an extractor class that may be combined with a head and only then placed on a device by the wrapping classifier). The returneddeviceis still the resolved target, so the consumer can use it later for its own.to(device)call.
- Returns:
(processor, model, device)tuple.processoris the loaded processor/tokenizer/feature-extractor (orNoneifprocessor_clswas explicitly set toNone).deviceis the resolved device string (the model was moved there iffmove_to_device=True).- Return type:
Tuple[Any, Any, str]
- Raises:
ImportError – if
torchortransformersis not installed. Install the[hf]extra to fix.
- clams.backends.hf.load_hf_pipeline(task: str, model_id: str, device: str | int | None = None, revision: str | None = None, model_kwargs: dict | None = None, pipeline_kwargs: dict | None = None) Tuple[Any, str | int][source]¶
Load a HuggingFace
transformers.pipeline()fortaskand return it ready for inference. Wraps the device / revision / kwargs-forwarding boilerplate that every pipeline-backed CLAMS app does identically. Use this for apps wrapping a task-level pipeline (ASR via"automatic-speech-recognition", NER via"token-classification", text classification, zero-shot, etc.); useload_hf_model()instead when the app needs raw access to the underlying model / processor (e.g., for custom chat-template formatting or batchedgeneratecalls).- Parameters:
task – pipeline task string forwarded to
transformers.pipeline()(e.g.,"automatic-speech-recognition","token-classification").model_id – HuggingFace model identifier (Hub repo name or local path) forwarded to
pipeline(model=...).device – target device. Accepts the string form (
'cuda','cpu','cuda:0') for parity withload_hf_model(), or the integer form accepted natively bypipeline(-1for CPU,0+for GPU index). WhenNone(default), auto-detects cuda availability and falls back to cpu (string form).revision – optional Git revision (commit hash, branch, or tag) on the Hub to pin the download to. Strongly recommended for production; see
load_hf_model()for rationale.model_kwargs – extra kwargs forwarded to the underlying
model.from_pretrained()via thepipeline(model_kwargs={...})channel.pipeline_kwargs – extra kwargs forwarded directly to
transformers.pipeline()(e.g.generate_kwargs,tokenizer,feature_extractor,batch_size,framework).model,task,device,revision, andmodel_kwargsare owned by this helper – explicit helper args take precedence if any collide.
- Returns:
(pipeline, device)tuple.deviceis the resolved device the pipeline is on, in the form it was passed (or the auto-resolved string form whendevice=None).- Return type:
- Raises:
ImportError – if
torchortransformersis not installed. Install the[hf]extra to fix.