Skip to content

Cpp rollouts#101

Open
johnzhang3 wants to merge 12 commits intorai-opensource:mainfrom
johnzhang3:cpp-rollouts
Open

Cpp rollouts#101
johnzhang3 wants to merge 12 commits intorai-opensource:mainfrom
johnzhang3:cpp-rollouts

Conversation

@johnzhang3
Copy link
Copy Markdown
Contributor

add cpp rollout features

- Add rollout() and sim() C++ functions with Python bindings
- Add RolloutBackend class in judo.utils.mujoco_cpp module
- Add rollout_backend config option to ControllerConfig to select between 'mujoco' (default) and 'mujoco_cpp' backends
- Update README with instructions to build C++ extensions
- Requires building C++ extensions with 'pixi run build' or manual CMake build
Copy link
Copy Markdown
Collaborator

@alberthli alberthli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

High-level comments:

  1. need to resolve pip installation of the cpp package
  2. need to unify the rollout backend API
  3. need to add tests verifying that the parallel rollouts from your judo_cpp impl match the regular mujoco rollouts
  4. at least one person with more cpp competence than me should review this PR. not sure who, but please request it.

Comment thread CHANGELOG.md
@@ -1,3 +1,12 @@
# v0.0.6

## Added
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tag yourself in this update to make sure you get credit!

Comment thread pyproject.toml
Comment on lines +183 to +189
[tool.pixi.feature.cpp.dependencies]
cmake = "*"
ninja = "*"
pybind11 = "*"
cxx-compiler = "*"
c-compiler = "*"
eigen = "*"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can we move this above [tool.pixi.environments]? it's weird that any dependency group definition comes below the environments.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe Eigen is used here...

Comment thread README.md

# first time only
pre-commit install
pybind11-stubgen mujoco -o typings/
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after this block in the README, also explain how to clean the cpp build files using pixi run clean


self.rollout_backend = RolloutBackend(num_threads=self.optimizer_cfg.num_rollouts, backend=rollout_backend)
# Determine backend: config override > parameter > "mujoco"
backend: Literal["mujoco", "mujoco_cpp"] = controller_config.rollout_backend or rollout_backend or "mujoco"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for readability reasons, I'd prefer if the logic for choosing the rollout backend wasn't so pythonic. a more explicit if/elif/else block would make this a lot easier to maintain

if backend == "mujoco_cpp":
if CppRolloutBackend is None:
raise ImportError(
"judo_cpp module is not available. Please build the C++ extensions or use 'mujoco' backend."
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you might want to explain what command to run explicitly here and point the user to the README

Comment thread judo_cpp/bindings.cpp
Shutdown the persistent thread pool.

Call this function to clean up the persistent thread pool when done with rollouts.
The pool will be automatically recreated on the next call to persistent_cpp_rollout.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where is persistent_cpp_rollout defined?

Comment thread judo_cpp/bindings.cpp
R"doc(
Run parallel MuJoCo rollouts.

Args:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: incorrect indentation for Args

Comment thread judo_cpp/bindings.cpp
Comment on lines +65 to +68
models: length-B list of mujoco._structs.MjModel
data: length-B list of mujoco._structs.MjData
x0: 2D array of shape (B, nq+nv), batched initial [qpos;qvel]
controls: 3D array of shape (B, horizon, nu), batched control inputs
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: style-wise, we don't align text like this in python code/docstrings, so i'd prefer removing the extra tabbed whitespace in all these docstring descriptions (this goes for other stuff too)

Comment thread judo_cpp/bindings.cpp
Comment on lines +71 to +73
tuple of three np.ndarray:
states -> shape (B, horizon+1, nq+nv) - MuJoCo states (includes initial state)
sensors -> shape (B, horizon, nsensordata) - sensor data
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of saying this, just write out the elements of the tuple following the same style as the usual docstring in the package. also, this says you return three, but two things are written here.

Comment thread judo_cpp/bindings.cpp
R"doc(
Run a single MuJoCo simulation step.

Args:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: incorrect indentation

Copy link
Copy Markdown

@dyershov-bdai dyershov-bdai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit of a drive-by review. We need to talk a bit more about C++ best practices and arch. But this PR needs move work.

Comment thread judo_cpp/rollout.h
// Thread pool for persistent thread management
class PersistentThreadPool {
public:
PersistentThreadPool(int num_threads);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use rule-of-zero or rule-of-(five|seven).

Comment thread judo_cpp/rollout.h
py::array_t<double> make_array(std::vector<double>& buf, int B, int T, int D);

// Thread pool for persistent thread management
class PersistentThreadPool {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread judo_cpp/rollout.h
namespace py = pybind11;

// Utility function to create numpy arrays from C++ vectors
py::array_t<double> make_array(std::vector<double>& buf, int B, int T, int D);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No single letter variables.

Comment thread judo_cpp/rollout.h
Comment on lines +62 to +65
const std::vector<const mjModel*>& models,
const std::vector<mjData*>& data,
const py::array_t<double>& x0,
const py::array_t<double>& controls
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's unsafe to hold references as member values. You may run into dangling reference if the referenced value moves out of scope.

Comment thread judo_cpp/rollout.h
#include <unordered_map>

namespace py = pybind11;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please define namespace. Do not put public API into global namespace.

Comment thread judo_cpp/rollout.cpp
Comment on lines +57 to +58
active_workers_--;
completed_tasks_++;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer pre over post increment/decrement.

Comment thread judo_cpp/rollout.cpp
};

// Move buf onto the heap so we can own it in the capsule:
auto heap_buf = new std::vector<double>(std::move(buf));
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This move doesn't do anything.

Comment thread judo_cpp/rollout.cpp

// ========== End Thread Pool Implementation ==========

py::array_t<double> make_array(std::vector<double>& buf,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you wish to move, then change signature to

Suggested change
py::array_t<double> make_array(std::vector<double>& buf,
py::array_t<double> make_array(std::vector<double>&& buf,

Comment thread judo_cpp/rollout.cpp

auto controls_unchecked = controls.unchecked<3>();

#pragma omp parallel for
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are not going to use ThreadPoolManager, then why commit it in this PR?

Comment thread pyproject.toml
Comment on lines +183 to +189
[tool.pixi.feature.cpp.dependencies]
cmake = "*"
ninja = "*"
pybind11 = "*"
cxx-compiler = "*"
c-compiler = "*"
eigen = "*"
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe Eigen is used here...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants