forked from karpathy/llm.c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.h
102 lines (88 loc) · 4.05 KB
/
utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
This file contains utilities shared between the different training scripts.
In particular, we define a series of macros xxxCheck that call the corresponding
C standard library function and check its return code. If an error was reported,
the program prints some debug information and exits.
*/
#ifndef UTILS_H
#define UTILS_H
#include <stdio.h>
#include <stdlib.h>
// ----------------------------------------------------------------------------
// fread convenience utils, with nice handling of error checking using macros
// simple replace fopen, fread, fclose, fseek
// with fopenCheck, freadCheck, fcloseCheck, fseekCheck
FILE *fopen_check(const char *path, const char *mode, const char *file, int line) {
FILE *fp = fopen(path, mode);
if (fp == NULL) {
fprintf(stderr, "Error: Failed to open file '%s' at %s:%d\n", path, file, line);
fprintf(stderr, "Error details:\n");
fprintf(stderr, " File: %s\n", file);
fprintf(stderr, " Line: %d\n", line);
fprintf(stderr, " Path: %s\n", path);
fprintf(stderr, " Mode: %s\n", mode);
fprintf(stderr, "---> HINT 1: dataset files/code have moved to dev/data recently (May 20, 2024). You may have to mv them from the legacy data/ dir to dev/data/(dataset), or re-run the data preprocessing script. Refer back to the main README\n");
fprintf(stderr, "---> HINT 2: possibly try to re-run `python train_gpt2.py`\n");
exit(EXIT_FAILURE);
}
return fp;
}
#define fopenCheck(path, mode) fopen_check(path, mode, __FILE__, __LINE__)
void fread_check(void *ptr, size_t size, size_t nmemb, FILE *stream, const char *file, int line) {
size_t result = fread(ptr, size, nmemb, stream);
if (result != nmemb) {
if (feof(stream)) {
fprintf(stderr, "Error: Unexpected end of file at %s:%d\n", file, line);
} else if (ferror(stream)) {
fprintf(stderr, "Error: File read error at %s:%d\n", file, line);
} else {
fprintf(stderr, "Error: Partial read at %s:%d. Expected %zu elements, read %zu\n",
file, line, nmemb, result);
}
fprintf(stderr, "Error details:\n");
fprintf(stderr, " File: %s\n", file);
fprintf(stderr, " Line: %d\n", line);
fprintf(stderr, " Expected elements: %zu\n", nmemb);
fprintf(stderr, " Read elements: %zu\n", result);
exit(EXIT_FAILURE);
}
}
#define freadCheck(ptr, size, nmemb, stream) fread_check(ptr, size, nmemb, stream, __FILE__, __LINE__)
void fclose_check(FILE *fp, const char *file, int line) {
if (fclose(fp) != 0) {
fprintf(stderr, "Error: Failed to close file at %s:%d\n", file, line);
fprintf(stderr, "Error details:\n");
fprintf(stderr, " File: %s\n", file);
fprintf(stderr, " Line: %d\n", line);
exit(EXIT_FAILURE);
}
}
#define fcloseCheck(fp) fclose_check(fp, __FILE__, __LINE__)
void fseek_check(FILE *fp, long off, int whence, const char *file, int line) {
if (fseek(fp, off, whence) != 0) {
fprintf(stderr, "Error: Failed to seek in file at %s:%d\n", file, line);
fprintf(stderr, "Error details:\n");
fprintf(stderr, " Offset: %ld\n", off);
fprintf(stderr, " Whence: %d\n", whence);
fprintf(stderr, " File: %s\n", file);
fprintf(stderr, " Line: %d\n", line);
exit(EXIT_FAILURE);
}
}
#define fseekCheck(fp, off, whence) fseek_check(fp, off, whence, __FILE__, __LINE__)
// ----------------------------------------------------------------------------
// malloc error-handling wrapper util
void *malloc_check(size_t size, const char *file, int line) {
void *ptr = malloc(size);
if (ptr == NULL) {
fprintf(stderr, "Error: Memory allocation failed at %s:%d\n", file, line);
fprintf(stderr, "Error details:\n");
fprintf(stderr, " File: %s\n", file);
fprintf(stderr, " Line: %d\n", line);
fprintf(stderr, " Size: %zu bytes\n", size);
exit(EXIT_FAILURE);
}
return ptr;
}
#define mallocCheck(size) malloc_check(size, __FILE__, __LINE__)
#endif