MCPcopy Create free account
hub / github.com/OpenSparseLLMs/Linear-MoE / generate_and_post_process

Function generate_and_post_process

linear_moe/generation/api.py:25–111  ·  view source on GitHub ↗

Run inference and post-process outputs, i.e., detokenize, move to cpu and convert to list. Args: model (torch.nn.Module): The model used for text generation. prompts (List[str], optional): A list of prompts to generate text from. tokens_to_generate (int): The ma

(model,
                              prompts=None,
                              tokens_to_generate=0,
                              return_output_log_probs=False,
                              top_k_sampling=0,
                              top_p_sampling=0.0,
                              top_p_decay=0.0,
                              top_p_bound=0.0,
                              temperature=1.0,
                              add_BOS=False,
                              use_eod_token_for_early_termination=True,
                              stop_on_double_eol=False,
                              stop_on_eol=False,
                              prevent_newline_after_colon=False,
                              random_seed=-1,
                              return_logits=False)

Source from the content-addressed store, hash-verified

23
24
25def generate_and_post_process(model,
26 prompts=None,
27 tokens_to_generate=0,
28 return_output_log_probs=False,
29 top_k_sampling=0,
30 top_p_sampling=0.0,
31 top_p_decay=0.0,
32 top_p_bound=0.0,
33 temperature=1.0,
34 add_BOS=False,
35 use_eod_token_for_early_termination=True,
36 stop_on_double_eol=False,
37 stop_on_eol=False,
38 prevent_newline_after_colon=False,
39 random_seed=-1,
40 return_logits=False):
41 """
42 Run inference and post-process outputs, i.e., detokenize,
43 move to cpu and convert to list.
44
45 Args:
46 model (torch.nn.Module): The model used for text generation.
47 prompts (List[str], optional): A list of prompts to generate text from.
48 tokens_to_generate (int): The maximum number of tokens to generate.
49 return_output_log_probs (bool): A flag indicating whether to return the output log probabilities for each generated token.
50 top_k_sampling (int): The value of k for top-k sampling.
51 top_p_sampling (float): The value of p for top-p sampling.
52 top_p_decay (float): The amount by which to decay the value of p for each token generated.
53 top_p_bound (float): The minimum value of p for top-p sampling.
54 temperature (float): The temperature value to apply during sampling.
55 add_BOS (bool): A flag indicating whether to add a beginning-of-sentence token to the generated output.
56 use_eod_token_for_early_termination (bool): A flag indicating whether to use the end-of-document token for early termination.
57 stop_on_double_eol (bool): A flag indicating whether to stop generating text when a double end-of-line token is generated.
58 stop_on_eol (bool): A flag indicating whether to stop generating text when an end-of-line token is generated.
59 prevent_newline_after_colon (bool): A flag indicating whether to prevent newline characters after a colon.
60 random_seed (int): The random seed to use for text generation.
61
62 Returns:
63 Tuple[List[str], List[str], List[List[float]], List[int]]: A tuple containing the following elements:
64 - prompts_plus_generations (List[str]): A list of prompts followed by the generated text.
65 - prompts_plus_generations_segments (List[str]): A list of segments corresponding to each prompt and generated text.
66 - output_log_probs (List[List[float]]): The output log probabilities for each generated token (if return_output_log_probs is True).
67 - tokens (List[int]): The generated tokens.
68
69 """
70
71 # Main inference.
72 tokens, lengths, output_log_probs, logits = generate(
73 model,
74 prompts=prompts,
75 tokens_to_generate=tokens_to_generate,
76 return_output_log_probs=return_output_log_probs,
77 top_k_sampling=top_k_sampling,
78 top_p_sampling=top_p_sampling,
79 top_p_decay=top_p_decay,
80 top_p_bound=top_p_bound,
81 temperature=temperature,
82 add_BOS=add_BOS,

Callers 2

generateMethod · 0.90
predictMethod · 0.90

Calls 2

generateFunction · 0.85
detokenize_generationsFunction · 0.85

Tested by

no test coverage detected