diff --git a/ane/2_compile/simple/neuron.plist b/ane/2_compile/simple/neuron.plist index e4e2d362fa..a24a8a1dcf 100644 --- a/ane/2_compile/simple/neuron.plist +++ b/ane/2_compile/simple/neuron.plist @@ -31,11 +31,11 @@ 1 InputChannels - 3 + 1 InputHeight 1 InputWidth - 1 + 16 InputPlaneStride 64 @@ -56,7 +56,7 @@ Params Type - Sign + ReLU Type Neuron diff --git a/ane/lib/.gitignore b/ane/lib/.gitignore index 09fe3849c3..997aa251f6 100644 --- a/ane/lib/.gitignore +++ b/ane/lib/.gitignore @@ -1 +1 @@ -ane.dylib +libane.dylib diff --git a/ane/lib/ane.mm b/ane/lib/ane.mm index 51fbf4e63a..48735f1fcd 100644 --- a/ane/lib/ane.mm +++ b/ane/lib/ane.mm @@ -43,6 +43,8 @@ int ANE_Open() { ret = dev->ANE_IsPowered(); printf("powered? %d\n", ret); + + return 0; } int stride_for_width(int width) { @@ -51,7 +53,7 @@ int stride_for_width(int width) { return ret; } -void *ANE_CreateTensor(int width, int height) { +void *ANE_TensorCreate(int width, int height) { // all float16 // input buffer @@ -73,9 +75,10 @@ void* ANE_TensorData(void *out_surf) { uint64_t ANE_Compile(char *prog, int sz) { int ret; + printf("ANE_Compile %p with size %d\n", prog, sz); H11ANEProgramCreateArgsStruct mprog = {0}; mprog.program = prog; - mprog.program_length = 0x8000; + mprog.program_length = sz; H11ANEProgramCreateArgsStructOutput *out = new H11ANEProgramCreateArgsStructOutput; memset(out, 0, sizeof(H11ANEProgramCreateArgsStructOutput)); @@ -93,7 +96,7 @@ uint64_t ANE_Compile(char *prog, int sz) { return program_handle; } -void ANE_Run(uint64_t program_handle, void *in_surf, void *out_surf) { +int ANE_Run(uint64_t program_handle, void *in_surf, void *out_surf) { int ret; H11ANEProgramRequestArgsStruct *pras = new H11ANEProgramRequestArgsStruct; memset(pras, 0, sizeof(H11ANEProgramRequestArgsStruct)); @@ -135,6 +138,8 @@ void ANE_Run(uint64_t program_handle, void *in_surf, void *out_surf) { MACH_PORT_NULL); printf("got message: %d sz %d\n", ret, message.header.msgh_size); delete pras; + + return 0; } } diff --git a/ane/lib/ane.py b/ane/lib/ane.py new file mode 100755 index 0000000000..5af0ef8629 --- /dev/null +++ b/ane/lib/ane.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python3 +from ctypes import * +import numpy as np +import faulthandler +faulthandler.enable() + +libane = cdll.LoadLibrary("libane.dylib") + +libane.ANE_Compile.argtypes = [c_char_p, c_int] +libane.ANE_Compile.restype = c_void_p + +libane.ANE_TensorCreate.restype = c_void_p + +libane.ANE_TensorData.argtypes = [c_void_p] +libane.ANE_TensorData.restype = POINTER(c_uint16) + +libane.ANE_Run.argtypes = [c_void_p]*3 +libane.ANE_Run.restype = c_int + +class ANETensor: + def __init__(self, *shape): + self.shape = shape + self.dtype = np.float16 + self.sz = int(np.prod(shape)) + self.tt = libane.ANE_TensorCreate(self.sz, 1) + + def data(self): + data = libane.ANE_TensorData(self.tt) + buf = np.ctypeslib.as_array(data, shape=(self.sz,)) + return np.frombuffer(buf, dtype=self.dtype) + +class ANE: + def __init__(self): + libane.ANE_Open() + + def compile(self, dat): + ret = libane.ANE_Compile(create_string_buffer(dat), len(dat)) + assert(ret is not None) + return ret + + def run(self, prog, tin, tout): + libane.ANE_Run(prog, tin.tt, tout.tt) + +if __name__ == "__main__": + ane = ANE() + + tin = ANETensor(16) + tout = ANETensor(16) + + tind = tin.data() + toutd = tout.data() + + tind[0:4] = [-1,1,-2,2] + print(tind) + + comp = ane.compile(open("../2_compile/model.hwx", "rb").read()) + ret = ane.run(comp, tin, tout) + + print(toutd) + diff --git a/ane/lib/build.sh b/ane/lib/build.sh index 4d9397b96a..29985f80ed 100755 --- a/ane/lib/build.sh +++ b/ane/lib/build.sh @@ -1,4 +1,3 @@ #!/bin/bash -clang++ ane.mm --shared -F /System/Library/PrivateFrameworks/ -framework ANEServices -framework IOSurface -framework Foundation -framework IOKit -o ane.dylib -codesign --entitlements entitlements.xml -s "Taylor Swift Child" ane.dylib +clang++ ane.mm --shared -F /System/Library/PrivateFrameworks/ -framework ANEServices -framework IOSurface -framework Foundation -framework IOKit -o libane.dylib diff --git a/ane/lib/lib.py b/ane/lib/lib.py new file mode 100755 index 0000000000..f0ed903e70 --- /dev/null +++ b/ane/lib/lib.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 +from ctypes import * +libane = cdll.LoadLibrary("libane.dylib") + +libane.ANE_Compile.argtypes = [c_char_p, c_int] +libane.ANE_Compile.restype = c_void_p + +libane.ANE_TensorCreate.restype = c_void_p + +libane.ANE_TensorData.argtypes = [c_void_p] +libane.ANE_TensorData.restype = c_void_p + +#libane.ANE_TensorRun.argtypes = [c_void_p]*3 + +libane.ANE_Open() + +dat = open("../2_compile/model.hwx", "rb").read() +comp = libane.ANE_Compile(create_string_buffer(dat), len(dat)) +print("compile", comp) + +tin = libane.ANE_TensorCreate(16,16) +tout = libane.ANE_TensorCreate(16,16) + +#addr = libane.ANE_TensorData(dat) +#print(dat, addr) + + + + diff --git a/ane/lib/sign_python.sh b/ane/lib/sign_python.sh new file mode 100755 index 0000000000..f2242df87c --- /dev/null +++ b/ane/lib/sign_python.sh @@ -0,0 +1,3 @@ +#!/bin/bash +codesign --entitlements entitlements.xml -s "Taylor Swift Child" /opt/homebrew/Cellar/python@3.9/3.9.0_2/Frameworks/Python.framework/Versions/3.9/Resources/Python.app/Contents/MacOS/Python +