Views
No views yet
fluxattn package and Block-Sparse-Attention to be installed as described in the official repository.1import torch
2import json
3from transformers import AutoTokenizer, AutoModelForCausalLM
4
5def load_sparse_model(model_path):
6 """
7 Dynamically loads the correct sparse architecture based on config.
8 """
9 config_path = f"{model_path}/config.json"
10 with open(config_path, "r") as f:
11 config_data = json.load(f)
12
13 arch = config_data.get("architectures", [])
14 if not arch:
15 raise ValueError("No architecture found in config.json")
16
17 arch_name = arch[0]
18 print(f"🚀 Detected architecture: {arch_name}")
19
20 # Register custom architectures
21 if "PawLlama" in arch_name:
22 from fluxattn.training.eval.modeling_flash_llama import (
23 PawLlamaForCausalLM, PawLlamaConfig
24 )
25 AutoModelForCausalLM.register(PawLlamaConfig, PawLlamaForCausalLM)
26 model_cls = PawLlamaForCausalLM
27
28 elif "PawQwen" in arch_name:
29 from fluxattn.training.eval.modeling_flash_qwen import (
30 PawQwen3ForCausalLM, PawQwen3Config
31 )
32 AutoModelForCausalLM.register(PawQwen3Config, PawQwen3ForCausalLM)
33 model_cls = PawQwen3ForCausalLM
34 else:
35 raise ValueError(f"Unsupported architecture: {arch_name}")
36
37 # Load model
38 model = model_cls.from_pretrained(
39 model_path,
40 torch_dtype=torch.bfloat16,
41 device_map="auto",
42 trust_remote_code=True,
43 )
44 return model
45
46# --- Execution ---
47model_path = "QQTang1223/Flux-Attention-Qwen3-4B" # <--- Replace with your checkpoint path
48tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
49
50print("Loading Flux Attention Model...")
51model = load_sparse_model(model_path)
52model.eval()
53
54# Generate
55input_text = "Explain quantum mechanics in one sentence."
56inputs = tokenizer(input_text, return_tensors="pt").to("cuda")
57
58print("Generating...")
59outputs = model.generate(**inputs, max_new_tokens=100)
60print("
61Output:
62" + tokenizer.decode(outputs[0], skip_special_tokens=True))1@misc{qiu2026fluxattentioncontextawarehybrid,
2 title={Flux Attention: Context-Aware Hybrid Attention for Efficient LLMs Inference},
3 author={Quantong Qiu and Zhiyi Hong and Yi Yang and Haitian Wang and Kebin Liu and Qingqing Dang and Juntao Li and Min Zhang},
4 year={2026},
5 eprint={2604.07394},
6 archivePrefix={arXiv},
7 primaryClass={cs.LG},
8 url={https://arxiv.org/abs/2604.07394},
9}