import os
import json
import random
import argparse
import os

os.environ["HF_ENDPOINT"] = "https://hf-mirror.com"

import numpy as np
from tqdm import tqdm

import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

datasets = [
    "narrativeqa",
    "qasper",
    "multifieldqa_en",
    "hotpotqa",
    "2wikimqa",
    "musique",
    "gov_report",
    "qmsum",
    "multi_news",
    "trec",
    "triviaqa",
    "samsum",
    "passage_count",
    "passage_retrieval_en",
    "lcc",
    "repobench-p",
]

dataset2maxlen = {
    "narrativeqa": 128,
    "qasper": 128,
    "multifieldqa_en": 64,
    "multifieldqa_zh": 64,
    "hotpotqa": 32,
    "2wikimqa": 32,
    "musique": 32,
    "dureader": 128,
    "gov_report": 512,
    "qmsum": 512,
    "multi_news": 512,
    "vcsum": 512,
    "trec": 64,
    "triviaqa": 32,
    "samsum": 128,
    "lsht": 64,
    "passage_count": 32,
    "passage_retrieval_en": 32,
    "passage_retrieval_zh": 32,
    "lcc": 64,
    "repobench-p": 64,
}

model2prompt = {
    "narrativeqa": "You are given a story, which can be either a novel or a movie script, and a question. Answer the question asconcisely as you can, using a single phrase if possible. Do not provide any explanation.\n\nStory: {context}\n\nNow, answer the question based on the story asconcisely as you can, using a single phrase if possible. Do not provide any explanation.\n\nQuestion: {input}\n\nAnswer:",
    "qasper": 'You are given a scientific article and a question. Answer the question as concisely as you can, using a single phrase or sentence if possible. If the question cannot be answered based on the information in the article, write "unanswerable". If the question is a yes/no question, answer "yes", "no", or "unanswerable". Do not provide any explanation.\n\nArticle: {context}\n\n Answer the question based on the above article as concisely as you can, using a single phrase or sentence if possible. If the question cannot be answered based on the information in the article, write "unanswerable". If the question is a yes/no question, answer "yes", "no", or "unanswerable". Do not provide any explanation.\n\nQuestion: {input}\n\nAnswer:',
    "multifieldqa_en": "Read the following text and answer briefly.\n\n{context}\n\nNow, answer the following question based on the above text, only give me the answer and do not output any other words.\n\nQuestion: {input}\nAnswer:",
    "multifieldqa_zh": "阅读以下文字并用中文简短回答：\n\n{context}\n\n现在请基于上面的文章回答下面的问题，只告诉我答案，不要输出任何其他字词。\n\n问题：{input}\n回答：",
    "hotpotqa": "Answer the question based on the given passages. Only give me the answer and do not output any other words.\n\nThe following are given passages.\n{context}\n\nAnswer the question based on the given passages. Only give me the answer and do not output any other words.\n\nQuestion: {input}\nAnswer:",
    "2wikimqa": "Answer the question based on the given passages. Only give me the answer and do not output any other words.\n\nThe following are given passages.\n{context}\n\nAnswer the question based on the given passages. Only give me the answer and do not output any other words.\n\nQuestion: {input}\nAnswer:",
    "musique": "Answer the question based on the given passages. Only give me the answer and do not output any other words.\n\nThe following are given passages.\n{context}\n\nAnswer the question based on the given passages. Only give me the answer and do not output any other words.\n\nQuestion: {input}\nAnswer:",
    "dureader": "请基于给定的文章回答下述问题。\n\n文章：{context}\n\n请基于上述文章回答下面的问题。\n\n问题：{input}\n回答：",
    "gov_report": "You are given a report by a government agency. Write a one-page summary of the report.\n\nReport:\n{context}\n\nNow, write a one-page summary of the report.\n\nSummary:",
    "qmsum": "You are given a meeting transcript and a query containing a question or instruction. Answer the query in one or more sentences.\n\nTranscript:\n{context}\n\nNow, answer the query based on the above meeting transcript in one or more sentences.\n\nQuery: {input}\nAnswer:",
    "multi_news": "You are given several news passages. Write a one-page summary of all news. \n\nNews:\n{context}\n\nNow, write a one-page summary of all the news.\n\nSummary:",
    "vcsum": "下面有一段会议记录，请你阅读后，写一段总结，总结会议的内容。\n会议记录：\n{context}\n\n会议总结：",
    "trec": "Please determine the type of the question below. Here are some examples of questions.\n\n{context}\n{input}",
    "triviaqa": "Answer the question based on the given passage. Only give me the answer and do not output any other words. The following are some examples.\n\n{context}\n\n{input}",
    "samsum": "Summarize the dialogue into a few short sentences. The following are some examples.\n\n{context}\n\n{input}",
    "lsht": "请判断给定新闻的类别，下面是一些例子。\n\n{context}\n{input}",
    "passage_count": "There are some paragraphs below sourced from Wikipedia. Some of them may be duplicates. Please carefully read these paragraphs and determine how many unique paragraphs there are after removing duplicates. In other words, how many non-repeating paragraphs are there in total?\n\n{context}\n\nPlease enter the final count of unique paragraphs after removing duplicates. The output format should only contain the number, such as 1, 2, 3, and so on.\n\nThe final answer is: ",
    "passage_retrieval_en": 'Here are 30 paragraphs from Wikipedia, along with an abstract. Please determine which paragraph the abstract is from.\n\n{context}\n\nThe following is an abstract.\n\n{input}\n\nPlease enter the number of the paragraph that the abstract is from. The answer format must be like "Paragraph 1", "Paragraph 2", etc.\n\nThe answer is: ',
    "passage_retrieval_zh": '以下是若干段落文字，以及其中一个段落的摘要。请确定给定的摘要出自哪一段。\n\n{context}\n\n下面是一个摘要\n\n{input}\n\n请输入摘要所属段落的编号。答案格式必须是"段落1"，"段落2"等格式\n\n答案是：',
    "lcc": "Please complete the code given below. \n{context}Next line of code:\n",
    "repobench-p": "Please complete the code given below. \n{context}{input}Next line of code:\n",
}

