gpjt commited on
Commit
b936caf
·
verified ·
1 Parent(s): 4ef5f9a

Upload model

Browse files
Files changed (6) hide show
  1. README.md +199 -0
  2. config.json +21 -0
  3. configuration_gpjtgpt2.py +11 -0
  4. gpt.py +188 -0
  5. model.safetensors +3 -0
  6. modeling_gpjtgpt2.py +18 -0
README.md ADDED
@@ -0,0 +1,199 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ library_name: transformers
3
+ tags: []
4
+ ---
5
+
6
+ # Model Card for Model ID
7
+
8
+ <!-- Provide a quick summary of what the model is/does. -->
9
+
10
+
11
+
12
+ ## Model Details
13
+
14
+ ### Model Description
15
+
16
+ <!-- Provide a longer summary of what this model is. -->
17
+
18
+ This is the model card of a 🤗 transformers model that has been pushed on the Hub. This model card has been automatically generated.
19
+
20
+ - **Developed by:** [More Information Needed]
21
+ - **Funded by [optional]:** [More Information Needed]
22
+ - **Shared by [optional]:** [More Information Needed]
23
+ - **Model type:** [More Information Needed]
24
+ - **Language(s) (NLP):** [More Information Needed]
25
+ - **License:** [More Information Needed]
26
+ - **Finetuned from model [optional]:** [More Information Needed]
27
+
28
+ ### Model Sources [optional]
29
+
30
+ <!-- Provide the basic links for the model. -->
31
+
32
+ - **Repository:** [More Information Needed]
33
+ - **Paper [optional]:** [More Information Needed]
34
+ - **Demo [optional]:** [More Information Needed]
35
+
36
+ ## Uses
37
+
38
+ <!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
39
+
40
+ ### Direct Use
41
+
42
+ <!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
43
+
44
+ [More Information Needed]
45
+
46
+ ### Downstream Use [optional]
47
+
48
+ <!-- This section is for the model use when fine-tuned for a task, or when plugged into a larger ecosystem/app -->
49
+
50
+ [More Information Needed]
51
+
52
+ ### Out-of-Scope Use
53
+
54
+ <!-- This section addresses misuse, malicious use, and uses that the model will not work well for. -->
55
+
56
+ [More Information Needed]
57
+
58
+ ## Bias, Risks, and Limitations
59
+
60
+ <!-- This section is meant to convey both technical and sociotechnical limitations. -->
61
+
62
+ [More Information Needed]
63
+
64
+ ### Recommendations
65
+
66
+ <!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->
67
+
68
+ Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
69
+
70
+ ## How to Get Started with the Model
71
+
72
+ Use the code below to get started with the model.
73
+
74
+ [More Information Needed]
75
+
76
+ ## Training Details
77
+
78
+ ### Training Data
79
+
80
+ <!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->
81
+
82
+ [More Information Needed]
83
+
84
+ ### Training Procedure
85
+
86
+ <!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
87
+
88
+ #### Preprocessing [optional]
89
+
90
+ [More Information Needed]
91
+
92
+
93
+ #### Training Hyperparameters
94
+
95
+ - **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
96
+
97
+ #### Speeds, Sizes, Times [optional]
98
+
99
+ <!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
100
+
101
+ [More Information Needed]
102
+
103
+ ## Evaluation
104
+
105
+ <!-- This section describes the evaluation protocols and provides the results. -->
106
+
107
+ ### Testing Data, Factors & Metrics
108
+
109
+ #### Testing Data
110
+
111
+ <!-- This should link to a Dataset Card if possible. -->
112
+
113
+ [More Information Needed]
114
+
115
+ #### Factors
116
+
117
+ <!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
118
+
119
+ [More Information Needed]
120
+
121
+ #### Metrics
122
+
123
+ <!-- These are the evaluation metrics being used, ideally with a description of why. -->
124
+
125
+ [More Information Needed]
126
+
127
+ ### Results
128
+
129
+ [More Information Needed]
130
+
131
+ #### Summary
132
+
133
+
134
+
135
+ ## Model Examination [optional]
136
+
137
+ <!-- Relevant interpretability work for the model goes here -->
138
+
139
+ [More Information Needed]
140
+
141
+ ## Environmental Impact
142
+
143
+ <!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
144
+
145
+ Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
146
+
147
+ - **Hardware Type:** [More Information Needed]
148
+ - **Hours used:** [More Information Needed]
149
+ - **Cloud Provider:** [More Information Needed]
150
+ - **Compute Region:** [More Information Needed]
151
+ - **Carbon Emitted:** [More Information Needed]
152
+
153
+ ## Technical Specifications [optional]
154
+
155
+ ### Model Architecture and Objective
156
+
157
+ [More Information Needed]
158
+
159
+ ### Compute Infrastructure
160
+
161
+ [More Information Needed]
162
+
163
+ #### Hardware
164
+
165
+ [More Information Needed]
166
+
167
+ #### Software
168
+
169
+ [More Information Needed]
170
+
171
+ ## Citation [optional]
172
+
173
+ <!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
174
+
175
+ **BibTeX:**
176
+
177
+ [More Information Needed]
178
+
179
+ **APA:**
180
+
181
+ [More Information Needed]
182
+
183
+ ## Glossary [optional]
184
+
185
+ <!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
186
+
187
+ [More Information Needed]
188
+
189
+ ## More Information [optional]
190
+
191
+ [More Information Needed]
192
+
193
+ ## Model Card Authors [optional]
194
+
195
+ [More Information Needed]
196
+
197
+ ## Model Card Contact
198
+
199
+ [More Information Needed]
config.json ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "GPJTGPT2Model"
4
+ ],
5
+ "auto_map": {
6
+ "AutoConfig": "configuration_gpjtgpt2.GPJTGPT2Config",
7
+ "AutoModel": "modeling_gpjtgpt2.GPJTGPT2Model"
8
+ },
9
+ "cfg": {
10
+ "context_length": 1024,
11
+ "drop_rate": 0.1,
12
+ "emb_dim": 768,
13
+ "n_heads": 12,
14
+ "n_layers": 12,
15
+ "qkv_bias": false,
16
+ "vocab_size": 50257
17
+ },
18
+ "dtype": "float32",
19
+ "model_type": "gpjtgpt2",
20
+ "transformers_version": "4.57.6"
21
+ }
configuration_gpjtgpt2.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import PretrainedConfig
2
+
3
+
4
+ class GPJTGPT2Config(PretrainedConfig):
5
+
6
+ model_type = "gpjtgpt2"
7
+
8
+ def __init__(self, cfg=None, **kwargs):
9
+ self.cfg = cfg
10
+
11
+ super().__init__(**kwargs)
gpt.py ADDED
@@ -0,0 +1,188 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Based on code from:
2
+ # "Build a Large Language Model (from Scratch)"
3
+ # Copyright 2023-2025 Sebastian Raschka
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Modifications copyright 2025 Giles Thomas
10
+
11
+ import torch
12
+ import torch.nn as nn
13
+
14
+
15
+ class MultiHeadAttention(nn.Module):
16
+
17
+ def __init__(
18
+ self,
19
+ d_in, d_out,
20
+ context_length,
21
+ dropout,
22
+ num_heads,
23
+ qkv_bias=False
24
+ ):
25
+ super().__init__()
26
+
27
+ assert d_out % num_heads == 0, "d_out must be divisible by num_heads"
28
+
29
+ self.d_out = d_out
30
+ self.num_heads = num_heads
31
+ self.head_dim = d_out // num_heads
32
+ self.W_query = nn.Linear(d_in, d_out, bias=qkv_bias)
33
+ self.W_key = nn.Linear(d_in, d_out, bias=qkv_bias)
34
+ self.W_value = nn.Linear(d_in, d_out, bias=qkv_bias)
35
+ self.out_proj = nn.Linear(d_out, d_out)
36
+ self.dropout = nn.Dropout(dropout)
37
+ self.register_buffer(
38
+ "mask",
39
+ torch.triu(torch.ones(context_length, context_length), diagonal=1)
40
+ )
41
+
42
+
43
+ def forward(self, x):
44
+ b, num_tokens, d_in = x.shape
45
+
46
+ keys = self.W_key(x)
47
+ queries = self.W_query(x)
48
+ values = self.W_value(x)
49
+
50
+ keys = keys.view(b, num_tokens, self.num_heads, self.head_dim)
51
+ queries = queries.view(b, num_tokens, self.num_heads, self.head_dim)
52
+ values = values.view(b, num_tokens, self.num_heads, self.head_dim)
53
+
54
+ keys = keys.transpose(1, 2)
55
+ queries = queries.transpose(1, 2)
56
+ values = values.transpose(1, 2)
57
+
58
+ attn_scores = queries @ keys.transpose(2, 3)
59
+ mask_bool = self.mask.bool()[:num_tokens, :num_tokens]
60
+
61
+ attn_scores.masked_fill_(mask_bool, -torch.inf)
62
+
63
+ attn_weights = torch.softmax(attn_scores / keys.shape[-1] ** 0.5, dim=-1)
64
+ attn_weights = self.dropout(attn_weights)
65
+
66
+ context_vec = (attn_weights @ values).transpose(1, 2)
67
+
68
+ context_vec = context_vec.contiguous().view(
69
+ b, num_tokens, self.d_out
70
+ )
71
+ context_vec = self.out_proj(context_vec)
72
+
73
+ return context_vec
74
+
75
+
76
+
77
+
78
+ class GELU(nn.Module):
79
+
80
+ def forward(self, x):
81
+ return 0.5 * x * (1 + torch.tanh(torch.sqrt(torch.tensor(2.0 / torch.pi)) * (x + 0.044715 * torch.pow(x, 3))))
82
+
83
+
84
+
85
+ class FeedForward(nn.Module):
86
+
87
+ def __init__(self, cfg):
88
+ super().__init__()
89
+ self.layers = nn.Sequential(
90
+ nn.Linear(cfg["emb_dim"], cfg["emb_dim"] * 4),
91
+ GELU(),
92
+ nn.Linear(cfg["emb_dim"] * 4, cfg["emb_dim"])
93
+ )
94
+
95
+
96
+ def forward(self, x):
97
+ return self.layers(x)
98
+
99
+
100
+
101
+ class LayerNorm(nn.Module):
102
+
103
+ def __init__(self, emb_dim):
104
+ super().__init__()
105
+
106
+ self.eps = 1e-5
107
+ self.scale = nn.Parameter(torch.ones(emb_dim))
108
+ self.shift = nn.Parameter(torch.zeros(emb_dim))
109
+
110
+
111
+ def forward(self, x):
112
+ mean = x.mean(dim=-1, keepdim=True)
113
+ var = x.var(dim=-1, keepdim=True, unbiased=False)
114
+ norm_x = (x - mean) / torch.sqrt(var + self.eps)
115
+ return self.scale * norm_x + self.shift
116
+
117
+
118
+
119
+ class TransformersBlock(nn.Module):
120
+
121
+ def __init__(self, cfg):
122
+ super().__init__()
123
+ self.att = MultiHeadAttention(
124
+ d_in=cfg["emb_dim"],
125
+ d_out=cfg["emb_dim"],
126
+ context_length=cfg["context_length"],
127
+ num_heads=cfg["n_heads"],
128
+ dropout=cfg["drop_rate"],
129
+ qkv_bias=cfg["qkv_bias"],
130
+ )
131
+ self.ff = FeedForward(cfg)
132
+ self.norm1 = LayerNorm(cfg["emb_dim"])
133
+ self.norm2 = LayerNorm(cfg["emb_dim"])
134
+ self.drop_shortcut = nn.Dropout(cfg["drop_rate"])
135
+
136
+
137
+ def forward(self, x):
138
+ shortcut = x
139
+ x = self.norm1(x)
140
+ x = self.att(x)
141
+ x = self.drop_shortcut(x)
142
+ x = x + shortcut
143
+
144
+ shortcut = x
145
+ x = self.norm2(x)
146
+ x = self.ff(x)
147
+ x = self.drop_shortcut(x)
148
+ x = x + shortcut
149
+
150
+ return x
151
+
152
+
153
+
154
+ class GPTModel(nn.Module):
155
+
156
+ def __init__(self, cfg):
157
+ super().__init__()
158
+
159
+ self.tok_emb = nn.Embedding(cfg["vocab_size"], cfg["emb_dim"])
160
+ self.pos_emb = nn.Embedding(cfg["context_length"], cfg["emb_dim"])
161
+ self.drop_emb = nn.Dropout(cfg["drop_rate"])
162
+
163
+ self.trf_blocks = nn.Sequential(
164
+ *[TransformersBlock(cfg) for _ in range(cfg["n_layers"])]
165
+ )
166
+
167
+ self.final_norm = LayerNorm(cfg["emb_dim"])
168
+
169
+ self.out_head = nn.Linear(
170
+ cfg["emb_dim"], cfg["vocab_size"], bias=False
171
+ )
172
+
173
+
174
+ def forward(self, in_idx):
175
+ batch_size, seq_len = in_idx.shape
176
+
177
+ tok_embeds = self.tok_emb(in_idx)
178
+ pos_embeds = self.pos_emb(torch.arange(seq_len, device=in_idx.device))
179
+ x = tok_embeds + pos_embeds
180
+
181
+ x = self.drop_emb(x)
182
+ x = self.trf_blocks(x)
183
+ x = self.final_norm(x)
184
+
185
+ logits = self.out_head(x)
186
+
187
+ return logits
188
+
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b1495987271249e1253f2344055b37cd5f6c3a5daba8cf11096eed00b66a504a
3
+ size 702388152
modeling_gpjtgpt2.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import PreTrainedModel
2
+
3
+ from .configuration_gpjtgpt2 import GPJTGPT2Config
4
+ from .gpt import GPTModel
5
+
6
+
7
+ class GPJTGPT2Model(PreTrainedModel):
8
+
9
+ config_class = GPJTGPT2Config
10
+
11
+
12
+ def __init__(self, config):
13
+ super().__init__(config)
14
+ self.model = GPTModel(config.cfg)
15
+
16
+
17
+ def forward(self, input_ids, **kwargs):
18
+ return self.model.forward(input_ids)