codewithdark commited on
Commit
14ffda8
·
verified ·
1 Parent(s): 469ae5e

Upload 6 files

Browse files
Model/codaBlock.py ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ import torch.nn.functional as F
4
+ from typing import Optional, Tuple
5
+
6
+ # Final Projection Block
7
+ class CodaBlock(nn.Module):
8
+ def __init__(self, d_model: int, vocab_size: int):
9
+ super().__init__()
10
+ self.norm = nn.LayerNorm(d_model)
11
+ self.output_proj = nn.Linear(d_model, vocab_size)
12
+
13
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
14
+ return self.output_proj(self.norm(x))
Model/latent_Recurrent.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ import torch.nn.functional as F
4
+ from typing import Optional, Tuple
5
+ from Model.prelude_Block import PreludeBlock
6
+ from Model.recurrent_Block import RecurrentBlock
7
+ from Model.codaBlock import CodaBlock
8
+
9
+ # Full Latent Recurrent Depth Model
10
+ class LatentRecurrentDepthLM(nn.Module):
11
+ def __init__(self, vocab_size: int, d_model: int, num_heads: int, dropout: float = 0.1):
12
+ super().__init__()
13
+ self.prelude = PreludeBlock(vocab_size, d_model, num_heads, dropout)
14
+ self.recurrent = RecurrentBlock(d_model, num_heads, dropout)
15
+ self.coda = CodaBlock(d_model, vocab_size)
16
+
17
+ def forward(self, x: torch.Tensor, num_iterations: int, mask: Optional[torch.Tensor] = None) -> torch.Tensor:
18
+ hidden = self.prelude(x, mask)
19
+ recurrent_state = torch.zeros_like(hidden)
20
+ for _ in range(num_iterations):
21
+ hidden, recurrent_state = self.recurrent(hidden, recurrent_state, mask)
22
+ return self.coda(hidden)
Model/model.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ import torch.nn.functional as F
4
+ from typing import Optional, Tuple
5
+ import math
6
+ from transformers import PretrainedConfig, PreTrainedModel
7
+ from Model.latent_Recurrent import LatentRecurrentDepthLM
8
+
9
+ # Configuration for the Latent Recurrent Depth Model
10
+ class LatentRecurrentDepthConfig(PretrainedConfig):
11
+ model_type = "latent_recurrent_depth"
12
+
13
+ def __init__(self, vocab_size=50257, d_model=768, num_heads=12, dropout=0.1, **kwargs):
14
+ super().__init__(**kwargs)
15
+ self.vocab_size = vocab_size
16
+ self.d_model = d_model
17
+ self.num_heads = num_heads
18
+ self.dropout = dropout
19
+
20
+
21
+ # Hugging Face-Compatible Model Wrapper
22
+ class LatentRecurrentDepthModel(PreTrainedModel):
23
+ config_class = LatentRecurrentDepthConfig
24
+ base_model_prefix = "latent_recurrent_depth"
25
+
26
+ def __init__(self, config: LatentRecurrentDepthConfig):
27
+ super().__init__(config)
28
+ self.latent_model = LatentRecurrentDepthLM(config.vocab_size, config.d_model, config.num_heads, config.dropout)
29
+ self.init_weights()
30
+
31
+ def forward(self, input_ids: torch.Tensor, num_iterations: int, mask: Optional[torch.Tensor] = None) -> torch.Tensor:
32
+ return self.latent_model(input_ids, num_iterations, mask)
33
+
34
+ def generate(
35
+ self,
36
+ input_ids: torch.Tensor,
37
+ max_length: int = 20,
38
+ num_iterations: int = 3,
39
+ temperature: float = 1.0,
40
+ top_k: Optional[int] = 50,
41
+ ) -> torch.Tensor:
42
+ """
43
+ Generate a sequence of tokens given input_ids.
44
+
45
+ Args:
46
+ input_ids: torch.Tensor of shape (batch, seq_length) containing the prompt.
47
+ max_length: The number of tokens to generate.
48
+ num_iterations: The number of recurrent iterations to use in each forward pass.
49
+ temperature: Temperature for scaling logits.
50
+ top_k: If set, only sample from the top k tokens.
51
+
52
+ Returns:
53
+ generated: torch.Tensor containing the generated sequence.
54
+ """
55
+ generated = input_ids.clone()
56
+ self.eval()
57
+ with torch.no_grad():
58
+ for _ in range(max_length):
59
+ # Get logits from the model for the current sequence.
60
+ logits = self.forward(generated, num_iterations=num_iterations)
61
+ # Use only the logits for the last token in the sequence.
62
+ next_token_logits = logits[:, -1, :] / temperature
63
+ if top_k is not None:
64
+ # Top-k filtering
65
+ top_k_logits, top_k_indices = torch.topk(next_token_logits, top_k)
66
+ probabilities = F.softmax(top_k_logits, dim=-1)
67
+ next_token = top_k_indices.gather(-1, torch.multinomial(probabilities, num_samples=1))
68
+ else:
69
+ probabilities = F.softmax(next_token_logits, dim=-1)
70
+ next_token = torch.multinomial(probabilities, num_samples=1)
71
+ generated = torch.cat([generated, next_token], dim=1)
72
+ # Optionally, break if the EOS token is generated.
73
+ if next_token.item() == self.config.eos_token_id:
74
+ break
75
+ return generated
Model/multi_head_Attention.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ import torch.nn.functional as F
4
+ from typing import Optional, Tuple
5
+
6
+ # Multi-Head Attention Mechanism
7
+ class MultiHeadAttention(nn.Module):
8
+ def __init__(self, d_model: int, num_heads: int, dropout: float = 0.1):
9
+ super().__init__()
10
+ assert d_model % num_heads == 0
11
+
12
+ self.d_model = d_model
13
+ self.num_heads = num_heads
14
+ self.head_dim = d_model // num_heads
15
+
16
+ self.q_proj = nn.Linear(d_model, d_model)
17
+ self.k_proj = nn.Linear(d_model, d_model)
18
+ self.v_proj = nn.Linear(d_model, d_model)
19
+ self.o_proj = nn.Linear(d_model, d_model)
20
+
21
+ self.dropout = nn.Dropout(dropout)
22
+
23
+ def forward(self, x: torch.Tensor, mask: Optional[torch.Tensor] = None) -> torch.Tensor:
24
+ batch_size, seq_len, d_model = x.shape
25
+
26
+ # Project and reshape for multi-head attention
27
+ q = self.q_proj(x).reshape(batch_size, seq_len, self.num_heads, self.head_dim)
28
+ k = self.k_proj(x).reshape(batch_size, seq_len, self.num_heads, self.head_dim)
29
+ v = self.v_proj(x).reshape(batch_size, seq_len, self.num_heads, self.head_dim)
30
+
31
+ # Transpose for attention computation
32
+ q, k, v = q.transpose(1, 2), k.transpose(1, 2), v.transpose(1, 2)
33
+
34
+ # Compute attention scores
35
+ scores = torch.matmul(q, k.transpose(-2, -1)) / math.sqrt(self.head_dim)
36
+ if mask is not None:
37
+ scores = scores.masked_fill(mask == 0, float('-inf'))
38
+
39
+ attn_weights = F.softmax(scores, dim=-1)
40
+ attn_weights = self.dropout(attn_weights)
41
+
42
+ # Apply attention to values
43
+ out = torch.matmul(attn_weights, v).transpose(1, 2).reshape(batch_size, seq_len, d_model)
44
+ return self.o_proj(out)
Model/prelude_Block.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ import torch.nn.functional as F
4
+ from typing import Optional, Tuple
5
+ from Model.multi_head_Attention import MultiHeadAttention
6
+
7
+
8
+ # Prelude Block (Initial Processing)
9
+ class PreludeBlock(nn.Module):
10
+ def __init__(self, vocab_size: int, d_model: int, num_heads: int, dropout: float = 0.1):
11
+ super().__init__()
12
+ self.token_embedding = nn.Embedding(vocab_size, d_model)
13
+ self.pos_encoding = nn.Parameter(torch.zeros(1, 1024, d_model))
14
+ self.attention = MultiHeadAttention(d_model, num_heads, dropout)
15
+ self.norm1, self.norm2 = nn.LayerNorm(d_model), nn.LayerNorm(d_model)
16
+ self.feed_forward = nn.Sequential(
17
+ nn.Linear(d_model, 4 * d_model),
18
+ nn.GELU(),
19
+ nn.Linear(4 * d_model, d_model),
20
+ nn.Dropout(dropout)
21
+ )
22
+
23
+ def forward(self, x: torch.Tensor, mask: Optional[torch.Tensor] = None) -> torch.Tensor:
24
+ seq_len = x.size(1)
25
+ x = self.token_embedding(x) + self.pos_encoding[:, :seq_len, :]
26
+ attended = self.attention(self.norm1(x), mask)
27
+ x = x + attended
28
+ return x + self.feed_forward(self.norm2(x))
Model/recurrent_Block.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ import torch.nn.functional as F
4
+ from typing import Optional, Tuple
5
+ from Model.multi_head_Attention import MultiHeadAttention
6
+
7
+ # Recurrent Block (Processing Over Time)
8
+ class RecurrentBlock(nn.Module):
9
+ def __init__(self, d_model: int, num_heads: int, dropout: float = 0.1):
10
+ super().__init__()
11
+ self.attention = MultiHeadAttention(d_model, num_heads, dropout)
12
+ self.norm1, self.norm2 = nn.LayerNorm(d_model), nn.LayerNorm(d_model)
13
+ self.feed_forward = nn.Sequential(
14
+ nn.Linear(d_model, 4 * d_model),
15
+ nn.GELU(),
16
+ nn.Linear(4 * d_model, d_model),
17
+ nn.Dropout(dropout)
18
+ )
19
+ self.state_proj = nn.Linear(d_model, d_model)
20
+
21
+ def forward(self, x: torch.Tensor, recurrent_state: torch.Tensor, mask: Optional[torch.Tensor] = None) -> Tuple[torch.Tensor, torch.Tensor]:
22
+ recurrent_state = self.state_proj(recurrent_state)
23
+ x = x + recurrent_state
24
+ attended = self.attention(self.norm1(x), mask)
25
+ return x + attended + self.feed_forward(self.norm2(x)), x