# Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved
#
# 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 pathlib

import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

from tico.quantization import convert, prepare
from tico.quantization.config.ptq import PTQConfig
from tico.quantization.evaluation.metric import compute_peir
from tico.quantization.evaluation.utils import plot_two_outputs
from tico.quantization.wrapq.mode import Mode
from tico.quantization.wrapq.wrappers.llama.quant_attn_prefill import (
    QuantLlamaAttentionPrefill,
)
from tico.utils.utils import SuppressWarning

name = "Maykeye/TinyLLama-v0"
MAX_SEQ = 256
model = AutoModelForCausalLM.from_pretrained(name, dtype=torch.float32)
tokenizer = AutoTokenizer.from_pretrained(name)

# Make sure pad token exists (Llama often uses eos as pad)
if tokenizer.pad_token_id is None:
    tokenizer.pad_token = tokenizer.eos_token

model.config.max_position_embeddings = MAX_SEQ

# -------------------------------------------------------------------------
# 1. Replace layer-0’s MLP with QuantLlamaMLP
# -------------------------------------------------------------------------
orig_attn = model.model.layers[0].self_attn
model.model.layers[0].self_attn = prepare(
    orig_attn, PTQConfig(wrapper_variant="prefill")
)
model.eval()

attn_q = model.model.layers[0].self_attn  # quant wrapper
assert isinstance(attn_q.wrapped, QuantLlamaAttentionPrefill)
rotary = model.model.rotary_emb

# -------------------------------------------------------------------------
# Helpers: tokenize + embed
# -------------------------------------------------------------------------
def make_inputs(prompt: str):
    batch = tokenizer(
        prompt,
        return_tensors="pt",
        padding="max_length",
        truncation=True,
        max_length=MAX_SEQ,
    )
    input_ids = batch["input_ids"]

    hidden = model.model.embed_tokens(input_ids)
    return hidden


# -------------------------------------------------------------------------
# 2. Single-pass calibration
# -------------------------------------------------------------------------
PROMPTS = [
    "The quick brown fox jumps over the lazy dog.",
    "In 2025, AI systems accelerated hardware-software co-design at scale.",
    "양자화는 왜 어려울까? 분포, 길이, 마스크가 관건이다.",
    "今日はいい天気ですね。ところでRoPE角度は長さに依存します。",
    "def quicksort(arr):\n    if len(arr) <= 1: return arr\n    ...",
    "Prices rose 3.14% — see Figure 2; emails: foo@bar.com!",
]

with torch.no_grad():
    for prompt in PROMPTS:
        embeds = make_inputs(prompt)
        position_ids = torch.arange(embeds.size(1)).unsqueeze(0)
        pos = rotary(embeds, position_ids)
        _ = attn_q(embeds, pos)  # observers collect

convert(attn_q)

assert attn_q._mode is Mode.QUANT, "Quantization mode should be active now."

# -------------------------------------------------------------------------
# 3. Quick diff check (INT-sim vs FP32)
# -------------------------------------------------------------------------
emb = make_inputs("check")
position_ids = torch.arange(emb.size(1)).unsqueeze(0)
pos = rotary(emb, position_ids)
with torch.no_grad():
    int8 = attn_q(emb, pos)[0]
    fp32 = orig_attn(emb, position_embeddings=pos, attention_mask=None)[0]

print("┌───────────── Quantization Error Summary ─────────────")
print(f"│ Mean |diff|: {(int8 - fp32).abs().mean().item():.6f}")
print(f"│ PEIR       : {compute_peir(fp32, int8) * 100:.6f} %")
print("└──────────────────────────────────────────────────────")
print(plot_two_outputs(fp32, int8))

# -------------------------------------------------------------------------
# 4. Export the quantized block
# -------------------------------------------------------------------------
import tico

save_path = pathlib.Path("attn_prefill.q.circle")
B, S, D = 1, MAX_SEQ, model.config.hidden_size
example = torch.randn(B, S, D)
example_pos = rotary(example, torch.arange(S)[None, :])

with SuppressWarning(UserWarning, ".*"):
    cm = tico.convert(attn_q, (example, example_pos))
cm.save(save_path)

print(f"Quantized Circle model saved to {save_path.resolve()}")
