-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcoreml_util.m
86 lines (71 loc) · 2.78 KB
/
coreml_util.m
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
#include "coreml_util.h"
int mlmodelc_to_espresso_ir(NSString *espressonet)
{
int ret = 0;
void* ctx = espresso_create_context(0x2718LL, 0xFFFFFFFFLL);
void* plan = espresso_create_plan(ctx, 0LL);
uint64_t vals[2];
ret = espresso_plan_add_network(
plan, (char*)[espressonet cStringUsingEncoding:NSUTF8StringEncoding], 0x10010LL, vals);
if (ret) {
NSLog(@"espresso_plan_add_network ret %d\n", ret);
return ret;
}
ret = espresso_plan_build(plan);
if (ret) {
NSLog(@"espresso_plan_build ret %d\n", ret);
return ret;
}
NSString* temp = @"/tmp/espresso_ir_dump/";
char* foo = (char*)[temp cStringUsingEncoding:NSUTF8StringEncoding];
ret = espresso_dump_ir(plan, &foo);
if (ret) {
NSLog(@"espressor_dump_ir ret %d\n", ret);
return ret;
}
espresso_plan_destroy(plan);
espresso_context_destroy(ctx);
return ret;
}
int espresso_ir_hwx(NSString *baseModelName, bool debug)
{
const char* output_base = "/tmp/hwx_output/";
os_log(OS_LOG_DEFAULT, "start compiler");
NSString* temp = @"/tmp/espresso_ir_dump/";
NSDictionary* iDictionary = @{
@"NetworkPlistName" : @"net.plist",
@"NetworkPlistPath" : temp,
};
NSArray* plistArray = @[ iDictionary ];
NSMutableDictionary* optionsDictionary = [NSMutableDictionary dictionaryWithCapacity:4];
NSMutableDictionary* flagsDictionary = [NSMutableDictionary dictionaryWithCapacity:4];
optionsDictionary[@"InputNetworks"] = plistArray;
mkdir(output_base, 0755);
optionsDictionary[@"OutputFilePath"] = [NSString
stringWithFormat:@"%@%@/", [NSString stringWithUTF8String:output_base], baseModelName];
mkdir([optionsDictionary[@"OutputFilePath"] cStringUsingEncoding:NSUTF8StringEncoding], 0755);
optionsDictionary[@"OutputFileName"] = @"model.hwx";
if (debug) {
flagsDictionary[@"CompileANEProgramForDebugging"] = [NSNumber numberWithBool:YES];
int debug_mask = 0x7fffffff;
flagsDictionary[@"DebugMask"] = [NSNumber numberWithInt:debug_mask];
}
// h11 (or anything?) works here too, and creates different outputs that don't
// run
flagsDictionary[@"TargetArchitecture"] = @"h13";
void (^simpleBlock)(ANECStatus status, NSDictionary* statusDictionary) =
^(ANECStatus status, NSDictionary* statusDictionary) {
// NSLog(@"status = %d\n", status);
// when status != 0 dump the dictionary
if (status) NSLog(@"%@", statusDictionary);
};
int ret = ANECCompile(optionsDictionary, flagsDictionary, simpleBlock);
if (!ret) {
NSLog(@"options:\n%@", optionsDictionary);
NSLog(@"result at %@%@", optionsDictionary[@"OutputFilePath"],
optionsDictionary[@"OutputFileName"]);
if (flagsDictionary[@"CompileANEProgramForDebugging"])
NSLog(@"other debug information at %@", optionsDictionary[@"OutputFilePath"]);
}
return ret;
}