Skip to content

Commit

Permalink
Enhance arg parse : checkpoint 1st, -t -s -p optional
Browse files Browse the repository at this point in the history
It would be ideal to specify parameters without having to set all. One could use getopt for that but it is not portable. So we roll our own minimal one.

Usage:

```bash
run <checkpoint_file> -t [temperature] -s [steps] -p [prompt]
```

Minimum:

```bash
run <checkpoint_file>
```

The following are optional:

-t [temperature] -s [steps] -p [prompt]

if they are omitted, default values are used.

The order of -t -s -p does not matter but the checkpoint_file has to be the first parameter.

To keep it simple without needing for extended checks, if the parameter to the last option is missing, the current default value is used instead.

Updated README.MD to reflect changes.

Incorporated requirement from: karpathy#218 (comment)
  • Loading branch information
trholding committed Aug 4, 2023
1 parent d16e9f1 commit 7bf7eb7
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions run.c
Original file line number Diff line number Diff line change
Expand Up @@ -523,27 +523,28 @@ int main(int argc, char *argv[]) {
gets(promptbuffer); // Read prompt
prompt=promptbuffer; // Set prompt
#else
for (int i = 1; i < argc; i++) {
// 'checkpoint' is necessary arg
if (argc < 2) {
printf("Usage: %s <checkpoint_file> \n", argv[0]);
exit(EXIT_FAILURE);
}
if (argc >= 2) { checkpoint = argv[1]; }
for (int i = 2; i < argc; i++) {
switch (argv[i][0]) {
case '-':
switch (argv[i][1]) {
case 'c': if (i + 1 < argc) { checkpoint = argv[++i]; } break;
// optional temperature. 0.0 = (deterministic) argmax sampling. 1.0 = baseline
case 't': if (i + 1 < argc) { temperature = atof(argv[++i]); } break;
case 's': if (i + 1 < argc) { steps = atoi(argv[++i]); } break;
case 'b': if (i + 1 < argc) { buffertokens = atoi(argv[++i]);} break;
case 'p': if (i + 1 < argc) { prompt = argv[++i]; } break;
default: printf("Invalid option: %s\n", argv[i]);
exit(EXIT_FAILURE);
} break;
default:
printf("Usage: %s -c <checkpoint_file> -t [temperature] -s [steps] -b [buffer_tokens] -p [prompt] \n", argv[0]);
printf("Usage: %s <checkpoint_file> -t [temperature] -s [steps] -p [prompt] \n", argv[0]);
exit(EXIT_FAILURE);
}
}
if (checkpoint == NULL) {
printf("Error: checkpoint file (model) not set. \nSet with %s -c <checkpoint_file>\n",argv[0]);
exit(EXIT_FAILURE);
}
#endif

// seed rng with time. if you want deterministic behavior use temperature 0.0
Expand Down

0 comments on commit 7bf7eb7

Please sign in to comment.