trioskosmos commited on
Commit
671525a
·
verified ·
1 Parent(s): 69c4849

Upload ai/OPTIMIZATION_IDEAS.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. ai/OPTIMIZATION_IDEAS.md +74 -0
ai/OPTIMIZATION_IDEAS.md ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # AI Training Optimization Roadmap
2
+
3
+ This document outlines potential strategies to further accelerate training throughput, focusing on optimizations that require significant refactoring or architectural changes.
4
+
5
+ ## 1. GPU-Resident Environment (The "Isaac Gym" Approach)
6
+ **Impact:** High (Potential 5-10x speedup for large batches)
7
+ **Difficulty:** High
8
+
9
+ Currently, the `VectorEnv` runs on CPU (Numba), and observations are copied to the GPU for the Policy Network. This CPU -> GPU transfer becomes a bottleneck at high throughputs (e.g., >100k SPS).
10
+
11
+ * **Proposal:** Port the entire logic in `ai/vector_env.py` and `engine/game/fast_logic.py` to **Numba CUDA** or **CuPy**.
12
+ * **Result:** The environment state remains on the GPU. `step()` returns a GPU tensor directly, which is fed into the Policy Network without transfer.
13
+ * **Challenges:** requires rewriting Numba CPU kernels to Numba CUDA kernels (handling thread divergence, shared memory, etc.).
14
+ * **Status:** [FEASIBILITY ANALYSIS COMPLETE]. See `ai/GPU_MIGRATION_GUIDE.md` and `ai/cuda_proof_of_concept.py` for the architectural blueprint.
15
+
16
+ ## 2. Pure Numba Adapter & Zero-Copy Interface
17
+ **Impact:** Medium (10-20% speedup)
18
+ **Difficulty:** Medium
19
+
20
+ The `VectorEnvAdapter` currently performs some Python-level logic in `step_wait` (reward calculation, array copying, info dictionary construction).
21
+
22
+ * **Proposal:** Move the reward calculation (`delta_scores * 50 - 5`) and "Auto-Reset" logic into the Numba `VectorGameState` class.
23
+ * **Result:** `step_wait` becomes a thin wrapper that just returns views of the underlying Numba arrays.
24
+ * **Refinement:** Use the `__array_interface__` or blind pointer passing to avoid any numpy array allocation overhead in Python.
25
+
26
+ ## 3. Observation Compression & Quantization
27
+ **Impact:** Medium (Reduced memory bandwidth, larger batch sizes)
28
+ **Difficulty:** Low/Medium
29
+
30
+ The observation space is 8192 floats (`float32`). This is 32KB per environment per step. For 256 envs, that's 8MB per step.
31
+
32
+ * **Proposal:** Most features are binary (0/1) or small integers.
33
+ * Return observations as `uint8` or `float16`.
34
+ * Use a custom SB3 `FeaturesExtractor` to cast to `float32` only *inside* the GPU network.
35
+ * **Benefit:** Reduces memory bandwidth between CPU and GPU by 4x (`float32` -> `uint8`).
36
+
37
+ ## 4. Incremental Action Masking
38
+ **Impact:** Low/Medium
39
+ **Difficulty:** Medium
40
+
41
+ `compute_action_masks` scans the entire hand every step.
42
+
43
+ * **Proposal:** Maintain the action mask as part of the persistent state.
44
+ * Only update the mask when the state changes (e.g., Card Played, Energy Charged).
45
+ * Most steps (e.g., Opponent Turn simulation) might not change the Agent's legal actions if the Agent is waiting? (Actually, Agent acts every step in this setup).
46
+ * Optimization: If a card was illegal last step and state hasn't changed relevantly (e.g. energy), it's still illegal. This is hard to prove correct.
47
+
48
+ ## 5. Opponent Distillation / Caching
49
+ **Impact:** Medium (Depends on Opponent Complexity)
50
+ **Difficulty:** High
51
+
52
+ If we move to smarter opponents (e.g., MCTS or Neural Net based), `step_opponent_vectorized` will become the bottleneck.
53
+
54
+ * **Proposal:**
55
+ * **Distillation:** Train a tiny decision tree or small MLP to mimic the smart opponent and run it via Numba inference.
56
+ * **Caching:** Pre-calculate opponent moves for common states? (Input space too large).
57
+
58
+ ## 6. Asynchronous Environment Stepping (Pipelining)
59
+ **Impact:** Medium
60
+ **Difficulty:** Medium
61
+
62
+ While the GPU is performing the Forward/Backward pass (Policy Update), the CPU is idle.
63
+
64
+ * **Proposal:** Run `VectorEnv.step()` in a separate thread/process while the GPU trains on the *previous* batch.
65
+ * **Note:** SB3's `SubprocVecEnv` tries this, but IPC overhead kills it. We need a **Threaded** Numba environment (releasing GIL) to do this efficiently in one process. Numba's `@njit(nogil=True)` enables this.
66
+
67
+ ## 7. Memory Layout Optimization (AoS vs SoA)
68
+ **Impact:** Low/Medium
69
+ **Difficulty:** High (Refactor hell)
70
+
71
+ Current layout mixes Structure of Arrays (SoA) and Arrays of Structures (AoS).
72
+
73
+ * **Proposal:** Ensure all hot arrays (`batch_global_ctx`, `batch_scores`) are contiguous in memory for the exact access pattern used by `step_vectorized`.
74
+ * **Check:** Access `batch_global_ctx[i, :]` vs `batch_global_ctx[:, k]`. Numba prefers loop-invariant access.