Get lengths given a list of templates. Args: templates (Union[List[str], str]): Input template(s). mode (str): Parsing mode. Choices are 'ppl' and 'gen'. Returns: Union[List[int], int]: Length(s) of the input tokens. If the input is a
(
self,
templates: Union[PromptType, List[PromptType]],
mode: str = 'ppl')
| 201 | return self.generate(inputs, max_out_len=max_out_len, **kwargs) |
| 202 | |
| 203 | def get_token_len_from_template( |
| 204 | self, |
| 205 | templates: Union[PromptType, List[PromptType]], |
| 206 | mode: str = 'ppl') -> Union[List[int], int]: |
| 207 | """Get lengths given a list of templates. |
| 208 | |
| 209 | Args: |
| 210 | templates (Union[List[str], str]): Input template(s). |
| 211 | mode (str): Parsing mode. Choices are 'ppl' and 'gen'. |
| 212 | |
| 213 | Returns: |
| 214 | Union[List[int], int]: Length(s) of the input tokens. If the input |
| 215 | is a list, a list of lengths will be returned. Otherwise, an int |
| 216 | will be returned. |
| 217 | """ |
| 218 | prompts = self.parse_template(templates, mode=mode) |
| 219 | assert isinstance(prompts, (list, str)), 'tokens must be list or str' |
| 220 | is_batched = isinstance(prompts, |
| 221 | list) and not isinstance(prompts, PromptList) |
| 222 | if not is_batched: |
| 223 | prompts = [prompts] |
| 224 | prompts = [str(prompt) for prompt in prompts] |
| 225 | token_lens = [self.get_token_len(prompt) for prompt in prompts] |
| 226 | return token_lens[0] if not is_batched else token_lens |
| 227 | |
| 228 | def sync_inputs(self, inputs: str) -> str: |
| 229 | """For some case, when it involves multiprocessing with multiple gpus, |