Warum handelt mein Lama 3.1 -Modell zwischen AutomodelformcausAllm und llamaforcausallm anders?Python

Python-Programme
Anonymous
 Warum handelt mein Lama 3.1 -Modell zwischen AutomodelformcausAllm und llamaforcausallm anders?

Post by Anonymous »

Ich habe einen Satz von Gewichten, einen Tokenizer, die gleiche Eingabeaufforderung und die gleiche Parameter für die Erzeugung. Wenn ich das Modell mit AutomodelforcausAllm lade, erhalte ich jedoch eine Ausgabe, und wenn ich es manuell mit llamaforcausAllm plus derselben Konfiguration und dem gleichen State_Dict baue, erhalte ich eine andere Ausgabe vollständig.

Code: Select all

import torch
from transformers import (
AutoTokenizer,
AutoModelForCausalLM,
LlamaForCausalLM,
LlamaConfig
)

# 1) Adjust these as needed
model_name = "meta-llama/Llama-3.1-8B"
prompt = "Hello from Llama 3.1! Tell me something interesting."
dtype = torch.float16  # or torch.float32 if needed

# 2) Get the tokenizer
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=False)

# Prepare input
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")

############################################
# A) Load with AutoModelForCausalLM
############################################

print("=== Loading with AutoModelForCausalLM ===")

model_auto = AutoModelForCausalLM.from_pretrained(
model_name,
attn_implementation="eager",  # matches your usage
torch_dtype=dtype
).cuda()
model_auto.eval()  # turn off dropout
config = model_auto.config
with torch.no_grad():
out_auto = model_auto(**inputs)
logits_auto = out_auto.logits  # shape: [batch_size, seq_len, vocab_size]

del model_auto
torch.cuda.empty_cache()

############################################
# B) Load with LlamaForCausalLM + config
############################################

print("=== Loading with LlamaForCausalLM + config ===")

# Get config from the same checkpoint
# Build Llama model directly
model_llama = LlamaForCausalLM(config).cuda()
model_llama.eval()

# Load the same weights that AutoModelForCausalLM used
model_auto_temp = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=dtype)
model_llama.load_state_dict(model_auto_temp.state_dict())
del model_auto_temp
torch.cuda.empty_cache()

with torch.no_grad():
out_llama = model_llama(**inputs)
logits_llama = out_llama.logits

############################################
# C) Compare the Logits
############################################

# Compute maximum absolute difference
max_diff = (logits_auto - logits_llama).abs().max()
print(f"\nMax absolute difference between logits: {max_diff.item()}")

if max_diff < 1e-7:
print("→ The logits are effectively identical (within floating-point precision).")
else:
print("→ There is a non-trivial difference in logits!")

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post