mjbatch-uni 0.2.1__tar.gz → 0.2.2__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mjbatch-uni
3
- Version: 0.2.1
3
+ Version: 0.2.2
4
4
  Summary: A Python library for running thousands of MuJoCo simulations in parallel on CPU (unilabsim fork).
5
5
  Keywords: mujoco,physics,simulation,batched,reinforcement-learning
6
6
  Author-Email: Kevin Zakka <zakka@berkeley.edu>, Yufei Jia <jyf23@mails.tsinghua.edu.cn>
@@ -80,6 +80,18 @@ Acceleration-stage sensors and contact forces are not available before `mj_step2
80
80
  be stale in that view. The default callback path and all outputs are unchanged unless the
81
81
  optional range is passed.
82
82
 
83
+ After a normal `step()` with `forward=False`, selected position- and velocity-stage sensor
84
+ columns can be refreshed from the final integration state without a full `mj_forward`:
85
+
86
+ ```python
87
+ batch.refresh_sensor_range(ids, (sensor_start, sensor_stop))
88
+ ```
89
+
90
+ The refresh runs in native worker threads, copies only the requested columns into the bound
91
+ `sensordata` view, and leaves acceleration-stage sensors (including contact forces) and all
92
+ other bound fields unchanged. Disjoint ranges can be refreshed in one dispatch with
93
+ `refresh_sensor_ranges(ids, (start0, stop0, start1, stop1, ...))`.
94
+
83
95
  ## Model randomization and variants
84
96
 
85
97
  `expand(field)` returns a live `(num_sims, ...)` view of a non-asset `MjModel` field.
@@ -51,6 +51,18 @@ Acceleration-stage sensors and contact forces are not available before `mj_step2
51
51
  be stale in that view. The default callback path and all outputs are unchanged unless the
52
52
  optional range is passed.
53
53
 
54
+ After a normal `step()` with `forward=False`, selected position- and velocity-stage sensor
55
+ columns can be refreshed from the final integration state without a full `mj_forward`:
56
+
57
+ ```python
58
+ batch.refresh_sensor_range(ids, (sensor_start, sensor_stop))
59
+ ```
60
+
61
+ The refresh runs in native worker threads, copies only the requested columns into the bound
62
+ `sensordata` view, and leaves acceleration-stage sensors (including contact forces) and all
63
+ other bound fields unchanged. Disjoint ranges can be refreshed in one dispatch with
64
+ `refresh_sensor_ranges(ids, (start0, stop0, start1, stop1, ...))`.
65
+
54
66
  ## Model randomization and variants
55
67
 
56
68
  `expand(field)` returns a live `(num_sims, ...)` view of a non-asset `MjModel` field.
@@ -4,7 +4,7 @@ build-backend = "scikit_build_core.build"
4
4
 
5
5
  [project]
6
6
  name = "mjbatch-uni"
7
- version = "0.2.1"
7
+ version = "0.2.2"
8
8
  description = "A Python library for running thousands of MuJoCo simulations in parallel on CPU (unilabsim fork)."
9
9
  readme = "README.md"
10
10
  license = "Apache-2.0"
@@ -79,6 +79,16 @@ class Batch:
79
79
  """
80
80
 
81
81
  def forward(self, ids: Annotated[NDArray, dict(shape=(None,), order='C')] | None = None) -> None: ...
82
+ def refresh_sensor_range(
83
+ self,
84
+ ids: Annotated[NDArray, dict(shape=(None,), order='C')] | None = ...,
85
+ sensor_range: Sequence[int] = ...,
86
+ ) -> None: ...
87
+ def refresh_sensor_ranges(
88
+ self,
89
+ ids: Annotated[NDArray, dict(shape=(None,), order='C')] | None = ...,
90
+ sensor_ranges: Sequence[int] = ...,
91
+ ) -> None: ...
82
92
 
83
93
  def reset(self, ids: Annotated[NDArray, dict(shape=(None,), order='C')] | None = None, keyframe: int = -1) -> None:
84
94
  """
@@ -318,6 +318,8 @@ struct QueryCtx {
318
318
  int npoint = 0;
319
319
  HfieldAlign align = HfieldAlign::World; // SampleHfield: sampling grid rotation
320
320
  mjtNum* out = nullptr; // (sel, npoint) rows
321
+ Slot* sensor = nullptr; // RefreshSensor: bound sensordata slot
322
+ const std::vector<int>* sensor_ranges = nullptr; // flattened (start, stop) pairs
321
323
  };
322
324
 
323
325
  // One substep of a callback-driven step (Op::Substep): the calling thread
