"""NExT-GPT image-to-text subset, staged ImageBind and GPTQ Vicuna v0.

Uses official NExT-GPT delta (LoRA, embeddings, input projection), not a
replacement VLM. GPTQ unpacking follows the public GPTQ/AutoGPTQ format:
W = scales[g_idx] * (unpack(qweight) - (unpack(qzeros) + 1)).
No AutoGPTQ CUDA extension or audio/video generators are required.
"""
import argparse
import gc
import hashlib
import json
from pathlib import Path
import sys
import time

import torch
from torch import nn
from PIL import Image
from safetensors import safe_open
from transformers import LlamaConfig, LlamaForCausalLM, LlamaTokenizer
from torchvision import transforms
from accelerate import init_empty_weights
from accelerate.utils import set_module_tensor_to_device

ROOT = Path(__file__).resolve().parents[1]

class GPTQLinear(nn.Module):
    """Portable reference 4-bit GEMM; dequantize one matrix per forward.

    Preserve activation-order grouping through g_idx. LoRA is applied as
    x @ A.T @ B.T with alpha/r=1 (official stage_3.yaml: 32/32).
    """
    def __init__(self, tensors, lora_a=None, lora_b=None):
        super().__init__()
        for name, value in tensors.items():
            self.register_buffer(name, value.to('cuda'))
        self.register_buffer('lora_a', None if lora_a is None else lora_a.half().to('cuda'))
        self.register_buffer('lora_b', None if lora_b is None else lora_b.half().to('cuda'))
        self.register_buffer('shift', torch.arange(0, 32, 4, device='cuda', dtype=torch.int32))
        # qzeros are stored along output columns, qweight along input rows.
        zero = ((self.qzeros.unsqueeze(-1) >> self.shift) & 15).reshape(self.qzeros.shape[0], -1)
        self.register_buffer('zero', (zero + 1).half())

    def forward(self, x):
        weight = ((self.qweight.unsqueeze(1) >> self.shift.view(1, 8, 1)) & 15)
        weight = weight.reshape(-1, self.qweight.shape[-1]).to(x.dtype)
        groups = self.g_idx.long()
        weight.sub_(self.zero[groups]).mul_(self.scales[groups])
        y = torch.matmul(x, weight)
        if self.lora_a is not None:
            y = y + (x @ self.lora_a.T) @ self.lora_b.T
        return y

def sha256(path):
    h = hashlib.sha256()
    with open(path, 'rb') as f:
        for block in iter(lambda: f.read(8 * 1024 * 1024), b''):
            h.update(block)
    return h.hexdigest()

def release():
    gc.collect()
    torch.cuda.empty_cache()

@torch.inference_mode()
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--weights', type=Path, default=ROOT.parent / '.nextgpt_weights')
    parser.add_argument('--image', type=Path, default=ROOT / 'evidence/real-inference/input.jpg')
    parser.add_argument('--prompt', default='What animal is shown in this picture? Answer in one short sentence.')
    parser.add_argument('--max-new-tokens', type=int, default=24)
    args = parser.parse_args()
    if not torch.cuda.is_available():
        raise SystemExit('CUDA is required. Use the isolated CUDA environment.')
    start = time.perf_counter()
    torch.manual_seed(13)
    torch.cuda.reset_peak_memory_stats()
    out = ROOT / 'evidence/real-inference'
    out.mkdir(parents=True, exist_ok=True)
    device = torch.device('cuda')
    weights = args.weights
    delta_path = weights / 'nextgpt_7b_tiva_v0/pytorch_model.pt'
    gptq_path = weights / 'vicuna-v0-gptq/model.safetensors'
    imagebind_path = weights / 'imagebind_huge.pth'
    print('GPU:', torch.cuda.get_device_name(0), flush=True)
    print('Loading official NExT-GPT delta on CPU...', flush=True)
    delta = torch.load(delta_path, map_location='cpu', weights_only=True, mmap=True)

    # Import official ImageBind without importing model/__init__.py, whose
    # DeepSpeed/audio/video dependencies are training/full-demo dependencies.
    sys.path.insert(0, str(ROOT / 'upstream/code/model/ImageBind'))
    from models.imagebind_model import imagebind_huge
    print('Stage 1: official ImageBind image encoder', flush=True)
    encoder, _ = imagebind_huge(pretrained=False)
    state = torch.load(imagebind_path, map_location='cpu', weights_only=True, mmap=True)
    encoder.load_state_dict(state, strict=True)
    del state
    # Retain only the vision modality after exact full-checkpoint validation.
    for name in ['modality_preprocessors', 'modality_trunks', 'modality_heads', 'modality_postprocessors']:
        modules = getattr(encoder, name)
        for key in list(modules.keys()):
            if key != 'vision':
                del modules[key]
    encoder = encoder.eval().half().to(device)
    transform = transforms.Compose([
        transforms.Resize(224, interpolation=transforms.InterpolationMode.BICUBIC),
        transforms.CenterCrop(224), transforms.ToTensor(),
        transforms.Normalize((.48145466, .4578275, .40821073), (.26862954, .26130258, .27577711)),
    ])
    image = transform(Image.open(args.image).convert('RGB')).unsqueeze(0).half().to(device)
    features = encoder({'vision': image})['vision']
    proj = nn.Linear(1024, 4096).half().to(device)
    proj.load_state_dict({k.removeprefix('llama_proj.'): v for k,v in delta.items() if k.startswith('llama_proj.')}, strict=True)
    visual = proj(features).unsqueeze(1).cpu()
    torch.save(visual, out / 'image_embedding.pt')
    encoder_time = time.perf_counter() - start
    del encoder, image, features, proj
    release()
    print('Image encoding complete; encoder unloaded.', flush=True)

    print('Stage 2: GPTQ Vicuna v0 + official NExT-GPT LoRA', flush=True)
    tokenizer = LlamaTokenizer.from_pretrained(weights / 'vicuna-v0-gptq', legacy=True, local_files_only=True)
    tokenizer.pad_token = tokenizer.eos_token
    for token in ['<Img>', '</Img>'] + [f'[IMG{i}]' for i in range(4)] + [f'[VID{i}]' for i in range(24)] + [f'[AUD{i}]' for i in range(8)]:
        tokenizer.add_tokens(token)
    assert len(tokenizer) == delta['llama_model.base_model.model.model.embed_tokens.weight'].shape[0], len(tokenizer)
    config = LlamaConfig.from_pretrained(weights / 'nextgpt_7b_tiva_v0')
    config._attn_implementation = 'eager'
    with init_empty_weights():
        model = LlamaForCausalLM(config)
    loaded = []
    with safe_open(gptq_path, framework='pt', device='cpu') as sf:
        keys = set(sf.keys())
        for name, module in list(model.named_modules()):
            if isinstance(module, nn.Linear) and name != 'lm_head':
                tensors = {key: sf.get_tensor(name + '.' + key) for key in ['qweight','qzeros','scales','g_idx']}
                prefix = 'llama_model.base_model.model.' + name
                a = delta.get(prefix + '.lora_A.default.weight')
                b = delta.get(prefix + '.lora_B.default.weight')
                if name.endswith(('q_proj','k_proj','v_proj','o_proj')):
                    assert a is not None and b is not None, prefix
                    loaded.append(prefix)
                parent_name, child = name.rsplit('.', 1)
                setattr(model.get_submodule(parent_name), child, GPTQLinear(tensors, a, b))
        for name, param in list(model.named_parameters()):
            delta_key = 'llama_model.base_model.model.' + name
            value = delta.get(delta_key)
            if value is None:
                if name not in keys:
                    raise RuntimeError('Missing parameter: ' + name)
                value = sf.get_tensor(name)
            set_module_tensor_to_device(model, name, device, value=value, dtype=torch.float16)
    assert len(loaded) == 128, len(loaded)
    for name, buffer in list(model.named_buffers()):
        if buffer.device.type != 'cuda':
            set_module_tensor_to_device(model, name, device)
    model.eval()
    print('Loaded 128 official LoRA pairs, official embeddings and LM head.', flush=True)
    embed = model.get_input_embeddings()
    def text_embedding(text, bos=False):
        ids = tokenizer(text, add_special_tokens=False, return_tensors='pt').input_ids.to(device)
        if bos:
            ids = torch.cat([torch.tensor([[tokenizer.bos_token_id]], device=device), ids], dim=1)
        return embed(ids)
    # Match prepare_generation_embedding: BOS before each text segment;
    # image embedding wrapped in <Img> ... </Img>.
    inputs = torch.cat([
        text_embedding('### Human:', bos=True), text_embedding('<Img>'),
        visual.to(device), text_embedding('</Img>'),
        text_embedding(args.prompt + '\n### Assistant:', bos=True),
    ], dim=1)
    print('PROMPT:', args.prompt, flush=True)
    print('Generating...', flush=True)
    generation_start = time.perf_counter()
    output = model.generate(inputs_embeds=inputs, attention_mask=torch.ones(inputs.shape[:2], device=device, dtype=torch.long),
        max_new_tokens=args.max_new_tokens, do_sample=False, use_cache=True,
        eos_token_id=tokenizer.eos_token_id, pad_token_id=tokenizer.pad_token_id)
    torch.cuda.synchronize()
    generation_elapsed = time.perf_counter() - generation_start
    total_elapsed = time.perf_counter() - start
    answer = tokenizer.decode(output[0], skip_special_tokens=True).split('###')[0].strip()
    result = {
        'task': 'NExT-GPT image + text -> text (minimal quantized subset)',
        'prompt': args.prompt, 'answer': answer, 'raw_token_ids': output[0].tolist(),
        'gpu': torch.cuda.get_device_name(0), 'torch': torch.__version__,
        'cuda': torch.version.cuda, 'image_sha256': sha256(args.image),
        'official_delta_sha256': sha256(delta_path),
        'gptq_base_sha256': sha256(gptq_path), 'imagebind_sha256': sha256(imagebind_path),
        'encoder_seconds': round(encoder_time,2),
        'generation_seconds': round(generation_elapsed,2),
        'total_seconds': round(total_elapsed,2),
        'peak_allocated_mib': round(torch.cuda.max_memory_allocated()/1024**2,1),
        'lora_pairs_loaded': len(loaded), 'generation': 'greedy, max_new_tokens='+str(args.max_new_tokens),
        'scope': 'Real local CUDA inference; quantized Vicuna v0; no image/audio/video generation.',
    }
    (out / 'result.json').write_text(json.dumps(result,ensure_ascii=False,indent=2),encoding='utf-8')
    (out / 'output.txt').write_text('PROMPT: '+args.prompt+'\nANSWER: '+answer+'\n',encoding='utf-8')
    print('ANSWER:', answer, flush=True)
    print(json.dumps(result,ensure_ascii=False,indent=2), flush=True)

if __name__ == '__main__':
    main()