model2maxlen = {
    "llama2": 3950,
    "llama-2": 3950,
    "llama3": 7950,
    "llama-3-70": 7950,
    "llama-3.1": 127000,
    "mistral": 31500,
    "qwen2.5": 127000,
    "gemma": 7950,
}


def set_seed(seed):
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    np.random.seed(seed)
    random.seed(seed)
    torch.backends.cudnn.benchmark = False
    torch.backends.cudnn.deterministic = True
    torch.cuda.manual_seed_all(seed)


def build_chat(prompt):
    prompt = f"[INST] {prompt} [/INST]"
    return prompt


def main(args):
    print("Loading data...")

    test_data = []

    prompts = []
    inputs = []
    contexts = []
    answerss = []
    lengths = []
    datasets = []
    languages = []
    all_classess = []
    _ids = []

    input_max_len = 0

    model_path = args.model_path.lower()

    for key in model2maxlen:
        if key in model_path:
            model_max_len = model2maxlen[key]

    output_max_len = dataset2maxlen[args.dataset]

    with open(args.data_file) as fp:
        for line in fp:
            example = json.loads(line)

            length = example["length"]
            if length > input_max_len:
                input_max_len = length

            template = model2prompt[args.dataset]
            prompt = template.format(**example)

            if "llama2" in args.model_path.lower():
                prompt = build_chat(prompt)

            example["prompt"] = prompt

            test_data.append(example)

    print(f"Max Length is {input_max_len}")

    if args.max_num_examples and len(test_data) > args.max_num_examples:
        if args.sample_method == "random":
            test_data = random.sample(test_data, args.max_num_examples)
        elif args.sample_method == "topk":
            test_data = test_data[: args.max_num_examples]

    for example in test_data:
        prompts.append(example["prompt"])
        inputs.append(example["input"])
        contexts.append(example["context"])
        answerss.append(example["answers"])
        lengths.append(example["length"])
        datasets.append(example["dataset"])
        languages.append(example["language"])
        all_classess.append(example["all_classes"])
        _ids.append(example["_id"])

    print("Finish loading model and tokenizer")
    model_name = model_path.split("/")[-1]

    os.makedirs(
        os.path.join(
            args.save_dir, f"{model_name}_{args.max_capacity_prompts}", args.dataset
        ),
        exist_ok=True,
    )

    fout = open(
        os.path.join(
            args.save_dir,
            f"{model_name}_{args.max_capacity_prompts}",
            args.dataset,
            f"{args.method}_{args.add_file_name}.json",
        ),
        "w",
    )

    for i in tqdm(range(0, len(prompts), args.eval_batch_size)):
        batch_prompts = prompts[i : i + args.eval_batch_size]
        batch_inputs = inputs[i : i + args.eval_batch_size]
        batch_contexts = contexts[i : i + args.eval_batch_size]
        batch_answerss = answerss[i : i + args.eval_batch_size]
        batch_lengths = lengths[i : i + args.eval_batch_size]

        batch_datasets = datasets[i : i + args.eval_batch_size]
        batch_languages = languages[i : i + args.eval_batch_size]
        batch_all_classess = all_classess[i : i + args.eval_batch_size]
        batch__ids = _ids[i : i + args.eval_batch_size]

        tokenized_prompts = tokenizer(
            batch_prompts, padding="longest", return_tensors="pt", add_special_tokens=True
        ).to("cuda")
        batch_input_ids = tokenized_prompts.input_ids
        attention_mask = tokenized_prompts.attention_mask

        if len(batch_input_ids[0]) > model_max_len:
            half = int(model_max_len / 2)
            prompt = tokenizer.decode(
                batch_input_ids[0][:half], skip_special_tokens=True
            ) + tokenizer.decode(batch_input_ids[0][-half:], skip_special_tokens=True)

            tokenized_prompts = tokenizer(
                prompt, padding="longest", return_tensors="pt", add_special_tokens=True
            ).to("cuda")
            batch_input_ids = tokenized_prompts.input_ids
            attention_mask = tokenized_prompts.attention_mask

        if args.max_capacity_prompts != -1:
            max_capacity_prompts = args.max_capacity_prompts
        elif args.max_capacity_prompts_ratio != -1:
            max_capacity_prompts = round(
                batch_input_ids.shape[1] * args.max_capacity_prompts_ratio
            )

        if args.method != "FullKV":
            if args.method.lower() in ["restkv"]:
                window_sizes = args.window_sizes

            kernel_sizes = args.kernel_sizes
            pooling = args.pooling
            ratio = args.pruning_ratio
            recent_size = args.recent_size

            layers = len(model.model.layers)
            # check if window_sizes is a list
            if not isinstance(window_sizes, list):
                window_sizes = [window_sizes] * layers
            if not isinstance(max_capacity_prompts, list):
                max_capacity_prompts = [max_capacity_prompts] * layers
            if not isinstance(kernel_sizes, list):
                kernel_sizes = [kernel_sizes] * layers
            if not isinstance(ratio, list):
                ratio = [ratio] * layers
            if not isinstance(recent_size, list):
                recent_size = [recent_size] * layers
            for i in range(layers):
                model.model.layers[i].self_attn.config.window_size = window_sizes[i]
                model.model.layers[
                    i
                ].self_attn.config.max_capacity_prompt = max_capacity_prompts[i]
                model.model.layers[i].self_attn.config.kernel_size = kernel_sizes[i]
                model.model.layers[i].self_attn.config.pooling = pooling
                model.model.layers[i].self_attn.config.merge = args.merge
                model.model.layers[i].self_attn.config.floor = args.floor
                model.model.layers[i].self_attn.config.ratio = ratio[i]
                model.model.layers[i].self_attn.config.recent_size = recent_size[i]
                if args.method.lower() == "restkv":
                    model.model.layers[i].self_attn.config.use_wo = args.use_wo
                    model.model.layers[i].self_attn.config.use_ema = args.use_ema
                    model.model.layers[i].self_attn.config.use_pyramid = args.use_pyramid
                    model.model.layers[i].self_attn.config.alpha = args.alpha

        context_length = batch_input_ids.shape[-1]

        if args.quant_method == None:
            output = model.generate(
                **tokenized_prompts,
                output_attentions=args.output_attentions,
                max_new_tokens=output_max_len,
                num_beams=1,
                do_sample=False,
                temperature=1.0,
                min_length=context_length + 1,
                eos_token_id=[tokenizer.eos_token_id],
                pad_token_id=tokenizer.pad_token_id,
            )
        else:
            output = model.generate(
                **tokenized_prompts,
                output_attentions=args.output_attentions,
                max_new_tokens=output_max_len,
                num_beams=1,
                do_sample=False,
                temperature=1.0,
                min_length=context_length + 1,
                eos_token_id=[tokenizer.eos_token_id],
                cache_implementation="quantized",
                cache_config={
                    "nbits": args.nbits,
                    "backend": "HQQ",
                    "device": "cuda",
                    "residual_length": output_max_len,
                    "axis_key": 1,
                    "q_group_size": 64,
                },
            )

        batch_outputs = tokenizer.batch_decode(
            [output[0][context_length:]], skip_special_tokens=True
        )

        batch_generations = batch_outputs

        torch.cuda.empty_cache()

        for j in range(args.eval_batch_size):
            example = {}

            example["prompt"] = batch_prompts[j]
            example["input"] = batch_inputs[j]
            example["context"] = batch_contexts[j]
            example["answers"] = batch_answerss[j]
            example["pred"] = batch_generations[j]
            example["length"] = batch_lengths[j]

            example["dataset"] = batch_datasets[j]
            example["language"] = batch_languages[j]
            example["all_classes"] = batch_all_classess[j]
            example["_id"] = batch__ids[j]

            fout.write(json.dumps(example) + "\n")


