You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm code-generating a function. I'm checking that the work vector size sz_w() in python matches the code-generated sz_w. I noticed that when the function is expanded with Function::expand(), sz_w matches the unexpanded function, but the code-generated expanded function does not match.
Reproduction on casadi 3.6.4:
"""Reproduction for inconsistent sz_w."""frompathlibimportPathimportcasadideftest_casadi_function_generation() ->None:
"""Compare sz_w between python and codegen, for MX and expanded function."""x=casadi.MX.sym("x", 2)
y=casadi.MX.sym("y")
function=casadi.Function("f", [x, y], [x, casadi.sin(y) *x], ["x", "y"], ["r", "q"])
# test the nominal functionprint("")
print(f"function.sz_arg(): {function.sz_arg()}")
print(f"function.sz_res(): {function.sz_res()}")
print(f"function.sz_iw(): {function.sz_iw()}")
print(f"function.sz_w(): {function.sz_w()}")
assertfunction.sz_arg() ==4# this passesassertfunction.sz_res() ==3# this passesassertfunction.sz_iw() ==0# this passesassertfunction.sz_w() ==3# this passes# generate filefilename1="gen1.c"function.generate(filename1)
# open and print filewithPath(filename1).open() ashandle:
lines=handle.readlines()
line_index=_get_work_fun_line(lines)
print("".join(lines[line_index : line_index+7])) # codegen consistent with above assertions# test the expanded functionexpanded_function=function.expand()
print(f"expanded_function.sz_arg(): {expanded_function.sz_arg()}")
print(f"expanded_function.sz_res(): {expanded_function.sz_res()}")
print(f"expanded_function.sz_iw(): {expanded_function.sz_iw()}")
print(f"expanded_function.sz_w(): {expanded_function.sz_w()}")
assertexpanded_function.sz_arg() ==2# this passesassertexpanded_function.sz_res() ==2# this passesassertexpanded_function.sz_iw() ==0# this passesassertexpanded_function.sz_w() ==3# PASSES BUT IS INCONSISTENT WITH CODEGEN# generate filefilename2="gen2.c"expanded_function.generate(filename2)
# open and print filewithPath(filename2).open() ashandle:
lines=handle.readlines()
line_index=_get_work_fun_line(lines)
print("".join(lines[line_index : line_index+7])) # codegen says sz_w == 0def_get_work_fun_line(lines):
"""Get the line number that starts the work function."""start= (
"CASADI_SYMBOL_EXPORT int f_work(casadi_int *sz_arg, casadi_int* sz_res, casadi_int *sz_iw, casadi_int *sz_w)"
)
fork, lineinenumerate(lines):
ifstartinline:
returnkerr_msg=f"could not find '{start}' in file"raiseValueError(err_msg)
if__name__=='__main__':
test_casadi_function_generation()
Here are the results
function.sz_arg(): 4
function.sz_res(): 3
function.sz_iw(): 0
function.sz_w(): 3
CASADI_SYMBOL_EXPORT int f_work(casadi_int *sz_arg, casadi_int* sz_res, casadi_int *sz_iw, casadi_int *sz_w) {
if (sz_arg) *sz_arg = 4;
if (sz_res) *sz_res = 3;
if (sz_iw) *sz_iw = 0;
if (sz_w) *sz_w = 3;
return 0;
}
expanded_function.sz_arg(): 2
expanded_function.sz_res(): 2
expanded_function.sz_iw(): 0
expanded_function.sz_w(): 3
CASADI_SYMBOL_EXPORT int f_work(casadi_int *sz_arg, casadi_int* sz_res, casadi_int *sz_iw, casadi_int *sz_w) {
if (sz_arg) *sz_arg = 2;
if (sz_res) *sz_res = 2;
if (sz_iw) *sz_iw = 0;
if (sz_w) *sz_w = 0;
return 0;
}
notice that for the code generated function, sz_w is 0 but in python it's reported as 3
The text was updated successfully, but these errors were encountered:
When evaluated in codegen, local variables will be allocated instead of using the work vector. So the work vector isn't needed for SX codegen. So it's not really a problem.
I'm code-generating a function. I'm checking that the work vector size
sz_w()
in python matches the code-generatedsz_w
. I noticed that when the function is expanded withFunction::expand()
,sz_w
matches the unexpanded function, but the code-generated expanded function does not match.Reproduction on casadi 3.6.4:
Here are the results
notice that for the code generated function,
sz_w
is0
but in python it's reported as3
The text was updated successfully, but these errors were encountered: