Skip to content

Commit 56e4e61

Browse files
committed
tools: update iris
1 parent 4a29cf7 commit 56e4e61

10 files changed

Lines changed: 528 additions & 8 deletions

File tree

base/tools/iris/iris.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
#include "iris_kernels.h"
1010
#include "iris_qwen3.h"
1111
#include "iris_safetensors.h"
12+
#ifdef _WIN32
13+
#include "iris_compat.h"
14+
#else
1215
#include <dirent.h>
16+
#endif
1317
#include <stdio.h>
1418
#include <stdlib.h>
1519
#include <string.h>

base/tools/iris/iris_compat.h

Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
/*
2+
* iris_compat.h - Windows portability shims
3+
*
4+
* Iris is written against POSIX. This header provides the small subset of
5+
* POSIX functionality the sources actually use (memory-mapped files, a
6+
* monotonic-ish wall clock, case-insensitive compares, and getopt_long) so the
7+
* engine builds with the MSVC/clang toolchain on Windows. On non-Windows
8+
* platforms this header is empty and the normal system headers are used.
9+
*
10+
* Include this BEFORE the POSIX headers it replaces. The build guards those
11+
* includes (<sys/mman.h>, <unistd.h>, <sys/time.h>, <dirent.h>, <getopt.h>)
12+
* with #ifndef _WIN32, and includes this header in the #else branch.
13+
*/
14+
#ifndef IRIS_COMPAT_H
15+
#define IRIS_COMPAT_H
16+
17+
#ifdef _WIN32
18+
19+
/* Quiet the deprecation warnings on the POSIX CRT names (open/close/strdup/...)
20+
* and keep <windows.h> from dragging in winsock + min/max macros. */
21+
#ifndef WIN32_LEAN_AND_MEAN
22+
#define WIN32_LEAN_AND_MEAN
23+
#endif
24+
#ifndef NOMINMAX
25+
#define NOMINMAX
26+
#endif
27+
#ifndef _CRT_SECURE_NO_WARNINGS
28+
#define _CRT_SECURE_NO_WARNINGS
29+
#endif
30+
#ifndef _CRT_NONSTDC_NO_WARNINGS
31+
#define _CRT_NONSTDC_NO_WARNINGS
32+
#endif
33+
34+
#include <windows.h>
35+
36+
#include <fcntl.h>
37+
#include <io.h>
38+
#include <stdint.h>
39+
#include <stdio.h>
40+
#include <stdlib.h>
41+
#include <string.h>
42+
#include <sys/stat.h>
43+
#include <time.h>
44+
45+
/* --- <strings.h> -------------------------------------------------------- */
46+
#define strcasecmp _stricmp
47+
#define strncasecmp _strnicmp
48+
49+
/* --- 64-bit stat -------------------------------------------------------- */
50+
/* The default Windows `struct stat` stores st_size in a 32-bit long, which
51+
* truncates the multi-GB model files. Route stat()/fstat() through the 64-bit
52+
* variants (struct _stat64 / _stat64 / _fstat64 share the same spelling). */
53+
#define stat _stat64
54+
#define fstat _fstat64
55+
56+
/* --- <sys/mman.h> (read-only file mappings) ----------------------------- */
57+
#define PROT_READ 0x1
58+
#define PROT_WRITE 0x2
59+
#define MAP_SHARED 0x1
60+
#define MAP_PRIVATE 0x2
61+
#define MAP_FAILED ((void *)-1)
62+
63+
/* Map a CRT file descriptor. offset is 0 in every iris call site. The view
64+
* stays valid after the mapping object is closed, so munmap only unmaps. */
65+
static __inline void *mmap(void *addr, size_t length, int prot, int flags, int fd, long long offset) {
66+
(void)addr;
67+
(void)prot;
68+
(void)flags;
69+
HANDLE fh = (HANDLE)_get_osfhandle(fd);
70+
if (fh == INVALID_HANDLE_VALUE)
71+
return MAP_FAILED;
72+
HANDLE mh = CreateFileMappingA(fh, NULL, PAGE_READONLY, 0, 0, NULL);
73+
if (!mh)
74+
return MAP_FAILED;
75+
void *p = MapViewOfFile(mh, FILE_MAP_READ, (DWORD)((unsigned long long)offset >> 32), (DWORD)((unsigned long long)offset & 0xFFFFFFFFu), length);
76+
CloseHandle(mh);
77+
return p ? p : MAP_FAILED;
78+
}
79+
80+
static __inline int munmap(void *addr, size_t length) {
81+
(void)length;
82+
return UnmapViewOfFile(addr) ? 0 : -1;
83+
}
84+
85+
/* --- <sys/time.h> ------------------------------------------------------- */
86+
/* WIN32_LEAN_AND_MEAN keeps winsock (and its struct timeval) out, so we own
87+
* the definition here. */
88+
struct timeval {
89+
long tv_sec;
90+
long tv_usec;
91+
};
92+
93+
static __inline int gettimeofday(struct timeval *tv, void *tz) {
94+
(void)tz;
95+
FILETIME ft;
96+
ULARGE_INTEGER t;
97+
GetSystemTimeAsFileTime(&ft);
98+
t.LowPart = ft.dwLowDateTime;
99+
t.HighPart = ft.dwHighDateTime;
100+
/* 100ns ticks since 1601-01-01 -> microseconds since the Unix epoch. */
101+
t.QuadPart -= 116444736000000000ULL;
102+
tv->tv_sec = (long)(t.QuadPart / 10000000ULL);
103+
tv->tv_usec = (long)((t.QuadPart % 10000000ULL) / 10ULL);
104+
return 0;
105+
}
106+
107+
/* --- <unistd.h>: sysconf(_SC_NPROCESSORS_ONLN) -------------------------- */
108+
#define _SC_NPROCESSORS_ONLN 1
109+
110+
static __inline long sysconf(int name) {
111+
if (name == _SC_NPROCESSORS_ONLN) {
112+
SYSTEM_INFO si;
113+
GetSystemInfo(&si);
114+
return (long)si.dwNumberOfProcessors;
115+
}
116+
return -1;
117+
}
118+
119+
/* --- <getopt.h> --------------------------------------------------------- */
120+
/* Compiled into the single translation unit (main.c) that defines
121+
* IRIS_NEED_GETOPT before including this header. */
122+
#ifdef IRIS_NEED_GETOPT
123+
124+
#define no_argument 0
125+
#define required_argument 1
126+
#define optional_argument 2
127+
128+
struct option {
129+
const char *name;
130+
int has_arg;
131+
int *flag;
132+
int val;
133+
};
134+
135+
char *optarg = NULL;
136+
int optind = 1;
137+
int opterr = 1;
138+
int optopt = 0;
139+
140+
static int iris_optpos = 1; /* position within a clustered short-option group */
141+
142+
static int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex) {
143+
optarg = NULL;
144+
if (optind >= argc)
145+
return -1;
146+
147+
char *arg = argv[optind];
148+
if (arg[0] != '-' || arg[1] == '\0')
149+
return -1; /* not an option; stop (no permutation) */
150+
151+
if (arg[1] == '-') {
152+
/* "--" terminator or a long option */
153+
if (arg[2] == '\0') {
154+
optind++;
155+
return -1;
156+
}
157+
const char *name = arg + 2;
158+
const char *eq = strchr(name, '=');
159+
size_t namelen = eq ? (size_t)(eq - name) : strlen(name);
160+
for (int i = 0; longopts && longopts[i].name; i++) {
161+
if (strlen(longopts[i].name) == namelen && strncmp(name, longopts[i].name, namelen) == 0) {
162+
optind++;
163+
if (longindex)
164+
*longindex = i;
165+
if (longopts[i].has_arg == required_argument) {
166+
if (eq)
167+
optarg = (char *)eq + 1;
168+
else if (optind < argc)
169+
optarg = argv[optind++];
170+
else {
171+
if (opterr)
172+
fprintf(stderr, "%s: option '--%s' requires an argument\n", argv[0], longopts[i].name);
173+
return '?';
174+
}
175+
}
176+
else if (longopts[i].has_arg == optional_argument && eq) {
177+
optarg = (char *)eq + 1;
178+
}
179+
if (longopts[i].flag) {
180+
*longopts[i].flag = longopts[i].val;
181+
return 0;
182+
}
183+
return longopts[i].val;
184+
}
185+
}
186+
if (opterr)
187+
fprintf(stderr, "%s: unrecognized option '--%s'\n", argv[0], name);
188+
optind++;
189+
return '?';
190+
}
191+
192+
/* short option (possibly clustered, e.g. -qv) */
193+
int c = arg[iris_optpos];
194+
const char *p = (c != ':') ? strchr(optstring, c) : NULL;
195+
if (!p) {
196+
optopt = c;
197+
if (opterr)
198+
fprintf(stderr, "%s: invalid option -- '%c'\n", argv[0], c);
199+
if (arg[++iris_optpos] == '\0') {
200+
optind++;
201+
iris_optpos = 1;
202+
}
203+
return '?';
204+
}
205+
206+
if (p[1] == ':') {
207+
/* option takes an argument */
208+
if (arg[iris_optpos + 1] != '\0') {
209+
optarg = &arg[iris_optpos + 1];
210+
optind++;
211+
}
212+
else if (optind + 1 < argc) {
213+
optarg = argv[optind + 1];
214+
optind += 2;
215+
}
216+
else {
217+
optopt = c;
218+
optind++;
219+
iris_optpos = 1;
220+
if (opterr)
221+
fprintf(stderr, "%s: option requires an argument -- '%c'\n", argv[0], c);
222+
return '?';
223+
}
224+
iris_optpos = 1;
225+
return c;
226+
}
227+
228+
/* option without an argument */
229+
if (arg[++iris_optpos] == '\0') {
230+
optind++;
231+
iris_optpos = 1;
232+
}
233+
return c;
234+
}
235+
236+
#endif /* IRIS_NEED_GETOPT */
237+
238+
#endif /* _WIN32 */
239+
#endif /* IRIS_COMPAT_H */

