Upload README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,100 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Linformer-based Language Model Inference on Hugging Face
|
2 |
+
|
3 |
+
This repository provides the code and configuration needed to use the Linformer-based language model hosted on Hugging Face under the model ID `anto18671/lumenspark`. The model is designed for efficient inference, leveraging the Linformer architecture to handle long sequences with reduced memory and computational overhead.
|
4 |
+
|
5 |
+
## Table of Contents
|
6 |
+
|
7 |
+
- [Introduction](#introduction)
|
8 |
+
- [Model Architecture](#model-architecture)
|
9 |
+
- [Inference Parameters](#inference-parameters)
|
10 |
+
- [Usage](#usage)
|
11 |
+
- [Model Hyperparameters](#model-hyperparameters)
|
12 |
+
- [License](#license)
|
13 |
+
|
14 |
+
## Introduction
|
15 |
+
|
16 |
+
This project provides the necessary setup and guidance to perform text generation using the Linformer-based language model, optimized for fast and efficient inference. The model is hosted on Hugging Face and can be loaded directly for tasks like text generation, completion, and other language modeling tasks.
|
17 |
+
|
18 |
+
The model has been trained on large datasets like OpenWebText and BookCorpus, but this repository focuses on inference, allowing you to generate text quickly with minimal resource consumption.
|
19 |
+
|
20 |
+
**Note**: This model uses a custom attention mechanism based on Linformer, which is not supported by Hugging Face's `AutoModel` API. Therefore, you must use the provided `LumensparkModel` and `LumensparkConfig` to load the model, as described below.
|
21 |
+
|
22 |
+
## Model Architecture
|
23 |
+
|
24 |
+
The model is based on the **Linformer Transformer**, which optimizes the standard self-attention mechanism found in traditional transformer models. Linformer reduces the quadratic complexity of self-attention, making it more efficient for long sequence processing during inference.
|
25 |
+
|
26 |
+
### Key Features of the Architecture:
|
27 |
+
|
28 |
+
1. **Linformer Attention**: Reduces the complexity of self-attention by using low-rank projections, enabling efficient handling of long sequences.
|
29 |
+
2. **Low-Rank Linear Projections**: Compresses the self-attention mechanism and feed-forward layers to reduce memory usage and computational costs.
|
30 |
+
3. **RMSNorm**: Utilizes Root Mean Square Layer Normalization (RMSNorm) to improve stability and speed during inference.
|
31 |
+
4. **Feed-Forward Layers**: Factorized feed-forward layers to maintain model expressiveness while reducing the parameter count.
|
32 |
+
5. **Residual Connections and Dropout**: Standard techniques that ensure robustness in the model's predictions during inference.
|
33 |
+
|
34 |
+
## Inference Parameters
|
35 |
+
|
36 |
+
When using the model for text generation or other inference tasks, a few parameters can be adjusted to control the quality and nature of the output:
|
37 |
+
|
38 |
+
1. **Max Length**: The maximum length of the generated sequence.
|
39 |
+
2. **Temperature**: Controls the randomness of predictions. Higher values make the output more random, while lower values make it more focused and deterministic.
|
40 |
+
3. **Top-k Sampling**: Limits sampling to the top `k` tokens in the probability distribution, ensuring that only high-probability tokens are considered.
|
41 |
+
4. **Top-p (Nucleus) Sampling**: Uses cumulative probability to filter the token pool, where only tokens contributing to the top `p` cumulative probability are considered.
|
42 |
+
5. **Repetition Penalty**: Penalizes repeated tokens to avoid the model generating repetitive text.
|
43 |
+
6. **No Repeat N-gram Size**: Prevents the generation of repeated sequences of a certain n-gram size.
|
44 |
+
|
45 |
+
These parameters can be adjusted during inference to control the nature of the generated text and optimize it for specific tasks or preferences.
|
46 |
+
|
47 |
+
## Usage
|
48 |
+
|
49 |
+
You can easily load the model and perform inference using Hugging Face's Transformers library. However, as this model uses Linformer-based attention, you **cannot** use the `AutoModel` APIs. Instead, the `LumensparkModel` and `LumensparkConfig` must be loaded, as shown in the following example:
|
50 |
+
|
51 |
+
```python
|
52 |
+
from lumenspark import LumensparkConfig, LumensparkModel
|
53 |
+
from transformers import AutoTokenizer
|
54 |
+
|
55 |
+
# Load the configuration and model from Hugging Face
|
56 |
+
config = LumensparkConfig.from_pretrained("anto18671/lumenspark")
|
57 |
+
model = LumensparkModel.from_pretrained("anto18671/lumenspark", config=config)
|
58 |
+
|
59 |
+
# Load the tokenizer
|
60 |
+
tokenizer = AutoTokenizer.from_pretrained("anto18671/lumenspark")
|
61 |
+
|
62 |
+
# Example input text
|
63 |
+
input_text = "Once upon a time"
|
64 |
+
|
65 |
+
# Tokenize the input text
|
66 |
+
inputs = tokenizer(input_text, return_tensors="pt")
|
67 |
+
|
68 |
+
# Generate text
|
69 |
+
output = model.generate(
|
70 |
+
**inputs,
|
71 |
+
max_length=100, # Maximum length of the generated sequence
|
72 |
+
temperature=0.7, # Controls randomness in predictions
|
73 |
+
top_k=50, # Top-k sampling to filter high-probability tokens
|
74 |
+
top_p=0.9, # Nucleus sampling to control diversity
|
75 |
+
repetition_penalty=1.2 # Penalize repetition
|
76 |
+
)
|
77 |
+
|
78 |
+
# Decode and print the generated text
|
79 |
+
print(tokenizer.decode(output[0], skip_special_tokens=True))
|
80 |
+
```
|
81 |
+
|
82 |
+
In this example, the model generates text based on the input prompt "Once upon a time," and you can adjust parameters like `max_length`, `temperature`, `top_k`, and `top_p` to control the output style.
|
83 |
+
|
84 |
+
## Model Hyperparameters
|
85 |
+
|
86 |
+
The model is configured with several hyperparameters that impact its architecture and performance:
|
87 |
+
|
88 |
+
- **`vocab_size`**: The size of the vocabulary.
|
89 |
+
- **`embed_dim`**: Dimensionality of the token and positional embeddings.
|
90 |
+
- **`depth`**: Number of Linformer transformer layers.
|
91 |
+
- **`heads`**: Number of attention heads for multi-head self-attention.
|
92 |
+
- **`seq_length`**: Maximum sequence length supported by the model.
|
93 |
+
- **`dropout`**: Dropout rate applied during training (not used during inference).
|
94 |
+
- **`k`**: The projection dimension for the low-rank attention mechanism.
|
95 |
+
|
96 |
+
These hyperparameters are optimized for efficient inference while handling large sequences, making the model capable of generating coherent and diverse text outputs.
|
97 |
+
|
98 |
+
## License
|
99 |
+
|
100 |
+
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for more details.
|