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,1464 @@
|
|
|
1
|
+
"""CUDA source of the forest engine's smooth dynamics (block per world), generated from the MuJoCo model."""
|
|
2
|
+
|
|
3
|
+
import mujoco
|
|
4
|
+
import numpy as np
|
|
5
|
+
|
|
6
|
+
CONST_LAYOUT = (
|
|
7
|
+
("c_parent", "i", "nb", 1), ("c_depth", "i", "nb", 1), ("c_qadr", "i", "nb", 1), ("c_dadr", "i", "nb", 1),
|
|
8
|
+
("c_nchild", "i", "nb", 1), ("c_child", "i", "nb", 3), ("c_pos", "f", "nb", 3), ("c_quat", "f", "nb", 4),
|
|
9
|
+
("c_axis", "f", "nb", 3), ("c_ipos", "f", "nb", 3), ("c_iquat", "f", "nb", 4), ("c_mass", "f", "nb", 1),
|
|
10
|
+
("c_inert", "f", "nb", 3), ("c_grav", "f", "one", 3), ("c_armature", "f", "nv", 1), ("c_nanc", "i", "nb", 1),
|
|
11
|
+
("c_anc", "i", "nb", 12), ("c_kp", "f", "nv", 1), ("c_kv", "f", "nv", 1), ("c_frclim", "f", "nv", 1),
|
|
12
|
+
("c_misc", "f", "one", 2),
|
|
13
|
+
("c_jtype", "i", "nb", 1), ("c_root", "i", "nb", 1), ("c_kq", "f", "nv", 1), ("c_afrc", "f", "nv", 1),
|
|
14
|
+
("c_act", "i", "nv", 1), ("c_ctrlr", "f", "nv", 2), ("c_dqadr", "i", "nv", 1), ("c_damp", "f", "nv", 1),
|
|
15
|
+
("c_gcomp", "f", "nb", 1),
|
|
16
|
+
)
|
|
17
|
+
JFREE, JHINGE, JSLIDE = 0, 1, 2
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
MAX_CHILDREN = 8
|
|
21
|
+
MIN_NV = 1
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def children_of(m):
|
|
25
|
+
"""[[child body ids] per body]."""
|
|
26
|
+
children = [[] for _ in range(m.nbody)]
|
|
27
|
+
for b in range(1, m.nbody):
|
|
28
|
+
children[m.body_parentid[b]].append(b)
|
|
29
|
+
return children
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def child_width(m):
|
|
33
|
+
"""c_child's entries per body: 3 (the G1's layout), or the model's most children per body."""
|
|
34
|
+
return max(3, max(len(c) for c in children_of(m)[1:]))
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def has_damping(m):
|
|
38
|
+
"""(the forest engine: c_damp is always in the layout, model_hooks.py's damping hooks are not applied)"""
|
|
39
|
+
return False
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def const_layout(m):
|
|
43
|
+
"""CONST_LAYOUT with c_child's and c_anc's widths for model m (child_width, anc_width)."""
|
|
44
|
+
w, wa = child_width(m), anc_width(m)
|
|
45
|
+
return tuple((sym, kind, n, w if sym == "c_child" else wa if sym == "c_anc" else width)
|
|
46
|
+
for sym, kind, n, width in CONST_LAYOUT)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
MAX_ANC = 40
|
|
50
|
+
ANC_UNROLLED = 16
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def hinge_ancestors(m):
|
|
54
|
+
"""[[hinge / slide ancestor body ids, nearest first] per body] (a free joint's body is no such ancestor)."""
|
|
55
|
+
out = [[] for _ in range(m.nbody)]
|
|
56
|
+
free = int(mujoco.mjtJoint.mjJNT_FREE)
|
|
57
|
+
for b in range(1, m.nbody):
|
|
58
|
+
k = m.body_parentid[b]
|
|
59
|
+
while k >= 1:
|
|
60
|
+
if not (m.body_jntnum[k] == 1 and int(m.jnt_type[m.body_jntadr[k]]) == free):
|
|
61
|
+
out[b].append(int(k))
|
|
62
|
+
k = m.body_parentid[k]
|
|
63
|
+
return out
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def anc_width(m):
|
|
67
|
+
"""c_anc's entries per body: 12 (the G1's layout), 16 for 13-16 hinge ancestors per body, MAX_ANC beyond."""
|
|
68
|
+
na = max((len(a) for a in hinge_ancestors(m)), default=0)
|
|
69
|
+
return 12 if na <= 12 else ANC_UNROLLED if na <= ANC_UNROLLED else MAX_ANC
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def anc_hooks(m):
|
|
73
|
+
"""The (anchor, replacement, count) edits of the engine text for c_anc's width beyond 12 (the mass matrix's rows
|
|
74
|
+
over a body's hinge ancestors; none for at most 12: the text unchanged). Up to ANC_UNROLLED the ancestors' motion
|
|
75
|
+
subspaces are held in registers (the loops unrolled, all loaded before the first store); beyond, a rolled loop
|
|
76
|
+
loads one ancestor and stores its entry (the same arithmetic per entry: dot3(w_a, n) + dot3(v_a, f))."""
|
|
77
|
+
w = anc_width(m)
|
|
78
|
+
if w == 12:
|
|
79
|
+
return ()
|
|
80
|
+
decl = (" c_anc[NB * 12];\n", f" c_anc[NB * {w}]; // (source.anc_hooks: {w} hinge ancestors per body)\n", 1)
|
|
81
|
+
if w <= ANC_UNROLLED:
|
|
82
|
+
return (decl,
|
|
83
|
+
(" int j = c_dadr[b], na = c_nanc[b], ad[12];\n float Jb[10], aw[12][3], av[12][3];\n",
|
|
84
|
+
f" int j = c_dadr[b], na = c_nanc[b], ad[{w}]; // (source.anc_hooks)\n"
|
|
85
|
+
f" float Jb[10], aw[{w}][3], av[{w}][3];\n", 1),
|
|
86
|
+
(" for (int a = 0; a < 12; a++) // the hinge ancestors' motion subspaces, all loaded before the first store\n"
|
|
87
|
+
" if (a < na) {\n int k = c_anc[12 * b + a];\n",
|
|
88
|
+
f" for (int a = 0; a < {w}; a++) // the hinge ancestors' motion subspaces, all loaded before the first store\n"
|
|
89
|
+
f" if (a < na) {{\n int k = c_anc[{w} * b + a];\n", 1),
|
|
90
|
+
(" for (int a = 0; a < 12; a++)\n if (a < na) s.M[TRI(j, ad[a])]",
|
|
91
|
+
f" for (int a = 0; a < {w}; a++)\n if (a < na) s.M[TRI(j, ad[a])]", 1))
|
|
92
|
+
return (decl,
|
|
93
|
+
(" int j = c_dadr[b], na = c_nanc[b], ad[12];\n float Jb[10], aw[12][3], av[12][3];\n",
|
|
94
|
+
" int j = c_dadr[b], na = c_nanc[b]; // (source.anc_hooks: the ancestors one at a time below)\n"
|
|
95
|
+
" float Jb[10];\n", 1),
|
|
96
|
+
("#pragma unroll\n for (int a = 0; a < 12; a++) // the hinge ancestors' motion subspaces, all loaded before the "
|
|
97
|
+
"first store\n if (a < na) {\n int k = c_anc[12 * b + a];\n ad[a] = c_dadr[k];\n"
|
|
98
|
+
" for (int t = 0; t < 3; t++) { aw[a][t] = s.W[k][t]; av[a][t] = s.V[k][t]; }\n }\n", "", 1),
|
|
99
|
+
("#pragma unroll\n for (int a = 0; a < 12; a++)\n if (a < na) s.M[TRI(j, ad[a])] = dot3(aw[a], n) + "
|
|
100
|
+
"dot3(av[a], f);\n",
|
|
101
|
+
"#pragma unroll 1\n for (int a = 0; a < na; a++) { // (source.anc_hooks: more than "
|
|
102
|
+
f"{ANC_UNROLLED} hinge ancestors)\n"
|
|
103
|
+
f" const int k = c_anc[{w} * b + a];\n"
|
|
104
|
+
" const float aw[3] = {s.W[k][0], s.W[k][1], s.W[k][2]}, av[3] = {s.V[k][0], s.V[k][1], s.V[k][2]};\n"
|
|
105
|
+
" s.M[TRI(j, c_dadr[k])] = dot3(aw, n) + dot3(av, f);\n }\n", 1))
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
COM_BODY = None
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def max_depth(m):
|
|
112
|
+
"""The kinematic tree's maximum depth (constants' second result)."""
|
|
113
|
+
depth = [0] * m.nbody
|
|
114
|
+
for b in range(1, m.nbody):
|
|
115
|
+
depth[b] = depth[m.body_parentid[b]] + 1
|
|
116
|
+
return max(depth)
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def gcoff_hooks(m):
|
|
120
|
+
"""The (anchor, replacement, count) edits of the engine text for gravcomp applied off the COM (fused bodies with
|
|
121
|
+
different gravcomp: nablaengine.model.gravcomp; none without: the text unchanged): c_gcoff (gcoff_constants), the
|
|
122
|
+
moment (R_b d_b) x gc m g in body_force and its VJP in the adjoint's body_local_vjp (N1's extra term: its cotangent
|
|
123
|
+
bWl, and the rotation's e x (f x Wl), e = R_b d_b, f = gc m g)."""
|
|
124
|
+
from nablaengine.model import gravcomp
|
|
125
|
+
if gravcomp.offsets(m) is None:
|
|
126
|
+
return ()
|
|
127
|
+
return (("DKC float c_kq[NV], c_afrc[NV], c_ctrlr[NV * 2], c_damp[NV], c_gcomp[NB];\n",
|
|
128
|
+
"DKC float c_kq[NV], c_afrc[NV], c_ctrlr[NV * 2], c_damp[NV], c_gcomp[NB];\n"
|
|
129
|
+
"DKC float c_gcoff[NB * 3]; // gravcomp's point: offset from the COM, body frame (source.gcoff_hooks)\n", 1),
|
|
130
|
+
(" for (int k = 0; k < 3; k++) N[k] = Ia[k] + u[k] + cf[k];\n",
|
|
131
|
+
" for (int k = 0; k < 3; k++) N[k] = Ia[k] + u[k] + cf[k];\n"
|
|
132
|
+
" { // gravcomp at its point (source.gcoff_hooks): + (R d) x gc m g\n"
|
|
133
|
+
" float e[3], f[3], t[3];\n"
|
|
134
|
+
" qrot(Q, c_gcoff + 3 * b, e);\n"
|
|
135
|
+
" for (int k = 0; k < 3; k++) f[k] = m * (gc * c_grav[k]);\n"
|
|
136
|
+
" cross(e, f, t);\n"
|
|
137
|
+
" for (int k = 0; k < 3; k++) N[k] += t[k];\n"
|
|
138
|
+
" }\n", 1),
|
|
139
|
+
(" for (int k = 0; k < 3; k++) { bVl[k] = F1[k]; bWl[k] = IL[k] + t[k] + u[k]; }\n",
|
|
140
|
+
" for (int k = 0; k < 3; k++) { bVl[k] = F1[k]; bWl[k] = IL[k] + t[k] + u[k]; }\n"
|
|
141
|
+
" float ge[3], gf[3]; // gravcomp at its point (source.gcoff_hooks): N1 += e x f, e = R d, f = gc m g\n"
|
|
142
|
+
" qrot(Q, c_gcoff + 3 * b, ge);\n"
|
|
143
|
+
" for (int k = 0; k < 3; k++) gf[k] = m * (c_gcomp[b] * c_grav[k]);\n"
|
|
144
|
+
" cross(ge, gf, t);\n"
|
|
145
|
+
" for (int k = 0; k < 3; k++) bWl[k] += t[k];\n", 1),
|
|
146
|
+
(" for (int k = 0; k < 3; k++) tb[k] = tau[k] + t[k];\n",
|
|
147
|
+
" for (int k = 0; k < 3; k++) tb[k] = tau[k] + t[k];\n"
|
|
148
|
+
" cross(gf, Wl, t); // (source.gcoff_hooks) e rotates with the body: e x (f x Wl)\n"
|
|
149
|
+
" cross(ge, t, u);\n"
|
|
150
|
+
" for (int k = 0; k < 3; k++) tb[k] += u[k];\n", 1))
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def gcoff_constants(m):
|
|
154
|
+
"""(c_gcoff values float32, its layout entry) for gcoff_hooks' models (None otherwise)."""
|
|
155
|
+
from nablaengine.model import gravcomp
|
|
156
|
+
off = gravcomp.offsets(m)
|
|
157
|
+
if off is None:
|
|
158
|
+
return None
|
|
159
|
+
return np.asarray(off, np.float32).ravel(), ("c_gcoff", "f", "nb", 3)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def constants(m, com_body=None):
|
|
163
|
+
"""Model arrays in const_layout(m) order (float32), and the tree's maximum depth; com_body: the name of the body
|
|
164
|
+
that takes the per-world COM offset (default COM_BODY: none)."""
|
|
165
|
+
nb = m.nbody
|
|
166
|
+
depth, children = [0] * nb, children_of(m)
|
|
167
|
+
for b in range(1, nb):
|
|
168
|
+
depth[b] = depth[m.body_parentid[b]] + 1
|
|
169
|
+
assert nb <= 128 and max(len(c) for c in children[1:]) <= MAX_CHILDREN
|
|
170
|
+
assert m.nmocap == 0, "no mocap bodies"
|
|
171
|
+
from nablaengine.forest_engine import tendon
|
|
172
|
+
assert not tendon.check(m), f"unsupported tendons: {tendon.check(m)[:4]}"
|
|
173
|
+
from nablaengine.forest_engine import equality
|
|
174
|
+
assert not equality.check(m), f"unsupported equality constraints: {equality.check(m)[:4]}"
|
|
175
|
+
assert not m.jnt_actgravcomp.any(), "gravcomp as a passive force (no actuatorgravcomp)"
|
|
176
|
+
qadr, dadr, axis, jtype, root = (np.zeros(nb) for _ in range(5))
|
|
177
|
+
axis = np.zeros((nb, 3))
|
|
178
|
+
kinds = {int(mujoco.mjtJoint.mjJNT_FREE): JFREE, int(mujoco.mjtJoint.mjJNT_HINGE): JHINGE,
|
|
179
|
+
int(mujoco.mjtJoint.mjJNT_SLIDE): JSLIDE}
|
|
180
|
+
dqadr = np.zeros(m.nv)
|
|
181
|
+
from nablaengine.forest_engine import model_hooks
|
|
182
|
+
model_hooks.integrator(m)
|
|
183
|
+
for b in range(1, nb):
|
|
184
|
+
j = m.body_jntadr[b]
|
|
185
|
+
assert m.body_jntnum[b] == 1, f"body {b}: exactly one joint per body"
|
|
186
|
+
jt = kinds[int(m.jnt_type[j])]
|
|
187
|
+
assert jt != JFREE or m.body_parentid[b] == 0, f"body {b}: a free joint on a tree root only"
|
|
188
|
+
assert jt == JFREE or not m.jnt_pos[j].any(), f"body {b}: the joint at the body origin"
|
|
189
|
+
assert jt == JFREE or m.qpos0[m.jnt_qposadr[j]] == 0, f"body {b}: joint ref 0 (the engine's angle is qpos)"
|
|
190
|
+
assert jt != JFREE or m.jnt_stiffness[j] == 0, f"body {b}: no free-joint spring"
|
|
191
|
+
qadr[b], dadr[b], axis[b], jtype[b] = m.jnt_qposadr[j], m.jnt_dofadr[j], m.jnt_axis[j], jt
|
|
192
|
+
if jt != JFREE:
|
|
193
|
+
dqadr[m.jnt_dofadr[j]] = m.jnt_qposadr[j]
|
|
194
|
+
k = b
|
|
195
|
+
while m.body_parentid[k] != 0:
|
|
196
|
+
k = m.body_parentid[k]
|
|
197
|
+
root[b] = k
|
|
198
|
+
child = np.zeros((nb, child_width(m)))
|
|
199
|
+
for b in range(1, nb):
|
|
200
|
+
child[b, :len(children[b])] = children[b]
|
|
201
|
+
nchild = np.array([len(children[b]) if b else 0 for b in range(nb)])
|
|
202
|
+
anc, nanc = np.zeros((nb, anc_width(m))), np.zeros(nb)
|
|
203
|
+
for b in range(1, nb):
|
|
204
|
+
k, lst = m.body_parentid[b], []
|
|
205
|
+
while k >= 1:
|
|
206
|
+
if jtype[k] != JFREE:
|
|
207
|
+
lst.append(k)
|
|
208
|
+
k = m.body_parentid[k]
|
|
209
|
+
assert len(lst) <= MAX_ANC
|
|
210
|
+
anc[b, :len(lst)], nanc[b] = lst, len(lst)
|
|
211
|
+
from nablaengine.forest_engine import muscle as _muscle
|
|
212
|
+
kp, kq, kv, frclim, afrc = np.zeros(m.nv), np.zeros(m.nv), np.zeros(m.nv), np.full(m.nv, 1e30), np.full(m.nv, 1e30)
|
|
213
|
+
act, ctrlr = np.full(m.nv, -1.0), np.tile([-1e30, 1e30], (m.nv, 1))
|
|
214
|
+
for a in range(m.nu):
|
|
215
|
+
j = m.actuator_trnid[a, 0]
|
|
216
|
+
if m.actuator_trntype[a] == mujoco.mjtTrn.mjTRN_TENDON:
|
|
217
|
+
continue
|
|
218
|
+
if _muscle.is_general(m, a):
|
|
219
|
+
continue
|
|
220
|
+
assert m.actuator_trntype[a] == mujoco.mjtTrn.mjTRN_JOINT, f"actuator {a}: joint / fixed-tendon transmissions"
|
|
221
|
+
assert int(m.jnt_type[j]) in (int(mujoco.mjtJoint.mjJNT_HINGE), int(mujoco.mjtJoint.mjJNT_SLIDE)), f"actuator {a}"
|
|
222
|
+
assert m.actuator_dyntype[a] == mujoco.mjtDyn.mjDYN_NONE and m.actuator_gaintype[a] == mujoco.mjtGain.mjGAIN_FIXED
|
|
223
|
+
assert int(m.actuator_biastype[a]) in (int(mujoco.mjtBias.mjBIAS_AFFINE), int(mujoco.mjtBias.mjBIAS_NONE))
|
|
224
|
+
assert m.actuator_gear[a, 0] == 1.0 and not m.actuator_gear[a, 1:].any()
|
|
225
|
+
dof = m.jnt_dofadr[j]
|
|
226
|
+
assert act[dof] < 0, f"actuator {a}: one actuator per dof"
|
|
227
|
+
bias = m.actuator_biasprm[a] if m.actuator_biastype[a] == mujoco.mjtBias.mjBIAS_AFFINE else np.zeros(10)
|
|
228
|
+
assert bias[0] == 0.0
|
|
229
|
+
act[dof], kp[dof], kq[dof], kv[dof] = a, m.actuator_gainprm[a, 0], -bias[1], -bias[2]
|
|
230
|
+
if m.actuator_ctrllimited[a]:
|
|
231
|
+
ctrlr[dof] = m.actuator_ctrlrange[a]
|
|
232
|
+
if m.actuator_forcelimited[a]:
|
|
233
|
+
afrc[dof] = m.actuator_forcerange[a, 1]
|
|
234
|
+
assert m.actuator_forcerange[a, 0] == -m.actuator_forcerange[a, 1]
|
|
235
|
+
if m.jnt_actfrclimited[j]:
|
|
236
|
+
frclim[dof] = m.jnt_actfrcrange[j, 1]
|
|
237
|
+
assert m.jnt_actfrcrange[j, 0] == -m.jnt_actfrcrange[j, 1]
|
|
238
|
+
com_body = com_body or COM_BODY
|
|
239
|
+
torso = mujoco.mj_name2id(m, mujoco.mjtObj.mjOBJ_BODY, com_body) if com_body else -1
|
|
240
|
+
assert com_body is None or torso >= 1, f'no body named "{com_body}": the per-world COM offset (com)'
|
|
241
|
+
from nablaengine.model import gravcomp as _gc
|
|
242
|
+
_off = _gc.offsets(m)
|
|
243
|
+
assert torso < 1 or _off is None or not _off[torso].any(), \
|
|
244
|
+
"the per-world COM offset's body has a gravcomp offset (fused bodies with different gravcomp)"
|
|
245
|
+
parts = [m.body_parentid, depth, qadr, dadr, nchild, child, m.body_pos, m.body_quat, axis, m.body_ipos,
|
|
246
|
+
m.body_iquat, m.body_mass, m.body_inertia, m.opt.gravity, m.dof_armature, nanc, anc, kp, kv, frclim,
|
|
247
|
+
[m.opt.timestep, torso], jtype, root, kq, afrc, act, ctrlr, dqadr, m.dof_damping, m.body_gravcomp]
|
|
248
|
+
return np.concatenate([np.asarray(x, np.float64).ravel() for x in parts]).astype(np.float32), max(depth)
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
def tree_header(m):
|
|
252
|
+
"""The kinematic trees' dof blocks (M is block diagonal over them): NTREE, MAXTD (the largest block), TREE_OF(t, o,
|
|
253
|
+
n) (block t: first dof o, n dofs), TREE_START(i) (the first dof of dof i's block) and TREE_ID(i) (its tree), for
|
|
254
|
+
chol_solve_trees and chol_solve_comps."""
|
|
255
|
+
adr, num = [int(x) for x in m.tree_dofadr], [int(x) for x in m.tree_dofnum]
|
|
256
|
+
of = " ".join(f"if ((t) == {t}) {{ (o) = {a}; (n) = {k}; }}" for t, (a, k) in enumerate(zip(adr, num)))
|
|
257
|
+
start = "0"
|
|
258
|
+
for a in adr[1:]:
|
|
259
|
+
start = f"((i) >= {a} ? {a} : {start})"
|
|
260
|
+
tid = "0"
|
|
261
|
+
for t, a in enumerate(adr[1:], 1):
|
|
262
|
+
tid = f"((i) >= {a} ? {t} : {tid})"
|
|
263
|
+
return (f"#define NTREE {len(adr)}\n#define MAXTD {max(num) if num else 0}\n#define TREE_ID(i) {tid}\n"
|
|
264
|
+
f"#define TREE_OF(t, o, n) do {{ (o) = 0; (n) = 0; {of} }} while (0)\n#define TREE_START(i) {start}\n")
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
def _body_set(bs, var="b"):
|
|
268
|
+
if not bs:
|
|
269
|
+
return "0"
|
|
270
|
+
if len(bs) <= 3:
|
|
271
|
+
return "(" + " || ".join(f"({var}) == {b}" for b in bs) + ")"
|
|
272
|
+
if max(bs) < 64:
|
|
273
|
+
return f"((int)((0x{sum(1 << b for b in bs):x}ULL >> ({var})) & 1ULL))"
|
|
274
|
+
lo, hi = sum(1 << b for b in bs if b < 64), sum(1 << (b - 64) for b in bs if b >= 64)
|
|
275
|
+
return (f"((int)(({var}) < 64 ? (0x{lo:x}ULL >> ({var})) & 1ULL : (0x{hi:x}ULL >> (({var}) - 64)) & 1ULL))")
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
def joint_header(m):
|
|
279
|
+
"""The model's joint types at compile time (the forest specialization): JT_OF(b), the joint type of body b >= 1
|
|
280
|
+
as a constant expression of b (JT_FREE_B / JT_SLIDE_B: the model's free / slide bodies, 0 when it has none), so the
|
|
281
|
+
branches on joint types the model does not have fold away; DQADR(i) / ACT_OF(i), the qpos address of hinge / slide dof
|
|
282
|
+
i and its joint actuator (-1: none), in closed form where the model's layout allows it (c_dqadr / c_act otherwise).
|
|
283
|
+
-DDK_FOREST_SPEC=0 reads all of them from the tables (the generic build, for bisection; the same bits)."""
|
|
284
|
+
jt = {int(mujoco.mjtJoint.mjJNT_FREE): JFREE, int(mujoco.mjtJoint.mjJNT_HINGE): JHINGE,
|
|
285
|
+
int(mujoco.mjtJoint.mjJNT_SLIDE): JSLIDE}
|
|
286
|
+
kind = [None] + [jt.get(int(m.jnt_type[m.body_jntadr[b]])) if m.body_jntnum[b] == 1 else None
|
|
287
|
+
for b in range(1, m.nbody)]
|
|
288
|
+
free = [b for b in range(1, m.nbody) if kind[b] == JFREE]
|
|
289
|
+
slide = [b for b in range(1, m.nbody) if kind[b] == JSLIDE]
|
|
290
|
+
fd = sorted(int(m.jnt_dofadr[m.body_jntadr[b]]) for b in free)
|
|
291
|
+
dq = {int(m.jnt_dofadr[j]): int(m.jnt_qposadr[j]) for j in range(m.njnt) if int(m.jnt_type[j]) != 0}
|
|
292
|
+
dqadr, free_dof = "(c_dqadr[i])", ""
|
|
293
|
+
if len(fd) <= 3 and all(q == i + sum(i >= f + 6 for f in fd) for i, q in dq.items()):
|
|
294
|
+
dqadr = "((i)" + "".join(f" + ((i) >= {f + 6})" for f in fd) + ")"
|
|
295
|
+
pred = " || ".join(f"((i) >= {f} && (i) < {f + 6})" for f in fd) or "0"
|
|
296
|
+
free_dof = f"#define JT_FREE_DOF(i) ({pred}) // (with the closed-form DQADR only)\n"
|
|
297
|
+
act = {}
|
|
298
|
+
from nablaengine.forest_engine import muscle
|
|
299
|
+
for a in range(m.nu):
|
|
300
|
+
if m.actuator_trntype[a] == mujoco.mjtTrn.mjTRN_JOINT and not muscle.is_general(m, a):
|
|
301
|
+
act[int(m.jnt_dofadr[m.actuator_trnid[a, 0]])] = a
|
|
302
|
+
act_of = "(c_act[i])"
|
|
303
|
+
if act:
|
|
304
|
+
d0, a0, n = min(act), act[min(act)], len(act)
|
|
305
|
+
if sorted(act) == list(range(d0, d0 + n)) and all(act[d] == d - d0 + a0 for d in act):
|
|
306
|
+
hi = "" if d0 + n == m.nv else f" && (i) < {d0 + n}"
|
|
307
|
+
act_of = f"((i) >= {d0}{hi} ? (i) - {d0 - a0} : -1)" if d0 or hi else f"((i) - {d0 - a0})"
|
|
308
|
+
else:
|
|
309
|
+
act_of = "(-1)"
|
|
310
|
+
return f"""#ifndef DK_FOREST_SPEC
|
|
311
|
+
#define DK_FOREST_SPEC 1 // the model's joint types and dof maps at compile time (source.joint_header); 0: the tables
|
|
312
|
+
#endif
|
|
313
|
+
#if DK_FOREST_SPEC
|
|
314
|
+
#define JT_FREE_B(b) {_body_set(free)}
|
|
315
|
+
#define JT_SLIDE_B(b) {_body_set(slide)}
|
|
316
|
+
#define JT_OF(b) (JT_FREE_B(b) ? JFREE : JT_SLIDE_B(b) ? JSLIDE : JHINGE)
|
|
317
|
+
#define DQADR(i) {dqadr}
|
|
318
|
+
{free_dof}#define ACT_OF(i) {act_of}
|
|
319
|
+
#else
|
|
320
|
+
#define JT_OF(b) (c_jtype[b])
|
|
321
|
+
#define DQADR(i) (c_dqadr[i])
|
|
322
|
+
#define ACT_OF(i) (c_act[i])
|
|
323
|
+
#endif
|
|
324
|
+
"""
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
def header(m, maxd, wpb):
|
|
328
|
+
return f"""
|
|
329
|
+
#include <cuda_runtime.h>
|
|
330
|
+
#define NB {m.nbody}
|
|
331
|
+
#define NQ {m.nq}
|
|
332
|
+
#define NV {m.nv}
|
|
333
|
+
#define NU {m.nu}
|
|
334
|
+
#define MAXD {maxd}
|
|
335
|
+
#define WPB {wpb}
|
|
336
|
+
#define NTRI (NV * (NV + 1) / 2)
|
|
337
|
+
#define NCH {child_width(m)}
|
|
338
|
+
#define TRI(i, j) ((i) * ((i) + 1) / 2 + (j)) // packed lower triangle, j <= i
|
|
339
|
+
""" + tree_header(m) + r"""
|
|
340
|
+
#ifndef DKC
|
|
341
|
+
#define DKC __device__ // where the model arrays live: global memory (L1-cached), as lanes index them by pair, body or
|
|
342
|
+
// dof (divergent addresses serialize in __constant__ memory); -DDKC=__constant__ for comparison
|
|
343
|
+
#endif
|
|
344
|
+
DKC int c_parent[NB], c_depth[NB], c_qadr[NB], c_dadr[NB], c_nchild[NB], c_child[NB * NCH], c_nanc[NB],
|
|
345
|
+
c_anc[NB * 12];
|
|
346
|
+
DKC float c_pos[NB * 3], c_quat[NB * 4], c_axis[NB * 3], c_ipos[NB * 3], c_iquat[NB * 4], c_mass[NB],
|
|
347
|
+
c_inert[NB * 3], c_grav[3], c_armature[NV], c_kp[NV], c_kv[NV], c_frclim[NV], c_misc[2];
|
|
348
|
+
DKC int c_jtype[NB], c_root[NB], c_act[NV], c_dqadr[NV]; // the forest's (CONST_LAYOUT)
|
|
349
|
+
DKC float c_kq[NV], c_afrc[NV], c_ctrlr[NV * 2], c_damp[NV], c_gcomp[NB];
|
|
350
|
+
#define JFREE 0
|
|
351
|
+
#define JHINGE 1
|
|
352
|
+
#define JSLIDE 2
|
|
353
|
+
""" + joint_header(m)
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
DEVICE = r"""
|
|
357
|
+
__device__ __forceinline__ void qmul(const float* a, const float* b, float* o) {
|
|
358
|
+
float w = a[0] * b[0] - a[1] * b[1] - a[2] * b[2] - a[3] * b[3];
|
|
359
|
+
float x = a[0] * b[1] + a[1] * b[0] + a[2] * b[3] - a[3] * b[2];
|
|
360
|
+
float y = a[0] * b[2] - a[1] * b[3] + a[2] * b[0] + a[3] * b[1];
|
|
361
|
+
float z = a[0] * b[3] + a[1] * b[2] - a[2] * b[1] + a[3] * b[0];
|
|
362
|
+
o[0] = w; o[1] = x; o[2] = y; o[3] = z;
|
|
363
|
+
}
|
|
364
|
+
__device__ __forceinline__ void qrot(const float* q, const float* v, float* o) { // o = q v q^-1
|
|
365
|
+
float tx = 2.f * (q[2] * v[2] - q[3] * v[1]), ty = 2.f * (q[3] * v[0] - q[1] * v[2]), tz = 2.f * (q[1] * v[1] - q[2] * v[0]);
|
|
366
|
+
o[0] = v[0] + q[0] * tx + q[2] * tz - q[3] * ty;
|
|
367
|
+
o[1] = v[1] + q[0] * ty + q[3] * tx - q[1] * tz;
|
|
368
|
+
o[2] = v[2] + q[0] * tz + q[1] * ty - q[2] * tx;
|
|
369
|
+
}
|
|
370
|
+
__device__ __forceinline__ void qrot_inv(const float* q, const float* v, float* o) {
|
|
371
|
+
float qc[4] = {q[0], -q[1], -q[2], -q[3]};
|
|
372
|
+
qrot(qc, v, o);
|
|
373
|
+
}
|
|
374
|
+
__device__ __forceinline__ void cross(const float* a, const float* b, float* o) {
|
|
375
|
+
o[0] = a[1] * b[2] - a[2] * b[1]; o[1] = a[2] * b[0] - a[0] * b[2]; o[2] = a[0] * b[1] - a[1] * b[0];
|
|
376
|
+
}
|
|
377
|
+
__device__ __forceinline__ float dot3(const float* a, const float* b) { return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; }
|
|
378
|
+
|
|
379
|
+
// Explicit roundings. The adjoint replays the forward's body poses, collision-geom poses and contact geometry (dist,
|
|
380
|
+
// pos, frame) and needs them bitwise (adjoint.py adj_poses / adj_contacts), in another kernel and, in the JAX library,
|
|
381
|
+
// another compilation unit. Written as plain C (qmul, qrot, ... above), the mul / add pairs are fused into FMAs by NVVM
|
|
382
|
+
// and ptxas depending on the surrounding code (the uses of a product, register pressure, -fmad, the architecture), so
|
|
383
|
+
// the same source can give results an ulp apart in two kernels. These intrinsics are never contracted (and __fmaf_rn
|
|
384
|
+
// is never split): an expression written with them has its IEEE value, the same in every kernel, compilation unit and
|
|
385
|
+
// -fmad setting (on one GPU: rsqrtf and sincosf are the architecture's; IEEE division and sqrtf, nvcc's default
|
|
386
|
+
// -prec-div / -prec-sqrt, are correctly rounded and need nothing). Every operation the replay reproduces is written
|
|
387
|
+
// with them: the *_rn helpers here and fwd_body (the poses, and with them the twists and accelerations), step.py's
|
|
388
|
+
// geom_pose, the contact geometry that collide_pair shares with the adjoint's adj_contact_geom (make_frame,
|
|
389
|
+
// sphere_sphere, plane_point, plane_capsule_frame, capsule_end, segment_point, capcap_*), the bounding tests, the limit
|
|
390
|
+
// test (limit_dists) and the rows' impedance (kbimp, imp_raw, contact_invw, efc_D). The forms are the ones ptxas chose
|
|
391
|
+
// for the plain C on sm_70, so the change left the V100 results bitwise as they were: a b + c d = fma(a, b, c d),
|
|
392
|
+
// a b - c d = fma(a, b, -(c d)), x +- a b = fma(+-a, b, x), sums from the left; a product with a second use is rounded
|
|
393
|
+
// first (W = Wp + round(qd aw) in fwd_body, p +- round(l a) for the two ends of a capsule). The rest of the engine
|
|
394
|
+
// (dynamics, solver, the adjoint's VJPs) keeps the plain C helpers above: it is not replayed. Test: the forward or the
|
|
395
|
+
// adjoint unit built with -fmad=false: the replay stays bitwise the forward's
|
|
396
|
+
#define RMUL(a, b) __fmul_rn((a), (b))
|
|
397
|
+
#define RADD(a, b) __fadd_rn((a), (b))
|
|
398
|
+
#define RSUB(a, b) __fsub_rn((a), (b))
|
|
399
|
+
#define RFMA(a, b, c) __fmaf_rn((a), (b), (c)) // a b + c, one rounding
|
|
400
|
+
#define RFMS(a, b, c) __fmaf_rn(-(a), (b), (c)) // c - a b, one rounding
|
|
401
|
+
|
|
402
|
+
__device__ __forceinline__ void qmul_rn(const float* a, const float* b, float* o) {
|
|
403
|
+
float w = RFMS(a[3], b[3], RFMS(a[2], b[2], RFMA(a[0], b[0], -RMUL(a[1], b[1]))));
|
|
404
|
+
float x = RFMS(a[3], b[2], RFMA(a[2], b[3], RFMA(a[0], b[1], RMUL(a[1], b[0]))));
|
|
405
|
+
float y = RFMA(a[3], b[1], RFMA(a[2], b[0], RFMA(a[0], b[2], -RMUL(a[1], b[3]))));
|
|
406
|
+
float z = RFMA(a[3], b[0], RFMS(a[2], b[1], RFMA(a[0], b[3], RMUL(a[1], b[2]))));
|
|
407
|
+
o[0] = w; o[1] = x; o[2] = y; o[3] = z;
|
|
408
|
+
}
|
|
409
|
+
// o = q v q^-1: t = 2 qv x v, o = v + q0 t + qv x t
|
|
410
|
+
__device__ __forceinline__ void qrot_rn(const float* q, const float* v, float* o) {
|
|
411
|
+
float tx = RMUL(2.f, RFMA(q[2], v[2], -RMUL(q[3], v[1])));
|
|
412
|
+
float ty = RMUL(2.f, RFMA(q[3], v[0], -RMUL(q[1], v[2])));
|
|
413
|
+
float tz = RMUL(2.f, RFMA(q[1], v[1], -RMUL(q[2], v[0])));
|
|
414
|
+
o[0] = RFMS(q[3], ty, RFMA(q[2], tz, RFMA(q[0], tx, v[0])));
|
|
415
|
+
o[1] = RFMS(q[1], tz, RFMA(q[3], tx, RFMA(q[0], ty, v[1])));
|
|
416
|
+
o[2] = RFMS(q[2], tx, RFMA(q[1], ty, RFMA(q[0], tz, v[2])));
|
|
417
|
+
}
|
|
418
|
+
__device__ __forceinline__ void cross_rn(const float* a, const float* b, float* o) {
|
|
419
|
+
o[0] = RFMA(a[1], b[2], -RMUL(a[2], b[1]));
|
|
420
|
+
o[1] = RFMA(a[2], b[0], -RMUL(a[0], b[2]));
|
|
421
|
+
o[2] = RFMA(a[0], b[1], -RMUL(a[1], b[0]));
|
|
422
|
+
}
|
|
423
|
+
__device__ __forceinline__ float dot3_rn(const float* a, const float* b) {
|
|
424
|
+
return RFMA(a[2], b[2], RFMA(a[0], b[0], RMUL(a[1], b[1])));
|
|
425
|
+
}
|
|
426
|
+
__device__ __forceinline__ void zaxis_rn(const float* q, float* a) { // a = q e_z: a plane's normal, a capsule's axis
|
|
427
|
+
const float ez[3] = {0.f, 0.f, 1.f};
|
|
428
|
+
qrot_rn(q, ez, a);
|
|
429
|
+
}
|
|
430
|
+
__device__ __forceinline__ float warp_sum(float v) {
|
|
431
|
+
for (int o = 16; o > 0; o >>= 1) v += __shfl_xor_sync(0xffffffffu, v, o);
|
|
432
|
+
return v;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
// fwd_body's three steps for a hinge body (b >= 2), in its order: the origin X and the parent-composed rotation q0 from
|
|
436
|
+
// the parent's pose and the body's constant frame (qb = c_quat + 4 b, pb = c_pos + 3 b); the hinge rotation ha at the
|
|
437
|
+
// joint angle (axis ax = c_axis + 3 b), which alone does not depend on the parent; the rest (Q = q0 ha, the twist, the
|
|
438
|
+
// accelerations). Split so that a sweep can compute fwd_hinge_rot (and load the constants) for every body before it
|
|
439
|
+
// starts (g1_engine.kinematics: the kinematics outputs, the same bits as fwd_body); fwd_body runs them as one
|
|
440
|
+
__device__ __forceinline__ void fwd_hinge_pos(const float* Qp, const float* Xp, const float* qb, const float* pb,
|
|
441
|
+
float* q0, float* X) {
|
|
442
|
+
float t[3];
|
|
443
|
+
qmul_rn(Qp, qb, q0);
|
|
444
|
+
qrot_rn(Qp, pb, t);
|
|
445
|
+
for (int k = 0; k < 3; k++) X[k] = RADD(Xp[k], t[k]);
|
|
446
|
+
}
|
|
447
|
+
__device__ __forceinline__ void fwd_hinge_rot(float ang, const float* ax, float* ha) {
|
|
448
|
+
float s, c;
|
|
449
|
+
sincosf(RMUL(0.5f, ang), &s, &c); // bitwise sinf, cosf with one argument reduction (V100: 4% faster forward)
|
|
450
|
+
ha[0] = c; ha[1] = RMUL(s, ax[0]); ha[2] = RMUL(s, ax[1]); ha[3] = RMUL(s, ax[2]);
|
|
451
|
+
}
|
|
452
|
+
__device__ __forceinline__ void fwd_hinge_rest(const float* q0, const float* ha, const float* ax, float qd, float qdd,
|
|
453
|
+
const float* Xp, const float* Wp, const float* Vp, const float* ALp,
|
|
454
|
+
const float* Ap, float* Q, const float* X, float* W, float* V,
|
|
455
|
+
float* AL, float* A) {
|
|
456
|
+
qmul_rn(q0, ha, Q);
|
|
457
|
+
float aw[3], awd[3], r[3], u[3], u2[3], wr[3];
|
|
458
|
+
qrot_rn(Q, ax, aw);
|
|
459
|
+
for (int k = 0; k < 3; k++) { r[k] = RSUB(X[k], Xp[k]); awd[k] = RMUL(aw[k], qd); W[k] = RADD(Wp[k], awd[k]); }
|
|
460
|
+
cross_rn(Wp, r, u);
|
|
461
|
+
for (int k = 0; k < 3; k++) V[k] = RADD(Vp[k], u[k]);
|
|
462
|
+
cross_rn(Wp, awd, u);
|
|
463
|
+
for (int k = 0; k < 3; k++) AL[k] = RADD(RFMA(aw[k], qdd, ALp[k]), u[k]);
|
|
464
|
+
cross_rn(ALp, r, u);
|
|
465
|
+
cross_rn(Wp, r, wr);
|
|
466
|
+
cross_rn(Wp, wr, u2);
|
|
467
|
+
for (int k = 0; k < 3; k++) A[k] = RADD(RADD(Ap[k], u[k]), u2[k]);
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
// forward kinematics and velocity / acceleration propagation of body b from its parent's state. Explicit roundings
|
|
471
|
+
// (RMUL ...): the forward (substep_pre, with qacc = 0) and the adjoint (adj_poses, at a*) compute the same bits
|
|
472
|
+
__device__ __forceinline__ void fwd_body(int b, const float* Qp, const float* Xp, const float* Wp, const float* Vp,
|
|
473
|
+
const float* ALp, const float* Ap, const float* qpos, const float* qvel,
|
|
474
|
+
const float* qacc, float* Q, float* X, float* W, float* V, float* AL,
|
|
475
|
+
float* A) {
|
|
476
|
+
const int jt = JT_OF(b);
|
|
477
|
+
if (jt == JFREE) { // free root: world position, quaternion, world linear / local angular velocity
|
|
478
|
+
const float* qp = qpos + c_qadr[b];
|
|
479
|
+
const float* qv = qvel + c_dadr[b];
|
|
480
|
+
const float* qa = qacc + c_dadr[b];
|
|
481
|
+
float n = rsqrtf(RFMA(qp[6], qp[6], RFMA(qp[5], qp[5], RFMA(qp[3], qp[3], RMUL(qp[4], qp[4])))));
|
|
482
|
+
for (int k = 0; k < 4; k++) Q[k] = RMUL(qp[3 + k], n);
|
|
483
|
+
for (int k = 0; k < 3; k++) { X[k] = qp[k]; V[k] = qv[k]; A[k] = RSUB(qa[k], c_grav[k]); }
|
|
484
|
+
qrot_rn(Q, qv + 3, W);
|
|
485
|
+
qrot_rn(Q, qa + 3, AL);
|
|
486
|
+
return;
|
|
487
|
+
}
|
|
488
|
+
float q0[4];
|
|
489
|
+
fwd_hinge_pos(Qp, Xp, c_quat + 4 * b, c_pos + 3 * b, q0, X);
|
|
490
|
+
float qj = qpos[c_qadr[b]], qd = qvel[c_dadr[b]], qdd = qacc[c_dadr[b]];
|
|
491
|
+
if (jt == JSLIDE) { // slide: X += R ax q; W = Wp, AL = ALp; V, A of the moving origin
|
|
492
|
+
float aw[3], r[3], u[3], u2[3], wr[3], awd[3], c2[3];
|
|
493
|
+
for (int k = 0; k < 4; k++) Q[k] = q0[k];
|
|
494
|
+
qrot_rn(Q, c_axis + 3 * b, aw);
|
|
495
|
+
for (int k = 0; k < 3; k++) { X[k] = RFMA(aw[k], qj, X[k]); r[k] = RSUB(X[k], Xp[k]); awd[k] = RMUL(aw[k], qd); }
|
|
496
|
+
for (int k = 0; k < 3; k++) { W[k] = Wp[k]; AL[k] = ALp[k]; }
|
|
497
|
+
cross_rn(Wp, r, u);
|
|
498
|
+
for (int k = 0; k < 3; k++) V[k] = RADD(RADD(Vp[k], u[k]), awd[k]);
|
|
499
|
+
cross_rn(ALp, r, u);
|
|
500
|
+
cross_rn(Wp, r, wr);
|
|
501
|
+
cross_rn(Wp, wr, u2);
|
|
502
|
+
cross_rn(Wp, awd, c2);
|
|
503
|
+
for (int k = 0; k < 3; k++)
|
|
504
|
+
A[k] = RFMA(aw[k], qdd, RADD(RADD(RADD(Ap[k], u[k]), u2[k]), RMUL(2.f, c2[k])));
|
|
505
|
+
return;
|
|
506
|
+
}
|
|
507
|
+
float ha[4];
|
|
508
|
+
fwd_hinge_rot(qj, c_axis + 3 * b, ha);
|
|
509
|
+
fwd_hinge_rest(q0, ha, c_axis + 3 * b, qd, qdd, Xp, Wp, Vp, ALp, Ap, Q, X, W, V, AL, A);
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
// force F and moment N about the body origin that body b alone needs (Newton-Euler at its COM)
|
|
513
|
+
__device__ __forceinline__ void body_force(int b, const float* Q, const float* W, const float* AL, const float* A,
|
|
514
|
+
const float* ipos, float* F, float* N) {
|
|
515
|
+
float cvec[3], u[3], u2[3], wc[3];
|
|
516
|
+
qrot(Q, ipos, cvec);
|
|
517
|
+
cross(AL, cvec, u);
|
|
518
|
+
cross(W, cvec, wc);
|
|
519
|
+
cross(W, wc, u2);
|
|
520
|
+
float m = c_mass[b], gc = c_gcomp[b]; // gravcomp: the force m (a - g) + gc m g (A holds a - g)
|
|
521
|
+
for (int k = 0; k < 3; k++) F[k] = m * (A[k] + u[k] + u2[k] + gc * c_grav[k]);
|
|
522
|
+
float qi[4], l[3], Iw[3], Ia[3], cf[3];
|
|
523
|
+
qmul(Q, c_iquat + 4 * b, qi);
|
|
524
|
+
qrot_inv(qi, W, l);
|
|
525
|
+
for (int k = 0; k < 3; k++) l[k] *= c_inert[3 * b + k];
|
|
526
|
+
qrot(qi, l, Iw);
|
|
527
|
+
qrot_inv(qi, AL, l);
|
|
528
|
+
for (int k = 0; k < 3; k++) l[k] *= c_inert[3 * b + k];
|
|
529
|
+
qrot(qi, l, Ia);
|
|
530
|
+
cross(W, Iw, u);
|
|
531
|
+
cross(cvec, F, cf);
|
|
532
|
+
for (int k = 0; k < 3; k++) N[k] = Ia[k] + u[k] + cf[k];
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
__device__ __forceinline__ void accumulate(const float* Xb, const float* Xc, const float* Fc, const float* Nc, float* F,
|
|
536
|
+
float* N) {
|
|
537
|
+
float r[3] = {Xc[0] - Xb[0], Xc[1] - Xb[1], Xc[2] - Xb[2]}, u[3];
|
|
538
|
+
cross(r, Fc, u);
|
|
539
|
+
for (int k = 0; k < 3; k++) { F[k] += Fc[k]; N[k] += Nc[k] + u[k]; }
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
__device__ __forceinline__ void torques(int b, const float* Q, const float* F, const float* N, float* qfrc) {
|
|
543
|
+
const int jt = JT_OF(b);
|
|
544
|
+
if (jt == JFREE) {
|
|
545
|
+
float l[3];
|
|
546
|
+
qrot_inv(Q, N, l);
|
|
547
|
+
float* o = qfrc + c_dadr[b];
|
|
548
|
+
for (int k = 0; k < 3; k++) { o[k] = F[k]; o[3 + k] = l[k]; }
|
|
549
|
+
} else {
|
|
550
|
+
float aw[3];
|
|
551
|
+
qrot(Q, c_axis + 3 * b, aw);
|
|
552
|
+
qfrc[c_dadr[b]] = dot3(aw, jt == JSLIDE ? F : N);
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
// spatial inertia of body b about the point O, packed as J (xx yy zz xy xz yz), h = m c (3), m; c = com - O
|
|
557
|
+
__device__ __forceinline__ void body_inertia(int b, const float* Q, const float* X, const float* O, const float* ipos,
|
|
558
|
+
float* J) {
|
|
559
|
+
float qi[4], R[9], c[3], t[3];
|
|
560
|
+
qmul(Q, c_iquat + 4 * b, qi);
|
|
561
|
+
for (int k = 0; k < 3; k++) {
|
|
562
|
+
float e[3] = {0.f, 0.f, 0.f};
|
|
563
|
+
e[k] = 1.f;
|
|
564
|
+
qrot(qi, e, R + 3 * k); // column k of the inertia frame
|
|
565
|
+
}
|
|
566
|
+
const float* I = c_inert + 3 * b;
|
|
567
|
+
J[0] = I[0] * R[0] * R[0] + I[1] * R[3] * R[3] + I[2] * R[6] * R[6];
|
|
568
|
+
J[1] = I[0] * R[1] * R[1] + I[1] * R[4] * R[4] + I[2] * R[7] * R[7];
|
|
569
|
+
J[2] = I[0] * R[2] * R[2] + I[1] * R[5] * R[5] + I[2] * R[8] * R[8];
|
|
570
|
+
J[3] = I[0] * R[0] * R[1] + I[1] * R[3] * R[4] + I[2] * R[6] * R[7];
|
|
571
|
+
J[4] = I[0] * R[0] * R[2] + I[1] * R[3] * R[5] + I[2] * R[6] * R[8];
|
|
572
|
+
J[5] = I[0] * R[1] * R[2] + I[1] * R[4] * R[5] + I[2] * R[7] * R[8];
|
|
573
|
+
qrot(Q, ipos, t);
|
|
574
|
+
for (int k = 0; k < 3; k++) c[k] = X[k] + t[k] - O[k];
|
|
575
|
+
float m = c_mass[b], cc = dot3(c, c);
|
|
576
|
+
J[0] += m * (cc - c[0] * c[0]); J[1] += m * (cc - c[1] * c[1]); J[2] += m * (cc - c[2] * c[2]);
|
|
577
|
+
J[3] -= m * c[0] * c[1]; J[4] -= m * c[0] * c[2]; J[5] -= m * c[1] * c[2];
|
|
578
|
+
J[6] = m * c[0]; J[7] = m * c[1]; J[8] = m * c[2]; J[9] = m;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
// (n, f) = I (w, v) for a packed spatial inertia about O; n = J w + h x v, f = m v - h x w
|
|
582
|
+
__device__ __forceinline__ void apply_inertia(const float* J, const float* w, const float* v, float* n, float* f) {
|
|
583
|
+
float hv[3], hw[3];
|
|
584
|
+
cross(J + 6, v, hv);
|
|
585
|
+
cross(J + 6, w, hw);
|
|
586
|
+
n[0] = J[0] * w[0] + J[3] * w[1] + J[4] * w[2] + hv[0];
|
|
587
|
+
n[1] = J[3] * w[0] + J[1] * w[1] + J[5] * w[2] + hv[1];
|
|
588
|
+
n[2] = J[4] * w[0] + J[5] * w[1] + J[2] * w[2] + hv[2];
|
|
589
|
+
for (int k = 0; k < 3; k++) f[k] = J[9] * v[k] - hw[k];
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
// motion subspace of dof t of body k about O: w (angular), v (velocity of the point O); a free body's O is its origin
|
|
593
|
+
__device__ __forceinline__ void dof_motion(int k, int t, const float* Q, const float* X, const float* O, float* w,
|
|
594
|
+
float* v) {
|
|
595
|
+
if (JT_OF(k) == JSLIDE) {
|
|
596
|
+
qrot(Q, c_axis + 3 * k, v);
|
|
597
|
+
w[0] = w[1] = w[2] = 0.f;
|
|
598
|
+
return;
|
|
599
|
+
}
|
|
600
|
+
if (JT_OF(k) == JFREE) {
|
|
601
|
+
float e[3] = {0.f, 0.f, 0.f};
|
|
602
|
+
if (t < 3) { e[t] = 1.f; w[0] = w[1] = w[2] = 0.f; v[0] = e[0]; v[1] = e[1]; v[2] = e[2]; return; }
|
|
603
|
+
e[t - 3] = 1.f;
|
|
604
|
+
qrot(Q, e, w); // local angular axes about the root origin = O
|
|
605
|
+
v[0] = v[1] = v[2] = 0.f;
|
|
606
|
+
return;
|
|
607
|
+
}
|
|
608
|
+
float r[3] = {O[0] - X[0], O[1] - X[1], O[2] - X[2]};
|
|
609
|
+
qrot(Q, c_axis + 3 * k, w);
|
|
610
|
+
cross(w, r, v);
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
// COM offset of body b in its frame, with the per-world torso offset (domain randomization) applied
|
|
614
|
+
__device__ __forceinline__ void body_ipos(int b, const float* com, float* out) {
|
|
615
|
+
for (int k = 0; k < 3; k++) out[k] = c_ipos[3 * b + k] + (b == (int)c_misc[1] ? com[k] : 0.f);
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
// in-place Cholesky A = L L^T (lower triangle of a dense NV x NV shared matrix) by one warp, left-looking (Crout):
|
|
619
|
+
// column k from the finished columns to its left; lane l computes rows k+1+l, k+33+l, ... as read-only dot products
|
|
620
|
+
// (row stride NV = 35 keeps the lanes on distinct banks; row k is a broadcast)
|
|
621
|
+
__device__ __forceinline__ void chol_warp(float* A, int lane) {
|
|
622
|
+
for (int k = 0; k < NV; k++) {
|
|
623
|
+
const float* Ak = A + k * NV;
|
|
624
|
+
float part = 0.f;
|
|
625
|
+
for (int j = lane; j < k; j += 32) part += Ak[j] * Ak[j];
|
|
626
|
+
float d = sqrtf(fmaxf(Ak[k] - warp_sum(part), 1e-30f)), inv = 1.f / d;
|
|
627
|
+
for (int i = k + 1 + lane; i < NV; i += 32) {
|
|
628
|
+
const float* Ai = A + i * NV;
|
|
629
|
+
float a0 = Ai[k], a1 = 0.f, a2 = 0.f, a3 = 0.f;
|
|
630
|
+
int j = 0;
|
|
631
|
+
for (; j + 4 <= k; j += 4) {
|
|
632
|
+
a0 -= Ai[j] * Ak[j]; a1 -= Ai[j + 1] * Ak[j + 1]; a2 -= Ai[j + 2] * Ak[j + 2]; a3 -= Ai[j + 3] * Ak[j + 3];
|
|
633
|
+
}
|
|
634
|
+
for (; j < k; j++) a0 -= Ai[j] * Ak[j];
|
|
635
|
+
A[i * NV + k] = ((a0 + a1) + (a2 + a3)) * inv;
|
|
636
|
+
}
|
|
637
|
+
__syncwarp();
|
|
638
|
+
if (lane == 0) A[k * NV + k] = d;
|
|
639
|
+
__syncwarp();
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
// Cholesky factorization and solve in registers: L L^T = A (A: packed lower triangle TRI(i, j) in shared memory, read
|
|
644
|
+
// before anything is written, so L may alias A), then x = A^-1 b in place (b: shared NV vector). Lane l holds rows l and
|
|
645
|
+
// l + 32 (NV <= 64) in registers; the factorization is right-looking with column k broadcast by __shfl_sync, all loops
|
|
646
|
+
// unrolled at compile time. L (packed lower triangle, with the true diagonal) is written to L for the back substitution
|
|
647
|
+
// and for callers that keep it.
|
|
648
|
+
#ifndef CHOL_INLINE
|
|
649
|
+
#define CHOL_INLINE __noinline__ // one copy of the (large, fully unrolled) code: -DCHOL_INLINE=__forceinline__ inlines it
|
|
650
|
+
#endif
|
|
651
|
+
__device__ CHOL_INLINE void chol_solve_reg(const float* A, float* L, float* b, int lane) {
|
|
652
|
+
static_assert(NV <= 64, "two rows per lane");
|
|
653
|
+
const int r0 = lane, r1 = lane + 32;
|
|
654
|
+
const bool has1 = r1 < NV;
|
|
655
|
+
float a0[NV], a1[NV], dinv[NV];
|
|
656
|
+
#pragma unroll
|
|
657
|
+
for (int j = 0; j < NV; j++) { a0[j] = j <= r0 ? A[TRI(r0, j)] : 0.f; a1[j] = has1 && j <= r1 ? A[TRI(r1, j)] : 0.f; }
|
|
658
|
+
__syncwarp();
|
|
659
|
+
#pragma unroll
|
|
660
|
+
for (int k = 0; k < NV; k++) {
|
|
661
|
+
float dkk = fmaxf(__shfl_sync(0xffffffffu, k < 32 ? a0[k] : a1[k], k & 31), 1e-30f);
|
|
662
|
+
float inv = rsqrtf(dkk);
|
|
663
|
+
inv = inv * (1.5f - 0.5f * dkk * inv * inv); // one Newton step: about full float precision
|
|
664
|
+
float d = dkk * inv;
|
|
665
|
+
dinv[k] = inv;
|
|
666
|
+
if (r0 > k) a0[k] *= inv; else if (r0 == k) a0[k] = d;
|
|
667
|
+
if (r1 > k) a1[k] *= inv; else if (r1 == k) a1[k] = d;
|
|
668
|
+
#pragma unroll
|
|
669
|
+
for (int j = k + 1; j < NV; j++) { // unpredicated: above the diagonal this updates the symmetric Schur complement
|
|
670
|
+
float ljk = __shfl_sync(0xffffffffu, j < 32 ? a0[k] : a1[k], j & 31); // (bounded, never read as L)
|
|
671
|
+
a0[j] -= a0[k] * ljk;
|
|
672
|
+
a1[j] -= a1[k] * ljk;
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
#pragma unroll
|
|
676
|
+
for (int j = 0; j < NV; j++) { if (j <= r0) L[TRI(r0, j)] = a0[j]; if (has1 && j <= r1) L[TRI(r1, j)] = a1[j]; }
|
|
677
|
+
float y0 = b[r0], y1 = has1 ? b[r1] : 0.f;
|
|
678
|
+
#pragma unroll
|
|
679
|
+
for (int k = 0; k < NV; k++) { // forward: L y = b, column-wise
|
|
680
|
+
float yk = __shfl_sync(0xffffffffu, k < 32 ? y0 : y1, k & 31) * dinv[k];
|
|
681
|
+
if (r0 == k) y0 = yk; else if (r0 > k) y0 -= a0[k] * yk;
|
|
682
|
+
if (r1 == k) y1 = yk; else if (r1 > k) y1 -= a1[k] * yk;
|
|
683
|
+
}
|
|
684
|
+
__syncwarp();
|
|
685
|
+
// backward: L^T x = y; lane i holds y_i and reads row k of L (contiguous) to update its entries i < k
|
|
686
|
+
#pragma unroll
|
|
687
|
+
for (int k = NV - 1; k >= 0; k--) {
|
|
688
|
+
float xk = __shfl_sync(0xffffffffu, k < 32 ? y0 : y1, k & 31) * dinv[k];
|
|
689
|
+
if (r0 == k) y0 = xk; else if (r0 < k) y0 -= L[TRI(k, r0)] * xk;
|
|
690
|
+
if (r1 == k) y1 = xk; else if (r1 < k) y1 -= L[TRI(k, r1)] * xk;
|
|
691
|
+
}
|
|
692
|
+
b[r0] = y0;
|
|
693
|
+
if (has1) b[r1] = y1;
|
|
694
|
+
__syncwarp();
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
// Compact variant of chol_solve_reg (same contract): the column loop is not unrolled. Lane registers a0 / a1 hold the
|
|
698
|
+
// trailing part of rows lane / lane + 32, shifted left by one entry per step (a0[m] = row(k + m) at step k), so every
|
|
699
|
+
// register index stays a compile-time constant; finished columns of L go to shared memory as they are produced. The
|
|
700
|
+
// trailing update runs in groups of 8 columns and stops after the last group that holds a column. Small code (no
|
|
701
|
+
// instruction-fetch stalls) at the price of more executed instructions than the fully unrolled version.
|
|
702
|
+
__device__ __noinline__ void chol_solve_compact(const float* A, float* L, float* b, int lane) {
|
|
703
|
+
static_assert(NV <= 64, "two rows per lane");
|
|
704
|
+
const int r0 = lane, r1 = lane + 32;
|
|
705
|
+
const bool has1 = r1 < NV;
|
|
706
|
+
float a0[NV], a1[NV];
|
|
707
|
+
#pragma unroll
|
|
708
|
+
for (int j = 0; j < NV; j++) { a0[j] = j <= r0 ? A[TRI(r0, j)] : 0.f; a1[j] = has1 && j <= r1 ? A[TRI(r1, j)] : 0.f; }
|
|
709
|
+
__syncwarp();
|
|
710
|
+
for (int k = 0; k < NV; k++) {
|
|
711
|
+
float dkk = fmaxf(__shfl_sync(0xffffffffu, k < 32 ? a0[0] : a1[0], k & 31), 1e-30f);
|
|
712
|
+
float inv = rsqrtf(dkk);
|
|
713
|
+
inv = inv * (1.5f - 0.5f * dkk * inv * inv);
|
|
714
|
+
float d = dkk * inv;
|
|
715
|
+
float l0 = r0 > k ? a0[0] * inv : (r0 == k ? d : 0.f), l1 = r1 > k ? a1[0] * inv : (r1 == k ? d : 0.f);
|
|
716
|
+
if (r0 >= k) L[TRI(r0, k)] = l0;
|
|
717
|
+
if (has1 && r1 >= k) L[TRI(r1, k)] = l1;
|
|
718
|
+
if (r0 == k) l0 = 0.f; // the pivot row takes no part in the trailing update
|
|
719
|
+
if (r1 == k) l1 = 0.f;
|
|
720
|
+
#pragma unroll
|
|
721
|
+
for (int g = 1; g < NV; g += 8) {
|
|
722
|
+
if (k + g >= NV) break; // uniform: no column left in this group
|
|
723
|
+
#pragma unroll
|
|
724
|
+
for (int m = g; m < g + 8 && m < NV; m++) {
|
|
725
|
+
int j = k + m;
|
|
726
|
+
float ljk = __shfl_sync(0xffffffffu, j < 32 ? l0 : l1, j & 31);
|
|
727
|
+
a0[m - 1] = a0[m] - l0 * ljk;
|
|
728
|
+
a1[m - 1] = a1[m] - l1 * ljk;
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
__syncwarp();
|
|
733
|
+
float y0 = b[r0], y1 = has1 ? b[r1] : 0.f;
|
|
734
|
+
for (int k = 0; k < NV; k++) { // forward: L y = b, column-wise
|
|
735
|
+
float yk = __shfl_sync(0xffffffffu, k < 32 ? y0 : y1, k & 31) / L[TRI(k, k)];
|
|
736
|
+
if (r0 == k) y0 = yk; else if (r0 > k) y0 -= L[TRI(r0, k)] * yk;
|
|
737
|
+
if (r1 == k) y1 = yk; else if (has1 && r1 > k) y1 -= L[TRI(r1, k)] * yk;
|
|
738
|
+
}
|
|
739
|
+
for (int k = NV - 1; k >= 0; k--) { // backward: L^T x = y
|
|
740
|
+
float xk = __shfl_sync(0xffffffffu, k < 32 ? y0 : y1, k & 31) / L[TRI(k, k)];
|
|
741
|
+
if (r0 == k) y0 = xk; else if (r0 < k) y0 -= L[TRI(k, r0)] * xk;
|
|
742
|
+
if (r1 == k) y1 = xk; else if (has1 && r1 < k) y1 -= L[TRI(k, r1)] * xk;
|
|
743
|
+
}
|
|
744
|
+
b[r0] = y0;
|
|
745
|
+
if (has1) b[r1] = y1;
|
|
746
|
+
__syncwarp();
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
// Broadcast variant (same contract, plus cb: 72 floats of 16-byte aligned shared scratch), for 32 < NV <= 35. The
|
|
750
|
+
// shuffles of the column broadcast (one per entry of the trailing update in chol_solve_reg / _compact) are the cost of
|
|
751
|
+
// those two; here column k goes through shared memory once (one store per lane) and is read back as float4 broadcasts,
|
|
752
|
+
// four entries per load. Rows 0..31 are in registers (lane r: row r, shifted left by one entry per step as in
|
|
753
|
+
// chol_solve_compact); the NT = NV - 32 tail rows are held transposed (lane j: A[32 + t][j]) and their own NT x NT
|
|
754
|
+
// block redundantly on every lane, so a lane updates its row's entries left of column 32 only. The forward solve runs
|
|
755
|
+
// inside the factorization (lane k broadcasts y_k with column k), and the pivot chain does not wait for the broadcast:
|
|
756
|
+
// lane k + 1 forms its next diagonal from its own entries. The backward solve reads L back by rows (one load per step).
|
|
757
|
+
// L is the same factor as chol_solve_compact's (same update order), the solve multiplies by the refined reciprocal
|
|
758
|
+
// pivots.
|
|
759
|
+
// Cycles per factor + solve of a 35 x 35 matrix, 4096 worlds (a standalone microbenchmark): V100 at 3 per SM 8.2k
|
|
760
|
+
// (chol_solve_compact 31.6k, _reg 16.2k), RTX 3090 at 2 per SM 8.9k (51.9k, 19.3k); in the engine (V100) the
|
|
761
|
+
// qacc_smooth phase went from 44.8k to 10.8k cycles.
|
|
762
|
+
// CHOL_BCAST_WIDE: chol_solve_bcast_wide (below) for 36 <= NV <= 48; with it chol_back32 / chol_resolve (NEWTON_REUSE_WIDE)
|
|
763
|
+
#ifndef CHOL_BCAST_WIDE
|
|
764
|
+
#define CHOL_BCAST_WIDE (NV >= 36 && NV <= 48)
|
|
765
|
+
#endif
|
|
766
|
+
#if NV > 32 && NV <= 35 || CHOL_BCAST_WIDE
|
|
767
|
+
#define CHOL_NT (NV - 32) // tail rows
|
|
768
|
+
// The arithmetic below is written with explicit roundings (FMS: c - a b in one fma; MUL): ptxas otherwise fuses
|
|
769
|
+
// mul / add pairs depending on the calling kernel, and k_substep, k_pre and k_post must agree bitwise
|
|
770
|
+
#define FMS(a, b, c) __fmaf_rn(-(a), (b), (c))
|
|
771
|
+
#define MUL(a, b) __fmul_rn((a), (b))
|
|
772
|
+
// rsqrtf for a normal argument (x >= 2^-126): the bare MUFU.RSQ without rsqrtf's scaling of denormals (same result)
|
|
773
|
+
__device__ __forceinline__ float rsqrt_normal(float x) {
|
|
774
|
+
float r;
|
|
775
|
+
asm("rsqrt.approx.ftz.f32 %0, %1;" : "=f"(r) : "f"(x));
|
|
776
|
+
return r;
|
|
777
|
+
}
|
|
778
|
+
// backward substitution L' x = y on rows 0..31 in blocks of 8 (lane r: y_r in, x_r out; the contributions of rows 32..
|
|
779
|
+
// already subtracted): a block's own triangle is solved redundantly on every lane (broadcast loads of L; rinv: the
|
|
780
|
+
// reciprocal pivots in shared memory), then each lane updates its row: one round of shuffles per block, not per row
|
|
781
|
+
__device__ __forceinline__ float chol_back32(const float* L, const float* rinv, float y, int lane) {
|
|
782
|
+
#pragma unroll
|
|
783
|
+
for (int kb = 24; kb >= 0; kb -= 8) {
|
|
784
|
+
float xb[8];
|
|
785
|
+
#pragma unroll
|
|
786
|
+
for (int j = 0; j < 8; j++) xb[j] = __shfl_sync(0xffffffffu, y, kb + j);
|
|
787
|
+
#pragma unroll
|
|
788
|
+
for (int j = 7; j >= 0; j--) {
|
|
789
|
+
float t = xb[j];
|
|
790
|
+
#pragma unroll
|
|
791
|
+
for (int i = j + 1; i < 8; i++) t = FMS(L[TRI(kb + i, kb + j)], xb[i], t);
|
|
792
|
+
xb[j] = MUL(t, rinv[kb + j]);
|
|
793
|
+
}
|
|
794
|
+
#pragma unroll
|
|
795
|
+
for (int j = 0; j < 8; j++)
|
|
796
|
+
y = lane < kb ? FMS(L[TRI(kb + j, min(lane, kb + j))], xb[j], y) : (lane == kb + j ? xb[j] : y);
|
|
797
|
+
}
|
|
798
|
+
return y;
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
#if NV <= 35
|
|
802
|
+
__device__ __noinline__ void chol_solve_bcast(const float* A, float* L, float* b, int lane, float* cb) {
|
|
803
|
+
static_assert(NV > 32 && NV <= 35, "a tail of 1..3 rows");
|
|
804
|
+
const int r = lane;
|
|
805
|
+
float a[32]; // a[m] = A[r][k + m] at step k (entries right of the diagonal: bounded garbage, never read as L)
|
|
806
|
+
#pragma unroll
|
|
807
|
+
for (int j = 0; j < 32; j++) a[j] = j <= r ? A[TRI(r, j)] : 0.f;
|
|
808
|
+
float T[CHOL_NT], tb[CHOL_NT * (CHOL_NT + 1) / 2], bt[CHOL_NT]; // A[32 + t][r]; the tail block (packed); b tail
|
|
809
|
+
#pragma unroll
|
|
810
|
+
for (int t = 0; t < CHOL_NT; t++) {
|
|
811
|
+
T[t] = A[TRI(32 + t, r)];
|
|
812
|
+
bt[t] = b[32 + t];
|
|
813
|
+
#pragma unroll
|
|
814
|
+
for (int u = 0; u <= t; u++) tb[t * (t + 1) / 2 + u] = A[TRI(32 + t, 32 + u)];
|
|
815
|
+
}
|
|
816
|
+
float y = b[r], myinv = 0.f;
|
|
817
|
+
__syncwarp();
|
|
818
|
+
float dn = a[0]; // the next pivot (lane k at step k): the diagonal after the updates so far
|
|
819
|
+
// unrolled by 2 (unroll 1: V100 13.7 against 12.7 ms per control step, RTX 3090 7.59 against 7.46; a test per group
|
|
820
|
+
// of the row update, or phases with fewer groups as the trailing part shrinks, i.e. more code: V100 13.0 / 15.2 ms)
|
|
821
|
+
#pragma unroll 2
|
|
822
|
+
for (int k = 0; k < 32; k++) {
|
|
823
|
+
float* buf = cb + (k & 1) * 36; // double buffered: [0, 4) L[32 + t][k], y_k; 4 + (j - k - 1): L[j][k], k < j < 32
|
|
824
|
+
float dkk = fmaxf(__shfl_sync(0xffffffffu, dn, k), 1e-30f);
|
|
825
|
+
float inv = rsqrt_normal(dkk);
|
|
826
|
+
inv = MUL(inv, FMS(MUL(MUL(0.5f, dkk), inv), inv, 1.5f)); // one Newton step: about full float precision
|
|
827
|
+
float d = MUL(dkk, inv);
|
|
828
|
+
float l = r > k ? MUL(a[0], inv) : (r == k ? d : 0.f);
|
|
829
|
+
if (r >= k) L[TRI(r, k)] = l;
|
|
830
|
+
if (r == k) {
|
|
831
|
+
myinv = inv;
|
|
832
|
+
y = MUL(y, inv);
|
|
833
|
+
float tw[4] = {0.f, 0.f, 0.f, y};
|
|
834
|
+
#pragma unroll
|
|
835
|
+
for (int t = 0; t < CHOL_NT; t++) { T[t] = MUL(T[t], inv); L[TRI(32 + t, k)] = T[t]; tw[t] = T[t]; }
|
|
836
|
+
*reinterpret_cast<float4*>(buf) = make_float4(tw[0], tw[1], tw[2], tw[3]);
|
|
837
|
+
l = 0.f; // the pivot row takes no part in the trailing update
|
|
838
|
+
}
|
|
839
|
+
if (r > k) buf[4 + r - k - 1] = l;
|
|
840
|
+
dn = FMS(l, l, a[1]); // lane k + 1: its next diagonal (= a[0] after the shift below)
|
|
841
|
+
__syncwarp();
|
|
842
|
+
float4 tv = *reinterpret_cast<const float4*>(buf);
|
|
843
|
+
float tl[3] = {tv.x, tv.y, tv.z};
|
|
844
|
+
y = FMS(l, tv.w, y);
|
|
845
|
+
#pragma unroll
|
|
846
|
+
for (int t = 0; t < CHOL_NT; t++) {
|
|
847
|
+
T[t] = FMS(tl[t], l, T[t]);
|
|
848
|
+
bt[t] = FMS(tl[t], tv.w, bt[t]);
|
|
849
|
+
#pragma unroll
|
|
850
|
+
for (int u = 0; u <= t; u++) tb[t * (t + 1) / 2 + u] = FMS(tl[t], tl[u], tb[t * (t + 1) / 2 + u]);
|
|
851
|
+
}
|
|
852
|
+
#pragma unroll
|
|
853
|
+
for (int g = 1; g < 32; g += 4) { // all groups every step (skipping the second half once k >= 16, one uniform
|
|
854
|
+
float4 c = *reinterpret_cast<const float4*>(buf + 4 + g - 1); // branch, was slower: RTX 3090 9.9k against 8.9k)
|
|
855
|
+
a[g - 1] = FMS(l, c.x, a[g]);
|
|
856
|
+
if (g + 1 < 32) a[g] = FMS(l, c.y, a[g + 1]);
|
|
857
|
+
if (g + 2 < 32) a[g + 1] = FMS(l, c.z, a[g + 2]);
|
|
858
|
+
if (g + 3 < 32) a[g + 2] = FMS(l, c.w, a[g + 3]);
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
float dt[CHOL_NT], it[CHOL_NT], xt[CHOL_NT]; // the tail block's factor and solve, on every lane
|
|
862
|
+
#pragma unroll
|
|
863
|
+
for (int t = 0; t < CHOL_NT; t++) {
|
|
864
|
+
float s = tb[t * (t + 1) / 2 + t];
|
|
865
|
+
#pragma unroll
|
|
866
|
+
for (int u = 0; u < t; u++) s = FMS(tb[t * (t + 1) / 2 + u], tb[t * (t + 1) / 2 + u], s);
|
|
867
|
+
float dkk = fmaxf(s, 1e-30f), inv = rsqrt_normal(dkk);
|
|
868
|
+
inv = MUL(inv, FMS(MUL(MUL(0.5f, dkk), inv), inv, 1.5f));
|
|
869
|
+
dt[t] = MUL(dkk, inv); it[t] = inv;
|
|
870
|
+
#pragma unroll
|
|
871
|
+
for (int v = t + 1; v < CHOL_NT; v++) {
|
|
872
|
+
float e = tb[v * (v + 1) / 2 + t];
|
|
873
|
+
#pragma unroll
|
|
874
|
+
for (int u = 0; u < t; u++) e = FMS(tb[v * (v + 1) / 2 + u], tb[t * (t + 1) / 2 + u], e);
|
|
875
|
+
tb[v * (v + 1) / 2 + t] = MUL(e, inv);
|
|
876
|
+
}
|
|
877
|
+
float yt = bt[t];
|
|
878
|
+
#pragma unroll
|
|
879
|
+
for (int u = 0; u < t; u++) yt = FMS(tb[t * (t + 1) / 2 + u], xt[u], yt);
|
|
880
|
+
xt[t] = MUL(yt, inv); // forward
|
|
881
|
+
}
|
|
882
|
+
if (lane == 0) {
|
|
883
|
+
#pragma unroll
|
|
884
|
+
for (int t = 0; t < CHOL_NT; t++) {
|
|
885
|
+
#pragma unroll
|
|
886
|
+
for (int u = 0; u < t; u++) L[TRI(32 + t, 32 + u)] = tb[t * (t + 1) / 2 + u];
|
|
887
|
+
L[TRI(32 + t, 32 + t)] = dt[t];
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
#pragma unroll
|
|
891
|
+
for (int t = CHOL_NT - 1; t >= 0; t--) { // backward on the tail
|
|
892
|
+
float x = xt[t];
|
|
893
|
+
#pragma unroll
|
|
894
|
+
for (int v = t + 1; v < CHOL_NT; v++) x = FMS(tb[v * (v + 1) / 2 + t], xt[v], x);
|
|
895
|
+
xt[t] = MUL(x, it[t]);
|
|
896
|
+
}
|
|
897
|
+
#pragma unroll
|
|
898
|
+
for (int t = 0; t < CHOL_NT; t++) y = FMS(T[t], xt[t], y);
|
|
899
|
+
__syncwarp();
|
|
900
|
+
// backward on rows 0..31: L' x = y, column by column, unrolled by 4 (fully: V100 12.73 against 12.44 ms, RTX 3090 the
|
|
901
|
+
// same; chol_back32 was slower here)
|
|
902
|
+
#pragma unroll 4
|
|
903
|
+
for (int k = 31; k >= 0; k--) {
|
|
904
|
+
float xk = __shfl_sync(0xffffffffu, MUL(y, myinv), k);
|
|
905
|
+
float lk = r < k ? L[TRI(k, r)] : 0.f;
|
|
906
|
+
y = r == k ? xk : FMS(lk, xk, y);
|
|
907
|
+
}
|
|
908
|
+
b[r] = y;
|
|
909
|
+
if (lane == 0) { // (lane t writing entry t became a dynamically indexed local array)
|
|
910
|
+
#pragma unroll
|
|
911
|
+
for (int t = 0; t < CHOL_NT; t++) b[32 + t] = xt[t];
|
|
912
|
+
}
|
|
913
|
+
__syncwarp();
|
|
914
|
+
}
|
|
915
|
+
#endif // NV <= 35
|
|
916
|
+
|
|
917
|
+
// x = (L L')^-1 b in place (b: shared NV vector) with a factor L from CHOL_SOLVE (packed lower triangle in shared
|
|
918
|
+
// memory, true diagonal), for a Newton iteration whose Hessian did not change; cb: scratch as for chol_solve_bcast.
|
|
919
|
+
// Blocks of 8 rows as in chol_back32 (forward likewise), the tail rows 32.. on every lane. V100: 3.0k cycles (column by
|
|
920
|
+
// column with one broadcast per row: 6.3k)
|
|
921
|
+
// CHOL_BCAST_WIDE: inlined. As a call, ptxas 12.4 (sm_70, sm_86) restored a register the callee needed by recomputing
|
|
922
|
+
// the value one call site had in it (the lane) also for k_post's other site (Hand::big), where it held the upper half
|
|
923
|
+
// of a pointer: an illegal address after the re-use (docs/perf/chol_wide.md)
|
|
924
|
+
#if CHOL_BCAST_WIDE
|
|
925
|
+
__device__ __forceinline__ void chol_resolve(const float* L, float* b, int lane, float* cb) {
|
|
926
|
+
#else
|
|
927
|
+
__device__ __noinline__ void chol_resolve(const float* L, float* b, int lane, float* cb) {
|
|
928
|
+
#endif
|
|
929
|
+
const int r = lane;
|
|
930
|
+
float* rinv = cb;
|
|
931
|
+
rinv[r] = 1.f / L[TRI(r, r)];
|
|
932
|
+
if (r < CHOL_NT) rinv[32 + r] = 1.f / L[TRI(32 + r, 32 + r)];
|
|
933
|
+
const int r1 = min(32 + r, NV - 1); // lanes 0 .. CHOL_NT - 1: tail row 32 + r (y1); others a valid dummy row
|
|
934
|
+
float y = b[r], y1 = b[r1];
|
|
935
|
+
const int tr = TRI(r, 0), tr1 = TRI(r1, 0);
|
|
936
|
+
__syncwarp();
|
|
937
|
+
#pragma unroll
|
|
938
|
+
for (int kb = 0; kb < 32; kb += 8) { // forward: L y = b on rows 0..31
|
|
939
|
+
float yb[8];
|
|
940
|
+
#pragma unroll
|
|
941
|
+
for (int j = 0; j < 8; j++) yb[j] = __shfl_sync(0xffffffffu, y, kb + j);
|
|
942
|
+
#pragma unroll
|
|
943
|
+
for (int j = 0; j < 8; j++) {
|
|
944
|
+
float t = yb[j];
|
|
945
|
+
#pragma unroll
|
|
946
|
+
for (int i = 0; i < j; i++) t = FMS(L[TRI(kb + j, kb + i)], yb[i], t);
|
|
947
|
+
yb[j] = MUL(t, rinv[kb + j]);
|
|
948
|
+
}
|
|
949
|
+
#pragma unroll
|
|
950
|
+
for (int j = 0; j < 8; j++) {
|
|
951
|
+
y = r >= kb + 8 ? FMS(L[tr + min(kb + j, r)], yb[j], y) : (r == kb + j ? yb[j] : y);
|
|
952
|
+
y1 = FMS(L[tr1 + kb + j], yb[j], y1);
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
float yt[CHOL_NT]; // the tail rows on every lane: forward, backward, then their part of rows 0..31
|
|
956
|
+
#pragma unroll
|
|
957
|
+
for (int t = 0; t < CHOL_NT; t++) {
|
|
958
|
+
float u = __shfl_sync(0xffffffffu, y1, t);
|
|
959
|
+
#pragma unroll
|
|
960
|
+
for (int i = 0; i < t; i++) u = FMS(L[TRI(32 + t, 32 + i)], yt[i], u);
|
|
961
|
+
yt[t] = MUL(u, rinv[32 + t]);
|
|
962
|
+
}
|
|
963
|
+
#pragma unroll
|
|
964
|
+
for (int t = CHOL_NT - 1; t >= 0; t--) {
|
|
965
|
+
float u = yt[t];
|
|
966
|
+
#pragma unroll
|
|
967
|
+
for (int i = t + 1; i < CHOL_NT; i++) u = FMS(L[TRI(32 + i, 32 + t)], yt[i], u);
|
|
968
|
+
yt[t] = MUL(u, rinv[32 + t]);
|
|
969
|
+
}
|
|
970
|
+
#pragma unroll
|
|
971
|
+
for (int t = 0; t < CHOL_NT; t++) y = FMS(L[TRI(32 + t, r)], yt[t], y);
|
|
972
|
+
y = chol_back32(L, rinv, y, lane);
|
|
973
|
+
b[r] = y;
|
|
974
|
+
if (lane == 0) {
|
|
975
|
+
#pragma unroll
|
|
976
|
+
for (int t = 0; t < CHOL_NT; t++) b[32 + t] = yt[t];
|
|
977
|
+
}
|
|
978
|
+
__syncwarp();
|
|
979
|
+
}
|
|
980
|
+
#undef FMS
|
|
981
|
+
#undef MUL
|
|
982
|
+
#endif
|
|
983
|
+
|
|
984
|
+
// Wide broadcast variant (same contract as chol_solve_bcast; cb: CHOL_CB floats of 16-byte aligned shared scratch) for
|
|
985
|
+
// 36 <= NV <= 48, e.g. a humanoid with a free object. Rows 0..31 as in chol_solve_bcast (lane r: row r in registers,
|
|
986
|
+
// shifted, column k broadcast through the double-buffered scratch together with the tail entries L[32 + t][k] and
|
|
987
|
+
// y_k), the WT = NV - 32 tail rows held transposed (lane r: A[32 + t][r]), but without the tail block on every lane:
|
|
988
|
+
// after the 32 steps the Schur complement S = A22 - L21 L21' and the tail's right-hand side b2 - L21 y1 are formed once,
|
|
989
|
+
// one entry per lane and round (dot products of length 32 over the rows of L21 in shared memory, in the order of the
|
|
990
|
+
// rank-1 updates: the same roundings as updating them step by step), written in place of A22 / b2; then lane t < WT
|
|
991
|
+
// factors and solves row t of S as chol_solve_small does (column broadcast through the scratch), and the backward
|
|
992
|
+
// solve runs column by column (tail, then rows 0..31). -DCHOL_BCAST_WIDE=0: the previous CHOL_SOLVE for these sizes
|
|
993
|
+
// (chol_solve_compact below Ampere, chol_solve_reg from Ampere on)
|
|
994
|
+
#if CHOL_BCAST_WIDE
|
|
995
|
+
#define CHOL_WT (NV - 32) // tail rows
|
|
996
|
+
#define CHOL_WH ((CHOL_WT + 4) / 4 * 4) // a step's header: L[32 + t][k] (t < WT), y_k, padded to float4
|
|
997
|
+
#define CHOL_WS (CHOL_WH + 32) // one buffer: the header, then L[j][k] at CHOL_WH + (j - k - 1)
|
|
998
|
+
#define CHOL_CB (2 * CHOL_WS) // the scratch (Sim::cbuf and the like)
|
|
999
|
+
#define WFMS(a, b, c) __fmaf_rn(-(a), (b), (c)) // explicit roundings, as chol_solve_bcast
|
|
1000
|
+
#define WMUL(a, b) __fmul_rn((a), (b))
|
|
1001
|
+
__device__ __forceinline__ float rsqrt_normal_w(float x) {
|
|
1002
|
+
float r;
|
|
1003
|
+
asm("rsqrt.approx.ftz.f32 %0, %1;" : "=f"(r) : "f"(x));
|
|
1004
|
+
return r;
|
|
1005
|
+
}
|
|
1006
|
+
__device__ __noinline__ void chol_solve_bcast_wide(const float* A, float* L, float* b, int lane, float* cb) {
|
|
1007
|
+
static_assert(NV >= 36 && NV <= 48, "a tail of 4..16 rows");
|
|
1008
|
+
const int r = lane;
|
|
1009
|
+
float a[32]; // a[m] = A[r][k + m] at step k (entries right of the diagonal: bounded garbage, never read as L)
|
|
1010
|
+
#pragma unroll
|
|
1011
|
+
for (int j = 0; j < 32; j++) a[j] = j <= r ? A[TRI(r, j)] : 0.f;
|
|
1012
|
+
float T[CHOL_WT]; // A[32 + t][r], updated to L[32 + t][r]
|
|
1013
|
+
#pragma unroll
|
|
1014
|
+
for (int t = 0; t < CHOL_WT; t++) T[t] = A[TRI(32 + t, r)];
|
|
1015
|
+
float y = b[r], myinv = 0.f;
|
|
1016
|
+
__syncwarp();
|
|
1017
|
+
float dn = a[0]; // the next pivot (lane k at step k)
|
|
1018
|
+
#pragma unroll 2
|
|
1019
|
+
for (int k = 0; k < 32; k++) {
|
|
1020
|
+
float* buf = cb + (k & 1) * CHOL_WS;
|
|
1021
|
+
float dkk = fmaxf(__shfl_sync(0xffffffffu, dn, k), 1e-30f);
|
|
1022
|
+
float inv = rsqrt_normal_w(dkk);
|
|
1023
|
+
inv = WMUL(inv, WFMS(WMUL(WMUL(0.5f, dkk), inv), inv, 1.5f)); // one Newton step
|
|
1024
|
+
float d = WMUL(dkk, inv);
|
|
1025
|
+
float l = r > k ? WMUL(a[0], inv) : (r == k ? d : 0.f);
|
|
1026
|
+
if (r >= k) L[TRI(r, k)] = l;
|
|
1027
|
+
if (r == k) {
|
|
1028
|
+
myinv = inv;
|
|
1029
|
+
y = WMUL(y, inv);
|
|
1030
|
+
float tw[CHOL_WH];
|
|
1031
|
+
#pragma unroll
|
|
1032
|
+
for (int t = 0; t < CHOL_WH; t++) tw[t] = 0.f;
|
|
1033
|
+
#pragma unroll
|
|
1034
|
+
for (int t = 0; t < CHOL_WT; t++) { T[t] = WMUL(T[t], inv); L[TRI(32 + t, k)] = T[t]; tw[t] = T[t]; }
|
|
1035
|
+
tw[CHOL_WT] = y;
|
|
1036
|
+
#pragma unroll
|
|
1037
|
+
for (int t = 0; t < CHOL_WH; t += 4)
|
|
1038
|
+
*reinterpret_cast<float4*>(buf + t) = make_float4(tw[t], tw[t + 1], tw[t + 2], tw[t + 3]);
|
|
1039
|
+
l = 0.f; // the pivot row takes no part in the trailing update
|
|
1040
|
+
}
|
|
1041
|
+
if (r > k) buf[CHOL_WH + r - k - 1] = l;
|
|
1042
|
+
dn = WFMS(l, l, a[1]); // lane k + 1: its next diagonal
|
|
1043
|
+
__syncwarp();
|
|
1044
|
+
float tl[CHOL_WH];
|
|
1045
|
+
#pragma unroll
|
|
1046
|
+
for (int t = 0; t < CHOL_WH; t += 4) {
|
|
1047
|
+
float4 v = *reinterpret_cast<const float4*>(buf + t);
|
|
1048
|
+
tl[t] = v.x; tl[t + 1] = v.y; tl[t + 2] = v.z; tl[t + 3] = v.w;
|
|
1049
|
+
}
|
|
1050
|
+
y = WFMS(l, tl[CHOL_WT], y);
|
|
1051
|
+
#pragma unroll
|
|
1052
|
+
for (int t = 0; t < CHOL_WT; t++) T[t] = WFMS(tl[t], l, T[t]);
|
|
1053
|
+
#pragma unroll
|
|
1054
|
+
for (int g = 1; g < 32; g += 4) {
|
|
1055
|
+
float4 c = *reinterpret_cast<const float4*>(buf + CHOL_WH + g - 1);
|
|
1056
|
+
a[g - 1] = WFMS(l, c.x, a[g]);
|
|
1057
|
+
if (g + 1 < 32) a[g] = WFMS(l, c.y, a[g + 1]);
|
|
1058
|
+
if (g + 2 < 32) a[g + 1] = WFMS(l, c.z, a[g + 2]);
|
|
1059
|
+
if (g + 3 < 32) a[g + 2] = WFMS(l, c.w, a[g + 3]);
|
|
1060
|
+
}
|
|
1061
|
+
}
|
|
1062
|
+
b[r] = y; // y_r of rows 0..31, read by the tail's right-hand side
|
|
1063
|
+
__syncwarp();
|
|
1064
|
+
// S = A22 - L21 L21' (packed, entry e < WT (WT + 1) / 2) and b2 - L21 y1 (entry WT (WT + 1) / 2 + t): each entry read
|
|
1065
|
+
// and written by one lane, in place (A22 / b2 are read nowhere else, L21 and y1 are in columns / entries < 32)
|
|
1066
|
+
constexpr int NS = CHOL_WT * (CHOL_WT + 1) / 2, NE = NS + CHOL_WT;
|
|
1067
|
+
#pragma unroll
|
|
1068
|
+
for (int e0 = 0; e0 < NE; e0 += 32) {
|
|
1069
|
+
const int e = e0 + lane;
|
|
1070
|
+
if (e < NE) {
|
|
1071
|
+
int t = 0;
|
|
1072
|
+
#pragma unroll
|
|
1073
|
+
for (int i = 1; i < CHOL_WT; i++) t = e >= i * (i + 1) / 2 ? i : t;
|
|
1074
|
+
const bool rhs = e >= NS;
|
|
1075
|
+
const int tr = rhs ? e - NS : t, u = e - t * (t + 1) / 2;
|
|
1076
|
+
const float* p = L + TRI(32 + tr, 0);
|
|
1077
|
+
const float* q = rhs ? b : L + TRI(32 + u, 0);
|
|
1078
|
+
float* out = rhs ? b + 32 + tr : L + TRI(32 + t, 32 + u);
|
|
1079
|
+
float s = rhs ? b[32 + tr] : A[TRI(32 + t, 32 + u)];
|
|
1080
|
+
#pragma unroll
|
|
1081
|
+
for (int j = 0; j < 32; j++) s = WFMS(p[j], q[j], s);
|
|
1082
|
+
*out = s;
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
__syncwarp();
|
|
1086
|
+
// the tail: lane t < WT holds row t of S (unshifted; the loop is unrolled), the forward solve inside the factorization
|
|
1087
|
+
const bool own = r < CHOL_WT;
|
|
1088
|
+
float s[CHOL_WT];
|
|
1089
|
+
#pragma unroll
|
|
1090
|
+
for (int j = 0; j < CHOL_WT; j++) s[j] = own && j <= r ? L[TRI(32 + r, 32 + j)] : 0.f;
|
|
1091
|
+
float yt = own ? b[32 + r] : 0.f, tinv = 0.f;
|
|
1092
|
+
__syncwarp();
|
|
1093
|
+
dn = s[0];
|
|
1094
|
+
#pragma unroll
|
|
1095
|
+
for (int k = 0; k < CHOL_WT; k++) {
|
|
1096
|
+
float* buf = cb + (k & 1) * 20; // [0]: y_k; [j - k]: L[32 + j][32 + k], k < j < WT
|
|
1097
|
+
float dkk = fmaxf(__shfl_sync(0xffffffffu, dn, k), 1e-30f);
|
|
1098
|
+
float inv = rsqrt_normal_w(dkk);
|
|
1099
|
+
inv = WMUL(inv, WFMS(WMUL(WMUL(0.5f, dkk), inv), inv, 1.5f));
|
|
1100
|
+
float d = WMUL(dkk, inv);
|
|
1101
|
+
float l = r > k ? WMUL(s[k], inv) : (r == k ? d : 0.f);
|
|
1102
|
+
if (own && r >= k) L[TRI(32 + r, 32 + k)] = l;
|
|
1103
|
+
if (r == k) {
|
|
1104
|
+
tinv = inv;
|
|
1105
|
+
yt = WMUL(yt, inv);
|
|
1106
|
+
buf[0] = yt;
|
|
1107
|
+
l = 0.f;
|
|
1108
|
+
}
|
|
1109
|
+
if (own && r > k) buf[r - k] = l;
|
|
1110
|
+
if (k + 1 < CHOL_WT) dn = WFMS(l, l, s[k + 1]);
|
|
1111
|
+
__syncwarp();
|
|
1112
|
+
yt = WFMS(l, buf[0], yt);
|
|
1113
|
+
#pragma unroll
|
|
1114
|
+
for (int j = k + 1; j < CHOL_WT; j++) s[j] = WFMS(l, buf[j - k], s[j]);
|
|
1115
|
+
}
|
|
1116
|
+
__syncwarp();
|
|
1117
|
+
#pragma unroll
|
|
1118
|
+
for (int k = CHOL_WT - 1; k >= 0; k--) { // backward on the tail: L22' x2 = y2, column by column
|
|
1119
|
+
float xk = __shfl_sync(0xffffffffu, WMUL(yt, tinv), k);
|
|
1120
|
+
float lk = r < k ? L[TRI(32 + k, 32 + r)] : 0.f;
|
|
1121
|
+
yt = r == k ? xk : WFMS(lk, xk, yt);
|
|
1122
|
+
}
|
|
1123
|
+
#pragma unroll
|
|
1124
|
+
for (int t = CHOL_WT - 1; t >= 0; t--) y = WFMS(T[t], __shfl_sync(0xffffffffu, yt, t), y); // y1 - L21' x2
|
|
1125
|
+
// backward on rows 0..31, as chol_solve_bcast
|
|
1126
|
+
#pragma unroll 4
|
|
1127
|
+
for (int k = 31; k >= 0; k--) {
|
|
1128
|
+
float xk = __shfl_sync(0xffffffffu, WMUL(y, myinv), k);
|
|
1129
|
+
float lk = r < k ? L[TRI(k, r)] : 0.f;
|
|
1130
|
+
y = r == k ? xk : WFMS(lk, xk, y);
|
|
1131
|
+
}
|
|
1132
|
+
b[r] = y;
|
|
1133
|
+
if (own) b[32 + r] = yt;
|
|
1134
|
+
__syncwarp();
|
|
1135
|
+
}
|
|
1136
|
+
#undef WFMS
|
|
1137
|
+
#undef WMUL
|
|
1138
|
+
#endif
|
|
1139
|
+
#ifndef CHOL_CB
|
|
1140
|
+
#define CHOL_CB 72 // CHOL_SOLVE's scratch (floats): chol_solve_bcast's two buffers of 36
|
|
1141
|
+
#endif
|
|
1142
|
+
|
|
1143
|
+
#if NV <= 32
|
|
1144
|
+
// chol_solve_small: as chol_solve_bcast (lane r: row r of A in registers, shifted one entry per step so that the
|
|
1145
|
+
// entry the step needs is a[0]; the column of L broadcast through the double-buffered scratch cb; the forward solve
|
|
1146
|
+
// during the factorization, then the backward one column by column), for NV <= 32 rows and no tail. Explicit roundings
|
|
1147
|
+
// (k_substep, k_pre and k_post must agree bitwise)
|
|
1148
|
+
#define SFMS(a, b, c) __fmaf_rn(-(a), (b), (c))
|
|
1149
|
+
#define SMUL(a, b) __fmul_rn((a), (b))
|
|
1150
|
+
__device__ __noinline__ void chol_solve_small(const float* A, float* L, float* b, int lane, float* cb) {
|
|
1151
|
+
const int r = lane;
|
|
1152
|
+
const bool own = r < NV;
|
|
1153
|
+
float a[32]; // a[m] = A[r][k + m] at step k (entries right of the diagonal: bounded garbage, never read as L)
|
|
1154
|
+
#pragma unroll
|
|
1155
|
+
for (int j = 0; j < 32; j++) a[j] = (own && j <= r && j < NV) ? A[TRI(r, j)] : 0.f;
|
|
1156
|
+
float y = own ? b[r] : 0.f, myinv = 0.f;
|
|
1157
|
+
__syncwarp();
|
|
1158
|
+
float dn = a[0]; // the next pivot (lane k at step k)
|
|
1159
|
+
#pragma unroll 2
|
|
1160
|
+
for (int k = 0; k < NV; k++) {
|
|
1161
|
+
float* buf = cb + (k & 1) * 36; // [0]: y_k; [1 + (j - k - 1)]: L[j][k], k < j < 32
|
|
1162
|
+
float dkk = fmaxf(__shfl_sync(0xffffffffu, dn, k), 1e-30f);
|
|
1163
|
+
float inv;
|
|
1164
|
+
asm("rsqrt.approx.ftz.f32 %0, %1;" : "=f"(inv) : "f"(dkk));
|
|
1165
|
+
inv = SMUL(inv, SFMS(SMUL(SMUL(0.5f, dkk), inv), inv, 1.5f)); // one Newton step
|
|
1166
|
+
float d = SMUL(dkk, inv);
|
|
1167
|
+
float l = r > k ? SMUL(a[0], inv) : (r == k ? d : 0.f);
|
|
1168
|
+
if (own && r >= k) L[TRI(r, k)] = l;
|
|
1169
|
+
if (r == k) {
|
|
1170
|
+
myinv = inv;
|
|
1171
|
+
y = SMUL(y, inv);
|
|
1172
|
+
buf[0] = y;
|
|
1173
|
+
l = 0.f; // the pivot row takes no part in the trailing update
|
|
1174
|
+
}
|
|
1175
|
+
if (r > k) buf[r - k] = l;
|
|
1176
|
+
dn = SFMS(l, l, a[1]);
|
|
1177
|
+
__syncwarp();
|
|
1178
|
+
y = SFMS(l, buf[0], y);
|
|
1179
|
+
#pragma unroll
|
|
1180
|
+
for (int g = 1; g < 32; g++) a[g - 1] = SFMS(l, buf[g], a[g]); // (buf[g] past the rows: zeros from those lanes)
|
|
1181
|
+
}
|
|
1182
|
+
__syncwarp();
|
|
1183
|
+
#pragma unroll 4
|
|
1184
|
+
for (int k = NV - 1; k >= 0; k--) { // backward: L' x = y
|
|
1185
|
+
float xk = __shfl_sync(0xffffffffu, SMUL(y, myinv), k);
|
|
1186
|
+
float lk = (r < k) ? L[TRI(k, r)] : 0.f;
|
|
1187
|
+
y = r == k ? xk : SFMS(lk, xk, y);
|
|
1188
|
+
}
|
|
1189
|
+
if (own) b[r] = y;
|
|
1190
|
+
__syncwarp();
|
|
1191
|
+
}
|
|
1192
|
+
#undef SFMS
|
|
1193
|
+
#undef SMUL
|
|
1194
|
+
#endif
|
|
1195
|
+
|
|
1196
|
+
// chol_solve_trees: the solve with a block-diagonal A, one block per kinematic tree (M and M + h kv: no coupling
|
|
1197
|
+
// between trees without equality constraints or tendons), same contract as CHOL_SOLVE (A == L allowed). Lane t factors
|
|
1198
|
+
// block t in registers (fully unrolled to MAXTD rows, a shorter block padded with identity rows: the same instructions
|
|
1199
|
+
// on every lane), solves its part of b, and writes its block of L; the entries of L outside the blocks are zero.
|
|
1200
|
+
// Explicit roundings (k_substep, k_pre and k_post must agree bitwise)
|
|
1201
|
+
#if NTREE <= 32 && MAXTD <= 12
|
|
1202
|
+
#define TFMS(a, b, c) __fmaf_rn(-(a), (b), (c))
|
|
1203
|
+
#define TMUL(a, b) __fmul_rn((a), (b))
|
|
1204
|
+
__device__ __noinline__ void chol_solve_trees(const float* A, float* L, float* b, int lane) {
|
|
1205
|
+
int o, n;
|
|
1206
|
+
TREE_OF(lane, o, n);
|
|
1207
|
+
float a[MAXTD * (MAXTD + 1) / 2], y[MAXTD], rinv[MAXTD];
|
|
1208
|
+
#pragma unroll
|
|
1209
|
+
for (int i = 0; i < MAXTD; i++) {
|
|
1210
|
+
#pragma unroll
|
|
1211
|
+
for (int j = 0; j <= i; j++) a[TRI(i, j)] = i < n ? A[TRI(o + i, o + j)] : (i == j ? 1.f : 0.f);
|
|
1212
|
+
y[i] = i < n ? b[o + i] : 0.f;
|
|
1213
|
+
}
|
|
1214
|
+
__syncwarp();
|
|
1215
|
+
for (int i = lane; i < NV; i += 32) // the entries outside the blocks (zero in A too: A == L is safe)
|
|
1216
|
+
for (int j = 0, j1 = TREE_START(i); j < j1; j++) L[TRI(i, j)] = 0.f;
|
|
1217
|
+
#pragma unroll
|
|
1218
|
+
for (int j = 0; j < MAXTD; j++) { // factor (Cholesky-Crout, column j), with the forward solve
|
|
1219
|
+
float s = a[TRI(j, j)];
|
|
1220
|
+
#pragma unroll
|
|
1221
|
+
for (int k = 0; k < j; k++) s = TFMS(a[TRI(j, k)], a[TRI(j, k)], s);
|
|
1222
|
+
float dkk = fmaxf(s, 1e-30f), inv;
|
|
1223
|
+
asm("rsqrt.approx.ftz.f32 %0, %1;" : "=f"(inv) : "f"(dkk));
|
|
1224
|
+
inv = TMUL(inv, TFMS(TMUL(TMUL(0.5f, dkk), inv), inv, 1.5f)); // one Newton step
|
|
1225
|
+
a[TRI(j, j)] = TMUL(dkk, inv);
|
|
1226
|
+
rinv[j] = inv;
|
|
1227
|
+
#pragma unroll
|
|
1228
|
+
for (int i = j + 1; i < MAXTD; i++) {
|
|
1229
|
+
float e = a[TRI(i, j)];
|
|
1230
|
+
#pragma unroll
|
|
1231
|
+
for (int k = 0; k < j; k++) e = TFMS(a[TRI(i, k)], a[TRI(j, k)], e);
|
|
1232
|
+
a[TRI(i, j)] = TMUL(e, inv);
|
|
1233
|
+
}
|
|
1234
|
+
float t = y[j];
|
|
1235
|
+
#pragma unroll
|
|
1236
|
+
for (int k = 0; k < j; k++) t = TFMS(a[TRI(j, k)], y[k], t);
|
|
1237
|
+
y[j] = TMUL(t, inv);
|
|
1238
|
+
}
|
|
1239
|
+
#pragma unroll
|
|
1240
|
+
for (int i = MAXTD - 1; i >= 0; i--) { // backward: L' x = y
|
|
1241
|
+
float t = y[i];
|
|
1242
|
+
#pragma unroll
|
|
1243
|
+
for (int k = i + 1; k < MAXTD; k++) t = TFMS(a[TRI(k, i)], y[k], t);
|
|
1244
|
+
y[i] = TMUL(t, rinv[i]);
|
|
1245
|
+
}
|
|
1246
|
+
#pragma unroll
|
|
1247
|
+
for (int i = 0; i < MAXTD; i++) {
|
|
1248
|
+
if (i < n) {
|
|
1249
|
+
b[o + i] = y[i];
|
|
1250
|
+
#pragma unroll
|
|
1251
|
+
for (int j = 0; j <= i; j++) L[TRI(o + i, o + j)] = a[TRI(i, j)];
|
|
1252
|
+
}
|
|
1253
|
+
}
|
|
1254
|
+
__syncwarp();
|
|
1255
|
+
}
|
|
1256
|
+
#undef TFMS
|
|
1257
|
+
#undef TMUL
|
|
1258
|
+
#endif
|
|
1259
|
+
|
|
1260
|
+
// which Cholesky the engine uses for M (CHOL_M_SOLVE: the smooth acceleration, implicitfast): chol_solve_trees by
|
|
1261
|
+
// default where the trees allow it (two or more, at most 12 dofs each; -DCHOL_TREES=0: CHOL_SOLVE)
|
|
1262
|
+
#ifndef CHOL_TREES
|
|
1263
|
+
#define CHOL_TREES (NTREE >= 2 && NTREE <= 32 && MAXTD <= 12)
|
|
1264
|
+
#endif
|
|
1265
|
+
|
|
1266
|
+
// which Cholesky the engine uses: CHOL_SOLVE(A, L, b, lane, cb) (cb: scratch for chol_solve_bcast). -DCHOL_BCAST=1
|
|
1267
|
+
// (the default where NV allows it) selects chol_solve_bcast (V100 20.5 -> 12.4 ms per mjlab control step at 10
|
|
1268
|
+
// iterations, RTX 3090 11.1 -> 7.5 ms, with NEWTON_REUSE); with -DCHOL_BCAST=0, -DCHOL_COMPACT=1 / =0 forces one of the
|
|
1269
|
+
// others, by default the compact one on Volta (V100: 26 vs 34 ms) and the unrolled one from Ampere on (RTX 3090: 14.7
|
|
1270
|
+
// vs 17.9 ms)
|
|
1271
|
+
#ifndef CHOL_BCAST
|
|
1272
|
+
#define CHOL_BCAST (NV > 32 && NV <= 35)
|
|
1273
|
+
#endif
|
|
1274
|
+
#ifndef CHOL_COMPACT
|
|
1275
|
+
#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ < 800
|
|
1276
|
+
#define CHOL_COMPACT 1
|
|
1277
|
+
#else
|
|
1278
|
+
#define CHOL_COMPACT 0
|
|
1279
|
+
#endif
|
|
1280
|
+
#endif
|
|
1281
|
+
#ifndef CHOL_SMALL // chol_solve_small below Ampere (Aloha-AgileX, 22 dofs: the Cholesky phases 3x faster than _compact),
|
|
1282
|
+
// from Ampere on chol_solve_reg as the tree engine (RTX 3090: forest / tree forward 1.01-1.07 -> 0.91-0.98 on go1, go2, h1,
|
|
1283
|
+
// spot; docs/perf/engine_switch.md), except the build targets sm_90 / sm_90a until verified there
|
|
1284
|
+
// (DK_FOREST_SM90_SMALL: nablaengine.cuda.forest_hold_defines)
|
|
1285
|
+
#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800 && !defined(DK_FOREST_SM90_SMALL)
|
|
1286
|
+
#define CHOL_SMALL 0
|
|
1287
|
+
#else
|
|
1288
|
+
#define CHOL_SMALL (NV <= 32)
|
|
1289
|
+
#endif
|
|
1290
|
+
#endif
|
|
1291
|
+
#if CHOL_BCAST
|
|
1292
|
+
#define CHOL_SOLVE(A, L, b, lane, cb) chol_solve_bcast(A, L, b, lane, cb)
|
|
1293
|
+
#elif CHOL_BCAST_WIDE
|
|
1294
|
+
#define CHOL_SOLVE(A, L, b, lane, cb) chol_solve_bcast_wide(A, L, b, lane, cb)
|
|
1295
|
+
#elif CHOL_SMALL
|
|
1296
|
+
#define CHOL_SOLVE(A, L, b, lane, cb) chol_solve_small(A, L, b, lane, cb)
|
|
1297
|
+
#elif CHOL_COMPACT
|
|
1298
|
+
#define CHOL_SOLVE(A, L, b, lane, cb) chol_solve_compact(A, L, b, lane)
|
|
1299
|
+
#else
|
|
1300
|
+
#define CHOL_SOLVE(A, L, b, lane, cb) chol_solve_reg(A, L, b, lane)
|
|
1301
|
+
#endif
|
|
1302
|
+
// chol_solve_comps: CHOL_SOLVE for a Hessian whose trees are coupled by some contacts only (same contract, A == L
|
|
1303
|
+
// allowed). The coupling is read from A (tree blocks with a nonzero entry), its transitive closure gives the connected
|
|
1304
|
+
// components of trees; the natural-order factor of A is zero between components and, within one, the factor of the
|
|
1305
|
+
// component's submatrix in ascending dof order, so each component is factored on its own: by its lowest tree's lane in
|
|
1306
|
+
// registers (gathered, unrolled to CB rows, padded with identity rows) when every component has at most CB dofs, else
|
|
1307
|
+
// the whole matrix by CHOL_SOLVE (a world with a large component). Explicit roundings as chol_solve_trees
|
|
1308
|
+
#ifndef CB
|
|
1309
|
+
#define CB 12
|
|
1310
|
+
#endif
|
|
1311
|
+
#if NTREE <= 32 && MAXTD <= CB
|
|
1312
|
+
#define CFMS(a, b, c) __fmaf_rn(-(a), (b), (c))
|
|
1313
|
+
#define CMUL(a, b) __fmul_rn((a), (b))
|
|
1314
|
+
__device__ __noinline__ void chol_solve_comps(const float* A, float* L, float* b, int lane, float* cb) {
|
|
1315
|
+
unsigned* adj = reinterpret_cast<unsigned*>(cb); // adj[t]: the trees coupled to tree t
|
|
1316
|
+
if (lane < NTREE) adj[lane] = 1u << lane;
|
|
1317
|
+
__syncwarp();
|
|
1318
|
+
#pragma unroll
|
|
1319
|
+
for (int i0 = 0; i0 < NV; i0 += 32) { // row i: the tree blocks left of its own with a nonzero entry
|
|
1320
|
+
const int i = i0 + lane;
|
|
1321
|
+
if (i < NV) {
|
|
1322
|
+
const int ti = TREE_ID(i), j1 = TREE_START(i);
|
|
1323
|
+
unsigned bits = 0u;
|
|
1324
|
+
for (int j = 0; j < j1; j++)
|
|
1325
|
+
if (A[TRI(i, j)] != 0.f) bits |= 1u << (TREE_ID(j));
|
|
1326
|
+
if (bits) {
|
|
1327
|
+
atomicOr(adj + ti, bits);
|
|
1328
|
+
for (unsigned v = bits; v; v &= v - 1) atomicOr(adj + (__ffs(v) - 1), 1u << ti);
|
|
1329
|
+
}
|
|
1330
|
+
}
|
|
1331
|
+
}
|
|
1332
|
+
__syncwarp();
|
|
1333
|
+
unsigned cm = lane < NTREE ? adj[lane] : 0u; // closure: cm = the component of tree lane
|
|
1334
|
+
for (bool more = true; more;) {
|
|
1335
|
+
unsigned c2 = cm;
|
|
1336
|
+
#pragma unroll
|
|
1337
|
+
for (int t = 0; t < NTREE; t++) {
|
|
1338
|
+
unsigned v = __shfl_sync(0xffffffffu, cm, t);
|
|
1339
|
+
if ((cm >> t) & 1u) c2 |= v;
|
|
1340
|
+
}
|
|
1341
|
+
more = __any_sync(0xffffffffu, c2 != cm);
|
|
1342
|
+
cm = c2;
|
|
1343
|
+
}
|
|
1344
|
+
const bool rep = lane < NTREE && (cm & ((1u << lane) - 1u)) == 0u; // the component's lowest tree
|
|
1345
|
+
int n = 0;
|
|
1346
|
+
#pragma unroll
|
|
1347
|
+
for (int t = 0; t < NTREE; t++) {
|
|
1348
|
+
int o, k;
|
|
1349
|
+
TREE_OF(t, o, k);
|
|
1350
|
+
if (rep && ((cm >> t) & 1u)) n += k;
|
|
1351
|
+
}
|
|
1352
|
+
if (__any_sync(0xffffffffu, n > CB)) { // a large component: the dense factorization
|
|
1353
|
+
CHOL_SOLVE(A, L, b, lane, cb);
|
|
1354
|
+
return;
|
|
1355
|
+
}
|
|
1356
|
+
int idx[CB]; // the component's dofs, ascending (trees are contiguous ascending dof ranges)
|
|
1357
|
+
#pragma unroll
|
|
1358
|
+
for (int i = 0; i < CB; i++) {
|
|
1359
|
+
int rem = i, d = 0;
|
|
1360
|
+
bool found = false;
|
|
1361
|
+
#pragma unroll
|
|
1362
|
+
for (int t = 0; t < NTREE; t++) {
|
|
1363
|
+
int o, k;
|
|
1364
|
+
TREE_OF(t, o, k);
|
|
1365
|
+
if (!found && ((cm >> t) & 1u)) {
|
|
1366
|
+
if (rem < k) { d = o + rem; found = true; } else rem -= k;
|
|
1367
|
+
}
|
|
1368
|
+
}
|
|
1369
|
+
idx[i] = d;
|
|
1370
|
+
}
|
|
1371
|
+
float a[CB * (CB + 1) / 2], y[CB], rinv[CB];
|
|
1372
|
+
#pragma unroll
|
|
1373
|
+
for (int i = 0; i < CB; i++) {
|
|
1374
|
+
#pragma unroll
|
|
1375
|
+
for (int j = 0; j <= i; j++) a[TRI(i, j)] = i < n ? A[TRI(idx[i], idx[j])] : (i == j ? 1.f : 0.f);
|
|
1376
|
+
y[i] = i < n ? b[idx[i]] : 0.f;
|
|
1377
|
+
}
|
|
1378
|
+
if (A != L) { // the entries between components (zero in A)
|
|
1379
|
+
__syncwarp();
|
|
1380
|
+
#pragma unroll
|
|
1381
|
+
for (int i0 = 0; i0 < NV; i0 += 32) {
|
|
1382
|
+
const int i = i0 + lane;
|
|
1383
|
+
const unsigned ci = __shfl_sync(0xffffffffu, cm, i < NV ? TREE_ID(i) : 0);
|
|
1384
|
+
if (i < NV)
|
|
1385
|
+
for (int j = 0; j < i; j++)
|
|
1386
|
+
if (!((ci >> (TREE_ID(j))) & 1u)) L[TRI(i, j)] = 0.f;
|
|
1387
|
+
}
|
|
1388
|
+
}
|
|
1389
|
+
#pragma unroll
|
|
1390
|
+
for (int j = 0; j < CB; j++) {
|
|
1391
|
+
float s = a[TRI(j, j)];
|
|
1392
|
+
#pragma unroll
|
|
1393
|
+
for (int k = 0; k < j; k++) s = CFMS(a[TRI(j, k)], a[TRI(j, k)], s);
|
|
1394
|
+
float dkk = fmaxf(s, 1e-30f), inv;
|
|
1395
|
+
asm("rsqrt.approx.ftz.f32 %0, %1;" : "=f"(inv) : "f"(dkk));
|
|
1396
|
+
inv = CMUL(inv, CFMS(CMUL(CMUL(0.5f, dkk), inv), inv, 1.5f));
|
|
1397
|
+
a[TRI(j, j)] = CMUL(dkk, inv);
|
|
1398
|
+
rinv[j] = inv;
|
|
1399
|
+
#pragma unroll
|
|
1400
|
+
for (int i = j + 1; i < CB; i++) {
|
|
1401
|
+
float e = a[TRI(i, j)];
|
|
1402
|
+
#pragma unroll
|
|
1403
|
+
for (int k = 0; k < j; k++) e = CFMS(a[TRI(i, k)], a[TRI(j, k)], e);
|
|
1404
|
+
a[TRI(i, j)] = CMUL(e, inv);
|
|
1405
|
+
}
|
|
1406
|
+
float t = y[j];
|
|
1407
|
+
#pragma unroll
|
|
1408
|
+
for (int k = 0; k < j; k++) t = CFMS(a[TRI(j, k)], y[k], t);
|
|
1409
|
+
y[j] = CMUL(t, inv);
|
|
1410
|
+
}
|
|
1411
|
+
#pragma unroll
|
|
1412
|
+
for (int i = CB - 1; i >= 0; i--) {
|
|
1413
|
+
float t = y[i];
|
|
1414
|
+
#pragma unroll
|
|
1415
|
+
for (int k = i + 1; k < CB; k++) t = CFMS(a[TRI(k, i)], y[k], t);
|
|
1416
|
+
y[i] = CMUL(t, rinv[i]);
|
|
1417
|
+
}
|
|
1418
|
+
__syncwarp();
|
|
1419
|
+
#pragma unroll
|
|
1420
|
+
for (int i = 0; i < CB; i++) {
|
|
1421
|
+
if (i < n) {
|
|
1422
|
+
b[idx[i]] = y[i];
|
|
1423
|
+
#pragma unroll
|
|
1424
|
+
for (int j = 0; j <= i; j++) L[TRI(idx[i], idx[j])] = a[TRI(i, j)];
|
|
1425
|
+
}
|
|
1426
|
+
}
|
|
1427
|
+
__syncwarp();
|
|
1428
|
+
}
|
|
1429
|
+
#undef CFMS
|
|
1430
|
+
#undef CMUL
|
|
1431
|
+
#endif
|
|
1432
|
+
|
|
1433
|
+
// the Newton Hessian's (CHOL_H_SOLVE): chol_solve_comps by default where the trees allow it (two or more, NV > 32: the
|
|
1434
|
+
// smaller models' CHOL_SOLVE is chol_solve_small; -DCHOL_COMPS=0: CHOL_SOLVE)
|
|
1435
|
+
#ifndef CHOL_COMPS
|
|
1436
|
+
#define CHOL_COMPS (NTREE >= 2 && NTREE <= 32 && MAXTD <= CB && NV > 32)
|
|
1437
|
+
#endif
|
|
1438
|
+
#if CHOL_COMPS
|
|
1439
|
+
#define CHOL_H_SOLVE(A, L, b, lane, cb) chol_solve_comps(A, L, b, lane, cb)
|
|
1440
|
+
#else
|
|
1441
|
+
#define CHOL_H_SOLVE(A, L, b, lane, cb) CHOL_SOLVE(A, L, b, lane, cb)
|
|
1442
|
+
#endif
|
|
1443
|
+
#if CHOL_TREES
|
|
1444
|
+
#define CHOL_M_SOLVE(A, L, b, lane, cb) chol_solve_trees(A, L, b, lane)
|
|
1445
|
+
#else
|
|
1446
|
+
#define CHOL_M_SOLVE(A, L, b, lane, cb) CHOL_SOLVE(A, L, b, lane, cb)
|
|
1447
|
+
#endif
|
|
1448
|
+
|
|
1449
|
+
// solve L L^T x = b in place (b in shared memory) with the factor from chol_warp
|
|
1450
|
+
__device__ __forceinline__ void chol_solve_warp(const float* L, float* b, int lane) {
|
|
1451
|
+
for (int k = 0; k < NV; k++) { // forward: L y = b
|
|
1452
|
+
if (lane == 0) b[k] /= L[k * NV + k];
|
|
1453
|
+
__syncwarp();
|
|
1454
|
+
for (int i = k + 1 + lane; i < NV; i += 32) b[i] -= L[i * NV + k] * b[k];
|
|
1455
|
+
__syncwarp();
|
|
1456
|
+
}
|
|
1457
|
+
for (int k = NV - 1; k >= 0; k--) { // backward: L^T x = y
|
|
1458
|
+
if (lane == 0) b[k] /= L[k * NV + k];
|
|
1459
|
+
__syncwarp();
|
|
1460
|
+
for (int i = lane; i < k; i += 32) b[i] -= L[k * NV + i] * b[k];
|
|
1461
|
+
__syncwarp();
|
|
1462
|
+
}
|
|
1463
|
+
}
|
|
1464
|
+
"""
|