File size: 5,863 Bytes
63199ca
dea8a41
63199ca
dea8a41
 
 
 
 
 
 
 
 
 
 
 
63199ca
dea8a41
 
 
63199ca
dea8a41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63199ca
 
 
 
 
 
 
 
 
 
 
dea8a41
 
 
 
 
63199ca
 
 
dea8a41
 
63199ca
dea8a41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# Linformer-based Language Model Inference

This repository provides the code and configuration needed to use the Linformer-based language model, designed for efficient inference, leveraging the Linformer architecture to handle long sequences with reduced memory and computational overhead.

## Table of Contents

- [Introduction](#introduction)
- [Model Architecture](#model-architecture)
- [Inference Parameters](#inference-parameters)
- [Usage](#usage)
- [Model Hyperparameters](#model-hyperparameters)
- [License](#license)

## Introduction

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 can be loaded for tasks like text generation, completion, and other language modeling tasks.

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.

**Note**: This model uses a custom attention mechanism based on Linformer. Therefore, you must use the provided `LumensparkModel` and `LumensparkConfig` to load the model.

## Model Architecture

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.

### Key Features of the Architecture:

1. **Linformer Attention**: Reduces the complexity of self-attention by using low-rank projections, enabling efficient handling of long sequences.
2. **Low-Rank Linear Projections**: Compresses the self-attention mechanism and feed-forward layers to reduce memory usage and computational costs.
3. **RMSNorm**: Utilizes Root Mean Square Layer Normalization (RMSNorm) to improve stability and speed during inference.
4. **Feed-Forward Layers**: Factorized feed-forward layers to maintain model expressiveness while reducing the parameter count.
5. **Residual Connections and Dropout**: Standard techniques that ensure robustness in the model's predictions during inference.

## Inference Parameters

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:

1. **Max Length**: The maximum length of the generated sequence.
2. **Temperature**: Controls the randomness of predictions. Higher values make the output more random, while lower values make it more focused and deterministic.
3. **Top-k Sampling**: Limits sampling to the top `k` tokens in the probability distribution, ensuring that only high-probability tokens are considered.
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.
5. **Repetition Penalty**: Penalizes repeated tokens to avoid the model generating repetitive text.
6. **No Repeat N-gram Size**: Prevents the generation of repeated sequences of a certain n-gram size.

These parameters can be adjusted during inference to control the nature of the generated text and optimize it for specific tasks or preferences.

## Usage

You can easily load the model and perform inference by installing the architecture via pip. Since this model uses Linformer-based attention, you **must** install the custom package and load the `LumensparkModel` and `LumensparkConfig`, as shown in the following example:

### Installation

First, install the package:

```bash

pip install lumenspark

```

### Inference Example

```python

from lumenspark import LumensparkConfig, LumensparkModel

from transformers import AutoTokenizer



# Load the configuration and model

config = LumensparkConfig.from_pretrained("path/to/your/model/config")

model = LumensparkModel.from_pretrained("path/to/your/model", config=config)



# Load the tokenizer

tokenizer = AutoTokenizer.from_pretrained("path/to/your/tokenizer")



# Example input text

input_text = "Once upon a time"



# Tokenize the input text

inputs = tokenizer(input_text, return_tensors="pt")



# Generate text

output = model.generate(

    **inputs,

    max_length=100,        # Maximum length of the generated sequence

    temperature=0.7,       # Controls randomness in predictions

    top_k=50,              # Top-k sampling to filter high-probability tokens

    top_p=0.9,             # Nucleus sampling to control diversity

    repetition_penalty=1.2 # Penalize repetition

)



# Decode and print the generated text

print(tokenizer.decode(output[0], skip_special_tokens=True))

```

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.

## Model Hyperparameters

The model is configured with several hyperparameters that impact its architecture and performance:

- **`vocab_size`**: The size of the vocabulary.

- **`embed_dim`**: Dimensionality of the token and positional embeddings.
- **`depth`**: Number of Linformer transformer layers.
- **`heads`**: Number of attention heads for multi-head self-attention.
- **`seq_length`**: Maximum sequence length supported by the model.

- **`dropout`**: Dropout rate applied during training (not used during inference).

- **`k`**: The projection dimension for the low-rank attention mechanism.



These hyperparameters are optimized for efficient inference while handling large sequences, making the model capable of generating coherent and diverse text outputs.



## License



This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for more details.