Files
SkillFactory/tests/test_contact_cycle_performance_v042.py
chenlin c528dd268f feat: checkpoint Go2 FL simulation performance v0.4.2
Include callable skill and live-GUI prerequisites, epoch sensor reads, packed snapshots, sampled bit-exact kinematics, and verified-tick rendering without changing the physical/control schedule.

Record bounded analytic S1 GUI/headless qualification, original failures and CPU evidence (1557 passed, 6 skipped). Preserve accepted_skill=false and sub-real-time limitations. Exclude native authorities, caches, large media and local delivery copies; retain sealed artifact digest.
2026-10-16 15:46:01 +08:00

167 lines
7.6 KiB
Python

"""CPU-only equivalence and fail-closed tests; not GPU/FPS qualification."""
import ast
import copy
import importlib.util
import json
import sys
from collections import defaultdict
from pathlib import Path
from types import SimpleNamespace as NS
import pytest
import torch
from test_contact_cycle_sim_evaluation_runtime import runtime, make_env
ROOT = Path(__file__).resolve().parents[1]
TASK = ROOT / 'source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim'
def standalone(name):
spec = importlib.util.spec_from_file_location('_perf_test_' + name, TASK / (name + '.py'))
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
def copy_function():
tree = ast.parse((TASK / 'packed_recording_v042.py').read_text())
function = next(n for n in tree.body if isinstance(n, ast.FunctionDef) and n.name == 'copy_snapshot')
ns = {'torch': torch, 'defaultdict': defaultdict}
exec(compile(ast.Module(body=[function], type_ignores=[]), '<actual_copy_snapshot>', 'exec'), ns)
return ns['copy_snapshot']
@pytest.mark.parametrize('group_cpu', [False, True])
def test_packing_retains_exact_dtype_shape_bits_order_and_owned_storage(group_cpu):
source = {'f32': torch.tensor([[0., -0., float('nan')], [float('inf'), 1.25, -2.]]).T,
'f64': torch.tensor([1.123456789012345], dtype=torch.float64),
'integer': torch.tensor([2**60 + 7, -2**60 + 9], dtype=torch.int64),
'bool': torch.tensor([True, False]), 'empty': torch.empty(0, 3),
'scalar': torch.tensor(5., dtype=torch.float32)}
source['alias'] = source['f32']
out = copy_function()(source, group_cpu=group_cpu)
assert list(out) == list(source)
for k, v in source.items():
assert out[k].shape == v.shape and out[k].dtype == v.dtype and out[k].device.type == 'cpu'
assert out[k].numpy().tobytes() == v.numpy().tobytes()
before = {k: v.clone() for k, v in out.items()}
source['integer'].zero_();source['f32'].zero_()
for k, v in before.items():
assert out[k].numpy().tobytes() == v.numpy().tobytes()
out['alias'].fill_(42)
assert out['f32'].numpy().tobytes() == before['f32'].numpy().tobytes()
def test_packing_rejects_non_tensor_and_transfer_error(monkeypatch):
f = copy_function()
with pytest.raises(TypeError):f({'not_tensor': 1})
def fail(*a, **k):raise RuntimeError('injected_pack_failure')
monkeypatch.setattr(torch, 'cat', fail)
with pytest.raises(RuntimeError, match='injected_pack_failure'):
f({'a': torch.ones(1)}, group_cpu=True)
def assignment(path, target):
tree = ast.parse(path.read_text())
method = next(n for n in ast.walk(tree) if isinstance(n, ast.FunctionDef) and n.name == 'snapshot')
return next(n.value for n in ast.walk(method) if isinstance(n, ast.Assign)
and any(isinstance(t, ast.Name) and t.id == target for t in n.targets))
def test_all_original_snapshot_expressions_are_identical_not_reinterpreted():
# The exact original expressions are checked, not another reimplementation.
assert ast.dump(assignment(TASK.parent / 'contact_cycle/recording.py', 'values')) == ast.dump(assignment(TASK / 'packed_recording_v042.py', 'values'))
assert ast.dump(assignment(TASK / 'recording.py', 'extra')) == ast.dump(assignment(TASK / 'packed_recording_v042.py', 'extra'))
class Sensor:
def __init__(self):self.value = {'generation': 0};self.reads = 0;self.fail = False;self.updates = []
@property
def data(self):
self.reads += 1
if self.fail:raise RuntimeError('sensor_read_failure')
return self.value
def update(self, dt, force_recompute=False):
self.updates.append((dt, force_recompute));self.value = {'generation': self.value['generation'] + 1}
if self.fail:raise RuntimeError('sensor_update_failure')
def reset(self, env_ids=None):
self.value = {'generation': 0}
if self.fail:raise RuntimeError('sensor_reset_failure')
def test_epoch_means_update_reset_not_global_tick_and_no_dropped_updates():
m = standalone('sensor_epoch_v042');source = Sensor();s = m.EpochSensor(source)
assert s.data is source.value
for _ in range(20):assert s.data is source.value
assert source.reads == 1
for dt, force in [(0., False), (.002, False), (0., True), (.002, True)]:
s.update(dt, force_recompute=force);assert s.data is source.value
assert s.data is source.value
assert source.reads == 5 and len(source.updates) == 4
s.reset();assert s.data is source.value and source.reads == 6
assert s.invalidations == 5
def test_failed_getter_does_not_cache_and_failed_update_invalidates():
m = standalone('sensor_epoch_v042');source = Sensor();s = m.EpochSensor(source);assert s.data
source.fail = True
with pytest.raises(RuntimeError, match='update'):s.update(.002)
for _ in range(2):
with pytest.raises(RuntimeError, match='read'):s.data
source.fail = False;assert s.data is source.value
source.fail = True
with pytest.raises(RuntimeError, match='reset'):s.reset()
with pytest.raises(RuntimeError, match='read'):s.data
def test_install_keeps_original_objects_and_rejects_double_install():
m = standalone('sensor_epoch_v042');original = Sensor()
class Scene:
sensors = {'contact': original}
def __getitem__(self, k):return self.sensors[k]
env = NS(scene=Scene());result = m.install(env)
assert env.scene['contact']._source is original
assert result['sensors'] == ['contact']
with pytest.raises(RuntimeError, match='once'):m.install(env)
def test_substep_two_keeps_offending_packed_frame_all_causes_and_no_render(make_env):
frames = [];pack = copy_function();renders = []
def evidence(event, s):
if event.kind != 'tick':return ()
f = pack({'value': torch.tensor([float('nan') if event.clock.global_tick == 2 else 1.])}, group_cpu=True)
frames.append(f)
return ('fault_one', 'fault_two') if not bool(torch.isfinite(f['value']).all()) else ()
env, hooks, _ = make_env(evidence=evidence)
env.cfg.sim.render_interval = 2;env.sim.is_rendering = True
env.sim.render = lambda **_: renders.append(True)
initial_calls = env.sim.calls # CPU fixture initialization has its own calls.
result = env.step(torch.zeros(1, 3))
assert result.stop.causes == ('fault_one', 'fault_two')
assert env.sim.calls - initial_calls == hooks.rollout_steps == 2 and len(frames) == 2 and not renders
assert bool(torch.isnan(frames[-1]['value']).all())
def test_transfer_exception_stops_before_third_step(make_env):
pack = copy_function();events = []
def evidence(event, s):
if event.kind == 'tick':
events.append(event.clock.global_tick)
if event.clock.global_tick == 2:pack({'bad': 1})
return ()
env, hooks, _ = make_env(evidence=evidence)
initial_calls = env.sim.calls
result = env.step(torch.zeros(1, 3))
assert env.sim.calls - initial_calls == hooks.rollout_steps == 2 and events == [1, 2]
assert any('recording_callback:TypeError' in x for x in result.stop.causes)
def test_options_and_loop_profile_do_not_step_or_change_render_interval(tmp_path):
m = standalone('performance_v042');p = tmp_path / m.CONFIG;p.parent.mkdir(parents=True)
p.write_text(json.dumps({'mode': 'baseline', 'profile': True}))
assert m.options(tmp_path)['mode'] == 'baseline'
p.write_text(json.dumps({'mode': 'nonsense', 'profile': True}))
with pytest.raises(ValueError):m.options(tmp_path)
p.write_text(json.dumps({'mode': 'baseline', 'profile': 1}))
with pytest.raises(ValueError):m.options(tmp_path)