MCPcopy Create free account
hub / github.com/Alpha-VLLM/LLaMA2-Accessory / generate

Method generate

accessory/model/meta.py:372–467  ·  view source on GitHub ↗

Generate text responses based on input prompts, optionally using images and controlling generation parameters. :param prompts: A batched list of string prompts for text generation. :param images: Optional; batched image tensor to be used in conjunction with the text example

(
        self,
        prompts: List[str],
        images: Optional[torch.FloatTensor] = None,
        max_gen_len: int = 512,
        temperature: float = 0.0,
        top_p: float = 0.95,
        additional_stop_symbols: Iterable[str] = ()
    )

Source from the content-addressed store, hash-verified

370
371 @ torch.inference_mode()
372 def generate(
373 self,
374 prompts: List[str],
375 images: Optional[torch.FloatTensor] = None,
376 max_gen_len: int = 512,
377 temperature: float = 0.0,
378 top_p: float = 0.95,
379 additional_stop_symbols: Iterable[str] = ()
380 ) -> List[str]:
381 """
382 Generate text responses based on input prompts, optionally using images and controlling generation parameters.
383
384 :param prompts: A batched list of string prompts for text generation.
385 :param images: Optional; batched image tensor to be used in conjunction with the text examples.
386 Shape: (bsz, channel, h, w).
387 :param max_gen_len: Maximum generation length for the responses. Default is 512.
388 :param temperature: Controls randomness in generation. Higher values lead to more random outputs.
389 Default is 0.0, namely deterministic generation.
390 :param top_p: Top-p sampling probability for more diverse generation. Default is 0.95.
391 :param additional_stop_symbols: Iterable of additional symbols to stop generation.
392 :return: A list of generated text responses corresponding to each input prompt.
393 """
394
395 if isinstance(prompts, str):
396 raise ValueError(f"{self.__class__}.generate expects a batched LIST of prompts, but str is given")
397
398 if images is not None:
399 images = images.to(list(self.parameters())[0].device)
400
401 bsz = len(prompts)
402 args = self.llma.args
403 assert bsz <= args.max_batch_size, (bsz, args.max_batch_size)
404
405 prompt_tokens = [self.tokenizer.encode(
406 x, bos=True, eos=False) for x in prompts]
407
408 min_prompt_size = min([len(t) for t in prompt_tokens])
409 max_prompt_size = max([len(t) for t in prompt_tokens])
410
411 max_seq_len = args.max_seq_len
412 if images is not None:
413 max_seq_len -= self.llma.image_words
414
415 total_len = min(max_seq_len, max_gen_len + max_prompt_size)
416 for k, t in enumerate(prompt_tokens):
417 prompt_tokens[k] = t[-(max_seq_len-max_gen_len):]
418
419 tokens = torch.full((bsz, total_len), 0).cuda().long()
420 input_text_mask = torch.full((bsz, total_len), False).cuda()
421 for k, t in enumerate(prompt_tokens):
422 tokens[k, : len(t)] = torch.tensor(t).long()
423 input_text_mask[k, : len(t)] = True
424 start_pos = min_prompt_size
425 prev_pos = 0
426
427 l_stop_tokens = [[self.tokenizer.eos_id]]
428 l_stop_tokens += [self.tokenizer.encode_segment(_) for _ in additional_stop_symbols]
429 l_stop_tokens += [self.tokenizer.encode_wo_prefix_space(_) for _ in additional_stop_symbols]

Callers 15

mainFunction · 0.95
run_inferFunction · 0.45
run_inferFunction · 0.45
run_infer_evalFunction · 0.45
run_inferFunction · 0.45
run_infer_evalFunction · 0.45
generate_outputFunction · 0.45
generate_outputFunction · 0.45
run_inferFunction · 0.45
generate_responseMethod · 0.45
mainFunction · 0.45

Calls 6

sample_top_pMethod · 0.95
encodeMethod · 0.80
encode_segmentMethod · 0.80
decodeMethod · 0.80
forward_inferenceMethod · 0.45

Tested by

no test coverage detected