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

Function generate

linear_moe/generation/api.py:114–211  ·  view source on GitHub ↗

Given prompts and input parameters, run inference and return the generated tokens, lengths, and output log probabilities. Args: model (torch.nn.Module): The model used for text generation. prompts (List[str], optional): A list of prompts to generate text from. t

(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)

Source from the content-addressed store, hash-verified

112
113
114def generate(model,
115 prompts=None,
116 tokens_to_generate=0,
117 return_output_log_probs=False,
118 top_k_sampling=0,
119 top_p_sampling=0.0,
120 top_p_decay=0.0,
121 top_p_bound=0.0,
122 temperature=1.0,
123 add_BOS=False,
124 use_eod_token_for_early_termination=True,
125 stop_on_double_eol=False,
126 stop_on_eol=False,
127 prevent_newline_after_colon=False,
128 random_seed=-1):
129 """
130 Given prompts and input parameters, run inference and return the generated tokens,
131 lengths, and output log probabilities.
132
133 Args:
134 model (torch.nn.Module): The model used for text generation.
135 prompts (List[str], optional): A list of prompts to generate text from.
136 tokens_to_generate (int): The maximum number of tokens to generate.
137 return_output_log_probs (bool): A flag indicating whether to return the output log probabilities for each generated token.
138 top_k_sampling (int): The value of k for top-k sampling.
139 top_p_sampling (float): The value of p for top-p sampling.
140 top_p_decay (float): The amount by which to decay the value of p for each token generated.
141 top_p_bound (float): The minimum value of p for top-p sampling.
142 temperature (float): The temperature value to apply during sampling.
143 add_BOS (bool): A flag indicating whether to add a beginning-of-sentence token to the generated output.
144 use_eod_token_for_early_termination (bool): A flag indicating whether to use the end-of-document token for early termination.
145 stop_on_double_eol (bool): A flag indicating whether to stop generating text when a double end-of-line token is generated.
146 stop_on_eol (bool): A flag indicating whether to stop generating text when an end-of-line token is generated.
147 prevent_newline_after_colon (bool): A flag indicating whether to prevent newline characters after a colon.
148 random_seed (int): The random seed to use for text generation.
149
150 Returns:
151 Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: A tuple containing the following elements:
152 - tokens (torch.Tensor): The prompt plus generated tokens.
153 - lengths (torch.Tensor): The lengths of the prompt plus the generated tokens.
154 - output_log_probs (torch.Tensor): The output log probabilities for each generated token.
155 """
156
157 # Make sure input params are avaialble to all ranks.
158 values = [
159 tokens_to_generate, return_output_log_probs, top_k_sampling,
160 top_p_sampling, top_p_decay, top_p_bound, temperature, add_BOS,
161 use_eod_token_for_early_termination, stop_on_double_eol, stop_on_eol,
162 prevent_newline_after_colon, random_seed
163 ]
164 values_float_tensor = broadcast_float_list(len(values), float_list=values)
165 tokens_to_generate = int(values_float_tensor[0].item())
166 return_output_log_probs = bool(values_float_tensor[1].item())
167 top_k_sampling = int(values_float_tensor[2].item())
168 top_p_sampling = values_float_tensor[3].item()
169 top_p_decay = values_float_tensor[4].item()
170 top_p_bound = values_float_tensor[5].item()
171 temperature = values_float_tensor[6].item()

Callers 1

Tested by

no test coverage detected