run predict
(model_predict, config, args_opt, rank)
| 191 | |
| 192 | |
| 193 | def run_predict(model_predict, config, args_opt, rank): |
| 194 | """run predict""" |
| 195 | from src.generate_finetune import generate_increment |
| 196 | # Define tokenizer |
| 197 | tokenizer = CodeTokenizer(mode='6b') |
| 198 | |
| 199 | # Tokenize input sentence to ids |
| 200 | samples = [ |
| 201 | "Hello there!", |
| 202 | "# language: Python\ndef add(a, b):\n '''\n Find the sum of a and b.\n '''\n", |
| 203 | "def add(a, b):\n '''\n Find the sum of a and b.\n '''\n", |
| 204 | "# 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", |
| 205 | "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", |
| 206 | "// language: JavaScript\nfunction prime(n) {\n // Find whether n is a prime number.\n", |
| 207 | "string morse_encoder(string text) {\n // Translate text into Morse code\n", |
| 208 | "def morse_encoder(text):\n # Translate text into Morse code separated by spaces\n", |
| 209 | f"% language: MATLAB\nfunction x = solve(A, b)\n % Solve Ax = b\n", |
| 210 | f"% language: MATLAB\nfunction [L, U] = lu(A)\n % Return LU factorization of A\n", |
| 211 | "def TCPState(state):\n # given a state in TCP protocol, return a list of next possible states\n", |
| 212 | "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", |
| 213 | "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", |
| 214 | "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", |
| 215 | "// language: C++\nint add(int a, int b) {\n /* Find the sum of a and b. */\n", |
| 216 | "int add(int a, int b) {\n /* Find the sum of a and b. */\n", |
| 217 | "// language: C++\nvoid sort(int *array, int len) {\n // Sort the array with length len\n", |
| 218 | "bool prime(int n) {\n // Find whether n is a prime number\n", |
| 219 | "def prime(n):\n # Find whether n is a prime number\n", |
| 220 | f"% language: MATLAB\nfunction H = hilbert(n)\n % Return Hilbert matrix of size n * n\n", |
| 221 | f"% language: MATLAB\nfunction L = cholesky(A)\n % Return Cholesky factorization of symmetric positive definete matrix A\n", |
| 222 | "// language: JavaScript\nfunction add(a, b) {\n // Find the sum of a and b.\n", |
| 223 | "# language: R\nadd<-function(a, b) {\n # Find the sum of a and b.\n", |
| 224 | ] |
| 225 | samples = [tokenizer.encode_code(l) for l in samples] |
| 226 | generations = [] |
| 227 | batch_size = config.batch_size |
| 228 | verbose = (rank % 8 == 0) |
| 229 | save_path = f'/home/work/sfs/xx/pangu_alpha_code/generation_batch/{args_opt.temperature}.txt' # TODO: set as current save path |
| 230 | save_dir = os.path.split(save_path)[0] |
| 231 | if rank == 0: |
| 232 | if not os.path.exists(save_dir): |
| 233 | os.makedirs(save_dir) |
| 234 | if not os.path.exists(save_path): |
| 235 | f = open(save_path, 'w') |
| 236 | f.close() |
| 237 | os.system(f'sudo chmod 777 -R {save_dir}') |
| 238 | batch = [] |
| 239 | input_length = [] |
| 240 | sample_ids = [] |
| 241 | for i, sample in enumerate(samples): |
| 242 | tokenized_token = sample |
| 243 | input_ids = np.array(tokenized_token).reshape(1, -1) |
| 244 | batch.append(input_ids) |
| 245 | input_length.append(input_ids.shape[1]) |
| 246 | sample_ids.append(i) |
| 247 | if (i + 1) % batch_size == 0: |
| 248 | valid_length = max(input_length) |
| 249 | for j in range(len(batch)): |
| 250 | batch[j] = np.pad(batch[j], ((0, 0), (0, valid_length - input_length[j])), |
no test coverage detected