lemms commited on
Commit
95d50fc
·
verified ·
1 Parent(s): 04e75aa

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +16 -15
app.py CHANGED
@@ -1,6 +1,6 @@
1
  #!/usr/bin/env python3
2
  """
3
- OpenLLM Real Models App - Final version with correct model architecture
4
  """
5
 
6
  import gradio as gr
@@ -33,21 +33,21 @@ class GPTConfig:
33
  self.bias = bias
34
 
35
  class GPT(nn.Module):
36
- """GPT-style transformer model - matches the actual trained model architecture"""
37
  def __init__(self, config):
38
  super().__init__()
39
  assert config.vocab_size is not None
40
  assert config.block_size is not None
41
  self.config = config
42
 
43
- # Word and position embeddings
44
- self.wte = nn.Embedding(config.vocab_size, config.n_embd)
45
- self.wpe = nn.Embedding(config.block_size, config.n_embd)
46
- self.drop = nn.Dropout(config.dropout)
47
-
48
- # Transformer blocks
49
- self.h = nn.ModuleList([Block(config) for _ in range(config.n_layer)])
50
- self.ln_f = nn.LayerNorm(config.n_embd)
51
 
52
  # Language model head
53
  self.lm_head = nn.Linear(config.n_embd, config.vocab_size, bias=config.bias)
@@ -72,13 +72,13 @@ class GPT(nn.Module):
72
  assert t <= self.config.block_size, f"Cannot forward sequence of length {t}, block size is only {self.config.block_size}"
73
 
74
  pos = torch.arange(0, t, dtype=torch.long, device=device).unsqueeze(0)
75
- tok_emb = self.wte(idx)
76
- pos_emb = self.wpe(pos)
77
- x = self.drop(tok_emb + pos_emb)
78
 
79
- for block in self.h:
80
  x = block(x)
81
- x = self.ln_f(x)
82
 
83
  if targets is not None:
84
  logits = self.lm_head(x)
@@ -144,6 +144,7 @@ class CausalSelfAttention(nn.Module):
144
  self.n_head = config.n_head
145
  self.n_embd = config.n_embd
146
  self.dropout = config.dropout
 
147
 
148
  def forward(self, x):
149
  B, T, C = x.size()
 
1
  #!/usr/bin/env python3
2
  """
3
+ OpenLLM Real Models App - Corrected version with exact model architecture
4
  """
5
 
6
  import gradio as gr
 
33
  self.bias = bias
34
 
35
  class GPT(nn.Module):
36
+ """GPT-style transformer model - EXACT architecture matching the saved model"""
37
  def __init__(self, config):
38
  super().__init__()
39
  assert config.vocab_size is not None
40
  assert config.block_size is not None
41
  self.config = config
42
 
43
+ # Create the transformer module with the exact naming convention
44
+ self.transformer = nn.ModuleDict(dict(
45
+ wte = nn.Embedding(config.vocab_size, config.n_embd),
46
+ wpe = nn.Embedding(config.block_size, config.n_embd),
47
+ drop = nn.Dropout(config.dropout),
48
+ h = nn.ModuleList([Block(config) for _ in range(config.n_layer)]),
49
+ ln_f = nn.LayerNorm(config.n_embd),
50
+ ))
51
 
52
  # Language model head
53
  self.lm_head = nn.Linear(config.n_embd, config.vocab_size, bias=config.bias)
 
72
  assert t <= self.config.block_size, f"Cannot forward sequence of length {t}, block size is only {self.config.block_size}"
73
 
74
  pos = torch.arange(0, t, dtype=torch.long, device=device).unsqueeze(0)
75
+ tok_emb = self.transformer.wte(idx)
76
+ pos_emb = self.transformer.wpe(pos)
77
+ x = self.transformer.drop(tok_emb + pos_emb)
78
 
79
+ for block in self.transformer.h:
80
  x = block(x)
81
+ x = self.transformer.ln_f(x)
82
 
83
  if targets is not None:
84
  logits = self.lm_head(x)
 
144
  self.n_head = config.n_head
145
  self.n_embd = config.n_embd
146
  self.dropout = config.dropout
147
+ self.bias = config.bias # Add bias parameter
148
 
149
  def forward(self, x):
150
  B, T, C = x.size()