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.
Files changed (136) hide show
  1. nablaengine/__init__.py +4 -0
  2. nablaengine/_fixtures/README.md +19 -0
  3. nablaengine/_fixtures/data/mf_s0.npz +0 -0
  4. nablaengine/_fixtures/mjlab/LICENSE +27 -0
  5. nablaengine/_fixtures/mjlab/README.md +113 -0
  6. nablaengine/_fixtures/mjlab/g1_meta.json +32 -0
  7. nablaengine/_fixtures/mjlab/g1_train.mjb +0 -0
  8. nablaengine/_optional.py +30 -0
  9. nablaengine/_paths.py +38 -0
  10. nablaengine/actuators/__init__.py +169 -0
  11. nablaengine/actuators/bam.py +306 -0
  12. nablaengine/actuators/bam_params/LICENSE +202 -0
  13. nablaengine/actuators/bam_params/xl330/m6.json +19 -0
  14. nablaengine/boxbox.py +259 -0
  15. nablaengine/ccd.py +1122 -0
  16. nablaengine/checks.py +376 -0
  17. nablaengine/cuda.py +483 -0
  18. nablaengine/dist_query.py +628 -0
  19. nablaengine/dual.py +223 -0
  20. nablaengine/dyn_query.py +685 -0
  21. nablaengine/engines.py +114 -0
  22. nablaengine/envs/__init__.py +24 -0
  23. nablaengine/envs/brax.py +165 -0
  24. nablaengine/envs/convert.py +771 -0
  25. nablaengine/envs/dmc.py +523 -0
  26. nablaengine/envs/env.py +233 -0
  27. nablaengine/envs/kin.py +371 -0
  28. nablaengine/envs/ops.py +134 -0
  29. nablaengine/envs/rewards.py +57 -0
  30. nablaengine/fixtures.py +243 -0
  31. nablaengine/forest_engine/__init__.py +0 -0
  32. nablaengine/forest_engine/adjoint.py +1726 -0
  33. nablaengine/forest_engine/adjoint_contact.py +493 -0
  34. nablaengine/forest_engine/bam_hooks.py +248 -0
  35. nablaengine/forest_engine/body_params.py +1007 -0
  36. nablaengine/forest_engine/capacity_diag.py +116 -0
  37. nablaengine/forest_engine/ccd_coop.py +250 -0
  38. nablaengine/forest_engine/chol_blk.py +80 -0
  39. nablaengine/forest_engine/contact_forces.py +142 -0
  40. nablaengine/forest_engine/contact_vjp.py +462 -0
  41. nablaengine/forest_engine/convex.py +1331 -0
  42. nablaengine/forest_engine/cylinder.py +73 -0
  43. nablaengine/forest_engine/dist_device.py +923 -0
  44. nablaengine/forest_engine/dyn_device.py +696 -0
  45. nablaengine/forest_engine/ellipsoid.py +179 -0
  46. nablaengine/forest_engine/elliptic.py +272 -0
  47. nablaengine/forest_engine/equality.py +842 -0
  48. nablaengine/forest_engine/floss.py +148 -0
  49. nablaengine/forest_engine/foot_offset.py +368 -0
  50. nablaengine/forest_engine/free_gyro.py +288 -0
  51. nablaengine/forest_engine/host.py +530 -0
  52. nablaengine/forest_engine/kdof64.py +127 -0
  53. nablaengine/forest_engine/kinematics.py +320 -0
  54. nablaengine/forest_engine/margin.py +167 -0
  55. nablaengine/forest_engine/margin_convex.py +326 -0
  56. nablaengine/forest_engine/midphase.py +439 -0
  57. nablaengine/forest_engine/model_hooks.py +913 -0
  58. nablaengine/forest_engine/motor_hooks.py +50 -0
  59. nablaengine/forest_engine/muscle.py +485 -0
  60. nablaengine/forest_engine/nb128.py +118 -0
  61. nablaengine/forest_engine/nb64.py +163 -0
  62. nablaengine/forest_engine/noslip.py +1124 -0
  63. nablaengine/forest_engine/nv128.py +1944 -0
  64. nablaengine/forest_engine/object_size.py +283 -0
  65. nablaengine/forest_engine/replay_guard.py +40 -0
  66. nablaengine/forest_engine/root_spread.py +126 -0
  67. nablaengine/forest_engine/solref.py +74 -0
  68. nablaengine/forest_engine/source.py +1464 -0
  69. nablaengine/forest_engine/sparse_pattern.py +527 -0
  70. nablaengine/forest_engine/spatial.py +1045 -0
  71. nablaengine/forest_engine/step.py +3083 -0
  72. nablaengine/forest_engine/surrogate.py +498 -0
  73. nablaengine/forest_engine/tape.py +199 -0
  74. nablaengine/forest_engine/tendon.py +845 -0
  75. nablaengine/forest_engine/terrain.py +1059 -0
  76. nablaengine/forest_engine/world_forces.py +166 -0
  77. nablaengine/forest_engine/world_params.py +230 -0
  78. nablaengine/g1_engine/__init__.py +0 -0
  79. nablaengine/g1_engine/adjoint.py +1661 -0
  80. nablaengine/g1_engine/adjoint_contact.py +577 -0
  81. nablaengine/g1_engine/bam_hooks.py +596 -0
  82. nablaengine/g1_engine/condim4.py +245 -0
  83. nablaengine/g1_engine/contact_forces.py +500 -0
  84. nablaengine/g1_engine/cylccd.py +1158 -0
  85. nablaengine/g1_engine/cylinder.py +216 -0
  86. nablaengine/g1_engine/elliptic.py +565 -0
  87. nablaengine/g1_engine/exact_replay.py +61 -0
  88. nablaengine/g1_engine/host.py +358 -0
  89. nablaengine/g1_engine/kinematics.py +320 -0
  90. nablaengine/g1_engine/mesh.py +305 -0
  91. nablaengine/g1_engine/mesh_tables.py +79 -0
  92. nablaengine/g1_engine/model_hooks.py +919 -0
  93. nablaengine/g1_engine/motor_hooks.py +70 -0
  94. nablaengine/g1_engine/replay_guard.py +55 -0
  95. nablaengine/g1_engine/source.py +1107 -0
  96. nablaengine/g1_engine/sphbox.py +93 -0
  97. nablaengine/g1_engine/step.py +3716 -0
  98. nablaengine/g1_engine/tape.py +173 -0
  99. nablaengine/g1_engine/world_params.py +447 -0
  100. nablaengine/identity.py +401 -0
  101. nablaengine/jax_ffi.py +2536 -0
  102. nablaengine/model/__init__.py +1 -0
  103. nablaengine/model/capability.py +694 -0
  104. nablaengine/model/fields.py +317 -0
  105. nablaengine/model/gravcomp.py +64 -0
  106. nablaengine/model/invweight.py +145 -0
  107. nablaengine/motor.py +143 -0
  108. nablaengine/object_geometry.py +609 -0
  109. nablaengine/params.py +258 -0
  110. nablaengine/provenance.py +196 -0
  111. nablaengine/reference/__init__.py +0 -0
  112. nablaengine/reference/adjoint_groups.py +226 -0
  113. nablaengine/reference/jax64.py +690 -0
  114. nablaengine/reference/warp_physics.py +262 -0
  115. nablaengine/sensors/__init__.py +4 -0
  116. nablaengine/sensors/cameras.py +285 -0
  117. nablaengine/sensors/data/LIVOX_MID360_LICENSE +21 -0
  118. nablaengine/sensors/data/livox_mid360.npz +0 -0
  119. nablaengine/sensors/g1.py +521 -0
  120. nablaengine/sensors/jax_rays.py +364 -0
  121. nablaengine/sensors/optix_rays.py +725 -0
  122. nablaengine/sensors/raycore.py +630 -0
  123. nablaengine/sensors/raygrad.py +374 -0
  124. nablaengine/sensors/raygrad_cuda.py +191 -0
  125. nablaengine/sensors/robot_mesh.py +273 -0
  126. nablaengine/sensors/torch_rays.py +275 -0
  127. nablaengine/terrain.py +424 -0
  128. nablaengine/torch_ffi.py +1328 -0
  129. nablaengine/workdir.py +37 -0
  130. nablaengine-1.0.2.dist-info/METADATA +236 -0
  131. nablaengine-1.0.2.dist-info/RECORD +136 -0
  132. nablaengine-1.0.2.dist-info/WHEEL +5 -0
  133. nablaengine-1.0.2.dist-info/licenses/LICENSE +202 -0
  134. nablaengine-1.0.2.dist-info/licenses/NOTICE +23 -0
  135. nablaengine-1.0.2.dist-info/licenses/THIRD_PARTY_NOTICES.md +176 -0
  136. nablaengine-1.0.2.dist-info/top_level.txt +1 -0
