-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathbase.py
More file actions
426 lines (395 loc) · 14.9 KB
/
Copy pathbase.py
File metadata and controls
426 lines (395 loc) · 14.9 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
"""Add hidden layer to the initial projection layer.
This is adapted from scripts/vnext/single_bandset_band_dropout/base_band_dropout_no_s1_drop_random_time.py
"""
import logging
from olmo_core.config import DType
from olmo_core.distributed.parallel.data_parallel import (
DataParallelConfig,
DataParallelType,
)
from olmo_core.optim import AdamWConfig
from olmo_core.optim.scheduler import CosWithWarmup
from olmo_core.train.callbacks import (
BeakerCallback,
CheckpointerCallback,
ConfigSaverCallback,
GarbageCollectorCallback,
GPUMemoryMonitorCallback,
)
from olmo_core.train.checkpoint import CheckpointerConfig
from olmo_core.train.common import Duration, LoadStrategy
from olmo_core.train.config import TrainerConfig
from olmoearth_pretrain.data.constants import Modality
from olmoearth_pretrain.data.dataloader import OlmoEarthDataLoaderConfig
from olmoearth_pretrain.data.dataset import OlmoEarthDatasetConfig
from olmoearth_pretrain.evals.datasets.normalize import NormMethod
from olmoearth_pretrain.evals.metrics import EvalMetric
from olmoearth_pretrain.internal.common import (
build_common_components as build_common_components_default,
)
from olmoearth_pretrain.internal.experiment import (
CommonComponents,
OlmoEarthVisualizeConfig,
SubCmd,
main,
)
from olmoearth_pretrain.internal.utils import MODEL_SIZE_ARGS
from olmoearth_pretrain.nn.flexi_vit import (
PoolingType,
)
from olmoearth_pretrain.nn.flexihelios import (
EncoderConfig,
PredictorConfig,
)
from olmoearth_pretrain.nn.latent_mim import LatentMIMConfig
from olmoearth_pretrain.nn.tokenization import ModalityTokenization, TokenizationConfig
from olmoearth_pretrain.train.callbacks import (
DownstreamEvaluatorCallbackConfig,
OlmoEarthSpeedMonitorCallback,
OlmoEarthWandBCallback,
)
from olmoearth_pretrain.train.callbacks.evaluator_callback import (
DownstreamTaskConfig,
EvalMode,
)
from olmoearth_pretrain.train.loss import LossConfig
from olmoearth_pretrain.train.masking import MaskingConfig
from olmoearth_pretrain.train.train_module.contrastive_latentmim import (
ContrastiveLatentMIMTrainModuleConfig,
)
logger = logging.getLogger(__name__)
MAX_PATCH_SIZE = 8
MIN_PATCH_SIZE = 1
RANDOM_BAND_DROPOUT_MAX_RATE = 0.2
# Per-pixel MLP applied BEFORE patchification in the initial pixel -> token
# projection. Each entry adds one hidden layer of the given width (ReLU
# activations between / after). The resulting ``H x W x hidden[-1]`` feature
# map is then patchified and linearly projected to ``embedding_size``. Leave
# empty / None to keep the original single nn.Linear projection. Override from
# launch scripts via --model.encoder_config.patch_embed_hidden_sizes='[768, 768]'.
PATCH_EMBED_HIDDEN_SIZES: list[int] = [64]
S2_SINGLE_BANDSET = ModalityTokenization(
band_groups=[
[
"B02",
"B03",
"B04",
"B08",
"B05",
"B06",
"B07",
"B8A",
"B11",
"B12",
"B01",
"B09",
],
]
)
LANDSAT_SINGLE_BANDSET = ModalityTokenization(
band_groups=[
["B8", "B1", "B2", "B3", "B4", "B5", "B6", "B7", "B9", "B10", "B11"],
]
)
ONLY_DECODE_MODALITIES = [
Modality.WORLDCOVER.name,
Modality.SRTM.name,
Modality.OPENSTREETMAP_RASTER.name,
Modality.WRI_CANOPY_HEIGHT_MAP.name,
Modality.CDL.name,
Modality.WORLDCEREAL.name,
]
# No S1 dropout — only apply band dropout to S2 and Landsat.
BAND_DROPOUT_MODALITIES = [
Modality.SENTINEL2_L2A.name,
Modality.LANDSAT.name,
]
def _tokenization_config() -> TokenizationConfig:
return TokenizationConfig(
overrides={
"sentinel2_l2a": S2_SINGLE_BANDSET,
"landsat": LANDSAT_SINGLE_BANDSET,
}
)
def _masking_config(
tokenization_config: TokenizationConfig | None = None,
) -> MaskingConfig:
return MaskingConfig(
strategy_config={
"type": "random_time_with_decode",
"encode_ratio": 0.5,
"decode_ratio": 0.5,
"random_ratio": 0.5,
"only_decode_modalities": ONLY_DECODE_MODALITIES,
},
tokenization_config=tokenization_config,
)
def build_common_components(
script: str, cmd: SubCmd, run_name: str, cluster: str, overrides: list[str]
) -> CommonComponents:
"""Build the common components for an experiment."""
config = build_common_components_default(script, cmd, run_name, cluster, overrides)
config.training_modalities = [
Modality.SENTINEL2_L2A.name,
Modality.SENTINEL1.name,
Modality.LANDSAT.name,
Modality.WORLDCOVER.name,
Modality.SRTM.name,
Modality.OPENSTREETMAP_RASTER.name,
Modality.WRI_CANOPY_HEIGHT_MAP.name,
Modality.CDL.name,
Modality.WORLDCEREAL.name,
]
config.tokenization_config = _tokenization_config()
return config
def build_train_module_config(
common: CommonComponents,
) -> ContrastiveLatentMIMTrainModuleConfig:
"""Build the train module config for an experiment."""
return ContrastiveLatentMIMTrainModuleConfig(
optim_config=AdamWConfig(lr=0.0001, weight_decay=0.02, fused=False),
rank_microbatch_size=64,
masking_config=_masking_config(common.tokenization_config),
loss_config=LossConfig(
loss_config={
"type": "modality_patch_discrimination_masked_negatives_vec",
"tau": 0.1,
"same_target_threshold": 0.999,
"mask_negatives_for_modalities": ONLY_DECODE_MODALITIES,
}
),
contrastive_config=LossConfig(
loss_config={
"type": "InfoNCE",
"weight": 0.05,
}
),
token_exit_cfg={modality: 0 for modality in common.training_modalities},
max_grad_norm=1.0,
scheduler=CosWithWarmup(warmup_steps=8000),
ema_decay=(1.0, 1.0),
dp_config=DataParallelConfig(
name=DataParallelType.fsdp,
param_dtype=DType.bfloat16,
reduce_dtype=DType.float32,
),
)
def build_dataloader_config(common: CommonComponents) -> OlmoEarthDataLoaderConfig:
"""Build the dataloader config for an experiment."""
return OlmoEarthDataLoaderConfig(
num_workers=16,
global_batch_size=512,
token_budget=2250,
prefetch_factor=4,
sampled_hw_p_list=list(range(1, 13)),
min_patch_size=MIN_PATCH_SIZE,
max_patch_size=MAX_PATCH_SIZE,
work_dir=common.save_folder,
seed=3622,
num_masked_views=2,
masking_config=_masking_config(common.tokenization_config),
)
def build_dataset_config(common: CommonComponents) -> OlmoEarthDatasetConfig:
"""Build the dataset config for an experiment."""
return OlmoEarthDatasetConfig(
h5py_dir="/weka/dfive-default/helios/dataset/osm_sampling/h5py_data_w_missing_timesteps_zstd_3_128_x_4/cdl_gse_landsat_openstreetmap_raster_sentinel1_sentinel2_l2a_srtm_worldcereal_worldcover_worldpop_wri_canopy_height_map/1138828",
training_modalities=common.training_modalities,
)
def build_trainer_config(common: CommonComponents) -> TrainerConfig:
"""Build the trainer config for an experiment."""
MAX_DURATION = Duration.epochs(300)
METRICS_COLLECT_INTERVAL = 10
CANCEL_CHECK_INTERVAL = 25
LOAD_STRATEGY = LoadStrategy.if_available
WANDB_USERNAME = "eai-ai2" # nosec
WANDB_PROJECT = "2026_04_22_add_hidden_layer_to_initial_projection"
PERMANENT_SAVE_INTERVAL = 5000
EPHERMERAL_SAVE_INTERVAL = 250
checkpointer_config = CheckpointerConfig(work_dir=common.save_folder)
wandb_callback = OlmoEarthWandBCallback(
name=common.run_name,
project=WANDB_PROJECT,
entity=WANDB_USERNAME,
enabled=True,
)
garbage_collector_callback = GarbageCollectorCallback(gc_interval=1)
EVAL_TASKS = {
"m-eurosat": DownstreamTaskConfig(
dataset="m-eurosat",
embedding_batch_size=128,
num_workers=0,
pooling_type=PoolingType.MEAN,
norm_stats_from_pretrained=True,
norm_method=NormMethod.NORM_NO_CLIP_2_STD,
input_modalities=[Modality.SENTINEL2_L2A.name],
eval_mode=EvalMode.KNN,
primary_metric=EvalMetric.ACCURACY,
eval_interval=Duration.steps(4000),
),
"m_so2sat": DownstreamTaskConfig(
dataset="m-so2sat",
embedding_batch_size=128,
num_workers=4,
pooling_type=PoolingType.MEAN,
norm_stats_from_pretrained=True,
eval_interval=Duration.steps(20000),
input_modalities=[Modality.SENTINEL2_L2A.name],
eval_mode=EvalMode.KNN,
primary_metric=EvalMetric.ACCURACY,
),
"mados": DownstreamTaskConfig(
dataset="mados",
embedding_batch_size=128,
probe_batch_size=128,
num_workers=8,
pooling_type=PoolingType.MEAN,
norm_stats_from_pretrained=False,
norm_method=NormMethod.NORM_NO_CLIP_2_STD,
probe_lr=0.01,
eval_interval=Duration.steps(4000),
input_modalities=[Modality.SENTINEL2_L2A.name],
eval_mode=EvalMode.LINEAR_PROBE,
primary_metric=EvalMetric.MICRO_F1,
),
"pastis": DownstreamTaskConfig(
dataset="pastis",
embedding_batch_size=32,
probe_batch_size=8,
num_workers=2,
pooling_type=PoolingType.MEAN,
norm_stats_from_pretrained=True,
probe_lr=0.1,
eval_interval=Duration.steps(20000),
input_modalities=[Modality.SENTINEL2_L2A.name],
epochs=50,
eval_mode=EvalMode.LINEAR_PROBE,
primary_metric=EvalMetric.MIOU,
),
"yemen_crop": DownstreamTaskConfig(
dataset="yemen_crop",
embedding_batch_size=32,
probe_batch_size=8,
num_workers=2,
pooling_type=PoolingType.MEAN,
norm_stats_from_pretrained=True,
norm_method=NormMethod.NORM_NO_CLIP_2_STD,
eval_interval=Duration.steps(20000),
probe_lr=0.001,
input_modalities=[Modality.SENTINEL2_L2A.name],
epochs=50,
eval_mode=EvalMode.LINEAR_PROBE,
),
"geo_ecosystem_annual_test": DownstreamTaskConfig(
dataset="geo_ecosystem_annual_test",
embedding_batch_size=32,
probe_batch_size=8,
num_workers=8,
pooling_type=PoolingType.MEAN,
norm_stats_from_pretrained=True,
norm_method=NormMethod.NORM_NO_CLIP_2_STD,
probe_lr=0.01,
eval_interval=Duration.steps(20000),
input_modalities=[Modality.SENTINEL2_L2A.name],
epochs=50,
eval_mode=EvalMode.LINEAR_PROBE,
),
"canada_wildfire_sat_eval_split": DownstreamTaskConfig(
dataset="canada_wildfire_sat_eval_split",
embedding_batch_size=32,
probe_batch_size=16,
patch_size=5, # TODO: This is changeable but we should know the valid sizes for inputs
num_workers=2,
pooling_type=PoolingType.MEAN,
norm_stats_from_pretrained=True,
norm_method=NormMethod.NORM_NO_CLIP_2_STD,
probe_lr=0.1,
eval_interval=Duration.steps(20000),
input_modalities=[Modality.SENTINEL2_L2A.name],
epochs=50,
eval_mode=EvalMode.LINEAR_PROBE,
use_dice_loss=True,
primary_metric=EvalMetric.CLASS_F1,
primary_metric_class=1,
),
}
trainer_config = (
TrainerConfig(
work_dir=common.save_folder,
load_strategy=LOAD_STRATEGY,
save_folder=common.save_folder,
cancel_check_interval=CANCEL_CHECK_INTERVAL,
metrics_collect_interval=METRICS_COLLECT_INTERVAL,
max_duration=MAX_DURATION,
checkpointer=checkpointer_config,
)
.with_callback("wandb", wandb_callback)
.with_callback("speed_monitor", OlmoEarthSpeedMonitorCallback())
.with_callback("gpu_memory_monitor", GPUMemoryMonitorCallback())
.with_callback("config_saver", ConfigSaverCallback())
.with_callback(
"downstream_evaluator",
DownstreamEvaluatorCallbackConfig(
tasks=EVAL_TASKS,
),
)
.with_callback("garbage_collector", garbage_collector_callback)
.with_callback("beaker", BeakerCallback())
.with_callback(
"checkpointer",
CheckpointerCallback(
save_interval=PERMANENT_SAVE_INTERVAL,
ephemeral_save_interval=EPHERMERAL_SAVE_INTERVAL,
),
)
)
return trainer_config
def build_visualize_config(common: CommonComponents) -> OlmoEarthVisualizeConfig:
"""Build the visualize config for an experiment."""
return OlmoEarthVisualizeConfig(
num_samples=None,
output_dir=str(f"{common.save_folder}/visualizations"),
std_multiplier=2.0,
)
def build_model_config(common: CommonComponents) -> LatentMIMConfig:
"""Build the model config for an experiment."""
model_size = MODEL_SIZE_ARGS["base_shallow_decoder"]
encoder_config = EncoderConfig(
embedding_size=model_size["encoder_embedding_size"],
num_heads=model_size["encoder_num_heads"],
depth=model_size["encoder_depth"],
mlp_ratio=model_size["mlp_ratio"],
supported_modality_names=common.training_modalities,
max_patch_size=MAX_PATCH_SIZE,
drop_path=0.1,
max_sequence_length=12,
tokenization_config=common.tokenization_config,
band_dropout_rate=RANDOM_BAND_DROPOUT_MAX_RATE,
random_band_dropout=True,
band_dropout_modalities=BAND_DROPOUT_MODALITIES,
patch_embed_hidden_sizes=PATCH_EMBED_HIDDEN_SIZES,
)
decoder_config = PredictorConfig(
encoder_embedding_size=model_size["encoder_embedding_size"],
decoder_embedding_size=model_size["decoder_embedding_size"],
depth=model_size["decoder_depth"],
mlp_ratio=model_size["mlp_ratio"],
num_heads=model_size["decoder_num_heads"],
supported_modality_names=common.training_modalities,
max_sequence_length=12,
tokenization_config=common.tokenization_config,
)
model_config = LatentMIMConfig(
encoder_config=encoder_config,
decoder_config=decoder_config,
)
return model_config
if __name__ == "__main__":
main(
common_components_builder=build_common_components,
model_config_builder=build_model_config,
train_module_config_builder=build_train_module_config,
dataset_config_builder=build_dataset_config,
dataloader_config_builder=build_dataloader_config,
trainer_config_builder=build_trainer_config,
visualize_config_builder=build_visualize_config,
)