deepak88 commited on
Commit
8c402fc
·
verified ·
1 Parent(s): 62e9495

Upload folder using huggingface_hub

Browse files
README.md CHANGED
@@ -1,3 +1,96 @@
1
- ---
2
- license: apache-2.0
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ model-index:
3
+ - name: gemma-from-scratch
4
+ results: []
5
+ ---
6
+
7
+ # My Gemma-like Model from Scratch
8
+
9
+ This model is a custom implementation of a Gemma-like architecture, trained from scratch.
10
+
11
+ ## Training Details
12
+ - **Architecture**: A 18-layer decoder-only transformer with Grouped-Query Attention.
13
+ - **Data**: Trained on the Wikitext-2 dataset.
14
+ - **Training Script**: The training script is available on GitHub at [https://github.com/your_github_repo](https://github.com/your_github_repo).
15
+ - **Parameters**: Total trainable parameters: 330.64 million.
16
+
17
+ ### Checkpointing
18
+ The training script includes a checkpointing mechanism. It automatically saves the model's progress every 50 steps and at the end of each epoch to a file named `checkpoint.pt`. You can resume training by simply re-running the script. The final model is saved as `pytorch_model.bin`.
19
+
20
+ ### Early Stopping
21
+ To prevent overfitting, the training process includes early stopping based on the validation loss. The script will monitor the loss on a dedicated validation set and stop training if it does not improve for 2 consecutive epochs.
22
+
23
+ ## Loading and Chatting with the Model
24
+
25
+ Since this model uses a custom architecture, it requires the model class definitions from the training script to be loaded.
26
+
27
+ Here's a step-by-step guide to get started:
28
+
29
+ 1. **Install Required Libraries**:
30
+ ```bash
31
+ pip install torch huggingface-hub tokenizers
32
+ ```
33
+
34
+ 2. **Copy the Model Architecture**:
35
+ Copy the `GemmaForCausalLM` and all its required sub-classes (`RMSNorm`, `RotaryPositionalEmbedding`, `MultiHeadAttention`, `MLP`, `TransformerBlock`) from this training script into your new Python file.
36
+
37
+ 3. **Load the Model and Tokenizer**:
38
+ ```python
39
+ import torch
40
+ from huggingface_hub import hf_hub_download
41
+ from tokenizers import Tokenizer
42
+
43
+ # Define your model's hyperparameters
44
+ config = {
45
+ "vocab_size": 30000,
46
+ "hidden_size": 1024,
47
+ "num_attention_heads": 8,
48
+ "num_key_value_heads": 1,
49
+ "num_layers": 18,
50
+ "intermediate_size": 4096,
51
+ "max_position_embeddings": 32768,
52
+ "attention_dropout": 0.0,
53
+ "hidden_dropout": 0.0,
54
+ "sliding_window": 512,
55
+ "device": "cuda" if torch.cuda.is_available() else "cpu"
56
+ }
57
+
58
+ # Instantiate the custom model and load the weights
59
+ model = GemmaForCausalLM(config)
60
+ model_path = hf_hub_download(repo_id="your_username/gemma-from-scratch", filename="pytorch_model.bin")
61
+ model.load_state_dict(torch.load(model_path, map_location=config["device"]))
62
+ model.to(config["device"]).eval()
63
+
64
+ # Load the tokenizer
65
+ tokenizer_path = hf_hub_download(repo_id="your_username/gemma-from-scratch", filename="tokenizer.json")
66
+ tokenizer = Tokenizer.from_file(tokenizer_path)
67
+ ```
68
+
69
+ 4. **Generate Text**:
70
+ ```python
71
+ def generate_text(model, tokenizer, prompt, max_length=50):
72
+ input_ids = tokenizer.encode(prompt).ids
73
+ input_tensor = torch.tensor(input_ids).unsqueeze(0).to(config["device"])
74
+
75
+ with torch.no_grad():
76
+ for _ in range(max_length):
77
+ logits, _ = model(input_tensor)
78
+ next_token_logits = logits[:, -1, :]
79
+ next_token = torch.argmax(next_token_logits, dim=-1).unsqueeze(0)
80
+ input_tensor = torch.cat([input_tensor, next_token], dim=-1)
81
+
82
+ # Stop if we generate the end-of-sentence token
83
+ if next_token.item() == tokenizer.token_to_id("</s>"):
84
+ break
85
+
86
+ return tokenizer.decode(input_tensor[0].tolist(), skip_special_tokens=True)
87
+
88
+ # Example usage
89
+ prompt = "The early bird catches the worm, but the second mouse gets the "
90
+ generated_text = generate_text(model, tokenizer, prompt)
91
+ print("Generated Text:")
92
+ print(generated_text)
93
+ ```
94
+
95
+ > **Note**: This model is for demonstration purposes. Its custom architecture is not directly compatible with the Hugging Face `transformers` library out-of-the-box. To use the model, you must also include the full model class definitions in your script.
96
+
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b09cd820360ea70af27bdebbb673412f9389b5e32b21363fba79e7a7a0d7313e
3
+ size 1322686639
special_tokens_map.json ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token": "<s>",
3
+ "eos_token": "</s>",
4
+ "pad_token": "<pad>",
5
+ "unk_token": "<unk>"
6
+ }
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "added_tokens_decoder": {
3
+ "0": {
4
+ "content": "<unk>",
5
+ "lstrip": false,
6
+ "normalized": false,
7
+ "rstrip": false,
8
+ "single_word": false,
9
+ "special": true
10
+ },
11
+ "1": {
12
+ "content": "<s>",
13
+ "lstrip": false,
14
+ "normalized": false,
15
+ "rstrip": false,
16
+ "single_word": false,
17
+ "special": true
18
+ },
19
+ "2": {
20
+ "content": "</s>",
21
+ "lstrip": false,
22
+ "normalized": false,
23
+ "rstrip": false,
24
+ "single_word": false,
25
+ "special": true
26
+ },
27
+ "3": {
28
+ "content": "<pad>",
29
+ "lstrip": false,
30
+ "normalized": false,
31
+ "rstrip": false,
32
+ "single_word": false,
33
+ "special": true
34
+ }
35
+ },
36
+ "bos_token": "<s>",
37
+ "clean_up_tokenization_spaces": false,
38
+ "eos_token": "</s>",
39
+ "extra_special_tokens": {},
40
+ "model_max_length": 1000000000000000019884624838656,
41
+ "pad_token": "<pad>",
42
+ "tokenizer_class": "PreTrainedTokenizerFast",
43
+ "unk_token": "<unk>"
44
+ }