if __name__ == "__main__":
    parser = argparse.ArgumentParser()

    parser.add_argument("--seed", type=int, default=42, help="")
    parser.add_argument("--base_dir", type=str, default="")
    parser.add_argument("--dataset", type=str, default="")
    parser.add_argument("--data_file", type=str, default="")
    parser.add_argument("--save_dir", type=str, default="")
    parser.add_argument("--add_file_name", type=str, default="")

    parser.add_argument(
        "--model_name",
        type=str,
        default=None,
        help="if specified, we will load the model to generate the predictions.",
    )
    parser.add_argument(
        "--model_path",
        type=str,
        default=None,
        help="if specified, we will load the model to generate the predictions.",
    )
    parser.add_argument("--use_fast_tokenizer", type=bool, default=True, help="")
    parser.add_argument("--output_attentions", type=bool, default=False, help="")

    parser.add_argument(
        "--max_num_examples",
        type=int,
        default=None,
        help="maximum number of examples to evaluate per task.",
    )
    parser.add_argument(
        "--sample_method",
        type=str,
        default="topk",
        choices=["random", "topk"],
        help="how to sample the examples.",
    )

    parser.add_argument("--max_new_tokens", type=int, default=None, help="")

    parser.add_argument(
        "--eval_batch_size", type=int, default=1, help="batch size for evaluation."
    )
    parser.add_argument("--window_sizes", type=int, default=32, help="")

    # Parameters for restkv
    parser.add_argument("--use_wo", action="store_true", help="Whether to use Wo")
    parser.add_argument("--use_ema", action="store_true", help="Whether to use EMA")
    parser.add_argument(
        "--use_pyramid", action="store_true", help="Whether to use Pyramid"
    )
    parser.add_argument(
        "--alpha", type=float, default=0.3, help="hyper-parameter used in EMA"
    )
    parser.add_argument("--kernel_sizes", type=int, default=5, help="")
    parser.add_argument(
        "--pooling",
        type=str,
        default="avgpool",
        choices=["maxpool", "avgpool", "adaptive"],
    )

    # Parameters for Ablation Study
    parser.add_argument(
        "--ss",
        type=str,
        default="none",
        choices=["none", "avgpool", "maxpool", "adaptive"],
    )
    parser.add_argument(
        "--ts", type=str, default="none", choices=["none", "mean", "inv-ema", "ema"]
    )
    parser.add_argument(
        "--indicator",
        type=str,
        default="random",
        choices=["random", "attn", "attn-v", "reconstruction"],
    )

    parser.add_argument("--use_cache", type=bool, default=True, help="")
    parser.add_argument(
        "--attn_implementation",
        type=str,
        default="flash_attention_2",
        choices=["flash_attention_2", "sdpa", "eager"],
    )
    parser.add_argument("--method", type=str, default=None)
    parser.add_argument(
        "--quant_method", type=str, default=None, choices=["kivi", "kvquant"]
    )
    parser.add_argument("--nbits", type=int, default=8, help="")
    parser.add_argument("--max_capacity_prompts", type=int, default=512, help="")
    parser.add_argument("--max_capacity_prompts_ratio", type=float, default=-1, help="")
    parser.add_argument(
        "--steps",
        type=int,
        default=-1,
        help="maximum number of examples to evaluate per task.",
    )
    parser.add_argument("--merge", type=str, default=None, help="kv merge method(look-m)")
    parser.add_argument(
        "--floor", type=float, default=0.2, help="hyper-parameter used in AdaKV"
    )
    parser.add_argument(
        "--head_path",
        type=str,
        default="./data/heads_score/Meta-Llama-3-8B-Instruct_retrieval_reasoning_heads.json",
        help="Path to head score (HeadKV)",
    )
    parser.add_argument(
        "--head_beta", type=float, default=1.01, help="hyper-parameter used on HeadKV"
    )
    parser.add_argument("--recent_size", type=int, default=32, help="")
    parser.add_argument(
        "--pruning_ratio", type=float, default=0.4, help="pruning ratio of Key Cache"
    )

    parser.add_argument(
        "--use_chat_format",
        action="store_true",
        help="If given, we will use the chat format for the prompts.",
    )
    parser.add_argument(
        "--chat_formatting_function",
        type=str,
        default="eval.templates.create_prompt_with_tulu_chat_format",
        help="The function to use to create the chat format. This function will be dynamically imported. Please see examples in `eval/templates.py`.",
    )

    args = parser.parse_args()

    set_seed(args.seed)
    if args.quant_method == "kvquant":
        from restkv.quantcache import KVQuantizedCache
        from transformers import cache_utils

        cache_utils.HQQQuantizedCache = KVQuantizedCache
    tokenizer = AutoTokenizer.from_pretrained(
        args.model_path, use_fast=args.use_fast_tokenizer, padding_side="left"
    )

    from restkv.monkeypatch import (
        replace_llama,
        replace_mistral,
        replace_qwen,
        replace_gemma,
    )

    if "llama" in args.model_path.lower():
        replace_llama(args.method.lower())
    elif "mistral" in args.model_path.lower():
        replace_mistral(args.method.lower())
    elif "qwen" in args.model_path.lower():
        replace_qwen(args.method.lower())
    elif "gemma" in args.model_path.lower():
        replace_gemma(args.method.lower())

    model = AutoModelForCausalLM.from_pretrained(
        args.model_path,
        torch_dtype=torch.float16,
        low_cpu_mem_usage=True,
        device_map="auto",
        use_cache=args.use_cache,
        attn_implementation=args.attn_implementation,
    )

    tokenizer.padding_side = "left"
    if tokenizer.pad_token is None:
        tokenizer.pad_token = tokenizer.eos_token
        tokenizer.pad_token_id = tokenizer.eos_token_id

    model.eval()

    save_dir = args.save_dir

    max_capacity_prompts = args.max_capacity_prompts

    for idx, dataset in enumerate(datasets):
        print(
            f"Working on max_capacity_prompts {args.max_capacity_prompts} dataset {dataset} - {idx}/{len(datasets)}"
        )

        args.dataset = dataset

        args.data_file = f"data/LongBench/{args.dataset}.jsonl"

        main(args)
