Spaces:
Sleeping
Sleeping
File size: 11,184 Bytes
adb16e5 f32c771 adb16e5 75fa51d 34a299c 75fa51d f32c771 75fa51d adb16e5 75fa51d adb16e5 75fa51d adb16e5 75fa51d f32c771 75fa51d 34a299c 75fa51d 34a299c 75fa51d f32c771 34a299c f32c771 1873612 f32c771 34a299c f32c771 34a299c f32c771 34a299c 1873612 f32c771 34a299c 1873612 f32c771 1873612 e937fab 34a299c e937fab f32c771 1873612 f32c771 1873612 34a299c 77c3281 1873612 29b0bec 914b31e e937fab 914b31e f32c771 1873612 e937fab 29b0bec 1873612 8546bd8 e937fab e4e8be1 29b0bec e937fab f32c771 db9aa1e 29b0bec be69ccc cc66355 29b0bec 1873612 29b0bec e937fab 1873612 29b0bec e4e8be1 e937fab 8546bd8 914b31e 1873612 e937fab 1873612 34a299c 1873612 f32c771 1873612 f32c771 e937fab cd670ac | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | # ================================================================
# CodeSymb AI โ THE GRAND AWAKENING ๐ฅ
# โ
Loads 240k Base Model
# โ
Loads 50,000-Step SFT Chat LoRA (Just finished!)
# โ
Mathematically Fuses for Native Speed
# NiftyOracleAI ๐ฎ๐ณ | codesymb.com ๐
# ================================================================
import gradio as gr
import torch
import torch.nn as nn
import torch.nn.functional as F
import sentencepiece as spm
import spaces
import os
from huggingface_hub import hf_hub_download, login
HF_TOKEN = os.environ.get("HF_TOKEN", "")
USERNAME = "NiftyOracleAI"
REPO_ID = f"{USERNAME}/NiftyOracle-Coder-30B"
TOK_REPO = f"{USERNAME}/niftyoracle-tokenizer"
if HF_TOKEN: login(token=HF_TOKEN)
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# CORE ARCHITECTURE (Fixed 2048 RoPE)
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
class RMSNorm(nn.Module):
def __init__(self, d, eps=1e-6):
super().__init__()
self.w = nn.Parameter(torch.ones(d)); self.eps = eps
def forward(self, x):
n = torch.rsqrt(x.float().pow(2).mean(-1, keepdim=True) + self.eps)
return (x.float() * n).to(x.dtype) * self.w.to(x.dtype)
class RoPE(nn.Module):
def __init__(self, dim, max_len=2048, base=500_000):
super().__init__()
inv = 1.0 / (base ** (torch.arange(0, dim, 2).float() / dim))
emb = torch.cat([torch.outer(torch.arange(max_len).float(), inv)] * 2, dim=-1)
self.register_buffer("cos", emb.cos()[None, None].to(torch.float16))
self.register_buffer("sin", emb.sin()[None, None].to(torch.float16))
def forward(self, n): return self.cos[:, :, :n], self.sin[:, :, :n]
def rot(x):
a, b = x[..., :x.shape[-1]//2], x[..., x.shape[-1]//2:]
return torch.cat([-b, a], dim=-1)
def rope(q, k, cos, sin): return (q*cos)+(rot(q)*sin), (k*cos)+(rot(k)*sin)
class Attn(nn.Module):
def __init__(self, nh=16, nkv=4, dm=2048):
super().__init__()
hd = dm // nh; self.nh = nh; self.nkv = nkv; self.hd = hd; self.rep = nh // nkv
self.q = nn.Linear(dm, nh*hd, bias=False)
self.k = nn.Linear(dm, nkv*hd, bias=False)
self.v = nn.Linear(dm, nkv*hd, bias=False)
self.o = nn.Linear(nh*hd, dm, bias=False)
self.pe = RoPE(hd)
def forward(self, x):
B, T, _ = x.shape
q = self.q(x).view(B,T,self.nh,self.hd).transpose(1,2)
k = self.k(x).view(B,T,self.nkv,self.hd).transpose(1,2)
v = self.v(x).view(B,T,self.nkv,self.hd).transpose(1,2)
cos, sin = self.pe(T); q, k = rope(q, k, cos, sin)
k = k.repeat_interleave(self.rep, dim=1); v = v.repeat_interleave(self.rep, dim=1)
o = F.scaled_dot_product_attention(q, k, v, is_causal=True)
return self.o(o.transpose(1,2).contiguous().view(B,T,-1))
class FFN(nn.Module):
def __init__(self, dm=2048, df=8192):
super().__init__()
self.g = nn.Linear(dm, df, bias=False)
self.u = nn.Linear(dm, df, bias=False)
self.d = nn.Linear(df, dm, bias=False)
def forward(self, x): return self.d(F.silu(self.g(x)) * self.u(x))
class Block(nn.Module):
def __init__(self, dm=2048, df=8192, nh=16, nkv=4):
super().__init__()
self.n1 = RMSNorm(dm); self.att = Attn(nh, nkv, dm)
self.n2 = RMSNorm(dm); self.ff = FFN(dm, df)
def forward(self, x):
x = x + self.att(self.n1(x)); x = x + self.ff(self.n2(x)); return x
class CodeSymb1B(nn.Module):
def __init__(self, vocab=50000):
super().__init__()
DM=2048; DF=8192; NL=16; NH=16; NKV=4
self.embed = nn.Embedding(vocab, DM)
self.blocks = nn.ModuleList([Block(DM,DF,NH,NKV) for _ in range(NL)])
self.norm = RMSNorm(DM)
self.head = nn.Linear(DM, vocab, bias=False)
def forward(self, tok):
x = self.embed(tok).to(torch.float16)
for b in self.blocks: x = b(x)
return self.head(self.norm(x))
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# LOAD & FUSE THE 50,000-STEP CHAT LORA!
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
print("โณ Loading Tokenizer...")
tok_path = hf_hub_download(TOK_REPO, "niftyoracle_tokenizer.model", token=HF_TOKEN or None)
sp = spm.SentencePieceProcessor(); sp.load(tok_path)
print("โณ Loading 240k Base Model...")
model = CodeSymb1B(vocab=sp.get_piece_size())
base_ckpt = hf_hub_download(REPO_ID, "checkpoints/latest.pt", token=HF_TOKEN or None)
state = torch.load(base_ckpt, map_location="cpu", weights_only=False)
base_weights = {k: v for k, v in state.get("model", state).items() if not k.endswith((".pe.cos", ".pe.sin"))}
model.load_state_dict(base_weights, strict=False)
print("โ
Base Model Loaded.")
print("โก FUSING THE NEW 50,000-STEP SFT LORA...")
try:
# ๐ฅ PULLING THE EXACT FILE YOU JUST UPLOADED FROM LIGHTNING AI!
lora_ckpt = hf_hub_download(REPO_ID, "checkpoints/sft_full_latest.pt", token=HF_TOKEN or None)
lora_state_full = torch.load(lora_ckpt, map_location="cpu", weights_only=False)
lora_state = lora_state_full.get("model", lora_state_full)
scale = 200 / 100 # Alpha(200) / Rank(100)
with torch.no_grad():
for i, block in enumerate(model.blocks):
for layer_name in ["att.q", "att.k", "att.v", "att.o", "ff.g", "ff.u", "ff.d"]:
A_key = f"blocks.{i}.{layer_name}.A"
B_key = f"blocks.{i}.{layer_name}.B"
# Check for PEFT format keys
peft_A_key = f"base_model.model.blocks.{i}.{layer_name}.lora_A.weight"
peft_B_key = f"base_model.model.blocks.{i}.{layer_name}.lora_B.weight"
A_val = lora_state.get(peft_A_key, lora_state.get(A_key))
B_val = lora_state.get(peft_B_key, lora_state.get(B_key))
if A_val is not None and B_val is not None:
A = A_val.float()
B = B_val.float()
delta = (B @ A) * scale if A.shape[1] == 100 else (A @ B).T * scale
target_layer = getattr(block, layer_name.split(".")[0])
sub_layer = getattr(target_layer, layer_name.split(".")[1])
sub_layer.weight.data += delta.to(sub_layer.weight.dtype)
print("โ
50k Chat LoRA Fused! The AI is fully conversational! ๐ฃ๏ธ")
except Exception as e:
print(f"โ ๏ธ Failed to fuse Chat LoRA: {e}")
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = model.to(device).half().eval()
print(f"โ
CodeSymb is ALIVE on {device}!")
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# INFERENCE ENGINE
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
@spaces.GPU(duration=60)
def chat_with_codesymb(message: str, history: list) -> str:
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
# EXACT SAME SYSTEM PROMPT FROM TRAINING!
system_prompt = (
"You are CodeSymb AI โ expert coding assistant built by Deepakkumar Malkar "
"at NiftyOracle India. Write clean code, explain simply, be friendly. "
"Nothing is impossible. codesymb.com"
)
full_prompt = f"<|system|>\n{system_prompt}\n"
for msg in history[-3:]:
if isinstance(msg, dict):
role = "<|user|>" if msg.get("role") == "user" else "<|assistant|>"
full_prompt += f"{role}\n{msg.get('content', '')}\n"
elif hasattr(msg, "role"):
role = "<|user|>" if msg.role == "user" else "<|assistant|>"
full_prompt += f"{role}\n{msg.content}\n"
else:
full_prompt += f"<|user|>\n{msg[0]}\n<|assistant|>\n{msg[1]}\n"
full_prompt += f"<|user|>\n{message}\n<|assistant|>\n"
prompt_ids = sp.encode(full_prompt)
if len(prompt_ids) > 1024: prompt_ids = prompt_ids[-1024:]
inp = torch.tensor([prompt_ids], dtype=torch.long).to(device)
generated = []
END_SEQ = sp.encode("<|endoftext|>")
with torch.no_grad():
for _ in range(512):
with torch.autocast("cuda", dtype=torch.float16):
logits = model(inp)[0, -1, :].float()
for gid in set(generated[-50:]):
logits[gid] -= 2.0
logits = logits / 0.5
top_k_values, _ = torch.topk(logits, 40)
logits[logits < top_k_values[-1]] = -float('inf')
sorted_logits, sorted_idx = torch.sort(logits, descending=True)
cum_probs = torch.cumsum(F.softmax(sorted_logits, dim=-1), dim=-1)
remove = cum_probs > 0.90
remove[1:] = remove[:-1].clone(); remove[0] = False
sorted_logits[remove] = -float("inf")
logits_filtered = torch.zeros_like(logits)
logits_filtered.scatter_(0, sorted_idx, sorted_logits)
probs = F.softmax(logits_filtered, dim=-1)
next_t = torch.multinomial(probs, 1).item()
if len(generated) >= 3 and generated[-1] == next_t and generated[-2] == next_t:
break
generated.append(next_t)
inp = torch.cat([inp, torch.tensor([[next_t]], device=device)], dim=1)
decoded = sp.decode(generated)
if "<|endoftext|>" in decoded or "<|user|>" in decoded:
break
response = sp.decode(generated).strip()
for tag in ["<|user|>", "<|system|>", "<|assistant|>", "<|endoftext|>"]:
response = response.split(tag)[0].strip()
return response if response else "I am CodeSymb! How can I help you? ๐ฅ"
with gr.Blocks(theme=gr.themes.Base(primary_hue="cyan", secondary_hue="blue"), css="body { background: #060D1F !important; color: white !important; }") as demo:
gr.HTML("""
<h1 style='text-align:center; color:#00D4FF;'>โก CodeSymb AI</h1>
<p style='text-align:center; color:#A0AEC0;'>
Built by <strong>Deepakkumar Malkar</strong> at NiftyOracle ๐ฎ๐ณ<br>
</p>
""")
gr.ChatInterface(
fn=chat_with_codesymb,
examples=[
["Hi! Who are you?"],
["Write a Python function to reverse a string."],
["What is an API?"],
]
)
if __name__ == "__main__":
demo.launch() |