Added cython bindings for model runners and commonmodel (#29607)
	
		
	
				
					
				
			* Added cython bindings for model runners and commonmodel * Removed cython language_level=3 * loop to set CXXFLAGS for both envsx-golf3
							parent
							
								
									8205590624
								
							
						
					
					
						commit
						e2e39d100b
					
				
				 16 changed files with 232 additions and 3 deletions
			
			
		@ -0,0 +1,23 @@ | 
				
			||||
# distutils: language = c++ | 
				
			||||
 | 
				
			||||
from cereal.visionipc.visionipc cimport cl_device_id, cl_context, cl_mem | 
				
			||||
 | 
				
			||||
cdef extern from "common/mat.h": | 
				
			||||
  cdef struct mat3: | 
				
			||||
    float v[9] | 
				
			||||
 | 
				
			||||
cdef extern from "common/clutil.h": | 
				
			||||
  cdef unsigned long CL_DEVICE_TYPE_DEFAULT | 
				
			||||
  cl_device_id cl_get_device_id(unsigned long) | 
				
			||||
  cl_context cl_create_context(cl_device_id) | 
				
			||||
 | 
				
			||||
cdef extern from "selfdrive/modeld/models/commonmodel.h": | 
				
			||||
  cppclass ModelFrame: | 
				
			||||
    int buf_size | 
				
			||||
    ModelFrame(cl_device_id, cl_context) | 
				
			||||
    float * prepare(cl_mem, int, int, int, int, mat3, cl_mem*) | 
				
			||||
 | 
				
			||||
cdef extern from "selfdrive/modeld/runners/runmodel.h": | 
				
			||||
  cdef int USE_CPU_RUNTIME | 
				
			||||
  cdef int USE_GPU_RUNTIME | 
				
			||||
  cdef int USE_DSP_RUNTIME | 
				
			||||
@ -0,0 +1,13 @@ | 
				
			||||
# distutils: language = c++ | 
				
			||||
 | 
				
			||||
from cereal.visionipc.visionipc cimport cl_mem | 
				
			||||
from cereal.visionipc.visionipc_pyx cimport CLContext as BaseCLContext | 
				
			||||
 | 
				
			||||
cdef class CLContext(BaseCLContext): | 
				
			||||
  pass | 
				
			||||
 | 
				
			||||
cdef class CLMem: | 
				
			||||
  cdef cl_mem * mem; | 
				
			||||
 | 
				
			||||
  @staticmethod | 
				
			||||
  cdef create(void*) | 
				
			||||
@ -0,0 +1,46 @@ | 
				
			||||
# distutils: language = c++ | 
				
			||||
# cython: c_string_encoding=ascii | 
				
			||||
 | 
				
			||||
import numpy as np | 
				
			||||
cimport numpy as cnp | 
				
			||||
from libc.string cimport memcpy | 
				
			||||
 | 
				
			||||
from cereal.visionipc.visionipc cimport cl_mem | 
				
			||||
from cereal.visionipc.visionipc_pyx cimport VisionBuf, CLContext as BaseCLContext | 
				
			||||
from .commonmodel cimport CL_DEVICE_TYPE_DEFAULT, cl_get_device_id, cl_create_context | 
				
			||||
from .commonmodel cimport USE_CPU_RUNTIME, USE_GPU_RUNTIME, USE_DSP_RUNTIME | 
				
			||||
from .commonmodel cimport mat3, ModelFrame as cppModelFrame | 
				
			||||
 | 
				
			||||
class Runtime: | 
				
			||||
  CPU = USE_CPU_RUNTIME | 
				
			||||
  GPU = USE_GPU_RUNTIME | 
				
			||||
  DSP = USE_DSP_RUNTIME | 
				
			||||
 | 
				
			||||
cdef class CLContext(BaseCLContext): | 
				
			||||
  def __cinit__(self): | 
				
			||||
    self.device_id = cl_get_device_id(CL_DEVICE_TYPE_DEFAULT) | 
				
			||||
    self.context = cl_create_context(self.device_id) | 
				
			||||
 | 
				
			||||
cdef class CLMem: | 
				
			||||
  @staticmethod | 
				
			||||
  cdef create(void * cmem): | 
				
			||||
    mem = CLMem() | 
				
			||||
    mem.mem = <cl_mem*> cmem | 
				
			||||
    return mem | 
				
			||||
 | 
				
			||||
cdef class ModelFrame: | 
				
			||||
  cdef cppModelFrame * frame | 
				
			||||
 | 
				
			||||
  def __cinit__(self, CLContext context): | 
				
			||||
    self.frame = new cppModelFrame(context.device_id, context.context) | 
				
			||||
 | 
				
			||||
  def __dealloc__(self): | 
				
			||||
    del self.frame | 
				
			||||
 | 
				
			||||
  def prepare(self, VisionBuf buf, float[:] projection, CLMem output): | 
				
			||||
    cdef mat3 cprojection | 
				
			||||
    memcpy(cprojection.v, &projection[0], 9*sizeof(float)) | 
				
			||||
    cdef float * data = self.frame.prepare(buf.buf.buf_cl, buf.width, buf.height, buf.stride, buf.uv_offset, cprojection, output.mem) | 
				
			||||
    if not data: | 
				
			||||
      return None | 
				
			||||
    return np.asarray(<cnp.float32_t[:self.frame.buf_size]> data) | 
				
			||||
@ -0,0 +1,9 @@ | 
				
			||||
# distutils: language = c++ | 
				
			||||
 | 
				
			||||
from libcpp.string cimport string | 
				
			||||
 | 
				
			||||
from cereal.visionipc.visionipc cimport cl_context | 
				
			||||
 | 
				
			||||
cdef extern from "selfdrive/modeld/runners/onnxmodel.h": | 
				
			||||
  cdef cppclass ONNXModel: | 
				
			||||
    ONNXModel(string, float*, size_t, int, bool, cl_context) | 
				
			||||
@ -0,0 +1,14 @@ | 
				
			||||
# distutils: language = c++ | 
				
			||||
# cython: c_string_encoding=ascii | 
				
			||||
 | 
				
			||||
from libcpp cimport bool | 
				
			||||
from libcpp.string cimport string | 
				
			||||
 | 
				
			||||
from .onnxmodel cimport ONNXModel as cppONNXModel | 
				
			||||
from selfdrive.modeld.models.commonmodel_pyx cimport CLContext | 
				
			||||
from selfdrive.modeld.runners.runmodel_pyx cimport RunModel | 
				
			||||
from selfdrive.modeld.runners.runmodel cimport RunModel as cppRunModel | 
				
			||||
 | 
				
			||||
cdef class ONNXModel(RunModel): | 
				
			||||
  def __cinit__(self, string path, float[:] output, int runtime, bool use_tf8, CLContext context): | 
				
			||||
    self.model = <cppRunModel *> new cppONNXModel(path, &output[0], len(output), runtime, use_tf8, context.context) | 
				
			||||
@ -0,0 +1,10 @@ | 
				
			||||
# distutils: language = c++ | 
				
			||||
 | 
				
			||||
from libcpp.string cimport string | 
				
			||||
 | 
				
			||||
cdef extern from "selfdrive/modeld/runners/runmodel.h": | 
				
			||||
  cdef cppclass RunModel: | 
				
			||||
    void addInput(string, float*, int) | 
				
			||||
    void setInputBuffer(string, float*, int) | 
				
			||||
    void * getCLBuffer(string) | 
				
			||||
    void execute() | 
				
			||||
@ -0,0 +1,6 @@ | 
				
			||||
# distutils: language = c++ | 
				
			||||
 | 
				
			||||
from .runmodel cimport RunModel as cppRunModel | 
				
			||||
 | 
				
			||||
cdef class RunModel: | 
				
			||||
  cdef cppRunModel * model | 
				
			||||
@ -0,0 +1,32 @@ | 
				
			||||
# distutils: language = c++ | 
				
			||||
# cython: c_string_encoding=ascii | 
				
			||||
 | 
				
			||||
from libcpp.string cimport string | 
				
			||||
from libc.string cimport memcpy | 
				
			||||
 | 
				
			||||
from selfdrive.modeld.models.commonmodel_pyx cimport CLMem | 
				
			||||
 | 
				
			||||
cdef class RunModel: | 
				
			||||
  def __dealloc__(self): | 
				
			||||
    del self.model | 
				
			||||
 | 
				
			||||
  def addInput(self, string name, float[:] buffer): | 
				
			||||
    if buffer is not None: | 
				
			||||
      self.model.addInput(name, &buffer[0], len(buffer)) | 
				
			||||
    else: | 
				
			||||
      self.model.addInput(name, NULL, 0) | 
				
			||||
 | 
				
			||||
  def setInputBuffer(self, string name, float[:] buffer): | 
				
			||||
    if buffer is not None: | 
				
			||||
      self.model.setInputBuffer(name, &buffer[0], len(buffer)) | 
				
			||||
    else: | 
				
			||||
      self.model.setInputBuffer(name, NULL, 0) | 
				
			||||
 | 
				
			||||
  def getCLBuffer(self, string name): | 
				
			||||
    cdef void * cl_buf = self.model.getCLBuffer(name) | 
				
			||||
    if not cl_buf: | 
				
			||||
      return None | 
				
			||||
    return CLMem.create(cl_buf) | 
				
			||||
 | 
				
			||||
  def execute(self): | 
				
			||||
    self.model.execute() | 
				
			||||
@ -0,0 +1,9 @@ | 
				
			||||
# distutils: language = c++ | 
				
			||||
 | 
				
			||||
from libcpp.string cimport string | 
				
			||||
 | 
				
			||||
from cereal.visionipc.visionipc cimport cl_context | 
				
			||||
 | 
				
			||||
cdef extern from "selfdrive/modeld/runners/snpemodel.h": | 
				
			||||
  cdef cppclass SNPEModel: | 
				
			||||
    SNPEModel(string, float*, size_t, int, bool, cl_context) | 
				
			||||
@ -0,0 +1,14 @@ | 
				
			||||
# distutils: language = c++ | 
				
			||||
# cython: c_string_encoding=ascii | 
				
			||||
 | 
				
			||||
from libcpp cimport bool | 
				
			||||
from libcpp.string cimport string | 
				
			||||
 | 
				
			||||
from .snpemodel cimport SNPEModel as cppSNPEModel | 
				
			||||
from selfdrive.modeld.models.commonmodel_pyx cimport CLContext | 
				
			||||
from selfdrive.modeld.runners.runmodel_pyx cimport RunModel | 
				
			||||
from selfdrive.modeld.runners.runmodel cimport RunModel as cppRunModel | 
				
			||||
 | 
				
			||||
cdef class SNPEModel(RunModel): | 
				
			||||
  def __cinit__(self, string path, float[:] output, int runtime, bool use_tf8, CLContext context): | 
				
			||||
    self.model = <cppRunModel *> new cppSNPEModel(path, &output[0], len(output), runtime, use_tf8, context.context) | 
				
			||||
@ -0,0 +1,9 @@ | 
				
			||||
# distutils: language = c++ | 
				
			||||
 | 
				
			||||
from libcpp.string cimport string | 
				
			||||
 | 
				
			||||
from cereal.visionipc.visionipc cimport cl_context | 
				
			||||
 | 
				
			||||
cdef extern from "selfdrive/modeld/runners/thneedmodel.h": | 
				
			||||
  cdef cppclass ThneedModel: | 
				
			||||
    ThneedModel(string, float*, size_t, int, bool, cl_context) | 
				
			||||
@ -0,0 +1,14 @@ | 
				
			||||
# distutils: language = c++ | 
				
			||||
# cython: c_string_encoding=ascii | 
				
			||||
 | 
				
			||||
from libcpp cimport bool | 
				
			||||
from libcpp.string cimport string | 
				
			||||
 | 
				
			||||
from .thneedmodel cimport ThneedModel as cppThneedModel | 
				
			||||
from selfdrive.modeld.models.commonmodel_pyx cimport CLContext | 
				
			||||
from selfdrive.modeld.runners.runmodel_pyx cimport RunModel | 
				
			||||
from selfdrive.modeld.runners.runmodel cimport RunModel as cppRunModel | 
				
			||||
 | 
				
			||||
cdef class ThneedModel(RunModel): | 
				
			||||
  def __cinit__(self, string path, float[:] output, int runtime, bool use_tf8, CLContext context): | 
				
			||||
    self.model = <cppRunModel *> new cppThneedModel(path, &output[0], len(output), runtime, use_tf8, context.context) | 
				
			||||
					Loading…
					
					
				
		Reference in new issue