# Created by BVH, Jul 2025.

import os
from hydra.core.config_store import ConfigStore
from custom.experiment.template import template_any4d_2b, S3_PRETRAINED_ROOT, S3_OUTPUT_ROOT

#################################################################

job = dict(  # this becomes JobConfig
    project='a4d2',
    group='debug',
    name='wm3',
    prepend_datetime=True,  # becomes {now:%m-%d-%H-%M}_{config.job.name}
    local_root='',  # empty = _DEFAULT_LOCAL_ROOT (/any4d on DGX, /tmp/a4d2 on SM)
    s3_root=S3_OUTPUT_ROOT,  # default from template.py; set '' to disable S3 sync
)

wandb = dict(
    enabled=True,
    entity='tri',
    project='a4d2',
    num_validation_logs=5,
)

any4d_config = dict(  # this is part of Any4DConfig, which inherits from Predict2Video2WorldModelConfig
    transforms='default',  # NOTE(bvh): reference_camera now separate per dataset
    dataloader='vidar_flex',  # was 'basile' (deleted in 68f6120a refactor); vidar_flex is the current flex dataloader for VidarDataset
    data_library='vidar',  # required so a4d_model picks dl_module.vidar_to_any4d (not anydata_to_any4d)
    vae='a4d_vae',
    video_entries=[
        # first entry = base / pretrained
        dict(
            # key: (channel_start, channel_end, in_proj_init, out_proj_init)
            # NOTE: there will be T*H*W tokens with this information
            rgb0=(0, 16, 'load', 'load'),  # do not change
            rgb0_input_mask=(16, 17, 'load', None),  # do not change
            rgb0_output_mask=(17, 18, 'load', None),  # do not change
            # NOTE(bvh): remove cams0 relative to AnyView
        ),
        # later entries = new viewpoints
        dict(
            # key: (channel_start, channel_end, in_proj_init, out_proj_init)
            rgb1=(0, 16, 'copy:rgb0/load', 'copy:rgb0/load'),
            rgb1_input_mask=(16, 17, 'copy:rgb0_input_mask/load', None),
            rgb1_output_mask=(17, 18, 'copy:rgb0_output_mask/load', None),
            # NOTE(bvh): remove cams1 relative to AnyView
        ),
        dict(
            rgb2=(0, 16, 'copy:rgb0/load', 'copy:rgb0/load'),
            rgb2_input_mask=(16, 17, 'copy:rgb0_input_mask/load', None),
            rgb2_output_mask=(17, 18, 'copy:rgb0_output_mask/load', None),
        ),
    ],
    lowdim_adaln_entries=dict(
        # key: (seq_start, seq_end, channel_start, channel_end, init)
        # NOTE: each token here exists only once, and they get flattened into one embedding
        traj=(0, 41, 0, 2, 'zero/load'),
        traj_input_mask=(0, 41, 2, 3, 'zero/load'),
    ),
    num_views=3,
    video_concat_mode='view',
    video_proj_mode='per_view',
    view_timestep_mode='per_view',
    block_gate_fix=True,  # should be enabled in every new training run
    loss_weights=dict(
        rgb0=1.0,
        rgb1=1.0,
        rgb2=1.0,
    ),
    harmonize_streams=True,
    harmonize_frames=True,
    load_modals=['rgb', 'traj'],  # NOTE(bvh): cams is needed in vidar to plot actions
    task_probs=dict(
        cross_modal=0.0,
        dyn_view_synth=0.0,
        forecast=1.0,
        pose_estimation=0.0,
        inverse_dynamics=0.0,
        policy=0.0,
        world_model=0.9,  # NOTE(bvh): leave 10% chance unconditional forecasting
    ),
    use_views='all',
    shuffle_cams2views=False,  # NOTE(bvh): important to have views become consistent & semantically meaningful!
    normalizer_stats='',  # TODO: revisit this
    relative_actions=False,  # TODO: revisit this
    action_multiplier=2.0,  # NOTE(bvh): amplify values to avoid drowning in noise
    train_directives=dict(
    ),
    val_directives=dict(
        tasks='forecast,world_model',
        remove_cond=True,
        # perturb_traj='basile1',  # for temporary experimentation
        # perturb_traj='basile2',
        perturb_traj='basile3',
    ),
    data_train_overrides=dict(frame_stride=1),  # cannot deviate from 41 traj tokens for now
    data_val_overrides=dict(frame_stride=1),
    track_metrics=dict(
        rgb0=['psnr', 'ssim'],
        rgb1=['psnr', 'ssim'],
    ),
    train_visuals_interval=99,
    train_visuals_detail=1,
    val_visuals_detail=1,
    visuals_quality=8,
    viz_input_blacklist=[],
    viz_mask_border_width=2,  # NOTE(bvh): useful for temporally varying stuff such as forecasting
    val_num_steps=35,
)

#################################################################

S3_PRETRAINED_PREFIX = r's3://tri-ml-sandbox-16011-us-west-2-datasets/cosmos-predict-2/checkpoints'

