relu in python

This commit is contained in:
George Hotz
2020-12-12 14:50:05 -08:00
parent d3886035dd
commit abb7b74208
7 changed files with 105 additions and 9 deletions

View File

@@ -31,11 +31,11 @@
<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>
@@ -56,7 +56,7 @@
<key>Params</key>
<dict>
<key>Type</key>
<string>Sign</string>
<string>ReLU</string>
</dict>
<key>Type</key>
<string>Neuron</string>

2
ane/lib/.gitignore vendored
View File

@@ -1 +1 @@
ane.dylib
libane.dylib

View File

@@ -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;
}
}

60
ane/lib/ane.py Executable file
View File

@@ -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)

View File

@@ -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

29
ane/lib/lib.py Executable file
View File

@@ -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)

3
ane/lib/sign_python.sh Executable file
View File

@@ -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