Mattimax commited on
Commit
14e68fe
·
verified ·
1 Parent(s): 96e6c88

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +135 -18
README.md CHANGED
@@ -77,25 +77,142 @@ pip install transformers torch
77
  ## Esempio di utilizzo in Python
78
 
79
  ```python
80
- from transformers import AutoModelForCausalLM, AutoTokenizer
81
  import torch
82
-
83
- # Carica modello e tokenizer
84
- model_name = "Mattimax/PicoDAC"
85
- tokenizer = AutoTokenizer.from_pretrained(model_name)
86
- model = AutoModelForCausalLM.from_pretrained(model_name)
87
-
88
- # Input di esempio
89
- input_text = "Ciao, come va?"
90
- inputs = tokenizer(input_text, return_tensors="pt")
91
-
92
- # Generazione
93
- with torch.no_grad():
94
- outputs = model.generate(**inputs, max_length=64, do_sample=True, temperature=0.8)
95
-
96
- # Decodifica
97
- response = tokenizer.decode(outputs[0], skip_special_tokens=True)
98
- print(response)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  ```
100
 
101
  **Suggerimenti pratici:**
 
77
  ## Esempio di utilizzo in Python
78
 
79
  ```python
 
80
  import torch
81
+ import torch.nn as nn
82
+ import torch.nn.functional as F
83
+ from huggingface_hub import hf_hub_download
84
+ from safetensors.torch import load_file
85
+ from transformers import PreTrainedTokenizerFast
86
+ import os
87
+
88
+ # ----------------------------
89
+ # Config modello
90
+ # ----------------------------
91
+ VOCAB_SIZE = 1920
92
+ D_MODEL = 240
93
+ N_LAYERS = 6
94
+ N_HEADS = 6
95
+ D_HEAD = 40
96
+ D_FF = 960
97
+ N_CTX = 64
98
+
99
+ device = "cuda" if torch.cuda.is_available() else "cpu"
100
+ print(f"Usando device: {device}")
101
+
102
+ # ----------------------------
103
+ # Scarica tokenizer e modello da HF
104
+ # ----------------------------
105
+ repo_id = "Mattimax/PicoDAC-IT"
106
+
107
+ tokenizer_path = hf_hub_download(repo_id, "tokenizer.json")
108
+ model_path = hf_hub_download(repo_id, "model.safetensors")
109
+ # opzionale: scales_path = hf_hub_download(repo_id, "best/scales.json")
110
+
111
+ # ----------------------------
112
+ # Carica tokenizer
113
+ # ----------------------------
114
+ tokenizer = PreTrainedTokenizerFast(tokenizer_file=tokenizer_path)
115
+ tokenizer.add_special_tokens({
116
+ "pad_token": "<PAD>",
117
+ "bos_token": "<BOS>",
118
+ "eos_token": "<EOS>",
119
+ "sep_token": "<SEP>",
120
+ "unk_token": "<UNK>"
121
+ })
122
+
123
+ # ----------------------------
124
+ # RMSNorm
125
+ # ----------------------------
126
+ class RMSNorm(nn.Module):
127
+ def __init__(self, dim, eps=1e-8):
128
+ super().__init__()
129
+ self.eps = eps
130
+ self.weight = nn.Parameter(torch.ones(dim))
131
+
132
+ def forward(self, x):
133
+ norm = x.pow(2).mean(-1, keepdim=True).add(self.eps).sqrt()
134
+ return x / norm * self.weight
135
+
136
+ # ----------------------------
137
+ # Transformer Block
138
+ # ----------------------------
139
+ class TransformerBlock(nn.Module):
140
+ def __init__(self):
141
+ super().__init__()
142
+ self.ln1 = RMSNorm(D_MODEL)
143
+ self.ln2 = RMSNorm(D_MODEL)
144
+ self.attn = nn.MultiheadAttention(D_MODEL, N_HEADS, batch_first=True)
145
+ self.ffn = nn.Sequential(
146
+ nn.Linear(D_MODEL, D_FF),
147
+ nn.SiLU(),
148
+ nn.Linear(D_FF, D_MODEL)
149
+ )
150
+
151
+ def forward(self, x, mask=None):
152
+ attn_out, _ = self.attn(self.ln1(x), self.ln1(x), self.ln1(x), attn_mask=mask)
153
+ x = x + attn_out
154
+ x = x + self.ffn(self.ln2(x))
155
+ return x
156
+
157
+ # ----------------------------
158
+ # TinyGPT
159
+ # ----------------------------
160
+ class TinyGPT(nn.Module):
161
+ def __init__(self):
162
+ super().__init__()
163
+ self.tok_emb = nn.Embedding(VOCAB_SIZE, D_MODEL)
164
+ self.pos_emb = nn.Embedding(N_CTX, D_MODEL)
165
+ self.layers = nn.ModuleList([TransformerBlock() for _ in range(N_LAYERS)])
166
+ self.ln_f = RMSNorm(D_MODEL)
167
+ self.head = nn.Linear(D_MODEL, VOCAB_SIZE, bias=False)
168
+ self.head.weight = self.tok_emb.weight
169
+
170
+ def forward(self, idx):
171
+ B, T = idx.shape
172
+ pos = torch.arange(T, device=idx.device).unsqueeze(0)
173
+ x = self.tok_emb(idx) + self.pos_emb(pos)
174
+ mask = torch.triu(torch.ones(T, T, device=idx.device) * float('-inf'), diagonal=1)
175
+ for layer in self.layers:
176
+ x = layer(x, mask=mask)
177
+ x = self.ln_f(x)
178
+ logits = self.head(x)
179
+ return logits
180
+
181
+ # ----------------------------
182
+ # Carica pesi
183
+ # ----------------------------
184
+ state_dict = load_file(model_path)
185
+ model = TinyGPT()
186
+ model.load_state_dict(state_dict, strict=False)
187
+ model.to(device)
188
+ model.eval()
189
+
190
+ # ----------------------------
191
+ # Funzione generazione
192
+ # ----------------------------
193
+ def generate(prompt, max_new_tokens=50, temperature=0.7):
194
+ model_input = tokenizer.encode(prompt, return_tensors="pt").to(device)
195
+ for _ in range(max_new_tokens):
196
+ logits = model(model_input)
197
+ next_token_logits = logits[0, -1, :] / temperature
198
+ probs = F.softmax(next_token_logits, dim=-1)
199
+ next_token = torch.multinomial(probs, num_samples=1)
200
+ model_input = torch.cat([model_input, next_token.unsqueeze(0)], dim=1)
201
+ if next_token.item() == tokenizer.eos_token_id:
202
+ break
203
+ return tokenizer.decode(model_input[0], skip_special_tokens=True)
204
+
205
+ # ----------------------------
206
+ # Loop chat
207
+ # ----------------------------
208
+ print("Chat PicoDAC-IT (scrivi 'exit' per uscire)")
209
+ while True:
210
+ user_input = input("Tu: ").strip()
211
+ if user_input.lower() in ["exit", "quit"]:
212
+ print("Chiusura chat. Ciao!")
213
+ break
214
+ response = generate(user_input)
215
+ print(f"PicoDAC-IT: {response}")
216
  ```
217
 
218
  **Suggerimenti pratici:**