Chris4K commited on
Commit
fb159fa
·
verified ·
1 Parent(s): 0477e0c

Update services/strategy.py

Browse files
Files changed (1) hide show
  1. services/strategy.py +7 -7
services/strategy.py CHANGED
@@ -49,17 +49,17 @@ class BestOfN(GenerationStrategy):
49
  def generate(self, generator: 'BaseGenerator', prompt: str, model_kwargs: Dict[str, Any], num_samples: int = 5, **kwargs) -> str:
50
  scored_outputs = []
51
  for _ in range(num_samples):
52
- input_ids = generator.tokenizer(prompt, return_tensors="pt").input_ids.to(self.device)
53
  output = generator.generate(input_ids, **model_kwargs)
54
  response =generator.tokenizer.decode(output[0], skip_special_tokens=True)
55
- score = generator.prm_model(**generator.tokenizer(response, return_tensors="pt").to(self.device)).logits.mean().item()
56
  scored_outputs.append((response, score))
57
  return max(scored_outputs, key=lambda x: x[1])[0]
58
 
59
 
60
  class BeamSearch(GenerationStrategy):
61
  def generate(self, generator: 'BaseGenerator', prompt: str, model_kwargs: Dict[str, Any], num_samples: int = 5, **kwargs) -> str:
62
- input_ids = generator.tokenizer(prompt, return_tensors="pt").input_ids.to(self.device)
63
  outputs = generator.generate(
64
  input_ids,
65
  num_beams=num_samples,
@@ -73,19 +73,19 @@ class DVT(GenerationStrategy):
73
  def generate(self, generator: 'BaseGenerator', prompt: str, model_kwargs: Dict[str, Any], num_samples: int = 5, **kwargs) -> str:
74
  results = []
75
  for _ in range(breadth):
76
- input_ids = generator.tokenizer(prompt, return_tensors="pt").input_ids.to(self.device)
77
  output = generator.generate(input_ids, **model_kwargs)
78
  response = generator.tokenizer.decode(output[0], skip_special_tokens=True)
79
- score = generator.prm_model(**generator.tokenizer(response, return_tensors="pt").to(self.device)).logits.mean().item()
80
  results.append((response, score))
81
 
82
  for _ in range(depth - 1):
83
  best_responses = sorted(results, key=lambda x: x[1], reverse=True)[:breadth]
84
  for response, _ in best_responses:
85
- input_ids = generator.tokenizer(response, return_tensors="pt").input_ids.to(self.device)
86
  output = generator.generate(input_ids, **model_kwargs)
87
  extended_response = generator.tokenizer.decode(output[0], skip_special_tokens=True)
88
- score = generator.prm_model(**generator.tokenizer(extended_response, return_tensors="pt").to(self.device)).logits.mean().item()
89
  results.append((extended_response, score))
90
  return max(results, key=lambda x: x[1])[0]
91
 
 
49
  def generate(self, generator: 'BaseGenerator', prompt: str, model_kwargs: Dict[str, Any], num_samples: int = 5, **kwargs) -> str:
50
  scored_outputs = []
51
  for _ in range(num_samples):
52
+ input_ids = generator.tokenizer(prompt, return_tensors="pt").input_ids.to(generator.device)
53
  output = generator.generate(input_ids, **model_kwargs)
54
  response =generator.tokenizer.decode(output[0], skip_special_tokens=True)
55
+ score = generator.prm_model(**generator.tokenizer(response, return_tensors="pt").to(generator.device)).logits.mean().item()
56
  scored_outputs.append((response, score))
57
  return max(scored_outputs, key=lambda x: x[1])[0]
58
 
59
 
60
  class BeamSearch(GenerationStrategy):
61
  def generate(self, generator: 'BaseGenerator', prompt: str, model_kwargs: Dict[str, Any], num_samples: int = 5, **kwargs) -> str:
62
+ input_ids = generator.tokenizer(prompt, return_tensors="pt").input_ids.to(generator.device)
63
  outputs = generator.generate(
64
  input_ids,
65
  num_beams=num_samples,
 
73
  def generate(self, generator: 'BaseGenerator', prompt: str, model_kwargs: Dict[str, Any], num_samples: int = 5, **kwargs) -> str:
74
  results = []
75
  for _ in range(breadth):
76
+ input_ids = generator.tokenizer(prompt, return_tensors="pt").input_ids.to(generator.device)
77
  output = generator.generate(input_ids, **model_kwargs)
78
  response = generator.tokenizer.decode(output[0], skip_special_tokens=True)
79
+ score = generator.prm_model(**generator.tokenizer(response, return_tensors="pt").to(generator.device)).logits.mean().item()
80
  results.append((response, score))
81
 
82
  for _ in range(depth - 1):
83
  best_responses = sorted(results, key=lambda x: x[1], reverse=True)[:breadth]
84
  for response, _ in best_responses:
85
+ input_ids = generator.tokenizer(response, return_tensors="pt").input_ids.to(generator.device)
86
  output = generator.generate(input_ids, **model_kwargs)
87
  extended_response = generator.tokenizer.decode(output[0], skip_special_tokens=True)
88
+ score = generator.prm_model(**generator.tokenizer(extended_response, return_tensors="pt").to(generator.device)).logits.mean().item()
89
  results.append((extended_response, score))
90
  return max(results, key=lambda x: x[1])[0]
91