Ubuntu
commited on
Commit
·
e24e118
1
Parent(s):
fff7eb2
Update: refined tars_v1_model.py structure and logic
Browse files- TARS-v1/tars_v1_model.py +46 -29
TARS-v1/tars_v1_model.py
CHANGED
|
@@ -1,49 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
import torch.nn as nn
|
| 3 |
-
from transformers import BertModel, GPTNeoForCausalLM,
|
| 4 |
|
| 5 |
class TARSQuantumHybrid(nn.Module):
|
| 6 |
"""
|
| 7 |
-
TARSQuantumHybrid:
|
| 8 |
-
with GPT-Neo's generative reasoning. Designed for advanced quantum-AI interaction.
|
| 9 |
-
|
| 10 |
-
Compatible with Hugging Face model hub and deployable in transformers pipelines.
|
| 11 |
"""
|
| 12 |
-
|
| 13 |
def __init__(self, bert_model="bert-base-uncased", gpt_model="EleutherAI/gpt-neo-125M"):
|
| 14 |
super(TARSQuantumHybrid, self).__init__()
|
| 15 |
-
self.
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def forward(self, input_ids, attention_mask=None, decoder_input_ids=None):
|
| 23 |
-
|
| 24 |
-
Forward pass through BERT + projected embeddings into GPT-Neo.
|
| 25 |
-
|
| 26 |
-
Parameters:
|
| 27 |
-
- input_ids: Token IDs for BERT
|
| 28 |
-
- attention_mask: Attention mask for BERT
|
| 29 |
-
- decoder_input_ids: Decoder inputs for GPT
|
| 30 |
-
|
| 31 |
-
Returns:
|
| 32 |
-
- GPTNeo output logits
|
| 33 |
-
"""
|
| 34 |
-
# Step 1: Encode with BERT
|
| 35 |
bert_output = self.bert(input_ids=input_ids, attention_mask=attention_mask)
|
| 36 |
-
cls_embedding = bert_output.last_hidden_state[:, 0, :] # [CLS] token
|
| 37 |
|
| 38 |
-
#
|
| 39 |
gpt_input = self.embedding_proj(cls_embedding).unsqueeze(1)
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
|
| 43 |
-
return
|
| 44 |
|
| 45 |
if __name__ == "__main__":
|
| 46 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
model = TARSQuantumHybrid()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
torch.save(model.state_dict(), "tars_v1.pt")
|
| 49 |
print("✅ TARS-v1 model saved successfully as 'tars_v1.pt'")
|
|
|
|
| 1 |
+
# tars_v1_model.py
|
| 2 |
+
"""
|
| 3 |
+
TARS-v1: A Hybrid Quantum-AI Model Combining BERT and GPT-Neo for Natural Language Understanding and Generation.
|
| 4 |
+
|
| 5 |
+
SEO Optimized Tags: Quantum AI, GPT-Neo BERT Hybrid, HuggingFace Compatible, NLP Model, TARS-v1, AI Assistant, SubatomicError, PyTorch Transformers
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
import torch
|
| 9 |
import torch.nn as nn
|
| 10 |
+
from transformers import BertModel, GPTNeoForCausalLM, BertTokenizer, GPT2Tokenizer
|
| 11 |
|
| 12 |
class TARSQuantumHybrid(nn.Module):
|
| 13 |
"""
|
| 14 |
+
TARSQuantumHybrid: Combines the deep language understanding of BERT with the generative capabilities of GPT-Neo.
|
|
|
|
|
|
|
|
|
|
| 15 |
"""
|
|
|
|
| 16 |
def __init__(self, bert_model="bert-base-uncased", gpt_model="EleutherAI/gpt-neo-125M"):
|
| 17 |
super(TARSQuantumHybrid, self).__init__()
|
| 18 |
+
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 19 |
+
|
| 20 |
+
# Load pretrained models
|
| 21 |
+
self.bert = BertModel.from_pretrained(bert_model).to(self.device)
|
| 22 |
+
self.gpt = GPTNeoForCausalLM.from_pretrained(gpt_model).to(self.device)
|
| 23 |
|
| 24 |
+
# BERT hidden size -> GPT input embedding projection
|
| 25 |
+
self.embedding_proj = nn.Linear(
|
| 26 |
+
self.bert.config.hidden_size,
|
| 27 |
+
self.gpt.config.hidden_size # FIXED: correct attribute
|
| 28 |
+
).to(self.device)
|
| 29 |
|
| 30 |
def forward(self, input_ids, attention_mask=None, decoder_input_ids=None):
|
| 31 |
+
# BERT processes input
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
bert_output = self.bert(input_ids=input_ids, attention_mask=attention_mask)
|
| 33 |
+
cls_embedding = bert_output.last_hidden_state[:, 0, :] # Extract [CLS] token
|
| 34 |
|
| 35 |
+
# Project BERT CLS to GPT's input embedding space
|
| 36 |
gpt_input = self.embedding_proj(cls_embedding).unsqueeze(1)
|
| 37 |
|
| 38 |
+
# GPT-Neo generates output based on embedded input
|
| 39 |
+
output = self.gpt(inputs_embeds=gpt_input, decoder_input_ids=decoder_input_ids)
|
| 40 |
+
return output
|
| 41 |
|
| 42 |
if __name__ == "__main__":
|
| 43 |
+
# Load tokenizers for test
|
| 44 |
+
bert_tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
|
| 45 |
+
gpt_tokenizer = GPT2Tokenizer.from_pretrained("EleutherAI/gpt-neo-125M")
|
| 46 |
+
|
| 47 |
+
# Initialize model
|
| 48 |
model = TARSQuantumHybrid()
|
| 49 |
+
model.eval()
|
| 50 |
+
|
| 51 |
+
# Test Input
|
| 52 |
+
sample_text = "What is quantum consciousness?"
|
| 53 |
+
tokens = bert_tokenizer(sample_text, return_tensors="pt").to(model.device)
|
| 54 |
+
|
| 55 |
+
# Dummy decoder input (for GPT)
|
| 56 |
+
decoder_input_ids = torch.tensor([[gpt_tokenizer.bos_token_id]]).to(model.device)
|
| 57 |
+
|
| 58 |
+
# Forward pass
|
| 59 |
+
with torch.no_grad():
|
| 60 |
+
output = model(input_ids=tokens['input_ids'],
|
| 61 |
+
attention_mask=tokens['attention_mask'],
|
| 62 |
+
decoder_input_ids=decoder_input_ids)
|
| 63 |
+
|
| 64 |
+
# Save model weights
|
| 65 |
torch.save(model.state_dict(), "tars_v1.pt")
|
| 66 |
print("✅ TARS-v1 model saved successfully as 'tars_v1.pt'")
|