# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from hydra.core.config_store import ConfigStore
from megatron.core import parallel_state
from torch.utils.data import DataLoader, DistributedSampler

from cosmos_predict2.data.dataset_video import WanDatasetCFGMask
from imaginaire.lazy_config import LazyCall as L
from cosmos_predict2.models.wan_warped_modeli2v import WanWarpedModel, WanWarpedConfig


def get_sampler(dataset) -> DistributedSampler:
    return DistributedSampler(
        dataset,
        num_replicas=parallel_state.get_data_parallel_world_size(),
        rank=parallel_state.get_data_parallel_rank(),
        shuffle=True,
        seed=0,
    )


cs = ConfigStore.instance()

video_dataset_train = L(WanDatasetCFGMask)(
    dataset_dir="/home/datasets/0910retry_aloha_3",
    num_frames=61,
    video_size=(736, 640),
    target_fps=10,
    uncond_rate=0.1
)



dataloader_train = L(DataLoader)(
    dataset=video_dataset_train,
    sampler=L(get_sampler)(dataset=video_dataset_train),
    batch_size=2,
    drop_last=True,
    num_workers=4,
    pin_memory=True,
)

# torchrun --nproc_per_node=8 --master_port=12341 -m utils.train --config=cosmos_predict2/configs/base/config.py -- experiment=i2v_5b_real0910
i2v_5b_real0910 = dict(
    defaults=[
        {"override /model": "wan_ddp_5b"},
        {"override /optimizer": "adamw"},
        {"override /scheduler": "lambdalinear"},
        {"override /ckpt_type": "standard"},
        {"override /data_val": "mock"},
        "_self_",
    ],
    job=dict(
        project="posttraining",
        group="wan",
        name="i2v_5b_real0910",
    ),
    model_parallel=dict(
        context_parallel_size=1,
    ),
    dataloader_train=dataloader_train,
    dataloader_val=None,
    trainer=dict(
        distributed_parallelism="ddp",
        callbacks=dict(
            iter_speed=dict(hit_thres=10, every_n=1),
        ),
        grad_accum_iter=1,
        max_iter=16000,
    ),
    model=L(WanWarpedModel)(
        config=L(WanWarpedConfig)(
            dit_pretrain_path="Wan2.2/Wan2.2-TI2V-5B",
            dit_pt_path='checkpoints/posttraining/wan/i2v_5b_pretrainv4/checkpoints/model/iter_000010000.pt',
            # mask_weight=3,
            loss_scale=1,
            fsdp_shard_size=0,
        )
    ),
    checkpoint=dict(
        save_iter=2000,
    ),
    optimizer=dict(
        lr=2 ** (-15.5),
    ),
    scheduler=dict(
        warm_up_steps=[200],
        cycle_lengths=[400_000],
        f_max=[0.99],
        f_min=[0.4],
    ),
)


for _item in [
    # 2b, agibot_head_center_fisheye_color
    i2v_5b_real0910,
]:
    # Get the experiment name from the global variable.
    experiment_name = [name.lower() for name, value in globals().items() if value is _item][0]

    cs.store(
        group="experiment",
        package="_global_",
        name=experiment_name,
        node=_item,
    )
