# 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 typing import List, Optional, Tuple

import torch

from cosmos_predict2.conditioner import DataType
from cosmos_predict2.models.text2image_dit import MiniTrainDIT


class MinimalV1LVGDiT(MiniTrainDIT):
    def __init__(self, *args, **kwargs):
        assert "in_channels" in kwargs, "in_channels must be provided"
        kwargs["in_channels"] += 1  # Add 1 for the condition mask
        super().__init__(*args, **kwargs)

    def forward(
        self,
        x_B_C_T_H_W: torch.Tensor,
        timesteps_B_T: torch.Tensor,
        crossattn_emb: torch.Tensor,
        condition_video_input_mask_B_C_T_H_W: Optional[torch.Tensor] = None,
        fps: Optional[torch.Tensor] = None,
        padding_mask: Optional[torch.Tensor] = None,
        data_type: Optional[DataType] = DataType.VIDEO,
        **kwargs,
    ) -> torch.Tensor | List[torch.Tensor] | Tuple[torch.Tensor, List[torch.Tensor]]:
        del kwargs
        tmp0 = x_B_C_T_H_W

        # BVH shape notes:
        # x_B_C_T_H_W: tensor[2, 16, 7, 16, 20] n=71680 (0.3Mb) x∈[-3.650, 3.568] μ=-0.005 σ=0.762 cuda:0
        # timesteps_B_T: tensor[2, 7] bf16 n=14 x∈[0.000, 0.949] μ=0.449 σ=0.424 cuda:1
        # crossattn_emb: tensor[2, 512, 1024] bf16 n=1048576 (2Mb) x∈[-0.656, 0.555] μ=3.052e-05 σ=0.018 cuda:1
        # condition_video_input_mask_B_C_T_H_W: tensor[2, 1, 7, 16, 20] bf16 n=4480 (8.8Kb) x∈[0., 1.000] μ=0.428 σ=0.494 cuda:1
        # fps: tensor[2] i64 μ=10.000 σ=0. cuda:1 [10, 10]
        # padding_mask: tensor[2, 1, 128, 160] bf16 n=40960 (80Kb) [38;2;127;127;127mall_zeros[0m cuda:1
        # data_type: <DataType.VIDEO: 'video'>

        if data_type == DataType.VIDEO:  # yes
            x_B_C_T_H_W = torch.cat([x_B_C_T_H_W, condition_video_input_mask_B_C_T_H_W.type_as(x_B_C_T_H_W)], dim=1)
        else:  # no
            B, _, T, H, W = x_B_C_T_H_W.shape
            x_B_C_T_H_W = torch.cat(
                [x_B_C_T_H_W, torch.zeros((B, 1, T, H, W), dtype=x_B_C_T_H_W.dtype, device=x_B_C_T_H_W.device)], dim=1
            )
        
        retval = super().forward(
            x_B_C_T_H_W=x_B_C_T_H_W,
            timesteps_B_T=timesteps_B_T,
            crossattn_emb=crossattn_emb,
            fps=fps,
            padding_mask=padding_mask,
            data_type=data_type,
        )

        # BVH shape notes:
        # x_B_C_T_H_W: tensor[2, 17, 7, 16, 20] bf16 n=76160 (0.1Mb) x∈[-3.656, 3.562] μ=0.020 σ=0.754 cuda:0
        # retval: tensor[2, 16, 7, 16, 20] bf16 n=71680 (0.1Mb) x∈[-4.969, 4.531] μ=-0.038 σ=0.996 grad UnsafeViewBackward0 cuda:0

        return retval


