Skip to content
CoMMALabPublic

About

Trace Compilation of Python Algorithms into CUDA Code

Resources

Stars

2 stars

Watchers

0 watching

Forks

Repository files navigation

mantis: a algorithmic CUDA trace compiler for rigid-body robotics kernels

mantis traces rigid-body algorithms, written once at the level of spatial-algebra operations, and compiles them per robot into CUDA for a thread, a warp or a block. Like cricket, it folds the robot's constants into straight-line code. Like GRiD, it can also run inside one problem in parallel, following the kinematic tree. Parallelism tiers are modeled after GLASS.

stage module what it does
robot robot.py URDF → GRiD's URDFParser → per-joint constants (transform coefficients, subspace, inertia), the same model and joint order as GRiD
frontend trace.py, algorithms.py algorithms are plain Python over traced Scalar / Vec3 / Motion / Force / Inertia (Featherstone's rigid / articulated forms) / SE3; running one records the IR, and topology loops unroll
IR ir.py SSA graph at two levels: GLASS-named spatial ops tagged with joint / depth / subtree / pass, each carrying its folded scalar expansion (constant folding, sparsity, CSE at both levels, dead-output elimination) and a float64 interpreter
scheduler schedule.py coarsen (scalarise cheap single-consumer nodes), level by tree, longest-first lane assignment, minimal syncs, shared-memory slots by interval colouring
backend cuda.py one header per robot: <alg>_thread / _warp / _block, templated on T, y[i * ys] outputs; warps are ranks, P problems per warp (docs/tier_layouts.html)
tuning tune.py, harness.py compile, validate and time candidate layouts; the winner is cached per robot

Algorithms: frame_poses, frame_jacobian (LOCAL_WORLD_ALIGNED), rnea, crba, aba, rnea_derivatives (analytical), and sphere_collision (fused FK + collision check of an all-sphere robot against a VAMP-style world: robot spheres and self pairs folded in as constants; spheres, capsules, boxes, halfspaces and cylinders per call; output the minimum signed distance), ik_lm_step (one Levenberg-Marquardt IK step for a frame's pose target, projected onto the joint limits; the caller runs the loop), and GRiM's LS IK step as ik_ls_step (Jacobi-scaled normal equations over the frame's chain), split into ik_ls_system and ik_ls_solve so the two halves can run at different precisions, with ik_pose_cost for the line search.

GRiM's IK kernels (kernels/ik_mantis) read everything about the robot from ik_geometry: the stacked pose residual and Jacobian of several frames, the world centres of collision spheres, and every joint's world twist, from which a collision gradient is a dot product. ik_geometry_value is the same without the Jacobian, for line-search trials. With rot_joint, a per-call rot (wxyz) replaces that movable joint's parent-to-joint rotation at q = 0 (a floating base's chart reference). Fixed joints are folded into the joints below them, so they cannot take one. ik_sqp_system and ik_masked_solve are SQP's Jacobi-scaled QP and its branch-free active-set re-solve. analytic_ik_candidates and analytic_ik_chain are the closed-form IK of the Panda family (8 branches per redundancy value) and its sphere-placement chain.

Joints: revolute, continuous (a revolute joint without limits), prismatic, fixed, and mimic (except ABA).

Usage

import mantis

robot = mantis.Robot("panda.urdf")
src = mantis.generate(robot, {"rnea": {}, "crba": {}, "frame_jacobian": {"frame": "panda_joint7"}})

# or pick each algorithm's layout by measurement (cached under ~/.cache/mantis)
from mantis import tune
best = tune.tune(mantis.trace(robot, "rnea"), batch=1024)
src = mantis.generate(robot, {"rnea": {}}, variants={"rnea": [mantis.Variant("thread"), best.variant]})
namespace r = mantis::robots::panda;
r::rnea_thread<float>(q, qd, qdd, tau, ys);                // one problem per thread
// warp / block: tables (r::rnea_<tier>_tables elements, once per block) are filled once per
// launch by r::rnea_<tier>_load_tables(tables, rank, size) from the calling group, then a sync
r::rnea_warp<float>(q, qd, qdd, tau, smem, tables, ys);    // r::rnea_warp_problems per warp; r::rnea_warp_smem per warp
r::rnea_block<float>(q, qd, qdd, tau, smem, tables, ys);   // r::rnea_block_problems per block of r::rnea_block_threads
// every thread passes its own problem's pointers: with P problems per warp, lane l serves
// problem l / (32 / P)

The cooperative tiers end without a barrier. Sync before you read another lane's outputs or reuse smem.

mantis.generate(..., kinematics={"frames": [...], "end_effector": ...}) also emits cricket's float kinematics interface (frame_poses, ee_pose, ee_pose_jacobian), which GRiM's traced=True builds use.

Derivative kernels

Any algorithm can be differentiated ahead of time: order, wrt and mode in its options trace it, differentiate the SSA graph (mantis.ad), and compile the result like any other trace. Folding, sparsity and CSE run on primal and derivative together, so structural zeros of a Jacobian or Hessian vanish and nothing is stored at runtime. A derivative must be requested at build time.

# tau, d1_tau = d tau / d[q; qd] (n x 2n), d2_tau = its upper triangle (n x (2n)(2n+1)/2)
src = mantis.generate(robot, {"rnea": {"order": 2, "wrt": ("q", "qd"), "mode": "forward"}})

The functions are named <alg>_d<order> (rnea_d2_thread). Output d{k}_y holds, for each entry of y, the k-th partials over index tuples j1 <= ... <= jk in lexicographic order (mantis.ad.indices(n, k)). mode="reverse" sweeps once per output entry, so it is the better choice for scalar or few outputs.

Test

pip install -e .                       # numpy; URDFParser / RBDReference come from the GRiD checkout
python -m pytest tests/test_oracle.py  # interpreter vs RBDReference and Pinocchio (CPU)
python -m pytest tests/test_cuda.py    # every tier vs interpreter, bit-identity, invariance, -O0/-O3 guard

Every tier computes each output element with one lane and one fixed expression, so tiers and group sizes are bit-identical under -fmad=false.

Benchmarks

The benchmarks live in GRiM, under benchmarks/ (bash benchmarks/run_all.sh from the GRiM root reruns them), since they time GRiM's motion kernels built on mantis traces. They compare mantis against pRRTC, cuRobo, pyroki, GRiD/GRiM and frax on Panda (7 DOF), Baxter (14/15) and Unitree G1 with hands (43). All runs are float32 on one RTX A5000, every output is checked against Pinocchio, and the times are per call at large batch.

FK + environment collision, B = 16384 (ms per call)

robot mantis pRRTC cuRobo pyroki GRiM GRiM + mantis FK
panda 0.37 0.45 4.39 7.11 0.53 0.53
baxter 0.53 0.75 5.90 12.89 2.20 2.33
g1 0.31 0.85 5.06 11.37 1.82 2.09

IK, 1024 problems x 8 seeds (ms per call, success rate)

mantis traces GRiM's own LS algorithm at GRiM's precision (float kinematics, double solve), so the column measures tracing alone.

robot mantis GRiM pyroki
panda 14.3 (95.4%) 70.0 (95.4%) 842.1 (98.2%)
baxter 14.3 (95.8%) 124.0 (95.8%) 2807.4 (98.9%)
g1 23.8 (98.5%) 207.5 (98.5%) 5573.2 (100.0%)

Inverse dynamics (RNEA), B = 16384 (ms per call)

robot mantis GRiD frax
panda 0.045 1.715 1.365
baxter 0.059 3.634 3.106
g1 0.473 12.733 11.108

Fastest per robot in bold. At small batch mantis gives some of this back: GRiD is mostly faster at B <= 128, most of all on g1 dynamics derivatives, and GRiM's block tier is faster on Panda IK at 1 x 8.

Trace the whole computation. Swapping mantis's FK into GRiM's hand-written kernels gained 1.2-3x on IK, nothing on collision, and nothing on FK alone, where pRRTC and cuRobo stay fastest; tracing GRiM's whole IK step gained 4.9-8.7x at equal algorithm and precision.

GRiM now runs every IK method on mantis's ik_geometry. Over 4 robots, 3 target sets and 8 methods, at 16384 problems x 8 seeds, the float32 build wins 75 of 78 cells against the hand-written kernels' fastest tier: 73 at the thread tier and the 2 analytic IK cells at the block tier. The median speedup is 13x: 15-80x for LS, SQP and HJCD LM without collision, 4-10x for them with collision, 3-17x for DLS, MPPI and canonical IK. The hand-written kernel keeps multi-end-effector DLS (mantis is 1.3-1.8x slower there). The full table is GRiM's test/benchmarks/IK_RESULTS.md.

About

Trace Compilation of Python Algorithms into CUDA Code

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages