-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathane_runtime.h
More file actions
250 lines (227 loc) · 10.6 KB
/
ane_runtime.h
File metadata and controls
250 lines (227 loc) · 10.6 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// ane_runtime.h — Reusable ANE in-memory compile/load/eval wrapper
// Uses _ANEInMemoryModel via private AppleNeuralEngine.framework
#pragma once
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
#import <objc/message.h>
#import <dlfcn.h>
#import <IOSurface/IOSurface.h>
typedef struct {
id model; // _ANEInMemoryModel
IOSurfaceRef *ioInputs;
IOSurfaceRef *ioOutputs;
id request; // _ANERequest
NSString *tmpDir;
int nInputs, nOutputs;
size_t *inputBytes;
size_t *outputBytes;
} ANEKernel;
static Class g_ANEDesc, g_ANEInMem, g_ANEReq, g_ANEIO;
static bool g_ane_loaded = false;
static void ane_init(void) {
if (g_ane_loaded) return;
dlopen("/System/Library/PrivateFrameworks/AppleNeuralEngine.framework/AppleNeuralEngine", RTLD_NOW);
g_ANEDesc = NSClassFromString(@"_ANEInMemoryModelDescriptor");
g_ANEInMem = NSClassFromString(@"_ANEInMemoryModel");
g_ANEReq = NSClassFromString(@"_ANERequest");
g_ANEIO = NSClassFromString(@"_ANEIOSurfaceObject");
g_ane_loaded = true;
}
static IOSurfaceRef ane_create_surface(size_t bytes) {
return IOSurfaceCreate((__bridge CFDictionaryRef)@{
(id)kIOSurfaceWidth: @(bytes),
(id)kIOSurfaceHeight: @1,
(id)kIOSurfaceBytesPerElement: @1,
(id)kIOSurfaceBytesPerRow: @(bytes),
(id)kIOSurfaceAllocSize: @(bytes),
(id)kIOSurfacePixelFormat: @0
});
}
// Compile a MIL graph with weight blob into an ANE kernel.
// milText: NSData of MIL text
// weightData: NSData of raw weight blob (can be nil)
// inputSizes/outputSizes: arrays of byte sizes for each I/O tensor
// Persistent disk cache for compiled ANE kernels.
// Key = hexStringIdentifier (hash of MIL + weights).
// On cache hit: restore compiled artifacts to tmpDir and skip compileWithQoS:.
static NSString *g_ane_cache_dir = nil;
static void ane_set_cache_dir(NSString *dir) {
g_ane_cache_dir = dir;
[[NSFileManager defaultManager] createDirectoryAtPath:dir
withIntermediateDirectories:YES attributes:nil error:nil];
}
static void ane_enable_cache(void) {
if (!g_ane_cache_dir) {
NSString *home = NSHomeDirectory();
ane_set_cache_dir([home stringByAppendingPathComponent:@".cache/ane_compile"]);
}
}
static ANEKernel *ane_compile(NSData *milText, NSData *weightData,
int nInputs, size_t *inputSizes,
int nOutputs, size_t *outputSizes) {
ane_init();
NSError *e = nil;
NSFileManager *fm = [NSFileManager defaultManager];
// NOTE: weights dict must always be non-nil — passing nil silently returns nil from modelWithMILText:
NSDictionary *wdict = weightData
? @{@"@model_path/weights/weight.bin": @{@"offset": @0, @"data": weightData}}
: @{};
id desc = ((id(*)(Class,SEL,id,id,id))objc_msgSend)(
g_ANEDesc, @selector(modelWithMILText:weights:optionsPlist:),
milText, wdict, nil);
if (!desc) return NULL;
id mdl = ((id(*)(Class,SEL,id))objc_msgSend)(
g_ANEInMem, @selector(inMemoryModelWithDescriptor:), desc);
// Pre-populate temp dir with MIL + weights
id hx = ((id(*)(id,SEL))objc_msgSend)(mdl, @selector(hexStringIdentifier));
NSString *td = [NSTemporaryDirectory() stringByAppendingPathComponent:hx];
[fm createDirectoryAtPath:[td stringByAppendingPathComponent:@"weights"]
withIntermediateDirectories:YES attributes:nil error:nil];
[milText writeToFile:[td stringByAppendingPathComponent:@"model.mil"] atomically:YES];
if (weightData)
[weightData writeToFile:[td stringByAppendingPathComponent:@"weights/weight.bin"] atomically:YES];
// Check disk cache
BOOL compiled = NO;
if (g_ane_cache_dir) {
NSString *cached = [g_ane_cache_dir stringByAppendingPathComponent:hx];
if ([fm fileExistsAtPath:cached]) {
// Cache hit: copy compiled artifacts back to tmpDir
[fm removeItemAtPath:td error:nil];
[fm copyItemAtPath:cached toPath:td error:&e];
compiled = (e == nil);
if (!compiled) {
// Corrupt cache entry — remove and recompile
[fm removeItemAtPath:cached error:nil];
[fm createDirectoryAtPath:[td stringByAppendingPathComponent:@"weights"]
withIntermediateDirectories:YES attributes:nil error:nil];
[milText writeToFile:[td stringByAppendingPathComponent:@"model.mil"] atomically:YES];
if (weightData)
[weightData writeToFile:[td stringByAppendingPathComponent:@"weights/weight.bin"] atomically:YES];
e = nil;
}
}
}
if (!compiled) {
if (!((BOOL(*)(id,SEL,unsigned int,id,NSError**))objc_msgSend)(
mdl, @selector(compileWithQoS:options:error:), 21, @{}, &e)) {
fprintf(stderr, "ANE compile failed: %s\n", [[e description] UTF8String]);
[fm removeItemAtPath:td error:nil];
return NULL;
}
// Save to cache
if (g_ane_cache_dir) {
NSString *cached = [g_ane_cache_dir stringByAppendingPathComponent:hx];
[fm removeItemAtPath:cached error:nil];
[fm copyItemAtPath:td toPath:cached error:nil];
}
}
if (!((BOOL(*)(id,SEL,unsigned int,id,NSError**))objc_msgSend)(
mdl, @selector(loadWithQoS:options:error:), 21, @{}, &e)) {
fprintf(stderr, "ANE load failed: %s\n", [[e description] UTF8String]);
[fm removeItemAtPath:td error:nil];
return NULL;
}
ANEKernel *k = calloc(1, sizeof(ANEKernel));
k->model = mdl;
k->tmpDir = td;
k->nInputs = nInputs;
k->nOutputs = nOutputs;
k->inputBytes = malloc(nInputs * sizeof(size_t));
k->outputBytes = malloc(nOutputs * sizeof(size_t));
memcpy(k->inputBytes, inputSizes, nInputs * sizeof(size_t));
memcpy(k->outputBytes, outputSizes, nOutputs * sizeof(size_t));
// Create IOSurfaces
k->ioInputs = malloc(nInputs * sizeof(IOSurfaceRef));
k->ioOutputs = malloc(nOutputs * sizeof(IOSurfaceRef));
for (int i = 0; i < nInputs; i++)
k->ioInputs[i] = ane_create_surface(inputSizes[i]);
for (int i = 0; i < nOutputs; i++)
k->ioOutputs[i] = ane_create_surface(outputSizes[i]);
// Build request
NSMutableArray *wIns = [NSMutableArray arrayWithCapacity:nInputs];
NSMutableArray *iIdx = [NSMutableArray arrayWithCapacity:nInputs];
for (int i = 0; i < nInputs; i++) {
[wIns addObject:((id(*)(Class,SEL,IOSurfaceRef))objc_msgSend)(
g_ANEIO, @selector(objectWithIOSurface:), k->ioInputs[i])];
[iIdx addObject:@(i)];
}
NSMutableArray *wOuts = [NSMutableArray arrayWithCapacity:nOutputs];
NSMutableArray *oIdx = [NSMutableArray arrayWithCapacity:nOutputs];
for (int i = 0; i < nOutputs; i++) {
[wOuts addObject:((id(*)(Class,SEL,IOSurfaceRef))objc_msgSend)(
g_ANEIO, @selector(objectWithIOSurface:), k->ioOutputs[i])];
[oIdx addObject:@(i)];
}
k->request = ((id(*)(Class,SEL,id,id,id,id,id,id,id))objc_msgSend)(
g_ANEReq, @selector(requestWithInputs:inputIndices:outputs:outputIndices:weightsBuffer:perfStats:procedureIndex:),
wIns, iIdx, wOuts, oIdx, nil, nil, @0);
return k;
}
static void ane_write_input(ANEKernel *k, int idx, const void *data, size_t bytes) {
IOSurfaceLock(k->ioInputs[idx], 0, NULL);
memcpy(IOSurfaceGetBaseAddress(k->ioInputs[idx]), data, bytes);
IOSurfaceUnlock(k->ioInputs[idx], 0, NULL);
}
static void ane_read_output(ANEKernel *k, int idx, void *data, size_t bytes) {
IOSurfaceLock(k->ioOutputs[idx], kIOSurfaceLockReadOnly, NULL);
memcpy(data, IOSurfaceGetBaseAddress(k->ioOutputs[idx]), bytes);
IOSurfaceUnlock(k->ioOutputs[idx], kIOSurfaceLockReadOnly, NULL);
}
// Rebuild the ANERequest for kernel k using externally provided IOSurfaces.
// Pass NULL for any surface to keep the kernel's own surface for that slot.
// Call this after ane_compile to wire shared surfaces between kernels.
static void ane_rewire(ANEKernel *k, IOSurfaceRef *ins, IOSurfaceRef *outs) {
// Swap in caller-provided surfaces (retain new, release old)
for (int i = 0; i < k->nInputs; i++) {
if (ins && ins[i]) {
IOSurfaceRef old = k->ioInputs[i];
k->ioInputs[i] = ins[i];
CFRetain(ins[i]);
CFRelease(old);
}
}
for (int i = 0; i < k->nOutputs; i++) {
if (outs && outs[i]) {
IOSurfaceRef old = k->ioOutputs[i];
k->ioOutputs[i] = outs[i];
CFRetain(outs[i]);
CFRelease(old);
}
}
// Rebuild request with updated surfaces
NSMutableArray *wIns = [NSMutableArray arrayWithCapacity:k->nInputs];
NSMutableArray *iIdx = [NSMutableArray arrayWithCapacity:k->nInputs];
for (int i = 0; i < k->nInputs; i++) {
[wIns addObject:((id(*)(Class,SEL,IOSurfaceRef))objc_msgSend)(
g_ANEIO, @selector(objectWithIOSurface:), k->ioInputs[i])];
[iIdx addObject:@(i)];
}
NSMutableArray *wOuts = [NSMutableArray arrayWithCapacity:k->nOutputs];
NSMutableArray *oIdx = [NSMutableArray arrayWithCapacity:k->nOutputs];
for (int i = 0; i < k->nOutputs; i++) {
[wOuts addObject:((id(*)(Class,SEL,IOSurfaceRef))objc_msgSend)(
g_ANEIO, @selector(objectWithIOSurface:), k->ioOutputs[i])];
[oIdx addObject:@(i)];
}
k->request = ((id(*)(Class,SEL,id,id,id,id,id,id,id))objc_msgSend)(
g_ANEReq, @selector(requestWithInputs:inputIndices:outputs:outputIndices:weightsBuffer:perfStats:procedureIndex:),
wIns, iIdx, wOuts, oIdx, nil, nil, @0);
}
static bool ane_eval(ANEKernel *k) {
NSError *e = nil;
return ((BOOL(*)(id,SEL,unsigned int,id,id,NSError**))objc_msgSend)(
k->model, @selector(evaluateWithQoS:options:request:error:),
21, @{}, k->request, &e);
}
static void ane_free(ANEKernel *k) {
if (!k) return;
NSError *e = nil;
((BOOL(*)(id,SEL,unsigned int,NSError**))objc_msgSend)(
k->model, @selector(unloadWithQoS:error:), 21, &e);
for (int i = 0; i < k->nInputs; i++) CFRelease(k->ioInputs[i]);
for (int i = 0; i < k->nOutputs; i++) CFRelease(k->ioOutputs[i]);
[[NSFileManager defaultManager] removeItemAtPath:k->tmpDir error:nil];
free(k->ioInputs); free(k->ioOutputs);
free(k->inputBytes); free(k->outputBytes);
free(k);
}