model = dict(  # this makes up parts of Predict2Video2WorldModelConfig and Predict2ModelManagerConfig
    # dit_path=f'{S3_PRETRAINED_PREFIX}/nvidia/Cosmos-Predict2-2B-Video2World/model-480p-10fps.pt',
    # dit_path=f's3://tri-ml-sandbox-16011-us-west-2-datasets/sagemaker/cosmos-predict2/a4d2/debug/09-03-21-11_s14b_wm2_lbm/model/iter_000006000_003072000.pt',
    # NOTE(bvh): resume from AnyView HR instead of previous WM:
    # dit_path='s3://tri-ml-sandbox-16011-us-west-2-datasets/sagemaker/cosmos-predict2/a4d2/cvpr/10-31-19-46_officialHR2v/model/iter_000008700_001670400.pt',
    # resume from rwm3_drive:
    # dit_path='s3://tri-ml-sandbox-16011-us-west-2-datasets/sagemaker/cosmos-predict2/a4d2/debug/11-26-04-59_s27_rwm3_drive/model/iter_000020000_002560000.pt',
    # dit_path='s3://tri-ml-sandbox-16011-us-west-2-datasets/sagemaker/cosmos-predict2/a4d2/debug/12-02-17-11_s27c_rwm3_drive/model/iter_000005000_000640000.pt',
    # dit_path='s3://tri-ml-sandbox-16011-us-west-2-datasets/sagemaker/cosmos-predict2/a4d2/debug/12-03-23-59_s27d_rwm3_drive/model/iter_000037000_009472000.pt',
  # dit_path='s3://tri-ml-sandbox-16011-us-west-2-datasets/sagemaker/cosmos-predict2/a4d2/debug/12-11-01-56_s27e_rwm3_drive40h/model/iter_000004000_001024000.pt',
    dit_path='s3://tri-ml-sandbox-16011-us-west-2-datasets/sagemaker/cosmos-predict2/a4d2/debug/12-11-01-56_s27e_rwm3_drive40h/model/iter_000049000_012544000.pt',
    # text_encoder_path=f'{S3_PRETRAINED_PREFIX}/google-t5/t5-11b',
    text_encoder_path='',  # disable
    vae_path=f'{S3_PRETRAINED_PREFIX}/nvidia/Cosmos-Predict2-2B-Video2World/tokenizer/tokenizer.pth',
    fsdp_shard_size=8,
    # sac_mode='none',  # default is predict2_2b_720 but that (and even mm_only) breaks with extra sattn
    ############
    run_validation=True,
    skip_first_validation='delay',  # to check VRAM
    validation_iter=500,  # frequency / interval of validation runs
    # max_val_iter=8,  # number of steps per validation run
    # max_iter=50000,  # total number of training steps
    max_iter=80000,  # total number of training steps
    # grad_accum_iter=2,
    grad_accum_iter=1,
    context_parallel_size=1,
    device_monitor=0,
    manual_gc_iter=288,
    manual_gc_warm_up=-1,  # never disable automatic GC to be safe (weird VRAM issue)
    ############
    state_t=11,  # for noise level; = latent # frames for now
    tokenizer_chunk_duration=41,  # for VAE; = raw # frames for now
)

checkpoint = dict(
    save_iter=1000,
    s3_folder='s3://tri-ml-sandbox-16011-us-west-2-datasets/sagemaker/cosmos-predict2/',
    early_sanity_check=5,
)

optimizer = dict(
    lr=3.0e-5,
)

scheduler = dict(
    warm_up_steps = [500],
    cycle_lengths = [model['max_iter']],
    f_start=[0.01],
    f_max=[1.0],
    f_min=[0.05],
)

dataset_train = dict(
    # config='custom/config/train/lbm_ikeal2_40.yaml',
    # config='custom/config/train/multi/drive_ike3_40_8g.yaml',
    config='custom/config/train/multi/drive_ike3_40h_16g.yaml',
    num_workers=4,  # per GPU
    batch_size=4,  # per GPU
)

dataset_val = dict(
    config=dict(
        PD4DEgoQL='custom/config/val/pd4d_ego_ike3_40h.yaml',  # 64
        PD4DAllQL='custom/config/val/pd4d_all_ike3_40h.yaml',  # 64
        PDv2QL='custom/config/val/pdv2_ike3_40h.yaml',  # 64
        ArgoVQL='custom/config/val/argo_ike3_40h.yaml',  # 64
        DDADQL='custom/config/val/ddad_ike3_40h.yaml',  # 64
        LyftQL='custom/config/val/lyft_ike3_40h.yaml',  # 64
        WaymoQL='custom/config/val/waymo_ike3_40h.yaml',  # 64
    ),
    num_workers=0,  # can crash debugger otherwise
    batch_size=1,
)

metrics = None  # dict()  # disable because fragile



#################################################################

cs = ConfigStore.instance()

this_config = template_any4d_2b(
    job, wandb, any4d_config, model,
    checkpoint, optimizer, scheduler,
    metrics, dataset_train, dataset_val)

# Use the filename (without extension) as the experiment name
experiment_name = 'any4d_' + os.path.splitext(os.path.basename(__file__))[0]

cs.store(
    group='experiment',
    package='_global_',
    name=experiment_name,
    node=this_config,
)


