Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into notebooks-mpc
Browse files Browse the repository at this point in the history
  • Loading branch information
Bourmindge Liey committed Jan 7, 2025
2 parents 0a58312 + 113e229 commit f4faf19
Show file tree
Hide file tree
Showing 49 changed files with 3,912 additions and 1,255 deletions.
66 changes: 66 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
FROM nvidia/cuda:12.6.3-devel-ubuntu22.04

# Prevent interactive prompts during package installation
ENV DEBIAN_FRONTEND=noninteractive

# Install system dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
python3.11 \
python3-pip \
python3.11-venv \
build-essential \
git \
curl \
ssh \
mesa-utils \
&& rm -rf /var/lib/apt/lists/* && apt-get clean

# Create symbolic links for python
RUN ln -sf /usr/bin/python3.11 /usr/bin/python \
&& ln -sf /usr/bin/python3.11 /usr/bin/python3

# Create non-root user
ARG USERNAME=vscode
ARG USER_UID=1000
ARG USER_GID=$USER_UID

RUN groupadd --gid $USER_GID $USERNAME \
&& useradd --uid $USER_UID --gid $USER_GID -m $USERNAME \
&& apt-get update \
&& apt-get install -y sudo \
&& echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \
&& chmod 0440 /etc/sudoers.d/$USERNAME

# Set environment variables for CUDA
ENV PATH="/usr/local/cuda-12.0/bin:${PATH}"
ENV LD_LIBRARY_PATH="/usr/local/cuda-12.0/lib64:${LD_LIBRARY_PATH}"

ENV NVIDIA_VISIBLE_DEVICES ${NVIDIA_VISIBLE_DEVICES:-all}
ENV NVIDIA_DRIVER_CAPABILITIES ${NVIDIA_DRIVER_CAPABILITIES:+$NVIDIA_DRIVER_CAPABILITIES,}graphics

# Create and set proper permissions for workspace directory
RUN mkdir -p /workspace && chown -R $USERNAME:$USERNAME /workspace && sudo chmod -R 777 /workspace

# Create virtual environment with proper permissions
ENV VIRTUAL_ENV=/home/vscode/venv
RUN python -m venv $VIRTUAL_ENV && \
chown -R $USERNAME:$USERNAME $VIRTUAL_ENV
ENV PATH="$VIRTUAL_ENV/bin:$PATH"

# Switch to non-root user
USER $USERNAME

# Install development tools
RUN pip install --no-cache-dir ruff

# Create a temporary directory for installation with proper permissions
WORKDIR /tmp/install

# Copy pyproject.toml with correct ownership
COPY --chown=$USERNAME:$USERNAME pyproject.toml .

# Install dependencies as non-root user
RUN pip install --no-cache-dir -e ".[test,gpu]"

# Set final working directory and ensure proper permissions
WORKDIR /workspace
58 changes: 58 additions & 0 deletions .devcontainer/devcontainer.linux.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"name": "Crazyflow Development",
"build": {
"dockerfile": "Dockerfile",
"context": ".."
},
"customizations": {
"vscode": {
"extensions": [
"ms-python.python",
"ms-python.pylance",
"charliermarsh.ruff",
"ms-vscode.test-adapter-converter",
"ms-python.pytest-adapter",
"nvidia.nsight-vscode-edition",
"ms-azuretools.vscode-docker",
"ms-toolsai.jupyter-renderers",
"ms-toolsai.jupyter",
"ms-toolsai.jupyter-keymap"
],
"settings": {
"python.defaultInterpreterPath": "/home/vscode/venv/bin/python",
"python.testing.pytestEnabled": true, // test framework
"python.testing.pytestArgs": [
"-m",
"not render"
],
"python.testing.unittestEnabled": false, // test framework
"python.testing.nosetestsEnabled": false, // test framework
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.ruff": true,
"source.organizeImports.ruff": true
},
"[python]": {
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.formatOnSave": true,
"editor.rulers": [
100
]
}
}
}
},
"containerEnv": {
"DISPLAY": "${localEnv:DISPLAY}",
"XAUTHORITY": "${localEnv:XAUTHORITY}"
},
"runArgs": [
"--interactive",
"--net=host",
"--ipc=host",
// "--gpus=all", //use only with GPU
// "--runtime=nvidia" //use only with GPU
],
"postCreateCommand": "python -m pip install -e '.[test,gpu]'",
"remoteUser": "vscode"
}
66 changes: 66 additions & 0 deletions .devcontainer/devcontainer.wsl2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"name": "Crazyflow Development",
"build": {
"dockerfile": "Dockerfile",
"context": ".."
},
"customizations": {
"vscode": {
"extensions": [
"ms-python.python",
"ms-python.pylance",
"charliermarsh.ruff",
"ms-vscode.test-adapter-converter",
"ms-python.pytest-adapter",
"nvidia.nsight-vscode-edition",
"ms-azuretools.vscode-docker",
"ms-toolsai.jupyter-renderers",
"ms-toolsai.jupyter",
"ms-toolsai.jupyter-keymap"
],
"settings": {
"python.defaultInterpreterPath": "/home/vscode/venv/bin/python",
"python.testing.pytestEnabled": true, // test framework
"python.testing.pytestArgs": [
"-m",
"not render"
],
"python.testing.unittestEnabled": false, // test framework
"python.testing.nosetestsEnabled": false, // test framework
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.ruff": true,
"source.organizeImports.ruff": true
},
"[python]": {
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.formatOnSave": true,
"editor.rulers": [
100
]
}
}
}
},
// mounts required for WSL2 X11
"mounts": [
"source=/tmp/.X11-unix,target=/tmp/.X11-unix,type=bind",
"source=/mnt/wslg,target=/mnt/wslg,type=bind",
"source=/usr/lib/wsl,target=/usr/lib/wsl,type=bind"
],"containerEnv": {
"DISPLAY": "${localEnv:DISPLAY}",
"XAUTHORITY": "${localEnv:XAUTHORITY}",
"WAYLAND_DISPLAY": "${localEnv:WAYLAND_DISPLAY}", // WSL2
"XDG_RUNTIME_DIR": "${localEnv:XDG_RUNTIME_DIR}", // WSL2
"PULSE_SERVER": "${localEnv:PULSE_SERVER}" // WSL2
},
"runArgs": [
"--interactive",
"--net=host",
"--ipc=host"
// "--gpus=all", //use only with GPU
// "--runtime=nvidia" //use only with GPU
],
"postCreateCommand": "python -m pip install -e '.[test,gpu]'",
"remoteUser": "vscode"
}
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
.vscode/
**.egg-info/
**/__pycache__/
logs
secrets
**/*.secret
saves
.pytest_cache
**/*.json
*.json
**/*.csv
build
.venv
!/.devcontainer/devcontainer.json
!/.devcontainer/devcontainer.linux.json
!/.devcontainer/devcontainer.wsl2.json
!/.vscode/launch.json
c_generated_code
16 changes: 16 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python Debugger: Current File",
"type": "debugpy",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": false
}
]
}
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,37 @@
### Using the project with VSCode devcontainers

**Running on CPU**: by default the containers run on CPU. You don't need to take any action.

**Running on GPU**: The devcontsainers can easily run using your computer's NVIDIA GPU on Linux and Windows. This makes sense if you want to accelerate simulation by running thousands of simulation in parallel. In order to work you need to install the [CUDA Toolkit](https://developer.nvidia.com/cuda-downloads?target_os=Linux&target_arch=x86_64&Distribution=WSL-Ubuntu&target_version=2.0&target_type=deb_local), [NVIDIA Container runtime](https://developer.nvidia.com/container-runtime) for your computer. Finally, enable GPU access to the devcontainers by setting the commented out `"--gpus=all"` and `"--runtime=nvidia"` flags in `devcontainer.json`.


**Linux**

1. Install [Docker](https://docs.docker.com/engine/install/) (, and make sure Docker Daemon is running)
2. Install [VSCode](https://code.visualstudio.com/), with [devcontainer extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers), and [remote dev pack](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-docker).
3. Clone this project's code. Rename `/.devcontainer/devcontainer.linux.json` to `/.devcontainer/devcontainer.json`.
4. Open this project in VSCode. VSCode should automatically detect the devcontainer and prompt you to `Reopen in container`. If not, see [here](https://code.visualstudio.com/docs/devcontainers/containers#_quick-start-open-an-existing-folder-in-a-container) to open manually. Note: Opening the container for the first time might take a while (up to 15 min), as the container is pulled from the web and build.

**Windows** (requires Windows 10 or later)

For windows, we require WSL2 to run the devcontainer. (So its actually Linux with extra steps.) Full instructions can be found [in the official docs](https://code.visualstudio.com/blogs/2020/07/01/containers-wsl#_getting-started). Here are the important steps:
1. Install [Docker](https://docs.docker.com/desktop/setup/install/windows-install/), and WSL2, and Ubuntu 22.04 LTS (, and make sure Docker Daemon is running)
2. Docker will recognize that you have WSL installed and prompt you via Windows Notifications to enable WSL integration -> confirm this with `Enable WSL integration`. If not, open `Docker Desktop`, navigate to the settings, and manually enable WSL integration. (There are TWO setting options for this. Make sure to enable BOTH!)
3. Install [VSCode](https://code.visualstudio.com/), with the [WSL extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-wsl), [devcontainer extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers), and [remote dev pack](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-docker).
4. Clone the source code for the exercises in the WSL2 file system to `/home` (`~`), or wherever you like. (Performance when working on the WSL file system is much better compared to Windows file system). You can access the WSL filesystem either by starting a WSL/Ubuntu console, or via the Windows File Explorer at `\\wsl.localhost\Ubuntu\home` (replace `Ubuntu` with your distro, if necessary).
7. Rename `/.devcontainer/devcontainer.windows.json` to `/.devcontainer/devcontainer.json`.
8. Open this project in VSCode. The easiest way to do so is by starting a WSL/Ubuntu shell, navigating via `cd` to the source code, then type `code .` to open VSCode. VSCode should automatically detect the devcontainer and prompt you to `Reopen in container`. If not, see [here](https://code.visualstudio.com/docs/devcontainers/containers#_quick-start-open-an-existing-folder-in-a-container) to open manually. Note: Opening the container for the first time might take a while (up to 15 min), as the container is pulled from the web and build.


**MacOS**

Should work like Linux. Untested.

____________

Known Issues:
- if building docker container fails at `RUN apt-get update`, make sure your host systems time is set correct: https://askubuntu.com/questions/1511514/docker-build-fails-at-run-apt-update-error-failed-to-solve-process-bin-sh

# crazyflow
Fast, parallelizable simulations of Crazyflies with JAX and MuJoCo.

Expand Down
35 changes: 15 additions & 20 deletions benchmark/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from ml_collections import config_dict

import crazyflow # noqa: F401, ensure gymnasium envs are registered
from crazyflow.sim.core import Sim
from crazyflow.sim import Sim


def analyze_timings(times: list[float], n_steps: int, n_worlds: int, freq: float) -> None:
Expand All @@ -19,8 +19,8 @@ def analyze_timings(times: list[float], n_steps: int, n_worlds: int, freq: float
tmax, idx_tmax = np.max(times), np.argmax(times)

# Check for significant variance
if tmax / tmin > 5:
print("Warning: step time varies by more than 5x. Is JIT compiling during the benchmark?")
if tmax / tmin > 10:
print("Warning: step time varies by more than 10x. Is JIT compiling during the benchmark?")
print(f"Times: max {tmax:.2e} @ {idx_tmax}, min {tmin:.2e} @ {idx_tmin}")

# Performance metrics
Expand All @@ -43,28 +43,23 @@ def profile_gym_env_step(sim_config: config_dict.ConfigDict, n_steps: int, devic
device = jax.devices(device)[0]

envs = gymnasium.make_vec(
"DroneReachPos-v0",
time_horizon_in_seconds=2,
return_datatype="numpy",
num_envs=sim_config.n_worlds,
**sim_config,
"DroneReachPos-v0", time_horizon_in_seconds=3, num_envs=sim_config.n_worlds, **sim_config
)

# Action for going up (in attitude control)
action = np.zeros((sim_config.n_worlds, 4), dtype=np.float32)
action[..., 0] = 0.3
# Step through env once to ensure JIT compilation
envs.reset_all(seed=42)
envs.step(action)
envs.reset(seed=42)
envs.step(action)

jax.block_until_ready(envs.unwrapped.sim.states.pos) # Ensure JIT compiled dynamics
jax.block_until_ready(envs.unwrapped.sim.data) # Ensure JIT compiled dynamics

# Step through the environment
for _ in range(n_steps):
tstart = time.perf_counter()
envs.step(action)
jax.block_until_ready(envs.unwrapped.sim.states.pos)
jax.block_until_ready(envs.unwrapped.sim.data)
times.append(time.perf_counter() - tstart)

envs.close()
Expand All @@ -83,14 +78,14 @@ def profile_step(sim_config: config_dict.ConfigDict, n_steps: int, device: str):

sim.reset()
sim.attitude_control(cmd)
sim.step()
jax.block_until_ready(sim.states.pos) # Ensure JIT compiled dynamics
sim.step(sim.freq // sim.control_freq)
jax.block_until_ready(sim.data) # Ensure JIT compiled dynamics

for _ in range(n_steps):
tstart = time.perf_counter()
sim.attitude_control(cmd)
sim.step()
jax.block_until_ready(sim.states.pos)
sim.step(sim.freq // sim.control_freq)
jax.block_until_ready(sim.data)
times.append(time.perf_counter() - tstart)

analyze_timings(times, n_steps, sim.n_worlds, sim.freq)
Expand All @@ -102,16 +97,16 @@ def main():
sim_config = config_dict.ConfigDict()
sim_config.n_worlds = 1
sim_config.n_drones = 1
sim_config.physics = "sys_id"
sim_config.physics = "analytical"
sim_config.control = "attitude"
sim_config.controller = "emulatefirmware"
sim_config.attitude_freq = 500
sim_config.device = device

print("Simulator performance")
profile_step(sim_config, 100, device)
profile_step(sim_config, 1000, device)

print("\nGymnasium environment performance")
profile_gym_env_step(sim_config, 100, device)
profile_gym_env_step(sim_config, 1000, device)


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit f4faf19

Please sign in to comment.