@@ -0,0 +1,163 @@
1
+ """More than 32 bodies in the forest engine (capability T1: at most 64, the world included)."""
2
+ import numpy as np
3
+
4
+ STALE_ASSERT = 'static_assert(NB <= 31, "the contact reduction: lanes 0 and 31 take two of the root rotation dofs");'
5
+ NOTE = " // (nb64: NB > 32)"
6
+
7
+
8
+ def needed(m):
9
+ return m.nbody > 32
10
+
11
+
12
+ def ancmask_packed(m, mask):
13
+ """(c_ancmask packed for host.loader's "u" kind as float32, its layout entry): one 32-bit word per body, two
14
+ (low, high: the device's unsigned long long) for more than 32 bodies."""
15
+ if m.nbody <= 32:
16
+ mask = np.asarray(mask, np.int64)
17
+ return np.stack([mask & 0xFFFF, mask >> 16], 1).ravel().astype(np.float32), ("c_ancmask", "u", "nb", 1)
18
+ mask = np.asarray([int(x) & 0xFFFFFFFFFFFFFFFF for x in mask], np.uint64)
19
+ words = np.stack([mask & np.uint64(0xFFFFFFFF), mask >> np.uint64(32)], 1).ravel()
20
+ halves = np.stack([words & np.uint64(0xFFFF), words >> np.uint64(16)], 1).ravel()
21
+ return halves.astype(np.float32), ("c_ancmask", "u", "nb", 2)
22
+
23
+
24
+ FORWARD = (
25
+ ("DKC unsigned int c_ancmask[NB];\n", "DKC unsigned long long c_ancmask[NB];" + NOTE + "\n", 1),
26
+ (" if (d == L) {\n float Qp[4], Xp[3], Wp[3], Vp[3], ALp[3], Ap[3], Qb[4], Xb[3], Wb[3], Vb[3], ALb[3], Ab[3];\n",
27
+ " for (int b = lane; b < NB; b += 32) if (c_depth[b] == L) { // (nb64: NB > 32, bodies lane and lane + 32)\n"
28
+ " const int p = c_parent[b];\n"
29
+ " float Qp[4], Xp[3], Wp[3], Vp[3], ALp[3], Ap[3], Qb[4], Xb[3], Wb[3], Vb[3], ALb[3], Ab[3];\n", 1),
30
+ (" const int rt = (b >= 1 && b < NB) ? c_root[b] : 1; // the body's tree root, whose origin is the tree's point O\n"
31
+ " const float O[3] = {s.X[rt][0], s.X[rt][1], s.X[rt][2]};\n"
32
+ " if (b >= 1 && b < NB) {\n",
33
+ " for (int b = lane; b < NB; b += 32) if (b >= 1) {" + NOTE + "\n"
34
+ " const int rt = c_root[b]; // the body's tree root, whose origin is the tree's point O\n"
35
+ " const float O[3] = {s.X[rt][0], s.X[rt][1], s.X[rt][2]};\n", 1),
36
+ (" if (d == L && c_nchild[b] > 0) {\n int nch = c_nchild[b];\n",
37
+ " for (int b = lane; b < NB; b += 32) if (c_depth[b] == L && c_nchild[b] > 0) {" + NOTE + "\n"
38
+ " int nch = c_nchild[b];\n", 1),
39
+ (" if (b >= 1 && b < NB) torques(b, s.Q[b], s.F[b], s.N[b], s.qsm); // qsm holds the bias for now\n"
40
+ " float sw[3], sv[3], kw[3], kv[3], n[3], f[3];\n"
41
+ " const float q1[4] = {s.Q[rt][0], s.Q[rt][1], s.Q[rt][2], s.Q[rt][3]}; // the root's rotation (its dofs' subspace)\n"
42
+ " const bool rfree = b >= 1 && b < NB && JT_OF(rt) == JFREE;\n"
43
+ " if (b >= 1 && b < NB && JT_OF(b) != JFREE) {\n",
44
+ " for (int b = lane; b < NB; b += 32) if (b >= 1) torques(b, s.Q[b], s.F[b], s.N[b], s.qsm); // (nb64)\n"
45
+ " for (int b = lane; b < NB; b += 32) if (b >= 1) { // (nb64: NB > 32, the rows of bodies lane and lane + 32)\n"
46
+ " const int rt = c_root[b];\n"
47
+ " const float O[3] = {s.X[rt][0], s.X[rt][1], s.X[rt][2]};\n"
48
+ " float sw[3], sv[3], kw[3], kv[3], n[3], f[3];\n"
49
+ " const float q1[4] = {s.Q[rt][0], s.Q[rt][1], s.Q[rt][2], s.Q[rt][3]}; // the root's rotation (its dofs' subspace)\n"
50
+ " const bool rfree = JT_OF(rt) == JFREE;\n"
51
+ " if (JT_OF(b) != JFREE) {\n", 1),
52
+ (" if (b >= 1 && b < NB && JT_OF(b) == JFREE) {\n float J1[10];\n",
53
+ " if (JT_OF(b) == JFREE) {\n float J1[10];\n", 1),
54
+ (" s.M[TRI(r0 + j, r0 + i)] = dot3(kw, n) + dot3(kv, f) + (i == j ? c_armature[r0 + j] : 0.f);\n"
55
+ " }\n }\n }\n __syncwarp();\n",
56
+ " s.M[TRI(r0 + j, r0 + i)] = dot3(kw, n) + dot3(kv, f) + (i == j ? c_armature[r0 + j] : 0.f);\n"
57
+ " }\n }\n }\n } // (nb64)\n __syncwarp();\n", 1),
58
+ ("__device__ __forceinline__ unsigned long long support_mask(unsigned x) {",
59
+ "__device__ __forceinline__ unsigned long long support_mask(unsigned long long x) {" + NOTE, 1),
60
+ (" unsigned am1[CPL], am2[CPL];\n", " unsigned long long am1[CPL], am2[CPL];" + NOTE + "\n", 1),
61
+ (" unsigned a1 = am1[0], a2 = am2[0];\n", " unsigned long long a1 = am1[0], a2 = am2[0];" + NOTE + "\n", 1),
62
+ (" unsigned m1 = __shfl_sync(0xffffffffu, a1, c & 31);\n unsigned m2 = __shfl_sync(0xffffffffu, a2, c & 31);\n",
63
+ " unsigned long long m1 = __shfl_sync(0xffffffffu, a1, c & 31);" + NOTE + "\n"
64
+ " unsigned long long m2 = __shfl_sync(0xffffffffu, a2, c & 31);\n", 1),
65
+ )
66
+
67
+ ADJOINT = (
68
+ (" if (d == L) {\n const float *Qp = s.Q[p], *Xp = s.X[p];\n",
69
+ " for (int b = lane; b < NB; b += 32) if (c_depth[b] == L) {" + NOTE + "\n"
70
+ " const int p = c_parent[b];\n const float *Qp = s.Q[p], *Xp = s.X[p];\n", 1),
71
+ (" float r[3];\n for (int k = 0; k < 3; k++) r[k] = b < NB ? s.X[b][k] - s.X[p][k] : 0.f;\n"
72
+ " const float sa = s.at[i], sl = lam[i], sy = ny[i];\n for (int L = 1; L <= MAXD; L++) {\n if (d == L) {\n",
73
+ " for (int L = 1; L <= MAXD; L++) {\n"
74
+ " for (int b = lane; b < NB; b += 32) if (c_depth[b] == L) {" + NOTE + "\n"
75
+ " const int p = c_parent[b], i = c_dadr[b];\n"
76
+ " float r[3];\n for (int k = 0; k < 3; k++) r[k] = s.X[b][k] - s.X[p][k];\n"
77
+ " const float sa = s.at[i], sl = lam[i], sy = ny[i];\n", 1),
78
+ (" if (b >= 1 && b < NB) {\n float ip[3];\n body_ipos(b, s.com, ip);\n body_local_vjp(",
79
+ " for (int b = lane; b < NB; b += 32) if (b >= 1) {" + NOTE + "\n float ip[3];\n body_ipos(b, s.com, ip);\n"
80
+ " body_local_vjp(", 1),
81
+ (" if (d == L) {\n float bW[3], bAL[3], bA[3], bAL2[3], bA2[3], bWl[3], bVl[3], bWy[3], bVy[3], F[3], N[3], t[3], u[3];\n",
82
+ " for (int b = lane; b < NB; b += 32) if (c_depth[b] == L) {" + NOTE + "\n const int p = c_parent[b];\n"
83
+ " float bW[3], bAL[3], bA[3], bAL2[3], bA2[3], bWl[3], bVl[3], bWy[3], bVy[3], F[3], N[3], t[3], u[3];\n", 1),
84
+ (" const unsigned m1 = c_ancmask[b1], m2 = c_ancmask[b2], sup = m1 ^ m2;",
85
+ " const unsigned long long m1 = c_ancmask[b1], m2 = c_ancmask[b2], sup = m1 ^ m2;" + NOTE, 1),
86
+ (" unsigned hb = sup & ~1u;", " unsigned long long hb = sup & ~1ull;", 1),
87
+ (" const int bi = __ffs(hb) - 1;\n hb &= hb - 1u;\n", " const int bi = __ffsll(hb) - 1;\n hb &= hb - 1ull;\n", 1),
88
+ (" const unsigned m1 = c_ancmask[c_gbody[PG1(pair)]], m2 = c_ancmask[c_gbody[PG2(pair)]];",
89
+ " const unsigned long long m1 = c_ancmask[c_gbody[PG1(pair)]], m2 = c_ancmask[c_gbody[PG2(pair)]];" + NOTE, 1),
90
+ )
91
+
92
+ SURROGATE = (
93
+ (" const unsigned m1 = c_ancmask[b1], m2 = c_ancmask[b2];\n unsigned hb = (m1 ^ m2) & ~1u;\n#pragma unroll 1\n"
94
+ " while (hb) {\n const int bi = __ffs(hb) - 1;\n hb &= hb - 1u;\n",
95
+ " const unsigned long long m1 = c_ancmask[b1], m2 = c_ancmask[b2]; // (nb64: NB > 32)\n"
96
+ " unsigned long long hb = (m1 ^ m2) & ~1ull;\n#pragma unroll 1\n"
97
+ " while (hb) {\n const int bi = __ffsll(hb) - 1;\n hb &= hb - 1ull;\n", 1),
98
+ (" const unsigned m1 = c_ancmask[c1], m2 = c_ancmask[c2];\n",
99
+ " const unsigned long long m1 = c_ancmask[c1], m2 = c_ancmask[c2]; // (nb64: NB > 32)\n", 1),
100
+ )
101
+
102
+ from nablaengine.g1_engine.model_hooks import ADJ_DEBUG_NB64 as ADJ_DEBUG
103
+
104
+ RED_SETUP = " const bool body = lane >= 1 && lane < NB;\n"
105
+ RED_SETUP_END = " int ntri = 0, nquad = 0;"
106
+ RED_START = " float f[3] = {0.f, 0.f, 0.f}, t[3] = {0.f, 0.f, 0.f}, pt[3] = {0.f, 0.f, 0.f}, pv[3] = {0.f, 0.f, 0.f};\n"
107
+ RED_END = " __syncwarp();\n APROF(APROF_CONTACT_REDUCE);\n"
108
+
109
+
110
+ def _indent(s, n=2):
111
+ return "".join((" " * n + ln if ln.strip() else ln) for ln in s.splitlines(True))
112
+
113
+
114
+ def reduction(src):
115
+ """adj_contacts' body reduction as two passes over the batch (pass h: the bodies lane + 32 h): the lane's body
116
+ setup (body, rb, jt, di, zl) moves from before the batch loop into each pass."""
117
+ for a, n in ((RED_SETUP, 1), (RED_START, 1), (RED_END, 1)):
118
+ if src.count(a) != n:
119
+ raise RuntimeError(f"nb64 reduction anchor found {src.count(a)} times, expected {n}: {a[:70]!r}")
120
+ i = src.index(RED_SETUP)
121
+ j = src.index(RED_SETUP_END, i)
122
+ setup = src[i:j]
123
+ k = src.index(RED_START, j)
124
+ e = src.index(RED_END, k)
125
+ body = src[k:e]
126
+ s2 = setup.replace(RED_SETUP, " const int bl = lane + 32 * h;\n const bool body = bl >= 1 && bl < NB;\n", 1)
127
+ old = " const int rb = body ? lane : 1,"
128
+ if s2.count(old) != 1:
129
+ raise RuntimeError("nb64 reduction: the lane's body line not found")
130
+ s2 = s2.replace(old, " const int rb = body ? bl : 1,", 1)
131
+ new = (" for (int h = 0; h < 2; h++) { // (nb64: NB > 32) pass h: the bodies lane + 32 h (the whole warp: shuffles)\n"
132
+ + _indent(s2, 4) + _indent(body, 2) + " }\n")
133
+ return src[:i] + src[j:k] + new + src[e:]
134
+
135
+
136
+ def _apply(src, hooks):
137
+ for a, b, n in hooks:
138
+ if src.count(a) != n:
139
+ raise RuntimeError(f"nb64 hook anchor found {src.count(a)} times, expected {n}: {a[:70]!r}")
140
+ src = src.replace(a, b)
141
+ return src
142
+
143
+
144
+ def apply(src, m, part="engine"):
145
+ """src (part "engine": adjoint.cuda_source's text after every other edit; "kinematics": the kinematics text before
146
+ the others) with the edits for more than 32 bodies; unchanged for at most 31 bodies (32: the adjoint's assert)."""
147
+ if part == "kinematics":
148
+ if not needed(m):
149
+ return src
150
+ from nablaengine.g1_engine import model_hooks as g1_hooks
151
+ return _apply(src, g1_hooks.kin_nb64_hooks())
152
+ if m.nbody < 32:
153
+ return src
154
+ if m.nbody == 32:
155
+ return _apply(src, ((STALE_ASSERT, 'static_assert(NB <= 32, "the contact reduction: one body per lane '
156
+ '(nb64: lane 0 the world)");', 1),))
157
+ if "adj_sur_hessian(" in src:
158
+ src = _apply(src, SURROGATE)
159
+ src = _apply(src, ((STALE_ASSERT, 'static_assert(NB <= 64, "the contact reduction: two passes, the bodies lane and '
160
+ 'lane + 32 (nb64)");', 1),) + FORWARD + ADJOINT)
161
+ if "#ifdef DK_ADJ_DEBUG" in src and "__device__ void adj_residual(" in src:
162
+ src = _apply(src, ADJ_DEBUG)
163
+ return reduction(src)