Skip to content

Commit

Permalink
adding scons conditions for RK3399
Browse files Browse the repository at this point in the history
  • Loading branch information
BogGyver committed Mar 29, 2020
1 parent cef9029 commit 888e779
Show file tree
Hide file tree
Showing 16 changed files with 955 additions and 39 deletions.
62 changes: 48 additions & 14 deletions SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,29 @@ AddOption('--asan',
help='turn on ASAN')

arch = subprocess.check_output(["uname", "-m"], encoding='utf8').rstrip()
is_tbp = os.path.isfile('/data/tinkla_buddy_pro')
if platform.system() == "Darwin":
arch = "Darwin"

if arch == "aarch64":
lenv = {
"LD_LIBRARY_PATH": '/data/data/com.termux/files/usr/lib',
"PATH": os.environ['PATH'],
"ANDROID_DATA": os.environ['ANDROID_DATA'],
"ANDROID_ROOT": os.environ['ANDROID_ROOT'],
}
if is_tbp:
lenv = {
"LD_LIBRARY_PATH": '/usr/lib',
"PATH": os.environ['PATH'],
"ANDROID_DATA": "/data",
"ANDROID_ROOT": "/",
}
else:
lenv = {
"LD_LIBRARY_PATH": '/data/data/com.termux/files/usr/lib',
"PATH": os.environ['PATH'],
"ANDROID_DATA": os.environ['ANDROID_DATA'],
"ANDROID_ROOT": os.environ['ANDROID_ROOT'],
}

cpppath = [
"#phonelibs/opencl/include",
"#phonelibs/snpe/include",
]
libpath = [
"#phonelibs/snpe/aarch64-android-clang3.8",
Expand All @@ -35,11 +45,36 @@ if arch == "aarch64":
"#phonelibs/nanovg",
"#phonelibs/libyuv/lib",
]

cflags = ["-DQCOM", "-mcpu=cortex-a57"]
cxxflags = ["-DQCOM", "-mcpu=cortex-a57"]

rpath = ["/system/vendor/lib64"]

