diff --git a/README.md b/README.md index be501b4..ecc7478 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,10 @@ # SkillFactory v0.0.2 -仓库:[chenlin/SkillFactory](https://gitea.robotquan.com/chenlin/SkillFactory)。真实机器人部件资产导入、仿真控制与技能训练;当前主线为 **Go2 FL有辅助单腿接触周期仿真功能版0.4.2**,不是整机或实机技能。 +> **0.5.0四技能开发已按用户要求暂停(2026-10-16)**:仅WIP检查点,尚无合格训练候选/新版本验收;[进度与证据](docs/current_status.md)、[恢复前置](docs/next_stage.md)。不要自动使用原48小时剩余预算继续运行。 -## 当前基线 +仓库:[chenlin/SkillFactory](https://gitea.robotquan.com/chenlin/SkillFactory)。真实机器人部件资产导入、仿真控制与技能训练;上一已封包基线为 **Go2 FL有辅助单腿接触周期仿真功能版0.4.2**,不是整机或实机技能。 + +## 上一已封包基线(0.4.2) - 可调用Python/CLI:启动、查询、有限命令队列、安全停止;默认解析控制,可选冻结u256残差。 - 0.4.2实测解析S1 GUI/headless各6连续周期、27000tick,完整轨迹与0.4.1逐字节一致。GUI截图33.98FPS;物理推进仅GUI **0.07060×**、headless **0.18325×**,不是实时仿真。 @@ -16,7 +18,7 @@ 3. [0.4.2实测结果](docs/contact_cycle_sim_performance_v042_result.md) / [证据路径](docs/evidence_index.md)。 4. [软硬件描述v2](docs/software_hardware_spec.md):Schema与静态校验已实现,通用运行adapter尚未实现。 -独立已封包交付:`performance_v042/deliveries/go2_fl_contact_cycle_sim-0.4.2/`,真实GUI演示入口`bash demo_gui.sh`。运行会创建新的有限会话/输出,整理任务本身不启动仿真。训练默认RTX5080,CPU回归不等于物理验证;[执行边界](docs/execution_authority.md)。 +独立已封包交付:`performance_v042/deliveries/go2_fl_contact_cycle_sim-0.4.2/`,在该独立目录内运行`bash demo_gui.sh`。仓库根同名脚本现为未完成0.5.0开发入口,默认冻结权重尚未提供,不作为当前交付;旧根入口副本为`demo_gui_v042.sh`。运行会创建新的有限会话/输出,整理任务本身不启动仿真。训练默认RTX5080,CPU回归不等于物理验证;[执行边界](docs/execution_authority.md)。 ## 文件保留原则 diff --git a/archives/four_skills_v050_pause.json b/archives/four_skills_v050_pause.json new file mode 100644 index 0000000..2d19bea --- /dev/null +++ b/archives/four_skills_v050_pause.json @@ -0,0 +1,494 @@ +{ + "format": "skillfactory.four-skills-paused-progress.v1", + "status": "paused_by_user_not_accepted", + "paused_at": "2026-10-16T17:29:13+08:00", + "recorded_at": "2026-10-16T17:37:59.908314+08:00", + "base_commit": "37d1b03b1a97170014cfc58258a8a82da2f59fb2", + "current_development_revision": 2, + "qualified_candidate": false, + "revision2_native_validation": "not_run", + "independent_evaluation_seeds_executed": [], + "three_sequences_five_loops": "not_run", + "native_illegal_switch_and_airborne_stop_probes": "not_run", + "training_weights_status": "u218revision1 diagnostic only; incompatible config identity withrevision2; no resume permission", + "automatic_continuation": false, + "resume_requires_new_user_instruction": true, + "original_stage_grant_reusable": false, + "pause_check": { + "native_four_skill_processes_alive": false, + "gpu_compute_processes": [], + "pending_reviewer": "stopped" + }, + "reserved_launches_NOT_actual_Kit_instances": 9, + "reserved_physics_calls_NOT_actual_total": 640000, + "actual_total_physics_calls": null, + "actual_total_limitation": "Some failed/cancelled attempts lack final worker counters. Last progress is not a final total; no guessed aggregate.", + "verification": { + "current_new_cpu_suite": { + "passed": 139, + "failed": 0, + "seconds": 5.71, + "command": "/home/cen/miniconda3/envs/linker_sim_isaac/bin/python scripts/test_cpu.py tests/test_four_skills*.py" + }, + "earlier_full_retained_cpu_suite_before_latest_revision2_changes": { + "passed": 1726, + "skipped": 3, + "seconds": 115.59 + }, + "syntax_ast_and_bash": "passed", + "git_diff_check": "passed", + "prior_protected_files_unchanged": 363, + "last_revision2_independent_review": "stopped at user pause; not an approval", + "validation_during_pause_was_CPU_only": true + }, + "important_local_artifacts_not_in_git": [ + { + "path": "logs/four_skills_v050/training01/latest.pt", + "exists": true, + "bytes": 604987, + "sha256": "1937c363d2f9236caee832ae4ae599ca02b05b8c0d5e094e60ff30c641b9bbc7" + }, + { + "path": "logs/four_skills_v050/training01/source_snapshot/configs/four_skills/v050.json", + "exists": true, + "bytes": 3063, + "sha256": "2868f4cc72eadb8a3f539ce51962a888921a70c3355caba031be92cd6df6d684" + }, + { + "path": "logs/four_skills_v050/training01/source_snapshot/source/SkillFactory/SkillFactory/tasks/manager_based/four_skills/mdp.py", + "exists": true, + "bytes": 18807, + "sha256": "2c40efefbb4acd5272eade52025ed9d3845f52df43ebd688aec274cc049a652a" + }, + { + "path": "logs/four_skills_v050/training01/parent-intervention.json", + "exists": true, + "bytes": 443, + "sha256": "059eff8736c892f36096544489712c3c5d146103e8ae37262b3a3a02f2c88590" + }, + { + "path": "logs/four_skills_v050/gui-bootstrap02/live-kit-original-speed.mp4", + "exists": true, + "bytes": 7276434, + "sha256": "3c2c68e18c397ce0d2aaa059a42d425c5b79b9a2ba2fca0b671695a92b92ff84" + }, + { + "path": "logs/four_skills_v050/gui-bootstrap02/gui-evidence.json", + "exists": true, + "bytes": 6146, + "sha256": "dff5209df437a428889986532fad286d8154321d79274ed060187b7ae6f23905" + }, + { + "path": "logs/four_skills_v050/PAUSED.json", + "exists": true, + "bytes": 461, + "sha256": "812a5e5e36f89ba06ad1211a3f191d464e2c1459a4286bdbccb58e33bb911075" + } + ], + "native_data_distribution": "Original approximately602MiB under ignored logs/four_skills_v050; not committed. Source clone is not a complete runtime/evidence package.", + "new_default_demo_weights_present": false, + "next_steps_after_explicit_resume": [ + "Re-register bounded resumed scope, never silently revive old scope/grants.", + "Finish review and native revision2 analytic/GUI validation plus bounded training preflight.", + "Fresh GPU training, development candidate selection/freeze, untouched independent-seed paired evaluation.", + "Three directions/holds each5continuousloops; native illegal switch/airborne-safe-stop; genuine GUI/video/package acceptance." + ], + "attempts": [ + { + "name": "bootstrap01", + "mode": "smoke", + "development_revision": 1, + "output": "logs/four_skills_v050/bootstrap01", + "reserved_calls_no_refund": 15000, + "original_supervisor_success": false, + "original_worker_functional_success": null, + "original_worker_training_complete": null, + "owned_cleanup": true, + "waited_exit": true, + "native_returncode": 1, + "final_physics_calls": null, + "last_durable_completed_update": null, + "last_durable_training_environment_transitions": null, + "physics_calls_at_last_complete_update_NOT_final_total": null, + "reason_or_scope": "Private worker rejected parent identity before Kit startup.", + "receipts": [ + { + "path": "logs/four_skills_v050/bootstrap01/worker-result.json", + "exists": false, + "bytes": null, + "sha256": null + }, + { + "path": "logs/four_skills_v050/bootstrap01/supervisor-result.json", + "exists": true, + "bytes": 15987, + "sha256": "6f692b9e82febc5bcba872eb14f210fc61ce85f8db850235ac0df5bbc706767a" + }, + { + "path": "logs/four_skills_v050/bootstrap01/progress.jsonl", + "exists": false, + "bytes": null, + "sha256": null + }, + { + "path": "logs/four_skills_v050/bootstrap01/stdout.log", + "exists": true, + "bytes": 884, + "sha256": "89bf194d516358aafe2bd2cb0bb0d9fd89872a9e37761af2e9d6c27aa4895220" + } + ] + }, + { + "name": "bootstrap02", + "mode": "smoke", + "development_revision": 1, + "output": "logs/four_skills_v050/bootstrap02", + "reserved_calls_no_refund": 15000, + "original_supervisor_success": false, + "original_worker_functional_success": null, + "original_worker_training_complete": null, + "owned_cleanup": true, + "waited_exit": true, + "native_returncode": 1, + "final_physics_calls": null, + "last_durable_completed_update": null, + "last_durable_training_environment_transitions": null, + "physics_calls_at_last_complete_update_NOT_final_total": null, + "reason_or_scope": "Exact parent entry check rejected relative script path before Kit startup.", + "receipts": [ + { + "path": "logs/four_skills_v050/bootstrap02/worker-result.json", + "exists": false, + "bytes": null, + "sha256": null + }, + { + "path": "logs/four_skills_v050/bootstrap02/supervisor-result.json", + "exists": true, + "bytes": 15987, + "sha256": "12facf0ccd312858116e9c48d7217cbd2e648b12687dbc621941c5b81d878f47" + }, + { + "path": "logs/four_skills_v050/bootstrap02/progress.jsonl", + "exists": false, + "bytes": null, + "sha256": null + }, + { + "path": "logs/four_skills_v050/bootstrap02/stdout.log", + "exists": true, + "bytes": 922, + "sha256": "8b567f2887b88854c301feee4190b8c33e959cba8ac25328033d1ad22cd7162b" + } + ] + }, + { + "name": "bootstrap03", + "mode": "smoke", + "development_revision": 1, + "output": "logs/four_skills_v050/bootstrap03", + "reserved_calls_no_refund": 15000, + "original_supervisor_success": false, + "original_worker_functional_success": false, + "original_worker_training_complete": null, + "owned_cleanup": true, + "waited_exit": true, + "native_returncode": 1, + "final_physics_calls": null, + "last_durable_completed_update": null, + "last_durable_training_environment_transitions": null, + "physics_calls_at_last_complete_update_NOT_final_total": null, + "reason_or_scope": "Inherited reward configuration field missing, before environment construction.", + "receipts": [ + { + "path": "logs/four_skills_v050/bootstrap03/worker-result.json", + "exists": true, + "bytes": 1143, + "sha256": "ed7567b822caff9c5e3cc171548af4fb0b5817726d302cb6cdb0c739c190aaf9" + }, + { + "path": "logs/four_skills_v050/bootstrap03/supervisor-result.json", + "exists": true, + "bytes": 17125, + "sha256": "2f93b8b25929d6254e3adc660392a589afbed20753d3505d0e532d2ebedb3378" + }, + { + "path": "logs/four_skills_v050/bootstrap03/progress.jsonl", + "exists": false, + "bytes": null, + "sha256": null + }, + { + "path": "logs/four_skills_v050/bootstrap03/stdout.log", + "exists": true, + "bytes": 1699, + "sha256": "19d1a54329bbaff3d32f700bf8a8d1560113bdf0b05e1239e23d89e643aebce4" + } + ] + }, + { + "name": "bootstrap04", + "mode": "smoke", + "development_revision": 1, + "output": "logs/four_skills_v050/bootstrap04", + "reserved_calls_no_refund": 15000, + "original_supervisor_success": true, + "original_worker_functional_success": true, + "original_worker_training_complete": null, + "owned_cleanup": true, + "waited_exit": true, + "native_returncode": 0, + "final_physics_calls": 4505, + "last_durable_completed_update": null, + "last_durable_training_environment_transitions": null, + "physics_calls_at_last_complete_update_NOT_final_total": null, + "reason_or_scope": "Revision1 analytic one-loop native bootstrap passed.", + "receipts": [ + { + "path": "logs/four_skills_v050/bootstrap04/worker-result.json", + "exists": true, + "bytes": 1830, + "sha256": "6e785679ff57a42e0212aa0bf91639e00c674886a285ae03a430fe5b6c5234df" + }, + { + "path": "logs/four_skills_v050/bootstrap04/supervisor-result.json", + "exists": true, + "bytes": 24900, + "sha256": "17904697960a2edd049c601843db1766254d105633ae3a7b22e77c6edbd0f166" + }, + { + "path": "logs/four_skills_v050/bootstrap04/progress.jsonl", + "exists": false, + "bytes": null, + "sha256": null + }, + { + "path": "logs/four_skills_v050/bootstrap04/stdout.log", + "exists": true, + "bytes": 6313, + "sha256": "b19e11182fad35fb9b6eea4d001a439357e1627862bc5d20a1177e3b5ef46401" + } + ] + }, + { + "name": "training-preflight01", + "mode": "train", + "development_revision": 1, + "output": "logs/four_skills_v050/training-preflight01", + "reserved_calls_no_refund": 160000, + "original_supervisor_success": false, + "original_worker_functional_success": false, + "original_worker_training_complete": null, + "owned_cleanup": true, + "waited_exit": true, + "native_returncode": 1, + "final_physics_calls": null, + "last_durable_completed_update": null, + "last_durable_training_environment_transitions": null, + "physics_calls_at_last_complete_update_NOT_final_total": null, + "reason_or_scope": "One feedback sample triggered more than one skill transition; first optimizer not reached.", + "receipts": [ + { + "path": "logs/four_skills_v050/training-preflight01/worker-result.json", + "exists": true, + "bytes": 1291, + "sha256": "1372ecfd1c21ffa656cc0d64f2c275eec24a1861eae1bd90212ac1061596b0dd" + }, + { + "path": "logs/four_skills_v050/training-preflight01/supervisor-result.json", + "exists": true, + "bytes": 18640, + "sha256": "4d011db41acac7b89f5287dc67f0b2ffbfa3bebe46ec6181faa28facc90795b1" + }, + { + "path": "logs/four_skills_v050/training-preflight01/progress.jsonl", + "exists": false, + "bytes": null, + "sha256": null + }, + { + "path": "logs/four_skills_v050/training-preflight01/stdout.log", + "exists": true, + "bytes": 6959, + "sha256": "64eb605f55a06bad197f8aa5ea7568b07de8158619eda2f64250b4354c41227f" + } + ] + }, + { + "name": "training-preflight02", + "mode": "train", + "development_revision": 1, + "output": "logs/four_skills_v050/training-preflight02", + "reserved_calls_no_refund": 160000, + "original_supervisor_success": true, + "original_worker_functional_success": false, + "original_worker_training_complete": true, + "owned_cleanup": true, + "waited_exit": true, + "native_returncode": 0, + "final_physics_calls": 640, + "last_durable_completed_update": 2, + "last_durable_training_environment_transitions": 16384, + "physics_calls_at_last_complete_update_NOT_final_total": 640, + "reason_or_scope": "Two-update native GPU preflight completed, not full four-skill qualification.", + "receipts": [ + { + "path": "logs/four_skills_v050/training-preflight02/worker-result.json", + "exists": true, + "bytes": 1087, + "sha256": "f75a8724f241f45312219505466ff6d5009cda0605a237023d3b0922d68fc416" + }, + { + "path": "logs/four_skills_v050/training-preflight02/supervisor-result.json", + "exists": true, + "bytes": 19262, + "sha256": "ba82e0452bbaa62fac710e0abe91fd35011102a755d53af7dfc758e439ede4dc" + }, + { + "path": "logs/four_skills_v050/training-preflight02/progress.jsonl", + "exists": true, + "bytes": 894, + "sha256": "646d133f94859a7fc589ae4b1039aa5df3a49c59cc900b610a559ebe7aed4729" + }, + { + "path": "logs/four_skills_v050/training-preflight02/stdout.log", + "exists": true, + "bytes": 7672, + "sha256": "997256f46a5b08af663123ab32c5347eef39317b12fa6043887f2f43b64f4814" + } + ] + }, + { + "name": "gui-bootstrap01", + "mode": "gui", + "development_revision": 1, + "output": "logs/four_skills_v050/gui-bootstrap01", + "reserved_calls_no_refund": 50000, + "original_supervisor_success": false, + "original_worker_functional_success": true, + "original_worker_training_complete": null, + "owned_cleanup": true, + "waited_exit": true, + "native_returncode": 0, + "final_physics_calls": 4505, + "last_durable_completed_update": null, + "last_durable_training_environment_transitions": null, + "physics_calls_at_last_complete_update_NOT_final_total": null, + "reason_or_scope": "Physical loop complete, but ffmpeg SIGINT exit255 caused original task failure; not reclassified.", + "receipts": [ + { + "path": "logs/four_skills_v050/gui-bootstrap01/worker-result.json", + "exists": true, + "bytes": 2498, + "sha256": "d749ebe6b7691dbce8363a2ac536f59de9cb8c0aed8ab97e90198a306c219236" + }, + { + "path": "logs/four_skills_v050/gui-bootstrap01/supervisor-result.json", + "exists": true, + "bytes": 38207, + "sha256": "e9e2a18d81573b9532437b902302cf9978ef2db4c2a9b5e5e3c83d36d50ddacc" + }, + { + "path": "logs/four_skills_v050/gui-bootstrap01/progress.jsonl", + "exists": false, + "bytes": null, + "sha256": null + }, + { + "path": "logs/four_skills_v050/gui-bootstrap01/stdout.log", + "exists": true, + "bytes": 1514145, + "sha256": "5e6eba93078052290a6b822066dc10a1ca2a313ab5a66437e3ca8ffff997c56f" + } + ] + }, + { + "name": "gui-bootstrap02", + "mode": "gui", + "development_revision": 1, + "output": "logs/four_skills_v050/gui-bootstrap02", + "reserved_calls_no_refund": 50000, + "original_supervisor_success": true, + "original_worker_functional_success": true, + "original_worker_training_complete": null, + "owned_cleanup": true, + "waited_exit": true, + "native_returncode": 0, + "final_physics_calls": 4505, + "last_durable_completed_update": null, + "last_durable_training_environment_transitions": null, + "physics_calls_at_last_complete_update_NOT_final_total": null, + "reason_or_scope": "Revision1 analytic small-tier one-loop GUI/video/normal-close bootstrap passed; ready PNGs have one-frame UI lag.", + "receipts": [ + { + "path": "logs/four_skills_v050/gui-bootstrap02/worker-result.json", + "exists": true, + "bytes": 8435, + "sha256": "90475a39ff06c60faadc3e9749e907ef0b0795d492feba07608b12b779e4c80d" + }, + { + "path": "logs/four_skills_v050/gui-bootstrap02/supervisor-result.json", + "exists": true, + "bytes": 37719, + "sha256": "16f492331a1202812d502298ada2ff39958b96d1d30ea26f6eef36933b8edc26" + }, + { + "path": "logs/four_skills_v050/gui-bootstrap02/progress.jsonl", + "exists": false, + "bytes": null, + "sha256": null + }, + { + "path": "logs/four_skills_v050/gui-bootstrap02/stdout.log", + "exists": true, + "bytes": 25482, + "sha256": "6f9925e76924ae40523551b0153a368e81ce80d8abdea3fb9225ca5ec42ec756" + } + ] + }, + { + "name": "training01", + "mode": "train", + "development_revision": 1, + "output": "logs/four_skills_v050/training01", + "reserved_calls_no_refund": 160000, + "original_supervisor_success": false, + "original_worker_functional_success": null, + "original_worker_training_complete": null, + "owned_cleanup": true, + "waited_exit": true, + "native_returncode": -15, + "final_physics_calls": null, + "last_durable_completed_update": 218, + "last_durable_training_environment_transitions": 1785856, + "physics_calls_at_last_complete_update_NOT_final_total": 69760, + "reason_or_scope": "Parent stopped at218completeupdates after confirmed hold-liveness/phase-reward defects; no candidate. Final worker receipt absent.", + "receipts": [ + { + "path": "logs/four_skills_v050/training01/worker-result.json", + "exists": false, + "bytes": null, + "sha256": null + }, + { + "path": "logs/four_skills_v050/training01/supervisor-result.json", + "exists": true, + "bytes": 109066, + "sha256": "6c4549f159049875713580030b5361d7fe7db78214640d844712ce0b1a5142f5" + }, + { + "path": "logs/four_skills_v050/training01/progress.jsonl", + "exists": true, + "bytes": 103056, + "sha256": "c78c4af2ca31654382bc22a4aca9cd17f26136c133a91449ccf4cf68b5541a11" + }, + { + "path": "logs/four_skills_v050/training01/stdout.log", + "exists": true, + "bytes": 112391, + "sha256": "6d6d6e1e9c6e34a1528d2d456eee01f487d3000b8884f6abc49b2e05cccd09da" + } + ] + } + ] +} diff --git a/configs/four_skills/v050.json b/configs/four_skills/v050.json new file mode 100644 index 0000000..df09239 --- /dev/null +++ b/configs/four_skills/v050.json @@ -0,0 +1,28 @@ +{ + "format": "skillfactory.four-skills.v0.5.0", + "task_id": "SkillFactory-Go2FL-FourSkills-v0", + "development_revision": 2, + "physics_dt_s": 0.002, + "decimation": 5, + "residual_limit_rad": 0.05, + "observation_dim": 48, + "skills": ["support", "retract", "swing", "plant"], + "transition_seconds": {"support": 0.0, "retract_unload": 1.0, "retract_lift": 0.6, "swing": 1.4, "plant_lower": 0.8, "plant_reload": 2.2}, + "clearance_m": 0.05, + "swing_arc_m": 0.01, + "gate": {"support_normal_weight_fraction_min": 0.5, "support_contact_ratio_min": 0.8, "support_dwell_s": 0.2, "air_gap_min_m": 0.01, "air_normal_max_N": 2.0, "air_dwell_s": 0.1, "unload_remaining_fraction_max": 0.8, "swing_signed_progress_min_fraction": 0.5, "landing_error_max_m": 0.05, "root_height_error_max_m": 0.1, "tilt_max_rad": 0.6, "penetration_max_m": 0.002, "normalized_effort_velocity_max": 1.00001, "joint_excess_max_rad": 0.01, "original_late_lift_gap_min_m": 0.02, "original_reload_check_progress": 0.8, "negative_normal_tolerance_N": 0.5, "auxiliary_vertical_max_N": 140.0, "auxiliary_lever_arm_max_m": 0.06}, + "hold_watchdog": "Each legitimate skill/hold request fixes its own intrinsic transition plus requested hold plus2s slack deadline; readiness flicker never extends it. First-readiness deadline and0.2s bad-hold protection remain. Completed held endpoints may persist indefinitely within run budget.", + "reward": "Original phase_objective (ground-level unload tracking, Fn-aware lift/swing, touchdown impact and load restoration) plus0.5ready minus0.02residual_energy minus10failure; no sampled force parameters in actor/critic.", + "transition_deadline_extra_s": 2.0, + "hold_max_s": 10.0, + "force": {"tiers_N": {"none": 0.0, "small": 1.5, "medium": 4.0, "full": 8.0}, "slot_s": 2.0, "active_probability": 0.8, "onset_offset_s": [0.2, 0.5], "duration_s": [0.5, 1.3], "ramp_s": 0.15, "frame": "world", "body": "virtual_payload", "point": "actual_payload_COM", "couple_Nm": [0.0, 0.0, 0.0], "actor_sees_parameters": false}, + "training": {"seed": 105011, "validation_seed": 115011, "independent_seeds": [125011, 125023, 125039, 125053, 125069, 125083, 125101, 125113], "num_envs": 128, "horizon": 64, "updates": 256, "max_wall_s": 64800, "checkpoint_interval": 16, "validation_interval": 64, "tier_updates": [[1, 32, "none"], [33, 96, "small"], [97, 176, "medium"], [177, 256, "full"]], "algorithm": {"learning_rate": 0.0003, "adam_eps": 0.00001, "gamma": 0.99, "gae_lambda": 0.95, "clip_ratio": 0.2, "epochs": 4, "minibatch_size": 1024, "value_coefficient": 0.5, "max_gradient_norm": 1.0, "target_kl": 0.03}}, + "sequences": [ + {"id": "x_forward", "direction_m": [0.024, 0.010], "holds_s": [0.5, 0.3, 0.4, 0.5], "loops": 5}, + {"id": "diagonal", "direction_m": [0.020, 0.024], "holds_s": [1.0, 0.5, 0.8, 0.7], "loops": 5}, + {"id": "y_forward", "direction_m": [0.010, 0.024], "holds_s": [1.5, 0.8, 1.0, 1.0], "loops": 5} + ], + "acceptance": {"central_all_three_sequences_five_loops_required": true, "independent_cases_per_tier": 8, "minimum_success_fraction_each_tier": 0.875, "no_disturbance_success_fraction": 1.0, "candidate_selection": "development-only success fraction then task reward; no independent-seed reselection", "improvement_claim": "only paired independent outcomes; ties or worse are not improvement"}, + "auxiliary": "UNCHANGED original CycleState active equivalent-other-leg vertical support only during unloading/flight, kp1800,kd120,max140N; same bounded passive XY/rotation guide and exactly zero vertical guide; skill-local progress drives original fraction, not central force labels. No stronger auxiliary or external-force cancellation feedforward.", + "scope": "aided floating-root Go2 FL single leg, simulation truth; no hardware, sim2real, multi-leg or whole-robot claim" +} diff --git a/demo_gui.sh b/demo_gui.sh index 26493db..eb9812e 100755 --- a/demo_gui.sh +++ b/demo_gui.sh @@ -1,13 +1,20 @@ #!/usr/bin/env bash -# Genuine live Kit window, not the offline FK animation. No training. +# Genuine live four-skill Kit demo; explicit fresh bounded session, no training. set -euo pipefail ROOT=$(cd -- "$(dirname -- "$0")" && pwd) : "${DISPLAY:?A local graphical DISPLAY is required for the real Kit window}" -OUT=${SKILL_DEMO_OUTPUT:-$(mktemp -d "${TMPDIR:-/tmp}/go2-fl-gui.XXXXXXXX")} +OUT=${SKILL_DEMO_OUTPUT:-$(mktemp -d "${TMPDIR:-/tmp}/go2-fl-four-skills.XXXXXXXX")} mkdir -p "$OUT" -CLI="$ROOT/scripts/go2_fl_skill.py" -echo "Live GUI S1 (analytic); new simulation evidence: $OUT" -echo "Close Kit while running = ABORT. After completion, final frame stays (max 10 min)." -/usr/bin/python3.12 "$CLI" authorize --registration "$OUT/session/registration.json" -/usr/bin/python3.12 "$CLI" run --registration "$OUT/session/registration.json" --output "$OUT/S1" --sequence S1 --controller analytic --gui -/usr/bin/python3.12 "$CLI" status --output "$OUT/S1" +CONTROLLER=${SKILL_CONTROLLER:-trained} +CHECKPOINT=${SKILL_CHECKPOINT:-"$ROOT/releases/go2_fl_four_skills-0.5.0/weights.pt"} +ARGS=(gui --new-session "$OUT/session" --name demo --controller "$CONTROLLER" + --tier "${SKILL_TIER:-small}" --sequence "${SKILL_SEQUENCE:-0}" --loops 5) +if [[ "$CONTROLLER" == trained ]]; then + [[ -f "$CHECKPOINT" ]] || { echo "Frozen0.5.0 weights not found: $CHECKPOINT" >&2; exit 2; } + ARGS+=(--checkpoint "$CHECKPOINT") +fi +if [[ -n ${SKILL_STOP_SKILL:-} ]]; then ARGS+=(--stop-skill "$SKILL_STOP_SKILL"); fi +echo "Go2 FL four skills0.5.0 | $CONTROLLER | real Kit window and original-speed recording" +echo "Output: $OUT/session/demo" +echo "SAFE STOP plants then supports; closing while active=ABORT. Not1x real-time physics." +/usr/bin/python3.12 "$ROOT/scripts/four_skills.py" "${ARGS[@]}" diff --git a/demo_gui_v042.sh b/demo_gui_v042.sh new file mode 100755 index 0000000..26493db --- /dev/null +++ b/demo_gui_v042.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash +# Genuine live Kit window, not the offline FK animation. No training. +set -euo pipefail +ROOT=$(cd -- "$(dirname -- "$0")" && pwd) +: "${DISPLAY:?A local graphical DISPLAY is required for the real Kit window}" +OUT=${SKILL_DEMO_OUTPUT:-$(mktemp -d "${TMPDIR:-/tmp}/go2-fl-gui.XXXXXXXX")} +mkdir -p "$OUT" +CLI="$ROOT/scripts/go2_fl_skill.py" +echo "Live GUI S1 (analytic); new simulation evidence: $OUT" +echo "Close Kit while running = ABORT. After completion, final frame stays (max 10 min)." +/usr/bin/python3.12 "$CLI" authorize --registration "$OUT/session/registration.json" +/usr/bin/python3.12 "$CLI" run --registration "$OUT/session/registration.json" --output "$OUT/S1" --sequence S1 --controller analytic --gui +/usr/bin/python3.12 "$CLI" status --output "$OUT/S1" diff --git a/docs/README.md b/docs/README.md index 4a81eba..cb9e1e1 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,9 +1,11 @@ # 文档入口 +**当前0.5.0四技能开发已暂停,未完成验收。** 先读[状态与本机证据](current_status.md)、[恢复交接](next_stage.md);[四技能合同](four_skills_v050.md)不是完成声明。旧0.4.2原包保持不变。 + ## 下一阶段必读 1. [当前状态](current_status.md):已验证能力、历史失败及未验证项。 -2. [下一阶段](next_stage.md):现有接口集成优先,不默认扩训/扩域。 +2. [下一阶段](next_stage.md):等待用户明确恢复;先复核并验证revision2修正。 3. [公开接口](go2_fl_skill_v4_api.md) / [运行与CPU回归](contact_cycle_runtime.md)。 4. [0.4.2实测结果](contact_cycle_sim_performance_v042_result.md) / [证据索引](evidence_index.md)。 5. [执行边界](execution_authority.md):自主预设护栏,新会话/绑定/输出,不复用旧grant。 diff --git a/docs/current_status.md b/docs/current_status.md index 47e8f7c..474df27 100644 --- a/docs/current_status.md +++ b/docs/current_status.md @@ -1,26 +1,39 @@ -# 当前状态:Go2 FL仿真功能版0.4.2 +# 当前状态:四技能0.5.0开发检查点,用户已暂停 -阶段已完成;本次仅整理文件,没有新Kit运行、物理验证或训练。 +**暂停时间:2026-10-16 17:29 +08。本次是WIP源码提交,不是已验收发布。** 未启动暂停前计划的revision2原生验证/新训练。原生任务均已退出,GPU compute进程为空,最后一项只读复核已停止;只有用户明确要求恢复后才能继续,不能消耗原48小时剩余额度。 -## 已验证基线 +## 已完成 -| 版本 | 实测范围 | 不可外推 | -|---|---|---| -| 0.4.2当前源码 | 解析S1 GUI/headless各6周期、27000tick;70张量及完整轨迹文件与0.4.1相同 | 未重验nominal20/S2/S3/u256、新绘制节拍的原生故障注入 | -| 0.4.1历史 | 解析S1 GUI/headless各6周期;真实提前关窗2040tick按预期中止并清理 | 中止不是完整成功,旧GUI资格不自动继承到0.4.2 | -| 0.4.0历史 | 解析20周期、三条6周期切换、u256 S1、停止/非法拒绝、独立目录公开接口复现 | 不是当前源码完整矩阵、全命令域或全GUI资格 | +- 新增support/retract/swing/plant、保持/查询/合法切换/安全停止的统一接口,以及反馈驱动中央组合器。 +- 新增48D技能条件残差PPO;actor/critic不读取外力采样参数、承重目标或辅助力标签。保留原解析参考、±0.05rad残差和原辅助,随机真实外力固定为0/≤1.5/≤4/≤8N四档。 +- 暂停提交前新CPU测试**139 passed**、AST/bash语法与diff检查通过;363个旧保护文件哈希不变。较早全套1726passed/3skipped属于最新revision2修改之前,不当作当前原生或全套资格。 +- **revision1实测**:`bootstrap04`无扰动解析1闭环4505tick通过;`gui-bootstrap02`小扰动解析1闭环4505tick、真实Kit窗口/原速录像、完成后render-only保留及正常退出通过。reset11/pose14在闭环期间不变。不是三组×五闭环或训练策略资格;原ready截图存在一帧UI滞后,不把它当精确帧对齐证据。 +- RTX5080训练预检完成2次更新/16384环境转移;正式`training01`完成**218次更新、1785856条环境转移**后有依据中止。最后完整更新记录69760共享物理调用;缺少最终worker报告,**终态实际总调用未知**,不把最后进度当最终计数。 +- 独立源码复核发现:短暂ready可永久关闭保持完成超时,奖励可能偏向提前离地/不恢复承重。暂停前已实现revision2修正、恢复原相位/辅助保护、强化离线审计和截图同步;**这些最新修改尚未完成原生复验及最终独立复核**。 -[0.4.2完整结果](contact_cycle_sim_performance_v042_result.md):真实GUI截图33.98FPS,全段绘制提交35.30次/秒;GUI54模拟秒耗时764.863墙钟秒,RTF **0.07060×**;headless294.679秒,RTF **0.18325×**。GPU整卡均值39.74%/133.43W,含桌面客户端,不是进程独占指标或GPU100%。 +## 原始尝试(均保留) -该阶段11Kit、67500实际物理调用、73500预留、0训练;结束57个owned身份均不存活。原始CPU验收1557passed/6skipped属于当时交付物,不是本次精简后测试集的数量。独立包及最终封装凭据见[证据索引](evidence_index.md)。 +证据根:`logs/four_skills_v050/`;9次登记、640000预留调用不是实际物理调用数。 -## 必须保留的限制与失败 +| 目录 | 原结果 | +|---|---| +| `bootstrap01` / `bootstrap02` | 私有worker父身份/相对入口路径校验失败;原件保留 | +| `bootstrap03` | 配置继承奖励字段缺失,环境构造前失败 | +| `bootstrap04` | revision1解析1闭环通过 | +| `training-preflight01` | 中央训练驱动同一反馈样本重复切换,首个优化前失败;修复另建目录 | +| `training-preflight02` | 2更新预检通过,不代表四技能训练完成 | +| `gui-bootstrap01` | 物理闭环完成,但ffmpeg SIGINT退出255使任务失败;不回溯改判 | +| `gui-bootstrap02` | revision1真实GUI/录像/正常退出通过 | +| `training01` | u218后父级因确认的源码缺陷中止;owned清理、wait通过,native退出-15,最终worker报告缺失 | -- `accepted_skill=false`。对象为浮动根Go2 FL单腿、3kg合成负载、仿真真值、有界XY/转动辅助,竖直guide为0;有限单seed功能通过不等于独立鲁棒验收、无辅助平衡、整机或实机能力。 -- 原46D残差PPO冻结u256完成256更新、名义9秒开发验证128/128通过,解析本身也能完成任务。v3有限矩阵双方6/6,没有功能通过率学习增益,不预设追加训练必要。 -- 严格P1双方均在首周期根XY超过30mm而失败;P2虽执行20行,但10组初态全部失配,不能支持学习策略优越性。P2修复只有CPU资格。 -- RootXY-v2完成81更新;u64功能128/128通过但126/128仍超过30mm,无合格候选,不可发布或恢复。扫描竞态最终修复尚未完成端到端训练验证。 -- ff-01 u256三周期物理完成但GPU监督失败;0.4.2首个独立headless退出观察器也异常,确切瞬时原因未知。后续通过不改写旧失败。 -- 旧Reach1.0.1/站立1.0.0不变。站立120秒漂移30.78mm超30mm仅获候选专属豁免,不能移用于本任务。 +## 权重与录像位置 -下一步见[下一阶段](next_stage.md)。历史逐次过程已归档,不再在当前状态追加旧准备/预算记录。 +- 诊断权重:`logs/four_skills_v050/training01/latest.pt`(u218)。**不是候选,不可发布,不可直接恢复训练**;原revision1配置/源码在该目录`source_snapshot/`。当前revision2运行器会拒绝其旧config身份。 +- 真实原速录像:`logs/four_skills_v050/gui-bootstrap02/live-kit-original-speed.mp4`;同目录PNG、`gui-evidence.json`、原轨迹/监督结果保留。 +- 可恢复索引及关键文件SHA256:[暂停清单](../archives/four_skills_v050_pause.json)。约602MiB原始日志、权重、录像及来源快照是本机产物,**未加入Git**;源码clone不包含这些实物。 + +## 尚未完成 + +revision2原生闭环/GUI复验、新训练、冻结合格候选、8独立seed×4档×2控制器配对评估、三组不同方向/时长各连续五闭环、真实非法切换/空中安全停止探针、最终打包和新版本验收均未完成。独立评估种子尚未执行;没有学习提升或四技能已达标结论。 + +`demo_gui.sh`现为0.5.0开发入口,默认需要**尚不存在的正式冻结权重**,不是可交付演示。旧入口副本为`demo_gui_v042.sh`;已封包0.4.2仍在`performance_v042/deliveries/go2_fl_contact_cycle_sim-0.4.2/`,原包/证据未改动。[0.4.2历史实测](contact_cycle_sim_performance_v042_result.md)仍只限定解析S1,历史`accepted_skill=false`/严格RootXY失败不变。 diff --git a/docs/evidence_index.md b/docs/evidence_index.md index 4559f23..8362407 100644 --- a/docs/evidence_index.md +++ b/docs/evidence_index.md @@ -2,7 +2,15 @@ 相对路径以仓库根为准。以下均为历史已发生运行,不是可复用grant;本次整理不产生新的物理资格。 -## 当前0.4.2 +## 四技能0.5.0暂停检查点(未验收) + +[当前状态](current_status.md) / [逐尝试与关键产物SHA256](../archives/four_skills_v050_pause.json)。本机根`logs/four_skills_v050/`保存9次登记的原始失败/通过结果、source_snapshot、原始轨迹、录像及诊断检查点;未加入Git。 + +- `training01/latest.pt`为revision1的u218诊断权重,不是候选;缺失最终worker报告,最后69760物理调用快照不是最终总数。 +- `gui-bootstrap02/`为revision1小扰动解析1闭环真实Kit/录像/正常退出证据,不代表当前revision2或三组五闭环通过。 +- `PAUSED.json`为用户暂停标记;独立种子矩阵、revision2原生复验与最终打包均未执行。不能自动恢复旧grant。 + +## 上一已封包0.4.2 [实测报告](contact_cycle_sim_performance_v042_result.md)。限定当前源码解析S1 GUI/headless,各27000tick;`accepted_skill=false`。 diff --git a/docs/execution_authority.md b/docs/execution_authority.md index c5e37e5..2c1bb4f 100644 --- a/docs/execution_authority.md +++ b/docs/execution_authority.md @@ -1,5 +1,9 @@ # 执行与授权边界 +## 最新:2026-10-16用户要求暂停 + +四技能0.5.0的48小时自主执行已暂停,只允许收尾、记录进度和Git提交。所有原生任务与只读子任务已停止;本机`logs/four_skills_v050/PAUSED.json`阻止继续使用阶段登记。等待用户新的明确恢复指令,不自行删除标记、延长旧截止、复用已消费grant或以剩余预算自动重跑。当前revision2尚未原生复验,u218仅诊断权重;详见[交接](next_stage.md)。下列为通常/历史边界,不覆盖最新暂停要求。 + 用户已明确后续预算无需逐次请批;在**当前用户目标范围内**自主执行,不以等待预算批准阻塞。但不限总预算不等于取消保护、自动扩域或无界后台任务。 ## 每次真实运行仍需 @@ -11,7 +15,7 @@ `go2_fl_skill.py authorize`是调用方显式创建新的≤12小时有限会话,只登记而不启动Kit。各run仍新建approval并串行受监督执行;公开接口上限详见[API](go2_fl_skill_v4_api.md)。不把历史任务剩余预算当新运行授权。 -## 本次及已完成阶段 +## 旧整理及已完成阶段(历史) 本次仅文件整理/CPU回归,不启动仿真或训练,不改变技术方向。上一阶段0.4.2已完成,当前不自动追加RootXY、训练、扰动/噪声、形态、整机/硬件、sim2real或通用adapter。 diff --git a/docs/four_skills_v050.md b/docs/four_skills_v050.md new file mode 100644 index 0000000..6f925fa --- /dev/null +++ b/docs/four_skills_v050.md @@ -0,0 +1,41 @@ +# Go2 FL四技能0.5.0:本轮固定合同 + +**状态:2026-10-16 17:29按用户要求暂停,未完成验收。** 本文是开发合同,不是完成声明;实际进度与恢复前置见[current_status.md](current_status.md)和[next_stage.md](next_stage.md)。旧0.4.2及证据保留。功能达标优先,不追求精度;不能据CPU测试、revision1试跑或旧周期结果声称当前revision2通过。原48小时预算不构成暂停后的自动继续许可。 + +## 对外合同 + +中央仅能调用`support/retract/swing/plant`、查询、保持、切换、安全停止,不下发承重目标、外力标签/参数或辅助力。 + +- `support`:从初始化接触或完成plant进入,固定命令地面支点,维持真实接触/根高度。 +- `retract`:仅从ready support进入;内部先卸载1秒再收腿0.6秒,离地后可保持。 +- `swing`:仅从ready retract或ready swing进入;接收有限世界XY位移目标,1.4秒内部平滑轨迹,真实空中移动后可保持。 +- `plant`:从ready retract/swing进入;内部0.8秒落地+2.2秒恢复承重,真实接触稳定后ready;此后才允许support。 +- 保持不reset、不改物理状态、不重锚,ready也继续执行当前低层控制;中央按真实ready与保持计时推进,不按预编相位时钟强行切换。非法切换拒绝且零控制状态变更。 +- 安全停止请求在support/plant稳定支撑后停止;空中请求走plant→support反馈闭环,不以关窗冒充安全停止;立即安全失败优先中止并留部分轨迹。 + +## 物理与功能门槛(首跑前固定) + +`configs/four_skills/v050.json`为机器合同。撑地/落地Fn≥0.5W、真实接触比例≥80%、连续稳定至少0.2秒;收腿/摆动gap≥10mm、Fn≤2N连续至少0.1秒;卸载至少下降20%;摆动各非零命令轴实际有向进度≥50%。落地误差≤50mm、根高度误差≤100mm、tilt≤0.6rad、穿透≤2mm、归一化力矩/速度≤1.00001、关节超界≤0.01rad。保留原瞬时相位保护:抬腿末段gap≥20mm、摆动gap≥10mm、reload进度>80%时Fn≥0.5W;接触获取/过早离地、负法向力及辅助几何/140N上限继续保护。误差完整记录,不以修改门槛获得通过。 + +沿用同一Go2FL资产/3kg载荷、500Hz PhysX/最高100Hz控制、原±0.05rad残差及原辅助:有界被动XY/转动guide,guideZ=0;原等效其他腿主动竖向支撑按卸载/飞行分数施加,kp1800/kd120/max140N,不增强,不把它当无辅助平衡。原辅助明确计入轨迹和报告。 + +## 外力与学习 + +环境独立采样真实世界系payload COM力,零附加力偶;各2秒slot内80%概率施加,时机随机、持续0.5–1.3秒、0.15秒平滑升降,方向各向同性。幅值四档none/≤1.5/≤4/≤8N;采样种子/环境/episode独立,成对控制器重放同一扰动。actor48D仅接收允许仿真传感状态、技能/空间目标/进度、解析参考与过去残差,**没有采样外力向量、标签、未来时机、承重目标或辅助力标签**。可视化/审计单独读取施加外力,不回流策略。 + +优先原解析IK+新共享四技能条件残差PPO(RTX5080),不加载旧46D权重冒充训练;解析零残差为基线。按none→small→medium→full分档课程,开发seed选候选并在独立seed前冻结。未优于配对解析基线不宣称提升。 + +## 验收与交付 + +- 三组不同方向/保持时长,分别连续5闭环,整个序列初始化后无reset/瞬移/状态覆盖。 +- 独立8seed×4外力档,解析/训练策略相同初态/扰动,报告所有成功/失败/未运行。无扰动8/8,其余每档≥7/8为本阶段限定目标;扩大样本必须在看独立结果前另登记,不按结果重选候选。 +- 真实Kit窗口显示技能、ready/保持、接触、施加外力、辅助和训练/序列状态;真实画面/动作/正常退出及原速Kit录像单独验证,不拿FK/GIF或CPU替代。 +- 保存新版本源码/权重/示例/一键demo/独立验收命令/简洁报告;旧版本原件不改写。首个失败留档,定位后新目录修复复验,禁止盲目重复。 + +## 时间与预算 + +本轮开始2026-10-16 16:07:37 +08,硬截止2026-10-18 16:07:37 +08;最后6小时不启动新训练。初始训练上限128环境×64 horizon×256更新(2097152环境转移),单训练≤18小时,定期检查点;必要技术迭代须记录差异/原因/新上限。任务初始护栏64次Kit启动、300万共享物理调用、120GiB新增逻辑产物。独立评估前已另存scope-amendment-01将启动上限改为96:固定8seed×4档×2控制器本身需要64启动,另外留开发/训练/GUI空间;单周期预留15000调用,300万/120GiB/48h不变,原scope原字节保留。单运行独立上限、预留清理,GPU串行,CPU复核可并行;达到目标即结束,不耗满预算。原始运行证据集中`logs/four_skills_v050/`,不堆逐次修复文档。 + +## 开发修正(独立种子尚未使用) + +training01因独立源码复核确认的保持活性/奖励漏洞在218完整更新后由父级中止;权重仅留诊断,不进入候选。一次短暂ready不再永久关闭保持完成看门狗;奖励改为复用原分相位目标,不能靠提前离地或reload继续悬空获奖。恢复原过早离地/末抬腿/摆动/reload及辅助几何瞬时保护,不放宽任何冻结功能数值。修正后拟定的新目录训练上限仍128×64×256、RTX5080、≤18小时,尚未启动,等待用户恢复。这是有依据的新开发迭代计划,不恢复旧失败目录或复用旧grant。原累计训练/预扣保留。后续独立评估须使用development_revision=2合同;旧开发版结果不冒充修正版资格。 diff --git a/docs/next_stage.md b/docs/next_stage.md index 3d90ca9..57bc69b 100644 --- a/docs/next_stage.md +++ b/docs/next_stage.md @@ -1,26 +1,25 @@ -# 下一阶段交接 +# 暂停后的恢复交接 -## 默认方向:集成现有仿真功能接口 +**当前等待用户新的恢复指令。不要自动继续,也不要因原48小时尚有余额而启动任何Kit或训练。** 本机`logs/four_skills_v050/PAUSED.json`已阻止复用本阶段运行登记;暂停前只读复核也已停止。 -1. 先读[当前状态](current_status.md)与[公开API](go2_fl_skill_v4_api.md),以已封包0.4.2为基线,确认上层启动、状态查询、队列和停止的使用需求。 -2. 先做CPU静态/生命周期回归:按[运行说明](contact_cycle_runtime.md)执行`python scripts/test_cpu.py`;CPU通过不证明物理成功。 -3. 如需修改当前源码,先固定受影响范围和验收项。0.4.2只重新验证了解析S1 GUI/headless,不能借0.4.0完整矩阵声称新版本全域或u256合格。 -4. 确需真实验证时,按[执行边界](execution_authority.md)创建新有限会话、新绑定、新输出;保留逐500Hz安全/证据及监督退出清理,不复用旧grant或失败目录。 +## 恢复时先读 -## 尚未具备的证据 +1. [当前进度/全部尝试/缺失证据](current_status.md)。 +2. [四技能合同及开发修正](four_skills_v050.md);机器配置`configs/four_skills/v050.json`为development_revision=2。 +3. [暂停索引](../archives/four_skills_v050_pause.json):原始结果、诊断权重、录像及SHA256。原件在本机`logs/`,未随Git分发。 -- 当前源码nominal20/S2/S3/u256以及新高频绘制节拍下的原生提前关闭、visualizer故障、安全停止/查看超时资格。 -- 1×实时物理:现有GUI0.07060×、headless0.18325×。若目标是吞吐优化,另固定范围并以相同协议/开销比较,不以渲染FPS替代物理速度。 -- 多seed、全命令域、无辅助平衡、整机/实机、sim2real或通用v2运行adapter。 +## 经用户明确恢复后再执行 -以上是证据缺口,不是自动待执行的仿真矩阵或训练任务。本次整理不启动Kit,不耗用旧预算。 +1. 重新确认目标、时间/存储/调用护栏;旧scope、grant、失败目录、暂停前预留均不能直接作为新授权。记录新的恢复决定和有限登记,不静默删除暂停标记或延长旧截止。 +2. 完成revision2修复的独立复核。重点是保持完成看门狗、分相位奖励/原相位保护、停止请求在下一观察之前路由,以及强化审计与实际显示帧的对齐。最后一次复核被用户暂停,不能称已批准。 +3. 用新目录进行revision2解析无扰动闭环及GUI基础验证,再做有上限的训练预检。检查实际四技能控制采样、真实保持完成、失败原因、原辅助、每步保护及退出清理,不只看reward或优化器更新数。 +4. 只有修复通过后才新建RTX5080训练。u218是有缺陷revision1的诊断权重,不是候选/续训许可;当前CLI没有恢复旧训练的资格。保留所有旧失败及预扣。 +5. 在开发seed上选择并冻结合格权重,然后才执行未使用的独立seed配对矩阵、三组×五连续闭环和真实非法切换/空中安全停止探针。解析/训练采用相同任务、观察初态及真实施加扰动;完整报告失败、未运行和比较范围,不预设优于解析。 +6. 最后实测`demo_gui.sh`与真实Kit录像/正常退出,打包新源码/权重/命令及简洁验收报告。必须明确辅助和物理推进速度;窗口约30FPS不等于1×物理实时。 -## 非默认方向 +## 文件与验收边界 -不为当前功能目标继续RootXY精度优化或追加训练。若后续明确选择此方向,须先回归扫描器竞态、监督器与候选提交路径,再重新固定独立实验;不得恢复不可选取u64、改写旧门槛/失败/账本或把P2失配当有效比较。原[配方](contact_cycle_rootxy_v2_plan.md)与[启动合同](contact_cycle_rootxy_v2_launch.md)仅保留为绑定输入和实现参考。 - -## 后续文件约定 - -- 当前结论只更新`current_status.md`,后续步骤只更新本文,证据位置只更新`evidence_index.md`。 -- 测试留可复用契约、边界和安全回归;任务临时探针/第N次修复报告放运行目录,不再堆入`docs/`和`tests/`。 -- 历史过程见[归档](README.md#历史资料),恢复到临时目录查看,不覆盖当前文件。 +- `legs/four_skills/`:协议/力采样/48D模型;`tasks/manager_based/four_skills/`:真实环境、动作、安全与GUI。 +- `scripts/four_skills.py`:有限会话与监督;`four_skills_runtime.py`:认证worker、训练与执行;`audit_four_skills.py`:CPU只读原轨迹/配对审计。 +- 现有CPU回归入口仍为`python scripts/test_cpu.py`,新回归为`tests/test_four_skills*.py`。CPU通过不是原生或技能验收;不要为提交而重跑GPU。 +- 0.4.2原包、历史严格RootXY失败、原权重/账本/源码及全部旧尝试保持不变。未来只更新状态、本文和证据索引,不恢复堆积逐次修复文档。 diff --git a/scripts/audit_four_skills.py b/scripts/audit_four_skills.py new file mode 100644 index 0000000..e858175 --- /dev/null +++ b/scripts/audit_four_skills.py @@ -0,0 +1,216 @@ +#!/usr/bin/env python3 +"""Independent CPU-only audit of complete four-skill original500Hz traces. + +This checks physical tensors, not the actor's reward or ready/success labels. +It never launches Kit, creates authority, alters traces or selects a candidate. +""" +import argparse +import importlib.util +import json +import math +from pathlib import Path +import sys + +import torch + +ROOT = Path(__file__).resolve().parents[1] + + +def audit(run): + run = Path(run) + config = json.loads((ROOT / 'configs/four_skills/v050.json').read_text()) + approval = json.loads((run / 'approval.json').read_text()) + worker = json.loads((run / 'worker-result.json').read_text()) + supervisor = json.loads((run / 'supervisor-result.json').read_text()) + trace = torch.load(run / 'trace.pt', map_location='cpu', weights_only=True) + initial = torch.load(run / 'initial-state.pt', map_location='cpu', weights_only=True) + reasons = [] + def require(test, reason): + if not bool(test): + reasons.append(reason) + count = len(trace['tick']) + require(count > 0, 'empty_trace') + require(torch.equal(trace['tick'][:, 0], torch.arange(1, count + 1)), 'physical_clock_not_contiguous') + require(count == worker.get('actual_ticks'), 'actual_call_trace_count_mismatch') + require(all(bool(torch.isfinite(v).all()) for v in trace.values() if v.is_floating_point()), 'nonfinite_evidence') + require(not trace['failed'].any(), 'runtime_failure_in_trace') + require(trace['limit_ratio'].max() <= 1.00001, 'effort_velocity') + require(trace['joint_excess'].max() <= .01, 'joint_limits') + require(trace['height_error'].max() <= .1, 'root_height') + require(trace['tilt'].max() <= .6, 'tilt') + require(trace['gap'].min() >= -.002, 'penetration') + require(trace['guide'][..., 2].abs().max() == 0, 'vertical_guide') + if 'auxiliary' in trace: + auxiliary = trace['auxiliary'] + az = auxiliary[..., 2] + require(az.sum(-1).max() <= 140., 'auxiliary_force') + require(az.min() >= -1e-6 and auxiliary[..., :2].abs().max() <= 1e-6 + and auxiliary[..., 5].abs().max() <= 1e-6 + and (auxiliary[..., 3:].norm(dim=-1) <= .06*az.clamp_min(0)+1e-6).all(), 'auxiliary_geometry') + else: + require(False, 'missing_auxiliary_evidence') + require(trace['normal'][..., 2].min() >= -.5, 'negative_normal') + require(worker.get('initialization_counters') == worker.get('final_counters'), 'reset_or_pose_write') + require(worker['physics_counts']['initialization'] == 0, 'unexpected_initialization_physics') + require(worker['physics_counts']['completed'] == count, 'unrecorded_physics') + transitions = json.loads((run / 'transitions.json').read_text()) + sequence = config['sequences'][approval['sequence']] + loops = approval.get('loops') or (1 if approval['mode'] == 'smoke' else sequence['loops']) + require(worker.get('requested_loops') == loops, 'approved_loop_count_mismatch') + require(approval['config_sha256'] == __import__('hashlib').sha256((ROOT / 'configs/four_skills/v050.json').read_bytes()).hexdigest(), 'approved_config_changed') + expected_edges = [('support', 'retract'), ('retract', 'swing'), ('swing', 'plant'), ('plant', 'support')] * loops + require([(e['from'], e['to']) for e in transitions] == expected_edges, 'missing_or_illegal_transitions') + require(all(e['feedback_ready'] and e['feedback_hold_complete'] for e in transitions), 'switch_without_feedback') + kinds = trace['skill'][:, 0] + starts = [0] + (kinds[1:] != kinds[:-1]).nonzero().flatten().add(1).tolist() + ends = starts[1:] + [count] + require(len(starts) == 4 * loops + 1, 'incomplete_skill_segments') + require([int(kinds[i]) for i in starts] == [0, 1, 2, 3] * loops + [0], 'actual_skill_order_mismatch') + require(torch.equal(trace['time_s'][:, 0], trace['tick'][:, 0].float() * .002), 'time_tick_mismatch') + require(torch.equal(trace['episode_id'][:, 0], initial['episode_id'][0].expand(count)), 'episode_changed_during_composition') + for j, start in enumerate(starts[1:]): + if j < len(transitions): + require(transitions[j]['at_tick'] == int(trace['tick'][start, 0]) - 1, 'transition_tick_mismatch') + for start, end in zip(starts, ends): + expected_age = torch.arange(1, end - start + 1).float() * .002 + require(torch.allclose(trace['age_s'][start:end, 0], expected_age, rtol=0., atol=.001), 'segment_age_mismatch') + age = trace['age_s'][:, 0] + expected_phase = torch.where(kinds == 0, 0, torch.where(kinds == 1, + torch.where(age < 1., 1, 2), torch.where(kinds == 2, 3, torch.where(age < .8, 4, 5)))) + if 'phase' in trace: + require(torch.equal(trace['phase'][:, 0], expected_phase), 'intrinsic_phase_mismatch') + else: + require(False, 'missing_phase_evidence') + require(not (((expected_phase == 2) & (age > 1.54)) & (trace['gap'][:, 0] < .02)).any(), 'original_late_lift_floor') + require(not ((expected_phase == 3) & (trace['gap'][:, 0] < .01)).any(), 'original_swing_clearance') + require(not (((expected_phase == 5) & (age > 2.56)) & + (trace['normal'][:, 0, 2] < .5*trace['weight'][:, 0])).any(), 'original_reload_deadline') + holds = sequence['holds_s'] + windows = [] + swing_index = 0 + for index, (start, end) in enumerate(zip(starts, ends)): + kind = int(kinds[start]) + name = config['skills'][kind] + hold = holds[kind] + dwell = .2 if kind in (0, 3) else .1 + window = math.ceil((hold + dwell) / .002) - 1 + if end - start < window: + require(False, f'{index}:{name}:incomplete_hold_window') + continue + part = slice(end - window, end) + normal = trace['normal'][part, 0, 2] + gap = trace['gap'][part, 0] + weight = trace['weight'][part, 0] + row = {'segment': index, 'skill': name, 'ticks': end-start, + 'checked_hold_dwell_s': window*.002, 'normal_min_N': float(normal.min()), + 'normal_max_N': float(normal.max()), 'gap_min_m': float(gap.min())} + if kind in (0, 3): + contact_fraction = float((normal >= .5 * weight).float().mean()) + require(contact_fraction >= .8, f'{index}:{name}:support_contact_fraction') + require(bool(((normal >= .5 * weight) & (gap <= .002)).all()), f'{index}:{name}:continuous_support_dwell_and_hold') + require(float(normal.mean()) >= .5 * float(weight.mean()), f'{index}:{name}:support_mean_load') + row['contact_fraction'] = contact_fraction + if kind == 3: + error = (trace['foot'][part] - trace['target'][part]).norm(dim=-1) + require(error.max() <= .05, f'{index}:landing_error') + row['landing_error_max_m'] = float(error.max()) + else: + require(bool((gap >= .01).all() and (normal <= 2.).all()), f'{index}:{name}:air_hold') + if kind == 1: + unload = (trace['age_s'][start:end, 0] >= .9) & (trace['age_s'][start:end, 0] <= 1.) + first_normal = initial['normal'][0, 2] if start == 0 else trace['normal'][start-1, 0, 2] + remain = trace['normal'][start:end, 0, 2][unload] + require(len(remain) and remain.mean() <= .8 * first_normal, f'{index}:unload') + else: + command = torch.tensor(sequence['direction_m']) * (1 if swing_index % 2 == 0 else -1) + entry = trace['foot'][start-1, 0, :2] + displacement = trace['foot'][end-1, 0, :2] - entry + directed = (trace['foot'][part, 0, :2] - entry) * command.sign() + require((directed >= .5 * command.abs()).all(), f'{index}:continuous_signed_swing_hold') + require(((displacement * command.sign()) >= .5 * command.abs()).all(), f'{index}:signed_swing') + row['actual_xy_m'] = displacement.tolist() + row['command_xy_m'] = command.tolist() + swing_index += 1 + windows.append(row) + # Reconstruct the exact private disturbance schedule, then compare to the + # original applied wrench tensor at each pre-step clock (not reward labels). + schedule_doc = json.loads((run / 'force-schedule.json').read_text()) + spec = importlib.util.spec_from_file_location('four_force_audit', ROOT / 'source/SkillFactory/SkillFactory/legs/four_skills/forces.py') + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + row = schedule_doc['rows'][0] + require(schedule_doc['seed'] == approval['seed'], 'schedule_seed_not_approved') + require(row['tier'] == approval['tier'], 'schedule_tier_not_approved') + require(row['env_id'] == 0 and row['episode_id'] == int(initial['episode_id'][0]), 'schedule_episode_not_initial_state') + runtime = json.loads((run / 'runtime-identity.json').read_text()) + payload_index = runtime['payload_body_index'] + require(len(runtime['body_names']) == trace['external'].shape[-2], 'body_index_layout_mismatch') + require(runtime['body_names'][payload_index] == 'virtual_payload', 'wrong_external_force_body') + schedule = module.ForceSchedule(schedule_doc['seed'], [row['env_id']], row['tier'], 'cpu', + duration_s=schedule_doc['duration_s'], episode_ids=[row['episode_id']]) + require(schedule.manifest() == schedule_doc, 'force_manifest_parameters_changed') + times = torch.cat((initial['time_s'][:1], trace['time_s'][:-1, 0])) + # CPU batched audit is in chunks, preserving every original sample. + maximum_force_difference = 0. + for start in range(0, count, 2048): + end = min(count, start + 2048) + predicted = torch.stack([schedule.at(t.reshape(1))[0] for t in times[start:end]]) + actual = trace['external'][start:end, 0] + expected = torch.zeros_like(actual) + expected[:, payload_index, :3] = predicted + maximum_force_difference = max(maximum_force_difference, float((expected - actual).abs().max())) + require(maximum_force_difference <= 2e-6, 'sampled_force_not_actually_applied') + tier_cap = config['force']['tiers_N'][approval['tier']] + require(trace['external'][..., :3].norm(dim=-1).max() <= tier_cap + .000001, 'force_out_of_approved_tier_range') + require(supervisor['success'] and supervisor['owned_cleanup'] and supervisor['waited_exit'], 'supervision_or_cleanup') + require(worker.get('functional_success') and worker.get('completed_loops') == loops, 'incomplete_original_result') + return {'passed': not reasons, 'failure_reasons': reasons, 'run': str(run.resolve()), + 'physics_ticks': count, 'requested_loops': loops, 'windows': windows, + 'max_force_reconstruction_difference_N': maximum_force_difference, + 'scope': 'CPU physical-tensor audit; runtime continuity separately requires original hooks/supervisor'} + + +def compare_pair(analytic, trained): + a, b = Path(analytic), Path(trained) + aa = json.loads((a / 'approval.json').read_text()) + ab = json.loads((b / 'approval.json').read_text()) + conditions = {k: aa[k] == ab[k] for k in ('seed', 'sequence', 'loops', 'tier', 'config_sha256', 'mode')} + identity_file = b / 'checkpoint-identity.json' + trained_identity = json.loads(identity_file.read_text()) if identity_file.exists() else {} + controllers = (aa['controller'] == 'analytic' and aa['checkpoint'] is None + and ab['controller'] == 'trained' and bool(ab['checkpoint_sha256']) + and trained_identity.get('sha256') == ab['checkpoint_sha256'] + and type(trained_identity.get('update')) is int and trained_identity['update'] > 0 + and trained_identity.get('identity', {}).get('config_sha256') == ab['config_sha256']) + integrity = {name: json.loads((p / 'supervisor-result.json').read_text())['success'] for name, p in [('analytic', a), ('trained', b)]} + sa = json.loads((a / 'force-schedule.json').read_text()) + sb = json.loads((b / 'force-schedule.json').read_text()) + ia = torch.load(a / 'initial-state.pt', weights_only=True, map_location='cpu') + ib = torch.load(b / 'initial-state.pt', weights_only=True, map_location='cpu') + fields = ['root', 'joint_pos', 'joint_vel', 'normal', 'foot', 'target', 'reference', 'q_ik', 'actor_observation'] + equality = {k: torch.equal(ia[k], ib[k]) for k in fields} + ta = torch.load(a / 'trace.pt', weights_only=True, map_location='cpu') + tb = torch.load(b / 'trace.pt', weights_only=True, map_location='cpu') + shared = min(len(ta['tick']), len(tb['tick'])) + force_prefix_equal = torch.equal(ta['external'][:shared], tb['external'][:shared]) + return {'paired': sa == sb and all(equality.values()) and all(conditions.values()) and controllers and all(integrity.values()) and force_prefix_equal, + 'approved_conditions_equal': conditions, 'analytic_vs_frozen_trained': controllers, + 'runtime_integrity': integrity, 'common_prefix_ticks': shared, 'applied_force_common_prefix_equal': force_prefix_equal, + 'analytic_physical_audit_passed': audit(a)['passed'], 'trained_physical_audit_passed': audit(b)['passed'], + 'disturbance_schedule_equal': sa == sb, + 'observed_initial_state_equal': equality, + 'limitation': 'observed physical/controller state, not a proof of complete hidden PhysX state identity'} + + +if __name__ == '__main__': + p = argparse.ArgumentParser(description=__doc__) + p.add_argument('run', type=Path) + p.add_argument('--pair', type=Path) + p.add_argument('--output', type=Path) + args = p.parse_args() + result = compare_pair(args.run, args.pair) if args.pair else audit(args.run) + text = json.dumps(result, indent=2, allow_nan=False) + '\n' + if args.output: + args.output.write_text(text) + print(text) + raise SystemExit(0 if result.get('passed', result.get('paired', False)) else 1) diff --git a/scripts/examples/go2_fl_four_skills.py b/scripts/examples/go2_fl_four_skills.py new file mode 100644 index 0000000..dc90d6a --- /dev/null +++ b/scripts/examples/go2_fl_four_skills.py @@ -0,0 +1,44 @@ +"""Public-interface examples for an already supervised four-skill runtime. + +`advance` is the certified owner's one-control-step callback. It owns actor +inference,500Hz safety/evidence, deadlines and cleanup. The central client below +never receives desired support loads or sampled force parameters, and cannot +reset/teleport the robot. Do not construct an unsupervised Kit here. +""" +from SkillFactory.legs.four_skills.api import CentralComposer + + +def compose(service, advance, *, direction_m=(.024, .010), holds_s=(.5, .3, .4, .5), loops=5): + program = CentralComposer(service, {'direction_m': direction_m, 'holds_s': holds_s, 'loops': loops}) + for _ in range(12000): + if program.update(): + return {'completed_loops': program.completed_loops, 'safe_stop': program.safely_stopped, + 'status': service.query()} + advance() + service.request_stop() + raise TimeoutError('Central command budget exhausted; owner must finish safe cleanup') + + +def hold_then_switch(service, advance): + """Illustrate calls individually; all transitions use actual ready feedback.""" + def wait(): + for _ in range(2000): + status = service.query() + if status['failed']: + raise RuntimeError(str(status['failure_reasons'])) + if status['ready'] and status['hold_complete']: + return status + advance() + raise TimeoutError('Skill feedback not ready; no forced transition or state overwrite') + service.hold(.8) + wait() + service.call('retract', hold_seconds=.5) + wait() + service.switch('swing', direction_m=[.020, .024], hold_seconds=1.) + wait() + service.call('plant', hold_seconds=.7) + wait() + service.call('support', hold_seconds=.8) + wait() + service.request_stop() + return service.query() diff --git a/scripts/four_skills.py b/scripts/four_skills.py new file mode 100644 index 0000000..8e108b5 --- /dev/null +++ b/scripts/four_skills.py @@ -0,0 +1,410 @@ +#!/usr/bin/env python3 +"""New v0.5.0 stage CLI: finite fresh runs, strict GPU/process ownership. + +No simulation or authority is created on import. Normal callers use the public +skill service; the private worker path requires a live parent socket handoff. +""" +import argparse +import ctypes +from datetime import datetime, timezone +import fcntl +import hashlib +import json +import os +from pathlib import Path +import signal +import socket +import struct +import subprocess +import sys +import time +import traceback +from types import SimpleNamespace + +ROOT = Path(__file__).resolve().parents[1] +STAGE = ROOT / 'logs/four_skills_v050' +CONFIG = ROOT / 'configs/four_skills/v050.json' +START = '2026-10-16T16:07:37+08:00' +DEADLINE = '2026-10-18T16:07:37+08:00' + + +def clock(): + return time.clock_gettime(time.CLOCK_BOOTTIME) + + +def sha(path): + return hashlib.sha256(Path(path).read_bytes()).hexdigest() + + +def write(path, value): + path = Path(path) + temp = path.with_suffix(path.suffix + '.pending') + data = json.dumps(value, sort_keys=True, indent=2, allow_nan=False).encode() + b'\n' + with temp.open('wb') as stream: + stream.write(data) + stream.flush() + os.fsync(stream.fileno()) + os.replace(temp, path) + + +def sources(): + files = [p for base, pattern in [('source', '*.py'), ('scripts', '*.py'), ('configs', '*.json')] + for p in (ROOT / base).rglob(pattern) if '__pycache__' not in p.parts] + for directory in ('legs/go2_fl', 'support/go2_fl_3kg_v0', 'go2_fl'): + files += [p for p in (ROOT / 'assets/generated' / directory).rglob('*') + if p.is_file() and '__pycache__' not in p.parts] + files += [ROOT / 'docs/four_skills_v050.md', ROOT / 'demo_gui.sh', ROOT / 'demo_gui_v042.sh'] + return {p.relative_to(ROOT).as_posix(): sha(p) for p in sorted(files)} + + +def scope(): + STAGE.mkdir(exist_ok=True) + if (STAGE / 'PAUSED.json').exists(): + raise PermissionError('Stage paused by user; a new explicit resume instruction is required') + p = STAGE / 'scope.json' + if not p.exists(): + remaining = datetime.fromisoformat(DEADLINE).timestamp() - time.time() + value = {'format': 'skillfactory.four-skills-stage.v050', 'started_wall': START, + 'deadline_wall': DEADLINE, 'boot_id': Path('/proc/sys/kernel/random/boot_id').read_text().strip(), + 'deadline_boottime': clock() + remaining, 'train_cutoff_boottime': clock() + remaining - 21600, + 'max_kit_launches': 64, 'max_reserved_physics_calls': 3000000, + 'max_owned_bytes': 120 << 30, 'user_authorized_hours': 48, + 'preserve_original_versions': True, 'scope': 'single aided Go2FL only'} + with p.open('x') as stream: + stream.write(json.dumps(value, indent=2) + '\n') + value = json.loads(p.read_text()) + if value['boot_id'] != Path('/proc/sys/kernel/random/boot_id').read_text().strip(): + raise RuntimeError('Stage boot changed; stop, do not revive old grants') + if clock() >= value['deadline_boottime']: + raise TimeoutError('48h stage deadline reached; no new work') + amendment = STAGE / 'scope-amendment-01.json' + if amendment.exists(): + update = json.loads(amendment.read_text()) + if (update.get('format') != 'skillfactory.four-skills-budget-amendment.v1' + or update.get('original_scope_sha256') != sha(p) + or update.get('max_kit_launches') != 96): + raise ValueError('Invalid recorded stage budget amendment') + value['max_kit_launches'] = 96 + value['amendment_sha256'] = sha(amendment) + return value + + +def new_session(path, mode): + """Explicit future caller invocation, never revival of the48h stage grant.""" + global STAGE + if mode not in ('gui', 'evaluate', 'probe'): + raise PermissionError('Standalone sessions cannot authorize training') + path = Path(path) + if (not path.is_absolute() or path != path.resolve() or path.exists() or not path.parent.is_dir() + or path.is_relative_to(ROOT / 'releases') or path.is_relative_to(ROOT / 'performance_v042') + or path.is_relative_to(Path('/home/cen/sf-deliveries')) + or (path.is_relative_to(ROOT) and not path.is_relative_to(ROOT / 'logs/four_skills_v050_sessions')) + or (path.is_relative_to(Path('/home/cen/sf-runs')) and path.parent != Path('/home/cen/sf-runs'))): + raise ValueError('Fresh canonical session outside protected evidence/source trees required') + path.mkdir() + STAGE = path + value = {'format': 'skillfactory.four-skills-explicit-session.v050', + 'issued_wall': datetime.now().astimezone().isoformat(), + 'boot_id': Path('/proc/sys/kernel/random/boot_id').read_text().strip(), + 'deadline_boottime': clock() + 3600, 'train_cutoff_boottime': clock(), + 'max_kit_launches': 1, 'max_reserved_physics_calls': 50000, + 'max_owned_bytes': 8 << 30, 'training_authorized': False, + 'scope': 'explicit caller invocation; not a48h-task extension or old-grant reuse'} + write(path / 'scope.json', value) + return value + + +def tree_bytes(path): + total = 0 + for base, dirs, files in os.walk(path, followlinks=False): + dirs[:] = [d for d in dirs if not Path(base, d).is_symlink()] + for name in files: + p = Path(base, name) + try: + if p.is_symlink(): + raise RuntimeError('Symlink inside owned outputs') + total += p.stat().st_size + except FileNotFoundError: + continue # atomic publication disappearance, never permission denial + return total + + +class Guard: + def __init__(self, payload, *, worker=False): + self.p = payload + self.interrupted = False + self.next_slow = 0 + self.worker = worker + + def __call__(self, *, cleanup=False): + if not cleanup and (self.interrupted or (Path(self.p['output']) / 'cancel.request').exists() + or (Path(self.p['output']).parent / 'PAUSED.json').exists()): + raise InterruptedError('Cooperative cancellation') + end = self.p['deadline_boottime'] if cleanup else self.p['deadline_boottime'] - 120 + if clock() >= end: + raise TimeoutError('Run/stage deadline; reserve cleanup') + if cleanup or clock() < self.next_slow: + return + self.next_slow = clock() + 1 + if tree_bytes(self.p['output']) > self.p['max_owned_bytes']: + raise RuntimeError('Owned artifact cap exceeded') + if os.statvfs(self.p['output']).f_bavail * os.statvfs(self.p['output']).f_frsize < 8 << 30: + raise RuntimeError('Minimum free disk headroom8GiB') + for relative, digest in self.p['sources'].items(): + if sha(ROOT / relative) != digest: + raise RuntimeError('Bound runtime source changed: ' + relative) + + +def reserve(args, sc): + p = STAGE / 'ledger.json' + ledger = json.loads(p.read_text()) if p.exists() else {'launches': 0, 'reserved_physics_calls': 0, 'runs': []} + calls = 160000 if args.mode == 'train' else 50000 + if args.mode in ('smoke', 'probe') or args.loops == 1: + calls = 15000 + if ledger['launches'] + 1 > sc['max_kit_launches'] or ledger['reserved_physics_calls'] + calls > sc['max_reserved_physics_calls']: + raise RuntimeError('Stage cap exhausted; do not silently extend') + if args.mode == 'train' and clock() >= sc['train_cutoff_boottime']: + raise TimeoutError('Final6h: no new training') + duration = 64800 if args.mode == 'train' else 2400 + output = (STAGE / args.name).absolute() + if (not args.name or Path(args.name).name != args.name or args.name.startswith('.') + or output.exists() or output.parent != STAGE or output.resolve() != output): + raise ValueError('Fresh unused simple run name required') + if tree_bytes(STAGE) > sc['max_owned_bytes']: + raise RuntimeError('Stage storage cap') + checkpoint = None if args.checkpoint is None else str(args.checkpoint.resolve(strict=True)) + if args.controller == 'trained' and checkpoint is None: + raise ValueError('Trained controller requires explicit immutable checkpoint') + if args.controller == 'analytic' and checkpoint is not None: + raise ValueError('Analytic controller cannot load weights') + if args.mode == 'train' and args.controller != 'analytic': + raise ValueError('Fresh training is not checkpoint resume') + output.mkdir() + payload = {'format': 'skillfactory.four-skills-run.v050', 'mode': args.mode, 'output': str(output), + 'seed': args.seed, 'sequence': args.sequence, 'loops': args.loops, + 'tier': args.tier, 'controller': args.controller, 'checkpoint': checkpoint, + 'stop_skill': args.stop_skill or ('retract' if args.mode == 'probe' else None), + 'checkpoint_sha256': None if checkpoint is None else sha(checkpoint), + 'sources': sources(), 'config_sha256': sha(CONFIG), 'boot_id': sc['boot_id'], + 'deadline_boottime': min(clock() + duration, sc['deadline_boottime']), + 'max_physics_calls': calls, 'max_owned_bytes': (32 if args.mode == 'train' else 8) << 30, + 'updates': args.updates, 'parent_pid': os.getpid(), 'issued_boottime': clock(), + 'stage_amendment_sha256': sc.get('amendment_sha256')} + ledger['launches'] += 1 + ledger['reserved_physics_calls'] += calls + ledger['runs'].append({'name': args.name, 'mode': args.mode, 'reserved_calls': calls, + 'output': str(output), 'refunded': False}) + write(p, ledger) + write(output / 'approval.json', payload) + snapshot = output / 'source_snapshot' + for relative in payload['sources']: + dest = snapshot / relative + dest.parent.mkdir(parents=True, exist_ok=True) + dest.write_bytes((ROOT / relative).read_bytes()) + return payload + + +def supervise(args): + import evaluate_cycle_sim_residual as owner + import rootxy_launch_v2 as ownership + from skill_launch_v4 import capture_gpu + sc = new_session(args.new_session, args.mode) if args.new_session else scope() + with (STAGE / 'serial.lock').open('a') as lock: + fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB) + payload = reserve(args, sc) + out = Path(payload['output']) + guard = Guard(payload) + # No child command before this exact dedicated-supervisor snapshot. + owned = owner.OwnedProcesses() + gpu_identity = capture_gpu() + write(out / 'gpu-identity.json', gpu_identity) + gpu = ownership.GPUOwnership(gpu_identity) + environment = {k: v for k, v in os.environ.items() if k not in + ('VIRTUAL_ENV', 'CONDA_PREFIX', 'PYTHONEXE', 'PYTHONHOME', 'args')} + environment.update(PYTHONDONTWRITEBYTECODE='1', PYTEST_DISABLE_PLUGIN_AUTOLOAD='1', + CUDA_VISIBLE_DEVICES=gpu_identity['uuid'], + SKILLFACTORY_GO2_ASSET_DIR=str(ROOT / 'assets/generated/go2_fl'), + SKILLFACTORY_LEG_BUNDLE=str(ROOT / 'assets/generated/legs/go2_fl'), + PYTHONPATH=os.pathsep.join([str(ROOT / 'source/SkillFactory'), str(ROOT / 'scripts'), + environment.get('PYTHONPATH', '')])) + (out / 'native').mkdir() + redirects = owner.native_redirects(out / 'native' / 'runtime', preparation=True) + for p in redirects.values(): + Path(p).mkdir(parents=True, exist_ok=True) + environment.update(redirects) + policy = {'artifact_root': str(out), 'mount_identity': owner.mount_identity(out), + 'native_paths': owner.native_paths(out), 'shared_native_paths': owner.shared_native_paths(), + 'task_owned_cache_paths': sorted(redirects.values()), 'free_space_paths': owner.free_space_paths(out), + 'free_space_mount_identities': {p: owner.mount_identity(Path(p)) for p in owner.free_space_paths(out)}, + 'check_interval_seconds': 1., 'min_free_bytes': 8 << 30, + 'max_native_growth_bytes': 16 << 30} + write(out / 'native-policy.json', policy) + monitor = owner.NativeMonitor(policy, SimpleNamespace(boottime=clock, STORAGE_SCOPE='four-skills-v050-monitored-not-quota')) + child = capture = None + errors = [] + clean = waited = False + parent, channel = socket.socketpair() + old_handlers = {} + try: + if ctypes.CDLL(None).prctl(36, 1, 0, 0, 0) != 0: + raise RuntimeError('Subreaper setup failed') + for sig in (signal.SIGTERM, signal.SIGINT): + old_handlers[sig] = signal.signal(sig, lambda *_: setattr(guard, 'interrupted', True)) + command = owner.launch_command([str(Path(__file__).resolve()), '--_worker-fd', str(channel.fileno())], environment) + write(out / 'invocation.json', {'command': command, 'cwd': str(ROOT), 'launch_identity': owner.launch_identity()}) + child = subprocess.Popen(command, cwd=ROOT, env=environment, stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, start_new_session=True, pass_fds=(channel.fileno(),)) + owned.attach(child) + channel.close() + capture = owner.StdoutCapture(child.stdout, out / 'stdout.log', SimpleNamespace(STDOUT_BYTES=8 << 20)) + packet = json.dumps(payload).encode() + parent.sendall(struct.pack('!I', len(packet)) + packet) + parent.close() + next_heartbeat = 0 + while child.poll() is None or not capture.eof: + guard() + capture.check() + gpu.check(owned) + monitor.check() + if clock() >= next_heartbeat: + write(out / 'supervisor-heartbeat.json', {'boottime': clock(), 'owned': sorted(owned.known), + 'gpu': gpu.clients().__str__(), 'deadline': payload['deadline_boottime']}) + next_heartbeat = clock() + 5 + time.sleep(.05) + child.wait() + waited = True + if child.returncode != 0: + errors.append('worker_nonzero:' + str(child.returncode)) + except BaseException: + errors.append(traceback.format_exc()) + finally: + parent.close() + channel.close() + try: + if child is not None: + if child.poll() is None and errors: + # File cancellation survives SDK signal-handler changes; + # allow Python evidence/checkpoint finalizers first. + write(out / 'cancel.request', {'reason': errors[-1][-4096:]}) + until = min(clock() + 15, payload['deadline_boottime'] - 10) + while child.poll() is None and clock() < until: + if capture is not None: + capture.pump() + time.sleep(.05) + clean = owner.cleanup_group(child, payload['deadline_boottime'], SimpleNamespace(boottime=clock), + owned, lambda: capture.pump() if capture else None) + child.wait(timeout=max(.1, payload['deadline_boottime'] - clock())) + waited = True + if capture is not None: + capture.close() + if capture.discarded or capture.error or not capture.eof: + errors.append('incomplete_stdout') + final_gpu = gpu.check(owned, final=True) + guard(cleanup=True) + monitor.check(force=True) + except BaseException: + errors.append('cleanup:' + traceback.format_exc()) + final_gpu = None + worker = json.loads((out / 'worker-result.json').read_text()) if (out / 'worker-result.json').exists() else None + if worker is not None and not worker.get('success'): + errors.append('worker_reported_failure:' + str(worker.get('error', worker.get('close_error', 'incomplete')))[-4096:]) + success = bool(not errors and clean and waited and worker and worker.get('success') + and worker.get('postclose_identity_verified') and child.returncode == 0) + result = {'success': success, 'functional_success': bool(success and worker.get('functional_success')), + 'training_complete': bool(success and worker.get('training_complete')), + 'owned_cleanup': clean, 'waited_exit': waited, + 'returncode': None if child is None else child.returncode, 'errors': errors, + 'gpu_final': final_gpu, 'owned_identities': sorted(owned.known), + 'native_monitor': monitor.report(), 'accepted_skill': False} + write(out / 'supervisor-result.json', result) + for sig, handler in old_handlers.items(): + signal.signal(sig, handler) + print(json.dumps({'output': str(out), 'success': success, + 'functional_success': result['functional_success'], + 'training_complete': result['training_complete'], 'errors': errors}), flush=True) + return 0 if success else 1 + + +def supervisor_command_matches(command, cwd): + if len(command) < 3: + return False + try: + script = Path(command[1].decode()) + if not script.is_absolute(): + script = Path(cwd) / script + return (script.resolve() == Path(__file__).resolve() + and command[2].decode() in ('smoke', 'evaluate', 'train', 'gui', 'probe')) + except (UnicodeError, OSError, ValueError): + return False + + +def worker(fd): + channel = socket.socket(fileno=fd) + peer_pid, peer_uid, _ = struct.unpack('3i', channel.getsockopt(socket.SOL_SOCKET, socket.SO_PEERCRED, 12)) + import evaluate_cycle_sim_residual as owner + command = Path(f'/proc/{peer_pid}/cmdline').read_bytes().split(b'\0') + if (peer_uid != os.getuid() or not owner.ancestor(peer_pid, os.getpid()) + or Path(f'/proc/{peer_pid}/exe').resolve() != Path('/usr/bin/python3.12') + or not supervisor_command_matches(command, Path(f'/proc/{peer_pid}/cwd').resolve())): + raise PermissionError('Authenticated exact live sole ancestor supervisor required') + channel.settimeout(30) + def receive(n): + chunks = b'' + while len(chunks) < n: + part = channel.recv(n - len(chunks)) + if not part: + raise EOFError('Incomplete parent handoff') + chunks += part + return chunks + size = struct.unpack('!I', receive(4))[0] + if size > 4 << 20: + raise ValueError('Oversized handoff') + payload = json.loads(receive(size)) + channel.close() + if payload['parent_pid'] != peer_pid or payload['format'] != 'skillfactory.four-skills-run.v050': + raise PermissionError('Wrong run identity') + if payload != json.loads((Path(payload['output']) / 'approval.json').read_text()): + raise PermissionError('Exact new approval required') + claim = Path(payload['output']) / 'worker-claim' + with claim.open('x'): + pass + guard = Guard(payload, worker=True) + for sig in (signal.SIGTERM, signal.SIGINT): + signal.signal(sig, lambda *_: setattr(guard, 'interrupted', True)) + guard() + from four_skills_runtime import run + return run(payload, guard, write) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--_worker-fd', type=int, help=argparse.SUPPRESS) + parser.add_argument('mode', nargs='?', choices=('smoke', 'evaluate', 'train', 'gui', 'probe', 'scope')) + parser.add_argument('--name') + parser.add_argument('--new-session', type=Path, + help='Explicit fresh<=1h nontraining invocation; no old stage-grant reuse') + parser.add_argument('--controller', choices=('analytic', 'trained'), default='analytic') + parser.add_argument('--checkpoint', type=Path) + parser.add_argument('--tier', choices=('none', 'small', 'medium', 'full'), default='none') + parser.add_argument('--seed', type=int, default=115011) + parser.add_argument('--sequence', type=int, choices=(0, 1, 2), default=0) + parser.add_argument('--loops', type=int, choices=(1, 2, 3, 4, 5)) + parser.add_argument('--updates', type=int) + parser.add_argument('--stop-skill', choices=('retract', 'swing')) + args = parser.parse_args() + if args._worker_fd is not None: + return worker(args._worker_fd) + if args.mode == 'scope': + print(json.dumps(scope(), indent=2)) + return 0 + if args.mode is None or args.name is None: + parser.error('Fresh --name and run mode required') + if args.mode == 'train': + args.seed = 105011 + if args.updates is not None and not 1 <= args.updates <= 256: + parser.error('Updates in1..256') + return supervise(args) + + +if __name__ == '__main__': + raise SystemExit(main()) diff --git a/scripts/four_skills_runtime.py b/scripts/four_skills_runtime.py new file mode 100644 index 0000000..193c9ee --- /dev/null +++ b/scripts/four_skills_runtime.py @@ -0,0 +1,474 @@ +"""Authenticated Isaac worker for the additive four-skill stage. + +Imported by four_skills.py only after private socket handoff. No old run grant, +checkpoint identity, accepted flag or legacy task is reinterpreted. +""" +import importlib +import json +import math +import os +from pathlib import Path +import sys +import time +import traceback + +ROOT = Path(__file__).resolve().parents[1] +TASK = 'SkillFactory.tasks.manager_based.four_skills.' +LEG = 'SkillFactory.legs.four_skills.' + + +def verify_project_imports(): + expected = ROOT / 'source/SkillFactory/SkillFactory' + paths = {} + for name, module in tuple(sys.modules.items()): + if (name == 'SkillFactory' or name.startswith('SkillFactory.')) and getattr(module, '__file__', None): + path = Path(module.__file__).resolve() + if not path.is_relative_to(expected): + raise RuntimeError('Imported project source outside current copy: ' + name) + paths[name] = str(path) + return paths + + +def normalize_scene(cfg, binding): + from SkillFactory.tasks.manager_based.four_skills.cfg import FourSkillsEnvCfg + if type(cfg) is not FourSkillsEnvCfg or cfg.scene.num_envs not in (1, 128): + raise ValueError('Exact new stage config, scalar or128envs required') + if cfg.scene.clone_in_fabric is not False: + raise ValueError('Non-fabric scene required') + # Same pinned SDK normalization mechanics; independent new config identity, + # not a fake RootXY instance or expanded old evaluator allowlist. + for name in binding.NORMALIZATION_SOURCES: + rel, expected = binding.ISAAC_PINS[name] + p = binding._module_file(name) + if p != binding.ISAAC_ROOT / rel or binding.sha256(p.read_bytes()) != expected: + raise ValueError('Installed scene normalizer drift: ' + name) + regex = '/World/envs/env_.*' + for name in ('ground', 'light', 'robot', 'foot_contact', 'mount_wrench'): + entity = getattr(cfg.scene, name) + entity.prim_path = entity.prim_path.format(ENV_REGEX_NS=regex) + if name in ('ground', 'light', 'robot'): + entity.spawn.spawn_path = entity.prim_path.replace(regex, '/World/envs/env_0') + elif name == 'foot_contact': + for field in ('filter_prim_paths_expr', 'sensor_shape_prim_expr', 'filter_shape_prim_expr'): + if hasattr(entity, field) and getattr(entity, field): + setattr(entity, field, [p.format(ENV_REGEX_NS=regex) for p in getattr(entity, field)]) + for obj, name, value in binding._reference_updates(cfg): + setattr(obj, name, value) + binding.canonical_config(cfg) + + +class Trace: + def __init__(self, output, guard, *, full=True): + self.output, self.guard, self.full = Path(output), guard, full + self.frames = [] + self.initial = None + self.maximum = {'limit_ratio': 0., 'joint_excess_rad': 0., 'height_error_m': 0., + 'tilt_rad': 0., 'external_N': 0., 'guideZ_N': 0.} + self.ticks = 0 + self.failure_frames = [] + + def snapshot(self, s): + import torch + phase, progress, target, _, fraction = s.reference() + ratio = torch.maximum((s.robot.data.applied_torque.torch[:, s.joints] / s.effort).abs().amax(-1), + (s.robot.data.joint_vel.torch[:, s.joints] / s.speed).abs().amax(-1)) + values = {'tick': s.substeps, 'time_s': s.time, 'skill': s.skill, 'age_s': s.age, + 'weight': s.weight, 'episode_id': torch.tensor(s.episode_ids, device=s.env.device), + 'phase': phase, + 'ready': s.ready, 'held_s': s.held, 'hold_required_s': s.hold_required, + 'hold_deadline_s': s.hold_deadline, 'hold_completed': s.hold_completed, + 'dwell_s': s.dwell, 'bad_hold_s': s.bad_hold, + 'unload_passed': s.unload_passed, 'unload_min_N': s.unload_min, + 'unload_initial_N': s.unload_initial, 'failed': s.failed, + 'root': torch.cat(s.root(), -1), 'joint_pos': s.q(), + 'joint_vel': s.robot.data.joint_vel.torch[:, s.joints], + 'applied_torque': s.robot.data.applied_torque.torch[:, s.joints], + 'foot': s.foot_center(), 'normal': s.contact(), + 'gap': s.foot_center()[:, 2] - s.bundle['foot']['radius'], + 'target': target, 'progress': progress, 'fixture_fraction': fraction, + 'external': s.external, 'guide': s.guide, 'auxiliary': s.auxiliary, + 'reference': s.controller.action, 'q_ik': s.controller.q_ik, + 'residual': s.residual_request, 'effective_residual_rad': s.effective_residual_rad, + 'actor_observation': s.policy(), 'limit_ratio': ratio, + 'joint_excess': torch.maximum(s.lower - s.q(), s.q() - s.upper).clamp(min=0).amax(-1), + 'height_error': (s.root()[0][:, 2] - s.base_origin[:, 2]).abs(), + 'tilt': s.tilt()} + return {k: v.detach().cpu().clone() for k, v in values.items()} + + def __call__(self, s): + import torch + # Training keeps aggregates and complete first offending snapshots, + # evaluation retains every500Hz original tensor with no thinning. + if self.full or bool(s.failed.any()): + frame = self.snapshot(s) + if self.full: + self.frames.append(frame) + elif len(self.failure_frames) < 256: + self.failure_frames.append(frame) + self.ticks += 1 + if self.full: + values = self.frames[-1] + for dest, src in [('limit_ratio', 'limit_ratio'), ('joint_excess_rad', 'joint_excess'), + ('height_error_m', 'height_error'), ('tilt_rad', 'tilt')]: + self.maximum[dest] = max(self.maximum[dest], float(values[src].max())) + self.maximum['external_N'] = max(self.maximum['external_N'], float(values['external'][..., :3].norm(dim=-1).max())) + self.maximum['guideZ_N'] = max(self.maximum['guideZ_N'], float(values['guide'][..., 2].abs().max())) + + def save(self): + import torch + self.guard(cleanup=True) + if self.initial is not None: + torch.save(self.initial, self.output / 'initial-state.pt') + if self.frames: + torch.save({k: torch.stack([f[k] for f in self.frames]) for k in self.frames[0]}, self.output / 'trace.pt') + if self.failure_frames: + torch.save(self.failure_frames, self.output / 'training-failure-frames.pt') + + +class TrainingComposer: + def __init__(self, env): + import torch + self.env = env + self.loops = torch.zeros(env.num_envs, dtype=torch.long, device=env.device) + self.episodes = list(env.s.episode_ids) + self.skill_counts = [0] * 4 + self.transition_count = 0 + + def update(self): + import torch + s, service = self.env.s, self.env.service + for i, episode in enumerate(s.episode_ids): + if episode != self.episodes[i]: + self.loops[i] = 0 + self.episodes[i] = episode + allowed = s.ready & (s.held + 1e-6 >= s.hold_required) & ~s.failed & ~s.stopped + kinds = s.skill.clone() # one decision per row, even after service mutation + # Only public skill calls mutate reference goals. No force arguments. + for skill in range(4): + ids = (allowed & (kinds == skill)).nonzero().flatten() + self.skill_counts[skill] += int((kinds == skill).sum()) + if not len(ids): + continue + next_skill = ('retract', 'swing', 'plant', 'support')[skill] + self.transition_count += len(ids) + if skill == 1: + for variant, vector in enumerate(((.024, .010), (.020, .024), (.010, .024))): + for sign in (1, -1): + selected = ids[(ids % 3 == variant) & (torch.where(self.loops[ids] % 2 == 0, 1, -1) == sign)] + if len(selected): + service.call('swing', hold_seconds=(.3, .6, 1.)[variant], + direction_m=[sign*x for x in vector], env_ids=selected) + else: + hold = (.4, 0., .5, .8)[skill] + service.call(next_skill, hold_seconds=hold, env_ids=ids) + if skill == 3: + self.loops[ids] += 1 + + +def reject_probe(env): + import torch + def snapshot(): + s = env.s + values = [s.q(), *s.root(), s.controller.q_ik, s.controller.action, s.skill, s.substeps, + s.age, s.held, s.hold_required, s.ground_target, s.controller.refreshes, + torch.get_rng_state(), *torch.cuda.get_rng_state_all()] + return [v.detach().cpu().clone() for v in values] + before = snapshot() + counts = (env.runtime_hooks.completed_steps, env.runtime_hooks.reset_calls, env.runtime_hooks.pose_write_calls) + rejected = False + reason = None + try: + env.service.call('plant') # support->plant is illegal, even if ready. + except ValueError as error: + rejected, reason = True, str(error) + after = snapshot() + unchanged = all(torch.equal(a, b) for a, b in zip(before, after)) and counts == ( + env.runtime_hooks.completed_steps, env.runtime_hooks.reset_calls, env.runtime_hooks.pose_write_calls) + if not rejected or not unchanged: + raise RuntimeError('Illegal public switch was not rejected atomically') + return {'rejected': rejected, 'reason': reason, 'carried_state_and_rng_equal': unchanged, + 'physical_calls_added': 0} + + +def execute_single(env, payload, output, guard, trace, model=None): + import torch + from SkillFactory.legs.four_skills.api import CentralComposer + config = env.cfg.four_parameters + sequence = dict(config['sequences'][payload['sequence']]) + if payload['mode'] == 'smoke': + sequence['loops'] = 1 + if payload.get('loops') is not None: + sequence['loops'] = payload['loops'] + composer = CentralComposer(env.service, sequence) + observation = env.reset(seed=payload['seed'])[0]['policy'] + trace.initial = trace.snapshot(env.s) + if payload['mode'] == 'probe': + (output / 'illegal-switch-probe.json').write_text(json.dumps(reject_probe(env), indent=2)) + initial_counts = {'resets': env.runtime_hooks.reset_calls, 'pose_writes': env.runtime_hooks.pose_write_calls} + (output / 'force-schedule.json').write_text(json.dumps(env.s.forces.manifest(), indent=2)) + (output / 'initial-counters.json').write_text(json.dumps(initial_counts)) + outcome = {'functional_success': False, 'requested_loops': sequence['loops'], 'completed_loops': 0} + try: + while env.s.time[0] < 240: + guard() + status = env.service.query() + if (payload.get('stop_skill') == status['skill'] and status['ready'] and not status['stop_requested']): + env.service.request_stop() + finished = composer.update() + env.central_progress = {'completed_loops': composer.completed_loops, 'requested_loops': composer.loops, + 'stop_requested': env.service.query()['stop_requested']} + if finished: + outcome['functional_success'] = not composer.safely_stopped + outcome['safe_stop_success'] = composer.safely_stopped + break + # The public switch may have changed skill/reference at this same + # physical clock; actor must consume the post-command observation. + observation = env.s.policy() + with torch.no_grad(): + action = torch.zeros(1, 3, device=env.device) if model is None else model.act(observation, deterministic=True)[0] + observation, _, terminated, _, _ = env.step(action) + observation = observation['policy'] + if bool(terminated.any()): + break + outcome.update(completed_loops=composer.completed_loops, status=env.service.query(), + transitions=composer.transitions, + actual_ticks=env.total_ticks, maximum=trace.maximum, + initialization_counters=initial_counts, + final_counters={'resets': env.runtime_hooks.reset_calls, + 'pose_writes': env.runtime_hooks.pose_write_calls}) + outcome['no_rollout_reset_or_pose_write'] = initial_counts == outcome['final_counters'] + outcome['functional_success'] &= outcome['no_rollout_reset_or_pose_write'] + if outcome.get('safe_stop_success'): + outcome['safe_stop_success'] &= (outcome['no_rollout_reset_or_pose_write'] + and outcome['status']['skill'] == 'support' + and outcome['status']['ready'] and outcome['status']['stopped']) + return outcome + finally: + trace.save() + (output / 'transitions.json').write_text(json.dumps(composer.transitions, indent=2)) + (output / 'public-call-receipts.json').write_text(json.dumps(env.s.transitions, indent=2)) + + +def train(env, payload, output, guard, trace): + import torch + from SkillFactory.legs.four_skills.model import FourSkillActorCritic, save_checkpoint + from SkillFactory.legs.standing_ppo import ppo_step + torch.set_num_threads(4) + cfg = env.cfg.four_parameters['training'] + torch.manual_seed(cfg['seed']) + torch.cuda.manual_seed_all(cfg['seed']) + model = FourSkillActorCritic().to(env.device) + initial_actor = {k: v.detach().clone() for k, v in model.actor.named_parameters()} + opt = torch.optim.Adam(model.parameters(), lr=cfg['algorithm']['learning_rate'], eps=cfg['algorithm']['adam_eps']) + identity = {'task': env.cfg.four_parameters['task_id'], 'config_sha256': payload['config_sha256'], + 'source_sha256': payload['sources'], 'train_seed': cfg['seed'], + 'observation': 'four-skills-48-fixed-si-v1', 'device': torch.cuda.get_device_name(0)} + save_checkpoint(output / 'untrained.pt', model, opt, identity, 0) + x = env.reset(seed=cfg['seed'])[0]['policy'] + trace.initial = trace.snapshot(env.s) + driver = TrainingComposer(env) + driver.update() + failures = {k: 0 for k in env.s.failure_masks} + history = [] + total_transitions = 0 + skill_control_samples = [0] * 4 + update_limit = payload.get('updates') or cfg['updates'] + for update in range(1, update_limit + 1): + guard() + tier = next(t for start, end, t in cfg['tier_updates'] if start <= update <= end) + if tier != env.s.force_tier: + env.s.force_tier = tier + # New training episodes at explicit curriculum boundary, not a hold + # reset, and not a paired independent evaluation. + x = env.reset()[0]['policy'] + data = {key: torch.empty(cfg['horizon'], env.num_envs, *shape, device=env.device, dtype=dtype) + for key, shape, dtype in [('features', (48,), torch.float32), ('latent', (3,), torch.float32), + ('log_prob', (), torch.float32), ('value', (), torch.float32), ('reward', (), torch.float32), + ('next_value', (), torch.float32), ('done', (), torch.bool)]} + update_failures = 0 + with torch.no_grad(): + for step in range(cfg['horizon']): + guard() + x = env.s.policy() # already post-command from previous boundary. + for k, n in enumerate(torch.bincount(env.s.skill, minlength=4).cpu().tolist()): + skill_control_samples[k] += n + action, latent, logp, value = model.act(x) + obs, reward, terminated, truncated, info = env.step(action) + # Advance exactly once, then bootstrap the very state that the + # next actor will see. Terminal snapshots remain pre-reset. + driver.update() + nx = env.s.policy() + nv = model.value(nx) + timeout = truncated & ~terminated + if bool(timeout.any()): + nv[timeout] = model.value(info['terminal_observation']['policy'][timeout]) + nv[terminated] = 0 + for key, v in [('features', x), ('latent', latent), ('log_prob', logp), ('value', value), + ('reward', reward), ('next_value', nv), ('done', terminated | truncated)]: + data[key][step].copy_(v) + for reason, count in info['failure_counts'].items(): + failures[reason] = failures.get(reason, 0) + count + update_failures += int(terminated.sum()) + x = nx + guard() + # Physical rollout only, GPU optimizer only. No saved robot trace replay. + metrics = ppo_step(model, opt, data, cfg['algorithm']) + total_transitions += cfg['horizon'] * env.num_envs + row = {'update': update, 'tier': tier, 'environment_transitions': total_transitions, + 'actual_physics_calls': env.total_ticks, 'episode_failures': update_failures, + 'mean_reward': float(data['reward'].mean()), 'learner': metrics, + 'skill_control_samples': skill_control_samples[:], + 'feedback_skill_samples': driver.skill_counts[:], 'transitions': driver.transition_count, + 'state_diagnostics': {'current_skill_counts': torch.bincount(env.s.skill, minlength=4).cpu().tolist(), + 'ready_count': int(env.s.ready.sum()), 'max_age_s': float(env.s.age.max()), + 'max_held_s': float(env.s.held.max()), 'min_gap_m': float((env.s.foot_center()[:, 2]-env.s.bundle['foot']['radius']).min())}} + history.append(row) + with (output / 'progress.jsonl').open('a') as stream: + stream.write(json.dumps(row, allow_nan=False) + '\n') + save_checkpoint(output / 'latest.pt', model, opt, identity, update, row) + if update % cfg['checkpoint_interval'] == 0 or update == update_limit: + save_checkpoint(output / f'checkpoint_u{update:04d}.pt', model, opt, identity, update, row) + torch.save(trace.snapshot(env.s), output / f'state_u{update:04d}.pt') + print(json.dumps({'training': row}), flush=True) + changed = {k: float((v.detach() - initial_actor[k]).abs().max()) for k, v in model.actor.named_parameters()} + trace.save() + return {'training_complete': True, 'updates': len(history), 'optimizer_device': env.device, + 'environment_transitions': total_transitions, 'actual_ticks': env.total_ticks, + 'failures': failures, 'actor_parameter_max_changes': changed, + 'skill_control_samples': skill_control_samples, 'feedback_skill_samples': driver.skill_counts, + 'transition_count': driver.transition_count, + 'candidate_selection_pending_development_validation': True, 'functional_success': False} + + +def run(payload, guard, write): + import torch + from isaaclab.app import AppLauncher + output = Path(payload['output']) + gui = payload['mode'] == 'gui' + launcher = app = env = hooks = paint = trace = None + result = {'success': False, 'functional_success': False, 'mode': payload['mode']} + code = 1 + try: + options = {'visualizer': 'kit'} if gui else {} + launcher = AppLauncher(headless=not gui, device='cuda:0', fast_shutdown=True, + kit_args=f'--/log/file={output / "native" / "kit.log"}', **options) + app = launcher.app + import evaluate_cycle_sim_residual as owner + from SkillFactory.legs.contact_cycle_sim import evaluation_binding as binding + from SkillFactory.tasks.manager_based.contact_cycle_sim.evaluation_env import RuntimeHooks + from SkillFactory.tasks.manager_based.four_skills.cfg import FourSkillsEnvCfg + from SkillFactory.tasks.manager_based.four_skills.env import FourSkillsEnv + for name in dict.fromkeys((*binding.REQUIRED_RUNTIME, *binding.ISAAC_PINS)): + importlib.import_module(name) + verify_project_imports() + owner.preimport_replicator() + owner.preimport_constructor_modules(binding) + owner.physx_constructor_export(binding, resolve=True) + if torch.cuda.get_device_name(0) != 'NVIDIA GeForce RTX 5080': + raise RuntimeError('Only approved RTX5080, no CPU training fallback') + cfg = FourSkillsEnvCfg() + cfg.scene.num_envs = 128 if payload['mode'] == 'train' else 1 + cfg.training_mode = payload['mode'] == 'train' + cfg.force_tier = payload['tier'] + cfg.seed = payload['seed'] + cfg.sim.device = 'cuda:0' + if gui: + from isaaclab_visualizers.kit import KitVisualizerCfg + cfg.sim.visualizer_cfgs = [KitVisualizerCfg()] + owner.preforward_video_recorder(cfg, importlib.import_module(owner.TASK + 'evaluation_env')) + owner.preimport_config_references(cfg, binding) + normalize_scene(cfg, binding) + before = binding.canonical_config(cfg) + (output / 'effective-config-before.json').write_bytes(before) + trace = Trace(output, guard, full=not cfg.training_mode) + def before_step(h): + guard() + if h.attempted_steps > payload['max_physics_calls']: + raise RuntimeError('Physical call cap exhausted before call') + with RuntimeHooks.isaac(before_step=before_step) as hooks: + env = FourSkillsEnv(cfg, runtime_hooks=hooks, guard=guard, record=trace) + write(output / 'runtime-identity.json', { + 'task': cfg.four_parameters['task_id'], 'torch': str(torch.__version__), + 'device': torch.cuda.get_device_name(0), 'num_envs': env.num_envs, + 'physics_dt_s': env.physics_dt, 'decimation': cfg.decimation, + 'payload_body_index': env.s.payload, 'body_names': list(env.s.robot.body_names), + 'joint_order': list(env.s.bundle['joint_order']), 'fixed_base': bool(env.s.robot.is_fixed_base), + 'effective_weight_N': env.s.weight.cpu().tolist(), + 'auxiliary_parameters': cfg.cycle_parameters['support'], + 'guide_stiffness': cfg.support_parameters['guide_stiffness'], + 'guide_damping': cfg.support_parameters['guide_damping'], + 'guide_wrench_limit': cfg.support_parameters['guide_wrench_limit']}) + actual = binding.canonical_config(cfg) + (output / 'effective-config-actual.json').write_bytes(actual) + if before != actual: + raise RuntimeError('Constructor changed effective bound config') + if gui: + from SkillFactory.tasks.manager_based.four_skills.gui import Presentation + paint = Presentation(app, env, output) + paint.controller_label = payload['controller'] + env.paint = paint + model = None + if payload.get('checkpoint'): + from SkillFactory.legs.four_skills.model import load_policy + model, ckpt = load_policy(payload['checkpoint'], 'cuda:0', payload['checkpoint_sha256']) + if (ckpt['identity'].get('config_sha256') != payload['config_sha256'] + or ckpt['identity'].get('task') != cfg.four_parameters['task_id']): + raise RuntimeError('Frozen new-stage checkpoint/config/task identity mismatch') + write(output / 'checkpoint-identity.json', {'sha256': payload['checkpoint_sha256'], + 'update': ckpt['update'], 'identity': ckpt['identity']}) + if cfg.training_mode: + result.update(train(env, payload, output, guard, trace)) + else: + result.update(execute_single(env, payload, output, guard, trace, model)) + guard() + write(output / 'runtime-project-imports.json', verify_project_imports()) + if binding.canonical_config(env.cfg) != actual or env.cfg is not cfg: + raise RuntimeError('Actual config changed during rollout') + if gui: + paint.finish(result['functional_success'], safely_stopped=result.get('safe_stop_success', False)) + result['gui'] = paint.report() + result['physics_counts'] = {'attempted': hooks.attempted_steps, 'completed': hooks.completed_steps, + 'rollout': hooks.rollout_steps, 'initialization': hooks.initialization_steps, + 'resets': hooks.reset_calls, 'pose_writes': hooks.pose_write_calls, + 'violations': hooks.violations} + if hooks.violations: + raise RuntimeError('Runtime reset/pose/ownership violation') + torch.save(trace.snapshot(env.s), output / 'terminal-state.pt') + env.close() + env = None + result['success'] = True # integrity only; task function is separate. + result['postclose_identity_verified'] = True + code = 0 + except BaseException: + result.update(success=False, error=traceback.format_exc()) + traceback.print_exc() + finally: + if hooks is not None: + result['physics_counts'] = {'attempted': hooks.attempted_steps, 'completed': hooks.completed_steps, + 'rollout': hooks.rollout_steps, 'initialization': hooks.initialization_steps, + 'resets': hooks.reset_calls, 'pose_writes': hooks.pose_write_calls, + 'violations': hooks.violations} + if trace is not None: + try: + trace.save() + except BaseException: + result.update(success=False, trace_save_error=traceback.format_exc()) + code = 1 + if env is not None: + try: + if trace is not None: + torch.save(trace.snapshot(env.s), output / 'terminal-state.pt') + env.close() + except BaseException: + result.update(success=False, close_error=traceback.format_exc()) + code = 1 + if paint is not None: + try: + paint.release() + except BaseException: + result.update(success=False, gui_release_error=traceback.format_exc()) + code = 1 + write(output / 'worker-result.json', result) + if app is not None: + app.close(exit_code=code) + return code diff --git a/source/SkillFactory/SkillFactory/legs/four_skills/__init__.py b/source/SkillFactory/SkillFactory/legs/four_skills/__init__.py new file mode 100644 index 0000000..f18c43a --- /dev/null +++ b/source/SkillFactory/SkillFactory/legs/four_skills/__init__.py @@ -0,0 +1 @@ +"""Additive four-skill simulation contract; legacy cycle identities are unchanged.""" diff --git a/source/SkillFactory/SkillFactory/legs/four_skills/api.py b/source/SkillFactory/SkillFactory/legs/four_skills/api.py new file mode 100644 index 0000000..aa017c3 --- /dev/null +++ b/source/SkillFactory/SkillFactory/legs/four_skills/api.py @@ -0,0 +1,109 @@ +"""Public in-process skill service and feedback-only central composition. + +No requested force, support load, external-force metadata or privileged callbacks +cross this seam. The same service is used by training, evaluation and the demo. +""" +import math + +SKILLS = ('support', 'retract', 'swing', 'plant') +LEGAL = {'support': ('retract',), 'retract': ('swing', 'plant'), + 'swing': ('swing', 'plant'), 'plant': ('support',)} + + +def seconds(value): + if type(value) not in (int, float) or not math.isfinite(value) or not 0 <= value <= 10: + raise ValueError('hold_seconds must be finite in [0,10]') + return float(value) + + +def direction(value): + if (not isinstance(value, (list, tuple)) or len(value) != 2 + or any(type(x) not in (int, float) or not math.isfinite(x) for x in value) + or not .008 <= math.hypot(*value) <= .04): + raise ValueError('swing direction_m must be finite XY, norm .008..04m') + return tuple(float(x) for x in value) + + +class SkillService: + def __init__(self, state): + self._state = state + + def call(self, skill, *, hold_seconds=0., direction_m=None, env_ids=None): + if skill not in SKILLS: + raise ValueError('Unknown skill') + duration = seconds(hold_seconds) + vector = direction(direction_m) if skill == 'swing' else None + if skill != 'swing' and direction_m is not None: + raise ValueError('Only swing accepts a spatial direction') + # State validates all selected rows before mutating any row. + return self._state.request(skill, duration, vector, env_ids) + + switch = call + + def hold(self, duration, *, env_ids=None): + return self._state.hold(seconds(duration), env_ids) + + def query(self, *, env_ids=None): + return self._state.query(env_ids) + + def request_stop(self, *, env_ids=None): + return self._state.request_stop(env_ids) + + +class CentralComposer: + """Feedback-driven scalar demo/evaluation program; no physics/state writes. + + A/B are fixed *commanded* ground endpoints. Direction reverses every other + loop rather than accumulating root/foot drift or reanchoring measured landings. + """ + def __init__(self, service, sequence): + self.service = service + self.vector = direction(sequence['direction_m']) + holds = sequence['holds_s'] + if not isinstance(holds, (list, tuple)) or len(holds) != 4: + raise ValueError('Four skill holds required') + self.holds = tuple(seconds(x) for x in holds) + self.loops = sequence['loops'] + if type(self.loops) is not int or not 1 <= self.loops <= 5: + raise ValueError('One to five loops required') + self.completed_loops = 0 + self.started = self.finished = self.safely_stopped = False + self.transitions = [] + + def update(self): + status = self.service.query() + if not self.started: + self.service.hold(self.holds[0]) + self.started = True + status = self.service.query() + if status['failed']: + raise RuntimeError('Central feedback failure: ' + ','.join(status['failure_reasons'])) + if status['stop_requested'] and self.completed_loops < self.loops: + self.safely_stopped = bool(status['stopped'] and status['skill'] == 'support' and status['ready']) + self.finished = self.safely_stopped + return self.finished + if self.finished or not status['ready'] or not status['hold_complete']: + return self.finished + skill = status['skill'] + if skill == 'support': + if self.completed_loops == self.loops: + self.service.request_stop() + self.finished = bool(self.service.query()['stopped']) + return self.finished + next_skill, hold, vector = 'retract', self.holds[1], None + elif skill == 'retract': + sign = 1 if self.completed_loops % 2 == 0 else -1 + next_skill, hold = 'swing', self.holds[2] + vector = [sign * x for x in self.vector] + elif skill == 'swing': + next_skill, hold, vector = 'plant', self.holds[3], None + elif skill == 'plant': + self.completed_loops += 1 + next_skill, hold, vector = 'support', self.holds[0], None + else: + raise RuntimeError('Invalid public skill feedback') + self.service.call(next_skill, hold_seconds=hold, direction_m=vector) + self.transitions.append({'from': skill, 'to': next_skill, + 'at_tick': status['tick'], 'completed_loops': self.completed_loops, + 'feedback_ready': True, 'feedback_hold_complete': True}) + return False diff --git a/source/SkillFactory/SkillFactory/legs/four_skills/forces.py b/source/SkillFactory/SkillFactory/legs/four_skills/forces.py new file mode 100644 index 0000000..0bd4701 --- /dev/null +++ b/source/SkillFactory/SkillFactory/legs/four_skills/forces.py @@ -0,0 +1,179 @@ +"""Pure, precomputed true-force schedules; never policy observations. + +Forces are world-frame Newtons applied at the payload COM with zero requested +couple. This module does not apply forces or depend on simulation/Kit imports. +""" + +import hashlib +import json +import math +import random +from collections.abc import Sequence +from numbers import Real +from types import MappingProxyType + +import torch + + +_TIER_MAX_N = MappingProxyType({"none": 0.0, "small": 1.5, "medium": 4.0, "full": 8.0}) +_SLOT_S = 2.0 +_RAMP_S = 0.15 +_ACTIVE_PROBABILITY = 0.8 + + +def _integer(value, name): + if type(value) is not int: + raise TypeError(f"{name} must be an exact int (not bool)") + return value + + +def _integers(values, name, *, unique=False): + if not isinstance(values, Sequence) or isinstance(values, (str, bytes)): + raise TypeError(f"{name} must be a sequence of exact ints") + result = [_integer(value, name) for value in values] + if unique and len(set(result)) != len(result): + raise ValueError(f"{name} must be unique") + return result + + +def _tier(value): + if not isinstance(value, str) or value not in _TIER_MAX_N: + raise ValueError(f"unknown force tier: {value!r}") + return value + + +class ForceSchedule: + """Episode-keyed schedules independent of row ordering and global RNG state. + + ``env_ids`` are immutable identities. ``reset(ids, ...)`` instead selects row + indices; only those rows change. A tier change affects only selected rows. + Evaluation pairs must use the same seed, environment and episode identities. + The manifest is audit data, not an actor observation. + """ + + def __init__(self, seed: int, env_ids: Sequence[int], tier: str, device, + duration_s: float = 300, episode_ids: Sequence[int] | None = None): + self._seed = _integer(seed, "seed") + self._env_ids = _integers(env_ids, "env_ids", unique=True) + tier = _tier(tier) + if (isinstance(duration_s, bool) or not isinstance(duration_s, Real) + or not math.isfinite(duration_s) or duration_s <= 0): + raise ValueError("duration_s must be finite and positive") + self._duration_s = float(duration_s) + episodes = ([0] * len(self._env_ids) if episode_ids is None + else _integers(episode_ids, "episode_ids")) + if len(episodes) != len(self._env_ids): + raise ValueError("episode_ids must match env_ids length") + self._device = torch.device(device) + self._slot_count = max(1, math.ceil(self._duration_s / _SLOT_S)) + self._rows = [] + batch = len(self._env_ids) + # Float64 timing avoids rounding away short ramps late in an episode. + self._onsets = torch.empty((batch, self._slot_count), dtype=torch.float64, + device=self._device) + self._ends = torch.empty_like(self._onsets) + self._peaks = torch.empty((batch, self._slot_count, 3), dtype=torch.float64, + device=self._device) + for row, episode in enumerate(episodes): + record = self._sample(self._env_ids[row], episode, tier) + self._store(row, record) + self._rows.append(record) + + def _sample(self, env_id, episode_id, tier): + key = json.dumps([self._seed, env_id, episode_id], separators=(",", ":")) + digest = hashlib.sha256(key.encode("ascii")).digest() + rng = random.Random(int.from_bytes(digest, "big")) + maximum = _TIER_MAX_N[tier] + events = [] + for slot in range(self._slot_count): + active = rng.random() < _ACTIVE_PROBABILITY + onset = slot * _SLOT_S + rng.uniform(0.2, 0.5) + duration = rng.uniform(0.5, 1.3) + amplitude = rng.uniform(0.25 * maximum, maximum) + # Uniform azimuth and uniform z give an isotropic 3-D unit vector. + z = rng.uniform(-1.0, 1.0) + azimuth = rng.uniform(0.0, 2.0 * math.pi) + radial = math.sqrt(max(0.0, 1.0 - z * z)) + direction = [radial * math.cos(azimuth), radial * math.sin(azimuth), z] + events.append({ + "slot_index": slot, "slot_start_s": slot * _SLOT_S, + "active": active, "onset_s": onset, "duration_s": duration, + "end_s": onset + duration, "amplitude_N": amplitude, + "direction_world": direction, + "peak_force_world_N": [amplitude * v if active else 0.0 for v in direction], + "truncated_by_horizon": onset + duration > self._duration_s, + "onset_beyond_horizon": onset > self._duration_s, + }) + return {"env_id": env_id, "episode_id": episode_id, "tier": tier, + "maximum_amplitude_N": maximum, "events": events} + + def _store(self, row, record): + events = record["events"] + self._onsets[row] = torch.tensor([e["onset_s"] for e in events], + dtype=torch.float64, device=self._device) + self._ends[row] = torch.tensor([e["end_s"] for e in events], + dtype=torch.float64, device=self._device) + self._peaks[row] = torch.tensor([e["peak_force_world_N"] for e in events], + dtype=torch.float64, device=self._device) + + def at(self, time: torch.Tensor) -> torch.Tensor: + """Return float32 [B, 3] world force; accept only times in [0, horizon].""" + if not isinstance(time, torch.Tensor): + raise TypeError("time must be a torch.Tensor") + if time.ndim != 1 or time.shape[0] != len(self._env_ids): + raise ValueError("time must have shape [number of environments]") + if time.dtype == torch.bool or time.is_complex(): + raise TypeError("time must contain real, non-boolean numbers") + time = time.to(device=self._device, dtype=torch.float64) + if not bool(torch.isfinite(time).all()) or bool((time < 0).any()): + raise ValueError("time must be finite and nonnegative") + if bool((time > self._duration_s).any()): + raise ValueError("time exceeds registered duration_s") + slots = (time / _SLOT_S).floor().long().clamp(max=self._slot_count - 1) + rows = torch.arange(len(self._env_ids), device=self._device) + onset = self._onsets[rows, slots] + end = self._ends[rows, slots] + # A raised cosine on each edge has max slope A*pi/(2*0.15), + # or 83.776 N/s at 8 N, below the 100 N/s contract. + edge = torch.minimum(time - onset, end - time).clamp(0.0, _RAMP_S) + envelope = 0.5 * (1.0 - torch.cos(math.pi * edge / _RAMP_S)) + return (self._peaks[rows, slots] * envelope[:, None]).to(torch.float32) + + def reset(self, ids: Sequence[int], episode_ids: Sequence[int], + tier: str | None = None): + """Resample selected row indices using explicit episode identities.""" + ids = _integers(ids, "ids", unique=True) + episodes = _integers(episode_ids, "episode_ids") + if len(ids) != len(episodes): + raise ValueError("episode_ids must match ids length") + if any(row < 0 or row >= len(self._env_ids) for row in ids): + raise ValueError("ids contains an out-of-range row index") + if tier is not None: + tier = _tier(tier) + records = [self._sample(self._env_ids[row], episode, + self._rows[row]["tier"] if tier is None else tier) + for row, episode in zip(ids, episodes)] + for row, record in zip(ids, records): + self._store(row, record) + self._rows[row] = record + + def manifest(self) -> dict: + """Return detached, JSON-serializable parameters and every sampled slot.""" + return json.loads(json.dumps({ + "schema": "four_skills_force_schedule_v0.5.0", + "seed": self._seed, "duration_s": self._duration_s, + "rng": "python.Random; SHA256(ASCII compact JSON [seed,env_id,episode_id]) big-endian seed", + "frame": "world", "application_point": "payload_com", + "requested_couple_Nm": [0.0, 0.0, 0.0], + "tier_maxima_N": dict(_TIER_MAX_N), + "slot_duration_s": _SLOT_S, "active_probability": _ACTIVE_PROBABILITY, + "onset_offset_uniform_s": [0.2, 0.5], + "duration_uniform_s": [0.5, 1.3], + "amplitude_uniform_fraction_of_max": [0.25, 1.0], + "direction_distribution": "isotropic 3D: uniform z in [-1,1], azimuth in [0,2*pi]", + "envelope": "raised cosine rise and fall; constant plateau", + "rise_s": _RAMP_S, "fall_s": _RAMP_S, + "maximum_force_derivative_N_per_s": 8.0 * math.pi / (2.0 * _RAMP_S), + "horizon_rule": "sample slots starting before horizon without clipping; at(horizon) allowed", + "rows": self._rows, + }, allow_nan=False)) diff --git a/source/SkillFactory/SkillFactory/legs/four_skills/model.py b/source/SkillFactory/SkillFactory/legs/four_skills/model.py new file mode 100644 index 0000000..af7003e --- /dev/null +++ b/source/SkillFactory/SkillFactory/legs/four_skills/model.py @@ -0,0 +1,265 @@ +"""Pure-Torch 48D four-skills residual policy; no simulator or robot-data imports.""" + +import hashlib +import io +import json +import math +import os +from pathlib import Path +import tempfile + +import torch +from torch import nn +from torch.distributions import Normal + +FEATURE_VERSION = "four-skills-48-fixed-si-v1" +FEATURE_DIM = 48 +CHECKPOINT_FORMAT = "skillfactory.four-skills-ppo.v1" + + +def _model_config(hidden, initial_std): + if type(hidden) is not int or hidden < 8: + raise ValueError("hidden must be an integer >= 8") + if type(initial_std) not in (int, float) or not math.isfinite(initial_std) or not 0.02 <= initial_std <= 0.5: + raise ValueError("initial_std must be finite and in [0.02, 0.5]") + return {"hidden": hidden, "initial_std": float(initial_std)} + + +def features(x): + """Validate q3,qdot3,wrench6,gravity3,omega3,root-error3,root-vel3, + foot-relative3,foot-vel3,contact3,gap1,skill4,target-relative3,progress1, + analytic-reference3,previous-residual3, in that order. No privileged labels. + """ + if ( + not isinstance(x, torch.Tensor) + or x.layout != torch.strided + or x.device.type == "meta" + or x.ndim != 2 + or x.shape[0] < 1 + or x.shape[1] != FEATURE_DIM + or x.dtype != torch.float32 + or not torch.isfinite(x).all() + ): + raise ValueError("features must be finite float32 [B,48], B >= 1") + return x + + +class FourSkillActorCritic(nn.Module): + """Actor and critic see the same state; the environment maps tanh output to ±0.05 rad.""" + + feature_version = FEATURE_VERSION + + def __init__(self, hidden=128, initial_std=0.08): + super().__init__() + self.model_config = _model_config(hidden, initial_std) + offset = torch.zeros(FEATURE_DIM, dtype=torch.float32) + offset[:3] = torch.tensor([0.0, 0.8, -1.5]) + scale = torch.ones(FEATURE_DIM, dtype=torch.float32) + scale[:3] = 0.5 + scale[3:6] = 10.0 + scale[6:12] = torch.tensor([50.0, 50.0, 50.0, 5.0, 5.0, 5.0]) + scale[15:18] = 5.0 + scale[18:21] = 0.1 + scale[21:24] = 0.5 + scale[24:27] = 0.4 + scale[30:33] = 50.0 + scale[33] = 0.05 + scale[38:41] = 0.4 + self.register_buffer("offset", offset) + self.register_buffer("scale", scale) + self.actor = nn.Sequential( + nn.Linear(FEATURE_DIM, hidden), nn.Tanh(), nn.Linear(hidden, hidden), nn.Tanh(), nn.Linear(hidden, 3) + ) + self.critic = nn.Sequential( + nn.Linear(FEATURE_DIM, hidden), nn.Tanh(), nn.Linear(hidden, hidden), nn.Tanh(), nn.Linear(hidden, 1) + ) + self.float() + for module in self.modules(): + if isinstance(module, nn.Linear): + nn.init.orthogonal_(module.weight, math.sqrt(2)) + nn.init.zeros_(module.bias) + # Exact analytic baseline prior, independent of state and random seed. + nn.init.zeros_(self.actor[-1].weight) + nn.init.zeros_(self.actor[-1].bias) + nn.init.orthogonal_(self.critic[-1].weight, 1.0) + self.log_std = nn.Parameter(torch.full((3,), math.log(initial_std), dtype=torch.float32)) + + def distribution_value(self, x): + x = features(x) + try: + x = x.to(device=self.offset.device) + except (RuntimeError, TypeError) as exc: + raise ValueError("Cannot move features to model device") from exc + x = ((x - self.offset) / self.scale).clamp(-10, 10) + return Normal( + self.actor(x), self.log_std.clamp(math.log(0.02), math.log(0.5)).exp(), validate_args=False + ), self.critic(x).squeeze(-1) + + def act(self, x, deterministic=False): + if type(deterministic) is not bool: + raise ValueError("deterministic must be bool") + dist, value = self.distribution_value(x) + latent = dist.mean if deterministic else dist.sample() + # PPO stores fixed Gaussian latents: the tanh Jacobian cancels in ratios. + return latent.tanh(), latent, dist.log_prob(latent).sum(-1), value + + def evaluate(self, x, latent): + x = features(x) + if ( + not isinstance(latent, torch.Tensor) + or latent.layout != torch.strided + or latent.device.type == "meta" + or latent.shape != (len(x), 3) + or latent.dtype != torch.float32 + or not torch.isfinite(latent).all() + ): + raise ValueError("latent must be finite float32 [B,3]") + try: + latent = latent.to(device=self.offset.device) + except (RuntimeError, TypeError) as exc: + raise ValueError("Cannot move latent to model device") from exc + dist, value = self.distribution_value(x) + return dist.log_prob(latent).sum(-1), value + + def value(self, x): + return self.distribution_value(x)[1] + + +def _plain_tree(value): + """Snapshot only weights-only-safe data, rejecting nonfinite tensors or metadata.""" + if type(value) is torch.Tensor: + if value.layout != torch.strided or value.device.type == "meta" or not torch.isfinite(value).all(): + raise ValueError("Checkpoint contains nonfinite or unsupported tensor") + return value.detach().cpu().clone() + if type(value) is dict: + if any(type(key) not in (str, int) for key in value): + raise ValueError("Checkpoint dictionary keys must be strings or integers") + return {key: _plain_tree(item) for key, item in value.items()} + if type(value) in (list, tuple): + return type(value)(_plain_tree(item) for item in value) + if value is None or type(value) in (str, bool, int): + return value + if type(value) is float and math.isfinite(value): + return value + raise ValueError("Checkpoint must contain only finite tensors and plain data") + + +def _canonical_identity(identity): + if type(identity) is not dict or not identity: + raise ValueError("identity must be a nonempty plain dictionary") + try: + return json.dumps(identity, sort_keys=True, separators=(",", ":"), allow_nan=False) + except (TypeError, ValueError) as exc: + raise ValueError("identity must contain canonical JSON data") from exc + + +def _validate_payload(payload): + keys = {"format", "features", "feature_dim", "model_config", "model", "optimizer", "identity", "update", "metrics", "rng"} + if type(payload) is not dict or set(payload) != keys: + raise ValueError("Malformed four-skills checkpoint fields") + if payload["format"] != CHECKPOINT_FORMAT or payload["features"] != FEATURE_VERSION: + raise ValueError("Wrong four-skills checkpoint format or feature version") + if type(payload["feature_dim"]) is not int or payload["feature_dim"] != FEATURE_DIM: + raise ValueError("Wrong four-skills feature dimension; legacy policies are incompatible") + config = payload["model_config"] + if type(config) is not dict or set(config) != {"hidden", "initial_std"}: + raise ValueError("Malformed model_config") + _model_config(**config) + if type(payload["update"]) is not int or payload["update"] < 0: + raise ValueError("update must be a nonnegative integer") + _canonical_identity(payload["identity"]) + if payload["metrics"] is not None and type(payload["metrics"]) is not dict: + raise ValueError("metrics must be a dictionary or None") + if type(payload["model"]) is not dict or type(payload["optimizer"]) is not dict: + raise ValueError("Malformed model or optimizer state") + if set(payload["optimizer"]) != {"state", "param_groups"}: + raise ValueError("Malformed optimizer state") + if type(payload["optimizer"]["state"]) is not dict or type(payload["optimizer"]["param_groups"]) is not list: + raise ValueError("Malformed optimizer state") + rng = payload["rng"] + if type(rng) is not dict or set(rng) != {"cpu", "cuda"} or type(rng["cuda"]) is not list: + raise ValueError("Malformed RNG state") + for state in [rng["cpu"], *rng["cuda"]]: + if not isinstance(state, torch.Tensor) or state.dtype != torch.uint8 or state.ndim != 1 or state.numel() == 0: + raise ValueError("RNG state must contain nonempty uint8 vectors") + if rng["cpu"].shape != torch.get_rng_state().shape: + raise ValueError("Invalid CPU RNG state size") + + +def _load_model_state(model, state): + expected = model.state_dict() + if set(state) != set(expected): + raise ValueError("Wrong four-skills model state fields") + for key, value in state.items(): + if not isinstance(value, torch.Tensor) or value.shape != expected[key].shape or value.dtype != expected[key].dtype: + raise ValueError(f"Invalid model tensor: {key}") + for key in ("offset", "scale"): + if not torch.equal(state[key].cpu(), expected[key].cpu()): + raise ValueError("Checkpoint changes fixed SI normalization") + model.load_state_dict(state, strict=True) + + +def save_checkpoint(path, model, optimizer, identity, update, metrics=None): + """Atomically publish a finite, plain-data checkpoint (no simulator resume state).""" + if not isinstance(model, FourSkillActorCritic) or not isinstance(optimizer, torch.optim.Optimizer): + raise ValueError("Expected FourSkillActorCritic and Torch optimizer") + payload = { + "format": CHECKPOINT_FORMAT, + "features": FEATURE_VERSION, + "feature_dim": FEATURE_DIM, + "model_config": model.model_config, + "model": dict(model.state_dict()), + "optimizer": optimizer.state_dict(), + "identity": identity, + "update": update, + "metrics": metrics, + "rng": {"cpu": torch.get_rng_state(), "cuda": torch.cuda.get_rng_state_all() if torch.cuda.is_available() else []}, + } + payload = _plain_tree(payload) + _validate_payload(payload) + # Validate architecture and fixed buffers before creating/replacing any artifact. + with torch.random.fork_rng(devices=[]): + check = FourSkillActorCritic(**payload["model_config"]) + _load_model_state(check, payload["model"]) + path = Path(path) + path.parent.mkdir(parents=True, exist_ok=True) + temporary = None + try: + with tempfile.NamedTemporaryFile(dir=path.parent, delete=False) as stream: + temporary = Path(stream.name) + torch.save(payload, stream) + stream.flush() + os.fsync(stream.fileno()) + os.replace(temporary, path) + finally: + if temporary is not None: + temporary.unlink(missing_ok=True) + + +def load_policy(path, device="cpu", expected_sha256=None, expected_identity=None): + """Verify optional digest before deserialization; return an eval model and payload.""" + contents = Path(path).read_bytes() + if expected_sha256 is not None: + if ( + type(expected_sha256) is not str + or len(expected_sha256) != 64 + or any(c not in "0123456789abcdefABCDEF" for c in expected_sha256) + or hashlib.sha256(contents).hexdigest() != expected_sha256.lower() + ): + raise ValueError("Checkpoint SHA256 mismatch or malformed expected digest") + try: + payload = torch.load(io.BytesIO(contents), map_location="cpu", weights_only=True) + except Exception as exc: + raise ValueError("Cannot read a weights-only four-skills checkpoint") from exc + payload = _plain_tree(payload) + _validate_payload(payload) + if expected_identity is not None and _canonical_identity(payload["identity"]) != _canonical_identity(expected_identity): + raise ValueError("Four-skills checkpoint identity mismatch") + with torch.random.fork_rng(devices=[]): + model = FourSkillActorCritic(**payload["model_config"]) + _load_model_state(model, payload["model"]) + try: + model.to(device=device) + except (RuntimeError, TypeError, ValueError, AssertionError) as exc: + raise ValueError(f"Cannot move four-skills model to device {device!r}") from exc + return model.eval(), payload diff --git a/source/SkillFactory/SkillFactory/tasks/manager_based/four_skills/__init__.py b/source/SkillFactory/SkillFactory/tasks/manager_based/four_skills/__init__.py new file mode 100644 index 0000000..4d1d186 --- /dev/null +++ b/source/SkillFactory/SkillFactory/tasks/manager_based/four_skills/__init__.py @@ -0,0 +1 @@ +"""Direct-construction four-skill task. Import only after Isaac AppLauncher.""" diff --git a/source/SkillFactory/SkillFactory/tasks/manager_based/four_skills/cfg.py b/source/SkillFactory/SkillFactory/tasks/manager_based/four_skills/cfg.py new file mode 100644 index 0000000..2240df3 --- /dev/null +++ b/source/SkillFactory/SkillFactory/tasks/manager_based/four_skills/cfg.py @@ -0,0 +1,70 @@ +"""Additive physical config: all original asset/auxiliary limits remain unchanged.""" +import json +from pathlib import Path + +from isaaclab.envs import mdp as base_mdp +from isaaclab.managers import ObservationGroupCfg, ObservationTermCfg, RewardTermCfg, TerminationTermCfg +from isaaclab.utils.configclass import configclass + +from SkillFactory.tasks.manager_based.contact_cycle_sim.cfg import SimContactCycleEnvCfg, RewardsCfg as CycleRewardsCfg +from SkillFactory.tasks.manager_based.single_leg_anchor.mdp.actions import BoundedJointPositionActionCfg +from . import mdp + +ROOT = Path(__file__).resolve().parents[6] + + +@configclass +class ObservationsCfg: + @configclass + class Policy(ObservationGroupCfg): + state = ObservationTermCfg(func=mdp.observation) + + def __post_init__(self): + self.enable_corruption = False + self.concatenate_terms = True + + policy = Policy() + + +@configclass +class RewardsCfg: + task = RewardTermCfg(func=mdp.reward, weight=1.) + + +@configclass +class TerminationsCfg: + time_out = TerminationTermCfg(func=base_mdp.time_out, time_out=True) + failed = TerminationTermCfg(func=mdp.failed) + + +@configclass +class FourSkillsEnvCfg(SimContactCycleEnvCfg): + four_config: str = str(ROOT / 'configs/four_skills/v050.json') + four_parameters: dict | None = None + force_tier: str = 'none' + training_mode: bool = False + observations = ObservationsCfg() + rewards = RewardsCfg() + terminations = TerminationsCfg() + + def __post_init__(self): + self.four_parameters = json.loads(Path(self.four_config).read_text()) + if self.four_parameters['format'] != 'skillfactory.four-skills.v0.5.0': + raise ValueError('Require independent four-skill configuration') + # The inherited constructor configures its own old reward terms before + # environment construction; use those only during that config phase. + skill_rewards = self.rewards + self.rewards = CycleRewardsCfg() + super().__post_init__() + self.rewards = skill_rewards + # Each environment owns a deterministic independently sampled real force + # schedule; legacy named scenarios are not superimposed. + self.cycle_parameters['events'] = [] + self.scene.num_envs = 1 + self.sim.dt = .002 + self.decimation = 5 + self.episode_length_s = 120. + self.actions.joint_position = BoundedJointPositionActionCfg( + class_type=mdp.FourSkillAction, asset_name='robot', + joint_names=self.leg_bundle['joint_order'], scale=.5, + preserve_order=True, use_default_offset=True) diff --git a/source/SkillFactory/SkillFactory/tasks/manager_based/four_skills/env.py b/source/SkillFactory/SkillFactory/tasks/manager_based/four_skills/env.py new file mode 100644 index 0000000..f9b2426 --- /dev/null +++ b/source/SkillFactory/SkillFactory/tasks/manager_based/four_skills/env.py @@ -0,0 +1,139 @@ +"""Explicit 500Hz safety stepping. Evaluation never autoresets or writes poses.""" +import torch +from isaaclab.envs import ManagerBasedRLEnv +from SkillFactory.legs.four_skills.api import SkillService +from .mdp import state + + +class FourSkillsEnv(ManagerBasedRLEnv): + def __init__(self, cfg, *, runtime_hooks, guard=lambda: None, record=None, paint=None): + self.runtime_hooks = runtime_hooks + self.guard, self.record, self.paint = guard, record, paint + self._reset_allowed = False + self._initial_reset_done = False + self.total_ticks = 0 + super().__init__(cfg) + self.s = state(self) + self.service = SkillService(self.s) + if (self._physics_handles_decimation or self.recorder_manager.active_terms + or self.physics_dt != .002 or self.cfg.decimation != 5 + or self.s.robot.is_fixed_base or self.cfg.sim.physics.solver_type != 0 + or self.cfg.sim.physics.enable_external_forces_every_iteration + or self.cfg.cycle_parameters['support'] != {'kp_N_m': 1800., 'kd_Ns_m': 120., 'max_N': 140.}): + raise RuntimeError('Require original aided floating-root PGS,500Hz/100Hz,unchanged auxiliary') + self.runtime_hooks.mode = 'locked' + self.runtime_hooks.guard_composer(self.s.robot.permanent_wrench_composer) + # Guard sensor and action resets as well as SDK pose writers. + for obj in [self.s.controller, self.action_manager, self.observation_manager, + *self.scene.sensors.values(), *self.s.robot.actuators.values()]: + self.runtime_hooks.guard_object(obj, ('reset',)) + + def _reset_idx(self, ids): + if not self._reset_allowed: + raise RuntimeError('No reset/pose overwrite inside held skills or composition') + self.runtime_hooks.require_initialization('four_skills._reset_idx') + ManagerBasedRLEnv._reset_idx(self, ids) + if hasattr(self, '_cycle_state'): + self._cycle_state.reset(ids) + + def reset(self, seed=None, env_ids=None, options=None): + if self._initial_reset_done and not self.cfg.training_mode: + raise RuntimeError('Exactly one initial reset for continuous evaluation') + if options is not None: + raise ValueError('No reset state options') + self._reset_allowed = True + try: + with self.runtime_hooks.scope('initialization'): + result = ManagerBasedRLEnv.reset(self, seed=seed, env_ids=env_ids) + self.s.controller.refresh(torch.arange(self.num_envs, device=self.device)) + self._initial_reset_done = True + return result + finally: + self._reset_allowed = False + + def reset_to(self, *args, **kwargs): + raise RuntimeError('No reset_to/state teleport API') + + def _training_reset(self, ids): + if not self.cfg.training_mode: + raise RuntimeError('Training reset prohibited during evaluation') + self._reset_allowed = True + try: + with self.runtime_hooks.scope('initialization'): + self._reset_idx(ids) + self.scene.write_data_to_sim() + self.sim.forward() + self.scene.update(0.) + self.s.controller.refresh(ids) + finally: + self._reset_allowed = False + + def step(self, action): + if not self._initial_reset_done: + raise RuntimeError('Initialize once before controls') + if (not isinstance(action, torch.Tensor) or action.shape != (self.num_envs, 3) + or action.dtype != torch.float32 or action.device != torch.device(self.device) + or not bool(torch.isfinite(action).all())): + raise ValueError('Finite float32[B,3] residual action required') + s = self.s + if bool(s.failed.any()) or (not self.cfg.training_mode and bool(s.stopped.any())): + raise RuntimeError('Never advance a failed/stopped session') + self.guard() + self.action_manager.process_action(action) + advanced = 0 + for _ in range(self.cfg.decimation): + self.guard() + if self.paint is not None and self.paint.closed: + raise InterruptedError('gui_window_closed_before_completion') + self._sim_step_counter += 1 + with self.runtime_hooks.wrench_update(s.robot.permanent_wrench_composer): + self.action_manager.apply_action() + self.scene.write_data_to_sim() + with self.runtime_hooks.scope('rollout'): + self.sim.step(render=False) + self.scene.update(dt=self.physics_dt) + s.after_physics(self.physics_dt) + self.total_ticks += 1 + advanced += 1 + if self.record is not None: + self.record(s) + # Preserve offending tick before any next physical step, including + # shortened training controls. Only new training episodes may reset. + if bool(s.failed.any()) or (not self.cfg.training_mode and bool(s.stopped.any())): + break + if self.paint is not None: + self.paint.check_health() + self.paint.update(self) + self.sim.render(skip_app_pumping=not self.render_enabled) + self.paint.check_health() + self.paint.after_render() + if self.paint.closed: + raise InterruptedError('gui_window_closed_before_completion') + elif self.sim.is_rendering and self._sim_step_counter % self.cfg.sim.render_interval == 0: + self.sim.render(skip_app_pumping=not self.render_enabled) + self.episode_length_buf += 1 + self.common_step_counter += 1 + s.controller.refresh(torch.arange(self.num_envs, device=self.device)) + reward = s.reward() + terminated = s.failed.clone() + truncated = s.time >= self.cfg.episode_length_s + terminal = s.policy().clone() + info = {'ticks_advanced': advanced, 'terminal_observation': {'policy': terminal}, + 'terminal_ids': torch.arange(self.num_envs, device=self.device), + 'failure_counts': {k: int(v.sum()) for k, v in s.failure_masks.items()}} + if self.cfg.training_mode: + ids = (terminated | truncated).nonzero().flatten() + if len(ids): + self._training_reset(ids) + if not self.cfg.training_mode and not bool(s.failed.any()): + # Route stops AFTER old-command physical evidence/reward and BEFORE + # the next observation/inference. Never apply an old-skill residual + # to a newly switched skill in the same step. + pending = (s.stop_requested & s.ready & ((s.skill == 1) | (s.skill == 2))).nonzero().flatten() + supported = (s.stop_requested & s.ready & (s.skill == 3)).nonzero().flatten() + if len(pending): + self.service.call('plant', hold_seconds=0., env_ids=pending) + if len(supported): + self.service.call('support', hold_seconds=0., env_ids=supported) + self.obs_buf = {'policy': s.policy()} + return self.obs_buf, reward, terminated, truncated, info diff --git a/source/SkillFactory/SkillFactory/tasks/manager_based/four_skills/gui.py b/source/SkillFactory/SkillFactory/tasks/manager_based/four_skills/gui.py new file mode 100644 index 0000000..d46ace1 --- /dev/null +++ b/source/SkillFactory/SkillFactory/tasks/manager_based/four_skills/gui.py @@ -0,0 +1,262 @@ +"""Genuine live Kit presentation and owned-X11 original-speed recording. + +Only camera/light/UI and nonphysical force-line prims are written. No Robot +poses, rigid-body fields, material/physics parameters, or control state writes. +""" +import ctypes +import json +import os +from pathlib import Path +import re +import signal +import subprocess +import time + +from SkillFactory.tasks.manager_based.contact_cycle_sim import skill_gui_v041 as base + +FFMPEG = Path('/home/cen/isaacsim-6.0.1/kit/python/lib/python3.12/site-packages/imageio_ffmpeg/binaries/ffmpeg-linux-x86_64-v7.0.2') + + +def owned_window(): + text = subprocess.check_output(['xprop', '-root', '_NET_CLIENT_LIST'], text=True) + for wid in re.findall(r'0x[0-9a-f]+', text): + try: + info = subprocess.check_output(['xprop', '-id', wid, '_NET_WM_PID', '_NET_WM_NAME'], + text=True, stderr=subprocess.DEVNULL) + match = re.search(r'_NET_WM_PID\(CARDINAL\) = (\d+)', info) + if match is None or int(match[1]) != os.getpid(): + continue + geo = subprocess.check_output(['xwininfo', '-id', wid], text=True, stderr=subprocess.DEVNULL) + def value(label): + return int(re.search(label + r':\s+(-?\d+)', geo)[1]) + if value('Width') >= 800 and value('Height') >= 600 and 'IsViewable' in geo: + return {'id': wid, 'pid': os.getpid(), 'x': value('Absolute upper-left X'), + 'y': value('Absolute upper-left Y'), 'width': value('Width'), + 'height': value('Height'), 'xprop': info, 'xwininfo': geo} + except subprocess.CalledProcessError: + continue # disappearing window is not interpreted as successful capture + return None + + +def wm_close(window): + class Data(ctypes.Union): + _fields_ = [('b', ctypes.c_char * 20), ('s', ctypes.c_short * 10), ('l', ctypes.c_long * 5)] + class ClientMessage(ctypes.Structure): + _fields_ = [('type', ctypes.c_int), ('serial', ctypes.c_ulong), ('send_event', ctypes.c_int), + ('display', ctypes.c_void_p), ('window', ctypes.c_ulong), + ('message_type', ctypes.c_ulong), ('format', ctypes.c_int), ('data', Data)] + class Event(ctypes.Union): + _fields_ = [('xclient', ClientMessage), ('padding', ctypes.c_long * 24)] + lib = ctypes.CDLL('libX11.so.6') + lib.XOpenDisplay.argtypes = [ctypes.c_char_p] + lib.XOpenDisplay.restype = ctypes.c_void_p + display = lib.XOpenDisplay(os.environ['DISPLAY'].encode()) + if not display: + raise RuntimeError('No actual X11 display') + lib.XInternAtom.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_int] + lib.XInternAtom.restype = ctypes.c_ulong + event = Event() + event.xclient.type = 33 + event.xclient.display = display + event.xclient.window = int(window['id'], 16) + event.xclient.message_type = lib.XInternAtom(display, b'WM_PROTOCOLS', False) + event.xclient.format = 32 + event.xclient.data.l[0] = lib.XInternAtom(display, b'WM_DELETE_WINDOW', False) + lib.XSendEvent.argtypes = [ctypes.c_void_p, ctypes.c_ulong, ctypes.c_int, ctypes.c_long, ctypes.POINTER(Event)] + lib.XSendEvent(display, int(window['id'], 16), False, 0, ctypes.byref(event)) + lib.XFlush.argtypes = [ctypes.c_void_p] + lib.XFlush(display) + lib.XCloseDisplay.argtypes = [ctypes.c_void_p] + lib.XCloseDisplay(display) + + +class Presentation(base.LivePresentation): + def __init__(self, app, env, output): + base.prepare_native() + self.output = Path(output) + self.window_info = None + self.video = self.video_log = None + self.video_command = None + self.video_exit = None + self.captures = [] + self.seen = set() + self.created = time.monotonic() + self.first_tick_wall = None + self.final_carry_equal = False + super().__init__(app, env.guard, lambda value: None) + self.env = env + self.initial_render_generation = env.sim.render_generation + self.attach(env) + self.expected_visualizer = next(v for v in env.sim.visualizers if v.cfg.visualizer_type == 'kit') + self.window.title = 'Go2 FL | four skills 0.5.0 | LIVE PhysX' + self.window.height = 350 + self.message.text = 'Aided single leg | original auxiliary | real force | no resets' + self.arrow = base.UsdGeom.BasisCurves.Define(base.omni.usd.get_context().get_stage(), '/World/ActualExternalForce') + self.arrow.CreateTypeAttr('linear') + self.arrow.CreateCurveVertexCountsAttr([2]) + self.arrow.CreateWidthsAttr([.004]) + self.arrow.SetWidthsInterpolation('constant') + self.arrow.CreateDisplayColorAttr([(1., .05, .03)]) + with self.window.frame: + # Rebuild with a separate explicit safety request; OS close remains + # immediate abort while physics is active, never a safe-stop claim. + with base.ui.VStack(spacing=4): + self.mode = base.ui.Label('', height=24) + self.phase = base.ui.Label('', height=24) + self.cycle = base.ui.Label('', height=24) + self.progress = base.ui.ProgressBar(height=14) + self.contact = base.ui.Label('', height=24) + self.position = base.ui.Label('', height=24) + self.message = base.ui.Label('Aided fixture | truth sensing | no pose reset', height=24) + base.ui.Button('Request SAFE STOP (plant then support)', clicked_fn=env.service.request_stop, height=28) + base.ui.Button('Close window (ABORT while running)', clicked_fn=self.request_close, height=28) + for name in ('Simulation Output Settings', 'Semantics Schema Editor', 'Layer', 'Render Settings'): + panel = base.ui.Workspace.get_window(name) + if panel is not None: + panel.visible = False + + def check_health(self): + v = self.expected_visualizer + if (not self.app.is_running() or v not in self.env.sim.visualizers + or not v.is_running() or v.is_training_paused()): + raise RuntimeError('Actual bound Kit visualizer closed/removed/paused') + + def update(self, env): + from SkillFactory.legs.four_skills.api import SKILLS + s = env.s + tick = int(s.substeps[0]) + self.last_tick = tick + self.render_updates = env.sim.render_generation - self.initial_render_generation + if self.window is not None: + skill = SKILLS[int(s.skill[0])] + self.mode.text = 'Control: ' + getattr(self, 'controller_label', 'analytic') + ' | ' + skill.upper() + self.phase.text = f'Ready: {bool(s.ready[0])} | hold {float(s.held[0]):.2f}/{float(s.hold_required[0]):.2f}s' + central = getattr(env, 'central_progress', {}) + loops = f"loop {central.get('completed_loops', 0)}/{central.get('requested_loops', '?')}" + self.cycle.text = f'{loops} | tick {tick} | sim {float(s.time[0]):.2f}s | 500Hz safety' + self.progress.model.set_value(min(float(s.held[0]) / max(.1, float(s.hold_required[0])), 1.)) + normal = float(s.contact()[0, 2]) + gap = float(s.foot_center()[0, 2] - s.bundle['foot']['radius']) + force = s.actual_external[0].tolist() + auxiliary = float(s.auxiliary[0, s.payload, 2]) + self.contact.text = f'Contact Fn {normal:.2f}N | gap {gap*1000:.1f}mm | auxiliaryZ {auxiliary:.1f}N' + self.position.text = f'REAL external world N: {force[0]:+.2f}, {force[1]:+.2f}, {force[2]:+.2f}' + if hasattr(self, 'arrow'): + pos = s.root()[0][0].detach().cpu().tolist() + end = [p + .025 * f for p, f in zip(pos, force)] + self.arrow.GetPointsAttr().Set([base.Gf.Vec3f(*pos), base.Gf.Vec3f(*end)]) + if tick and self.window_info is None: + self.window_info = owned_window() + if self.window_info is not None: + self.start_video() + if tick and self.first_tick_wall is None: + self.first_tick_wall = time.monotonic() + + def after_render(self): + from SkillFactory.legs.four_skills.api import SKILLS + s = self.env.s + key = (int(s.skill[0]), bool(s.ready[0])) + capture_key = key + label = f'{SKILLS[key[0]]}-' + ('ready' if key[1] else 'enter') + if float(s.actual_external.norm()) > .5 and 'external-force' not in self.seen: + capture_key, label = 'external-force', 'external-force' + if self.window_info is not None and self.last_tick > 5 and capture_key not in self.seen: + import torch + before = torch.cat((s.q(), *s.root()), -1).clone() + counts = (self.env.runtime_hooks.completed_steps, self.env.runtime_hooks.reset_calls, + self.env.runtime_hooks.pose_write_calls) + # UI/compositor is one displayed frame behind after a single paint. + # Synchronize only by rendering the SAME verified physical state. + for _ in range(3): + self.env.sim.render() + self.check_health() + time.sleep(.05) + if (not torch.equal(before, torch.cat((s.q(), *s.root()), -1)) + or counts != (self.env.runtime_hooks.completed_steps, self.env.runtime_hooks.reset_calls, + self.env.runtime_hooks.pose_write_calls)): + raise RuntimeError('Presentation synchronization advanced physics') + self.capture(label) + self.seen.add(capture_key) + + def start_video(self): + w = self.window_info + self.video_command = [str(FFMPEG), '-hide_banner', '-y', '-f', 'x11grab', '-framerate', '30', + '-video_size', f'{w["width"]//2*2}x{w["height"]//2*2}', + '-i', f'{os.environ["DISPLAY"]}+{w["x"]},{w["y"]}', '-an', '-c:v', 'libx264', + '-preset', 'ultrafast', '-crf', '25', '-pix_fmt', 'yuv420p', '-t', '2400', + str(self.output / 'live-kit-original-speed.mp4')] + self.video_log = (self.output / 'screen-video.log').open('w') + self.video = subprocess.Popen(self.video_command, stdin=subprocess.PIPE, + stdout=self.video_log, stderr=subprocess.STDOUT) + (self.output / 'actual-window.json').write_text(json.dumps(w, indent=2)) + + def capture(self, label): + from PIL import ImageGrab + w = self.window_info + image = ImageGrab.grab(xdisplay=os.environ['DISPLAY']) + image = image.crop((w['x'], w['y'], w['x'] + w['width'], w['y'] + w['height'])) + path = self.output / (label + '.png') + image.save(path) + self.captures.append({'path': path.name, 'tick': self.last_tick, 'wall_time': time.time(), + 'state': self.env.service.query()}) + + def finish(self, complete, *, safely_stopped=False): + import torch + self.protocol_completed = complete + if not complete and not safely_stopped: + return + self.message.text = ('SEQUENCE COMPLETE' if complete else 'SAFE STOP (not full sequence)') + ' | physical state retained | normal close' + before = torch.cat((self.env.s.q(), *self.env.s.root()), -1).clone() + counts = (self.env.runtime_hooks.completed_steps, self.env.runtime_hooks.reset_calls, + self.env.runtime_hooks.pose_write_calls) + end = time.monotonic() + 5 + while not self.closed and time.monotonic() < end: + self.guard() + self.check_health() + self.env.sim.render() + time.sleep(1/30) + self.capture('final-retained') + self.final_carry_equal = torch.equal(before, torch.cat((self.env.s.q(), *self.env.s.root()), -1)) + self.hold_no_physics = counts == (self.env.runtime_hooks.completed_steps, self.env.runtime_hooks.reset_calls, + self.env.runtime_hooks.pose_write_calls) + if not self.final_carry_equal or not self.hold_no_physics: + raise RuntimeError('Final render-only view changed physical state') + if self.window_info is None: + raise RuntimeError('No verified owned actual Kit window') + wm_close(self.window_info) + close_deadline = time.monotonic() + 10 + while not self.closed and time.monotonic() < close_deadline: + self.guard() + self.env.sim.render() + if not self.closed: + raise RuntimeError('Actual WM_DELETE not observed') + self.close_reason = 'actual_WM_DELETE_after_completion' + self.stop_video() + + def stop_video(self): + if self.video is not None: + if self.video.poll() is None: + # ffmpeg SIGINT normally returns255 even after finalizing MP4; + # explicit stdin quit is the documented clean exit0 path. + self.video.stdin.write(b'q\n') + self.video.stdin.flush() + self.video.stdin.close() + self.video_exit = self.video.wait(timeout=30) + if self.video_log is not None and not self.video_log.closed: + self.video_log.close() + if self.video_exit != 0: + raise RuntimeError('Real Kit recording did not exit cleanly') + + def report(self): + result = {'genuine_Kit_window': self.window_info, 'protocol_completed': self.protocol_completed, + 'render_updates': self.render_updates, 'close_reason': self.close_reason, + 'final_carry_equal': self.final_carry_equal, 'hold_no_physics': self.hold_no_physics, + 'video_command': self.video_command, 'video_waited_exit': self.video_exit, + 'captures': self.captures, 'recording_fps_is_not_simulation_rtf': True} + (self.output / 'gui-evidence.json').write_text(json.dumps(result, indent=2)) + return result + + def release(self): + self.stop_video() + self.report() + super().release() diff --git a/source/SkillFactory/SkillFactory/tasks/manager_based/four_skills/mdp.py b/source/SkillFactory/SkillFactory/tasks/manager_based/four_skills/mdp.py new file mode 100644 index 0000000..6c6dde0 --- /dev/null +++ b/source/SkillFactory/SkillFactory/tasks/manager_based/four_skills/mdp.py @@ -0,0 +1,398 @@ +"""Skill-local references and real state feedback; no force-sampling actor labels.""" +import json +from pathlib import Path + +import torch + +from isaaclab.utils.math import axis_angle_from_quat +from SkillFactory.legs.contact_cycle.mechanics import inverse_rotate, reduce_to_com +from SkillFactory.legs.contact_cycle.task import phase_failures, phase_objective +from SkillFactory.legs.contact_cycle_sim.config import combine +from SkillFactory.legs.contact_cycle_sim.reference import ReferenceController +from SkillFactory.legs.four_skills.api import LEGAL, SKILLS +from SkillFactory.legs.four_skills.forces import ForceSchedule +from SkillFactory.tasks.manager_based.contact_cycle.mdp import CycleState +from SkillFactory.tasks.manager_based.single_leg_anchor.mdp.actions import BoundedJointPositionAction + + +def smooth(x): + x = x.clamp(0, 1) + return x.square() * (3 - 2 * x) + + +def hold_watchdog(ready, held, required, completed, age, deadline): + """A brief ready edge cannot buy an unlimited unfinished requested hold.""" + completed = completed | (ready & (held + 1e-6 >= required)) + return completed, ~completed & (age > deadline) + + +class FourSkillState(CycleState): + def __init__(self, env): + super().__init__(env) + n, d = env.num_envs, env.device + self.config = env.cfg.four_parameters + self.skill = torch.zeros(n, dtype=torch.long, device=d) + self.age = torch.zeros(n, device=d) + self.age_ticks = torch.zeros(n, device=d, dtype=torch.long) + self.dwell = torch.zeros(n, device=d) + self.held = torch.zeros(n, device=d) + self.hold_required = torch.zeros(n, device=d) + self.ready = torch.zeros(n, dtype=torch.bool, device=d) + self.ever_ready = torch.zeros_like(self.ready) + self.hold_completed = torch.zeros_like(self.ready) + self.hold_timed_out = torch.zeros_like(self.ready) + self.hold_deadline = torch.full((n,), 2., device=d) + self.stop_requested = torch.zeros_like(self.ready) + self.stopped = torch.zeros_like(self.ready) + self.failed = torch.zeros_like(self.ready) + self.functional_failed = torch.zeros_like(self.ready) + self.bad_hold = torch.zeros(n, device=d) + self.unload_initial = self.weight.clone() + self.unload_min = self.weight.clone() + self.unload_passed = torch.ones_like(self.ready) + self.ground_target = self.origin.clone() + self.entry_ground = self.origin.clone() + self.swing_start = self.origin.clone() + self.swing_delta = torch.zeros(n, 3, device=d) + self.entry_foot = self.foot_center().clone() + self.episode_ids = [0] * n + self.forces = ForceSchedule(env.cfg.seed, list(range(n)), env.cfg.force_tier, d, + duration_s=300., episode_ids=self.episode_ids) + self.controller = ReferenceController(self, env.cfg.residual_parameters['reference']) + self.residual_request = torch.zeros(n, 3, device=d) + self.reference_used = torch.zeros_like(self.residual_request) + self.effective_residual_rad = torch.zeros_like(self.residual_request) + self.controller.reset(torch.arange(n, device=d)) + self.lower = self.time.new_tensor([j['limits']['lower'] for j in self.bundle['joints']]) + self.upper = self.time.new_tensor([j['limits']['upper'] for j in self.bundle['joints']]) + self.speed = self.time.new_tensor([j['limits']['velocity'] for j in self.bundle['joints']]) + self.failure_masks = {} + self.transitions = [] + self.force_tier = env.cfg.force_tier + self.actual_external = torch.zeros(n, 3, device=d) + + def reset(self, ids): + # CycleState.__init__ calls the virtual reset before additive fields exist. + super().reset(ids) + if not hasattr(self, 'skill'): + return + self.skill[ids] = 0 + self.age_ticks[ids] = 0 + for v in (self.age, self.dwell, self.held, self.hold_required, self.bad_hold): + v[ids] = 0 + for v in (self.ready, self.ever_ready, self.hold_completed, self.hold_timed_out, + self.stop_requested, self.stopped, self.failed, self.functional_failed): + v[ids] = False + self.hold_deadline[ids] = 2. + self.ground_target[ids] = self.origin[ids] + self.entry_ground[ids] = self.origin[ids] + self.entry_foot[ids] = self.foot_center()[ids] + self.swing_delta[ids] = 0 + self.unload_initial[ids] = self.weight[ids] + self.unload_min[ids] = self.weight[ids] + self.unload_passed[ids] = True + rows = ids.cpu().tolist() + for i in rows: + self.episode_ids[i] += 1 + self.forces.reset(rows, [self.episode_ids[i] for i in rows], tier=self.force_tier) + self.residual_request[ids] = 0 + self.reference_used[ids] = 0 + self.effective_residual_rad[ids] = 0 + self.controller.reset(ids) + + def ids(self, ids): + if ids is None: + return torch.arange(self.env.num_envs, device=self.env.device) + if isinstance(ids, torch.Tensor): + x = ids + else: + if not isinstance(ids, (list, tuple)) or any(type(i) is not int for i in ids): + raise ValueError('Exact integer environment indices required') + x = torch.tensor(ids, device=self.env.device, dtype=torch.long) + if (x.ndim != 1 or x.dtype not in (torch.int32, torch.int64) or not len(x) + or bool((x < 0).any() or (x >= self.env.num_envs).any()) or len(x.unique()) != len(x)): + raise ValueError('Invalid/duplicate environment indices') + return x.to(device=self.env.device, dtype=torch.long) + + def reference(self): + if not hasattr(self, 'skill'): + return super().reference() + t, k = self.age, self.skill + phase = torch.zeros_like(k) + progress = torch.zeros_like(t) + target = self.ground_target.clone() + force = self.weight.clone() + fraction = torch.zeros_like(t) + # Same six original physical subphases, now owned by the active skill, + # with indefinitely held endpoints, not by a global periodic timer. + retract = k == 1 + u, lift = smooth(t / 1.), smooth((t - 1.) / .6) + phase = torch.where(retract, torch.where(t < 1., 1, 2), phase) + progress = torch.where(retract, torch.where(t < 1., t, ((t - 1.) / .6).clamp(0, 1)), progress) + target[:, 2] += torch.where(retract, .05 * lift, 0.) + force = torch.where(retract, self.weight * (1 - u), force) + fraction = torch.where(retract, u, fraction) + swing = k == 2 + p = (t / 1.4).clamp(0, 1) + swung = self.swing_start + self.swing_delta * smooth(p)[:, None] + swung[:, 2] = self.origin[:, 2] + .05 + .01 * torch.sin(torch.pi * p).square() + target = torch.where(swing[:, None], swung, target) + phase = torch.where(swing, 3, phase) + progress = torch.where(swing, p, progress) + force = torch.where(swing, 0., force) + fraction = torch.where(swing, 1., fraction) + plant = k == 3 + lower, reload = smooth(t / .8), smooth((t - .8) / 2.2) + target[:, 2] += torch.where(plant, .05 * (1 - lower), 0.) + phase = torch.where(plant, torch.where(t < .8, 4, 5), phase) + progress = torch.where(plant, torch.where(t < .8, (t / .8).clamp(0, 1), ((t - .8) / 2.2).clamp(0, 1)), progress) + force = torch.where(plant, self.weight * reload, force) + fraction = torch.where(plant, 1 - reload, fraction) + return phase, progress, target, force, fraction + + def apply_loads(self): + # Original guide/auxiliary formula, exactly ONE COM composer submission. + # No old deterministic events or extra cancellation feedforward. + _, _, _, _, fraction = self.reference() + pos, quat, vel, omega = self.root() + target, target_vel = self.port_reference() + identity = torch.zeros_like(quat) + identity[:, 3] = 1 + com = self.robot.data.body_com_pos_w.torch + disp = torch.cat((pos - target, axis_angle_from_quat(quat)), -1) + velocity = torch.cat((vel - target_vel, omega), -1) + raw = (-self.stiffness * disp - self.damping * velocity).clamp(-self.wrench_limit, self.wrench_limit) + if not self.env.cfg.guide_enabled: + raw.zero_() + self.guide.zero_() + self.guide[:, self.payload] = reduce_to_com(raw[:, :3], raw[:, 3:], identity, pos, com[:, self.payload]) + support = self.cc['support'] + fz = fraction * (self.weight + support['kp_N_m'] * (target[:, 2] - pos[:, 2]) + + support['kd_Ns_m'] * (target_vel[:, 2] - vel[:, 2])) + force = torch.zeros_like(pos) + if self.env.cfg.auxiliary_enabled: + force[:, 2] = fz.clamp(0, support['max_N']) + self.auxiliary.zero_() + self.auxiliary[:, self.payload] = reduce_to_com(force, torch.zeros_like(force), identity, pos, com[:, self.payload]) + self.actual_external = self.forces.at(self.time) + self.external.zero_() + self.external[:, self.payload, :3] = self.actual_external + total = self.external + self.guide + self.auxiliary + self.robot.permanent_wrench_composer.set_forces_and_torques_index( + forces=total[..., :3], torques=total[..., 3:], body_ids=self.all_bodies, + positions=None, is_global=True) + + def after_physics(self, dt): + self.substeps += 1 + self.time[:] = self.substeps * dt + self.age_ticks += 1 + self.age[:] = self.age_ticks * dt + self.sensed = self.raw_sensors() + normal = self.contact()[:, 2] + gap = self.foot_center()[:, 2] - self.bundle['foot']['radius'] + _, self.edge_on = self.latch.update(normal, dt) + self.acquisition.update(self.time, gap, normal, self.weight, dt) + phase = self.reference()[0] + self.edge_off = self.separation.update(gap, self.latch.contact, phase, dt) + self.impact = torch.where(phase == 4, torch.maximum(self.impact, normal), torch.zeros_like(normal)) + g = self.config['gate'] + support = (normal >= .5 * self.weight) & self.latch.contact & (gap <= .002) + airborne = (gap >= .01) & (normal <= 2.) + final = torch.where(self.skill == 0, self.age >= 0., + torch.where(self.skill == 1, self.age >= 1.6, + torch.where(self.skill == 2, self.age >= 1.4, self.age >= 3.))) + good = torch.where((self.skill == 0) | (self.skill == 3), support, airborne) + retract_unload = (self.skill == 1) & (self.age <= 1.002) + self.unload_min = torch.where(retract_unload, torch.minimum(self.unload_min, normal), self.unload_min) + self.unload_passed |= (self.skill == 1) & (self.unload_min <= .8 * self.unload_initial) + good &= (self.skill != 1) | self.unload_passed + disp = self.foot_center() - self.entry_foot + requested = self.swing_delta + directed = ((disp[:, :2] * requested[:, :2].sign()) >= .5 * requested[:, :2].abs()) | (requested[:, :2].abs() < 1e-6) + good &= (self.skill != 2) | directed.all(-1) + land_error = (self.foot_center() - self.ground_target).norm(dim=-1) + good &= (self.skill != 3) | (land_error <= .05) + dwell_required = torch.where((self.skill == 0) | (self.skill == 3), .2, .1) + self.dwell = torch.where(good & final, self.dwell + dt, 0.) + self.ready = self.dwell + 1e-6 >= dwell_required + self.ever_ready |= self.ready + self.held = torch.where(self.ready, self.held + dt, 0.) + self.hold_completed, hold_expired = hold_watchdog( + self.ready, self.held, self.hold_required, self.hold_completed, self.age, self.hold_deadline) + self.hold_timed_out |= hold_expired + self.bad_hold = torch.where(self.ever_ready & ~good, self.bad_hold + dt, 0.) + deadline = torch.where(self.skill == 0, 2., torch.where(self.skill == 1, 3.6, + torch.where(self.skill == 2, 3.4, 5.))) + self.functional_failed |= ((~self.ever_ready & (self.age > deadline)) + | self.hold_timed_out | (self.bad_hold > .2)) + self.stopped |= self.stop_requested & (self.skill == 0) & self.ready + masks = self.safety() + self.failure_masks = {**masks, 'skill_completion_or_hold': self.functional_failed, + 'requested_hold_timeout': self.hold_timed_out} + all_failed = torch.stack(list(self.failure_masks.values())).any(0) + self.failed |= all_failed + + def safety(self): + pos, quat, velocity, omega = self.root() + gap = self.foot_center()[:, 2] - self.bundle['foot']['radius'] + ratio = torch.maximum((self.robot.data.applied_torque.torch[:, self.joints] / self.effort).abs().amax(-1), + (self.robot.data.joint_vel.torch[:, self.joints] / self.speed).abs().amax(-1)) + excess = torch.maximum(self.lower - self.q(), self.q() - self.upper).clamp(min=0).amax(-1) + finite = torch.isfinite(torch.cat((self.raw_sensors(), pos, quat, velocity, omega, + self.foot_center(), self.contact(), + self.robot.data.applied_torque.torch[:, self.joints], + ratio[:, None], excess[:, None], + self.controller.q_ik, self.controller.action, self.residual_request, + self.guide.flatten(1), self.auxiliary.flatten(1), + self.external.flatten(1)), -1)).all(-1) + phase, progress, _, _, _ = self.reference() + auxiliary_z = self.auxiliary[..., 2] + original_phase = phase_failures(phase, progress, gap, self.contact()[:, 2], self.weight) + return {**original_phase, 'premature_separation': self.separation.premature, + 'nonfinite': ~finite, 'contact_acquisition_timeout': self.acquisition.failed, + 'negative_normal': self.contact()[:, 2] < -.5, + 'auxiliary_force': auxiliary_z.sum(-1) > 140., + 'auxiliary_geometry': ((self.auxiliary[..., :2].abs().amax(dim=(-1, -2)) > 1e-6) + | (auxiliary_z.amin(-1) < -1e-6) + | (self.auxiliary[..., 5].abs().amax(-1) > 1e-6) + | (self.auxiliary[..., 3:].norm(dim=-1) > .06 * auxiliary_z.clamp_min(0) + 1e-6).any(-1)), + 'auxiliary_support_phase': (phase == 0) & (self.auxiliary.abs().amax(dim=(-1, -2)) > 1e-6), + 'effort_velocity': ratio > 1.00001, + 'joint_limit': excess > .01, 'vertical_guide': self.guide[..., 2].abs().amax(-1) != 0, + 'penetration': gap < -.002, 'root_height': (pos[:, 2] - self.base_origin[:, 2]).abs() > .1, + 'tilt': self.tilt() > .6} + + def request(self, skill, hold, vector, ids=None): + ids = self.ids(ids) + old = self.skill[ids].cpu().tolist() + if any(skill not in LEGAL[SKILLS[i]] for i in old): + raise ValueError('Illegal skill transition') + if bool((~self.ready[ids] | self.failed[ids] | self.stopped[ids] + | (self.held[ids] + 1e-6 < self.hold_required[ids])).any()): + raise ValueError('Transition requires ready, completed hold, live healthy skill') + before = self.substeps[ids].clone() + self.entry_ground[ids] = self.ground_target[ids] + self.entry_foot[ids] = self.foot_center()[ids] + if skill == 'retract': + self.unload_initial[ids] = self.contact()[ids, 2].clamp(min=1.) + self.unload_min[ids] = self.unload_initial[ids] + self.unload_passed[ids] = False + if skill == 'swing': + delta = self.time.new_tensor([*vector, 0.]) + self.swing_start[ids] = self.ground_target[ids] + self.swing_start[ids, 2] = self.origin[ids, 2] + .05 + self.swing_delta[ids] = delta + self.ground_target[ids] += delta + self.ground_target[ids, 2] = self.origin[ids, 2] + self.skill[ids] = SKILLS.index(skill) + self.age[ids] = self.dwell[ids] = self.held[ids] = self.bad_hold[ids] = 0 + self.age_ticks[ids] = 0 + self.ready[ids] = self.ever_ready[ids] = self.hold_completed[ids] = self.hold_timed_out[ids] = False + self.hold_required[ids] = hold + self.hold_deadline[ids] = self.time.new_tensor([2., 3.6, 3.4, 5.])[self.skill[ids]] + hold + self.controller.refresh(ids) + if not torch.equal(before, self.substeps[ids]): + raise RuntimeError('Transition advanced physical clock') + if self.env.num_envs == 1: + self.transitions.append({'from': SKILLS[old[0]], 'to': skill, 'tick': int(before[0]), + 'hold_s': hold, 'direction_m': vector}) + return {'accepted': True, 'skill': skill, 'env_ids': ids.cpu().tolist()} + + def hold(self, duration, ids=None): + ids = self.ids(ids) + if bool((self.failed[ids] | self.stopped[ids]).any()): + raise ValueError('Cannot hold failed/stopped skill') + self.hold_required[ids] = duration + self.held[ids] = 0 + self.hold_completed[ids] = self.ready[ids] & (duration == 0) + intrinsic = self.time.new_tensor([0., 1.6, 1.4, 3.])[self.skill[ids]] + self.hold_deadline[ids] = torch.maximum(self.age[ids], intrinsic) + duration + 2. + return {'accepted': True, 'hold_seconds': duration} + + def request_stop(self, ids=None): + ids = self.ids(ids) + self.stop_requested[ids] = True + self.hold_required[ids] = 0 + self.stopped[ids] |= self.ready[ids] & (self.skill[ids] == 0) + return {'accepted': True, 'safe_support_required': True} + + def query(self, ids=None): + ids = self.ids(ids) + rows = [] + for i in ids.cpu().tolist(): + rows.append({'skill': SKILLS[int(self.skill[i])], 'tick': int(self.substeps[i]), + 'ready': bool(self.ready[i]), 'hold_complete': bool(self.held[i] + 1e-6 >= self.hold_required[i]), + 'held_s': float(self.held[i]), 'age_s': float(self.age[i]), + 'failed': bool(self.failed[i]), 'stopped': bool(self.stopped[i]), + 'stop_requested': bool(self.stop_requested[i]), + 'failure_reasons': [k for k, v in self.failure_masks.items() if bool(v[i])], + 'normal_N': float(self.contact()[i, 2]), + 'gap_m': float(self.foot_center()[i, 2] - self.bundle['foot']['radius'])}) + return rows[0] if len(rows) == 1 else rows + + def policy(self): + _, progress, target, _, _ = self.reference() + pos, quat, vel, omega = self.root() + foot = self.foot_center() + gravity = torch.zeros_like(pos) + gravity[:, 2] = -1 + # Explicit field allowlist. No forces, desired normal load, fixture + # labels, random seeds/onsets/durations or future disturbance values. + return torch.cat((self.q(), self.robot.data.joint_vel.torch[:, self.joints], self.mount_wrench(), + inverse_rotate(quat, gravity), inverse_rotate(quat, omega), + pos - self.base_origin, vel, inverse_rotate(quat, foot - pos), self.point_velocity(), + self.contact(), (foot[:, 2] - self.bundle['foot']['radius'])[:, None], + torch.nn.functional.one_hot(self.skill, 4).float(), + target - self.base_origin, progress[:, None], + self.controller.action, self.residual_request), -1) + + def reward(self): + phase, progress, target, desired_force, _ = self.reference() + center = self.foot_center() + gap = center[:, 2] - self.bundle['foot']['radius'] + # Reuse the old phase-correct objective: ground tracking while unloading, + # Fn-aware airborne lift/swing, and load restoration throughout reload. + task, violation = phase_objective(phase, progress, (center - target).norm(dim=-1), gap, + self.contact()[:, 2], desired_force, self.slip_velocity(), self.impact, self.weight, + self.cc['contact_transition']['unload_tracking_scale_m']) + return (task + .5 * self.ready.float() - .02 * self.residual_request.square().mean(-1) + - 10 * (self.failed | violation).float()) + + +def state(env): + if not hasattr(env, '_cycle_state'): + env._cycle_state = FourSkillState(env) + if not isinstance(env._cycle_state, FourSkillState): + raise RuntimeError('Wrong additive four-skill state') + return env._cycle_state + + +def observation(env): + return state(env).policy() + + +def reward(env): + return state(env).reward() + + +def failed(env): + return state(env).failed + + +class FourSkillAction(BoundedJointPositionAction): + def __init__(self, cfg, env): + super().__init__(cfg, env) + state(env) + + def process_actions(self, actions): + s = state(self._env) + full, request, delta = combine(s.controller.action, actions, .05, .5) + super().process_actions(full) + s.reference_used.copy_(s.controller.action) + s.residual_request.copy_(request) + s.effective_residual_rad.copy_(delta) + s.executed.copy_(self.raw_actions) + + def apply_actions(self): + state(self._env).apply_loads() + super().apply_actions() diff --git a/tests/test_four_skills_api.py b/tests/test_four_skills_api.py new file mode 100644 index 0000000..c6e80ea --- /dev/null +++ b/tests/test_four_skills_api.py @@ -0,0 +1,90 @@ +"""CPU-only public command validation and exact supervisor-entry regression.""" +import importlib.util +from pathlib import Path +import sys + +import pytest + +ROOT = Path(__file__).resolve().parents[1] + + +def load(name, path): + spec = importlib.util.spec_from_file_location(name, ROOT / path) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +api = load('four_api', 'source/SkillFactory/SkillFactory/legs/four_skills/api.py') +cli = load('four_cli', 'scripts/four_skills.py') +runtime = load('four_runtime', 'scripts/four_skills_runtime.py') + + +def test_paused_stage_cannot_create_or_reuse_authority(tmp_path, monkeypatch): + monkeypatch.setattr(cli, 'STAGE', tmp_path) + (tmp_path / 'PAUSED.json').write_text('{"reason":"user"}') + with pytest.raises(PermissionError, match='paused by user'): + cli.scope() + assert not (tmp_path / 'scope.json').exists() + + +def test_exact_supervisor_command_relative_and_absolute(): + script = ROOT / 'scripts/four_skills.py' + for name in (str(script), 'scripts/four_skills.py'): + assert cli.supervisor_command_matches([b'/usr/bin/python3.12', name.encode(), b'smoke'], ROOT) + assert not cli.supervisor_command_matches([b'python', b'/tmp/four_skills.py', b'smoke'], ROOT) + assert not cli.supervisor_command_matches([b'python', str(script).encode(), b'--_worker-fd'], ROOT) + assert not cli.supervisor_command_matches([b'python', b'scripts/four_skills.py', b'smoke'], '/tmp') + assert not cli.supervisor_command_matches([b'python'], ROOT) + + +class Sink: + def __init__(self): + self.calls = [] + + def request(self, *args): + self.calls.append(args) + return args + + +@pytest.mark.parametrize('kwargs', [dict(skill='unknown'), dict(skill='support', direction_m=[.01, .02]), + dict(skill='swing', direction_m=[0., 0.]), dict(skill='swing', direction_m=[.1, 0.]), + dict(skill='swing', direction_m=[True, .02]), dict(skill='support', hold_seconds=True), + dict(skill='support', hold_seconds=float('nan')), dict(skill='support', hold_seconds=11), + dict(skill='support', external_force=[1, 2, 3]), dict(skill='support', desired_force=50)]) +def test_invalid_public_command_never_mutates_state(kwargs): + state = Sink() + service = api.SkillService(state) + with pytest.raises((ValueError, TypeError)): + service.call(**kwargs) + assert state.calls == [] + + +def test_public_fields_only_and_legal_graph(): + s = Sink() + api.SkillService(s).call('swing', hold_seconds=.3, direction_m=[.02, .01]) + assert s.calls == [('swing', .3, (.02, .01), None)] + assert api.LEGAL == {'support': ('retract',), 'retract': ('swing', 'plant'), + 'swing': ('swing', 'plant'), 'plant': ('support',)} + + +def test_training_composer_one_transition_per_row_per_feedback_sample(): + from types import SimpleNamespace + import torch + s = SimpleNamespace(episode_ids=[0, 0], skill=torch.tensor([0, 2]), + ready=torch.ones(2, dtype=torch.bool), held=torch.ones(2), hold_required=torch.zeros(2), + failed=torch.zeros(2, dtype=torch.bool), stopped=torch.zeros(2, dtype=torch.bool)) + calls = [] + class Service: + def call(self, skill, *, env_ids, **kwargs): + assert bool(s.ready[env_ids].all()) + calls.append((skill, env_ids.tolist())) + s.skill[env_ids] = api.SKILLS.index(skill) + s.ready[env_ids] = False + env = SimpleNamespace(s=s, service=Service(), num_envs=2, device='cpu') + driver = runtime.TrainingComposer(env) + driver.update() + assert calls == [('retract', [0]), ('plant', [1])] + assert driver.transition_count == 2 + driver.update() + assert len(calls) == 2 diff --git a/tests/test_four_skills_audit.py b/tests/test_four_skills_audit.py new file mode 100644 index 0000000..e266a4d --- /dev/null +++ b/tests/test_four_skills_audit.py @@ -0,0 +1,141 @@ +"""Synthetic counterexamples for evidence checking, not physical success proof.""" +import copy +import hashlib +import importlib.util +import json +from pathlib import Path + +import pytest +import torch +from SkillFactory.legs.four_skills.forces import ForceSchedule + +ROOT = Path(__file__).resolve().parents[1] +spec = importlib.util.spec_from_file_location('four_auditor', ROOT / 'scripts/audit_four_skills.py') +auditor = importlib.util.module_from_spec(spec) +spec.loader.exec_module(auditor) + + +def js(path, value): + path.write_text(json.dumps(value)) + + +def smooth(x): + x = x.clamp(0, 1) + return x.square() * (3 - 2*x) + + +@pytest.fixture +def run(tmp_path): + out = tmp_path / 'analytic' + out.mkdir() + sizes = [350, 1000, 950, 1850, 350] + kinds = [0, 1, 2, 3, 0] + count = sum(sizes) + skill = torch.cat([torch.full((n,), k, dtype=torch.long) for k, n in zip(kinds, sizes)]) + age = torch.cat([torch.arange(1, n+1).float()*.002 for n in sizes]) + foot = torch.zeros(count, 1, 3) + foot[..., 2] = .02 + fn = torch.ones(count, 1) * 50 + a, b, c, d = 350, 1350, 2300, 4150 + foot[a:b, 0, 2] += .05 * smooth((age[a:b]-1)/.6) + fn[a:b, 0] *= 1-smooth(age[a:b]) + p = smooth(age[b:c]/1.4) + foot[b:c, 0, :2] = p[:, None]*torch.tensor([.024, .010]) + foot[b:c, 0, 2] = .07 + fn[b:c] = 0 + foot[c:, 0, :2] = torch.tensor([.024, .010]) + foot[c:d, 0, 2] += .05*(1-smooth(age[c:d]/.8)) + fn[c:d, 0] *= smooth((age[c:d]-.8)/2.2) + normal = torch.zeros(count, 1, 3) + normal[..., 2] = fn + zero = torch.zeros(count, 1) + t = {'tick': torch.arange(1, count+1)[:, None], + 'time_s': (torch.arange(1, count+1).float()*.002)[:, None], + 'skill': skill[:, None], 'age_s': age[:, None], 'episode_id': torch.ones(count, 1, dtype=torch.long), + 'weight': torch.ones(count, 1)*50, 'failed': zero.bool(), 'root': torch.zeros(count, 1, 13), + 'joint_pos': torch.zeros(count, 1, 3), 'joint_vel': torch.zeros(count, 1, 3), + 'normal': normal, 'foot': foot, 'gap': foot[..., 2]-.02, 'target': foot.clone(), + 'reference': torch.zeros(count, 1, 3), 'q_ik': torch.zeros(count, 1, 3), + 'actor_observation': torch.zeros(count, 1, 48), 'external': torch.zeros(count, 1, 2, 6), + 'guide': torch.zeros(count, 1, 2, 6), 'auxiliary': torch.zeros(count, 1, 2, 6), + 'phase': torch.where(skill == 0, 0, torch.where(skill == 1, torch.where(age < 1., 1, 2), + torch.where(skill == 2, 3, torch.where(age < .8, 4, 5))))[:, None], + 'limit_ratio': zero.clone(), + 'joint_excess': zero.clone(), 'height_error': zero.clone(), 'tilt': zero.clone()} + initial = {k: v[0].clone() for k, v in t.items()} + initial['time_s'].zero_() + torch.save(t, out/'trace.pt') + torch.save(initial, out/'initial-state.pt') + approval = {'seed': 115011, 'sequence': 0, 'loops': 1, 'mode': 'evaluate', 'tier': 'none', + 'config_sha256': hashlib.sha256((ROOT/'configs/four_skills/v050.json').read_bytes()).hexdigest(), + 'controller': 'analytic', 'checkpoint': None, 'checkpoint_sha256': None} + js(out/'approval.json', approval) + counters = {'resets': 11, 'pose_writes': 14} + js(out/'worker-result.json', {'actual_ticks': count, 'requested_loops': 1, 'completed_loops': 1, + 'functional_success': True, 'initialization_counters': counters, 'final_counters': counters, + 'physics_counts': {'initialization': 0, 'completed': count}}) + js(out/'supervisor-result.json', {'success': True, 'owned_cleanup': True, 'waited_exit': True}) + js(out/'runtime-identity.json', {'payload_body_index': 0, 'body_names': ['virtual_payload', 'foot']}) + js(out/'force-schedule.json', ForceSchedule(115011, [0], 'none', 'cpu', episode_ids=[1]).manifest()) + js(out/'transitions.json', [dict(zip(('from', 'to', 'at_tick'), (old, new, tick)), + feedback_ready=True, feedback_hold_complete=True) for old, new, tick in + [('support', 'retract', a), ('retract', 'swing', b), ('swing', 'plant', c), ('plant', 'support', d)]]) + assert auditor.audit(out)['passed'] + return out + + +def change_trace(run, change): + t = torch.load(run/'trace.pt', weights_only=True) + change(t) + torch.save(t, run/'trace.pt') + return auditor.audit(run) + + +def test_actual_tensor_skill_order_cannot_be_replaced_by_claimed_json(run): + result = change_trace(run, lambda t: t['skill'].__setitem__(slice(350, 1350), 3)) + assert not result['passed'] and 'actual_skill_order_mismatch' in result['failure_reasons'] + + +@pytest.mark.parametrize('field', ['time_s', 'episode_id']) +def test_timestamps_and_episode_identity_are_not_unchecked_labels(run, field): + result = change_trace(run, lambda t: t[field].__setitem__((100, 0), t[field][100, 0]+1)) + assert not result['passed'] + + +def test_transition_tick_must_equal_actual_tensor_boundary(run): + edges = json.loads((run/'transitions.json').read_text()) + edges[0]['at_tick'] += 1 + js(run/'transitions.json', edges) + assert 'transition_tick_mismatch' in auditor.audit(run)['failure_reasons'] + + +def test_support_ratio_alone_cannot_hide_interrupted_continuous_hold(run): + result = change_trace(run, lambda t: t['normal'].__setitem__((340, 0, 2), 0.)) + assert not result['passed'] + assert any('continuous_support' in reason for reason in result['failure_reasons']) + + +def test_swing_progress_must_persist_through_hold_not_only_last_frame(run): + result = change_trace(run, lambda t: t['foot'].__setitem__((slice(2100, 2299), 0, slice(0, 2)), 0.)) + assert any('continuous_signed_swing_hold' in reason for reason in result['failure_reasons']) + + +def test_zero_requested_couple_is_verified_not_just_net_force(run): + result = change_trace(run, lambda t: t['external'].__setitem__((20, 0, 0, 3), .1)) + assert 'sampled_force_not_actually_applied' in result['failure_reasons'] + + +def test_schedule_tier_must_equal_approval(run): + js(run/'force-schedule.json', ForceSchedule(115011, [0], 'small', 'cpu', episode_ids=[1]).manifest()) + assert 'schedule_tier_not_approved' in auditor.audit(run)['failure_reasons'] + + +def test_same_initial_tensors_and_force_schedule_do_not_pair_different_tasks(run, tmp_path): + import shutil + other = tmp_path/'trained' + shutil.copytree(run, other) + a = json.loads((other/'approval.json').read_text()) + a.update(controller='trained', checkpoint='/example/weights.pt', checkpoint_sha256='a'*64, sequence=1) + js(other/'approval.json', a) + result = auditor.compare_pair(run, other) + assert not result['paired'] and not result['approved_conditions_equal']['sequence'] diff --git a/tests/test_four_skills_forces.py b/tests/test_four_skills_forces.py new file mode 100644 index 0000000..fb2e299 --- /dev/null +++ b/tests/test_four_skills_forces.py @@ -0,0 +1,208 @@ +"""CPU synthetic schedule tests, not simulator, hardware or task-success proof.""" + +import importlib.util +import json +import math +from pathlib import Path +import random + +import pytest +import torch + + +PATH = (Path(__file__).resolve().parents[1] + / "source/SkillFactory/SkillFactory/legs/four_skills/forces.py") +SPEC = importlib.util.spec_from_file_location("four_skills_forces", PATH) +MODULE = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(MODULE) +ForceSchedule = MODULE.ForceSchedule + + +def schedule(**kwargs): + options = dict(seed=137, env_ids=[10, 20, 30], tier="full", device="cpu") + options.update(kwargs) + return ForceSchedule(**options) + + +def test_paired_analytic_trained_schedules_are_identical_and_order_independent(): + analytic = schedule(episode_ids=[3, 4, 5]) + trained = schedule(episode_ids=[3, 4, 5]) + assert analytic.manifest() == trained.manifest() + for t in torch.linspace(0, 300, 501, dtype=torch.float64): + assert torch.equal(analytic.at(t.repeat(3)), trained.at(t.repeat(3))) + reordered = schedule(env_ids=[30, 10, 20], episode_ids=[5, 3, 4]) + rows = analytic.manifest()["rows"] + assert reordered.manifest()["rows"] == [rows[2], rows[0], rows[1]] + alone = schedule(env_ids=[20], episode_ids=[4]) + assert alone.manifest()["rows"] == [rows[1]] + assert schedule(seed=138).manifest()["rows"] != schedule().manifest()["rows"] + + +def test_reset_order_subset_and_tiers_preserve_unselected_rows(): + first, second = schedule(), schedule() + before = first.manifest()["rows"] + first.reset([2, 0], [9, 7], tier="small") + second.reset([0], [7], tier="small") + second.reset([2], [9], tier="small") + assert first.manifest() == second.manifest() + assert first.manifest()["rows"][1] == before[1] + expected = schedule(env_ids=[10, 30], episode_ids=[7, 9], tier="small") + assert [first.manifest()["rows"][i] for i in [0, 2]] == expected.manifest()["rows"] + first.reset([0], [8]) + assert first.manifest()["rows"][0]["tier"] == "small" + first.reset([0], [7]) + assert first.manifest() == second.manifest() + for t in [0.4, 8.5, 32.9, 99.4]: + assert torch.equal(first.at(torch.full((3,), t)), second.at(torch.full((3,), t))) + + +@pytest.mark.parametrize("tier,maximum", [("none", 0), ("small", 1.5), ("medium", 4), ("full", 8)]) +def test_event_bounds_nonoverlap_and_force_bounds(tier, maximum): + s = schedule(tier=tier) + manifest = s.manifest() + assert manifest["tier_maxima_N"] == dict(none=0, small=1.5, medium=4, full=8) + assert manifest["frame"] == "world" + assert manifest["application_point"] == "payload_com" + assert manifest["requested_couple_Nm"] == [0, 0, 0] + for row in manifest["rows"]: + previous_end = -1 + for e in row["events"]: + assert 0.2 - 1e-12 <= e["onset_s"] - e["slot_start_s"] <= 0.5 + 1e-12 + assert 0.5 <= e["duration_s"] <= 1.3 + assert 0.25 * maximum <= e["amplitude_N"] <= maximum + assert math.isclose(sum(v*v for v in e["direction_world"]), 1, abs_tol=1e-14) + assert previous_end < e["onset_s"] + assert e["end_s"] < e["slot_start_s"] + 2 + previous_end = e["end_s"] + for t in torch.linspace(0, 300, 1001): + force = s.at(t.repeat(3)) + assert force.shape == (3, 3) and force.dtype == torch.float32 + assert force.device.type == "cpu" + assert bool((force.norm(dim=1) <= maximum + 1e-6).all()) + + +def test_isotropic_directions_active_probability_and_uniform_sampling_sanity(): + # Fixed seed, broad statistical checks: implementation still defines the distribution. + s = schedule(env_ids=list(range(40))) + events = [e for r in s.manifest()["rows"] for e in r["events"]] + directions = torch.tensor([e["direction_world"] for e in events], dtype=torch.float64) + assert bool((directions.mean(0).abs() < 0.035).all()) + assert bool(((directions.square().mean(0) - 1/3).abs() < 0.025).all()) + assert 0.77 < sum(e["active"] for e in events) / len(events) < 0.83 + assert abs(sum(e["amplitude_N"] for e in events) / len(events) - 5) < 0.12 + + +def test_raised_cosine_values_ramp_bound_inactive_and_gaps(): + s = schedule(env_ids=[10]) + events = s.manifest()["rows"][0]["events"] + active = next(e for e in events if e["active"]) + inactive = next(e for e in events if not e["active"]) + peak = torch.tensor(active["peak_force_world_N"], dtype=torch.float32) + start, end = active["onset_s"], active["end_s"] + for t, scale in [(start, 0), (start + .075, .5), (start + .15, 1), + (end - .15, 1), (end - .075, .5), (end, 0)]: + torch.testing.assert_close(s.at(torch.tensor([t], dtype=torch.float64))[0], + scale * peak, atol=1e-6, rtol=1e-6) + for t in [0, active["slot_start_s"], end + .05, + inactive["onset_s"] + .2, 300]: + assert torch.equal(s.at(torch.tensor([t], dtype=torch.float64)), torch.zeros(1, 3)) + # Sample both ramps at high resolution, measuring vector (not component) slope. + times = torch.cat([torch.linspace(start - .01, start + .16, 1701, dtype=torch.float64), + torch.linspace(end - .16, end + .01, 1701, dtype=torch.float64)]) + values = torch.stack([s.at(t.reshape(1))[0] for t in times]) + for segment in [slice(0, 1701), slice(1701, 3402)]: + rates = values[segment].diff(dim=0).norm(dim=1) / times[segment].diff() + assert rates.max() <= 100 + assert s.manifest()["maximum_force_derivative_N_per_s"] < 100 + + +def test_no_global_rng_mutation_on_construct_reset_or_query(): + python_state = random.getstate() + torch_state = torch.random.get_rng_state().clone() + s = schedule() + s.reset([1, 0], [5, 9], tier="medium") + s.at(torch.tensor([1., 2., 3.])) + s.manifest() + assert random.getstate() == python_state + assert torch.equal(torch.random.get_rng_state(), torch_state) + + +def test_manifest_is_complete_detached_and_reconstructs_force(): + s = schedule(duration_s=3.1) + manifest = json.loads(json.dumps(s.manifest(), allow_nan=False)) + for i, row in enumerate(manifest["rows"]): + assert len(row["events"]) == 2 + for event in row["events"]: + assert event["truncated_by_horizon"] == (event["end_s"] > 3.1) + assert event["onset_beyond_horizon"] == (event["onset_s"] > 3.1) + t = min(3.1, event["onset_s"] + .1) + edge = max(0, min(.15, t - event["onset_s"], event["end_s"] - t)) + scale = .5 * (1 - math.cos(math.pi * edge / .15)) + expected = torch.tensor(event["peak_force_world_N"]) * scale + result = s.at(torch.full((3,), t, dtype=torch.float64))[i] + torch.testing.assert_close(result, expected, atol=1e-6, rtol=1e-6) + manifest["rows"][0]["events"][0]["amplitude_N"] = -1 + assert s.manifest()["rows"][0]["events"][0]["amplitude_N"] >= 2 + short = schedule(duration_s=.1) + assert all(r["events"][0]["onset_beyond_horizon"] for r in short.manifest()["rows"]) + assert torch.equal(short.at(torch.full((3,), .1, dtype=torch.float64)), torch.zeros(3, 3)) + s.at(torch.full((3,), 3.1, dtype=torch.float64)) + with pytest.raises(ValueError, match="exceeds"): + s.at(torch.full((3,), 3.10000001, dtype=torch.float64)) + + +@pytest.mark.parametrize("kwargs,error", [ + ({"seed": True}, TypeError), ({"seed": 1.0}, TypeError), + ({"env_ids": [1, True]}, TypeError), ({"env_ids": [1., 2]}, TypeError), + ({"env_ids": [1, 1]}, ValueError), ({"env_ids": "12"}, TypeError), + ({"episode_ids": [1]}, ValueError), ({"episode_ids": [0, 0, False]}, TypeError), + ({"tier": "large"}, ValueError), ({"tier": []}, ValueError), + ({"duration_s": True}, ValueError), ({"duration_s": "300"}, ValueError), + ({"duration_s": 0}, ValueError), ({"duration_s": -1}, ValueError), + ({"duration_s": float("nan")}, ValueError), ({"duration_s": float("inf")}, ValueError), +]) +def test_constructor_bad_inputs(kwargs, error): + with pytest.raises(error): + schedule(**kwargs) + + +@pytest.mark.parametrize("time,error", [ + ([0, 0, 0], TypeError), (torch.zeros(3, 1), ValueError), + (torch.zeros(2), ValueError), (torch.tensor([0, 0, -1]), ValueError), + (torch.tensor([0., float("nan"), 0]), ValueError), + (torch.tensor([0., float("inf"), 0]), ValueError), + (torch.tensor([0, 301, 0]), ValueError), (torch.ones(3, dtype=torch.bool), TypeError), + (torch.zeros(3, dtype=torch.complex64), TypeError), +]) +def test_at_bad_inputs(time, error): + with pytest.raises(error): + schedule().at(time) + + +@pytest.mark.parametrize("ids,episodes,tier,error", [ + ([0, 0], [1, 2], None, ValueError), ([3], [0], None, ValueError), + ([-1], [0], None, ValueError), ([True], [0], None, TypeError), + ([1.0], [0], None, TypeError), ([0], [True], None, TypeError), + ([0], [], None, ValueError), ([0], [0], "unknown", ValueError), + ([0, 9], [1, 2], "small", ValueError), +]) +def test_reset_bad_inputs_do_not_mutate(ids, episodes, tier, error): + s = schedule() + before = s.manifest() + with pytest.raises(error): + s.reset(ids, episodes, tier) + assert s.manifest() == before + + +def test_empty_batch_empty_reset_and_integer_times(): + empty = schedule(env_ids=[]) + assert empty.at(torch.empty(0)).shape == (0, 3) + empty.reset([], []) + assert empty.manifest()["rows"] == [] + s = schedule() + before = s.manifest() + s.reset([], [], tier="none") + assert s.manifest() == before + assert s.at(torch.zeros(3, dtype=torch.int64)).shape == (3, 3) + # No unspecified nonnegative constraint on integer identity keys. + assert schedule(seed=-1, env_ids=[-10], episode_ids=[-3]).manifest()["seed"] == -1 diff --git a/tests/test_four_skills_model.py b/tests/test_four_skills_model.py new file mode 100644 index 0000000..1b2538e --- /dev/null +++ b/tests/test_four_skills_model.py @@ -0,0 +1,314 @@ +"""Synthetic CPU-only four-skills seam tests: no Kit, robot data, or physical claims.""" + +import hashlib +import importlib.util +from pathlib import Path +import sys +import types + +import pytest +import torch + +ROOT = Path(__file__).resolve().parents[1] +LEGS = ROOT / "source/SkillFactory/SkillFactory/legs" +SPEC = importlib.util.spec_from_file_location("_four_skills_model_test", LEGS / "four_skills/model.py") +model_module = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(model_module) +FourSkillActorCritic = model_module.FourSkillActorCritic +features = model_module.features +save_checkpoint = model_module.save_checkpoint +load_policy = model_module.load_policy + + +@pytest.fixture(autouse=True) +def cpu_only(): + # Small synthetic matrices do not benefit from a large host thread pool. + previous = torch.get_num_threads() + torch.set_num_threads(1) + yield + torch.set_num_threads(previous) + + +def test_four_skills_model_shapes_zero_baseline_and_shared_inputs(): + model = FourSkillActorCritic(hidden=16) + x = torch.randn(7, 48) + assert features(x) is x + action, latent, logp, value = model.act(x, deterministic=True) + assert action.shape == latent.shape == (7, 3) + assert logp.shape == value.shape == (7,) + assert not action.any() and not latent.any() + assert torch.isfinite(logp).all() and torch.isfinite(value).all() + assert torch.equal(value, model.value(x)) + assert model.actor[0].in_features == model.critic[0].in_features == 48 + assert model.feature_version == "four-skills-48-fixed-si-v1" + assert model.model_config == {"hidden": 16, "initial_std": 0.08} + assert model.critic[-1].weight.any() + assert not torch.optim.Adam(model.parameters()).state + + +def test_four_skills_model_fixed_si_order_and_actor_critic_normalization(): + model = FourSkillActorCritic(hidden=8) + expected_offset = torch.zeros(48) + expected_offset[:3] = torch.tensor([0.0, 0.8, -1.5]) + expected_scale = torch.tensor( + [0.5] * 3 + [10.0] * 3 + [50.0] * 3 + [5.0] * 3 + + [1.0] * 3 + [5.0] * 3 + [0.1] * 3 + [0.5] * 3 + + [0.4] * 3 + [1.0] * 3 + [50.0] * 3 + [0.05] + + [1.0] * 4 + [0.4] * 3 + [1.0] + [1.0] * 3 + [1.0] * 3 + ) + assert torch.equal(model.offset, expected_offset) + assert torch.equal(model.scale, expected_scale) + seen = [] + handles = [net[0].register_forward_pre_hook(lambda _, args: seen.append(args[0])) + for net in (model.actor, model.critic)] + normalized = torch.linspace(-2, 2, 48).expand(2, -1) + model.value(expected_offset + expected_scale * normalized) + for handle in handles: + handle.remove() + assert len(seen) == 2 + assert torch.allclose(seen[0], normalized, atol=1e-6) + assert torch.equal(seen[0], seen[1]) + + +def test_four_skills_model_latent_logp_tanh_and_std_bounds(): + model = FourSkillActorCritic(hidden=8) + x = torch.randn(11, 48) + action, latent, logp, value = model.act(x) + assert latent.any() and action.abs().max() <= 1 + assert torch.equal(action, latent.tanh()) + evaluated_logp, evaluated_value = model.evaluate(x, latent) + assert torch.equal(logp, evaluated_logp) and torch.equal(value, evaluated_value) + expected = torch.distributions.Normal(torch.zeros_like(latent), 0.08).log_prob(latent).sum(-1) + assert torch.allclose(logp, expected, atol=1e-6) + for log_std, expected_std in [(-100.0, 0.02), (100.0, 0.5)]: + with torch.no_grad(): + model.log_std.fill_(log_std) + assert torch.allclose(model.distribution_value(x)[0].stddev, torch.full_like(latent, expected_std)) + + +@pytest.mark.parametrize("kwargs", [ + {"hidden": True}, {"hidden": 8.0}, {"hidden": 7}, {"hidden": 0}, {"hidden": -1}, + {"initial_std": True}, {"initial_std": "0.08"}, {"initial_std": 0.0}, + {"initial_std": float("nan")}, {"initial_std": float("inf")}, + {"initial_std": 0.019}, {"initial_std": 0.501}, +]) +def test_four_skills_model_rejects_invalid_args(kwargs): + with pytest.raises(ValueError): + FourSkillActorCritic(**kwargs) + + +@pytest.mark.parametrize("bad", [ + torch.zeros(2, 46), torch.zeros(2, 32), torch.zeros(48), torch.zeros(0, 48), + torch.zeros(2, 48, dtype=torch.float64), torch.zeros(2, 48, dtype=torch.int64), + torch.full((2, 48), float("nan")), torch.full((2, 48), float("inf")), [[0.0] * 48], + torch.empty(2, 48, device="meta"), torch.zeros(2, 48).to_sparse(), +]) +def test_four_skills_model_rejects_bad_features(bad): + model = FourSkillActorCritic(hidden=8) + for method in (features, model.act, model.value): + with pytest.raises(ValueError, match="features"): + method(bad) + + +@pytest.mark.parametrize("bad", [torch.zeros(2, 4), torch.zeros(3, 3), + torch.zeros(2, 3, dtype=torch.float64), + torch.full((2, 3), float("nan")), None, + torch.empty(2, 3, device="meta")]) +def test_four_skills_model_rejects_bad_latents(bad): + with pytest.raises(ValueError, match="latent"): + FourSkillActorCritic(hidden=8).evaluate(torch.zeros(2, 48), bad) + + +def test_four_skills_model_exact_types_and_global_default_dtype(): + with pytest.raises(ValueError, match="bool"): + FourSkillActorCritic(hidden=8).act(torch.zeros(2, 48), deterministic=1) + default = FourSkillActorCritic() + assert default.model_config == {"hidden": 128, "initial_std": 0.08} + for std in (0.02, 0.5): + assert FourSkillActorCritic(hidden=8, initial_std=std).model_config["initial_std"] == std + previous = torch.get_default_dtype() + try: + torch.set_default_dtype(torch.float64) + model = FourSkillActorCritic(hidden=8) + assert all(p.dtype == torch.float32 for p in model.parameters()) + assert model.value(torch.zeros(2, 48, dtype=torch.float32)).dtype == torch.float32 + finally: + torch.set_default_dtype(previous) + + +def test_four_skills_model_generic_standing_ppo_step_synthetic(): + # Private test namespace avoids SkillFactory.__init__ (Kit registration), even + # when this test is invoked directly rather than through scripts/test_cpu.py. + name = "_four_skills_cpu_legs" + package = types.ModuleType(name) + package.__path__ = [str(LEGS)] + sys.modules[name] = package + try: + spec = importlib.util.spec_from_file_location(name + ".standing_ppo", LEGS / "standing_ppo.py") + standing = importlib.util.module_from_spec(spec) + spec.loader.exec_module(standing) + torch.manual_seed(23) + model = FourSkillActorCritic(hidden=8) + optimizer = torch.optim.Adam(model.parameters(), lr=3e-4) + x = torch.randn(3, 4, 48) + with torch.no_grad(): + _, latent, logp, value = model.act(x.flatten(0, 1)) + data = {"features": x, "latent": latent.reshape(3, 4, 3), + "log_prob": logp.reshape(3, 4), "value": value.reshape(3, 4), + "next_value": torch.zeros(3, 4), "reward": torch.randn(3, 4), + "done": torch.zeros(3, 4, dtype=torch.bool)} + cfg = {"gamma": 0.99, "gae_lambda": 0.95, "epochs": 2, "minibatch_size": 4, + "target_kl": 1.0, "clip_ratio": 0.2, "value_coefficient": 0.5, "max_gradient_norm": 1.0} + result = standing.ppo_step(model, optimizer, data, cfg) + assert result["minibatches"] == 6 and result["sampling_log_prob_error"] == 0 + assert optimizer.state and model.actor[-1].weight.any() + assert all(torch.isfinite(p).all() for p in model.parameters()) + finally: + for module in list(sys.modules): + if module == name or module.startswith(name + "."): + del sys.modules[module] + + +@pytest.fixture +def checkpoint(tmp_path): + torch.manual_seed(7) + model = FourSkillActorCritic(hidden=8) + optimizer = torch.optim.Adam(model.parameters(), lr=3e-4) + # Exercise nonempty Adam moments without robot data or a training run. + x = torch.randn(4, 48) + logp, value = model.evaluate(x, torch.randn(4, 3) * 0.08) + (value.square().mean() - logp.mean()).backward() + optimizer.step() + path = tmp_path / "policy.pt" + identity = {"task": "go2-fl-four-skills-v0.5.0", "source": "synthetic", "seed": 7} + save_checkpoint(path, model, optimizer, identity, 1, {"loss": 0.25}) + return path, model, optimizer, identity, x + + +def assert_tree_equal(left, right): + assert type(left) is type(right) + if isinstance(left, torch.Tensor): + assert torch.equal(left, right) + elif isinstance(left, dict): + assert left.keys() == right.keys() + for key in left: + assert_tree_equal(left[key], right[key]) + elif isinstance(left, (list, tuple)): + assert len(left) == len(right) + for a, b in zip(left, right): + assert_tree_equal(a, b) + else: + assert left == right + + +def test_four_skills_model_checkpoint_exact_roundtrip(checkpoint): + path, model, optimizer, identity, x = checkpoint + rng = torch.get_rng_state().clone() + digest = hashlib.sha256(path.read_bytes()).hexdigest() + loaded, payload = load_policy(path, expected_sha256=digest, expected_identity=dict(reversed(list(identity.items())))) + assert not loaded.training and next(loaded.parameters()).device.type == "cpu" + assert torch.equal(rng, torch.get_rng_state()) # loading does not reseed callers + assert payload["format"] == "skillfactory.four-skills-ppo.v1" + assert payload["features"] == model.feature_version and payload["feature_dim"] == 48 + assert payload["update"] == 1 and payload["metrics"] == {"loss": 0.25} + assert_tree_equal(dict(model.state_dict()), dict(loaded.state_dict())) + assert_tree_equal(optimizer.state_dict(), payload["optimizer"]) + assert torch.equal(payload["rng"]["cpu"], rng) + assert payload["rng"]["cuda"] == [] + for a, b in zip(model.act(x, deterministic=True), loaded.act(x, deterministic=True)): + assert torch.equal(a, b) + with pytest.raises(ValueError, match="identity"): + load_policy(path, expected_identity={**identity, "source": "wrong"}) + with pytest.raises(ValueError, match="device"): + load_policy(path, device="not-a-device") + + +def test_four_skills_model_digest_checked_before_torch_load(checkpoint, monkeypatch): + path, *_ = checkpoint + calls = [] + monkeypatch.setattr(torch, "load", lambda *args, **kwargs: calls.append(args)) + with pytest.raises(ValueError, match="SHA256"): + load_policy(path, expected_sha256="0" * 64) + assert not calls + + +@pytest.mark.parametrize("field,value", [ + ("format", "skillfactory.standing-ppo-checkpoint.v1"), + ("features", "contact-cycle-sim-46-fixed-si-v1"), ("feature_dim", 46), ("feature_dim", 32), + ("feature_dim", 48.0), ("model_config", {"hidden": True, "initial_std": 0.08}), + ("model_config", {"hidden": 8, "initial_std": float("nan")}), + ("model_config", {"hidden": 8, "initial_std": 0.08, "extra": 1}), + ("update", True), ("update", -1), ("rng", {}), ("optimizer", {}), ("identity", {}), + ("rng", {"cpu": torch.zeros(2, dtype=torch.uint8), "cuda": []}), + ("identity", {"not_json": torch.zeros(1)}), ("metrics", {"loss": float("inf")}), +]) +def test_four_skills_model_rejects_malformed_checkpoint(checkpoint, field, value): + path, *_ = checkpoint + payload = torch.load(path, weights_only=True) + payload[field] = value + torch.save(payload, path) + with pytest.raises(ValueError): + load_policy(path) + + +@pytest.mark.parametrize("corruption", ["nonfinite", "optimizer_nonfinite", "shape", "dtype", "scale", "missing", "extra"]) +def test_four_skills_model_rejects_corrupt_state(checkpoint, corruption): + path, *_ = checkpoint + payload = torch.load(path, weights_only=True) + state = payload["model"] + if corruption == "nonfinite": + state["critic.0.weight"][0, 0] = float("inf") + elif corruption == "optimizer_nonfinite": + next(iter(payload["optimizer"]["state"].values()))["exp_avg"].flatten()[0] = float("nan") + elif corruption == "shape": + state["actor.0.weight"] = torch.zeros(8, 46) + elif corruption == "dtype": + state["log_std"] = state["log_std"].double() + elif corruption == "scale": + state["scale"][0] = 1.0 + elif corruption == "missing": + del state["log_std"] + else: + state["extra"] = torch.zeros(1) + torch.save(payload, path) + with pytest.raises(ValueError): + load_policy(path) + + +@pytest.mark.parametrize("corruption", ["parameter", "optimizer", "metrics", "custom"]) +def test_four_skills_model_atomic_nonfinite_and_custom_object_rejection(checkpoint, corruption): + path, model, optimizer, identity, _ = checkpoint + original = path.read_bytes() + metrics = None + if corruption == "parameter": + with torch.no_grad(): + next(model.parameters()).flatten()[0] = float("nan") + elif corruption == "optimizer": + next(iter(optimizer.state.values()))["exp_avg"].flatten()[0] = float("inf") + elif corruption == "metrics": + metrics = {"loss": float("nan")} + else: + metrics = {"custom": object()} + with pytest.raises(ValueError): + save_checkpoint(path, model, optimizer, identity, 2, metrics) + assert path.read_bytes() == original + assert list(path.parent.iterdir()) == [path] + + +def test_four_skills_model_atomic_replace_failure_cleanup(checkpoint, monkeypatch): + path, model, optimizer, identity, _ = checkpoint + original = path.read_bytes() + def fail_replace(*args): + raise OSError("synthetic replace failure") + monkeypatch.setattr(model_module.os, "replace", fail_replace) + with pytest.raises(OSError, match="synthetic"): + save_checkpoint(path, model, optimizer, identity, 2) + assert path.read_bytes() == original + assert list(path.parent.iterdir()) == [path] + + +def test_four_skills_model_rejects_unreadable_checkpoint(tmp_path): + path = tmp_path / "bad.pt" + path.write_bytes(b"not a torch checkpoint") + with pytest.raises(ValueError, match="weights-only"): + load_policy(path) diff --git a/tests/test_four_skills_safety.py b/tests/test_four_skills_safety.py new file mode 100644 index 0000000..f8547cd --- /dev/null +++ b/tests/test_four_skills_safety.py @@ -0,0 +1,88 @@ +"""Synthetic safety fault injection, never native physical acceptance.""" +import ast +from pathlib import Path +from types import SimpleNamespace as NS + +import pytest +import torch + +ROOT = Path(__file__).resolve().parents[1] +PATH = ROOT / 'source/SkillFactory/SkillFactory/tasks/manager_based/four_skills/mdp.py' +tree = ast.parse(PATH.read_text()) +method = next(n for c in tree.body if isinstance(c, ast.ClassDef) and c.name == 'FourSkillState' + for n in c.body if isinstance(n, ast.FunctionDef) and n.name == 'safety') +from SkillFactory.legs.contact_cycle.task import phase_failures, phase_objective +namespace = {'torch': torch, 'phase_failures': phase_failures} +exec(compile(ast.fix_missing_locations(ast.Module(body=[method], type_ignores=[])), str(PATH), 'exec'), namespace) + + +def fake_state(): + torque = torch.zeros(1, 3) + q = torch.zeros(1, 3) + return NS(root=lambda: (torch.tensor([[0., 0., .3]]), torch.tensor([[0., 0., 0., 1.]]), + torch.zeros(1, 3), torch.zeros(1, 3)), + foot_center=lambda: torch.tensor([[0., 0., .02]]), bundle={'foot': {'radius': .02}}, + robot=NS(data=NS(applied_torque=NS(torch=torque), joint_vel=NS(torch=torch.zeros(1, 3)))), + joints=[0, 1, 2], effort=torch.ones(3) * 20, speed=torch.ones(3) * 20, + lower=torch.ones(3) * -2, upper=torch.ones(3) * 2, q=lambda: q, + raw_sensors=lambda: torch.zeros(1, 18), contact=lambda: torch.tensor([[0., 0., 50.]]), + guide=torch.zeros(1, 2, 6), auxiliary=torch.zeros(1, 2, 6), external=torch.zeros(1, 2, 6), + base_origin=torch.tensor([[0., 0., .3]]), acquisition=NS(failed=torch.tensor([False])), + separation=NS(premature=torch.tensor([False])), weight=torch.tensor([50.]), + reference=lambda: (torch.tensor([0]), torch.tensor([0.]), None, None, None), + controller=NS(q_ik=torch.zeros(1, 3), action=torch.zeros(1, 3)), + residual_request=torch.zeros(1, 3), tilt=lambda: torch.zeros(1)) + + +@pytest.mark.parametrize('bad', [float('nan'), float('inf'), -float('inf')]) +def test_nonfinite_torque_fails_closed_not_nan_comparison_bypass(bad): + s = fake_state() + assert not any(bool(x.any()) for x in namespace['safety'](s).values()) + s.robot.data.applied_torque.torch[0, 1] = bad + assert namespace['safety'](s)['nonfinite'].tolist() == [True] + + +def test_original_actuator_collision_and_guide_limits_preserved(): + s = fake_state() + s.robot.data.applied_torque.torch[0, 0] = 21 + assert namespace['safety'](s)['effort_velocity'].item() + s = fake_state() + s.guide[0, 0, 2] = .001 + assert namespace['safety'](s)['vertical_guide'].item() + s = fake_state() + s.foot_center = lambda: torch.tensor([[0., 0., .0179]]) + assert namespace['safety'](s)['penetration'].item() + + +def test_brief_ready_cannot_disable_requested_hold_watchdog(): + method = next(n for n in tree.body if isinstance(n, ast.FunctionDef) and n.name == 'hold_watchdog') + ns = {'torch': torch} + exec(compile(ast.fix_missing_locations(ast.Module(body=[method], type_ignores=[])), str(PATH), 'exec'), ns) + completed = torch.tensor([False]) + deadline = torch.tensor([4.]) + required = torch.tensor([.4]) + expired = torch.tensor([False]) + # Even granting brief ready intervals directly, no0.4s hold can complete. + for tick in range(1, 2501): + ready = torch.tensor([tick % 100 < 26]) + held = torch.tensor([.002 * (tick % 100) if bool(ready[0]) else 0.]) + completed, now_expired = ns['hold_watchdog'](ready, held, required, completed, + torch.tensor([tick * .002]), deadline) + expired |= now_expired + assert expired.item() and not completed.item() + # A genuinely completed hold stays complete during indefinite endpoint hold; + # the separate existing bad-hold rule still owns later physical failures. + completed, expired = ns['hold_watchdog'](torch.tensor([True]), torch.tensor([.5]), required, + torch.tensor([False]), torch.tensor([3.]), deadline) + assert completed.item() and not expired.item() + completed, expired = ns['hold_watchdog'](torch.tensor([False]), torch.tensor([0.]), required, + completed, torch.tensor([100.]), deadline) + assert completed.item() and not expired.item() + + +def test_phase_reward_does_not_pay_for_early_lift_during_unload(): + def score(gap): + return phase_objective(torch.tensor([1]), torch.tensor([.5]), torch.tensor([gap]), + torch.tensor([gap]), torch.tensor([25.]), torch.tensor([25.]), torch.zeros(1), + torch.zeros(1), torch.tensor([50.]))[0] + assert score(0.).item() > score(.01).item()