base/tools/iris/iris_gguf.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@
77
*/
88

99
#include "iris_gguf.h"
10+
#ifdef _WIN32
11+
#include "iris_compat.h"
12+
#else
13+
#include <sys/mman.h>
14+
#include <unistd.h>
15+
#endif
1016
#include <fcntl.h>
1117
#include <stdio.h>
1218
#include <stdlib.h>
1319
#include <string.h>
14-
#include <sys/mman.h>
1520
#include <sys/stat.h>
16-
#include <unistd.h>
1721

1822
/* GGML tensor types we support. BF16 checkpoints use only F32 + BF16; Q8_0
1923
* checkpoints additionally use the Q8_0 block-quantized type for the large

base/tools/iris/iris_image.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
#include "iris.h"
1212
#include "jpeg.h"
13+
#ifdef _WIN32
14+
#include "iris_compat.h"
15+
#endif
1316
#include <math.h>
1417
#include <stdio.h>
1518
#include <stdlib.h>

base/tools/iris/iris_qwen3.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
#include "iris_gguf.h"
1414
#include "iris_kernels.h"
1515
#include "iris_safetensors.h"
16+
#ifdef _WIN32
17+
#include "iris_compat.h"
18+
#else
1619
#include <dirent.h>
20+
#endif
1721
#include <math.h>
1822
#include <stdio.h>
1923
#include <stdlib.h>

base/tools/iris/iris_safetensors.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33
*/
44

