MCPcopy Create free account
hub / github.com/RUC-NLPIR/SmartSearch / generate

Method generate

src/flashrag/generator/generator.py:105–170  ·  view source on GitHub ↗
(self, input_list: List, batch_size=None, **params)

Source from the content-addressed store, hash-verified

103 return passage_ids, passage_masks.bool()
104
105 def generate(self, input_list: List, batch_size=None, **params):
106 if isinstance(input_list, str):
107 input_list = [input_list]
108 if batch_size is None:
109 batch_size = self.batch_size
110
111 generation_params = deepcopy(self.generation_params)
112 generation_params.update(params)
113
114 # deal stop params
115 stop_sym = None
116 if "stop" in generation_params:
117 from flashrag.generator.stop_word_criteria import StopWordCriteria
118
119 stop_sym = generation_params.pop("stop")
120 stopping_criteria = [
121 StopWordCriteria(
122 tokenizer=self.tokenizer,
123 prompts=input_list,
124 stop_words=stop_sym,
125 )
126 ]
127 generation_params["stopping_criteria"] = stopping_criteria
128
129 generation_params = resolve_max_tokens(params, generation_params, prioritize_new_tokens=True)
130
131 responses = []
132 for idx in trange(0, len(input_list), batch_size, desc="Generation process: "):
133 batched_prompts = input_list[idx : idx + batch_size]
134 if self.fid:
135 # assume each input in input_list is a list, contains K string
136 input_ids, attention_mask = self.encode_passages(batched_prompts)
137 inputs = {
138 "input_ids": input_ids.to(self.device),
139 "attention_mask": attention_mask.to(self.device),
140 }
141 else:
142 inputs = self.tokenizer(
143 batched_prompts,
144 return_tensors="pt",
145 padding=True,
146 truncation=True,
147 max_length=self.max_input_len,
148 ).to(self.device)
149
150 # TODO: multi-gpu inference
151 import torch
152 with torch.inference_mode():
153 if self.fid:
154 if 'max_new_tokens' in generation_params:
155 max_new_tokens = generation_params.pop('max_new_tokens')
156 else:
157 max_new_tokens = 32
158
159 outputs = self.model.generate(**inputs, max_new_tokens=max_new_tokens, pad_token_id=self.tokenizer.pad_token_id, decoder_start_token_id=self.tokenizer.pad_token_id)
160 else:
161 outputs = self.model.generate(**inputs, **generation_params)
162 outputs = self.tokenizer.batch_decode(

Callers

nothing calls this directly

Calls 7

encode_passagesMethod · 0.95
StopWordCriteriaClass · 0.90
resolve_max_tokensFunction · 0.90
popMethod · 0.80
toMethod · 0.80
updateMethod · 0.45
generateMethod · 0.45

Tested by

no test coverage detected