Tim-Salzmann / l4casadi

Use PyTorch Models with CasADi for data-driven optimization or learning-based optimal control. Supports Acados.
MIT License
370 stars 28 forks source link

Tera_renderer for MacOS #14

Closed jy-cds closed 1 year ago

jy-cds commented 1 year ago

The instruction states that I should "compile tera_renderer from source and place the binary in l4casadi/template_generation/bin" for macOS. From my limited understanding, this means

  1. "git clone" the tera_renderer repo onto my home directory
  2. perform the "cargo build" as instructed in the tera_renderer github repo
  3. Find the binary files after step 2 and put them to the specified l4casadi location.

However, for step 3, I looked through the tera_renderer folder and could not find any .exe files (which I think is the binary file the README was referring to?). Can you help point me to the right binary files? I do see that there is a folder named "bin" but it is empty except for .gitignore in "/Users/jing/tera_renderer/acados/bin". I also noticed that when I try to run any examples, the terminal still says "Dowloading https://github.com/acados/tera_renderer/releases/download/v0.0.34/t_renderer-v0.0.34-osx", which ends with "Successfully downloaded t_renderer. Segmentation fault: 11". Does this mean I have successfully run the exmaples?

Moreover, if any of my steps are wrong please also let me know!

Much thanks! Jing

Tim-Salzmann commented 1 year ago

Hi Jing,

Thanks for reaching out.

However, for step 3, I looked through the tera_renderer folder and could not find any .exe files This will not be a .exe file but a binary file without any file ending t_renderer.

You will have to copy the binary to l4casadi/template_generation/bin.

Assuming you are using Acados you will also copy the same file to <acados_folder>/bin.

To make your life easier I uploaded a M1 compiled binary to the git. Feel free to try and use this binary. https://github.com/Tim-Salzmann/l4casadi/tree/main/l4casadi/template_generation/_arm_tera_renderer

Let me know if this helps!

Best Tim

jy-cds commented 1 year ago

Hi Tim,

Thanks for the binary file! That fixes the tera_renderer issue. However, when I run the examples, I still encounter the same fault output:

Has this happened before? I should add that since I'm using Mac M1, for torch to work with other packages, I had to put the following lines in the beginning of the example files:

import os os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE".

Thanks! Jing

Tim-Salzmann commented 1 year ago

Hi Jing,

I am using a M1 too, and I do not have to do anything special like os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"..

Here are the steps on M1 that make the readme.py execute successfully from scratch.

  1. Create a new virtual env: conda create -n l4casadi_test python=3.9 (does not have to be conda but any venv manager).
  2. git clone https://github.com/Tim-Salzmann/l4casadi.git
  3. pip install -e requirements_build.txt
  4. pip install . --no-build-isolation
  5. python examples/readme.py

Let me know if this works.

Best Tim

jy-cds commented 1 year ago

Hi Tim,

Thanks again for your help! I followed the instructions provided above (setting up a new condo env) and got to pip install -e requirements_build.txt, with the error:

ERROR: requirements_build.txt is not a valid editable requirement. It should either be a path to a local project or a VCS URL (beginning with bzr+http, bzr+https, bzr+ssh, bzr+sftp, bzr+ftp, bzr+lp, bzr+file, git+http, git+https, git+ssh, git+git, git+file, hg+file, hg+http, hg+https, hg+ssh, hg+static-http, svn+ssh, svn+http, svn+https, svn+svn, svn+file).

So instead, I manually installed everything with Mamba install setuptools scikit-build cmake ninja and Mamba install pytorch since trying to do ‘mamba install torch’ errors withtorch does not exist (perhaps a typo or a missing channel).

The I proceeded with pip install . --no-build-isolationin the l4casadi folder, and ran the ‘readme.py’example, which returns me the error: Intel MKL FATAL ERROR: This system does not meet the minimum requirements for use of the Intel(R) Math Kernel Library.’ which from my understanding from online searches is a specific problem with MAC M1 with ‘torch’ installed. So I still had to put os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE" in the beginning to fix this error. And running this version of the environment + l4casadi package, I ran into the following error on the line import l4casadi as l4c, which I never saw before.. Maybe it was how I installed pytorch?

