Commit 4169ea6f authored by Konstantin Baierer's avatar Konstantin Baierer
Browse files

🔥 drop native.py and nutils.py

Use case now handled by numpy in utils.py
parent b9e70b59
__all__ = [
"binnednn","cairoextras","common","components","dbtables",
"fgen","gmmtree","gtkyield","hocr","lang","native",
"mlp","multiclass","default","lineest"
"fgen","gmmtree","gtkyield","hocr","lang","mlp","multiclass",
"default","lineest"
]
################################################################
......
################################################################
### A simple set of functions for embedding native C code
### inside Python.
################################################################
from __future__ import print_function
import os,hashlib
from ctypes import c_int,c_float,c_double,c_byte
from numpy.ctypeslib import ndpointer
import ctypes
import time
import errno
import contextlib
@contextlib.contextmanager
def lockfile(fname,delay=0.5):
while 1:
try:
fd = os.open(fname,os.O_RDWR|os.O_CREAT|os.O_EXCL)
except OSError as e:
if e.errno!=errno.EEXIST: raise
time.sleep(delay)
continue
else:
break
try:
yield fd
finally:
os.close(fd)
os.unlink(fname)
I = c_int
F = c_float
D = c_double
B = c_byte
for d in range(1,4):
for T,t in [("I","int32"),("F","float32"),("D","float64"),("B","int8"),("U","uint8")]:
exec "A%d%s = ndpointer(dtype='%s',ndim=%d,flags='CONTIGUOUS,ALIGNED')"%(d,T,t,d)
class CompileError(Exception):
pass
def compile_and_find(c_string,prefix=".pynative",opt="-g -O4",libs="-lm",
options="-shared -fopenmp -std=c99 -fPIC",verbose=0):
if not os.path.exists(prefix):
os.mkdir(prefix)
m = hashlib.md5()
m.update(c_string)
base = m.hexdigest()
if verbose: print("hash", base, "for", c_string[:20], "...")
with lockfile(os.path.join(prefix,base+".lock")):
so = os.path.join(prefix,base+".so")
if os.path.exists(so):
if verbose: print("returning existing", so)
return so
source = os.path.join(prefix,base+".c")
with open(source,"w") as stream:
stream.write(c_string)
cmd = "gcc "+opt+" "+libs+" "+options+" "+source+" -o "+so
if verbose: print("#", cmd)
if os.system(cmd)!=0:
if verbose: print("compilation failed")
raise CompileError()
return so
def compile_and_load(c_string,**keys):
path = compile_and_find(c_string,**keys)
return ctypes.CDLL(path)
from ocrolib.native import *
lstm_utils = r"""
#include <math.h>
void sumouter(int r,int n,int m,double out[n][m],double u[r][n],double v[r][m]) {
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
double total = 0.0;
for(int k=0;k<r;k++) total += u[k][i]*v[k][j];
out[i][j] = total;
}
}
}
void sumprod(int r,int n,double out[n],double u[r][n],double v[r][n]) {
for(int i=0;i<n;i++) {
double total = 0.0;
for(int k=0;k<r;k++) total += u[k][i]*v[k][i];
out[i] = total;
}
}
"""
lstm_native = compile_and_load(lstm_utils)
lstm_native.sumouter.argtypes = [I,I,I,A2D,A2D,A2D]
lstm_native.sumprod.argtypes = [I,I,A1D,A2D,A2D]
def sumouter(u,v,out=None):
assert out.shape==u.shape[1:]+v.shape[1:] and u.shape[:1]==v.shape[:1]
lstm_native.sumouter(u.shape[0],out.shape[0],out.shape[1],out,u,v)
return out
def sumprod(u,v,out=None):
assert out.shape==u.shape[1:] and out.shape==v.shape[1:] and u.shape[:1]==v.shape[:1]
lstm_native.sumprod(len(u),len(out),out,u,v)
return out
def test():
from pylab import randn
sumouter(randn(11,3),randn(11,4),out=randn(3,4))
sumprod(randn(11,7),randn(11,7),out=randn(7))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment