Views
No views yet
| Model | Substr-EM | F1 Score |
|---|---|---|
| meta-llama/Llama-3.2-1B (base) | 56.00% | 12.51% |
| meta-llama/Llama-3.2-1B-Instruct | 86.00% | 23.62% |
| hxia7/Llama-3.2-1B-block-FT (full-attention) | 87.00% | 26.59% |
| hxia7/Llama-3.2-1B-block-FT (block-attention) | 88.00% | 27.53% |
| hxia7/Qwen3-8B-block-FT (full-attention) | 91.00% | 25.18% |
| hxia7/Qwen3-8B-block-FT (block-attention) | 90.00% | 23.71% |
[Block-Attention] prefix token and 4D block mask)[1, 1, seq_len, seq_len] during prefill. model.generate() only accepts 2D masks, so inference requires manual prefill + autoregressive decode:1import torch
2from transformers import AutoTokenizer, AutoModelForCausalLM
3from src.data.block import build_attention_mask, convert_attention_mask_to_model_required
4
5model = AutoModelForCausalLM.from_pretrained("hxia7/Qwen3-8B-block-FT", torch_dtype=torch.bfloat16, device_map="auto")
6tokenizer = AutoTokenizer.from_pretrained("hxia7/Qwen3-8B-block-FT")
7
8blocks = [
9 "\nYou are an intelligent AI assistant. Please answer questions based on the user's instructions. Below are some reference documents that may help you in answering the user's question.\n\n",
10 "- Title: Document 1\nContent of document 1...\n",
11 "- Title: Document 2\nContent of document 2...\n",
12 "\n\nPlease write a high-quality answer for the given question using only the provided search documents.\nQuestion: What is X?\n\n\n",
13]
14
15@torch.no_grad()
16def block_generate(model, tokenizer, blocks, max_new_tokens=128):
17 block_token_counts = []
18 all_ids = []
19 for b in blocks:
20 ids = tokenizer.encode(b, add_special_tokens=False)
21 all_ids.extend(ids)
22 block_token_counts.append(len(ids))
23
24 input_ids = torch.tensor([all_ids], dtype=torch.int64, device=model.device)
25 total_len = len(all_ids)
26
27 helper = torch.tril(torch.ones(total_len + 64, total_len + 64, dtype=torch.bool))
28 attn_mask = build_attention_mask(
29 local_attention_block_tokens=torch.tensor(block_token_counts[:-1], dtype=torch.long),
30 global_attention_block_tokens=torch.tensor(block_token_counts[-1], dtype=torch.long),
31 lower_triangular_matrix=helper,
32 )
33 attn_mask = convert_attention_mask_to_model_required(attn_mask)
34 attn_mask = attn_mask.unsqueeze(0).unsqueeze(0).to(model.device)
35
36 outputs = model(input_ids=input_ids, attention_mask=attn_mask, use_cache=True)
37 past_kv = outputs.past_key_values
38 next_token = torch.argmax(outputs.logits[:, -1, :], dim=-1, keepdim=True)
39
40 generated = []
41 for _ in range(max_new_tokens - 1):
42 if next_token.item() == tokenizer.eos_token_id:
43 break
44 generated.append(next_token.item())
45 outputs = model(input_ids=next_token, past_key_values=past_kv, use_cache=True)
46 past_kv = outputs.past_key_values
47 next_token = torch.argmax(outputs.logits[:, -1, :], dim=-1, keepdim=True)
48
49 if next_token.item() != tokenizer.eos_token_id:
50 generated.append(next_token.item())
51
52 return tokenizer.decode(generated, skip_special_tokens=True).strip()
53
54answer = block_generate(model, tokenizer, blocks)
55print(answer)1from transformers import AutoTokenizer, AutoModelForCausalLM
2
3model = AutoModelForCausalLM.from_pretrained("hxia7/Qwen3-8B-block-FT", torch_dtype=torch.bfloat16, device_map="auto")
4tokenizer = AutoTokenizer.from_pretrained("hxia7/Qwen3-8B-block-FT")
5
6prompt = "Your full RAG prompt here..."
7inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=3968).to(model.device)
8outputs = model.generate(**inputs, max_new_tokens=128, do_sample=False, pad_token_id=tokenizer.eos_token_id)
9answer = tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:], skip_special_tokens=True)
10print(answer)