[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"similar-Tim-Salzmann--l4casadi":3,"tool-Tim-Salzmann--l4casadi":61},[4,18,26,36,44,53],{"id":5,"name":6,"github_repo":7,"description_zh":8,"stars":9,"difficulty_score":10,"last_commit_at":11,"category_tags":12,"status":17},4358,"openclaw","openclaw\u002Fopenclaw","OpenClaw 是一款专为个人打造的本地化 AI 助手，旨在让你在自己的设备上拥有完全可控的智能伙伴。它打破了传统 AI 助手局限于特定网页或应用的束缚，能够直接接入你日常使用的各类通讯渠道，包括微信、WhatsApp、Telegram、Discord、iMessage 等数十种平台。无论你在哪个聊天软件中发送消息，OpenClaw 都能即时响应，甚至支持在 macOS、iOS 和 Android 设备上进行语音交互，并提供实时的画布渲染功能供你操控。\n\n这款工具主要解决了用户对数据隐私、响应速度以及“始终在线”体验的需求。通过将 AI 部署在本地，用户无需依赖云端服务即可享受快速、私密的智能辅助，真正实现了“你的数据，你做主”。其独特的技术亮点在于强大的网关架构，将控制平面与核心助手分离，确保跨平台通信的流畅性与扩展性。\n\nOpenClaw 非常适合希望构建个性化工作流的技术爱好者、开发者，以及注重隐私保护且不愿被单一生态绑定的普通用户。只要具备基础的终端操作能力（支持 macOS、Linux 及 Windows WSL2），即可通过简单的命令行引导完成部署。如果你渴望拥有一个懂你",349277,3,"2026-04-06T06:32:30",[13,14,15,16],"Agent","开发框架","图像","数据工具","ready",{"id":19,"name":20,"github_repo":21,"description_zh":22,"stars":23,"difficulty_score":10,"last_commit_at":24,"category_tags":25,"status":17},3808,"stable-diffusion-webui","AUTOMATIC1111\u002Fstable-diffusion-webui","stable-diffusion-webui 是一个基于 Gradio 构建的网页版操作界面，旨在让用户能够轻松地在本地运行和使用强大的 Stable Diffusion 图像生成模型。它解决了原始模型依赖命令行、操作门槛高且功能分散的痛点，将复杂的 AI 绘图流程整合进一个直观易用的图形化平台。\n\n无论是希望快速上手的普通创作者、需要精细控制画面细节的设计师，还是想要深入探索模型潜力的开发者与研究人员，都能从中获益。其核心亮点在于极高的功能丰富度：不仅支持文生图、图生图、局部重绘（Inpainting）和外绘（Outpainting）等基础模式，还独创了注意力机制调整、提示词矩阵、负向提示词以及“高清修复”等高级功能。此外，它内置了 GFPGAN 和 CodeFormer 等人脸修复工具，支持多种神经网络放大算法，并允许用户通过插件系统无限扩展能力。即使是显存有限的设备，stable-diffusion-webui 也提供了相应的优化选项，让高质量的 AI 艺术创作变得触手可及。",162132,"2026-04-05T11:01:52",[14,15,13],{"id":27,"name":28,"github_repo":29,"description_zh":30,"stars":31,"difficulty_score":32,"last_commit_at":33,"category_tags":34,"status":17},1381,"everything-claude-code","affaan-m\u002Feverything-claude-code","everything-claude-code 是一套专为 AI 编程助手（如 Claude Code、Codex、Cursor 等）打造的高性能优化系统。它不仅仅是一组配置文件，而是一个经过长期实战打磨的完整框架，旨在解决 AI 代理在实际开发中面临的效率低下、记忆丢失、安全隐患及缺乏持续学习能力等核心痛点。\n\n通过引入技能模块化、直觉增强、记忆持久化机制以及内置的安全扫描功能，everything-claude-code 能显著提升 AI 在复杂任务中的表现，帮助开发者构建更稳定、更智能的生产级 AI 代理。其独特的“研究优先”开发理念和针对 Token 消耗的优化策略，使得模型响应更快、成本更低，同时有效防御潜在的攻击向量。\n\n这套工具特别适合软件开发者、AI 研究人员以及希望深度定制 AI 工作流的技术团队使用。无论您是在构建大型代码库，还是需要 AI 协助进行安全审计与自动化测试，everything-claude-code 都能提供强大的底层支持。作为一个曾荣获 Anthropic 黑客大奖的开源项目，它融合了多语言支持与丰富的实战钩子（hooks），让 AI 真正成长为懂上",145895,2,"2026-04-08T11:32:59",[14,13,35],"语言模型",{"id":37,"name":38,"github_repo":39,"description_zh":40,"stars":41,"difficulty_score":32,"last_commit_at":42,"category_tags":43,"status":17},2271,"ComfyUI","Comfy-Org\u002FComfyUI","ComfyUI 是一款功能强大且高度模块化的视觉 AI 引擎，专为设计和执行复杂的 Stable Diffusion 图像生成流程而打造。它摒弃了传统的代码编写模式，采用直观的节点式流程图界面，让用户通过连接不同的功能模块即可构建个性化的生成管线。\n\n这一设计巧妙解决了高级 AI 绘图工作流配置复杂、灵活性不足的痛点。用户无需具备编程背景，也能自由组合模型、调整参数并实时预览效果，轻松实现从基础文生图到多步骤高清修复等各类复杂任务。ComfyUI 拥有极佳的兼容性，不仅支持 Windows、macOS 和 Linux 全平台，还广泛适配 NVIDIA、AMD、Intel 及苹果 Silicon 等多种硬件架构，并率先支持 SDXL、Flux、SD3 等前沿模型。\n\n无论是希望深入探索算法潜力的研究人员和开发者，还是追求极致创作自由度的设计师与资深 AI 绘画爱好者，ComfyUI 都能提供强大的支持。其独特的模块化架构允许社区不断扩展新功能，使其成为当前最灵活、生态最丰富的开源扩散模型工具之一，帮助用户将创意高效转化为现实。",108111,"2026-04-08T11:23:26",[14,15,13],{"id":45,"name":46,"github_repo":47,"description_zh":48,"stars":49,"difficulty_score":32,"last_commit_at":50,"category_tags":51,"status":17},4721,"markitdown","microsoft\u002Fmarkitdown","MarkItDown 是一款由微软 AutoGen 团队打造的轻量级 Python 工具，专为将各类文件高效转换为 Markdown 格式而设计。它支持 PDF、Word、Excel、PPT、图片（含 OCR）、音频（含语音转录）、HTML 乃至 YouTube 链接等多种格式的解析，能够精准提取文档中的标题、列表、表格和链接等关键结构信息。\n\n在人工智能应用日益普及的今天，大语言模型（LLM）虽擅长处理文本，却难以直接读取复杂的二进制办公文档。MarkItDown 恰好解决了这一痛点，它将非结构化或半结构化的文件转化为模型“原生理解”且 Token 效率极高的 Markdown 格式，成为连接本地文件与 AI 分析 pipeline 的理想桥梁。此外，它还提供了 MCP（模型上下文协议）服务器，可无缝集成到 Claude Desktop 等 LLM 应用中。\n\n这款工具特别适合开发者、数据科学家及 AI 研究人员使用，尤其是那些需要构建文档检索增强生成（RAG）系统、进行批量文本分析或希望让 AI 助手直接“阅读”本地文件的用户。虽然生成的内容也具备一定可读性，但其核心优势在于为机器",93400,"2026-04-06T19:52:38",[52,14],"插件",{"id":54,"name":55,"github_repo":56,"description_zh":57,"stars":58,"difficulty_score":10,"last_commit_at":59,"category_tags":60,"status":17},4487,"LLMs-from-scratch","rasbt\u002FLLMs-from-scratch","LLMs-from-scratch 是一个基于 PyTorch 的开源教育项目，旨在引导用户从零开始一步步构建一个类似 ChatGPT 的大型语言模型（LLM）。它不仅是同名技术著作的官方代码库，更提供了一套完整的实践方案，涵盖模型开发、预训练及微调的全过程。\n\n该项目主要解决了大模型领域“黑盒化”的学习痛点。许多开发者虽能调用现成模型，却难以深入理解其内部架构与训练机制。通过亲手编写每一行核心代码，用户能够透彻掌握 Transformer 架构、注意力机制等关键原理，从而真正理解大模型是如何“思考”的。此外，项目还包含了加载大型预训练权重进行微调的代码，帮助用户将理论知识延伸至实际应用。\n\nLLMs-from-scratch 特别适合希望深入底层原理的 AI 开发者、研究人员以及计算机专业的学生。对于不满足于仅使用 API，而是渴望探究模型构建细节的技术人员而言，这是极佳的学习资源。其独特的技术亮点在于“循序渐进”的教学设计：将复杂的系统工程拆解为清晰的步骤，配合详细的图表与示例，让构建一个虽小但功能完备的大模型变得触手可及。无论你是想夯实理论基础，还是为未来研发更大规模的模型做准备",90106,"2026-04-06T11:19:32",[35,15,13,14],{"id":62,"github_repo":63,"name":64,"description_en":65,"description_zh":66,"ai_summary_zh":67,"readme_en":68,"readme_zh":69,"quickstart_zh":70,"use_case_zh":71,"hero_image_url":72,"owner_login":73,"owner_name":74,"owner_avatar_url":75,"owner_bio":76,"owner_company":76,"owner_location":76,"owner_email":76,"owner_twitter":76,"owner_website":76,"owner_url":77,"languages":78,"stars":91,"forks":92,"last_commit_at":93,"license":94,"difficulty_score":95,"env_os":96,"env_gpu":97,"env_ram":98,"env_deps":99,"category_tags":109,"github_topics":110,"view_count":32,"oss_zip_url":76,"oss_zip_packed_at":76,"status":17,"created_at":122,"updated_at":123,"faqs":124,"releases":153},5681,"Tim-Salzmann\u002Fl4casadi","l4casadi","Use PyTorch Models with CasADi for data-driven optimization or learning-based optimal control.  Supports Acados.","l4casadi 是一个专为数据驱动优化和学习型最优控制设计的开源框架，它实现了 PyTorch 深度学习模型与 CasADi 数值优化工具的无缝集成。在机器人轨迹规划、流体导航等复杂场景中，开发者常面临如何将训练好的神经网络嵌入传统优化求解器的难题，而 l4casadi 正是为了解决这一“最后一公里”的衔接问题而生。\n\n该工具主要面向从事控制理论、机器人学及自动化领域的研究人员和算法工程师。只要你的 PyTorch 模型具备可微分且可追踪的特性，l4casadi 就能将其转化为 CasADi 可调用的函数，从而利用高效的数值优化算法（如支持 Acados）进行求解。其核心技术亮点在于对计算效率的深度优化：v2 版本原生支持 PyTorch 批处理机制，通过将多个输入合并为单次调用，可将运算速度提升 5 到 10 倍；同时，它严格保持输入张量的维度一致性，避免了以往版本中因形状自动变换引发的混淆，并允许用户灵活选择是否生成海森矩阵以适应不同精度的需求。无论是构建穿越神经辐射场（NeRF）的无碰撞轨迹，还是模拟湍流中的节能航行，l4casadi 都能让基于学习的模型轻松融入严谨的数学优化","l4casadi 是一个专为数据驱动优化和学习型最优控制设计的开源框架，它实现了 PyTorch 深度学习模型与 CasADi 数值优化工具的无缝集成。在机器人轨迹规划、流体导航等复杂场景中，开发者常面临如何将训练好的神经网络嵌入传统优化求解器的难题，而 l4casadi 正是为了解决这一“最后一公里”的衔接问题而生。\n\n该工具主要面向从事控制理论、机器人学及自动化领域的研究人员和算法工程师。只要你的 PyTorch 模型具备可微分且可追踪的特性，l4casadi 就能将其转化为 CasADi 可调用的函数，从而利用高效的数值优化算法（如支持 Acados）进行求解。其核心技术亮点在于对计算效率的深度优化：v2 版本原生支持 PyTorch 批处理机制，通过将多个输入合并为单次调用，可将运算速度提升 5 到 10 倍；同时，它严格保持输入张量的维度一致性，避免了以往版本中因形状自动变换引发的混淆，并允许用户灵活选择是否生成海森矩阵以适应不同精度的需求。无论是构建穿越神经辐射场（NeRF）的无碰撞轨迹，还是模拟湍流中的节能航行，l4casadi 都能让基于学习的模型轻松融入严谨的数学优化流程。","[![PyPI version](https:\u002F\u002Fbadge.fury.io\u002Fpy\u002Fl4casadi.svg)](https:\u002F\u002Fbadge.fury.io\u002Fpy\u002Fl4casadi)\n![L4CasADi CI](https:\u002F\u002Fgithub.com\u002FTim-Salzmann\u002Fl4casadi\u002Factions\u002Fworkflows\u002Fci.yaml\u002Fbadge.svg)\n![Downloads](https:\u002F\u002Fimg.shields.io\u002Fpypi\u002Fdm\u002Fl4casadi.svg)\n\n---\n# Learning 4 CasADi Framework\n\nL4CasADi enables the seamless integration of PyTorch-learned models with CasADi for efficient and potentially\nhardware-accelerated numerical optimization.\nThe only requirement on the PyTorch model is to be traceable and differentiable.\n\n\u003Cdiv align=\"center\">\n  \u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002FTim-Salzmann_l4casadi_readme_9be9d2a7b2b6.gif\" alt=\"Collision-free minimum snap optimized trajectory through a NeRF\" width=\"200\" height=\"200\">\n  \u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002FTim-Salzmann_l4casadi_readme_c9ca6a32716c.gif\" alt=\"Energy Efficient Fish Navigation in Turbulent Flow\" width=\"400\" height=\"200\">\n  \u003Cbr>\u003Ca target=\"_blank\" href=\"https:\u002F\u002Fcolab.research.google.com\u002Fgithub\u002FTim-Salzmann\u002Fl4casadi\u002Fblob\u002Fmain\u002Fexamples\u002Fnerf_trajectory_optimization\u002FNeRF_Trajectory_Optimization.ipynb\">\u003Cimg src=\"https:\u002F\u002Fcolab.research.google.com\u002Fassets\u002Fcolab-badge.svg\" alt=\"Open In Colab\"\u002F>\u003C\u002Fa>\n  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n  \u003Ca target=\"_blank\" href=\"https:\u002F\u002Fcolab.research.google.com\u002Fgithub\u002FTim-Salzmann\u002Fl4casadi\u002Fblob\u002Fmain\u002Fexamples\u002Ffish_turbulent_flow\u002FFish_Turbulent_Flow.ipynb\">\u003Cimg src=\"https:\u002F\u002Fcolab.research.google.com\u002Fassets\u002Fcolab-badge.svg\" alt=\"Open In Colab\"\u002F>\u003C\u002Fa>\n  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n  \u003Cp>\u003Ci>Two L4CasADi examples: Collision-free trajectory through a NeRF and Navigation in Turbulent Flow\u003C\u002Fi>\u003C\u002Fp>\n\u003C\u002Fdiv>\n\narXiv: [Learning for CasADi: Data-driven Models in Numerical Optimization](https:\u002F\u002Farxiv.org\u002Fpdf\u002F2312.05873.pdf)\n\nTalk: [Youtube](https:\u002F\u002Fyoutu.be\u002FUYdkRnGr8eM?si=KEPcFEL9b7Vk2juI&t=3348)\n\n## L4CasADi v2 Breaking Changes\nAfter feedback from first use-cases L4CasADi v2 is designed with efficiency and simplicity in mind.\n\nThis leads to the following breaking changes:\n\n- L4CasADi v2 can leverage PyTorch's batching capabilities for increased efficiency. When passing `batched=True`,\nL4CasADi will understand the **first** input dimension as batch dimension. Thus, first and second-order derivatives\nacross elements of this dimension are assumed to be **sparse-zero**. To make use of this, instead of having multiple calls to a L4CasADi function in\nyour CasADi program, batch all inputs together and have a single L4CasADi call. An example of this can be seen when\ncomparing the [non-batched NeRF example](examples\u002Fnerf_trajectory_optimization\u002Fnerf_trajectory_optimization.py) with the\n[batched NeRF example](examples\u002Fnerf_trajectory_optimization\u002Fnerf_trajectory_optimization_batched.py) which is faster by\na factor of 5-10x.\n- L4CasADi v2 will not change the shape of an input anymore as this was a source of confusion. The tensor forwarded to\nthe PyTorch model will resemble the **exact dimension** of the input variable by CasADi. You are responsible to make\nsure that the PyTorch model handles a **two-dimensional** input matrix! Accordingly, the parameter\n`model_expects_batch_dim` is removed.\n- By default, L4CasADi v2 will not provide the Hessian, but the Jacobian of the Adjoint. This is sufficient for most\nmany optimization problems. However, you can explicitly request the generation of the Hessian by passing\n`generate_jac_jac=True`.\n\n[\u002F\u002F]: # (L4CasADi v2 can use the new **torch compile** functionality starting from PyTorch 2.4. By passing `scripting=False`. This\nwill lead to a longer compile time on first L4CasADi function call but will lead to a overall faster\nexecution. However, currently this functionality is experimental and not fully stable across all models. In the long\nterm there is a good chance this will become the default over scripting once the functionality is stabilized by the\nTorch developers.)\n\n## Table of Content\n- [Projects using L4CasADi](#projects-using-l4casadi)\n- [Installation](#installation)\n    - [Prerequisites](#prerequisites)\n    - [Windows Installation](#windows-installation)\n    - [Pip Install (CPU Only)](#pip-install-cpu-only)\n    - [From Source (CPU Only)](#from-source-cpu-only)\n    - [GPU (CUDA)](#gpu-cuda)\n- [Quick Start](#quick-start)\n- [Online Learning](#online-learning-and-updating)\n- [Naive L4CasADi](#naive-l4casadi) - Use this for small Multi Layer Perceptron Models.\n- [Real-time L4CasADi](#real-time-l4casadi) - Use this for fast MPC with Acados.\n- [Examples](#examples)\n\nIf you use this framework please cite the following two papers:\n\n```\n@article{salzmann2023neural,\n  title={Real-time Neural-MPC: Deep Learning Model Predictive Control for Quadrotors and Agile Robotic Platforms},\n  author={Salzmann, Tim and Kaufmann, Elia and Arrizabalaga, Jon and Pavone, Marco and Scaramuzza, Davide and Ryll, Markus},\n  journal={IEEE Robotics and Automation Letters},\n  doi={10.1109\u002FLRA.2023.3246839},\n  year={2023}\n}\n```\n\n```\n@inproceedings{{salzmann2024l4casadi,\n  title={Learning for CasADi: Data-driven Models in Numerical Optimization},\n  author={Salzmann, Tim and Arrizabalaga, Jon and Andersson, Joel and Pavone, Marco and Ryll, Markus},\n  booktitle={Learning for Dynamics and Control Conference (L4DC)},\n  year={2024}\n}\n```\n\n## Projects using L4CasADi\n- Real-time Neural-MPC: Deep Learning Model Predictive Control for Quadrotors and Agile Robotic Platforms \u003Cbr\u002F> [Paper](https:\u002F\u002Farxiv.org\u002Fpdf\u002F2203.07747.pdf) | [Code](https:\u002F\u002Fgithub.com\u002FTUM-AAS\u002Fneural-mpc)\n- AC4MPC: Actor-Critic Reinforcement Learning for Nonlinear Model Predictive Control \u003Cbr\u002F> [Paper](https:\u002F\u002Farxiv.org\u002Fpdf\u002F2406.03995)\n- Reinforcement Learning based MPC with Neural Dynamical Models \u003Cbr\u002F> [Paper](https:\u002F\u002Ffolk.ntnu.no\u002Fskoge\u002Fpublications\u002F2024\u002Fadhau-ecc24\u002FECC24_0903_FI.pdf)\n- Neural Potential Field for Obstacle-Aware Local Motion Planning \u003Cbr\u002F> [Paper](https:\u002F\u002Farxiv.org\u002Fpdf\u002F2310.16362.pdf) | [Video](https:\u002F\u002Fwww.youtube.com\u002Fwatch?v=KL3bfvUwGqs) | [Code](https:\u002F\u002Fgithub.com\u002Fcog-isa\u002FNPField)\n- N-MPC for Deep Neural Network-Based Collision Avoidance exploiting Depth Images \u003Cbr\u002F> [Paper](https:\u002F\u002Farxiv.org\u002Fpdf\u002F2402.13038.pdf) | [Code](https:\u002F\u002Fgithub.com\u002Fntnu-arl\u002Fcolpred_nmpc)\n- An Integrated Framework for Autonomous Driving Planning and Tracking based on NNMPC Considering Road Surface Variations \u003Cbr\u002F> [Paper](https:\u002F\u002Fieeexplore.ieee.org\u002Fabstract\u002Fdocument\u002F10571565)\n\nIf your project is using L4CasADi and you would like to be featured here, please reach out.\n\n---\n## Installation\n### Prerequisites\nIndependently if you install from source or via pip you will need to meet the following requirements:\n\n- Working build system: CMake compatible C++ compiler.\n    - **On Linux\u002FmacOS:** GCC version 10 or higher is recommended.\n    - **On Windows:** You must install CMake and **Visual C++ Build Tools** (do not use a GCC compiler).\n- PyTorch (`>=2.0`) installation in your python environment.\\\n`python -c \"import torch; print(torch.__version__)\"`\n\n### Windows Installation\n\nFor users installing on Windows, please ensure the following steps are taken:\n\n1. **Install CMake and Visual C++ Build Tools:**\nDownload and install [CMake](https:\u002F\u002Fcmake.org\u002Fdownload\u002F) and the [Visual C++ Build Tools](https:\u002F\u002Fvisualstudio.microsoft.com\u002Fvisual-cpp-build-tools\u002F).\n2. **Ensure Tools Are on Your PATH:**\nMake sure both CMake and the Visual C++ Build Tools are added to your system's PATH. This can be done either via the installers (by selecting the option to update the PATH) or manually.\n3. **Mind the File Path Length:**\nWhen building the L4CasADi model, ensure that the directory path does not exceed Windows’ maximum character limit. Long paths might lead to build issues, so choose a directory with a short path if possible.\n\n### Pip Install (CPU Only)\n- Ensure Torch CPU-version is installed\\\n`pip install torch>=2.0 --index-url https:\u002F\u002Fdownload.pytorch.org\u002Fwhl\u002Fcpu`\n- Ensure all build dependencies are installed\n```\nsetuptools>=68.1\nscikit-build>=0.17\ncmake>=3.27\nninja>=1.11\n```\n\n- Run\\\n`pip install l4casadi --no-build-isolation`\n\n### From Source (CPU Only)\n- Clone the repository\\\n`git clone https:\u002F\u002Fgithub.com\u002FTim-Salzmann\u002Fl4casadi.git`\n\n- All build dependencies installed via\\\n`pip install -r requirements_build.txt`\n\n- Build from source\\\n`pip install . --no-build-isolation`\n\nThe `--no-build-isolation` flag is required for L4CasADi to find and link against the installed PyTorch.\n\n### GPU (CUDA)\nCUDA installation requires nvcc to be installed which is part of the CUDA toolkit and can be installed on Linux via\n`sudo apt-get -y install cuda-toolkit-XX-X` (where `XX-X` is your installed Cuda version - e.g. `12-3`).\nOnce the CUDA toolkit is installed nvcc is commonly found at `\u002Fusr\u002Flocal\u002Fcuda\u002Fbin\u002Fnvcc`.\n\nMake sure `nvcc -V` can be executed and run `pip install l4casadi --no-build-isolation` or `CUDACXX=\u003CPATH_TO_NVCC> pip install . --no-build-isolation` to build from source.\n\nIf `nvcc` is not automatically part of your path you can specify the `nvcc` path for L4CasADi.\nE.g. `CUDACXX=\u003CPATH_TO_NVCC> pip install l4casadi --no-build-isolation`.\n\n---\n\n## Quick Start\n\nDefining an L4CasADi model in Python given a pre-defined PyTorch model is as easy as\n```python\nimport l4casadi as l4c\n\nl4c_model = l4c.L4CasADi(pyTorch_model, device='cpu')\n```\n\nwhere the architecture of the PyTorch model is unrestricted and large models can be accelerated with dedicated hardware.\n\n---\n\n## Online Learning and Updating\nL4CasADi supports updating the PyTorch model online in the CasADi graph. To use this feature, pass `mutable=True` when\ninitializing a L4CasADi. To update the model, call the `update` function on the `L4CasADi` object.\nYou can optionally pass an updated model as parameter. If no model is passed, the reference passed at\ninitialization is assumed to be updated and will be used for the update.\n\n---\n\n## Naive L4CasADi\n\nWhile L4CasADi was designed with efficiency in mind by internally leveraging torch's C++ interface, this can still\nresult in overhead, which can be disproportionate for small, simple models. Thus, L4CasADi additionally provides a\n`NaiveL4CasADiModule` which directly recreates the PyTorch computational graph using CasADi operations and copies the\nweights --- leading to a pure C computational graph without context switches to torch. However, this approach is\nlimited to a small predefined subset of PyTorch operations --- only `MultiLayerPerceptron`\nmodels and CPU inference are supported.\n\nThe torch framework overhead dominates for networks smaller than three hidden layers, each with 64\nneurons (or equivalent). For models smaller than this size we recommend using the NaiveL4CasADiModule.\nFor larger models, the overhead becomes negligible and L4CasADi should be used.\n\nhttps:\u002F\u002Fgithub.com\u002FTim-Salzmann\u002Fl4casadi\u002Fblob\u002Ff7b16fba90f4d3ee53217b560f26b47e6b23e44a\u002Fexamples\u002Fnaive\u002Freadme.py#L5-L9\n\n---\n## Real-time L4CasADi\nReal-time L4Casadi (former `Approximated` approach in [ML-CasADi](https:\u002F\u002Fgithub.com\u002FTUM-AAS\u002Fml-casadi)) is the underlying framework powering\n[Real-time Neural-MPC](https:\u002F\u002Farxiv.org\u002Fpdf\u002F2203.07747). It replaces complex models with local Taylor approximations.\nFor certain optimization procedures (such as MPC with multiple shooting nodes) this can lead to improved optimization times.\nHowever, `Real-time L4Casadi`, comes with many restrictions (only Python, no C(++) code generation, ...) and is therefore not\na one-to-one replacement for `L4Casadi`. Rather it is a complementary framework for certain special use cases.\n\nMore information [here](l4casadi\u002Frealtime).\n\nhttps:\u002F\u002Fgithub.com\u002FTim-Salzmann\u002Fl4casadi\u002Fblob\u002Ff7b16fba90f4d3ee53217b560f26b47e6b23e44a\u002Fl4casadi\u002Frealtime\u002Fexamples\u002Freadme.py#L32-L43\n\n---\n\n## Examples\nhttps:\u002F\u002Fgithub.com\u002FTim-Salzmann\u002Fl4casadi\u002Fblob\u002Ff7b16fba90f4d3ee53217b560f26b47e6b23e44a\u002Fexamples\u002Freadme.py#L28-L40\n\nPlease note that only `casadi.MX` symbolic variables are supported as input.\n\nMulti-input multi-output functions can be realized by concatenating the symbolic inputs when passing to the model and\nsplitting them inside the PyTorch function.\n\nTo use GPU (CUDA) simply pass `device=\"cuda\"` to the `L4CasADi` constructor.\n\nFurther examples:\n- Collision-free minimum snap optimized trajectory through a NeRF: [examples\u002Fnerf_trajectory_optimization](\u002Fexamples\u002Fnerf_trajectory_optimization)\n- Energy Efficient Fish Navigation in Turbulent Flow: [examples\u002Ffish_turbulent_flow](\u002Fexamples\u002Ffish_turbulent_flow)\n- Simple nonlinear programming with L4CasADi model as objective and constraints: [examples\u002Fsimple_nlp.py](\u002Fexamples\u002Fsimple_nlp.py)\n- L4CasADi in pure C(++) projects: [examples\u002Fcpp_executable](\u002Fexamples\u002Fcpp_usage)\n- Use PyTorch L4CasADi Model in Matlab: [examples\u002Fmatlab](\u002Fexamples\u002Fmatlab)\n\n---\n\n## Acados Integration\nTo use this framework with Acados:\n- Follow the [installation instructions](https:\u002F\u002Fdocs.acados.org\u002Finstallation\u002Findex.html).\n- Install the [Python Interface](https:\u002F\u002Fdocs.acados.org\u002Fpython_interface\u002Findex.html).\n- Ensure that `LD_LIBRARY_PATH` is set correctly (`DYLD_LIBRARY_PATH`on MacOS).\n- Ensure that `ACADOS_SOURCE_DIR` is set correctly.\n\nAn example of how a PyTorch model can be used as dynamics model in the Acados framework for Model Predictive Control \ncan be found in [examples\u002Facados.py](\u002Fexamples\u002Facados.py)\n\nTo use L4CasADi with Acados you will have to set `model_external_shared_lib_dir` and `model_external_shared_lib_name`\nin the `AcadosOcp.solver_options` accordingly.\n\n```\nocp.solver_options.model_external_shared_lib_dir = l4c_model.shared_lib_dir\nocp.solver_options.model_external_shared_lib_name = l4c_model.name\n```\n\nhttps:\u002F\u002Fgithub.com\u002FTim-Salzmann\u002Fl4casadi\u002Fblob\u002Ff7b16fba90f4d3ee53217b560f26b47e6b23e44a\u002Fexamples\u002Facados.py#L156-L160\n\n---\n\n## FYIs\n\n\n### Warm Up\n\nNote that PyTorch builds the graph on first execution. Thus, the first call(s) to the CasADi function will be slow.\nYou can warm up to the execution graph by calling the generated CasADi function one or multiple times before using it.\n","[![PyPI version](https:\u002F\u002Fbadge.fury.io\u002Fpy\u002Fl4casadi.svg)](https:\u002F\u002Fbadge.fury.io\u002Fpy\u002Fl4casadi)\n![L4CasADi CI](https:\u002F\u002Fgithub.com\u002FTim-Salzmann\u002Fl4casadi\u002Factions\u002Fworkflows\u002Fci.yaml\u002Fbadge.svg)\n![Downloads](https:\u002F\u002Fimg.shields.io\u002Fpypi\u002Fdm\u002Fl4casadi.svg)\n\n---\n# Learning 4 CasADi 框架\n\nL4CasADi 能够将 PyTorch 训练的模型无缝集成到 CasADi 中，从而实现高效且可能由硬件加速的数值优化。对 PyTorch 模型的唯一要求是可导出和可微分。\n\n\u003Cdiv align=\"center\">\n  \u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002FTim-Salzmann_l4casadi_readme_9be9d2a7b2b6.gif\" alt=\"通过 NeRF 的无碰撞最小化弹跳轨迹\" width=\"200\" height=\"200\">\n  \u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002FTim-Salzmann_l4casadi_readme_c9ca6a32716c.gif\" alt=\"湍流中节能的鱼类导航\" width=\"400\" height=\"200\">\n  \u003Cbr>\u003Ca target=\"_blank\" href=\"https:\u002F\u002Fcolab.research.google.com\u002Fgithub\u002FTim-Salzmann\u002Fl4casadi\u002Fblob\u002Fmain\u002Fexamples\u002Fnerf_trajectory_optimization\u002FNeRF_Trajectory_Optimization.ipynb\">\u003Cimg src=\"https:\u002F\u002Fcolab.research.google.com\u002Fassets\u002Fcolab-badge.svg\" alt=\"在 Colab 中打开\"\u002F>\u003C\u002Fa>\n  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n  \u003Ca target=\"_blank\" href=\"https:\u002F\u002Fcolab.research.google.com\u002Fgithub\u002FTim-Salzmann\u002Fl4casadi\u002Fblob\u002Fmain\u002Fexamples\u002Ffish_turbulent_flow\u002FFish_Turbulent_Flow.ipynb\">\u003Cimg src=\"https:\u002F\u002Fcolab.research.google.com\u002Fassets\u002Fcolab-badge.svg\" alt=\"在 Colab 中打开\"\u002F>\u003C\u002Fa>\n  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n  \u003Cp>\u003Ci>两个 L4CasADi 示例：通过 NeRF 的无碰撞轨迹以及在湍流中的导航\u003C\u002Fi>\u003C\u002Fp>\n\u003C\u002Fdiv>\n\narXiv: [Learning for CasADi：数值优化中的数据驱动模型](https:\u002F\u002Farxiv.org\u002Fpdf\u002F2312.05873.pdf)\n\n演讲：[Youtube](https:\u002F\u002Fyoutu.be\u002FUYdkRnGr8eM?si=KEPcFEL9b7Vk2juI&t=3348)\n\n## L4CasADi v2 的重大变更\n根据首批用例的反馈，L4CasADi v2 在设计时充分考虑了效率与简洁性。\n\n这带来了以下几项重大变更：\n\n- L4CasADi v2 可以利用 PyTorch 的批处理功能来提升效率。当设置 `batched=True` 时，L4CasADi 会将输入张量的**第一个维度**视为批次维度。因此，该维度上的各元素之间的一阶和二阶导数都被假定为**稀疏零矩阵**。为了充分利用这一点，在你的 CasADi 程序中，不要多次调用 L4CasADi 函数，而是将所有输入批量合并后只进行一次 L4CasADi 调用。例如，对比[非批处理的 NeRF 示例](examples\u002Fnerf_trajectory_optimization\u002Fnerf_trajectory_optimization.py)和[批处理的 NeRF 示例](examples\u002Fnerf_trajectory_optimization\u002Fnerf_trajectory_optimization_batched.py)，后者速度提升了 5 到 10 倍。\n- L4CasADi v2 不再改变输入的形状，因为这一行为曾引发混淆。传递给 PyTorch 模型的张量将完全匹配 CasADi 输入变量的**精确维度**。你需要确保 PyTorch 模型能够正确处理**二维**输入矩阵！相应地，参数 `model_expects_batch_dim` 已被移除。\n- 默认情况下，L4CasADi v2 不会提供 Hessian 矩阵，而只会提供伴随梯度的雅可比矩阵。对于大多数优化问题来说，这已经足够了。不过，你也可以通过设置 `generate_jac_jac=True` 来显式请求生成 Hessian 矩阵。\n\n[\u002F\u002F]: # (L4CasADi v2 可以从 PyTorch 2.4 开始使用新的 **torch compile** 功能。只需将 `scripting=False` 即可。这会导致首次调用 L4CasADi 函数时编译时间较长，但整体执行速度会更快。然而，目前该功能仍处于实验阶段，并非所有模型都能稳定运行。长期来看，一旦 Torch 开发者团队将此功能稳定下来，它很可能会取代 scripting 成为默认选项。)\n\n## 目录\n- [使用 L4CasADi 的项目](#projects-using-l4casadi)\n- [安装](#installation)\n    - [先决条件](#prerequisites)\n    - [Windows 安装](#windows-installation)\n    - [仅 CPU 的 pip 安装](#pip-install-cpu-only)\n    - [仅 CPU 的源码安装](#from-source-cpu-only)\n    - [GPU (CUDA)](#gpu-cuda)\n- [快速入门](#quick-start)\n- [在线学习](#online-learning-and-updating)\n- [Naive L4CasADi](#naive-l4casadi) - 适用于小型多层感知器模型。\n- [实时 L4CasADi](#real-time-l4casadi) - 适用于结合 Acados 的快速 MPC。\n- [示例](#examples)\n\n如果你使用了本框架，请引用以下两篇论文：\n\n```\n@article{salzmann2023neural,\n  title={实时神经网络 MPC：用于四旋翼无人机及敏捷机器人平台的深度学习模型预测控制},\n  author={Salzmann, Tim 和 Kaufmann, Elia 和 Arrizabalaga, Jon 和 Pavone, Marco 和 Scaramuzza, Davide 和 Ryll, Markus},\n  journal={IEEE Robotics and Automation Letters},\n  doi={10.1109\u002FLRA.2023.3246839},\n  year={2023}\n}\n```\n\n```\n@inproceedings{{salzmann2024l4casadi,\n  title={Learning for CasADi：数值优化中的数据驱动模型},\n  author={Salzmann, Tim 和 Arrizabalaga, Jon 和 Andersson, Joel 和 Pavone, Marco 和 Ryll, Markus},\n  booktitle={动力学与控制学习会议 (L4DC)},\n  year={2024}\n}\n```\n\n## 使用 L4CasADi 的项目\n- 实时神经网络 MPC：用于四旋翼无人机及敏捷机器人平台的深度学习模型预测控制\u003Cbr\u002F> [论文](https:\u002F\u002Farxiv.org\u002Fpdf\u002F2203.07747.pdf) | [代码](https:\u002F\u002Fgithub.com\u002FTUM-AAS\u002Fneural-mpc)\n- AC4MPC：用于非线性模型预测控制的演员-评论家强化学习\u003Cbr\u002F> [论文](https:\u002F\u002Farxiv.org\u002Fpdf\u002F2406.03995)\n- 基于强化学习的 MPC，结合神经动力学模型\u003Cbr\u002F> [论文](https:\u002F\u002Ffolk.ntnu.no\u002Fskoge\u002Fpublications\u002F2024\u002Fadhau-ecc24\u002FECC24_0903_FI.pdf)\n- 用于障碍物感知局部运动规划的神经势场\u003Cbr\u002F> [论文](https:\u002F\u002Farxiv.org\u002Fpdf\u002F2310.16362.pdf) | [视频](https:\u002F\u002Fwww.youtube.com\u002Fwatch?v=KL3bfvUwGqs) | [代码](https:\u002F\u002Fgithub.com\u002Fcog-isa\u002FNPField)\n- 基于深度神经网络的 N-MPC，利用深度图像实现避障\u003Cbr\u002F> [论文](https:\u002F\u002Farxiv.org\u002Fpdf\u002F2402.13038.pdf) | [代码](https:\u002F\u002Fgithub.com\u002Fntnu-arl\u002Fcolpred_nmpc)\n- 基于 NNMPC 的自动驾驶规划与跟踪一体化框架，考虑路面变化\u003Cbr\u002F> [论文](https:\u002F\u002Fieeexplore.ieee.org\u002Fabstract\u002Fdocument\u002F10571565)\n\n如果你的项目正在使用 L4CasADi，并希望在此处展示，请联系我们。\n\n---\n## 安装\n\n### 前置条件\n无论您是从源代码安装还是通过 pip 安装，都需要满足以下要求：\n\n- 可用的构建系统：兼容 CMake 的 C++ 编译器。\n  - **在 Linux\u002FmacOS 上：** 建议使用 GCC 10 或更高版本。\n  - **在 Windows 上：** 必须安装 CMake 和 **Visual C++ 构建工具**（请勿使用 GCC 编译器）。\n- 在您的 Python 环境中安装 PyTorch (`>=2.0`)。\\\n`python -c \"import torch; print(torch.__version__)\"`\n\n### Windows 安装\n对于在 Windows 上安装的用户，请确保执行以下步骤：\n\n1. **安装 CMake 和 Visual C++ 构建工具：**\n   下载并安装 [CMake](https:\u002F\u002Fcmake.org\u002Fdownload\u002F) 和 [Visual C++ 构建工具](https:\u002F\u002Fvisualstudio.microsoft.com\u002Fvisual-cpp-build-tools\u002F)。\n2. **确保工具已添加到 PATH：**\n   确保 CMake 和 Visual C++ 构建工具都已添加到系统的 PATH 中。这可以通过安装程序（选择更新 PATH 的选项）或手动完成。\n3. **注意文件路径长度：**\n   在构建 L4CasADi 模型时，确保目录路径不超过 Windows 的最大字符限制。过长的路径可能会导致构建问题，因此尽可能选择路径较短的目录。\n\n### 使用 pip 安装（仅 CPU）\n- 确保已安装 Torch 的 CPU 版本：\n  `pip install torch>=2.0 --index-url https:\u002F\u002Fdownload.pytorch.org\u002Fwhl\u002Fcpu`\n- 确保所有构建依赖项已安装：\n  ```\n  setuptools>=68.1\n  scikit-build>=0.17\n  cmake>=3.27\n  ninja>=1.11\n  ```\n- 运行：\n  `pip install l4casadi --no-build-isolation`\n\n### 从源代码安装（仅 CPU）\n- 克隆仓库：\n  `git clone https:\u002F\u002Fgithub.com\u002FTim-Salzmann\u002Fl4casadi.git`\n- 通过以下命令安装所有构建依赖项：\n  `pip install -r requirements_build.txt`\n- 从源代码构建：\n  `pip install . --no-build-isolation`\n\n`--no-build-isolation` 标志是 L4CasADi 找到并链接已安装的 PyTorch 所必需的。\n\n### GPU（CUDA）\nCUDA 的安装需要安装 nvcc，它是 CUDA 工具包的一部分，在 Linux 上可以通过以下命令安装：\n`sudo apt-get -y install cuda-toolkit-XX-X`（其中 `XX-X` 是您已安装的 CUDA 版本，例如 `12-3`）。安装 CUDA 工具包后，nvcc 通常位于 `\u002Fusr\u002Flocal\u002Fcuda\u002Fbin\u002Fnvcc`。\n\n确保可以执行 `nvcc -V`，然后运行 `pip install l4casadi --no-build-isolation` 或者 `CUDACXX=\u003CPATH_TO_NVCC> pip install . --no-build-isolation` 来从源代码构建。\n\n如果 `nvcc` 不在您的路径中，您可以为 L4CasADi 指定 `nvcc` 的路径。例如：\n`CUDACXX=\u003CPATH_TO_NVCC> pip install l4casadi --no-build-isolation`。\n\n---\n\n## 快速入门\n在 Python 中定义一个 L4CasADi 模型，只需提供一个预先定义好的 PyTorch 模型即可，操作非常简单：\n```python\nimport l4casadi as l4c\n\nl4c_model = l4c.L4CasADi(pyTorch_model, device='cpu')\n```\n\n其中，PyTorch 模型的架构没有限制，大型模型可以借助专用硬件加速。\n\n---\n\n## 在线学习与更新\nL4CasADi 支持在 CasADi 图中在线更新 PyTorch 模型。要使用此功能，在初始化 L4CasADi 时传递 `mutable=True`。要更新模型，调用 `L4CasADi` 对象上的 `update` 函数。您可以选择性地传入更新后的模型作为参数。如果不传入模型，则假定在初始化时传入的参考模型已被更新，并将用于更新。\n\n---\n\n## Naive L4CasADi\n虽然 L4CasADi 在设计时考虑了效率，内部利用了 PyTorch 的 C++ 接口，但这仍然可能带来一定的开销，对于小型、简单的模型来说，这种开销可能会显得过大。因此，L4CasADi 还提供了一个 `NaiveL4CasADiModule`，它直接使用 CasADi 操作重新创建 PyTorch 的计算图，并复制权重——从而生成纯 C 计算图，避免了与 PyTorch 的上下文切换。然而，这种方法仅限于一小部分预定义的 PyTorch 操作——仅支持 `MultiLayerPerceptron` 模型和 CPU 推理。\n\n当网络层数少于三层，且每层仅有 64 个神经元（或等效规模）时，PyTorch 框架本身的开销会占据主导地位。对于小于该规模的模型，我们建议使用 NaiveL4CasADiModule。而对于更大的模型，这种开销则变得可以忽略，此时应使用 L4CasADi。\n\nhttps:\u002F\u002Fgithub.com\u002FTim-Salzmann\u002Fl4casadi\u002Fblob\u002Ff7b16fba90f4d3ee53217b560f26b47e6b23e44a\u002Fexamples\u002Fnaive\u002Freadme.py#L5-L9\n\n---\n## 实时 L4CasADi\n实时 L4CasADi（原 [ML-CasADi](https:\u002F\u002Fgithub.com\u002FTUM-AAS\u002Fml-casadi) 中的“近似”方法）是驱动 [实时神经 MPC](https:\u002F\u002Farxiv.org\u002Fpdf\u002F2203.07747) 的底层框架。它用局部泰勒近似替换复杂模型。对于某些优化过程（如具有多个射击节点的 MPC），这可以显著提高优化速度。然而，“实时 L4CasADi”有许多限制（仅支持 Python，无法生成 C(++) 代码等），因此它并不能完全替代 L4CasADi。相反，它是一个针对特定应用场景的补充框架。\n\n更多信息请参见 [这里](l4casadi\u002Frealtime)。\n\nhttps:\u002F\u002Fgithub.com\u002FTim-Salzmann\u002Fl4casadi\u002Fblob\u002Ff7b16fba90f4d3ee53217b560f26b47e6b23e44a\u002Fl4casadi\u002Frealtime\u002Fexamples\u002Freadme.py#L32-L43\n\n---\n\n## 示例\nhttps:\u002F\u002Fgithub.com\u002FTim-Salzmann\u002Fl4casadi\u002Fblob\u002Ff7b16fba90f4d3ee53217b560f26b47e6b23e44a\u002Fexamples\u002Freadme.py#L28-L40\n\n请注意，仅支持 `casadi.MX` 符号变量作为输入。\n多输入多输出函数可以通过在传递给模型时连接符号输入，并在 PyTorch 函数内部将其拆分来实现。\n要使用 GPU（CUDA），只需在 `L4CasADi` 构造函数中传递 `device=\"cuda\"` 即可。\n更多示例：\n- 无碰撞最小化 snap 优化轨迹穿过 NeRF：[examples\u002Fnerf_trajectory_optimization](\u002Fexamples\u002Fnerf_trajectory_optimization)\n- 节能鱼类在湍流中的导航：[examples\u002Ffish_turbulent_flow](\u002Fexamples\u002Ffish_turbulent_flow)\n- 使用 L4CasADi 模型作为目标函数和约束的简单非线性规划：[examples\u002Fsimple_nlp.py](\u002Fexamples\u002Fsimple_nlp.py)\n- L4CasADi 在纯 C(++) 项目中的应用：[examples\u002Fcpp_executable](\u002Fexamples\u002Fcpp_usage)\n- 在 Matlab 中使用 PyTorch L4CasADi 模型：[examples\u002Fmatlab](\u002Fexamples\u002Fmatlab)\n\n---\n\n## Acados 集成\n要将本框架与 Acados 配合使用，请按照以下步骤操作：\n- 按照 [安装说明](https:\u002F\u002Fdocs.acados.org\u002Finstallation\u002Findex.html) 进行安装。\n- 安装 [Python 接口](https:\u002F\u002Fdocs.acados.org\u002Fpython_interface\u002Findex.html)。\n- 确保正确设置 `LD_LIBRARY_PATH`（在 macOS 上为 `DYLD_LIBRARY_PATH`）。\n- 确保正确设置 `ACADOS_SOURCE_DIR`。\n\n关于如何在 Acados 框架中将 PyTorch 模型用作模型预测控制的动力学模型的示例，可在 [examples\u002Facados.py](\u002Fexamples\u002Facados.py) 中找到。\n\n若要将 L4CasADi 与 Acados 结合使用，需在 `AcadosOcp.solver_options` 中相应地设置 `model_external_shared_lib_dir` 和 `model_external_shared_lib_name`。\n\n```python\nocp.solver_options.model_external_shared_lib_dir = l4c_model.shared_lib_dir\nocp.solver_options.model_external_shared_lib_name = l4c_model.name\n```\n\n[GitHub 链接](https:\u002F\u002Fgithub.com\u002FTim-Salzmann\u002Fl4casadi\u002Fblob\u002Ff7b16fba90f4d3ee53217b560f26b47e6b23e44a\u002Fexamples\u002Facados.py#L156-L160)\n\n---\n\n## 补充信息\n\n\n### 预热\n\n请注意，PyTorch 会在首次执行时构建计算图。因此，首次调用 CasADi 函数时会比较慢。您可以在实际使用之前，先调用生成的 CasADi 函数一到多次，以完成预热并进入优化的执行状态。","# L4CasADi 快速上手指南\n\nL4CasADi 是一个能够将 PyTorch 训练的深度学习模型无缝集成到 CasADi 数值优化框架中的工具。它支持硬件加速，适用于需要数据驱动模型的复杂优化问题（如模型预测控制 MPC、轨迹规划等）。\n\n## 环境准备\n\n在开始之前，请确保您的系统满足以下要求：\n\n### 系统要求\n- **操作系统**：Linux, macOS 或 Windows。\n- **编译器**：\n  - **Linux\u002FmacOS**：推荐 GCC 10 或更高版本。\n  - **Windows**：必须安装 **CMake** 和 **Visual C++ Build Tools**（不要使用 GCC）。\n- **Python 版本**：建议 Python 3.8+。\n\n### 前置依赖\n- **PyTorch**：版本需 `>=2.0`。\n  ```bash\n  python -c \"import torch; print(torch.__version__)\"\n  ```\n- **构建工具**（用于从源码编译或 pip 安装时的构建过程）：\n  ```bash\n  setuptools>=68.1\n  scikit-build>=0.17\n  cmake>=3.27\n  ninja>=1.11\n  ```\n\n> **注意**：Windows 用户请确保 CMake 和 Visual C++ Build Tools 已添加到系统环境变量 `PATH` 中，且项目路径不要超过 Windows 的最大字符长度限制。\n\n## 安装步骤\n\n### 方案一：CPU 版本安装（推荐大多数用户）\n\n如果您仅需在 CPU 上运行，或者尚未安装 GPU 版本的 PyTorch，请按以下步骤操作：\n\n1. **安装 CPU 版 PyTorch**（使用官方源或国内镜像）：\n   ```bash\n   # 使用官方源\n   pip install torch>=2.0 --index-url https:\u002F\u002Fdownload.pytorch.org\u002Fwhl\u002Fcpu\n   \n   # 或使用清华大学镜像加速\n   pip install torch>=2.0 --index-url https:\u002F\u002Fpypi.tuna.tsinghua.edu.cn\u002Fsimple\n   ```\n\n2. **安装构建依赖**：\n   ```bash\n   pip install setuptools>=68.1 scikit-build>=0.17 cmake>=3.27 ninja>=1.11\n   ```\n\n3. **安装 L4CasADi**：\n   > **关键**：必须添加 `--no-build-isolation` 参数，以便 L4CasADi 能正确链接已安装的 PyTorch。\n   ```bash\n   pip install l4casadi --no-build-isolation\n   ```\n\n### 方案二：GPU (CUDA) 版本安装\n\n如果您需要使用 GPU 加速，请先确保安装了完整的 CUDA Toolkit 和 `nvcc` 编译器。\n\n1. **安装 CUDA Toolkit** (以 Ubuntu 为例)：\n   ```bash\n   sudo apt-get -y install cuda-toolkit-12-3  # 请将版本号替换为您需要的版本\n   ```\n   验证安装：`nvcc -V` 应能正常输出版本信息。\n\n2. **安装 L4CasADi**：\n   如果 `nvcc` 不在系统路径中，需指定路径：\n   ```bash\n   export CUDACXX=\u002Fusr\u002Flocal\u002Fcuda\u002Fbin\u002Fnvcc  # 替换为实际 nvcc 路径\n   pip install l4casadi --no-build-isolation\n   ```\n   或者直接指定变量安装：\n   ```bash\n   CUDACXX=\u002Fusr\u002Flocal\u002Fcuda\u002Fbin\u002Fnvcc pip install l4casadi --no-build-isolation\n   ```\n\n### 方案三：从源码安装\n\n如需使用最新开发版功能：\n\n```bash\ngit clone https:\u002F\u002Fgithub.com\u002FTim-Salzmann\u002Fl4casadi.git\ncd l4casadi\npip install -r requirements_build.txt\npip install . --no-build-isolation\n```\n\n## 基本使用\n\nL4CasADi 的核心用法非常简单：只需将一个标准的 PyTorch 模型包装即可在 CasADi 中使用。\n\n### 1. 定义 PyTorch 模型\n首先定义并训练好你的 PyTorch 模型（必须是可微分且可追踪的）。\n\n```python\nimport torch\nimport torch.nn as nn\n\nclass SimpleNet(nn.Module):\n    def __init__(self):\n        super(SimpleNet, self).__init__()\n        self.fc = nn.Linear(2, 1)\n\n    def forward(self, x):\n        return self.fc(x)\n\npytorch_model = SimpleNet()\n# 假设这里已经加载了训练好的权重\n# pytorch_model.load_state_dict(...) \npytorch_model.eval()\n```\n\n### 2. 包装为 L4CasADi 模型\n使用 `l4c.L4CasADi` 进行包装。\n\n```python\nimport l4casadi as l4c\nimport casadi as ca\n\n# 初始化 L4CasADi 模型\n# device 可以是 'cpu' 或 'cuda'\nl4c_model = l4c.L4CasADi(pytorch_model, device='cpu')\n\n# 创建 CasADi 符号变量\nx = ca.SX.sym('x', 2)\n\n# 将 CasADi 符号传入 L4CasADi 模型，生成 CasADi 函数\n# 此时 l4c_model(x) 返回的是一个 CasADi 表达式\ncasadi_expr = l4c_model(x)\n\n# 创建 CasADi 函数用于数值计算或优化\nf = ca.Function('f', [x], [casadi_expr])\n\n# 测试运行\nimport numpy as np\ntest_input = np.array([1.0, 2.0])\nresult = f(test_input)\nprint(f\"Optimization result: {result}\")\n```\n\n### 进阶提示：在线更新模型\n如果需要在优化过程中动态更新神经网络权重（例如在线学习），请在初始化时设置 `mutable=True`：\n\n```python\nl4c_model = l4c.L4CasADi(pytorch_model, device='cpu', mutable=True)\n\n# ... 在优化循环中 ...\n# 更新模型权重\nnew_weights = ... \npytorch_model.load_state_dict(new_weights)\nl4c_model.update()  # 通知 CasADi 图更新\n```\n\n### 性能优化建议\n- **批量处理 (Batching)**：L4CasADi v2 支持批量输入以提升效率。如果您的应用场景涉及多个采样点，请将输入维度合并，并在调用时利用批处理能力，这通常能带来 5-10 倍的速度提升。\n- **小模型选择**：如果您的网络非常小（少于 3 层隐藏层，每层少于 64 神经元），PyTorch 的调用开销可能占主导。此时建议使用 `NaiveL4CasADiModule`，它将直接转换为纯 CasADi\u002FC 代码，但仅支持简单的 MLP 结构。","某自动驾驶团队正在开发一套基于神经辐射场（NeRF）的无人机复杂环境轨迹规划系统，需要利用深度学习模型感知障碍物并实时生成无碰撞的最优飞行路径。\n\n### 没有 l4casadi 时\n- **框架割裂严重**：感知模块用 PyTorch 训练，规划模块用 CasADi 求解，两者无法直接互通，工程师需手动导出权重并重写前向传播逻辑，极易出错。\n- **梯度计算困难**：为了在优化过程中考虑神经网络预测的不确定性，需要计算模型输出的高阶导数，手动推导或近似差分不仅精度低且耗时巨大。\n- **实时性差**：由于无法利用 GPU 加速优化过程中的模型推理，每次迭代都要在 CPU 上串行调用模型，导致规划频率远低于控制需求，无人机容易撞机。\n- **维护成本高**：一旦感知模型结构更新，整个优化代码库都需要重新适配和调试，开发迭代周期长达数周。\n\n### 使用 l4casadi 后\n- **无缝集成**：l4casadi 直接将可微分的 PyTorch 模型封装为 CasADi 函数，无需重写任何代码，实现了“训练即部署”的流畅工作流。\n- **自动高阶微分**：借助 l4casadi 的自动微分能力，系统能精确高效地计算神经网络输出的雅可比矩阵甚至海森矩阵，显著提升了优化收敛速度和轨迹平滑度。\n- **硬件加速与批处理**：利用 l4casadi v2 的批处理特性，将多个采样点打包一次性送入 GPU 推理，结合 CasADi 的高效求解器，规划速度提升了 5-10 倍，满足实时避障需求。\n- **敏捷迭代**：研究人员可以随意调整 PyTorch 模型架构，l4casadi 自动适配维度变化，让算法验证周期从数周缩短至数小时。\n\nl4casadi 通过打通深度学习与数值优化的壁垒，让数据驱动的高性能实时控制变得简单且高效。","https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002FTim-Salzmann_l4casadi_c9ca6a32.gif","Tim-Salzmann","Tim Salzmann","https:\u002F\u002Foss.gittoolsai.com\u002Favatars\u002FTim-Salzmann_4c434df3.png",null,"https:\u002F\u002Fgithub.com\u002FTim-Salzmann",[79,83,87],{"name":80,"color":81,"percentage":82},"Python","#3572A5",67.6,{"name":84,"color":85,"percentage":86},"C++","#f34b7d",31.1,{"name":88,"color":89,"percentage":90},"CMake","#DA3434",1.4,568,47,"2026-04-07T09:24:17","MIT",4,"Linux, macOS, Windows","非必需。如需 GPU 加速，需要安装 NVIDIA CUDA Toolkit (包含 nvcc)，具体版本需与已安装的 PyTorch CUDA 版本匹配（例如 cuda-12-3）。未指定具体显卡型号和显存大小。","未说明",{"notes":100,"python":101,"dependencies":102},"1. 构建环境要求：必须安装 CMake 和兼容的 C++ 编译器（Linux\u002FmacOS 推荐 GCC 10+；Windows 必须安装 Visual C++ Build Tools，不可使用 GCC）。2. Windows 用户需注意文件路径长度限制，过长的路径可能导致构建失败。3. 安装时必须添加 '--no-build-isolation' 参数，以便正确链接已安装的 PyTorch。4. PyTorch 模型必须是可追踪（traceable）且可微分的。5. v2 版本中，传入的 PyTorch 模型必须能处理二维输入矩阵。","未说明 (需兼容 PyTorch >=2.0)",[103,104,105,106,107,108],"torch>=2.0","casadi","setuptools>=68.1","scikit-build>=0.17","cmake>=3.27","ninja>=1.11",[14],[111,104,112,113,114,115,116,117,118,119,120,121],"acados","learning-control","machine-learning","optimization","optimization-algorithms","cplusplus","deep-learning","python","pytorch","optimization-methods","learning-for-control","2026-03-27T02:49:30.150509","2026-04-09T05:24:01.817097",[125,130,135,140,145,149],{"id":126,"question_zh":127,"answer_zh":128,"source_url":129},25779,"如何将 L4Casadi 生成的 C 代码集成到现有的 C++ CMake 项目中？","维护者已在 `examples\u002Fcpp_usage` 目录中提供了具体的 C++ 集成示例。此外，确保在构建时正确设置 `LD_LIBRARY_PATH` 环境变量，以便系统能找到所需的共享库。如果之前构建失败，建议删除 `.\u002F_skbuild` 文件夹或重新克隆项目到空文件夹中进行干净构建。","https:\u002F\u002Fgithub.com\u002FTim-Salzmann\u002Fl4casadi\u002Fissues\u002F9",{"id":131,"question_zh":132,"answer_zh":133,"source_url":134},25780,"在 Windows 上使用 pip 安装 L4Casadi 时遇到 CMake 构建错误怎么办？","Windows 支持已通过 PR #60 添加。如果仍然遇到构建错误，请确保您的 pip 和 setuptools 是最新版本（运行 `pip3 install --upgrade pip setuptools`），并且 Python 版本不低于 3.9。如果问题依旧，可能需要具备 Windows C\u002FC++ 开发经验的用户进一步调试，目前维护者表示没有简单的直接解决方案，欢迎提交 PR。","https:\u002F\u002Fgithub.com\u002FTim-Salzmann\u002Fl4casadi\u002Fissues\u002F24",{"id":136,"question_zh":137,"answer_zh":138,"source_url":139},25781,"如何在 Linux aarch64 架构机器上成功安装 L4Casadi？","请简化安装步骤：首先激活 Python 环境，然后运行 `pip install -r requirements_build.txt` 安装所有构建依赖。接着在当前目录下运行 `pip install . --no-build-isolation` 进行安装。确保系统已预装兼容的 PyTorch 版本（例如通过 `python3 -c \"import torch\"` 验证）。如果之前构建失败，请删除 `.\u002F_skbuild` 文件夹后重试。","https:\u002F\u002Fgithub.com\u002FTim-Salzmann\u002Fl4casadi\u002Fissues\u002F11",{"id":141,"question_zh":142,"answer_zh":143,"source_url":144},25782,"运行 acados.py 示例时出现 'Library not loaded: liblearned_dyn.dylib' 错误如何解决？","该错误通常是因为安装了 PyPi 上的发布版本而非最新的 GitHub 代码，导致生成的动态库不匹配。解决方法是：不要使用 `pip install l4casadi`，而是克隆仓库后在本地运行 `pip install . --no-build-isolation`（注意命令中的点 `.` 代表本地路径）。同时确保 `pip` 和 `setuptools` 已更新至最新版本，且 Python 版本 >= 3.9。","https:\u002F\u002Fgithub.com\u002FTim-Salzmann\u002Fl4casadi\u002Fissues\u002F21",{"id":146,"question_zh":147,"answer_zh":148,"source_url":144},25783,"为什么使用 'pip install l4casadi --no-build-isolation' 会报 metadata name 不一致的错误？","这是因为直接通过 pip 从 PyPi 安装特定版本时，元数据可能存在命名不一致的问题（expected 'l4casadi', but metadata has 'unknown'）。正确的做法是避免直接从 PyPi 安装带有 `--no-build-isolation` 参数的包，而是应该先 `git clone` 仓库，然后在本地目录执行 `pip install . --no-build-isolation` 来构建最新代码。",{"id":150,"question_zh":151,"answer_zh":152,"source_url":129},25784,"安装或构建失败时，通用的清理和重试步骤是什么？","如果遇到各种奇怪的构建或链接错误（如文件格式错误、符号未找到等），最常见的原因是之前的构建缓存损坏。通用的解决步骤是：删除项目根目录下的 `.\u002F_skbuild` 文件夹，或者直接将项目重新克隆到一个全新的空文件夹中，然后重新运行安装命令。这能确保从头开始进行干净的构建。",[]]