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,199 @@
1
+ """The forward tape of the G1 engine: one record per world and substep, the residuals of the analytic adjoint."""
2
+
3
+ import numpy as np
4
+
5
+ from nablaengine.forest_engine import step
6
+
7
+ ALIGN = 32
8
+ FLAG_UNSUPPORTED = 31
9
+ FLAG_REFACT = 1 << 8
10
+ FLAG_NONCONV = 1 << 9
11
+ FLAG_SINGULAR = 1 << 10
12
+ FLAG_VERIFY_DIFF = 1 << 11
13
+ FLAG_LQ_TREE = 1 << 12
14
+ FLAG_LH_SPARSE = 1 << 13
15
+ FLAG_H_LOWRANK = 1 << 14
16
+ FLAG_H_DENSE = 1 << 15
17
+ SINGULAR_NU = 5e-3
18
+ CON_NONE = 0xFFFFFFFF
19
+
20
+ _FIELDS = (
21
+ ("hdr", "OFF_HDR", 1),
22
+ ("flags", "OFF_FLAGS", 1),
23
+ ("min_nu", "OFF_MIN_NU", 1),
24
+ ("reserved", "OFF_RESERVED", 1),
25
+ ("clamp", "OFF_CLAMP", lambda nq, nv, ntri, nc, nef: -(-nv // 32)),
26
+ ("kvoff", "OFF_KVOFF", lambda nq, nv, ntri, nc, nef: -(-nv // 32)),
27
+ ("uclamp", "OFF_UCLAMP", lambda nq, nv, ntri, nc, nef: -(-nv // 32)),
28
+ ("zone", "OFF_ZONE", lambda nq, nv, ntri, nc, nef: -(-nef // 32)),
29
+ ("con", "OFF_CON", lambda nq, nv, ntri, nc, nef: nc),
30
+ ("qpos", "OFF_QPOS", lambda nq, nv, ntri, nc, nef: nq),
31
+ ("qvel", "OFF_QVEL", lambda nq, nv, ntri, nc, nef: nv),
32
+ ("qacc", "OFF_QACC", lambda nq, nv, ntri, nc, nef: nv),
33
+ ("qacc_adv", "OFF_QADV", lambda nq, nv, ntri, nc, nef: nv),
34
+ ("kvw", "OFF_KVW", lambda nq, nv, ntri, nc, nef: nv),
35
+ ("mslope", "OFF_MSLOPE", lambda nq, nv, ntri, nc, nef: nv),
36
+ (None, None, ALIGN),
37
+ ("L_H", "OFF_LH", lambda nq, nv, ntri, nc, nef: ntri),
38
+ (None, None, ALIGN),
39
+ ("L_Q", "OFF_LQ", lambda nq, nv, ntri, nc, nef: ntri),
40
+ (None, None, ALIGN),
41
+ )
42
+
43
+
44
+ _TERRAIN_FIELDS = (("telem", "OFF_TELEM", lambda nq, nv, ntri, nc, nef: -(-nc // 2)),)
45
+ _NATIVE_FIELDS = (("taux", "OFF_TAUX", lambda nq, nv, ntri, nc, nef: nc),)
46
+ _NOSLIP_FIELDS = (
47
+ ("ns_qn", "OFF_NSQN", lambda nq, nv, ntri, nc, nef: nv),
48
+ ("ns_hdr", "OFF_NSHDR", 1),
49
+ ("ns_st", "OFF_NSST", lambda nq, nv, ntri, nc, nef: nc),
50
+ ("ns_con", "OFF_NSCON", lambda nq, nv, ntri, nc, nef: 13 * nc),
51
+ ("ns_f", "OFF_NSF", lambda nq, nv, ntri, nc, nef: nef),
52
+ ("ns_df", "OFF_NSDF", lambda nq, nv, ntri, nc, nef: nef),
53
+ ("ns_jar", "OFF_NSJAR", lambda nq, nv, ntri, nc, nef: nef),
54
+ (None, None, ALIGN),
55
+ ("L_M", "OFF_LM", lambda nq, nv, ntri, nc, nef: ntri),
56
+ (None, None, ALIGN),
57
+ )
58
+
59
+
60
+ def _table(m, max_contacts=None, max_rows=None, motor=False, terrain=False):
61
+ nc = max_contacts or step.maxcon(m)
62
+ nv, ntri = m.nv, m.nv * (m.nv + 1) // 2
63
+ nlim = int(sum(m.jnt_limited[j] for j in range(m.njnt) if m.jnt_type[j] in (2, 3)))
64
+ assert nv <= 128 and m.npair < 65536 and nc <= 255 and step.nefc(nc, m, max_rows) < 1024 and (nlim < 64 or nv > 64), \
65
+ "field widths (pair 16 bits, ncon 8, nefc 10, nlim 6 bits)"
66
+ fields = _FIELDS
67
+ if step.clamp_implicit()[0] != 2:
68
+ fields = tuple(f for f in fields if f[0] != "kvw")
69
+ if not motor:
70
+ fields = tuple(f for f in fields if f[0] != "mslope")
71
+ from nablaengine.forest_engine import tendon as _ten
72
+ if _ten.applies(m) and _ten.spatial_build(m):
73
+ i = [f[0] for f in fields].index("qvel") + 1
74
+ from nablaengine.forest_engine import muscle
75
+ nta = max(len(_ten.actuators(m)), 1)
76
+ fields = fields[:i] + (("tact", "OFF_TACT", (2 + (muscle.NDER if muscle.applies(m) else 0)) * nta),) + fields[i:]
77
+ if terrain:
78
+ i = [f[0] for f in fields].index("con") + 1
79
+ fields = fields[:i] + _TERRAIN_FIELDS + (_NATIVE_FIELDS if terrain == "native" else ()) + fields[i:]
80
+ if int(m.opt.noslip_iterations) > 0:
81
+ fields = fields + _NOSLIP_FIELDS
82
+ st = _storage(m)
83
+ out, off = [], 0
84
+ for name, define, n in fields:
85
+ if name is None:
86
+ off = -(-off // n) * n
87
+ continue
88
+ n = n(m.nq, nv, ntri, nc, step.nefc(nc, m, max_rows)) if callable(n) else n
89
+ if st is not None and name in ("L_H", "L_Q"):
90
+ n = st["nnz_lh" if name == "L_H" else "nnz_lq"]
91
+ out.append((name, define, off, n))
92
+ off += n
93
+ return out, off
94
+
95
+
96
+ def _storage(m):
97
+ if m.nv <= 64:
98
+ return None
99
+ from nablaengine.forest_engine import nv128
100
+ return nv128.storage(m)
101
+
102
+
103
+ def kind(terrain):
104
+ """The record kind of a build: False (no terrain), "analytic" or "native" (terrain: (TerrainConfig, geoms), a
105
+ TerrainConfig or None)."""
106
+ if terrain is None:
107
+ return False
108
+ cfg = terrain[0] if isinstance(terrain, tuple) else terrain
109
+ return "native" if cfg.mode == "native" else "analytic"
110
+
111
+
112
+ def layout(m, max_contacts=None, terrain=False, max_rows=None, motor=False):
113
+ """{name: (offset, length)} of a record in 32-bit words (mjlab model: L_H at 192, L_Q at 832, 1472 words). motor:
114
+ a motor-limit build (nablaengine.motor: the mslope field). terrain: a terrain build's record kind (kind())."""
115
+ return {name: (off, n) for name, _, off, n in _table(m, max_contacts, max_rows, motor, terrain)[0]}
116
+
117
+
118
+ def rec_words(m, max_contacts=None, terrain=False, max_rows=None, motor=False):
119
+ """Record length in words (a multiple of ALIGN)."""
120
+ return _table(m, max_contacts, max_rows, motor, terrain)[1]
121
+
122
+
123
+ def header(m, max_contacts=None, terrain=False, max_rows=None, motor=False):
124
+ """#define OFF_* / REC_WORDS / TAPE_* for the engine's CUDA code (step.py)."""
125
+ tab, total = _table(m, max_contacts, max_rows, motor, terrain)
126
+ lines = [f"#define REC_WORDS {total} // tape record (tape.py): words per world and substep"]
127
+ lines += [f"#define {define} {off} // {name}, {n} words" for name, define, off, n in tab]
128
+ lines += [f"#define TAPE_REFACT {FLAG_REFACT}u", f"#define TAPE_NONCONV {FLAG_NONCONV}u",
129
+ f"#define TAPE_SINGULAR {FLAG_SINGULAR}u", f"#define TAPE_VERIFY_DIFF {FLAG_VERIFY_DIFF}u",
130
+ f"#define TAPE_SINGULAR_NU {SINGULAR_NU}f", f"#define TAPE_CON_NONE {CON_NONE}u"]
131
+ return "\n" + "\n".join(lines) + "\n"
132
+
133
+
134
+ def tri_unpack(packed, n):
135
+ """(..., n (n + 1) / 2) packed lower triangles TRI(i, j) -> (..., n, n) lower-triangular matrices."""
136
+ packed = np.asarray(packed)
137
+ i, j = np.tril_indices(n)
138
+ out = np.zeros(packed.shape[:-1] + (n, n), packed.dtype)
139
+ out[..., i, j] = packed[..., i * (i + 1) // 2 + j]
140
+ return out
141
+
142
+
143
+ def decode(arr, m, max_contacts=None, max_rows=None, motor=False, terrain=False):
144
+ """Records (..., REC_WORDS) float32 -> dict of arrays (leading shape kept): ncon, nefc, nlim, niter, flags (and
145
+ unsupported / refact / nonconv / singular / verify_diff / lq_tree: L_Q = R, Q = R' R, the tree factor / lh_sparse: L_H = R on P_H, nv128
146
+ the sparse Newton factor), min_nu,
147
+ clamp (u64 dof mask) and clamp_dof (bool, nv), zone (bool, step.nefc), pair / slot (int, maxcon; pair -1 past
148
+ ncon), qpos, qvel, qacc, qacc_adv, L_H, L_Q (packed triangles; the pattern storage records, whose L_Q / L_H hold only
149
+ their patterns' entries, expanded with zeros elsewhere);
150
+ motor: a motor-limit build's record, with mslope (-df/dqdot per dof); terrain: a terrain build's record kind
151
+ (kind()), with elem (the contact's terrain element) and, native, taux."""
152
+ a = np.ascontiguousarray(arr, np.float32)
153
+ nw = rec_words(m, max_contacts, terrain, max_rows=max_rows, motor=motor)
154
+ assert a.shape[-1] == nw, (a.shape, nw)
155
+ u = a.view(np.uint32)
156
+ lay = layout(m, max_contacts, terrain, max_rows=max_rows, motor=motor)
157
+ sl = lambda name: slice(lay[name][0], lay[name][0] + lay[name][1])
158
+ hdr, flags = u[..., lay["hdr"][0]].astype(np.int64), u[..., lay["flags"][0]].astype(np.int64)
159
+ nlim = u[..., lay["reserved"][0]].astype(np.int64) if m.nv > 64 else (hdr >> 18) & 63
160
+ out = dict(ncon=hdr & 255, nefc=(hdr >> 8) & 1023, nlim=nlim, niter=hdr >> 24, flags=flags,
161
+ unsupported=flags & FLAG_UNSUPPORTED, refact=(flags & FLAG_REFACT) != 0,
162
+ nonconv=(flags & FLAG_NONCONV) != 0, singular=(flags & FLAG_SINGULAR) != 0,
163
+ verify_diff=(flags & FLAG_VERIFY_DIFF) != 0, lq_tree=(flags & FLAG_LQ_TREE) != 0,
164
+ lh_sparse=(flags & FLAG_LH_SPARSE) != 0, h_lowrank=(flags & FLAG_H_LOWRANK) != 0,
165
+ h_dense=(flags & FLAG_H_DENSE) != 0,
166
+ min_nu=a[..., lay["min_nu"][0]])
167
+ cw = u[..., sl("clamp")].astype(np.uint64)
168
+ out["clamp"] = sum(cw[..., k] << np.uint64(32 * k) for k in range(cw.shape[-1]))
169
+ out["clamp_dof"] = ((cw[..., :, None] >> np.arange(32, dtype=np.uint64)) & np.uint64(1)).astype(bool).reshape(
170
+ cw.shape[:-1] + (-1,))[..., :m.nv]
171
+ zw = u[..., sl("zone")].astype(np.uint64)
172
+ out["zone"] = ((zw[..., :, None] >> np.arange(32, dtype=np.uint64)) & np.uint64(1)).astype(bool).reshape(
173
+ zw.shape[:-1] + (-1,))
174
+ nc = max_contacts or step.maxcon(m)
175
+ out["zone"] = out["zone"][..., :step.nefc(nc, m, max_rows)]
176
+ e = u[..., sl("con")].astype(np.int64)
177
+ past = np.arange(nc) >= out["ncon"][..., None]
178
+ out["pair"], out["slot"] = np.where(past, -1, e & 0xFFFF), np.where(past, -1, e >> 16)
179
+ if terrain:
180
+ x = np.ascontiguousarray(u[..., sl("telem")]).view(np.uint16)[..., :nc].astype(np.int64)
181
+ out["elem"] = np.where(past, -1, x)
182
+ if terrain == "native":
183
+ out["taux"] = np.where(past, 0, u[..., sl("taux")].astype(np.int64))
184
+ for name in ("qpos", "qvel", "qacc", "qacc_adv", "L_H", "L_Q") + (("mslope",) if motor else ()):
185
+ out[name] = a[..., sl(name)]
186
+ st = _storage(m)
187
+ if st is not None:
188
+ ntri = m.nv * (m.nv + 1) // 2
189
+ for name, lay_ in (("L_Q", st["tl"]), ("L_H", st["cl"])):
190
+ if lay_ is not None:
191
+ full = np.zeros(out[name].shape[:-1] + (ntri,), np.float32)
192
+ full[..., np.asarray(lay_["tri"])] = out[name]
193
+ out[name] = full
194
+ if "ns_qn" in lay:
195
+ nh = u[..., lay["ns_hdr"][0]].astype(np.int64)
196
+ out.update(ns_qacc_newton=a[..., sl("ns_qn")], ns_iter=nh & 255, ns_flags=nh & ~255,
197
+ ns_status=u[..., sl("ns_st")].astype(np.int64), ns_force=a[..., sl("ns_f")], L_M=a[..., sl("L_M")],
198
+ ns_con=a[..., sl("ns_con")].reshape(a.shape[:-1] + (nc, 13)))
199
+ return out