Filter löschen
Filter löschen

Errors with using legacy_code to integrate C++ codes generated from deep learning toolbox using MKL-DNN

3 Ansichten (letzte 30 Tage)
I am trying to use a deep reinforcement learning policy represented by a deep neural network (DNN) (with custom layers) in a simulink model and run such model on hardware with desktop real-time. After checking all potential solutions provided by Matlab, I felt that the only solution for me is to (1) generate C++ codes using Intel Math Kernel Library for Deep Neural Networks (MKL-DNN) following here, and (2) use legacy_code tool to integrate the generated C++ function into my simulink model following here.
I completed Step (1) with the following codes (Note that Agent769.mat contains the trained RL agent)
%% generate policy evaluation function
load Agent769.mat
generatePolicyFunction(saved_agent)
%% generate C/C++ codes
cfg = coder.config('mex');
% On the configuration object, set the target language to C++, and set DeepLearningConfig to the target library 'mkldnn'. This option generates code using the Intel Math Kernel Library for Deep Neural Networks (Intel MKL-DNN).
cfg.TargetLang = 'C++';
cfg.DeepLearningConfig = coder.DeepLearningConfig('mkldnn');
%Set an example input value for the policy evaluation function. To find the observation dimension, use the getObservationInfo function. In this case, the observations are in a four-element vector.
argstr = '{ones(6,1)}';
%Generate code using the codegen function.
codegen('-config','cfg','evaluatePolicy','-args',argstr,'-report');
When I executed Step (2) with the following codes
def = legacy_code('initialize');
def.HeaderFiles ={'evaluatePolicy.h','counterbus.h','emlrt.h'};
def.SourceFiles ={'evaluatePolicy.cpp'};
def.IncPaths = {'C:\Software\oneDNN-1.4\include','D:\Boran\Pendubot\codegen\mex\evaluatePolicy',...
'C:\Software\oneDNN-1.4\build\include'};
def.SrcPaths = {'D:\Boran\Pendubot\codegen\mex\evaluatePolicy'};
def.SFunctionName = 'evaluatePolicy_sfun';
def.OutputFcnSpec = 'double y1 = evaluatePolicy(emlrtStack u1[1], double u2[6])'; %COUNTERBUS
def.Options.language = 'C++';
legacy_code('sfcn_cmex_generate',def);
legacy_code('compile',def);
I met the following error:
Error using legacycode.LCT.legacyCodeImpl
The data type "emlrtStack" is neither a built-in data type, nor defined by a Simulink.AliasType, Simulink.NumericType, Simulink.Bus
object, or an enumerated data type:
--> double y1 = evaluatePolicy(emlrtStack u1[1], double u2[6])
Error in legacy_code (line 103)
[varargout{1:nargout}] = legacycode.LCT.legacyCodeImpl(action, varargin{1:end});
Error in compile (line 10)
legacy_code('sfcn_cmex_generate',def);
It seems that the program cannot recognize the data type emlrtStack, although it is used in the function evaluationPolicy of evaluationPolicy.cpp (generated in Step (1)):
real32_T evaluatePolicy(const emlrtStack *sp, const real_T observation1[6])
{
policy0_0 *iobj_0;
emlrtStack b_st;
emlrtStack c_st;
emlrtStack d_st;
emlrtStack e_st;
emlrtStack st;
real32_T meanAndStd[2];
real32_T r;
st.prev = sp;
st.tls = sp->tls;
b_st.prev = &st;
b_st.tls = st.tls;
c_st.prev = &b_st;
c_st.tls = b_st.tls;
d_st.prev = &c_st;
d_st.tls = c_st.tls;
e_st.prev = &d_st;
e_st.tls = d_st.tls;
  2 Kommentare
Denis Gurchenkov
Denis Gurchenkov am 8 Okt. 2023
Hi Pan, not sure how this was missed earlier, our apologies. I suspect you can make some headway if you change the codegen target from mex to lib:
cfg = coder.config('lib');
The thing is, in your original reproduction steps you are using MATLAB Coder to generate a "mex" file. In that mode, the generated C/C++ code is not human readable and contains details such as this emlrtstack type that you have no access to. Your intent was to generate standalone C/C++ code. For that, use the 'lib' target instead.
Hariprasad Ravishankar
Hariprasad Ravishankar am 9 Okt. 2023
Hi Pan,
In addition, you can also use the Policy block to model your deep reinforcement policy in Simulink and generate code from it. Here are some references:
generatePolicyFunction can be unsed in a MATLAB Function block in Simulink to simulate and generate code from it. Here is an example:
https://www.mathworks.com/help/reinforcement-learning/ug/sil-and-pil-verification-for-reinforcement-learning.html?searchHighlight=generatePolicyFunction&s_tid=srchtitle_support_results_3_generatePolicyFunction
Finally, you can also use the deep learning predict block to generate code for the deep learning 'predict' function

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Produkte


Version

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by