@@ -416,6 +418,7 @@ class Batch {
416
418
  Substep2, // split callback: apply writes and mj_step2
417
419
  Forward,
418
420
  Reset,
421
+ RefreshSensor,
419
422
  SetConst,
420
423
  JacSite,
421
424
  SampleHfield
@@ -522,6 +525,41 @@ class Batch {
522
525
  Run(Op::Forward, Parse(ids), 0);
523
526
  }
524
527
 
528
+ void RefreshSensorRanges(std::optional<Ids> ids, const std::vector<int>& sensor_ranges) {
529
+ ReentryGuard();
530
+ for (size_t i = 0; i < sensor_ranges.size(); i += 2) {
531
+ int start = sensor_ranges[i], stop = sensor_ranges[i + 1];
532
+ if (start < 0 || start >= stop || stop > template_->nsensordata) {
533
+ throw nb::value_error("sensor range entries must satisfy 0 <= start < stop <= nsensordata");
534
+ }
535
+ }
536
+ auto sel = Parse(ids);
537
+ Slot* sensor;
538
+ {
539
+ std::lock_guard<std::mutex> lock(mu_);
540
+ sensor = &BoundOrAdd(Field(data_fields_, "sensordata"), false);
541
+ }
542
+ QueryCtx ctx;
543
+ ctx.sensor = sensor;
544
+ ctx.sensor_ranges = &sensor_ranges;
545
+ Run(Op::RefreshSensor, std::move(sel), 0, nullptr, &ctx);
546
+ }
547
+
548
+ void refresh_sensor_range(std::optional<Ids> ids, std::optional<std::vector<int>> sensor_range) {
549
+ if (!sensor_range || sensor_range->size() != 2) {
550
+ throw nb::value_error("sensor_range must have shape (2,)");
551
+ }
552
+ RefreshSensorRanges(std::move(ids), *sensor_range);
553
+ }
554
+
555
+ void refresh_sensor_ranges(std::optional<Ids> ids,
556
+ std::optional<std::vector<int>> sensor_ranges) {
557
+ if (!sensor_ranges || sensor_ranges->empty() || sensor_ranges->size() % 2 != 0) {
558
+ throw nb::value_error("sensor_ranges must contain (start, stop) pairs");
559
+ }
560
+ RefreshSensorRanges(std::move(ids), *sensor_ranges);
561
+ }
562
+
525
563
  void reset(std::optional<Ids> ids, int keyframe) {
526
564
  ReentryGuard();
527
565
  if (keyframe < -1 || keyframe >= template_->nkey) {
@@ -908,6 +946,17 @@ class Batch {
908
946
  case Op::Reset:
909
947
  mj_forward(m, d);
910
948
  break;
949
+ case Op::RefreshSensor:
950
+ mj_kinematics(m, d);
951
+ mj_comPos(m, d);
952
+ mj_comVel(m, d);
953
+ mj_sensorPos(m, d);
954
+ mj_sensorVel(m, d);
955
+ for (int j = 0; j < static_cast<int>(ctx->sensor_ranges->size()); j += 2) {
956
+ CopySensorRange(*ctx->sensor, d, i, (*ctx->sensor_ranges)[j],
957
+ (*ctx->sensor_ranges)[j + 1] - (*ctx->sensor_ranges)[j]);
958
+ }
959
+ return;
911
960
  // The query ops run kinematics only: they do not change the integration
912
961
  // state, so the copy-out tail below is skipped. mjData is shared by every
913
962
  // sim a worker serves, and copying out would leak another sim's stale
@@ -99,6 +99,18 @@ handler installed at import; installing another handler later disables it.)")
99
99
  "so they affect the current substep. If the callback raises, state remains at "
100
100
  "the last completed substep; intermediate mjData is recovered on the next call.")
101
101
  .def("forward", &Batch::forward, "ids"_a.noconvert() = nb::none())
102
+ .def("refresh_sensor_range", &Batch::refresh_sensor_range, "ids"_a.noconvert() = nb::none(),
103
+ "sensor_range"_a,
104
+ "Refresh position- and velocity-stage sensors in a half-open sensordata column "
105
+ "range from each selected simulation's final integration state. The work runs "
106
+ "in native worker threads and only the declared columns are copied into the "
107
+ "bound sensordata view; acceleration-stage sensors (including contact forces) "
108
+ "and every other bound field are preserved.")
109
+ .def("refresh_sensor_ranges", &Batch::refresh_sensor_ranges, "ids"_a.noconvert() = nb::none(),
110
+ "sensor_ranges"_a,
111
+ "Refresh disjoint position- and velocity-stage sensor column ranges, expressed "
112
+ "as a flattened sequence of half-open (start, stop) pairs. Semantics match "
113
+ "refresh_sensor_range(); columns outside every requested range are preserved.")
102
114
  .def("reset", &Batch::reset, "ids"_a.noconvert() = nb::none(), "keyframe"_a = -1,
103
115
  "mj_resetData, or mj_resetDataKeyframe when keyframe >= 0, then mj_forward.")
104
116
  .def("jac_site", &Batch::jac_site, "site"_a, "jacp"_a.noconvert() = nb::none(),
File without changes
File without changes