55
#include "iris_safetensors.h"
6+
#ifdef _WIN32
7+
#include "iris_compat.h"
8+
#else
9+
#include <sys/mman.h>
10+
#include <unistd.h>
11+
#endif
612
#include <fcntl.h>
713
#include <stdio.h>
814
#include <stdlib.h>
915
#include <string.h>
10-
#include <sys/mman.h>
1116
#include <sys/stat.h>
12-
#include <unistd.h>
1317

1418
/* Minimal JSON parser for safetensors header */
1519

base/tools/iris/iris_sample.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@
77

88
#include "iris.h"
99
#include "iris_kernels.h"
10+
#ifdef _WIN32
11+
#include "iris_compat.h"
12+
#else
13+
#include <sys/time.h>
14+
#endif
1015
#include <math.h>
1116
#include <stdio.h>
1217
#include <stdlib.h>
1318
#include <string.h>
14-
#include <sys/time.h>
1519
#include <time.h>
1620

1721
#ifdef USE_METAL

base/tools/iris/iris_transformer_flux.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@
1717
#include "iris_gguf.h"
1818
#include "iris_kernels.h"
1919
#include "iris_safetensors.h"
20+
#ifdef _WIN32
21+
#include "iris_compat.h"
22+
#else
2023
#include <dirent.h>
24+
#include <sys/time.h>
25+
#endif
2126
#include <math.h>
2227
#include <stdio.h>
2328
#include <stdlib.h>
2429
#include <string.h>
25-
#include <sys/time.h>
2630
#include <time.h>
2731

2832
/* External timing counters from iris_sample.c */

base/tools/iris/main.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,18 @@
2424
#include "iris_depth.h"
2525
#include "iris_kernels.h"
2626
#include "iris_upscale.h"
27+
#ifdef _WIN32
28+
#define IRIS_NEED_GETOPT
29+
#include "iris_compat.h"
30+
#else
2731
#include <getopt.h>
32+
#include <sys/time.h>
33+
#include <unistd.h>
34+
#endif
2835
#include <stdio.h>
2936
#include <stdlib.h>
3037
#include <string.h>
31-
#include <sys/time.h>
3238
#include <time.h>
33-
#include <unistd.h>
3439

3540
#ifdef USE_METAL
3641
#include "iris_metal.h"

0 commit comments

Comments
 (0)