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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
import yaml
|
| 4 |
+
from transformers import GPT2Config, PreTrainedTokenizerFast
|
| 5 |
+
from huggingface_hub import HfApi, upload_file
|
| 6 |
+
import os
|
| 7 |
+
import json
|
| 8 |
+
import sentencepiece as spm
|
| 9 |
+
from tokenizers import SentencePieceBPETokenizer
|
| 10 |
+
|
| 11 |
+
# Define the TinyLlama Model
|
| 12 |
+
class TinyLlama(nn.Module):
|
| 13 |
+
def __init__(self, config):
|
| 14 |
+
super(TinyLlama, self).__init__()
|
| 15 |
+
self.embedding = nn.Embedding(config.vocab_size, config.n_embd)
|
| 16 |
+
self.transformer_blocks = nn.ModuleList([
|
| 17 |
+
nn.TransformerEncoderLayer(
|
| 18 |
+
d_model=config.n_embd,
|
| 19 |
+
nhead=config.n_head,
|
| 20 |
+
dim_feedforward=config.hidden_dim
|
| 21 |
+
)
|
| 22 |
+
for _ in range(config.n_layer)
|
| 23 |
+
])
|
| 24 |
+
self.output_layer = nn.Linear(config.n_embd, config.vocab_size)
|
| 25 |
+
|
| 26 |
+
def forward(self, x):
|
| 27 |
+
x = self.embedding(x)
|
| 28 |
+
for block in self.transformer_blocks:
|
| 29 |
+
x = block(x)
|
| 30 |
+
x = self.output_layer(x)
|
| 31 |
+
return x
|
| 32 |
+
|
| 33 |
+
# Load Model Configuration from YAML file
|
| 34 |
+
model_config_path = "/home/jax/out/custom-model/final/model_config.yaml"
|
| 35 |
+
with open(model_config_path, 'r') as file:
|
| 36 |
+
config_data = yaml.safe_load(file)
|
| 37 |
+
|
| 38 |
+
# Create Model Configuration
|
| 39 |
+
config = GPT2Config(
|
| 40 |
+
vocab_size=config_data.get("vocab_size", 32000), # Adjust to match TinyLlama's vocab size
|
| 41 |
+
n_embd=config_data.get("n_embd", 2048), # Embedding size for TinyLlama
|
| 42 |
+
n_layer=config_data.get("n_layer", 24), # Number of transformer layers
|
| 43 |
+
n_head=config_data.get("n_head", 16), # Number of attention heads
|
| 44 |
+
hidden_dim=config_data.get("hidden_dim", 8192) # Feedforward layer dimension
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
# Instantiate the TinyLlama Model
|
| 48 |
+
model = TinyLlama(config)
|
| 49 |
+
|
| 50 |
+
# Load Weights from the .pth file
|
| 51 |
+
model_weights_path = "/home/jax/out/custom-model/final/lit_model.pth"
|
| 52 |
+
model_weights = torch.load(model_weights_path, map_location=torch.device('cpu'), weights_only=True)
|
| 53 |
+
model.load_state_dict(model_weights, strict=False) # strict=False to allow for minor mismatches
|
| 54 |
+
|
| 55 |
+
# Directory to Save the Model and Tokenizer for Hugging Face
|
| 56 |
+
model_dir = "./huggingface_tinyllama"
|
| 57 |
+
os.makedirs(model_dir, exist_ok=True)
|
| 58 |
+
|
| 59 |
+
# Save the Model Weights
|
| 60 |
+
model_weights_save_path = os.path.join(model_dir, "pytorch_model.bin")
|
| 61 |
+
torch.save(model.state_dict(), model_weights_save_path)
|
| 62 |
+
|
| 63 |
+
# Save the Configuration in JSON format
|
| 64 |
+
config_save_path = os.path.join(model_dir, "config.json")
|
| 65 |
+
with open(config_save_path, 'w') as f:
|
| 66 |
+
json.dump(config.to_dict(), f)
|
| 67 |
+
|
| 68 |
+
# Load and Convert the SentencePiece model to Hugging Face-compatible format
|
| 69 |
+
# Load the SentencePiece model
|
| 70 |
+
tokenizer_path = "/home/jax/out/custom-model/final/tokenizer.model"
|
| 71 |
+
sp_tokenizer = spm.SentencePieceProcessor()
|
| 72 |
+
sp_tokenizer.load(tokenizer_path)
|
| 73 |
+
|
| 74 |
+
# Create a new tokenizer using the tokenizers library
|
| 75 |
+
hf_tokenizer = SentencePieceBPETokenizer(
|
| 76 |
+
vocab=tokenizer_path, # Load SentencePiece model
|
| 77 |
+
add_prefix_space=True
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
# Train the tokenizer if needed (optional step, depending on your model and usage)
|
| 81 |
+
# hf_tokenizer.train([tokenizer_path])
|
| 82 |
+
|
| 83 |
+
# Set special tokens
|
| 84 |
+
hf_tokenizer.add_special_tokens(["<unk>", "<pad>", "<s>", "</s>"])
|
| 85 |
+
|
| 86 |
+
# Wrap the tokenizer with PreTrainedTokenizerFast for Hugging Face compatibility
|
| 87 |
+
hf_tokenizer = PreTrainedTokenizerFast.from_pretrained(tokenizer_path)
|
| 88 |
+
|
| 89 |
+
# Save the Hugging Face tokenizer
|
| 90 |
+
hf_tokenizer.save_pretrained(model_dir)
|
| 91 |
+
|
| 92 |
+
# Push to Hugging Face Hub
|
| 93 |
+
api.create_repo(repo_id=repo_id, token=os.getenv('HUGGINGFACE_API_TOKEN'), exist_ok=True)
|
| 94 |
+
|
| 95 |
+
# Create a Model Card for the Model
|
| 96 |
+
model_card = """
|
| 97 |
+
---
|
| 98 |
+
language: en
|
| 99 |
+
tags:
|
| 100 |
+
- tinyllama
|
| 101 |
+
- language-model
|
| 102 |
+
- chat
|
| 103 |
+
license: apache-2.0
|
| 104 |
+
---
|
| 105 |
+
|
| 106 |
+
# TinyLlama 1.1B Chat Model
|
| 107 |
+
|
| 108 |
+
## Model Description
|
| 109 |
+
TinyLlama is a lightweight LLaMA-based model with 1.1 billion parameters, designed to perform well on conversational and text generation tasks. It has been fine-tuned specifically for chat applications, providing coherent and context-aware responses.
|
| 110 |
+
|
| 111 |
+
## Training Data
|
| 112 |
+
The model was trained on a diverse dataset, including web text, books, and conversational data, to make it capable of handling a wide range of language styles.
|
| 113 |
+
|
| 114 |
+
## Usage
|
| 115 |
+
You can use this model for conversational AI, text completion, or other natural language generation tasks. Here’s a quick example:
|
| 116 |
+
|
| 117 |
+
```python
|
| 118 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 119 |
+
|
| 120 |
+
tokenizer = AutoTokenizer.from_pretrained("jacksonstrut/tinyllama-1.1B-chat")
|
| 121 |
+
model = AutoModelForCausalLM.from_pretrained("jacksonstrut/tinyllama-1.1B-chat")
|
| 122 |
+
|
| 123 |
+
input_ids = tokenizer("Hello, how are you?", return_tensors="pt").input_ids
|
| 124 |
+
output = model.generate(input_ids)
|
| 125 |
+
print(tokenizer.decode(output[0]))
|
| 126 |
+
```
|
| 127 |
+
|
| 128 |
+
## Limitations
|
| 129 |
+
- The model may produce biased or inappropriate outputs as it is trained on general datasets from the internet.
|
| 130 |
+
- It may not be suitable for all applications, especially those requiring factual accuracy.
|
| 131 |
+
|
| 132 |
+
## License
|
| 133 |
+
This model is licensed under the Apache 2.0 License.
|
| 134 |
+
"""
|
| 135 |
+
|
| 136 |
+
# Save the Model Card to README.md
|
| 137 |
+
readme_path = os.path.join(model_dir, "README.md")
|
| 138 |
+
with open(readme_path, 'w') as f:
|
| 139 |
+
f.write(model_card)
|
| 140 |
+
|
| 141 |
+
# Upload the Model Card to Hugging Face Hub
|
| 142 |
+
|
| 143 |
+
# Upload files to Hugging Face Hub
|
| 144 |
+
upload_file(
|
| 145 |
+
path_or_fileobj=model_weights_save_path,
|
| 146 |
+
path_in_repo="pytorch_model.bin",
|
| 147 |
+
repo_id=repo_id,
|
| 148 |
+
token=os.getenv('HUGGINGFACE_API_TOKEN')
|
| 149 |
+
)
|
| 150 |
+
upload_file(
|
| 151 |
+
path_or_fileobj=config_save_path,
|
| 152 |
+
path_in_repo="config.json",
|
| 153 |
+
repo_id=repo_id,
|
| 154 |
+
token=os.getenv('HUGGINGFACE_API_TOKEN')
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
# Upload the tokenizer files
|
| 158 |
+
upload_file(
|
| 159 |
+
path_or_fileobj=os.path.join(model_dir, "tokenizer.model"),
|
| 160 |
+
path_in_repo="tokenizer.model",
|
| 161 |
+
repo_id=repo_id,
|
| 162 |
+
token=os.getenv('HUGGINGFACE_API_TOKEN')
|
| 163 |
+
)
|
| 164 |
+
upload_file(
|
| 165 |
+
path_or_fileobj=os.path.join(model_dir, "tokenizer_config.json"),
|
| 166 |
+
path_in_repo="tokenizer_config.json",
|
| 167 |
+
repo_id=repo_id,
|
| 168 |
+
token=os.getenv('HUGGINGFACE_API_TOKEN')
|
| 169 |
+
)
|