E-Rong commited on
Commit
90f17a9
·
verified ·
1 Parent(s): 17efbc9

Add smoke_test.py for HF Job validation

Browse files
Files changed (1) hide show
  1. smoke_test.py +126 -0
smoke_test.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Smoke test: download TIL repo, verify imports, run 100 steps, push dummy checkpoint."""
3
+ import os, sys
4
+
5
+ print("="*60)
6
+ print("SMOKE TEST: HF Job private repo access + training basics")
7
+ print("="*60)
8
+
9
+ # 1. Test snapshot_download of private Space
10
+ print("\n[1/5] Downloading TIL repo via snapshot_download...")
11
+ from huggingface_hub import snapshot_download
12
+ snapshot_download(
13
+ repo_id="e-rong/til-26-ae",
14
+ repo_type="space",
15
+ local_dir="/app/til-26-ae-repo",
16
+ allow_patterns=["til_environment/**"],
17
+ )
18
+ print(" ✓ Downloaded")
19
+
20
+ # 2. Install TIL environment
21
+ print("\n[2/5] Installing TIL environment...")
22
+ import subprocess
23
+ subprocess.run(["pip", "install", "-e", "."], cwd="/app/til-26-ae-repo/til-26-ae", check=True)
24
+ print(" ✓ Installed")
25
+
26
+ # 3. Verify imports
27
+ print("\n[3/5] Verifying imports...")
28
+ sys.path.insert(0, "/app/til-26-ae-repo/til-26-ae")
29
+ from til_environment.bomberman_env import Bomberman
30
+ from til_environment.config import default_config
31
+ from pettingzoo.utils.conversions import aec_to_parallel
32
+ print(" ✓ Imports OK")
33
+
34
+ # 4. Run 100 steps of dummy training
35
+ print("\n[4/5] Running 100 training steps...")
36
+ from sb3_contrib import MaskablePPO
37
+ from sb3_contrib.common.wrappers import ActionMasker
38
+ from stable_baselines3.common.monitor import Monitor
39
+ import gymnasium
40
+ from gymnasium.spaces import Box, Discrete
41
+ import numpy as np
42
+
43
+ class QuickEnv(gymnasium.Env):
44
+ def __init__(self):
45
+ super().__init__()
46
+ cfg = default_config()
47
+ cfg.env.render_mode = None
48
+ raw = Bomberman(cfg)
49
+ self._parallel_env = aec_to_parallel(raw)
50
+ self.agent_id = "agent_0"
51
+ self._episode_count = 0
52
+ self.action_space = Discrete(6)
53
+ vl = int(cfg.dynamics.vision.behind) + int(cfg.dynamics.vision.ahead) + 1
54
+ vw = int(cfg.dynamics.vision.left) + int(cfg.dynamics.vision.right) + 1
55
+ av = vl * vw * 25
56
+ br = int(cfg.entities.base.vision_radius)
57
+ bs = 2 * br + 1
58
+ bv = bs * bs * 25
59
+ self._obs_size = av + bv + 11
60
+ self.observation_space = Box(low=-np.inf, high=np.inf, shape=(self._obs_size,), dtype=np.float32)
61
+ self._last_action_mask = None
62
+ self._last_obs_dict = None
63
+ def reset(self, seed=None, options=None):
64
+ self._episode_count += 1
65
+ obs_dict, _ = self._parallel_env.reset(seed=self._episode_count, options=options)
66
+ self._last_obs_dict = obs_dict
67
+ self._last_action_mask = obs_dict[self.agent_id]["action_mask"].astype(bool)
68
+ return self._flatten(obs_dict[self.agent_id]), {}
69
+ def step(self, action):
70
+ actions = {self.agent_id: action}
71
+ for aid, obs in self._last_obs_dict.items():
72
+ if aid != self.agent_id:
73
+ valid = np.where(obs["action_mask"] == 1)[0]
74
+ actions[aid] = int(np.random.choice(valid)) if len(valid) > 0 else 0
75
+ obs_dict, rewards, terminations, truncations, infos = self._parallel_env.step(actions)
76
+ self._last_obs_dict = obs_dict
77
+ if self.agent_id not in obs_dict:
78
+ return np.zeros(self._obs_size, dtype=np.float32), 0.0, True, False, {}
79
+ self._last_action_mask = obs_dict[self.agent_id]["action_mask"].astype(bool)
80
+ obs = self._flatten(obs_dict[self.agent_id])
81
+ r = float(rewards.get(self.agent_id, 0.0))
82
+ done = terminations.get(self.agent_id, False) or truncations.get(self.agent_id, False)
83
+ return obs, r, done, False, infos.get(self.agent_id, {})
84
+ def action_masks(self):
85
+ return self._last_action_mask
86
+ def _flatten(self, od):
87
+ return np.concatenate([
88
+ od["agent_viewcone"].flatten(), od["base_viewcone"].flatten(),
89
+ np.array([od["direction"]], dtype=np.float32),
90
+ od["location"].flatten().astype(np.float32),
91
+ od["base_location"].flatten().astype(np.float32),
92
+ od["health"].flatten().astype(np.float32),
93
+ np.array([od["frozen_ticks"]], dtype=np.float32),
94
+ od["base_health"].flatten().astype(np.float32),
95
+ od["team_resources"].flatten().astype(np.float32),
96
+ np.array([od["team_bombs"]], dtype=np.float32),
97
+ np.array([od["step"]], dtype=np.float32),
98
+ ], dtype=np.float32)
99
+
100
+ env = ActionMasker(QuickEnv(), lambda e: e.action_masks())
101
+ env = Monitor(env)
102
+
103
+ model = MaskablePPO(
104
+ "MlpPolicy", env,
105
+ learning_rate=3e-4, n_steps=128, batch_size=32, n_epochs=2,
106
+ gamma=0.99, clip_range=0.2, ent_coef=0.01,
107
+ verbose=0, device="cuda",
108
+ )
109
+ model.learn(total_timesteps=100, progress_bar=False)
110
+ print(" ✓ 100 steps completed")
111
+
112
+ # 5. Push dummy checkpoint to Hub
113
+ print("\n[5/5] Pushing dummy checkpoint to Hub...")
114
+ from huggingface_hub import HfApi
115
+ model.save("/app/smoke_test_ckpt.zip")
116
+ HfApi().upload_file(
117
+ path_or_fileobj="/app/smoke_test_ckpt.zip",
118
+ path_in_repo="smoke_test_ckpt.zip",
119
+ repo_id="E-Rong/til-26-ae-agent",
120
+ repo_type="model",
121
+ )
122
+ print(" ✓ Pushed to Hub")
123
+
124
+ print("\n" + "="*60)
125
+ print("SMOKE TEST PASSED — Ready for full training job")
126
+ print("="*60)