boryasbora commited on
Commit
cdd1ac6
·
verified ·
1 Parent(s): b53f0c6

Update huggingface_llm.py

Browse files
Files changed (1) hide show
  1. huggingface_llm.py +9 -7
huggingface_llm.py CHANGED
@@ -9,18 +9,20 @@ class HuggingFaceLLM(LLM):
9
  model_id: str = Field(..., description="Hugging Face model ID")
10
  temperature: float = Field(default=0.7, description="Sampling temperature")
11
  max_tokens: int = Field(default=256, description="Maximum number of tokens to generate")
 
 
12
  _model: Any = PrivateAttr()
13
  _tokenizer: Any = PrivateAttr()
14
- # _device: str = PrivateAttr()
15
 
16
  def __init__(self, **kwargs):
17
  super().__init__(**kwargs)
18
- # self._device = "cpu"
 
19
  self._load_model()
20
 
21
  def _load_model(self):
22
  self._tokenizer = AutoTokenizer.from_pretrained(self.model_id)
23
- self._model = AutoModelForCausalLM.from_pretrained(self.model_id)
24
 
25
  @property
26
  def _llm_type(self) -> str:
@@ -33,8 +35,8 @@ class HuggingFaceLLM(LLM):
33
  run_manager: Optional[CallbackManagerForLLMRun] = None,
34
  **kwargs: Any,
35
  ) -> str:
36
- input_ids = self._tokenizer.encode(prompt, return_tensors="pt")
37
-
38
  with torch.no_grad():
39
  output = self._model.generate(
40
  input_ids,
@@ -43,10 +45,10 @@ class HuggingFaceLLM(LLM):
43
  do_sample=True,
44
  pad_token_id=self._tokenizer.eos_token_id
45
  )
46
-
47
  response = self._tokenizer.decode(output[0], skip_special_tokens=True)
48
  return response[len(prompt):].strip()
49
 
50
  @property
51
  def _identifying_params(self) -> Dict[str, Any]:
52
- return {"model_id": self.model_id, "temperature": self.temperature, "max_tokens": self.max_tokens}
 
9
  model_id: str = Field(..., description="Hugging Face model ID")
10
  temperature: float = Field(default=0.7, description="Sampling temperature")
11
  max_tokens: int = Field(default=256, description="Maximum number of tokens to generate")
12
+ device: str = Field(default="cpu", description="Device to run the model on")
13
+
14
  _model: Any = PrivateAttr()
15
  _tokenizer: Any = PrivateAttr()
 
16
 
17
  def __init__(self, **kwargs):
18
  super().__init__(**kwargs)
19
+ if self.device == "cpu":
20
+ self.device = "cuda" if torch.cuda.is_available() else "cpu"
21
  self._load_model()
22
 
23
  def _load_model(self):
24
  self._tokenizer = AutoTokenizer.from_pretrained(self.model_id)
25
+ self._model = AutoModelForCausalLM.from_pretrained(self.model_id).to(self.device)
26
 
27
  @property
28
  def _llm_type(self) -> str:
 
35
  run_manager: Optional[CallbackManagerForLLMRun] = None,
36
  **kwargs: Any,
37
  ) -> str:
38
+ input_ids = self._tokenizer.encode(prompt, return_tensors="pt").to(self.device)
39
+
40
  with torch.no_grad():
41
  output = self._model.generate(
42
  input_ids,
 
45
  do_sample=True,
46
  pad_token_id=self._tokenizer.eos_token_id
47
  )
48
+
49
  response = self._tokenizer.decode(output[0], skip_special_tokens=True)
50
  return response[len(prompt):].strip()
51
 
52
  @property
53
  def _identifying_params(self) -> Dict[str, Any]:
54
+ return {"model_id": self.model_id, "temperature": self.temperature, "max_tokens": self.max_tokens, "device": self.device}