Skip to content

Fix #3368: clip hfield grid indices before int cast in MJX collision#3373

Open
Ashutosh0x wants to merge 1 commit into
google-deepmind:mainfrom
Ashutosh0x:fix/issue-3368
Open

Fix #3368: clip hfield grid indices before int cast in MJX collision#3373
Ashutosh0x wants to merge 1 commit into
google-deepmind:mainfrom
Ashutosh0x:fix/issue-3368

Conversation

@Ashutosh0x

Copy link
Copy Markdown
Contributor

Problem

In _hfield_collision, the computed grid indices cmin and rmin are float values that may be very large (e.g. +inf or beyond int32 range) when the colliding object is far outside the height field. Calling .astype(int) directly on such values produces an integer overflow, which generates a JAX/XLA warning and results in undefined behaviour on GPU targets.

Note: PR #3369 fixes a related overflow in the _box_box and _convex_convex paths. This PR addresses the remaining overflow in the hfield path.

Fixes #3368.

Changes

mjx/mujoco/mjx/_src/collision_convex.py

Clip cmin and rmin to a valid range before calling .astype(int):

# Before
cmin = jp.floor(...).astype(int)
rmin = jp.floor(...).astype(int)

# After
cmin = jp.clip(jp.floor(...), -(h.ncol - 1), h.ncol - 1).astype(int)
rmin = jp.clip(jp.floor(...), -(h.nrow - 1), h.nrow - 1).astype(int)

The clip bounds match the existing clamps inside make_prisms (jp.clip(ri, 0, h.nrow - 2) / jp.clip(ci, 0, h.ncol - 2)), so the observable collision result is unchanged for in-bounds objects. For out-of-bounds objects the prisms simply land on the nearest valid row/column, which is the correct fallback behaviour.

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.

MJX collision checking produces overflow casting warning

1 participant