# 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.

import argparse
import os
import pickle
import sys
from tqdm import tqdm
import torch
import numpy as np

from wan.modules.t5 import T5EncoderModel


"""example command
python -m scripts.get_t5_embeddings --dataset_path datasets/hdvila
"""


def parse_args() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(description="Compute T5 embeddings for text prompts")
    parser.add_argument("--dataset_path", type=str, default="datasets/hdvila", help="Root path to the dataset")
    parser.add_argument("--max_length", type=int, default=512, help="Maximum length of the text embedding")
    parser.add_argument(
        "--cache_dir", type=str, default="Wan2.2-TI2V-5B", help="Directory to cache the T5 model"
    )
    parser.add_argument("--shards", type=int, default=1, help="Number of shards to split the dataset into")
    parser.add_argument("--current_shard", type=int, default=0, help="Current shard index to process")
    return parser.parse_args()


def main(args) -> None:
    metas_dir = os.path.join(args.dataset_path, "metas")
    metas_list = [
        os.path.join(metas_dir, filename) for filename in sorted(os.listdir(metas_dir)) if filename.endswith(".txt")
    ]
    metas_list = metas_list[args.current_shard::args.shards]

    t5_xxl_dir = os.path.join(args.dataset_path, "umt5_wan")
    os.makedirs(t5_xxl_dir, exist_ok=True)

    # Initialize T5
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    encoder = T5EncoderModel(
                text_len=args.max_length,
                dtype=torch.bfloat16,
                device=device,
                checkpoint_path=os.path.join(args.cache_dir, 'models_t5_umt5-xxl-enc-bf16.pth'),
                tokenizer_path=os.path.join(args.cache_dir, 'google/umt5-xxl'),
            )

    for meta_filename in tqdm(metas_list):
        t5_xxl_filename = os.path.join(t5_xxl_dir, os.path.basename(meta_filename).replace(".txt", ".pt"))
        if os.path.exists(t5_xxl_filename):
            # Skip if the file already exists
            continue

        with open(meta_filename, "r") as fp:
            prompt = fp.read().strip()

        # Compute T5 embeddings
        encoded_text = encoder([prompt], device)  # list of np.ndarray in (len, 1024)

        encoded_text = encoded_text[0].cpu()
        torch.save(encoded_text, t5_xxl_filename)
    print("done.")


if __name__ == "__main__":
    args = parse_args()
    main(args)
