File size: 3,627 Bytes
67ba414
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# Gradient Analysis Plotting

This folder contains the plotting utilities for the gradient-analysis workflow.

There are two plotting entry points:

1. [plot_gradient_analysis.py](./plot_gradient_analysis.py)
- pulls one W&B run directly
- exports local `json` / `csv`
- writes per-step PNG plots

2. [plot_icml_steps.py](./plot_icml_steps.py)
- builds a fixed 3-step comparison figure from already-exported `metrics.json` files
- intended for paper-style summary figures

For the training-side workflow and arguments, see:
- [docs/guide_gradient_analysis.md](../docs/guide_gradient_analysis.md)

Current default training behavior from [config/base.yaml](../config/base.yaml):
- `trainer.gradient_analysis_mode=True`
- `trainer.gradient_analysis_every=50`
- `trainer.gradient_analysis_env_groups=null`
- `trainer.gradient_analysis_group_size=null`
- `trainer.exit_after_gradient_analysis=False`

## Typical Workflow

### 1. Run one analysis job

Example helper runner:

```bash
bash scripts/runs/run_sokoban_ppo_filter_grad_analysis.sh \
  --gpus 0,1,2,3,4,5,6,7
```

That job:
- trains for `101` steps
- validates before training and every `10` steps
- runs gradient analysis at steps `1`, `51`, and `101`
- uses a training batch of `8x16`
- uses a separate gradient-analysis batch of `128x16`

### 2. List available analysis steps in W&B

```bash
python gradient_analysis/plot_gradient_analysis.py \
  --wandb-path deimos-xing/ragen_gradient_analysis/<run_id> \
  --list-steps
```

### 3. Plot all analysis steps from that run

```bash
python gradient_analysis/plot_gradient_analysis.py \
  --wandb-path deimos-xing/ragen_gradient_analysis/<run_id>
```

Default output directory:

```text
gradient_analysis_outputs/<run_name>_<run_id>/
```

### 4. Plot only one step

```bash
python gradient_analysis/plot_gradient_analysis.py \
  --wandb-path deimos-xing/ragen_gradient_analysis/<run_id> \
  --step 1
```

### 5. Choose your own output directory

```bash
python gradient_analysis/plot_gradient_analysis.py \
  --wandb-path deimos-xing/ragen_gradient_analysis/<run_id> \
  --step 1 \
  --output-dir gradient_analysis_outputs/my_custom_dir
```

## Files Produced By `gradient_analysis/plot_gradient_analysis.py`

For each selected step, the script writes:

- `gradient_analysis_summary_step_<N>.png`
- `gradient_analysis_plots_step_<N>.png`
- `gradient_analysis_loss_plots_step_<N>.png`
- `gradient_analysis_reward_std_step_<N>.png`
- `gradient_analysis_normed_grads_step_<N>.png`
- `gradient_analysis_metrics_step_<N>.json`
- `gradient_analysis_bucket_rv_table_step_<N>.csv`

The `metrics.json` export is the bridge to the paper-style plotting script.

## Building A 3-Step Comparison Figure

If you have three exported step directories and want the fixed grid figure:

```bash
python gradient_analysis/plot_icml_steps.py \
  --mode ppo \
  --step0-dir /path/to/step0 \
  --step20-dir /path/to/step20 \
  --step40-dir /path/to/step40 \
  --out gradient_analysis_outputs/ppo_step0_20_40.png
```

Each step directory must contain:

```text
metrics.json
```

If your exported file is named `gradient_analysis_metrics_step_<N>.json`, copy or rename it to `metrics.json` inside each step directory before calling `plot_icml_steps.py`.

## What To Inspect First

For a new run, start with:

1. `gradient_analysis_summary_step_<N>.png`
2. `gradient_analysis_plots_step_<N>.png`
3. `gradient_analysis_metrics_step_<N>.json`

Those three are usually enough to tell:
- how many buckets were populated
- whether task gradients dominate regularizer gradients
- whether gradient magnitude is monotonic or non-monotonic in reward variance