run predict
(model_predict, config, args_opt, rank)
| 199 | |
| 200 | |
| 201 | def run_predict(model_predict, config, args_opt, rank): |
| 202 | """run predict""" |
| 203 | from src.generate import generate, generate_increment |
| 204 | # Define tokenizer |
| 205 | tokenizer = CodeTokenizer(mode='6b') |
| 206 | |
| 207 | # Tokenize input sentence to ids |
| 208 | samples = [ |
| 209 | "# language: Python\ndef add(a, b):\n '''\n Find the sum of a and b.\n '''\n", |
| 210 | "def add(a, b):\n '''\n Find the sum of a and b.\n '''\n", |
| 211 | "# 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", |
| 212 | "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", |
| 213 | "// language: C++\nint add(int a, int b) {\n /* Find the sum of a and b. */\n", |
| 214 | "int add(int a, int b) {\n /* Find the sum of a and b. */\n", |
| 215 | "bool prime(int n) {\n // Find whether n is a prime number\n", |
| 216 | "// language: JavaScript\nfunction add(a, b) {\n // Find the sum of a and b.\n", |
| 217 | "# language: R\nadd<-function(a, b) {\n # Find the sum of a and b.\n", |
| 218 | ] |
| 219 | verbose = False |
| 220 | for i, sample in enumerate(samples): |
| 221 | for _ in range(1): |
| 222 | tokenized_token = tokenizer.encode_code(sample) |
| 223 | input_ids = np.array(tokenized_token).reshape(1, -1) |
| 224 | # Call inference |
| 225 | generate_func = generate_increment if config.use_past else generate |
| 226 | t0 = time.perf_counter() |
| 227 | output_ids = generate_func(model_predict, input_ids, args_opt, verbose) |
| 228 | # Decode output ids to sentence |
| 229 | t1 = time.perf_counter() |
| 230 | output_samples = tokenizer.decode_code(output_ids.tolist()) |
| 231 | output_samples_str = "".join(output_samples) |
| 232 | if rank % 8 == 0: |
| 233 | print(f"=================== prompt {i} ====================") |
| 234 | print(sample, flush=True) |
| 235 | print(f"=================== generation {i} ====================") |
| 236 | print(output_samples_str, flush=True) |
| 237 | print( |
| 238 | 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") |
| 239 | break |
| 240 | |
| 241 | |
| 242 | def main(): |
no test coverage detected