Skip to content

Commit 5242b7b

Browse files
committed
[hf] factor model loading from HuggingFace model provider
1 parent 3099982 commit 5242b7b

1 file changed

Lines changed: 14 additions & 10 deletions

File tree

src/krnel/graph/runners/local_runner/model_registry_implementations.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -246,18 +246,9 @@ def get_llm_output_logits(self, runner, op: LLMLogitScoresOp):
246246
class HuggingFaceProvider(ModelProvider):
247247
"""Provider for HuggingFace Transformers models."""
248248

249-
def _process_batches(self, log, inputs, op, output_hidden_states):
250-
import torch
249+
def _load_model(self, op: LLMLayerActivationsOp | LLMLogitScoresOp):
251250
from transformers import AutoModelForCausalLM, AutoTokenizer
252-
log = log.bind(device=op.device)
253-
254-
if isinstance(op.text, (JSONColumnType, ConversationColumnType)) and not op.apply_chat_template:
255-
raise ValueError("HuggingFaceProvider requires apply_chat_template=True when using JSONColumnType for 'text'.")
256-
257-
# Load model and tokenizer
258251
_, model_name = get_model_provider(op.model_name)
259-
log = log.bind(model_name=model_name)
260-
log.info("loading HuggingFace model")
261252
tokenizer = AutoTokenizer.from_pretrained(model_name)
262253
model = AutoModelForCausalLM.from_pretrained(
263254
model_name,
@@ -266,6 +257,19 @@ def _process_batches(self, log, inputs, op, output_hidden_states):
266257
)
267258
# note: there is a difference between from_pretrained(torch_dtype='float16') and model.half()
268259
model.eval()
260+
return model
261+
262+
def _process_batches(self, log, inputs, op, output_hidden_states):
263+
import torch
264+
log = log.bind(device=op.device)
265+
266+
if isinstance(op.text, (JSONColumnType, ConversationColumnType)) and not op.apply_chat_template:
267+
raise ValueError("HuggingFaceProvider requires apply_chat_template=True when using JSONColumnType for 'text'.")
268+
269+
# Load model and tokenizer
270+
log = log.bind(model_name=op.model_name)
271+
log.info("loading HuggingFace model")
272+
model = self._load_model(op)
269273

270274
if op.torch_compile:
271275
model.compile(backend='eager')

0 commit comments

Comments
 (0)