relu in python
MODIFY
ane/2_compile/simple/neuron.plist
ane/2_compile/simple/neuron.plist
<integer>1</integer>
<key>InputChannels</key>
- <integer>3</integer>
+ <integer>1</integer>
<key>InputHeight</key>
<integer>1</integer>
<key>InputWidth</key>
- <integer>1</integer>
+ <integer>16</integer>
<key>InputPlaneStride</key>
<integer>64</integer>
<key>Params</key>
<dict>
<key>Type</key>
- <string>Sign</string>
+ <string>ReLU</string>
</dict>
<key>Type</key>
<string>Neuron</string>

MODIFY
ane/lib/.gitignore
ane/lib/.gitignore
-ane.dylib
+libane.dylib

MODIFY
ane/lib/ane.mm
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;
}
}

ADD
ane/lib/ane.py
+#!/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)
+

MODIFY
ane/lib/build.sh
ane/lib/build.sh
#!/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

ADD
ane/lib/lib.py
+#!/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)
+
+
+
+

ADD
ane/lib/sign_python.sh
+#!/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
+

relu in python