Skip to content

Commit daa115e

Browse files
authored
refactor: privatize non-public solver internals (#1683)
1 parent 7e9778d commit daa115e

9 files changed

Lines changed: 96 additions & 94 deletions

File tree

asv/benchmarks/benchmark_mujoco.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,8 @@ def print_trace(trace, indent, steps):
484484
print_trace(sub_trace, indent + 1, steps)
485485
if indent == 0:
486486
step_time = trace["step"][0]
487-
mujoco_warp_step_time = trace["step"][1]["mujoco_warp_step"][0]
487+
step_trace = trace["step"][1]
488+
mujoco_warp_step_time = step_trace["_mujoco_warp_step"][0]
488489
overhead = 100.0 * (step_time - mujoco_warp_step_time) / step_time
489490
print("---------------------------------------------")
490491
print(f"Newton overhead:\t{overhead:.2f} %")

asv/benchmarks/simulation/bench_mujoco.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ def track_simulate(self, world_count):
181181
trace = tracer.add_trace(trace, tracer.trace())
182182

183183
step_time = trace["step"][0]
184-
mujoco_warp_step_time = trace["step"][1]["mujoco_warp_step"][0]
184+
step_trace = trace["step"][1]
185+
mujoco_warp_step_time = step_trace["_mujoco_warp_step"][0]
185186
overhead = 100.0 * (step_time - mujoco_warp_step_time) / step_time
186187
return overhead
187188

newton/_src/solvers/featherstone/solver_featherstone.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ def __init__(
122122

123123
self._step = 0
124124

125-
self.compute_articulation_indices(model)
126-
self.allocate_model_aux_vars(model)
125+
self._compute_articulation_indices(model)
126+
self._allocate_model_aux_vars(model)
127127

128128
if self.use_tile_gemm:
129129
# create a custom kernel to evaluate the system matrix for this type
@@ -140,7 +140,7 @@ def __init__(
140140
# todo: should not be necessary?
141141
wp.load_module(device=wp.get_device())
142142

143-
def compute_articulation_indices(self, model):
143+
def _compute_articulation_indices(self, model):
144144
# calculate total size and offsets of Jacobian and mass matrices for entire system
145145
if model.joint_count:
146146
self.J_size = 0
@@ -210,7 +210,7 @@ def compute_articulation_indices(self, model):
210210
self.articulation_dof_start = wp.array(articulation_dof_start, dtype=wp.int32, device=model.device)
211211
self.articulation_coord_start = wp.array(articulation_coord_start, dtype=wp.int32, device=model.device)
212212

213-
def allocate_model_aux_vars(self, model):
213+
def _allocate_model_aux_vars(self, model):
214214
# allocate mass, Jacobian matrices, and other auxiliary variables pertaining to the model
215215
if model.joint_count:
216216
# system matrices
@@ -244,7 +244,7 @@ def allocate_model_aux_vars(self, model):
244244
device=model.device,
245245
)
246246

247-
def allocate_state_aux_vars(self, model, target, requires_grad):
247+
def _allocate_state_aux_vars(self, model, target, requires_grad):
248248
# allocate auxiliary variables that vary with state
249249
if model.body_count:
250250
# joints
@@ -302,7 +302,7 @@ def step(
302302
model = self.model
303303

304304
if not getattr(state_aug, "_featherstone_augmented", False):
305-
self.allocate_state_aux_vars(model, state_aug, requires_grad)
305+
self._allocate_state_aux_vars(model, state_aug, requires_grad)
306306
if control is None:
307307
control = model.control(clone_variables=False)
308308

newton/_src/solvers/mujoco/solver_mujoco.py

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2091,34 +2091,34 @@ def __init__(
20912091
self.mjw_model.opt.run_collision_detection = use_mujoco_contacts
20922092

20932093
@event_scope
2094-
def mujoco_warp_step(self):
2094+
def _mujoco_warp_step(self):
20952095
self._mujoco_warp.step(self.mjw_model, self.mjw_data)
20962096

20972097
@event_scope
20982098
@override
20992099
def step(self, state_in: State, state_out: State, control: Control, contacts: Contacts, dt: float):
21002100
if self.use_mujoco_cpu:
2101-
self.apply_mjc_control(self.model, state_in, control, self.mj_data)
2101+
self._apply_mjc_control(self.model, state_in, control, self.mj_data)
21022102
if self.update_data_interval > 0 and self._step % self.update_data_interval == 0:
21032103
# XXX updating the mujoco state at every step may introduce numerical instability
2104-
self.update_mjc_data(self.mj_data, self.model, state_in)
2104+
self._update_mjc_data(self.mj_data, self.model, state_in)
21052105
self.mj_model.opt.timestep = dt
21062106
self._mujoco.mj_step(self.mj_model, self.mj_data)
2107-
self.update_newton_state(self.model, state_out, self.mj_data)
2107+
self._update_newton_state(self.model, state_out, self.mj_data)
21082108
else:
21092109
self.enable_rne_postconstraint(state_out)
2110-
self.apply_mjc_control(self.model, state_in, control, self.mjw_data)
2110+
self._apply_mjc_control(self.model, state_in, control, self.mjw_data)
21112111
if self.update_data_interval > 0 and self._step % self.update_data_interval == 0:
2112-
self.update_mjc_data(self.mjw_data, self.model, state_in)
2112+
self._update_mjc_data(self.mjw_data, self.model, state_in)
21132113
self.mjw_model.opt.timestep.fill_(dt)
21142114
with wp.ScopedDevice(self.model.device):
21152115
if self.mjw_model.opt.run_collision_detection:
2116-
self.mujoco_warp_step()
2116+
self._mujoco_warp_step()
21172117
else:
2118-
self.convert_contacts_to_mjwarp(self.model, state_in, contacts)
2119-
self.mujoco_warp_step()
2118+
self._convert_contacts_to_mjwarp(self.model, state_in, contacts)
2119+
self._mujoco_warp_step()
21202120

2121-
self.update_newton_state(self.model, state_out, self.mjw_data)
2121+
self._update_newton_state(self.model, state_out, self.mjw_data)
21222122
self._step += 1
21232123
return state_out
21242124

@@ -2135,7 +2135,7 @@ def enable_rne_postconstraint(self, state_out: State):
21352135
print("Setting model.sensor_rne_postconstraint True")
21362136
m.sensor_rne_postconstraint = True
21372137

2138-
def convert_contacts_to_mjwarp(self, model: Model, state_in: State, contacts: Contacts):
2138+
def _convert_contacts_to_mjwarp(self, model: Model, state_in: State, contacts: Contacts):
21392139
# Ensure the inverse shape mapping exists (lazy creation)
21402140
if self.newton_shape_to_mjc_geom is None:
21412141
self._create_inverse_shape_mapping()
@@ -2194,16 +2194,16 @@ def convert_contacts_to_mjwarp(self, model: Model, state_in: State, contacts: Co
21942194
@override
21952195
def notify_model_changed(self, flags: int):
21962196
if flags & SolverNotifyFlags.BODY_INERTIAL_PROPERTIES:
2197-
self.update_model_inertial_properties()
2197+
self._update_model_inertial_properties()
21982198
if flags & SolverNotifyFlags.JOINT_PROPERTIES:
2199-
self.update_joint_properties()
2199+
self._update_joint_properties()
22002200
if flags & SolverNotifyFlags.JOINT_DOF_PROPERTIES:
2201-
self.update_joint_dof_properties()
2201+
self._update_joint_dof_properties()
22022202
if flags & SolverNotifyFlags.SHAPE_PROPERTIES:
2203-
self.update_geom_properties()
2203+
self._update_geom_properties()
22042204
self.update_pair_properties()
22052205
if flags & SolverNotifyFlags.MODEL_PROPERTIES:
2206-
self.update_model_properties()
2206+
self._update_model_properties()
22072207
if flags & SolverNotifyFlags.CONSTRAINT_PROPERTIES:
22082208
self.update_eq_properties()
22092209
self.update_mimic_eq_properties()
@@ -2241,7 +2241,7 @@ def _data_is_mjwarp(data):
22412241
# Check if the data is a mujoco_warp Data object
22422242
return hasattr(data, "nworld")
22432243

2244-
def apply_mjc_control(self, model: Model, state: State, control: Control | None, mj_data: MjWarpData | MjData):
2244+
def _apply_mjc_control(self, model: Model, state: State, control: Control | None, mj_data: MjWarpData | MjData):
22452245
if control is None or control.joint_f is None:
22462246
if state.body_f is None:
22472247
return
@@ -2337,7 +2337,7 @@ def apply_mjc_control(self, model: Model, state: State, control: Control | None,
23372337
mj_data.ctrl[:] = ctrl.numpy().flatten()
23382338
mj_data.qfrc_applied[:] = qfrc.numpy()
23392339

2340-
def update_mjc_data(self, mj_data: MjWarpData | MjData, model: Model, state: State | None = None):
2340+
def _update_mjc_data(self, mj_data: MjWarpData | MjData, model: Model, state: State | None = None):
23412341
is_mjwarp = SolverMuJoCo._data_is_mjwarp(mj_data)
23422342
if is_mjwarp:
23432343
# we have an MjWarp Data object
@@ -2380,7 +2380,7 @@ def update_mjc_data(self, mj_data: MjWarpData | MjData, model: Model, state: Sta
23802380
mj_data.qpos[:] = qpos.numpy().flatten()[: len(mj_data.qpos)]
23812381
mj_data.qvel[:] = qvel.numpy().flatten()[: len(mj_data.qvel)]
23822382

2383-
def update_newton_state(
2383+
def _update_newton_state(
23842384
self,
23852385
model: Model,
23862386
state: State,
@@ -2468,7 +2468,7 @@ def update_newton_state(
24682468
)
24692469

24702470
@staticmethod
2471-
def find_body_collision_filter_pairs(
2471+
def _find_body_collision_filter_pairs(
24722472
model: Model,
24732473
selected_bodies: nparray,
24742474
colliding_shapes: nparray,
@@ -2504,7 +2504,7 @@ def find_body_collision_filter_pairs(
25042504
return body_exclude_pairs
25052505

25062506
@staticmethod
2507-
def color_collision_shapes(
2507+
def _color_collision_shapes(
25082508
model: Model, selected_shapes: nparray, visualize_graph: bool = False, shape_keys: list[str] | None = None
25092509
) -> nparray:
25102510
"""
@@ -3083,13 +3083,13 @@ def get_custom_attribute(name: str) -> nparray | None:
30833083
colliding_shapes_per_world = len(colliding_shapes)
30843084

30853085
# filter out non-colliding bodies using excludes
3086-
body_filters = self.find_body_collision_filter_pairs(
3086+
body_filters = self._find_body_collision_filter_pairs(
30873087
model,
30883088
selected_bodies,
30893089
colliding_shapes,
30903090
)
30913091

3092-
shape_color = self.color_collision_shapes(
3092+
shape_color = self._color_collision_shapes(
30933093
model, colliding_shapes, visualize_graph=False, shape_keys=model.shape_key
30943094
)
30953095

@@ -3819,9 +3819,9 @@ def get_body_name(body_idx: int) -> str:
38193819
self.mj_model = spec.compile()
38203820
self.mj_data = mujoco.MjData(self.mj_model)
38213821

3822-
self.update_mjc_data(self.mj_data, model, state)
3822+
self._update_mjc_data(self.mj_data, model, state)
38233823

3824-
# fill some MjWarp model fields that are outdated after update_mjc_data.
3824+
# fill some MjWarp model fields that are outdated after _update_mjc_data.
38253825
# just setting qpos0 to d.qpos leads to weird behavior here, needs
38263826
# to be investigated.
38273827

@@ -4090,7 +4090,7 @@ def get_body_name(body_idx: int) -> str:
40904090
)
40914091

40924092
# expand model fields that can be expanded:
4093-
self.expand_model_fields(self.mjw_model, nworld)
4093+
self._expand_model_fields(self.mjw_model, nworld)
40944094

40954095
# update solver options from Newton model (only if not overridden by constructor)
40964096
self._update_solver_options(overridden_options=overridden_options)
@@ -4099,7 +4099,7 @@ def get_body_name(body_idx: int) -> str:
40994099
# now complete the data from the Newton model
41004100
self.notify_model_changed(SolverNotifyFlags.ALL)
41014101

4102-
def expand_model_fields(self, mj_model: MjWarpModel, nworld: int):
4102+
def _expand_model_fields(self, mj_model: MjWarpModel, nworld: int):
41034103
if nworld == 1:
41044104
return
41054105

@@ -4309,7 +4309,7 @@ def get_option(name: str):
43094309
device=self.model.device,
43104310
)
43114311

4312-
def update_model_inertial_properties(self):
4312+
def _update_model_inertial_properties(self):
43134313
if self.model.body_count == 0:
43144314
return
43154315

@@ -4357,7 +4357,7 @@ def update_model_inertial_properties(self):
43574357
# - cam_pos0, light_pos0, actuator_acc0: other derived quantities
43584358
self._mujoco_warp.set_const(self.mjw_model, self.mjw_data)
43594359

4360-
def update_joint_dof_properties(self):
4360+
def _update_joint_dof_properties(self):
43614361
"""Update all joint DOF properties including effort limits, friction, armature, solimplimit, solref, passive stiffness and damping, and joint limit ranges in the MuJoCo model."""
43624362
if self.model.joint_dof_count == 0:
43634363
return
@@ -4482,7 +4482,7 @@ def update_joint_dof_properties(self):
44824482
# - cam_pos0, light_pos0, actuator_acc0: other derived quantities
44834483
self._mujoco_warp.set_const(self.mjw_model, self.mjw_data)
44844484

4485-
def update_joint_properties(self):
4485+
def _update_joint_properties(self):
44864486
"""Update joint properties including joint positions, joint axes, and relative body transforms in the MuJoCo model."""
44874487
if self.model.joint_count == 0:
44884488
return
@@ -4535,7 +4535,7 @@ def update_joint_properties(self):
45354535
device=self.model.device,
45364536
)
45374537

4538-
def update_geom_properties(self):
4538+
def _update_geom_properties(self):
45394539
"""Update geom properties including collision radius, friction, and contact parameters in the MuJoCo model."""
45404540

45414541
# Get number of geoms and worlds from MuJoCo model
@@ -4650,7 +4650,7 @@ def update_pair_properties(self):
46504650
device=self.model.device,
46514651
)
46524652

4653-
def update_model_properties(self):
4653+
def _update_model_properties(self):
46544654
"""Update model properties including gravity in the MuJoCo model."""
46554655
if self.use_mujoco_cpu:
46564656
self.mj_model.opt.gravity[:] = np.array([*self.model.gravity.numpy()[0]])
@@ -4843,7 +4843,7 @@ def update_actuator_properties(self):
48434843
"""Update CTRL_DIRECT actuator properties (gainprm, biasprm) in the MuJoCo model.
48444844
48454845
Only updates actuators that use CTRL_DIRECT mode. JOINT_TARGET actuators are
4846-
updated via update_joint_dof_properties() using joint_target_ke/kd.
4846+
updated via _update_joint_dof_properties() using joint_target_ke/kd.
48474847
"""
48484848
if self.mjc_actuator_ctrl_source is None or self.mjc_actuator_to_newton_idx is None:
48494849
return

newton/_src/solvers/vbd/particle_vbd_kernels.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ def compute_cofactor_derivative(F: wp.mat33, scale: float) -> mat99:
643643

644644

645645
@wp.kernel
646-
def count_num_adjacent_edges(
646+
def _count_num_adjacent_edges(
647647
edges_array: wp.array(dtype=wp.int32, ndim=2), num_vertex_adjacent_edges: wp.array(dtype=wp.int32)
648648
):
649649
for edge_id in range(edges_array.shape[0]):
@@ -663,7 +663,7 @@ def count_num_adjacent_edges(
663663

664664

665665
@wp.kernel
666-
def fill_adjacent_edges(
666+
def _fill_adjacent_edges(
667667
edges_array: wp.array(dtype=wp.int32, ndim=2),
668668
vertex_adjacent_edges_offsets: wp.array(dtype=wp.int32),
669669
vertex_adjacent_edges_fill_count: wp.array(dtype=wp.int32),
@@ -703,7 +703,7 @@ def fill_adjacent_edges(
703703

704704

705705
@wp.kernel
706-
def count_num_adjacent_faces(
706+
def _count_num_adjacent_faces(
707707
face_indices: wp.array(dtype=wp.int32, ndim=2), num_vertex_adjacent_faces: wp.array(dtype=wp.int32)
708708
):
709709
for face in range(face_indices.shape[0]):
@@ -717,7 +717,7 @@ def count_num_adjacent_faces(
717717

718718

719719
@wp.kernel
720-
def fill_adjacent_faces(
720+
def _fill_adjacent_faces(
721721
face_indices: wp.array(dtype=wp.int32, ndim=2),
722722
vertex_adjacent_faces_offsets: wp.array(dtype=wp.int32),
723723
vertex_adjacent_faces_fill_count: wp.array(dtype=wp.int32),
@@ -748,7 +748,7 @@ def fill_adjacent_faces(
748748

749749

750750
@wp.kernel
751-
def count_num_adjacent_springs(
751+
def _count_num_adjacent_springs(
752752
springs_array: wp.array(dtype=wp.int32), num_vertex_adjacent_springs: wp.array(dtype=wp.int32)
753753
):
754754
num_springs = springs_array.shape[0] / 2
@@ -761,7 +761,7 @@ def count_num_adjacent_springs(
761761

762762

763763
@wp.kernel
764-
def fill_adjacent_springs(
764+
def _fill_adjacent_springs(
765765
springs_array: wp.array(dtype=wp.int32),
766766
vertex_adjacent_springs_offsets: wp.array(dtype=wp.int32),
767767
vertex_adjacent_springs_fill_count: wp.array(dtype=wp.int32),
@@ -784,7 +784,7 @@ def fill_adjacent_springs(
784784

785785

786786
@wp.kernel
787-
def count_num_adjacent_tets(
787+
def _count_num_adjacent_tets(
788788
tet_indices: wp.array(dtype=wp.int32, ndim=2), num_vertex_adjacent_tets: wp.array(dtype=wp.int32)
789789
):
790790
for tet in range(tet_indices.shape[0]):
@@ -800,7 +800,7 @@ def count_num_adjacent_tets(
800800

801801

802802
@wp.kernel
803-
def fill_adjacent_tets(
803+
def _fill_adjacent_tets(
804804
tet_indices: wp.array(dtype=wp.int32, ndim=2),
805805
vertex_adjacent_tets_offsets: wp.array(dtype=wp.int32),
806806
vertex_adjacent_tets_fill_count: wp.array(dtype=wp.int32),

newton/_src/solvers/vbd/rigid_vbd_kernels.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,7 +1054,7 @@ def evaluate_joint_force_hessian(
10541054
# Utility kernels
10551055
# -----------------------------
10561056
@wp.kernel
1057-
def count_num_adjacent_joints(
1057+
def _count_num_adjacent_joints(
10581058
joint_parent: wp.array(dtype=wp.int32),
10591059
joint_child: wp.array(dtype=wp.int32),
10601060
num_body_adjacent_joints: wp.array(dtype=wp.int32),
@@ -1072,7 +1072,7 @@ def count_num_adjacent_joints(
10721072

10731073

10741074
@wp.kernel
1075-
def fill_adjacent_joints(
1075+
def _fill_adjacent_joints(
10761076
joint_parent: wp.array(dtype=wp.int32),
10771077
joint_child: wp.array(dtype=wp.int32),
10781078
body_adjacent_joints_offsets: wp.array(dtype=wp.int32),

0 commit comments

Comments
 (0)