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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
datasets:
|
| 4 |
+
- crownelius/Opus-4.6-Reasoning-3300x
|
| 5 |
+
base_model:
|
| 6 |
+
- microsoft/phi-2
|
| 7 |
+
- venkycs/phi-2-instruct
|
| 8 |
+
pipeline_tag: text-generation
|
| 9 |
+
---
|
| 10 |
+
**LBNET-2.7B-BASE model card**
|
| 11 |
+
|
| 12 |
+
We introduce the first-ever Logic/Reasoning-based transformer model based on Phi-2.
|
| 13 |
+
In February 2026, we created an experimental architecture called LBNets, an attempt to inject reasoning-like layers into a model's architecture. In this case, we experimented with Phi-2.
|
| 14 |
+
|
| 15 |
+
**Here is the logic behind LBNET-2.7B-BASE:**
|
| 16 |
+
- Split the base model into two halves: pre-reasoning and post-reasoning layers.
|
| 17 |
+
- Between these layers, you insert:
|
| 18 |
+
- learnable latent 'reasoning tokens'
|
| 19 |
+
- reasoning blocks (cross-attention: latent tokens attend to the main hidden states (the “context”), self-attention: latent tokens attend to each other, MLP)
|
| 20 |
+
- reasoning injector back into the main stream
|
| 21 |
+
|
| 22 |
+
To make generation workable:
|
| 23 |
+
- During prefill (the initial prompt, past_length == 0 and seq_len > 1), the model runs the reasoning loop once.
|
| 24 |
+
- During token-by-token generation with KV-cache (seq_len == 1), the model skips the reasoning loop (otherwise it gets slow and unstable).
|
| 25 |
+
|
| 26 |
+
LBNETS-2.7B-BASE achieves much above average benchmarks for its size compared to other models:
|
| 27 |
+
|
| 28 |
+
| Tasks |Version|Filter|n-shot| Metric | |Value | |Stderr|
|
| 29 |
+
|-------------|------:|------|-----:|--------|---|-----:|---|-----:|
|
| 30 |
+
|arc_challenge| 1|none | 0|acc |↑ |0.5324|± |0.0146|
|
| 31 |
+
| | |none | 0|acc_norm|↑ |0.5478|± |0.0145|
|
| 32 |
+
|arc_easy | 1|none | 0|acc |↑ |0.8047|± |0.0081|
|
| 33 |
+
| | |none | 0|acc_norm|↑ |0.7862|± |0.0084|
|
| 34 |
+
|boolq | 2|none | 0|acc |↑ |0.8346|± |0.0065|
|
| 35 |
+
|openbookqa | 1|none | 0|acc |↑ |0.4040|± |0.0220|
|
| 36 |
+
| | |none | 0|acc_norm|↑ |0.5160|± |0.0224|
|
| 37 |
+
|piqa | 1|none | 0|acc |↑ |0.7889|± |0.0095|
|
| 38 |
+
| | |none | 0|acc_norm|↑ |0.7949|± |0.0094|
|
| 39 |
+
|winogrande | 1|none | 0|acc |↑ |0.7577|± |0.0120|
|
| 40 |
+
|
| 41 |
+
We reccommend running this model on at least an RTX 3050 with 8gb of VRAM.
|
| 42 |
+
FOR FULL MODEL FUNCTIONALITY, YOU MUST USE THE CHAT SCRIPT BELOW:
|
| 43 |
+
|
| 44 |
+
The script is ROCm-friendly. May need tweaking for CUDA setups.
|
| 45 |
+
|
| 46 |
+
'''python
|
| 47 |
+
|
| 48 |
+
import os
|
| 49 |
+
import argparse
|
| 50 |
+
import torch
|
| 51 |
+
from transformers import AutoTokenizer
|
| 52 |
+
|
| 53 |
+
from configuration import PhiReasoningConfig
|
| 54 |
+
from modeling import PhiForLogicalReasoning
|
| 55 |
+
|
| 56 |
+
# ROCm allocator hint (helps fragmentation on AMD ROCm)
|
| 57 |
+
os.environ.setdefault("PYTORCH_HIP_ALLOC_CONF", "expandable_segments:True,max_split_size_mb:64")
|
| 58 |
+
|
| 59 |
+
DEFAULT_SYSTEM_PROMPT = "You are LBNets, a helpful assistant."
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def format_prompt(system_prompt: str, user_text: str, history, max_turns: int = 6) -> str:
|
| 63 |
+
"""
|
| 64 |
+
Build a single instruction that includes recent chat history.
|
| 65 |
+
This keeps compatibility with your training template.
|
| 66 |
+
|
| 67 |
+
history: list of (user, assistant) tuples
|
| 68 |
+
"""
|
| 69 |
+
system_prompt = (system_prompt or "").strip()
|
| 70 |
+
user_text = (user_text or "").strip()
|
| 71 |
+
|
| 72 |
+
convo = ""
|
| 73 |
+
for u, a in history[-max_turns:]:
|
| 74 |
+
convo += f"User: {u}\nAssistant: {a}\n"
|
| 75 |
+
|
| 76 |
+
instruction = ""
|
| 77 |
+
if convo:
|
| 78 |
+
instruction += "Conversation so far:\n" + convo + "\n"
|
| 79 |
+
instruction += "Current user message:\n" + user_text
|
| 80 |
+
|
| 81 |
+
return (
|
| 82 |
+
f"### System:\n{system_prompt}\n\n"
|
| 83 |
+
f"### Instruction:\n{instruction}\n\n"
|
| 84 |
+
f"### Response:\n"
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
@torch.inference_mode()
|
| 89 |
+
def generate_text(model, tok, prompt_text: str, device: str, max_new_tokens: int = 256) -> str:
|
| 90 |
+
inputs = tok(
|
| 91 |
+
prompt_text,
|
| 92 |
+
return_tensors="pt",
|
| 93 |
+
add_special_tokens=False,
|
| 94 |
+
truncation=True,
|
| 95 |
+
max_length=768, # history makes prompts longer; keep sane
|
| 96 |
+
).to(device)
|
| 97 |
+
|
| 98 |
+
in_len = inputs["input_ids"].shape[1]
|
| 99 |
+
|
| 100 |
+
out_ids = model.generate(
|
| 101 |
+
**inputs,
|
| 102 |
+
do_sample=False, # greedy
|
| 103 |
+
use_cache=True, # KV cache (fast)
|
| 104 |
+
max_new_tokens=max_new_tokens,
|
| 105 |
+
min_new_tokens=1,
|
| 106 |
+
|
| 107 |
+
# general anti-loop controls (not per-problem patching)
|
| 108 |
+
repetition_penalty=1.10,
|
| 109 |
+
no_repeat_ngram_size=3,
|
| 110 |
+
|
| 111 |
+
pad_token_id=tok.pad_token_id,
|
| 112 |
+
eos_token_id=tok.eos_token_id,
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
new_ids = out_ids[0][in_len:]
|
| 116 |
+
text = tok.decode(new_ids, skip_special_tokens=True)
|
| 117 |
+
|
| 118 |
+
# Avoid "blank" replies from leading newline spam
|
| 119 |
+
return text.lstrip("\n").rstrip()
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
def load_model(model_path: str, device: str):
|
| 123 |
+
cfg = PhiReasoningConfig.from_pretrained(model_path)
|
| 124 |
+
cfg.attn_implementation = "eager"
|
| 125 |
+
cfg.use_cache = True
|
| 126 |
+
|
| 127 |
+
tok = AutoTokenizer.from_pretrained(model_path)
|
| 128 |
+
if tok.pad_token is None:
|
| 129 |
+
tok.pad_token = tok.eos_token
|
| 130 |
+
tok.pad_token_id = tok.eos_token_id
|
| 131 |
+
|
| 132 |
+
model = PhiForLogicalReasoning.from_pretrained(
|
| 133 |
+
model_path,
|
| 134 |
+
config=cfg,
|
| 135 |
+
torch_dtype=torch.float16, # often faster/more compatible on ROCm than bf16
|
| 136 |
+
low_cpu_mem_usage=True,
|
| 137 |
+
).to(device)
|
| 138 |
+
|
| 139 |
+
model.eval()
|
| 140 |
+
|
| 141 |
+
gate = model.model.reasoning_injector.gate_scale.detach().float().cpu().numpy()
|
| 142 |
+
total_params = sum(p.numel() for p in model.parameters())
|
| 143 |
+
print(f"Loaded: {model_path}")
|
| 144 |
+
print(f"Parameters: {total_params:,}")
|
| 145 |
+
print(f"Gate scale: {gate}")
|
| 146 |
+
print(f"Device: {device}")
|
| 147 |
+
|
| 148 |
+
return model, tok
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
def main():
|
| 152 |
+
ap = argparse.ArgumentParser()
|
| 153 |
+
ap.add_argument("--model_path", default="./outputs/phi-reasoning-crownelius-opus")
|
| 154 |
+
ap.add_argument("--device", default="cuda:0")
|
| 155 |
+
ap.add_argument("--system_prompt", default=DEFAULT_SYSTEM_PROMPT)
|
| 156 |
+
ap.add_argument("--max_new_tokens", type=int, default=256)
|
| 157 |
+
ap.add_argument("--history_turns", type=int, default=6)
|
| 158 |
+
args = ap.parse_args()
|
| 159 |
+
|
| 160 |
+
model, tok = load_model(args.model_path, args.device)
|
| 161 |
+
|
| 162 |
+
history = []
|
| 163 |
+
|
| 164 |
+
print("\n============================================================")
|
| 165 |
+
print("LBNets Chat Ready!")
|
| 166 |
+
print("Commands: 'quit' to exit | 'reset' to clear conversation")
|
| 167 |
+
print("============================================================\n")
|
| 168 |
+
|
| 169 |
+
while True:
|
| 170 |
+
user = input("User: ").strip()
|
| 171 |
+
if not user:
|
| 172 |
+
continue
|
| 173 |
+
|
| 174 |
+
if user.lower() in ("quit", "exit", "q"):
|
| 175 |
+
break
|
| 176 |
+
|
| 177 |
+
if user.lower() in ("reset", "/reset"):
|
| 178 |
+
history.clear()
|
| 179 |
+
print("AI: Conversation reset.\n")
|
| 180 |
+
continue
|
| 181 |
+
|
| 182 |
+
prompt = format_prompt(args.system_prompt, user, history, max_turns=args.history_turns)
|
| 183 |
+
resp = generate_text(model, tok, prompt, args.device, max_new_tokens=args.max_new_tokens)
|
| 184 |
+
|
| 185 |
+
print(f"AI: {resp}\n")
|
| 186 |
+
|
| 187 |
+
# store turn
|
| 188 |
+
history.append((user, resp))
|
| 189 |
+
|
| 190 |
+
|
| 191 |
+
if __name__ == "__main__":
|
| 192 |
+
main()
|
| 193 |
+
'''
|
| 194 |
+
|