if is_tbp:
cflags = ["-mcpu=cortex-a57"]
cxxflags = ["-mcpu=cortex-a57"]
cpppath = [
"#phonelibs/opencl/include",
"/data/op_rk3399_setup/support_files/include",
"/data/op_rk3399_setup/snpe/include",
]
libpath = [
"/data/op_rk3399_setup/snpe/lib/lib",
"/usr/lib",
"/data/data/com.termux/files/usr/lib",
"/system/vendor/lib64",
"/system/comma/usr/lib",
"#phonelibs/nanovg",
"/data/op_rk3399_setup/support_files/lib",
]
rpath = ["/system/vendor/lib64",
"/data/op_rk3399_setup/snpe/lib/lib",
"/data/op_rk3399_setup/support_files/lib",
"external/tensorflow/lib",
"cereal",
"/usr/lib",
"selfdrive/common",
]
else:
cflags = ["-DQCOM", "-mcpu=cortex-a57"]
cxxflags = ["-DQCOM", "-mcpu=cortex-a57"]
rpath = ["/system/vendor/lib64"]
else:
lenv = {
"PATH": "#external/bin:" + os.environ['PATH'],
Expand Down Expand Up @@ -123,7 +158,6 @@ env = Environment(
"#phonelibs/android_hardware_libhardware/include",
"#phonelibs/android_system_core/include",
"#phonelibs/linux/include",
"#phonelibs/snpe/include",
"#phonelibs/nanovg",
"#selfdrive/common",
"#selfdrive/camerad",
Expand Down Expand Up @@ -178,7 +212,7 @@ def abspath(x):
#zmq = 'zmq'
# still needed for apks
zmq = FindFile("libzmq.a", libpath)
Export('env', 'arch', 'zmq', 'SHARED')
Export('env', 'arch', 'zmq', 'SHARED', 'is_tbp')

# cereal and messaging are shared with the system
SConscript(['cereal/SConscript'])
Expand Down Expand Up @@ -222,7 +256,7 @@ SConscript(['selfdrive/proclogd/SConscript'])
SConscript(['selfdrive/ui/SConscript'])
SConscript(['selfdrive/loggerd/SConscript'])

if arch == "aarch64":
if arch == "aarch64" and not is_tbp:
SConscript(['selfdrive/logcatd/SConscript'])
SConscript(['selfdrive/sensord/SConscript'])
SConscript(['selfdrive/clocksd/SConscript'])
Expand Down
10 changes: 6 additions & 4 deletions cereal/SConscript
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
Import('env', 'arch', 'zmq')

Import('env', 'arch', 'zmq', 'is_tbp')
gen_dir = Dir('gen')
messaging_dir = Dir('messaging')

Expand Down Expand Up @@ -55,8 +54,11 @@ Depends('messaging/impl_zmq.cc', services_h)

# note, this rebuilds the deps shared, zmq is statically linked to make APK happy
# TODO: get APK to load system zmq to remove the static link
shared_lib_shared_lib = [zmq, 'm', 'stdc++'] + ["gnustl_shared"] if arch == "aarch64" else [zmq]
env.SharedLibrary('messaging_shared', messaging_objects, LIBS=shared_lib_shared_lib)
if is_tbp:
shared_lib_shared_lib = [zmq, 'm', 'stdc++']
else:
shared_lib_shared_lib = [zmq, 'm', 'stdc++'] + ["gnustl_shared"] if arch == "aarch64" else [zmq]
env.SharedLibrary('messaging_shared', messaging_objects, LIBS=shared_lib_shared_lib)

env.Program('messaging/bridge', ['messaging/bridge.cc'], LIBS=[messaging_lib, 'zmq'])
Depends('messaging/bridge.cc', services_h)
Expand Down
4 changes: 2 additions & 2 deletions cereal/SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import subprocess

zmq = 'zmq'
arch = subprocess.check_output(["uname", "-m"], encoding='utf8').rstrip()

is_tbp = os.path.isfile('/data/tinkla_buddy_pro')
cereal_dir = Dir('.')

cpppath = [
Expand Down Expand Up @@ -45,5 +45,5 @@ env = Environment(
)


Export('env', 'zmq', 'arch')
Export('env', 'zmq', 'arch','is_tbp')
SConscript(['SConscript'])
5 changes: 3 additions & 2 deletions cereal/messaging/messaging_pyx_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ def get_ext_filename(self, ext_name):
extra_compile_args = ["-std=c++11"]
libraries = ['zmq']
ARCH = subprocess.check_output(["uname", "-m"], encoding='utf8').rstrip() # pylint: disable=unexpected-keyword-arg

is_tbp = os.path.isfile('/data/tinkla_buddy_pro')
if ARCH == "aarch64":
extra_compile_args += ["-Wno-deprecated-register"]
libraries += ['gnustl_shared']
if not is_tbp:
libraries += ['gnustl_shared']

setup(name='CAN parser',
cmdclass={'build_ext': BuildExtWithoutPlatformSuffix},
Expand Down
Binary file modified phonelibs/json11/json11.o
Binary file not shown.
10 changes: 7 additions & 3 deletions selfdrive/camerad/SConscript
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
Import('env', 'arch', 'messaging', 'common', 'gpucommon', 'visionipc', 'cereal')
Import('env', 'arch', 'is_tbp', 'messaging', 'common', 'gpucommon', 'visionipc', 'cereal')

libs = ['m', 'pthread', common, 'jpeg', 'json', cereal, 'OpenCL', messaging, 'czmq', 'zmq', 'capnp', 'kj', 'capnp_c', visionipc, gpucommon]

if arch == "aarch64":
libs += ['gsl', 'CB', 'adreno_utils', 'EGL', 'GLESv3', 'cutils', 'ui']
cameras = ['cameras/camera_qcom.c']
if is_tbp:
libs += []
cameras = ['cameras/camera_frame_stream.cc']
else:
libs += ['gsl', 'CB', 'adreno_utils', 'EGL', 'GLESv3', 'cutils', 'ui']
cameras = ['cameras/camera_qcom.c']
else:
libs += []
cameras = ['cameras/camera_frame_stream.cc']
Expand Down
23 changes: 15 additions & 8 deletions selfdrive/common/SConscript
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Import('env', 'arch', 'SHARED')
Import('env', 'arch', 'SHARED','is_tbp')

if SHARED:
fxn = env.SharedLibrary
Expand All @@ -17,13 +17,20 @@ files = [
]

if arch == "aarch64":
defines = {}
files += [
'framebuffer.cc',
'touch.c',
'visionbuf_ion.c',
]
_gpu_libs = ['gui', 'adreno_utils']
if is_tbp:
defines = {"CLU_NO_CACHE": None}
files += [
'visionbuf_cl.c',
]
_gpu_libs = ["GL"]
else:
defines = {}
files += [
'framebuffer.cc',
'touch.c',
'visionbuf_ion.c',
]
_gpu_libs = ['gui', 'adreno_utils']
else:
defines = {"CLU_NO_CACHE": None}
files += [
Expand Down
7 changes: 4 additions & 3 deletions selfdrive/loggerd/SConscript
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
Import('env', 'arch', 'messaging', 'common', 'visionipc')
Import('env', 'arch', 'is_tbp', 'messaging', 'common', 'visionipc')

src = ['loggerd.cc', 'logger.cc']
libs = ['zmq', 'czmq', 'capnp', 'kj', 'z',
'avformat', 'avcodec', 'swscale', 'avutil',
'yuv', 'bz2', common, 'json', messaging, visionipc]

if arch == "aarch64":
src += ['encoder.c', 'raw_logger.cc']
libs += ['OmxVenc', 'OmxCore', 'cutils']
if not is_tbp:
src += ['encoder.c', 'raw_logger.cc']
libs += ['OmxVenc', 'OmxCore', 'cutils']

env.Program(src, LIBS=libs)
7 changes: 5 additions & 2 deletions selfdrive/modeld/SConscript
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Import('env', 'arch', 'messaging', 'common', 'gpucommon', 'visionipc')
Import('env', 'arch','is_tbp', 'messaging', 'common', 'gpucommon', 'visionipc')
lenv = env.Clone()

libs = [messaging, common, 'OpenCL', 'SNPE', 'capnp', 'zmq', 'kj', 'yuv', gpucommon, visionipc]
Expand All @@ -8,9 +8,12 @@ common_src = [
"runners/snpemodel.cc",
"transforms/loadyuv.c",
"transforms/transform.c"]

if is_tbp:
arch = "aarch64_TBP"
if arch == "aarch64":
libs += ['gsl', 'CB', 'gnustl_shared']
elif arch == "aarch64_TBP":
libs += ['pthread']
else:
libs += ['symphony-cpu', 'pthread']

Expand Down
98 changes: 98 additions & 0 deletions selfdrive/modeld/runners/tfmodel.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include "tfmodel.h"
#include <stdio.h>
#include <string>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdexcept>
#include "common/util.h"
#include "common/utilpp.h"
#include "common/swaglog.h"
#include <cassert>


TFModel::TFModel(const char *path, float *_output, size_t _output_size, int runtime) {
output = _output;
output_size = _output_size;

char tmp[1024];
strncpy(tmp, path, sizeof(tmp));
strstr(tmp, ".dlc")[0] = '\0';
strcat(tmp, ".keras");
LOGD("loading model %s", tmp);

assert(pipe(pipein) == 0);
assert(pipe(pipeout) == 0);

std::string exe_dir = util::dir_name(util::readlink("/proc/self/exe"));
std::string keras_runner = exe_dir + "/runners/keras_runner.py";

proc_pid = fork();
if (proc_pid == 0) {
LOGD("spawning keras process %s", keras_runner.c_str());
char *argv[] = {(char*)keras_runner.c_str(), tmp, NULL};
dup2(pipein[0], 0);
dup2(pipeout[1], 1);
close(pipein[0]);
close(pipein[1]);
close(pipeout[0]);
close(pipeout[1]);
execvp(keras_runner.c_str(), argv);
}

// parent
close(pipein[0]);
close(pipeout[1]);
}

TFModel::~TFModel() {
close(pipein[1]);
close(pipeout[0]);
kill(proc_pid, SIGTERM);
}

void TFModel::pwrite(float *buf, int size) {
char *cbuf = (char *)buf;
int tw = size*sizeof(float);
while (tw > 0) {
int err = write(pipein[1], cbuf, tw);
//printf("host write %d\n", err);
assert(err >= 0);
cbuf += err;
tw -= err;
}
//printf("host write done\n");
}

void TFModel::pread(float *buf, int size) {
char *cbuf = (char *)buf;
int tr = size*sizeof(float);
while (tr > 0) {
int err = read(pipeout[0], cbuf, tr);
//printf("host read %d/%d\n", err, tr);
assert(err >= 0);
cbuf += err;
tr -= err;
}
//printf("host read done\n");
}

void TFModel::addRecurrent(float *state, int state_size) {
rnn_input_buf = state;
rnn_state_size = state_size;
}

void TFModel::addDesire(float *state, int state_size) {
desire_input_buf = state;
desire_state_size = state_size;
}

void TFModel::execute(float *net_input_buf, int buf_size) {
// order must be this
pwrite(net_input_buf, buf_size);
pwrite(desire_input_buf, desire_state_size);
pwrite(rnn_input_buf, rnn_state_size);
pread(output, output_size);
}

35 changes: 35 additions & 0 deletions selfdrive/modeld/runners/tfmodel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef TFMODEL_H
#define TFMODEL_H

#include <stdlib.h>
#include "runmodel.h"

struct TFState;

class TFModel : public RunModel {
public:
TFModel(const char *path, float *output, size_t output_size, int runtime);
~TFModel();
void addRecurrent(float *state, int state_size);
void addDesire(float *state, int state_size);
void execute(float *net_input_buf, int buf_size);
private:
int proc_pid;

float *output;
size_t output_size;

float *rnn_input_buf = NULL;
int rnn_state_size;
float *desire_input_buf = NULL;
int desire_state_size;

// pipe to communicate to keras subprocess
void pread(float *buf, int size);
void pwrite(float *buf, int size);
int pipein[2];
int pipeout[2];
};

#endif

1 change: 1 addition & 0 deletions selfdrive/registration.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python3.7
import os
import json

Expand Down
Loading

0 comments on commit 888e779

Please sign in to comment.