MCPcopy Index your code
hub / github.com/zai-org/CodeGeeX / run_predict

Function run_predict

codegeex/mindspore/generation.py:195–246  ·  view source on GitHub ↗

run predict

(model_predict, config, args_opt, rank)

Source from the content-addressed store, hash-verified

193
194
195def run_predict(model_predict, config, args_opt, rank):
196 """run predict"""
197 from src.generate import generate, generate_increment
198 # Define tokenizer
199 tokenizer = CodeTokenizer(mode='6b')
200
201 # Tokenize input sentence to ids
202 samples = [
203 "Hello there!",
204 "# language: Python\ndef add(a, b):\n '''\n Find the sum of a and b.\n '''\n",
205 "def add(a, b):\n '''\n Find the sum of a and b.\n '''\n",
206 "# language: Python\ndef optimization():\n '''\n Find the maximum of P=E**2*R/(R + r)**2 if E and r are fixed but R varies. Import sympy. Use sympy. Find where the derivative is equal to zero. Substitute the value of R into P.\n '''\n",
207 "from typing import List\n\n\ndef has_close_elements(numbers: List[float], threshold: float) -> bool:\n \"\"\" Check if in given list of numbers, are any two numbers closer to each other than\n given threshold.\n >>> has_close_elements([1.0, 2.0, 3.0], 0.5)\n False\n >>> has_close_elements([1.0, 2.8, 3.0, 4.0, 5.0, 2.0], 0.3)\n True\n \"\"\"\n",
208 "// language: JavaScript\nfunction prime(n) {\n // Find whether n is a prime number.\n",
209 "string morse_encoder(string text) {\n // Translate text into Morse code\n",
210 "def morse_encoder(text):\n # Translate text into Morse code separated by spaces\n",
211 f"% language: MATLAB\nfunction x = solve(A, b)\n % Solve Ax = b\n",
212 f"% language: MATLAB\nfunction [L, U] = lu(A)\n % Return LU factorization of A\n",
213 "def TCPState(state):\n # given a state in TCP protocol, return a list of next possible states\n",
214 "def coordinates(p1, p2, precision=0)\n # p1 is (x1, y1), p2 is (x2, y2), return the distance between p1 and p2 on a cartesian plane, rounded to precision\n",
215 "double travel(double total_time, double run_time, double rest_time, double speed) {\n // the horse runs for run_time with speed speed and rests for rest_time, return the distance it travels after total_time\n",
216 "def travel(total_time, run_time, rest_time, speed):\n # the horse runs for run_time with speed speed and rests for rest_time, return the distance it travels after total_time\n",
217 "// language: C++\nint add(int a, int b) {\n /* Find the sum of a and b. */\n",
218 "int add(int a, int b) {\n /* Find the sum of a and b. */\n",
219 "// language: C++\nvoid sort(int *array, int len) {\n // Sort the array with length len\n",
220 "bool prime(int n) {\n // Find whether n is a prime number\n",
221 "def prime(n):\n # Find whether n is a prime number\n",
222 f"% language: MATLAB\nfunction H = hilbert(n)\n % Return Hilbert matrix of size n * n\n",
223 f"% language: MATLAB\nfunction L = cholesky(A)\n % Return Cholesky factorization of symmetric positive definete matrix A\n",
224 "// language: JavaScript\nfunction add(a, b) {\n // Find the sum of a and b.\n",
225 "# language: R\nadd<-function(a, b) {\n # Find the sum of a and b.\n",
226 ]
227 verbose = False
228 for i, sample in enumerate(samples):
229 for _ in range(1):
230 tokenized_token = tokenizer.encode_code(sample)
231 input_ids = np.array(tokenized_token).reshape(1, -1)
232 # Call inference
233 generate_func = generate_increment if config.use_past else generate
234 t0 = time.perf_counter()
235 output_ids = generate_func(model_predict, input_ids, args_opt, verbose)
236 # Decode output ids to sentence
237 t1 = time.perf_counter()
238 output_samples = tokenizer.decode_code(output_ids.tolist())
239 output_samples_str = "".join(output_samples)
240 if rank % 8 == 0:
241 print(f"=================== prompt {i} ====================")
242 print(sample, flush=True)
243 print(f"=================== generation {i} ====================")
244 print(output_samples_str, flush=True)
245 print(
246 f"=== Total time (s): {t1 - t0}, {output_ids.shape[-1] - input_ids.shape[-1]} tokens, {(output_ids.shape[-1] - input_ids.shape[-1]) / (t1 - t0)} token/s")
247
248
249def main():

Callers 1

mainFunction · 0.70

Calls 3

encode_codeMethod · 0.95
decode_codeMethod · 0.95
CodeTokenizerClass · 0.90

Tested by

no test coverage detected