nablaengine 1.0.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- nablaengine/__init__.py +4 -0
- nablaengine/_fixtures/README.md +19 -0
- nablaengine/_fixtures/data/mf_s0.npz +0 -0
- nablaengine/_fixtures/mjlab/LICENSE +27 -0
- nablaengine/_fixtures/mjlab/README.md +113 -0
- nablaengine/_fixtures/mjlab/g1_meta.json +32 -0
- nablaengine/_fixtures/mjlab/g1_train.mjb +0 -0
- nablaengine/_optional.py +30 -0
- nablaengine/_paths.py +38 -0
- nablaengine/actuators/__init__.py +169 -0
- nablaengine/actuators/bam.py +306 -0
- nablaengine/actuators/bam_params/LICENSE +202 -0
- nablaengine/actuators/bam_params/xl330/m6.json +19 -0
- nablaengine/boxbox.py +259 -0
- nablaengine/ccd.py +1122 -0
- nablaengine/checks.py +376 -0
- nablaengine/cuda.py +483 -0
- nablaengine/dist_query.py +628 -0
- nablaengine/dual.py +223 -0
- nablaengine/dyn_query.py +685 -0
- nablaengine/engines.py +114 -0
- nablaengine/envs/__init__.py +24 -0
- nablaengine/envs/brax.py +165 -0
- nablaengine/envs/convert.py +771 -0
- nablaengine/envs/dmc.py +523 -0
- nablaengine/envs/env.py +233 -0
- nablaengine/envs/kin.py +371 -0
- nablaengine/envs/ops.py +134 -0
- nablaengine/envs/rewards.py +57 -0
- nablaengine/fixtures.py +243 -0
- nablaengine/forest_engine/__init__.py +0 -0
- nablaengine/forest_engine/adjoint.py +1726 -0
- nablaengine/forest_engine/adjoint_contact.py +493 -0
- nablaengine/forest_engine/bam_hooks.py +248 -0
- nablaengine/forest_engine/body_params.py +1007 -0
- nablaengine/forest_engine/capacity_diag.py +116 -0
- nablaengine/forest_engine/ccd_coop.py +250 -0
- nablaengine/forest_engine/chol_blk.py +80 -0
- nablaengine/forest_engine/contact_forces.py +142 -0
- nablaengine/forest_engine/contact_vjp.py +462 -0
- nablaengine/forest_engine/convex.py +1331 -0
- nablaengine/forest_engine/cylinder.py +73 -0
- nablaengine/forest_engine/dist_device.py +923 -0
- nablaengine/forest_engine/dyn_device.py +696 -0
- nablaengine/forest_engine/ellipsoid.py +179 -0
- nablaengine/forest_engine/elliptic.py +272 -0
- nablaengine/forest_engine/equality.py +842 -0
- nablaengine/forest_engine/floss.py +148 -0
- nablaengine/forest_engine/foot_offset.py +368 -0
- nablaengine/forest_engine/free_gyro.py +288 -0
- nablaengine/forest_engine/host.py +530 -0
- nablaengine/forest_engine/kdof64.py +127 -0
- nablaengine/forest_engine/kinematics.py +320 -0
- nablaengine/forest_engine/margin.py +167 -0
- nablaengine/forest_engine/margin_convex.py +326 -0
- nablaengine/forest_engine/midphase.py +439 -0
- nablaengine/forest_engine/model_hooks.py +913 -0
- nablaengine/forest_engine/motor_hooks.py +50 -0
- nablaengine/forest_engine/muscle.py +485 -0
- nablaengine/forest_engine/nb128.py +118 -0
- nablaengine/forest_engine/nb64.py +163 -0
- nablaengine/forest_engine/noslip.py +1124 -0
- nablaengine/forest_engine/nv128.py +1944 -0
- nablaengine/forest_engine/object_size.py +283 -0
- nablaengine/forest_engine/replay_guard.py +40 -0
- nablaengine/forest_engine/root_spread.py +126 -0
- nablaengine/forest_engine/solref.py +74 -0
- nablaengine/forest_engine/source.py +1464 -0
- nablaengine/forest_engine/sparse_pattern.py +527 -0
- nablaengine/forest_engine/spatial.py +1045 -0
- nablaengine/forest_engine/step.py +3083 -0
- nablaengine/forest_engine/surrogate.py +498 -0
- nablaengine/forest_engine/tape.py +199 -0
- nablaengine/forest_engine/tendon.py +845 -0
- nablaengine/forest_engine/terrain.py +1059 -0
- nablaengine/forest_engine/world_forces.py +166 -0
- nablaengine/forest_engine/world_params.py +230 -0
- nablaengine/g1_engine/__init__.py +0 -0
- nablaengine/g1_engine/adjoint.py +1661 -0
- nablaengine/g1_engine/adjoint_contact.py +577 -0
- nablaengine/g1_engine/bam_hooks.py +596 -0
- nablaengine/g1_engine/condim4.py +245 -0
- nablaengine/g1_engine/contact_forces.py +500 -0
- nablaengine/g1_engine/cylccd.py +1158 -0
- nablaengine/g1_engine/cylinder.py +216 -0
- nablaengine/g1_engine/elliptic.py +565 -0
- nablaengine/g1_engine/exact_replay.py +61 -0
- nablaengine/g1_engine/host.py +358 -0
- nablaengine/g1_engine/kinematics.py +320 -0
- nablaengine/g1_engine/mesh.py +305 -0
- nablaengine/g1_engine/mesh_tables.py +79 -0
- nablaengine/g1_engine/model_hooks.py +919 -0
- nablaengine/g1_engine/motor_hooks.py +70 -0
- nablaengine/g1_engine/replay_guard.py +55 -0
- nablaengine/g1_engine/source.py +1107 -0
- nablaengine/g1_engine/sphbox.py +93 -0
- nablaengine/g1_engine/step.py +3716 -0
- nablaengine/g1_engine/tape.py +173 -0
- nablaengine/g1_engine/world_params.py +447 -0
- nablaengine/identity.py +401 -0
- nablaengine/jax_ffi.py +2536 -0
- nablaengine/model/__init__.py +1 -0
- nablaengine/model/capability.py +694 -0
- nablaengine/model/fields.py +317 -0
- nablaengine/model/gravcomp.py +64 -0
- nablaengine/model/invweight.py +145 -0
- nablaengine/motor.py +143 -0
- nablaengine/object_geometry.py +609 -0
- nablaengine/params.py +258 -0
- nablaengine/provenance.py +196 -0
- nablaengine/reference/__init__.py +0 -0
- nablaengine/reference/adjoint_groups.py +226 -0
- nablaengine/reference/jax64.py +690 -0
- nablaengine/reference/warp_physics.py +262 -0
- nablaengine/sensors/__init__.py +4 -0
- nablaengine/sensors/cameras.py +285 -0
- nablaengine/sensors/data/LIVOX_MID360_LICENSE +21 -0
- nablaengine/sensors/data/livox_mid360.npz +0 -0
- nablaengine/sensors/g1.py +521 -0
- nablaengine/sensors/jax_rays.py +364 -0
- nablaengine/sensors/optix_rays.py +725 -0
- nablaengine/sensors/raycore.py +630 -0
- nablaengine/sensors/raygrad.py +374 -0
- nablaengine/sensors/raygrad_cuda.py +191 -0
- nablaengine/sensors/robot_mesh.py +273 -0
- nablaengine/sensors/torch_rays.py +275 -0
- nablaengine/terrain.py +424 -0
- nablaengine/torch_ffi.py +1328 -0
- nablaengine/workdir.py +37 -0
- nablaengine-1.0.2.dist-info/METADATA +236 -0
- nablaengine-1.0.2.dist-info/RECORD +136 -0
- nablaengine-1.0.2.dist-info/WHEEL +5 -0
- nablaengine-1.0.2.dist-info/licenses/LICENSE +202 -0
- nablaengine-1.0.2.dist-info/licenses/NOTICE +23 -0
- nablaengine-1.0.2.dist-info/licenses/THIRD_PARTY_NOTICES.md +176 -0
- nablaengine-1.0.2.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,913 @@
|
|
|
1
|
+
"""Edits of the kernels' text for models outside the G1's structure (nv < 32, more than 3 children per body)."""
|
|
2
|
+
|
|
3
|
+
ENGINE_CHILD = (
|
|
4
|
+
("DKC int c_parent[NB], c_depth[NB], c_qadr[NB], c_dadr[NB], c_nchild[NB], c_child[NB * 3], c_nanc[NB],",
|
|
5
|
+
"DKC int c_parent[NB], c_depth[NB], c_qadr[NB], c_dadr[NB], c_nchild[NB], c_child[NB * NCH], c_nanc[NB],", 1),
|
|
6
|
+
("c_child[3 * b + i]", "c_child[NCH * b + i]", 3),
|
|
7
|
+
)
|
|
8
|
+
KIN_CHILD = (
|
|
9
|
+
(" for (int i = 0; i < 3; i++) ch[i] = i < n ? c_child[3 * b + i] : 0;",
|
|
10
|
+
" for (int i = 0; i < NCH; i++) ch[i] = i < n ? c_child[NCH * b + i] : 0;", 1),
|
|
11
|
+
(" int ch[3];", " int ch[NCH];", 2),
|
|
12
|
+
)
|
|
13
|
+
ENGINE_SMALL_NV = (
|
|
14
|
+
(" const int r0 = lane, r1 = lane + 32;\n const bool has1 = r1 < NV;",
|
|
15
|
+
" const int r0 = min(lane, NV - 1), r1 = lane + 32; // lanes >= NV repeat row NV - 1 (model_hooks)\n"
|
|
16
|
+
" const bool has1 = r1 < NV;", 2),
|
|
17
|
+
(" const int r0 = lane, r1 = min(lane + 32, NV - 1);",
|
|
18
|
+
" const int r0 = min(lane, NV - 1), r1 = min(lane + 32, NV - 1); // (model_hooks)", 1),
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
ENGINE_ADJ_SCOPE = (
|
|
22
|
+
('static_assert(!HAS_BOX && NFLOSS == 0, "the contact adjoint is for the mjlab model: no boxes, no frictionloss rows");',
|
|
23
|
+
'// (model_hooks: plane-box contacts and frictionloss rows in the contact adjoint)', 1),
|
|
24
|
+
('static_assert(HAS_BOX == 0 && NFLOSS == 0, "the adjoint is for the mjlab model (no boxes, no frictionloss rows)");',
|
|
25
|
+
'// (model_hooks: plane-box contacts and frictionloss rows in the adjoint)', 1),
|
|
26
|
+
)
|
|
27
|
+
ENGINE_PLANE_BOX = (
|
|
28
|
+
(" if (t2 != 2) return -1;\n",
|
|
29
|
+
""" if (t2 == 3) { // plane-box corner `slot` (model_hooks): the forward's collide_pair arithmetic, then its VJP
|
|
30
|
+
float cl[3] = {(slot & 1) ? sz2[0] : -sz2[0], (slot & 2) ? sz2[1] : -sz2[1], (slot & 4) ? sz2[2] : -sz2[2]}, cw[3];
|
|
31
|
+
qrot(q2, cl, cw);
|
|
32
|
+
float cd = dot3(p2, n) - dot3(p1, n);
|
|
33
|
+
*dist = cd + dot3(n, cw);
|
|
34
|
+
for (int k = 0; k < 3; k++) pos[k] = cw[k] + p2[k] - 0.5f * n[k] * (*dist);
|
|
35
|
+
make_frame(n, fr);
|
|
36
|
+
if (!(-0.5f < n[1] && n[1] < 0.5f)) br |= ADJ_BR_FRAME_Z;
|
|
37
|
+
if (cot) { // c = x + cw: xbar = cbar, tbar = cw x cbar; cbar = pbar - n (n . pbar) / 2 + n dbar
|
|
38
|
+
float pn = dot3(n, cot + 1);
|
|
39
|
+
for (int k = 0; k < 3; k++) gb[6 + k] = cot[1 + k] + n[k] * (cot[0] - 0.5f * pn);
|
|
40
|
+
cross(cw, gb + 6, gb + 9);
|
|
41
|
+
}
|
|
42
|
+
return br;
|
|
43
|
+
}
|
|
44
|
+
if (t2 != 2) return -1;\n""", 1),
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
ENGINE_DAMPING = (
|
|
48
|
+
(" c_inert[NB * 3], c_grav[3], c_armature[NV], c_kp[NV], c_kv[NV], c_frclim[NV], c_misc[2];\n",
|
|
49
|
+
" c_inert[NB * 3], c_grav[3], c_armature[NV], c_kp[NV], c_kv[NV], c_frclim[NV], c_misc[2];\n"
|
|
50
|
+
"DKC float c_damp[NV]; // joint damping (model_hooks)\n", 1),
|
|
51
|
+
(" s.qsm[i] = act - s.qsm[i];",
|
|
52
|
+
" s.qsm[i] = act - c_damp[i] * s.qvel[i] - s.qsm[i]; // the passive damping force (model_hooks)", 1),
|
|
53
|
+
("{ s.L[TRI(i, i)] += h * c_kv[i]; s.tmp[i] = s.Ma[i]; }",
|
|
54
|
+
"{ s.L[TRI(i, i)] += h * (c_kv[i] + c_damp[i]); s.tmp[i] = s.Ma[i]; }", 1),
|
|
55
|
+
("{ s.abar[i] -= h * c_kv[i] * s.y[i]; s.ny[i] = -s.y[i]; }",
|
|
56
|
+
"{ s.abar[i] -= h * (c_kv[i] + c_damp[i]) * s.y[i]; s.ny[i] = -s.y[i]; }", 1),
|
|
57
|
+
("{ float x = s.did[i] - h * c_kv[i] * s.a[i]; acc += x * x; }",
|
|
58
|
+
"{ float x = s.did[i] - h * (c_kv[i] + c_damp[i]) * s.a[i]; acc += x * x; }", 1),
|
|
59
|
+
(" s.ubar[i - 6] += kl;\n }\n __syncwarp();\n",
|
|
60
|
+
" s.ubar[i - 6] += kl;\n }\n __syncwarp();\n"
|
|
61
|
+
" for (int i = lane; i < NV; i += 32) s.Pv[i] += c_damp[i] * s.lam[i]; // joint damping (model_hooks)\n"
|
|
62
|
+
" __syncwarp();\n", 1),
|
|
63
|
+
(" float r = s.did[i] + c_armature[i] * s.a[i] - tau - s.jtf[i];",
|
|
64
|
+
" float r = s.did[i] + c_armature[i] * s.a[i] - tau + c_damp[i] * s.v[i] - s.jtf[i];", 1),
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
ENGINE_FLOSS = (
|
|
68
|
+
(" const int r = nl + __popc(bal & below);",
|
|
69
|
+
" const int r = NFLOSS + nl + __popc(bal & below); // after the frictionloss rows (model_hooks)", 1),
|
|
70
|
+
(" const int r0 = nlim + c + 3 * (ntri + __popc(b3 & below)); // the contact's first row",
|
|
71
|
+
" const int r0 = NFLOSS + nlim + c + 3 * (ntri + __popc(b3 & below)); // the contact's first row (model_hooks)",
|
|
72
|
+
1),
|
|
73
|
+
(""" const unsigned below = (1u << lane) - 1u;
|
|
74
|
+
int nl = 0;
|
|
75
|
+
#pragma unroll 1
|
|
76
|
+
for (int i0 = 0; i0 < NV; i0 += 32) {
|
|
77
|
+
const int i = min(i0 + lane, NV - 1); // clamped so that the loads need no branch (as the forward)""",
|
|
78
|
+
""" const unsigned below = (1u << lane) - 1u;
|
|
79
|
+
// the frictionloss rows (model_hooks): row r = the number of frictionloss dofs before (dof order, the first rows),
|
|
80
|
+
// J = e_i, pos 0, aref = -b v_i (substep_pre), constant D; on its quadratic zone (the tape's bit) the row force
|
|
81
|
+
// -D (a_i + b v_i) gives P_v += D b lam (limit_row_vjp without the position terms); its linear pieces are constant
|
|
82
|
+
int nf = 0;
|
|
83
|
+
#pragma unroll 1
|
|
84
|
+
for (int i0 = 0; i0 < NV; i0 += 32) {
|
|
85
|
+
const int i = i0 + lane;
|
|
86
|
+
const bool on = i < NV && c_floss[i] > 0.f;
|
|
87
|
+
const unsigned bal = __ballot_sync(0xffffffffu, on);
|
|
88
|
+
if (on) {
|
|
89
|
+
const int r = nf + __popc(bal & below);
|
|
90
|
+
if ((s.zone[r >> 5] >> (r & 31)) & 1u) {
|
|
91
|
+
float ri[6];
|
|
92
|
+
adj_row_impedance(c_dsolref, c_dsolimp, 0.f, c_dinvw[i], ri);
|
|
93
|
+
s.Pv[i] += ri[4] * ri[1] * s.lam[i];
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
nf += __popc(bal);
|
|
97
|
+
}
|
|
98
|
+
int nl = 0;
|
|
99
|
+
#pragma unroll 1
|
|
100
|
+
for (int i0 = 0; i0 < NV; i0 += 32) {
|
|
101
|
+
const int i = min(i0 + lane, NV - 1); // clamped so that the loads need no branch (as the forward)""", 1),
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
from nablaengine.dual import DUAL_DEVICE
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def has_sphere_box(m):
|
|
109
|
+
"""A sphere-box contact pair (the forward's collide_pair needs ENGINE_SPHERE_BOX)."""
|
|
110
|
+
return _has_pair(m, "SPHERE", "BOX")
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def has_capsule_box(m):
|
|
114
|
+
"""A capsule-box contact pair."""
|
|
115
|
+
return _has_pair(m, "CAPSULE", "BOX")
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def _has_pair(m, a, b):
|
|
119
|
+
import mujoco
|
|
120
|
+
want = sorted((int(getattr(mujoco.mjtGeom, "mjGEOM_" + a)), int(getattr(mujoco.mjtGeom, "mjGEOM_" + b))))
|
|
121
|
+
return any(sorted((int(m.geom_type[m.pair_geom1[p]]), int(m.geom_type[m.pair_geom2[p]]))) == want
|
|
122
|
+
for p in range(m.npair))
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def capbox_source():
|
|
126
|
+
"""step.py's sphere_box and capsule_box's candidate search (g1_engine.step.capbox_segs_source on the forest
|
|
127
|
+
engine's text) as templates over the scalar type, generated from their text, with the candidate's sphere centre and
|
|
128
|
+
a contact slot's replay: cb_sphere_box_t, cb_segs_t, cb_point_t, cb_slot_t. Over RF the forward computes the bits
|
|
129
|
+
that the adjoint's D1 replay (ADJ_CAPBOX) takes, its branches included (nablaengine.dual)."""
|
|
130
|
+
import re
|
|
131
|
+
from nablaengine.dual import RF_DEVICE
|
|
132
|
+
from nablaengine.forest_engine import step
|
|
133
|
+
from nablaengine.g1_engine import step as g1_step
|
|
134
|
+
src = step.STEP_DEVICE
|
|
135
|
+
a = re.search(r"__device__ void sphere_box\(", src).start()
|
|
136
|
+
body = src[a:src.index("\n}\n", a) + 3] + g1_step.capbox_segs_source(src)
|
|
137
|
+
body = re.sub(r"\bfloat\b", "T", body)
|
|
138
|
+
body = re.sub(r"\b(fabs|sqrt|fmin|fmax)f\(", r"t_\1(", body)
|
|
139
|
+
body = re.sub(r"\b(mv3|mtv3|mm3|mmt3|dot3|safe_div)\(", r"t_\1(", body)
|
|
140
|
+
body = re.sub(r"\bsphere_box\(", "cb_sphere_box_t(", body)
|
|
141
|
+
body = re.sub(r"\bcapbox_segs\(", "cb_segs_t(", body)
|
|
142
|
+
body = re.sub(r"__device__ (__noinline__ )?(void|int) ", r"template <class T>\n__device__ \1\2 ", body)
|
|
143
|
+
if "add_contact" in body or "atomic" in body:
|
|
144
|
+
raise RuntimeError("capbox_source: unexpected side effects in the generated text")
|
|
145
|
+
rf = "#ifndef DK_RF_DEVICE\n#define DK_RF_DEVICE\n" + RF_DEVICE + "#endif\n"
|
|
146
|
+
return ("// sphere-box and capsule-box contacts over a scalar type (model_hooks.capbox_source: step.py's sphere_box"
|
|
147
|
+
" and\n// capsule_box's candidate search, generated from their text)\n" + DUAL_DEVICE + rf + body + r"""
|
|
148
|
+
// the centre of capsule_box's candidate sphere at segment parameter seg (cb_segs_t), capsule_box's arithmetic
|
|
149
|
+
template <class T>
|
|
150
|
+
__device__ void cb_point_t(const T* cp, const T* axisw, T hl, const T* bp, const T* R, T seg, T* ce) {
|
|
151
|
+
T d[3] = {cp[0] - bp[0], cp[1] - bp[1], cp[2] - bp[2]}, pos[3], axis[3], lc[3], t[3];
|
|
152
|
+
t_mtv3(R, d, pos);
|
|
153
|
+
t_mtv3(R, axisw, axis);
|
|
154
|
+
for (int k = 0; k < 3; k++) lc[k] = pos[k] + axis[k] * hl * seg;
|
|
155
|
+
t_mv3(R, lc, t);
|
|
156
|
+
for (int k = 0; k < 3; k++) ce[k] = bp[k] + t[k];
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// contact `slot` of a sphere (t1 1: slot 0) or capsule (t1 2: candidate slot; a1 its axis) against a box (R2 row
|
|
160
|
+
// major, sz2 its half sizes): 1 and its distance, position, normal (from geom 1 towards the box) when it exists
|
|
161
|
+
template <class T>
|
|
162
|
+
__device__ int cb_slot_t(int t1, int slot, const T* p1, const T* a1, const float* sz1, const T* p2, const T* R2,
|
|
163
|
+
const float* sz2, T* dist, T* pos, T* nrm) {
|
|
164
|
+
T Z[3] = {T(sz2[0]), T(sz2[1]), T(sz2[2])};
|
|
165
|
+
if (t1 == 1) {
|
|
166
|
+
if (slot != 0) return 0;
|
|
167
|
+
cb_sphere_box_t<T>(p1, T(sz1[0]), p2, R2, Z, dist, pos, nrm);
|
|
168
|
+
return 1;
|
|
169
|
+
}
|
|
170
|
+
T sg[2], C[3];
|
|
171
|
+
if (slot >= cb_segs_t<T>(p1, a1, T(sz1[1]), p2, R2, Z, sg)) return 0;
|
|
172
|
+
cb_point_t<T>(p1, a1, T(sz1[1]), p2, R2, sg[slot], C);
|
|
173
|
+
cb_sphere_box_t<T>(C, T(sz1[0]), p2, R2, Z, dist, pos, nrm);
|
|
174
|
+
return 1;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// the forward's sphere-box (t1 1) and capsule-box (t1 2) contacts over RF (the adjoint's replay takes its branches)
|
|
178
|
+
template <class S>
|
|
179
|
+
__device__ void cb_contacts(S& s, int pair, int t1, const float* p1, const float* a1, const float* sz1, const float* p2,
|
|
180
|
+
const float* R2, const float* sz2) {
|
|
181
|
+
RF P1[3], A1[3], P2[3], Q2[9], Z[3], sg[2] = {RF(0.f), RF(0.f)};
|
|
182
|
+
for (int k = 0; k < 3; k++) { P1[k] = RF(p1[k]); A1[k] = RF(a1[k]); P2[k] = RF(p2[k]); Z[k] = RF(sz2[k]); }
|
|
183
|
+
for (int k = 0; k < 9; k++) Q2[k] = RF(R2[k]);
|
|
184
|
+
const int ns = t1 == 1 ? 1 : cb_segs_t<RF>(P1, A1, RF(sz1[1]), P2, Q2, Z, sg);
|
|
185
|
+
for (int e = 0; e < ns; e++) {
|
|
186
|
+
RF C[3], dd, pp[3], nn[3];
|
|
187
|
+
if (t1 == 1) for (int k = 0; k < 3; k++) C[k] = P1[k];
|
|
188
|
+
else cb_point_t<RF>(P1, A1, RF(sz1[1]), P2, Q2, sg[e], C);
|
|
189
|
+
cb_sphere_box_t<RF>(C, RF(sz1[0]), P2, Q2, Z, &dd, pp, nn);
|
|
190
|
+
if (dd.v < 0.f) {
|
|
191
|
+
float pos[3] = {pp[0].v, pp[1].v, pp[2].v}, n[3] = {nn[0].v, nn[1].v, nn[2].v}, fr[9];
|
|
192
|
+
make_frame(n, fr);
|
|
193
|
+
add_contact(s, pair, e, dd.v, pos, fr);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
""")
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def capbox_hooks(m):
|
|
202
|
+
"""Sphere-box and capsule-box pairs (MuJoCo Warp's sphere_box, capsule_box_witness + capsule_box): the forward's
|
|
203
|
+
contacts over RF (cb_contacts; ahead of box_pair, outside HAS_BOX), and the contact adjoint's replay and VJP
|
|
204
|
+
(ADJ_CAPBOX, part "engine")."""
|
|
205
|
+
hooks = (("#if HAS_BOX\n// capsule-box (t1 2) or box-box (t1 3) into o:",
|
|
206
|
+
capbox_source() + "#if HAS_BOX\n// capsule-box (t1 2) or box-box (t1 3) into o:", 1),
|
|
207
|
+
(" capsule_box(o, 0, p1, a1, c_gsize[3 * g1], c_gsize[3 * g1 + 1], p2, R2, c_gsize + 3 * g2);\n",
|
|
208
|
+
" cb_contacts(o, 0, 2, p1, a1, c_gsize + 3 * g1, p2, R2, c_gsize + 3 * g2); // (model_hooks.capbox_hooks)\n",
|
|
209
|
+
1))
|
|
210
|
+
return hooks + (ENGINE_SPHERE_BOX if has_sphere_box(m) else ())
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
ENGINE_SPHERE_BOX = (
|
|
214
|
+
(" if ((t1 == 2 || t1 == 3) && t2 == 3) { // capsule-box, box-box: past the OBB test, in k_ccd (the split forward)\n",
|
|
215
|
+
""" if (t1 == 1 && t2 == 3) { // sphere-box (MuJoCo Warp's sphere_box; model_hooks.capbox_hooks: over RF)
|
|
216
|
+
float R2[9];
|
|
217
|
+
quat2mat(q2, R2);
|
|
218
|
+
cb_contacts(s, pair, 1, p1, p1, sz1, p2, R2, sz2);
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
if ((t1 == 2 || t1 == 3) && t2 == 3) { // capsule-box, box-box: past the OBB test, in k_ccd (the split forward)\n""", 1),
|
|
222
|
+
)
|
|
223
|
+
|
|
224
|
+
ADJ_CAPBOX = """ if ((t1 == 1 || t1 == 2) && t2 == 3) { // sphere-box / capsule-box contact `slot` (model_hooks.capbox_hooks): the
|
|
225
|
+
// forward's RF arithmetic replayed (the forward's bits), then over duals (D1, the same values) once per pose
|
|
226
|
+
// direction of each geom (cb_dir_vjp): exact distance, position and normal tangents
|
|
227
|
+
#if CB_COOP_ON
|
|
228
|
+
if (cot) return 0; // (the VJP: adj_contacts' cooperative pass, cb_coop)
|
|
229
|
+
#endif
|
|
230
|
+
float R2[9], a1[3] = {0.f, 0.f, 1.f}, nrm[3];
|
|
231
|
+
quat2mat(q2, R2);
|
|
232
|
+
if (t1 == 2) zaxis_rn(q1, a1);
|
|
233
|
+
{ // (over RF: the forward's arithmetic, the values of the D1 replays without their tangents)
|
|
234
|
+
RF P1[3], A1[3], P2[3], Q2[9], dd, pp[3], nn[3];
|
|
235
|
+
for (int k = 0; k < 3; k++) { P1[k] = RF(p1[k]); A1[k] = RF(a1[k]); P2[k] = RF(p2[k]); }
|
|
236
|
+
for (int k = 0; k < 9; k++) Q2[k] = RF(R2[k]);
|
|
237
|
+
if (!cb_slot_t<RF>(t1, slot, P1, A1, sz1, P2, Q2, sz2, &dd, pp, nn)) return -1;
|
|
238
|
+
*dist = dd.v;
|
|
239
|
+
for (int k = 0; k < 3; k++) { pos[k] = pp[k].v; nrm[k] = nn[k].v; }
|
|
240
|
+
}
|
|
241
|
+
make_frame(nrm, fr);
|
|
242
|
+
if (!(-0.5f < nrm[1] && nrm[1] < 0.5f)) br |= ADJ_BR_FRAME_Z;
|
|
243
|
+
if (cot) {
|
|
244
|
+
float nbar[3];
|
|
245
|
+
make_frame_vjp(fr, cot + 4, nbar);
|
|
246
|
+
for (int j = 0; j < 12; j++) gb[j] = cb_dir_vjp(t1, slot, j, p1, a1, sz1, p2, R2, sz2, cot, nbar);
|
|
247
|
+
}
|
|
248
|
+
return br;
|
|
249
|
+
}
|
|
250
|
+
"""
|
|
251
|
+
|
|
252
|
+
CB_DIR_VJP = """// (model_hooks.CB_DIR_VJP) pose direction j of a sphere-box / capsule-box contact's VJP, gb[j]
|
|
253
|
+
__device__ __noinline__ float cb_dir_vjp(int t1, int slot, int j, const float* p1, const float* a1, const float* sz1,
|
|
254
|
+
const float* p2, const float* R2, const float* sz2, const float* cot,
|
|
255
|
+
const float* nbar) {
|
|
256
|
+
const int b = j / 6, a = j % 3;
|
|
257
|
+
const bool rot = (j % 6) >= 3;
|
|
258
|
+
if (t1 == 1 && b == 0 && rot) return 0.f; // (a sphere's contact does not turn with it: gb stays 0)
|
|
259
|
+
float e[3] = {0.f, 0.f, 0.f}, ta[3];
|
|
260
|
+
e[a] = 1.f;
|
|
261
|
+
cross(e, a1, ta);
|
|
262
|
+
D1 P1[3], A1[3], P2[3], Q2[9], dd, pp[3], nn[3];
|
|
263
|
+
for (int k = 0; k < 3; k++) {
|
|
264
|
+
P1[k] = D1(p1[k], !rot && b == 0 && k == a ? 1.f : 0.f);
|
|
265
|
+
A1[k] = D1(a1[k], rot && b == 0 ? ta[k] : 0.f);
|
|
266
|
+
P2[k] = D1(p2[k], !rot && b == 1 && k == a ? 1.f : 0.f);
|
|
267
|
+
}
|
|
268
|
+
for (int c = 0; c < 3; c++) { // ([e_a] R)_rc = (e_a x R_col c)_r
|
|
269
|
+
float col[3] = {R2[c], R2[3 + c], R2[6 + c]}, x[3];
|
|
270
|
+
cross(e, col, x);
|
|
271
|
+
for (int r = 0; r < 3; r++) Q2[3 * r + c] = D1(R2[3 * r + c], rot && b == 1 ? x[r] : 0.f);
|
|
272
|
+
}
|
|
273
|
+
cb_slot_t<D1>(t1, slot, P1, A1, sz1, P2, Q2, sz2, &dd, pp, nn);
|
|
274
|
+
float g = cot[0] * dd.d;
|
|
275
|
+
for (int k = 0; k < 3; k++) g += cot[1 + k] * pp[k].d + nbar[k] * nn[k].d;
|
|
276
|
+
return g;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
"""
|
|
280
|
+
|
|
281
|
+
CB_COOP_DISPATCH = """#define CB_COOP_PAIR(pair, slot) ((c_gtype[PG1(pair)] == 1 || c_gtype[PG1(pair)] == 2) && c_gtype[PG2(pair)] == 3)
|
|
282
|
+
#define CB_COOP_WIDE(pair, slot) 1 // (12 directions: both geoms move)
|
|
283
|
+
// (model_hooks.CB_DIR_VJP) cb_dir_vjp of contact (pair, slot) from the geoms' world poses, rec: cot[0..3], nbar
|
|
284
|
+
__device__ __forceinline__ float cb_coop_dir(const float (*gx)[3], const float (*gq)[4], int pair, int slot, int j,
|
|
285
|
+
const float* rec) {
|
|
286
|
+
const int g1 = PG1(pair), g2 = PG2(pair), t1 = c_gtype[g1];
|
|
287
|
+
float R2[9], a1[3] = {0.f, 0.f, 1.f};
|
|
288
|
+
quat2mat(gq[g2], R2);
|
|
289
|
+
if (t1 == 2) zaxis_rn(gq[g1], a1);
|
|
290
|
+
return cb_dir_vjp(t1, slot, j, gx[g1], a1, c_gsize + 3 * g1, gx[g2], R2, c_gsize + 3 * g2, rec, rec + 4);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
"""
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
def plane_mesh_hooks(part="engine"):
|
|
297
|
+
"""Plane-mesh contacts as MuJoCo Warp 3.17's plane_convex (g1_engine.mesh: the deepest vertex, the incident
|
|
298
|
+
polygon pruned to 4 by hull4, the mesh-centre filter), in place of the older plane_mesh: CV_PLANE (the fused
|
|
299
|
+
forward's collide_pair and the split forward's k_ccd) becomes plane_mesh_mj over RF (msh_plane), and (part
|
|
300
|
+
"engine") the adjoint replays msh_plane_t over D1 in adj_mesh_geom once (the replay, which records the slot's
|
|
301
|
+
vertex) and evaluates the slot's expression msh_slot_t per pose direction (g1_engine.mesh's SEARCH_ONCE: bitwise
|
|
302
|
+
the same tangents; the vertex handed from the replay call to the VJP call, MESH_MVID). forest_engine.convex's RF
|
|
303
|
+
instance of the older mesh collision (CV_ROUNDED) is left out: the mesh pairs are MuJoCo Warp 3.17's ports
|
|
304
|
+
(convex_mj_hooks)."""
|
|
305
|
+
from nablaengine.dual import RF_DEVICE
|
|
306
|
+
from nablaengine.g1_engine import mesh
|
|
307
|
+
head = ("// box-box (MuJoCo Warp's box_box, margin 0): separating-axis test, then face-vertex or edge-edge clipping\n"
|
|
308
|
+
"#define BB_MAXPT 24\n")
|
|
309
|
+
rf = "#ifndef DK_RF_DEVICE\n#define DK_RF_DEVICE\n" + RF_DEVICE + "#endif\n"
|
|
310
|
+
wrap = """// plane-mesh (model_hooks.plane_mesh_hooks): CV_PLANE's signature, MuJoCo Warp 3.17's plane_convex
|
|
311
|
+
__device__ __noinline__ void plane_mesh_mj(const float* pp, const float* n, int g2i, const float* p2, const float* q2,
|
|
312
|
+
CSink& o) {
|
|
313
|
+
o.n = 0; o.unsupported = 0;
|
|
314
|
+
float d4[4], p4[12], fr[9];
|
|
315
|
+
int vid_[4];
|
|
316
|
+
msh_plane(pp, n, c_gmesh[g2i], p2, q2, d4, p4, vid_);
|
|
317
|
+
make_frame(n, fr);
|
|
318
|
+
for (int i = 0; i < 4; i++)
|
|
319
|
+
if (d4[i] < 0.f) add_contact(o, 0, i, d4[i], p4 + 3 * i, fr);
|
|
320
|
+
}
|
|
321
|
+
"""
|
|
322
|
+
adj_old = " cvd::plane_mesh(P1, nn, g2i, P2, Q2, o);\n"
|
|
323
|
+
adj_new = (" D1 d4[4], p4[12]; // (model_hooks.plane_mesh_hooks: the forward's msh_plane over D1)\n"
|
|
324
|
+
" if (j < 0) {\n"
|
|
325
|
+
" int vid[4] = {-1, -1, -1, -1};\n"
|
|
326
|
+
" msh_plane_t<D1>(P1, nn, c_gmesh[g2i], P2, Q2, d4, p4, vid);\n"
|
|
327
|
+
" *mvid = vid[slot & 3];\n"
|
|
328
|
+
" cvd::real f9[9];\n cvd::make_frame(nn, f9);\n o.n = 0; o.unsupported = 0;\n"
|
|
329
|
+
" for (int i = 0; i < 4; i++)\n if (d4[i] < 0.f) cvd::add_contact(o, 0, i, d4[i], p4 + 3 * i, f9);\n"
|
|
330
|
+
" } else { // (the slot only, from the replay's vertex)\n"
|
|
331
|
+
" msh_slot_t<D1>(P1, nn, c_mvadr[c_gmesh[g2i]], *mvid, P2, Q2, d4 + (slot & 3), p4 + 3 * (slot & 3));\n"
|
|
332
|
+
" cvd::real f9[9];\n cvd::make_frame(nn, f9);\n o.n = 0; o.unsupported = 0;\n"
|
|
333
|
+
" cvd::add_contact(o, 0, slot, d4[slot & 3], p4 + 3 * (slot & 3), f9);\n"
|
|
334
|
+
" }\n")
|
|
335
|
+
rounded = "#ifndef CV_ROUNDED\n#define CV_ROUNDED 1 // (convex.rounded_source; $DK_CV_ROUNDED=0: the float forward)\n#endif\n"
|
|
336
|
+
mdev = mesh.DEVICE
|
|
337
|
+
for a, b in mesh.SEARCH_ONCE[:5] + mesh.SEARCH_ONCE[6:7]:
|
|
338
|
+
if mdev.count(a) != 1:
|
|
339
|
+
raise RuntimeError(f"plane_mesh_hooks: no {a!r} in g1_engine.mesh's text")
|
|
340
|
+
mdev = mdev.replace(a, b)
|
|
341
|
+
return ((head, "#if HAS_MESH\n" + DUAL_DEVICE + rf + mdev + wrap + "#endif\n" + head, 1),
|
|
342
|
+
(rounded, "#define CV_ROUNDED 0 // (model_hooks.plane_mesh_hooks: convex.rounded_source's RF instance not needed)\n",
|
|
343
|
+
1),
|
|
344
|
+
("#define CV_PLANE plane_mesh_r\n", "#define CV_PLANE plane_mesh_mj // (model_hooks.plane_mesh_hooks)\n", 1),
|
|
345
|
+
("#define CV_PLANE plane_mesh\n", "#define CV_PLANE plane_mesh_mj // (model_hooks.plane_mesh_hooks)\n", 1)
|
|
346
|
+
) + (((adj_old, adj_new, 1),) if part == "engine" else ())
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
def boxbox_fwd_hooks():
|
|
350
|
+
"""The forward's box-box primitive (BOX_BOX_CCD 0): MuJoCo Warp's box_box (nablaengine.boxbox, up to 8 contacts)
|
|
351
|
+
over RF, so that the adjoint's D1 replay (ADJ_BOX_BOX_PRIM: bb_slot_t) takes its branches bit for bit; the
|
|
352
|
+
templates go ahead of step.py's box_box, whose primitive half it replaces."""
|
|
353
|
+
from nablaengine import boxbox
|
|
354
|
+
from nablaengine.dual import RF_DEVICE
|
|
355
|
+
head = ("// box-box (MuJoCo Warp's box_box, margin 0): separating-axis test, then face-vertex or edge-edge clipping\n"
|
|
356
|
+
"#define BB_MAXPT 24\n")
|
|
357
|
+
start = (" const float* R2, const float* s2) {\n const float margin = 0.f;\n")
|
|
358
|
+
body = """#if !BOX_BOX_CCD
|
|
359
|
+
{ // MuJoCo Warp's box_box (nablaengine.boxbox; model_hooks.boxbox_fwd_hooks) over RF
|
|
360
|
+
RF P1[3], Q1[9], P2[3], Q2[9], cd[BB_MAXVERT], cp[BB_MAXVERT][3], cn[3];
|
|
361
|
+
for (int k = 0; k < 3; k++) { P1[k] = RF(p1[k]); P2[k] = RF(p2[k]); }
|
|
362
|
+
for (int k = 0; k < 9; k++) { Q1[k] = RF(R1[k]); Q2[k] = RF(R2[k]); }
|
|
363
|
+
const int nb = bb_box_box_t<RF>(P1, Q1, s1, P2, Q2, s2, 0.f, cd, cp, cn);
|
|
364
|
+
if (nb > 0) {
|
|
365
|
+
float nrm[3] = {cn[0].v, cn[1].v, cn[2].v}, fr[9];
|
|
366
|
+
make_frame(nrm, fr);
|
|
367
|
+
for (int i = 0; i < nb; i++)
|
|
368
|
+
if (cd[i].v < 0.f) {
|
|
369
|
+
float pos[3] = {cp[i][0].v, cp[i][1].v, cp[i][2].v};
|
|
370
|
+
add_contact(s, pair, i, cd[i].v, pos, fr);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
#endif
|
|
376
|
+
"""
|
|
377
|
+
rf = "#ifndef DK_RF_DEVICE\n#define DK_RF_DEVICE\n" + RF_DEVICE + "#endif\n"
|
|
378
|
+
return ((head, rf + boxbox.DEVICE + head, 1), (start, start + body, 1))
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
def box_box_prim_source():
|
|
382
|
+
"""step.py's box_box with the separating-axis primitive (BOX_BOX_CCD 0, NATIVECCD disabled) as a template over the
|
|
383
|
+
scalar type, generated from its text: box_box_prim_t<T>(p1, R1, s1, p2, R2, s2, slot, &dist, pos, nrm) -> 1 when
|
|
384
|
+
contact slot (the forward's add_contact slot) exists, its distance, position and normal (the frame is
|
|
385
|
+
make_frame(nrm)). With T = D1 the adjoint replays it once per pose direction (ADJ_BOX_BOX_PRIM)."""
|
|
386
|
+
from nablaengine.forest_engine import step
|
|
387
|
+
src = step.STEP_DEVICE
|
|
388
|
+
head = ("template <class S>\n__device__ void box_box(S& s, int pair, const float* p1, const float* R1, "
|
|
389
|
+
"const float* s1, const float* p2,\n const float* R2, const float* s2) {\n")
|
|
390
|
+
i = src.index(head)
|
|
391
|
+
j = src.index(" float fr[9];\n make_frame(nrm, fr);\n#if BOX_BOX_CCD", i)
|
|
392
|
+
body = src[i + len(head):j]
|
|
393
|
+
import re
|
|
394
|
+
for a, b in (("flag_unsupported(s, 2);", "(void)0;"), ("return;", "return 0;")):
|
|
395
|
+
body = body.replace(a, b)
|
|
396
|
+
body = re.sub(r"\bfloat\b", "T", body)
|
|
397
|
+
body = re.sub(r"\b(mv3|mtv3|mm3|mmt3|dot3|safe_div|rotmore_of)\(", r"t_\1(", body)
|
|
398
|
+
body = re.sub(r"\b(fabs|sqrt|fmin|fmax)f\(", r"t_\1(", body)
|
|
399
|
+
if "atomic" in body or "add_contact" in body:
|
|
400
|
+
raise RuntimeError("box_box_prim_source: unexpected side effects in the generated text")
|
|
401
|
+
sig = ("// step.py's box_box (the separating-axis primitive) over a scalar type (model_hooks.box_box_prim_source)\n"
|
|
402
|
+
"template <class T>\n"
|
|
403
|
+
"__device__ __noinline__ int box_box_prim_t(const T* p1, const T* R1, const float* s1, const T* p2,\n"
|
|
404
|
+
" const T* R2, const float* s2, int slot, T* out_dist, T* out_pos,\n"
|
|
405
|
+
" T* out_nrm) {\n")
|
|
406
|
+
tail = """ if (slot < 0 || slot >= n || !(depth[slot] < 0.f)) return 0;
|
|
407
|
+
T pl[3] = {pts[slot][0], pts[slot][1], pts[slot][2] + hz}, pc[3];
|
|
408
|
+
t_mv3(rw, pl, pc);
|
|
409
|
+
for (int k = 0; k < 3; k++) { out_pos[k] = pc[k] + pw[k]; out_nrm[k] = nrm[k]; }
|
|
410
|
+
*out_dist = depth[slot];
|
|
411
|
+
return 1;
|
|
412
|
+
}
|
|
413
|
+
"""
|
|
414
|
+
return sig + body + tail
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
ADJ_BOX_BOX_PRIM = """ if (t1 == 3 && t2 == 3) { // box-box, the separating-axis primitive (nablaengine.boxbox): the contact's distance,
|
|
418
|
+
// position and normal differentiated in forward mode (D1) along the 12 pose directions (positions; rotations
|
|
419
|
+
// about the world axes, dR = [e] R), with the collision's branches fixed by the values
|
|
420
|
+
float R1[9], R2[9], nrm[3];
|
|
421
|
+
quat2mat(q1, R1);
|
|
422
|
+
quat2mat(q2, R2);
|
|
423
|
+
{
|
|
424
|
+
D1 P1[3], Q1[9], P2[3], Q2[9], dd, pp[3], nn[3];
|
|
425
|
+
for (int k = 0; k < 3; k++) { P1[k] = D1(p1[k]); P2[k] = D1(p2[k]); }
|
|
426
|
+
for (int k = 0; k < 9; k++) { Q1[k] = D1(R1[k]); Q2[k] = D1(R2[k]); }
|
|
427
|
+
if (!bb_slot_t<D1>(P1, Q1, sz1, P2, Q2, sz2, slot, &dd, pp, nn)) return -1;
|
|
428
|
+
*dist = dd.v;
|
|
429
|
+
for (int k = 0; k < 3; k++) { pos[k] = pp[k].v; nrm[k] = nn[k].v; }
|
|
430
|
+
}
|
|
431
|
+
make_frame(nrm, fr);
|
|
432
|
+
if (!(-0.5f < nrm[1] && nrm[1] < 0.5f)) br |= ADJ_BR_FRAME_Z;
|
|
433
|
+
if (cot) {
|
|
434
|
+
float nbar[3];
|
|
435
|
+
make_frame_vjp(fr, cot + 4, nbar);
|
|
436
|
+
for (int j = 0; j < 12; j++) {
|
|
437
|
+
D1 P1[3], Q1[9], P2[3], Q2[9], dd, pp[3], nn[3];
|
|
438
|
+
const int b = j / 6, a = j % 3;
|
|
439
|
+
const bool rot = (j % 6) >= 3;
|
|
440
|
+
for (int k = 0; k < 3; k++) {
|
|
441
|
+
P1[k] = D1(p1[k], !rot && b == 0 && k == a ? 1.f : 0.f);
|
|
442
|
+
P2[k] = D1(p2[k], !rot && b == 1 && k == a ? 1.f : 0.f);
|
|
443
|
+
}
|
|
444
|
+
for (int r = 0; r < 3; r++)
|
|
445
|
+
for (int c = 0; c < 3; c++) { // ([e_a] R)_rc = (e_a x R_col c)_r
|
|
446
|
+
const float* R = b ? R2 : R1;
|
|
447
|
+
float e[3] = {0.f, 0.f, 0.f}, col[3] = {R[c], R[3 + c], R[6 + c]}, x[3];
|
|
448
|
+
e[a] = 1.f;
|
|
449
|
+
cross(e, col, x);
|
|
450
|
+
Q1[3 * r + c] = D1(R1[3 * r + c], rot && b == 0 ? x[r] : 0.f);
|
|
451
|
+
Q2[3 * r + c] = D1(R2[3 * r + c], rot && b == 1 ? x[r] : 0.f);
|
|
452
|
+
}
|
|
453
|
+
bb_slot_t<D1>(P1, Q1, sz1, P2, Q2, sz2, slot, &dd, pp, nn);
|
|
454
|
+
gb[j] = cot[0] * dd.d;
|
|
455
|
+
for (int k = 0; k < 3; k++) gb[j] += cot[1 + k] * pp[k].d + nbar[k] * nn[k].d;
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
return br;
|
|
459
|
+
}
|
|
460
|
+
"""
|
|
461
|
+
|
|
462
|
+
|
|
463
|
+
def convex_dual_source(ns="cvd", real="D1"):
|
|
464
|
+
"""forest_engine.convex.instantiate (kept here for the adjoint's callers)."""
|
|
465
|
+
from nablaengine.forest_engine import convex
|
|
466
|
+
return convex.instantiate(ns, real)
|
|
467
|
+
|
|
468
|
+
|
|
469
|
+
ADJ_MESH = """#if HAS_MESH
|
|
470
|
+
// a mesh pair's contact (model_hooks): plane_mesh or convex_pair replayed over duals (cvd) once per pose direction
|
|
471
|
+
// (positions; rotations about the world axes, dR = [e] R) with the branches fixed by the values: the contact slot's
|
|
472
|
+
// distance, position and frame (j = -1) and their exact tangents (gb, adj_contact_geom's layout). Plane-mesh: the
|
|
473
|
+
// replay pass records the slot's vertex in *mvid (adj_contact_one's lane variable, -1 before its replay call) and the
|
|
474
|
+
// pose directions' passes evaluate only the slot's expression from it (plane_mesh_hooks); the VJP's call (cot) after
|
|
475
|
+
// the replay skips its own replay pass (dist, pos, fr are not read there: zeros)
|
|
476
|
+
__device__ __noinline__ int adj_mesh_geom(int g1i, int t1, int g2i, int t2, int slot, const float* p1, const float* q1,
|
|
477
|
+
const float* p2, const float* q2, float* dist, float* pos, float* fr,
|
|
478
|
+
float* nu, const float* cot, float* gb, int* mvid) {
|
|
479
|
+
float R1[9], R2[9];
|
|
480
|
+
quat2mat(q1, R1);
|
|
481
|
+
quat2mat(q2, R2);
|
|
482
|
+
*nu = __int_as_float(0x7f800000);
|
|
483
|
+
const bool once = t1 == 0 && cot && *mvid >= 0; // (the replay call's vertex)
|
|
484
|
+
if (once) {
|
|
485
|
+
*dist = 0.f;
|
|
486
|
+
for (int k = 0; k < 3; k++) pos[k] = 0.f;
|
|
487
|
+
for (int k = 0; k < 9; k++) fr[k] = 0.f;
|
|
488
|
+
}
|
|
489
|
+
for (int j = once ? 0 : -1; j < (cot ? 12 : 0); j++) {
|
|
490
|
+
cvd::real P1[3], Q1[9], P2[3], Q2[9];
|
|
491
|
+
const int b = j < 0 ? -1 : j / 6, a = j < 0 ? 0 : j % 3;
|
|
492
|
+
const bool rot = j >= 0 && (j % 6) >= 3;
|
|
493
|
+
for (int k = 0; k < 3; k++) {
|
|
494
|
+
P1[k] = D1(p1[k], !rot && b == 0 && k == a ? 1.f : 0.f);
|
|
495
|
+
P2[k] = D1(p2[k], !rot && b == 1 && k == a ? 1.f : 0.f);
|
|
496
|
+
}
|
|
497
|
+
for (int r = 0; r < 3; r++)
|
|
498
|
+
for (int c = 0; c < 3; c++) {
|
|
499
|
+
const float* R = b == 1 ? R2 : R1;
|
|
500
|
+
float e[3] = {0.f, 0.f, 0.f}, col[3] = {R[c], R[3 + c], R[6 + c]}, x[3];
|
|
501
|
+
e[a] = 1.f;
|
|
502
|
+
cross(e, col, x);
|
|
503
|
+
Q1[3 * r + c] = D1(R1[3 * r + c], rot && b == 0 ? x[r] : 0.f);
|
|
504
|
+
Q2[3 * r + c] = D1(R2[3 * r + c], rot && b == 1 ? x[r] : 0.f);
|
|
505
|
+
}
|
|
506
|
+
cvd::DSink o;
|
|
507
|
+
if (t1 == 0) {
|
|
508
|
+
float n0[3];
|
|
509
|
+
zaxis_rn(q1, n0); // the plane's normal: the forward's value (collide_pair), the z axis's tangent
|
|
510
|
+
cvd::real nn[3] = {D1(n0[0], Q1[2].d), D1(n0[1], Q1[5].d), D1(n0[2], Q1[8].d)};
|
|
511
|
+
cvd::plane_mesh(P1, nn, g2i, P2, Q2, o);
|
|
512
|
+
} else {
|
|
513
|
+
cvd::convex_pair(g1i, t1, P1, Q1, g2i, t2, P2, Q2, o);
|
|
514
|
+
}
|
|
515
|
+
int i = 0;
|
|
516
|
+
while (i < o.n && o.slot[i] != slot) i++;
|
|
517
|
+
if (i == o.n) return -1;
|
|
518
|
+
if (j < 0) {
|
|
519
|
+
*dist = o.dist[i].v;
|
|
520
|
+
for (int k = 0; k < 3; k++) pos[k] = o.pos[i][k].v;
|
|
521
|
+
for (int k = 0; k < 9; k++) fr[k] = o.fr[i][k].v;
|
|
522
|
+
} else {
|
|
523
|
+
gb[j] = cot[0] * o.dist[i].d;
|
|
524
|
+
for (int k = 0; k < 3; k++) gb[j] += cot[1 + k] * o.pos[i][k].d;
|
|
525
|
+
for (int k = 0; k < 9; k++) gb[j] += cot[4 + k] * o.fr[i][k].d;
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
return (-0.5f < fr[1] && fr[1] < 0.5f) ? 0 : ADJ_BR_FRAME_Z;
|
|
529
|
+
}
|
|
530
|
+
#endif
|
|
531
|
+
"""
|
|
532
|
+
|
|
533
|
+
|
|
534
|
+
MESH_MVID = (
|
|
535
|
+
("__device__ __forceinline__ int adj_pair_geom(int pair, int slot, const float (*gx)[3], const float (*gq)[4],\n"
|
|
536
|
+
" float* dist, float* pos, float* fr, float* nu, const float* cot,\n"
|
|
537
|
+
" float* gb) {\n",
|
|
538
|
+
"__device__ __forceinline__ int adj_pair_geom(int pair, int slot, const float (*gx)[3], const float (*gq)[4],\n"
|
|
539
|
+
" float* dist, float* pos, float* fr, float* nu, const float* cot,\n"
|
|
540
|
+
" float* gb, int* mvid) { // mvid: model_hooks.ADJ_MESH\n"),
|
|
541
|
+
(" const int br = adj_pair_geom(pair, slot, s.gx, s.gq, &d, p, fr, &nu, nullptr, nullptr);\n",
|
|
542
|
+
" int mvid = -1; // (model_hooks.ADJ_MESH: the replay's plane-mesh vertex for the VJP's call)\n"
|
|
543
|
+
" const int br = adj_pair_geom(pair, slot, s.gx, s.gq, &d, p, fr, &nu, nullptr, nullptr, &mvid);\n"),
|
|
544
|
+
(" adj_pair_geom(pair, slot, s.gx, s.gq, &d2, p2, fr2, &nu2, cot, gb);\n",
|
|
545
|
+
" adj_pair_geom(pair, slot, s.gx, s.gq, &d2, p2, fr2, &nu2, cot, gb, &mvid);\n"),
|
|
546
|
+
)
|
|
547
|
+
|
|
548
|
+
|
|
549
|
+
def has_floss(m):
|
|
550
|
+
return bool((m.dof_frictionloss > 0).any())
|
|
551
|
+
|
|
552
|
+
|
|
553
|
+
def box_pairs(m):
|
|
554
|
+
"""(there is a pair with a box, every such pair is plane-box or box-box: the adjoint has them, there is a box-box
|
|
555
|
+
pair)."""
|
|
556
|
+
import mujoco
|
|
557
|
+
box, plane = int(mujoco.mjtGeom.mjGEOM_BOX), int(mujoco.mjtGeom.mjGEOM_PLANE)
|
|
558
|
+
pairs = [(int(m.geom_type[m.pair_geom1[p]]), int(m.geom_type[m.pair_geom2[p]])) for p in range(m.npair)]
|
|
559
|
+
with_box = [t for t in pairs if box in t]
|
|
560
|
+
return bool(with_box), all(plane in t or t == (box, box) for t in with_box), (box, box) in with_box
|
|
561
|
+
|
|
562
|
+
|
|
563
|
+
def integrator(m):
|
|
564
|
+
"""The forest engine's integrator for model m: "implicitfast", "euler" (MuJoCo's Euler with eulerdamp: the joint
|
|
565
|
+
damping implicit) or "explicit" (Euler with eulerdamp disabled, or without joint damping: qvel' = qvel + h qacc,
|
|
566
|
+
as MuJoCo skips the damped solve then)."""
|
|
567
|
+
import mujoco
|
|
568
|
+
I = mujoco.mjtIntegrator
|
|
569
|
+
if m.opt.integrator == I.mjINT_IMPLICITFAST:
|
|
570
|
+
return "implicitfast"
|
|
571
|
+
assert m.opt.integrator == I.mjINT_EULER, (
|
|
572
|
+
f"integrator {I(int(m.opt.integrator)).name}: the forest engine has implicitfast and Euler")
|
|
573
|
+
damp = not (m.opt.disableflags & mujoco.mjtDisableBit.mjDSBL_EULERDAMP) and bool((m.dof_damping > 0).any())
|
|
574
|
+
return "euler" if damp else "explicit"
|
|
575
|
+
|
|
576
|
+
|
|
577
|
+
_L_IMPLICIT = """#if DK_CLAMP_IMPLICIT == 2
|
|
578
|
+
s.L[TRI(i, i)] += h * ((off ? 0.f : c_kv[i] * fabsf(s.kvw[i])) + c_damp[i]);
|
|
579
|
+
#else
|
|
580
|
+
s.L[TRI(i, i)] += h * ((off ? 0.f : c_kv[i]) + c_damp[i]);
|
|
581
|
+
#endif
|
|
582
|
+
"""
|
|
583
|
+
_Q_IMPLICIT = """#if DK_CLAMP_IMPLICIT == 2
|
|
584
|
+
const float kv = ((s.kvoff[i >> 5] >> (i & 31)) & 1u) ? 0.f : c_kv[i] * fabsf(s.kvw[i]);
|
|
585
|
+
#else
|
|
586
|
+
const float kv = ((s.kvoff[i >> 5] >> (i & 31)) & 1u) ? 0.f : c_kv[i];
|
|
587
|
+
#endif
|
|
588
|
+
s.abar[i] -= h * (kv + c_damp[i]) * s.y[i];
|
|
589
|
+
"""
|
|
590
|
+
_NO_SOFT_KV = """#if DK_CLAMP_IMPLICIT == 2
|
|
591
|
+
#error "DK_CLAMP_IMPLICIT=2 (the implicit kv's fading weight) needs implicitfast"
|
|
592
|
+
#endif
|
|
593
|
+
"""
|
|
594
|
+
ENGINE_EULER = (
|
|
595
|
+
(_L_IMPLICIT, _NO_SOFT_KV + " (void)off;\n s.L[TRI(i, i)] += h * c_damp[i]; // Euler, eulerdamp (model_hooks)\n", 1),
|
|
596
|
+
(_Q_IMPLICIT, " s.abar[i] -= h * c_damp[i] * s.y[i]; // Euler, eulerdamp: Q = M + h damping (model_hooks)\n", 1),
|
|
597
|
+
)
|
|
598
|
+
ENGINE_EXPLICIT = (
|
|
599
|
+
(_L_IMPLICIT, _NO_SOFT_KV + " (void)off; // explicit Euler (model_hooks): qacc' = qacc after the solve\n", 1),
|
|
600
|
+
(" CHOL_M_SOLVE(s.L, s.L, s.tmp, lane, s.cbuf);\n",
|
|
601
|
+
" CHOL_M_SOLVE(s.L, s.L, s.tmp, lane, s.cbuf);\n"
|
|
602
|
+
" for (int i = lane; i < NV; i += 32) s.tmp[i] = s.qacc[i]; // explicit Euler (model_hooks)\n __syncwarp();\n", 1),
|
|
603
|
+
(_Q_IMPLICIT, " // explicit Euler (model_hooks): Q = M, a* bar = a_adv bar\n", 1),
|
|
604
|
+
)
|
|
605
|
+
|
|
606
|
+
ENGINE_SPRING = (
|
|
607
|
+
("DKC float c_kq[NV], c_afrc[NV], c_ctrlr[NV * 2], c_damp[NV], c_gcomp[NB];\n",
|
|
608
|
+
"DKC float c_kq[NV], c_afrc[NV], c_ctrlr[NV * 2], c_damp[NV], c_gcomp[NB];\n"
|
|
609
|
+
"DKC float c_stiff[NV], c_sref[NV]; // joint stiffness, spring reference (model_hooks)\n", 1),
|
|
610
|
+
(" s.qsm[i] = act - c_damp[i] * s.qvel[i] - s.qsm[i];\n",
|
|
611
|
+
" s.qsm[i] = act - c_damp[i] * s.qvel[i] - c_stiff[i] * (s.qpos[c_dqadr[i]] - c_sref[i]) - s.qsm[i];"
|
|
612
|
+
" // spring (model_hooks)\n", 1),
|
|
613
|
+
(" s.Pv[i] += c_damp[i] * s.lam[i];\n if (a < 0) continue;\n",
|
|
614
|
+
" s.Pv[i] += c_damp[i] * s.lam[i];\n s.Pq[i] += c_stiff[i] * s.lam[i]; // joint spring (model_hooks)\n"
|
|
615
|
+
" if (a < 0) continue;\n", 1),
|
|
616
|
+
)
|
|
617
|
+
|
|
618
|
+
|
|
619
|
+
def has_spring(m):
|
|
620
|
+
"""A hinge / slide joint with stiffness (free joints: none, source.constants asserts)."""
|
|
621
|
+
return bool((m.jnt_stiffness > 0).any())
|
|
622
|
+
|
|
623
|
+
|
|
624
|
+
def spring_constants(m):
|
|
625
|
+
"""(c_stiff, c_sref) per dof for ENGINE_SPRING."""
|
|
626
|
+
import numpy as np
|
|
627
|
+
k, ref = np.zeros(m.nv), np.zeros(m.nv)
|
|
628
|
+
for j in range(m.njnt):
|
|
629
|
+
if m.jnt_stiffness[j] > 0:
|
|
630
|
+
k[m.jnt_dofadr[j]], ref[m.jnt_dofadr[j]] = m.jnt_stiffness[j], m.qpos_spring[m.jnt_qposadr[j]]
|
|
631
|
+
return k, ref
|
|
632
|
+
|
|
633
|
+
|
|
634
|
+
def needed(m):
|
|
635
|
+
"""(the forest engine: always applied)"""
|
|
636
|
+
return True
|
|
637
|
+
|
|
638
|
+
|
|
639
|
+
def _apply(src, hooks):
|
|
640
|
+
for a, b, n in hooks:
|
|
641
|
+
if src.count(a) != n:
|
|
642
|
+
raise RuntimeError(f"model hook anchor found {src.count(a)} times, expected {n}: {a[:70]!r}")
|
|
643
|
+
src = src.replace(a, b)
|
|
644
|
+
return src
|
|
645
|
+
|
|
646
|
+
|
|
647
|
+
def apply(src, m, part="engine", damped=False):
|
|
648
|
+
"""src with the hooks model m needs (the forest engine: NCH and joint damping are in its text; the contact pairs
|
|
649
|
+
beyond the mjlab primitives get their forward and adjoint hooks here: plane-box, box-box, sphere-box, capsule-box,
|
|
650
|
+
meshes); part: "engine" (adjoint.cuda_source's text), "forward" (the forward alone) or "kinematics". damped: an
|
|
651
|
+
Euler model without damping takes the damped update ("euler") instead of "explicit" (a build with per-world
|
|
652
|
+
dof_damping: world_params.damped)."""
|
|
653
|
+
hooks = ()
|
|
654
|
+
if part == "kinematics":
|
|
655
|
+
from nablaengine.forest_engine import nb64, nb128
|
|
656
|
+
src = nb128.apply(nb64.apply(src, m, "kinematics"), m, "kinematics")
|
|
657
|
+
if m.nv < 32 and part in ("engine", "forward"):
|
|
658
|
+
hooks += ENGINE_SMALL_NV if part == "engine" else ENGINE_SMALL_NV[:1]
|
|
659
|
+
if part == "engine":
|
|
660
|
+
from nablaengine.forest_engine import source
|
|
661
|
+
hooks += source.anc_hooks(m)
|
|
662
|
+
hooks += source.gcoff_hooks(m)
|
|
663
|
+
from nablaengine.forest_engine import step
|
|
664
|
+
hooks += step.limit_hooks(m)
|
|
665
|
+
if has_floss(m) and part == "engine":
|
|
666
|
+
hooks += ENGINE_FLOSS + (('static_assert(NFLOSS == 0, "the forest adjoint: no frictionloss rows yet");',
|
|
667
|
+
'// (model_hooks: the frictionloss rows in the adjoint)', 1),
|
|
668
|
+
('static_assert(NFLOSS == 0, "(the forest engine: the contact adjoint is not used, '
|
|
669
|
+
'DK_ADJ_CONTACTS 0)");', '// (model_hooks: the contact rows after the frictionloss rows)',
|
|
670
|
+
1))
|
|
671
|
+
from nablaengine.forest_engine import step
|
|
672
|
+
hooks += step.floss_hooks(m)
|
|
673
|
+
if part == "engine" and integrator(m) != "implicitfast":
|
|
674
|
+
hooks += ENGINE_EULER if integrator(m) == "euler" or damped else ENGINE_EXPLICIT
|
|
675
|
+
if part == "engine" and has_spring(m):
|
|
676
|
+
hooks += ENGINE_SPRING
|
|
677
|
+
if box_pairs(m)[0] and part == "engine":
|
|
678
|
+
hooks += ENGINE_PLANE_BOX
|
|
679
|
+
mesh = has_mesh(m)
|
|
680
|
+
if (box_prim(m) or mesh) and part == "engine":
|
|
681
|
+
extra = DUAL_DEVICE + (convex_dual_source() + ADJ_MESH if mesh else "")
|
|
682
|
+
hooks += (("__device__ __noinline__ int adj_contact_geom(", extra + "__device__ __noinline__ int adj_contact_geom(",
|
|
683
|
+
1),)
|
|
684
|
+
if mesh and part in ("engine", "forward"):
|
|
685
|
+
hooks += plane_mesh_hooks(part)
|
|
686
|
+
capbox = has_sphere_box(m) or has_capsule_box(m)
|
|
687
|
+
from nablaengine.g1_engine import model_hooks as g1_hooks
|
|
688
|
+
if capbox and part in ("engine", "forward"):
|
|
689
|
+
hooks += capbox_hooks(m)
|
|
690
|
+
if capbox and part == "engine":
|
|
691
|
+
hooks += ((" if (t1 != 2 || t2 != 2) return -1;\n", ADJ_CAPBOX + " if (t1 != 2 || t2 != 2) return -1;\n", 1),
|
|
692
|
+
("__device__ __noinline__ int adj_contact_geom(", CB_DIR_VJP + "__device__ __noinline__ int adj_contact_geom(",
|
|
693
|
+
1)) + g1_hooks.capbox_coop_hooks(CB_COOP_DISPATCH)
|
|
694
|
+
if box_prim(m) and part in ("engine", "forward"):
|
|
695
|
+
hooks += boxbox_fwd_hooks()
|
|
696
|
+
if box_prim(m) and part == "engine":
|
|
697
|
+
hooks += ((" float n[3], c1b[3] = {0.f, 0.f, 0.f}, c2b[3] = {0.f, 0.f, 0.f}, nb[3];\n",
|
|
698
|
+
ADJ_BOX_BOX_PRIM + " float n[3], c1b[3] = {0.f, 0.f, 0.f}, c2b[3] = {0.f, 0.f, 0.f}, nb[3];\n", 1),)
|
|
699
|
+
if mesh and part == "engine":
|
|
700
|
+
hooks += ((" int g1 = PG1(pair), g2 = PG2(pair);\n return adj_contact_geom(",
|
|
701
|
+
" int g1 = PG1(pair), g2 = PG2(pair);\n#if HAS_MESH\n if (c_gtype[g2] == 4 && c_gtype[g1] == 0) // (model_hooks: "
|
|
702
|
+
"plane-mesh)\n"
|
|
703
|
+
" return adj_mesh_geom(g1, c_gtype[g1], g2, c_gtype[g2], slot, gx[g1], gq[g1], gx[g2], gq[g2], dist, "
|
|
704
|
+
"pos, fr, nu, cot, gb, mvid);\n#endif\n return adj_contact_geom(", 1),)
|
|
705
|
+
hooks += tuple((a, b, 1) for a, b in MESH_MVID)
|
|
706
|
+
from nablaengine.forest_engine import cylinder
|
|
707
|
+
hooks += cylinder.hooks(m, part, "cyc_pair_f" if _ccd_float() else "cyc_pair_r")
|
|
708
|
+
from nablaengine.forest_engine import ellipsoid
|
|
709
|
+
hooks += ellipsoid.hooks(m, part, "cyc_pair_f" if _ccd_float() else "cyc_pair_r")
|
|
710
|
+
if part in ("engine", "forward"):
|
|
711
|
+
hooks += convex_mj_hooks(m, part)
|
|
712
|
+
from nablaengine import ccd
|
|
713
|
+
src = _apply(src, hooks)
|
|
714
|
+
if part == "engine" and (any(ccd_pairs(m)) or cylinder.ccd_pairs(m) or ellipsoid.ccd_pairs(m)):
|
|
715
|
+
from nablaengine.g1_engine import cylccd
|
|
716
|
+
src = cylccd.apply_rec(src)
|
|
717
|
+
if ccd_pairs(m)[1] and not sbb_boxbox(m):
|
|
718
|
+
from nablaengine.forest_engine import ccd_coop
|
|
719
|
+
src = ccd_coop.apply(src)
|
|
720
|
+
if part == "forward":
|
|
721
|
+
from nablaengine.g1_engine import exact_replay
|
|
722
|
+
src = exact_replay.apply(src, m)
|
|
723
|
+
return ccd.apply_gjk_mj4(src)
|
|
724
|
+
|
|
725
|
+
|
|
726
|
+
def ccd_pairs(m):
|
|
727
|
+
"""(the model has a mesh pair other than plane-mesh, it has a box-box pair collided by native CCD): the pairs
|
|
728
|
+
MuJoCo Warp collides by its convex collision (GJK, EPA, multicontact), which the forest engine runs as the G1
|
|
729
|
+
engine's port of MuJoCo Warp 3.17's (convex_mj_hooks)."""
|
|
730
|
+
import mujoco
|
|
731
|
+
G = mujoco.mjtGeom
|
|
732
|
+
mesh, plane = int(G.mjGEOM_MESH), int(G.mjGEOM_PLANE)
|
|
733
|
+
types = [(int(m.geom_type[m.pair_geom1[p]]), int(m.geom_type[m.pair_geom2[p]])) for p in range(m.npair)]
|
|
734
|
+
return (any(mesh in t and plane not in t for t in types),
|
|
735
|
+
box_pairs(m)[2] and not (m.opt.disableflags & mujoco.mjtDisableBit.mjDSBL_NATIVECCD))
|
|
736
|
+
|
|
737
|
+
|
|
738
|
+
def convex_mj_header(m):
|
|
739
|
+
"""The #defines of g1_engine.cylccd's code for the forest model m: MuJoCo Warp's EPA iterations (16 when every
|
|
740
|
+
convex pair is box-box, else ccd_iterations), the multicontact buffers (npolygonmax, nmeshdegmax: at least a box's
|
|
741
|
+
4 and 3), the mesh tables (forest_engine.convex's, the same layout), multiccd (box-box always, as MuJoCo Warp)."""
|
|
742
|
+
import mujoco
|
|
743
|
+
from nablaengine.forest_engine import convex, cylinder, step
|
|
744
|
+
meshes, boxbox = ccd_pairs(m)
|
|
745
|
+
mt = convex.mesh_tables(m, step.collision_geoms(m))
|
|
746
|
+
it = int(m.opt.ccd_iterations)
|
|
747
|
+
multi = int(not (m.opt.disableflags & mujoco.mjtDisableBit.mjDSBL_MULTICCD))
|
|
748
|
+
cyl = cylinder.ccd_with_cylinder(m)
|
|
749
|
+
from nablaengine.forest_engine import ellipsoid
|
|
750
|
+
ell = ellipsoid.ccd_pairs(m)
|
|
751
|
+
return (f"#define CY_GJK_ITERATIONS {it}\n#define CY_EPA_ITERATIONS {it if meshes or cyl or ell else 16}\n"
|
|
752
|
+
f"#define CY_CCD_TOLERANCE {float(m.opt.ccd_tolerance):.9g}f\n"
|
|
753
|
+
f"#define CY_NPOLY {max(16 if cyl else 4, mt['poly_max'] if mt else 0)}\n#define CY_NDEG {max(3, mt['deg_max'] if mt else 0)}\n"
|
|
754
|
+
f"#define CY_HAS_MESH {int(mt is not None)}\n#define CY_MULTICCD {multi}\n")
|
|
755
|
+
|
|
756
|
+
|
|
757
|
+
_CYT = "#define CYT(t) ((t) == 4 ? 6 : (t)) // (model_hooks.convex_mj_hooks: the forest's mesh type 4 as cylccd's 6)\n"
|
|
758
|
+
|
|
759
|
+
CONVEX_MJ_WRAP = r"""// convex pairs (model_hooks.convex_mj_hooks): CV_PAIR's signature, g1_engine.cylccd's MuJoCo Warp 3.17 CCD over RF
|
|
760
|
+
__device__ __noinline__ void convex_pair_mj(int g1i, int t1, const float* p1, const float* q1, int g2i, int t2,
|
|
761
|
+
const float* p2, const float* q2, CSink& o) {
|
|
762
|
+
cyc_pair_r(CYT(t1), g1i, p1, q1, c_gsize + 3 * g1i, CYT(t2), g2i, p2, q2, c_gsize + 3 * g2i, o);
|
|
763
|
+
}
|
|
764
|
+
"""
|
|
765
|
+
|
|
766
|
+
ADJ_CCD = """ if ((t2 == 4 && t1 != 0) || (BOX_BOX_CCD && t1 == 3 && t2 == 3)) { // native CCD pairs (model_hooks.convex_mj_hooks:
|
|
767
|
+
// g1_engine.cylccd): the forward's contact `slot` replayed, its VJP by D1 along the 12 pose directions
|
|
768
|
+
if (cyc_adj(CYT(t1), p1, q1, sz1, CYT(t2), p2, q2, sz2, slot, cot, gb, dist, pos, fr, crec) < 0) return -1;
|
|
769
|
+
if (!(-0.5f < fr[1] && fr[1] < 0.5f)) br |= ADJ_BR_FRAME_Z;
|
|
770
|
+
return br;
|
|
771
|
+
}
|
|
772
|
+
"""
|
|
773
|
+
|
|
774
|
+
|
|
775
|
+
def _ccd_float():
|
|
776
|
+
import os
|
|
777
|
+
return os.environ.get("DK_CCD_FLOAT", "0") not in ("", "0")
|
|
778
|
+
|
|
779
|
+
|
|
780
|
+
def convex_mj_hooks(m, part):
|
|
781
|
+
"""The forest engine's native CCD pairs (ccd_pairs: a mesh with a sphere, capsule, box or mesh; box-box unless
|
|
782
|
+
NATIVECCD is disabled) through the G1 engine's port of MuJoCo Warp 3.17's convex collision (g1_engine.cylccd: gjk
|
|
783
|
+
phase, EPA, multicontact; the forward over RF, the adjoint's replay over D1, exact tangents), in place of
|
|
784
|
+
forest_engine.convex (CV_PAIR) and step.py's native-CCD box_box: the device code ahead of box_pair, CV_PAIR,
|
|
785
|
+
box_pair's box-box, and (part "engine") the contact adjoint's branch ADJ_CCD with cyc_adj."""
|
|
786
|
+
import re
|
|
787
|
+
from nablaengine.g1_engine import cylccd
|
|
788
|
+
from nablaengine.forest_engine import cylinder
|
|
789
|
+
meshes, boxbox = ccd_pairs(m)
|
|
790
|
+
cyl = cylinder.ccd_pairs(m)
|
|
791
|
+
from nablaengine.forest_engine import ellipsoid
|
|
792
|
+
ell = ellipsoid.ccd_pairs(m)
|
|
793
|
+
if not (meshes or boxbox or cyl or ell):
|
|
794
|
+
return ()
|
|
795
|
+
dev = cylccd.device()
|
|
796
|
+
if ellipsoid.has_ellipsoid(m):
|
|
797
|
+
dev = ellipsoid.support_text(dev)
|
|
798
|
+
dev = re.sub(r"#ifndef CSINK_DEFINED\n.*?#endif\n", "", dev, count=1, flags=re.S)
|
|
799
|
+
old = " if (!((f1 && f2) || (g1.type == 2 && f2) || (f1 && g2.type == 2))) midx = -1; // (_supports_multiccd)"
|
|
800
|
+
if dev.count(old) != 3:
|
|
801
|
+
raise RuntimeError("convex_mj_hooks: no _supports_multiccd test in cylccd's text")
|
|
802
|
+
dev = dev.replace(old, " if (!((g1.type == 3 && g2.type == 3) || (CY_MULTICCD && ((f1 && f2) || (g1.type == 2 && f2) || "
|
|
803
|
+
"(f1 && g2.type == 2))))) midx = -1;\n // (_supports_multiccd; model_hooks.convex_mj_hooks: "
|
|
804
|
+
"box-box always)")
|
|
805
|
+
wrap, adj_fn = CONVEX_MJ_WRAP, cylccd.ADJ_FN
|
|
806
|
+
if _ccd_float():
|
|
807
|
+
fdev = cylccd.instantiate("cycf", "float")
|
|
808
|
+
i = cylccd.GLOBAL.index("// the forward's contacts of the pair")
|
|
809
|
+
dev = "#define CY_REC 0\n" + dev
|
|
810
|
+
fwd = cylccd.GLOBAL[i:].replace("cyc_pair_r", "cyc_pair_f").replace("cyc_pair_x", "cyc_pair_xf")
|
|
811
|
+
fwd = fwd.replace("RF P1[3]", "float P1[3]")
|
|
812
|
+
fwd = fwd.replace("cyc_geom<RF>", "cyc_geom<float>").replace("cycr::", "cycf::").replace(", RF>(", ", float>(")
|
|
813
|
+
fwd = fwd.replace("d.dist[i].v", "d.dist[i]").replace("d.pos[i][k].v", "d.pos[i][k]").replace("d.fr[i][k].v", "d.fr[i][k]")
|
|
814
|
+
dev += fdev + fwd
|
|
815
|
+
wrap, adj_fn = wrap.replace("cyc_pair_r(", "cyc_pair_f("), adj_fn.replace("cyc_pair_r(", "cyc_pair_f(")
|
|
816
|
+
dev = convex_mj_header(m) + _CYT + dev + wrap
|
|
817
|
+
sbb = boxbox and sbb_boxbox(m)
|
|
818
|
+
if sbb:
|
|
819
|
+
dev += sbb_device(m)
|
|
820
|
+
anchor = "#if HAS_BOX\n// capsule-box (t1 2) or box-box (t1 3) into o:"
|
|
821
|
+
hooks = ((anchor, dev + anchor, 1),)
|
|
822
|
+
if meshes:
|
|
823
|
+
hooks += (("#define CV_PAIR convex_pair_r\n", "#define CV_PAIR convex_pair_mj // (model_hooks.convex_mj_hooks)\n", 1),
|
|
824
|
+
("#define CV_PAIR convex_pair\n", "#define CV_PAIR convex_pair_mj // (model_hooks.convex_mj_hooks)\n", 1))
|
|
825
|
+
if sbb:
|
|
826
|
+
old = " box_box(o, 0, p1, R1, c_gsize + 3 * g1, p2, R2, c_gsize + 3 * g2);\n"
|
|
827
|
+
hooks += ((old, "#if BOX_BOX_CCD // (model_hooks.sbb_device: nablaengine.ccd's box-box CCD, the tree engine's)\n"
|
|
828
|
+
" if (!sbb::boxes_separated(p1, R1, c_gsize + 3 * g1, p2, R2, c_gsize + 3 * g2))\n"
|
|
829
|
+
" sbb::ccd_box_box(p1, R1, c_gsize + 3 * g1, p2, R2, c_gsize + 3 * g2, o);\n#else\n"
|
|
830
|
+
+ old + "#endif\n", 1),)
|
|
831
|
+
elif boxbox:
|
|
832
|
+
old = " box_box(o, 0, p1, R1, c_gsize + 3 * g1, p2, R2, c_gsize + 3 * g2);\n"
|
|
833
|
+
hooks += ((old, "#if BOX_BOX_CCD // (model_hooks.convex_mj_hooks: MuJoCo Warp 3.17's CCD)\n"
|
|
834
|
+
f" {'cyc_pair_f' if _ccd_float() else 'cyc_pair_r'}(3, g1, p1, q1, c_gsize + 3 * g1, 3, g2, p2, q2, "
|
|
835
|
+
"c_gsize + 3 * g2, o);\n#else\n"
|
|
836
|
+
+ old + "#endif\n", 1),)
|
|
837
|
+
if part == "engine":
|
|
838
|
+
head = "__device__ __noinline__ int adj_contact_geom("
|
|
839
|
+
adj = ADJ_CCD
|
|
840
|
+
if cyl:
|
|
841
|
+
old = "(BOX_BOX_CCD && t1 == 3 && t2 == 3)) {"
|
|
842
|
+
adj = adj.replace(old, "(BOX_BOX_CCD && t1 == 3 && t2 == 3)\n || " + cylinder.CCD_COND + ") {")
|
|
843
|
+
if ell:
|
|
844
|
+
old = "(BOX_BOX_CCD && t1 == 3 && t2 == 3)"
|
|
845
|
+
adj = adj.replace(old, old + "\n || " + ellipsoid.CCD_COND, 1)
|
|
846
|
+
if sbb:
|
|
847
|
+
adj_fn = "namespace sbb {\n" + _sbb_adj_device() + "} // namespace sbb\n" + adj_fn
|
|
848
|
+
adj = ADJ_SBB + adj
|
|
849
|
+
hooks += ((head, adj_fn + head, 1),
|
|
850
|
+
(" if (t1 != 2 || t2 != 2) return -1;\n", adj + " if (t1 != 2 || t2 != 2) return -1;\n", 1))
|
|
851
|
+
if meshes:
|
|
852
|
+
hooks += ((" cvd::convex_pair(g1i, t1, P1, Q1, g2i, t2, P2, Q2, o);\n",
|
|
853
|
+
" return -1; // (model_hooks.convex_mj_hooks: the convex pairs in cyc_adj)\n", 1),)
|
|
854
|
+
return hooks
|
|
855
|
+
|
|
856
|
+
|
|
857
|
+
def sbb_boxbox(m):
|
|
858
|
+
"""A G1-class model (engines.is_g1_class: one free tree, e.g. the Booster T1's foot-foot pair) collides its box-box
|
|
859
|
+
pairs under native CCD with the tree engine's code, nablaengine.ccd's ccd_box_box, and differentiates them with its
|
|
860
|
+
exact VJP ccd_box_box_vjp (ADJ_SBB), as g1_engine.model_hooks does, in place of g1_engine.cylccd's CCD and the
|
|
861
|
+
replay of all of it over D1 along 12 pose directions per contact (cyc_adj): that replay took a world with a
|
|
862
|
+
Booster T1 foot-foot contact 10x the adjoint time of the others (V100: 38.3 against 3.8 ms for 4096 such worlds),
|
|
863
|
+
and the slowest worlds set the kernel's time. Multi-tree models (objects) keep cyc_adj (exact tangents also for the EPA
|
|
864
|
+
features ccd_box_box_vjp takes as points of their boxes)."""
|
|
865
|
+
from nablaengine import engines
|
|
866
|
+
return engines.is_g1_class(m)
|
|
867
|
+
|
|
868
|
+
|
|
869
|
+
def sbb_device(m):
|
|
870
|
+
"""nablaengine.ccd's box-box CCD in namespace sbb (its CSink struct is the forest's, step.py), its #defines
|
|
871
|
+
(ccd.header) and the separating-axis prefilter boxes_separated (g1_engine.model_hooks.box_box_hook)."""
|
|
872
|
+
import re as _re
|
|
873
|
+
from nablaengine import ccd
|
|
874
|
+
from nablaengine.g1_engine import model_hooks as g1_hooks
|
|
875
|
+
dev, n = _re.subn(r"#define SINK_MAX 8\nstruct CSink \{.*?\};\n", "", ccd.DEVICE, count=1, flags=_re.S)
|
|
876
|
+
if n != 1:
|
|
877
|
+
raise RuntimeError("sbb_device: no CSink struct in nablaengine.ccd's text")
|
|
878
|
+
wrap = g1_hooks.box_box_hook()[1][len(ccd.DEVICE):]
|
|
879
|
+
sep = wrap[:wrap.index("template <class S>\n__device__ void box_box(")]
|
|
880
|
+
assert "__device__ bool boxes_separated(" in sep
|
|
881
|
+
hdr = "".join(f"#ifndef {l.split()[1]}\n{l}\n#endif\n" for l in ccd.header(m).splitlines())
|
|
882
|
+
return ("// a G1-class model's box-box pairs (forest_engine.model_hooks.sbb_device: nablaengine.ccd's CCD, the tree\n"
|
|
883
|
+
"// engine's)\n" + hdr + "namespace sbb {\n" + dev + sep + "} // namespace sbb\n")
|
|
884
|
+
|
|
885
|
+
|
|
886
|
+
def _sbb_adj_device():
|
|
887
|
+
from nablaengine import ccd
|
|
888
|
+
return ccd.ADJ_DEVICE
|
|
889
|
+
|
|
890
|
+
|
|
891
|
+
ADJ_SBB = """ if (BOX_BOX_CCD && t1 == 3 && t2 == 3) { // box-box of a G1-class model (model_hooks.sbb_boxbox): the forward's
|
|
892
|
+
// CCD contact `slot` and its exact VJP (nablaengine.ccd ccd_box_box_vjp), as the tree engine's ADJ_BOX_BOX
|
|
893
|
+
float R1[9], R2[9];
|
|
894
|
+
quat2mat(q1, R1);
|
|
895
|
+
quat2mat(q2, R2);
|
|
896
|
+
if (!sbb::ccd_box_box_vjp(p1, R1, sz1, p2, R2, sz2, slot, cot, gb, dist, pos, fr)) return -1;
|
|
897
|
+
if (!(-0.5f < fr[1] && fr[1] < 0.5f)) br |= ADJ_BR_FRAME_Z;
|
|
898
|
+
return br;
|
|
899
|
+
}
|
|
900
|
+
"""
|
|
901
|
+
|
|
902
|
+
|
|
903
|
+
def has_mesh(m):
|
|
904
|
+
"""A mesh collision geom in a pair: its contacts get the forward-mode VJP (adj_mesh_geom)."""
|
|
905
|
+
import mujoco
|
|
906
|
+
mesh = int(mujoco.mjtGeom.mjGEOM_MESH)
|
|
907
|
+
return any(int(m.geom_type[g]) == mesh for p in range(m.npair) for g in (m.pair_geom1[p], m.pair_geom2[p]))
|
|
908
|
+
|
|
909
|
+
|
|
910
|
+
def box_prim(m):
|
|
911
|
+
"""A box-box pair collided by the separating-axis primitive (NATIVECCD disabled): its contacts get the adjoint."""
|
|
912
|
+
import mujoco
|
|
913
|
+
return box_pairs(m)[2] and bool(m.opt.disableflags & mujoco.mjtDisableBit.mjDSBL_NATIVECCD)
|