dlopen(/Users/jing/opt/anaconda3/envs/l4casadi/lib/python3.9/site-packages/l4casadi/lib/libl4casadi.dylib, 0x000A): Library not loaded: @rpath/libmkl_intel_ilp64.2.dylib Referenced from: /Users/jing/opt/anaconda3/envs/l4casadi/lib/python3.9/site-packages/l4casadi/lib/libl4casadi.dylib Reason: tried: '/Users/jing/opt/anaconda3/envs/l4casadi/lib/python3.9/lib-dynload/../../libmkl_intel_ilp64.2.dylib' (no such file), '/Users/jing/opt/anaconda3/envs/l4casadi/bin/../lib/libmkl_intel_ilp64.2.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS@rpath/libmkl_intel_ilp64.2.dylib' (no such file), '/Users/jing/opt/anaconda3/envs/l4casadi/lib/python3.9/lib-dynload/../../libmkl_intel_ilp64.2.dylib' (no such file), '/Users/jing/opt/anaconda3/envs/l4casadi/bin/../lib/libmkl_intel_ilp64.2.dylib' (no such file), '/usr/local/lib/libmkl_intel_ilp64.2.dylib' (no such file), '/usr/lib/libmkl_intel_ilp64.2.dylib' (no such file, not in dyld cache) File "/Users/jing/l4casadi/examples/readme.py", line 6, in import l4casadi as l4c OSError: dlopen(/Users/jing/opt/anaconda3/envs/l4casadi/lib/python3.9/site-packages/l4casadi/lib/libl4casadi.dylib, 0x000A): Library not loaded: @rpath/libmkl_intel_ilp64.2.dylib Referenced from: /Users/jing/opt/anaconda3/envs/l4casadi/lib/python3.9/site-packages/l4casadi/lib/libl4casadi.dylib Reason: tried: '/Users/jing/opt/anaconda3/envs/l4casadi/lib/python3.9/lib-dynload/../../libmkl_intel_ilp64.2.dylib' (no such file), '/Users/jing/opt/anaconda3/envs/l4casadi/bin/../lib/libmkl_intel_ilp64.2.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS@rpath/libmkl_intel_ilp64.2.dylib' (no such file), '/Users/jing/opt/anaconda3/envs/l4casadi/lib/python3.9/lib-dynload/../../libmkl_intel_ilp64.2.dylib' (no such file), '/Users/jing/opt/anaconda3/envs/l4casadi/bin/../lib/libmkl_intel_ilp64.2.dylib' (no such file), '/usr/local/lib/libmkl_intel_ilp64.2.dylib' (no such file), '/usr/lib/libmkl_intel_ilp64.2.dylib' (no such file, not in dyld cache)

Now I am curious how you can circumvent all these issues with MAC M1 + PyTorch. If I uninstall PyTorch, for other files, the ‘Intel MKL FATAL ERROR’ immediately goes away.

Tim-Salzmann commented 1 year ago

Hi Jing,

My apologies, there was a typo. It should be: pip install -r requirements_build.txt. I am not familiar with mamba. I think for it to work seamlessly you have to make sure that your python environment is also an arm (aarch64) environment and not an intel via rosetta environment. I am not sure if or how this is possible with mamba - I am using the M1 version of miniconda from [1]. Further, please make sure the installed torch version is >=2.0.

Let me know if this helps!

Best Tim

[1] https://docs.conda.io/projects/miniconda/en/latest/


Edit: Also it is very weird to me that torch is not a package you can install via mamba. This might be the problem. I recommend using pip for installing the packages and potentially use miniconda vs mamba.

Tim-Salzmann commented 1 year ago

Hi Jing,

I'm checking to see if you solved the problem. If so, please close the issue. If you still having problems, I am happy to help.

Best Tim

jy-cds commented 1 year ago

Hi Tim,

Apologies for the late reply - I was out of office for the past couple days. Everything works now! I really appreciate your help! And just to check - in this new version, I no longer to have to worry about putting tera-renderer executables in the .bin folders anymore, correct?

Much Thanks!! Jing

Tim-Salzmann commented 1 year ago

Hi Jing,

great to hear! Correct - I re-wrote the code generation to not have a dependency on the pre-compiled tera_renderer. However, should you use Acados, you will still need tera_renderer as Acados relies on it for code generation.

Best Tim