Upload TextGenerationPipeline
Browse files- README.md +199 -0
- config.json +16 -0
- generation_config.json +4 -0
- model.safetensors +3 -0
- special_tokens_map.json +16 -0
- tokenizer.json +0 -0
- tokenizer_config.json +24 -0
- zzjrabbit2.py +166 -0
README.md
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
library_name: transformers
|
| 3 |
+
tags: []
|
| 4 |
+
---
|
| 5 |
+
|
| 6 |
+
# Model Card for Model ID
|
| 7 |
+
|
| 8 |
+
<!-- Provide a quick summary of what the model is/does. -->
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
## Model Details
|
| 13 |
+
|
| 14 |
+
### Model Description
|
| 15 |
+
|
| 16 |
+
<!-- Provide a longer summary of what this model is. -->
|
| 17 |
+
|
| 18 |
+
This is the model card of a 🤗 transformers model that has been pushed on the Hub. This model card has been automatically generated.
|
| 19 |
+
|
| 20 |
+
- **Developed by:** [More Information Needed]
|
| 21 |
+
- **Funded by [optional]:** [More Information Needed]
|
| 22 |
+
- **Shared by [optional]:** [More Information Needed]
|
| 23 |
+
- **Model type:** [More Information Needed]
|
| 24 |
+
- **Language(s) (NLP):** [More Information Needed]
|
| 25 |
+
- **License:** [More Information Needed]
|
| 26 |
+
- **Finetuned from model [optional]:** [More Information Needed]
|
| 27 |
+
|
| 28 |
+
### Model Sources [optional]
|
| 29 |
+
|
| 30 |
+
<!-- Provide the basic links for the model. -->
|
| 31 |
+
|
| 32 |
+
- **Repository:** [More Information Needed]
|
| 33 |
+
- **Paper [optional]:** [More Information Needed]
|
| 34 |
+
- **Demo [optional]:** [More Information Needed]
|
| 35 |
+
|
| 36 |
+
## Uses
|
| 37 |
+
|
| 38 |
+
<!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
|
| 39 |
+
|
| 40 |
+
### Direct Use
|
| 41 |
+
|
| 42 |
+
<!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
|
| 43 |
+
|
| 44 |
+
[More Information Needed]
|
| 45 |
+
|
| 46 |
+
### Downstream Use [optional]
|
| 47 |
+
|
| 48 |
+
<!-- This section is for the model use when fine-tuned for a task, or when plugged into a larger ecosystem/app -->
|
| 49 |
+
|
| 50 |
+
[More Information Needed]
|
| 51 |
+
|
| 52 |
+
### Out-of-Scope Use
|
| 53 |
+
|
| 54 |
+
<!-- This section addresses misuse, malicious use, and uses that the model will not work well for. -->
|
| 55 |
+
|
| 56 |
+
[More Information Needed]
|
| 57 |
+
|
| 58 |
+
## Bias, Risks, and Limitations
|
| 59 |
+
|
| 60 |
+
<!-- This section is meant to convey both technical and sociotechnical limitations. -->
|
| 61 |
+
|
| 62 |
+
[More Information Needed]
|
| 63 |
+
|
| 64 |
+
### Recommendations
|
| 65 |
+
|
| 66 |
+
<!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->
|
| 67 |
+
|
| 68 |
+
Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
|
| 69 |
+
|
| 70 |
+
## How to Get Started with the Model
|
| 71 |
+
|
| 72 |
+
Use the code below to get started with the model.
|
| 73 |
+
|
| 74 |
+
[More Information Needed]
|
| 75 |
+
|
| 76 |
+
## Training Details
|
| 77 |
+
|
| 78 |
+
### Training Data
|
| 79 |
+
|
| 80 |
+
<!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->
|
| 81 |
+
|
| 82 |
+
[More Information Needed]
|
| 83 |
+
|
| 84 |
+
### Training Procedure
|
| 85 |
+
|
| 86 |
+
<!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
|
| 87 |
+
|
| 88 |
+
#### Preprocessing [optional]
|
| 89 |
+
|
| 90 |
+
[More Information Needed]
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
#### Training Hyperparameters
|
| 94 |
+
|
| 95 |
+
- **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
|
| 96 |
+
|
| 97 |
+
#### Speeds, Sizes, Times [optional]
|
| 98 |
+
|
| 99 |
+
<!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
|
| 100 |
+
|
| 101 |
+
[More Information Needed]
|
| 102 |
+
|
| 103 |
+
## Evaluation
|
| 104 |
+
|
| 105 |
+
<!-- This section describes the evaluation protocols and provides the results. -->
|
| 106 |
+
|
| 107 |
+
### Testing Data, Factors & Metrics
|
| 108 |
+
|
| 109 |
+
#### Testing Data
|
| 110 |
+
|
| 111 |
+
<!-- This should link to a Dataset Card if possible. -->
|
| 112 |
+
|
| 113 |
+
[More Information Needed]
|
| 114 |
+
|
| 115 |
+
#### Factors
|
| 116 |
+
|
| 117 |
+
<!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
|
| 118 |
+
|
| 119 |
+
[More Information Needed]
|
| 120 |
+
|
| 121 |
+
#### Metrics
|
| 122 |
+
|
| 123 |
+
<!-- These are the evaluation metrics being used, ideally with a description of why. -->
|
| 124 |
+
|
| 125 |
+
[More Information Needed]
|
| 126 |
+
|
| 127 |
+
### Results
|
| 128 |
+
|
| 129 |
+
[More Information Needed]
|
| 130 |
+
|
| 131 |
+
#### Summary
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
## Model Examination [optional]
|
| 136 |
+
|
| 137 |
+
<!-- Relevant interpretability work for the model goes here -->
|
| 138 |
+
|
| 139 |
+
[More Information Needed]
|
| 140 |
+
|
| 141 |
+
## Environmental Impact
|
| 142 |
+
|
| 143 |
+
<!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
|
| 144 |
+
|
| 145 |
+
Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
|
| 146 |
+
|
| 147 |
+
- **Hardware Type:** [More Information Needed]
|
| 148 |
+
- **Hours used:** [More Information Needed]
|
| 149 |
+
- **Cloud Provider:** [More Information Needed]
|
| 150 |
+
- **Compute Region:** [More Information Needed]
|
| 151 |
+
- **Carbon Emitted:** [More Information Needed]
|
| 152 |
+
|
| 153 |
+
## Technical Specifications [optional]
|
| 154 |
+
|
| 155 |
+
### Model Architecture and Objective
|
| 156 |
+
|
| 157 |
+
[More Information Needed]
|
| 158 |
+
|
| 159 |
+
### Compute Infrastructure
|
| 160 |
+
|
| 161 |
+
[More Information Needed]
|
| 162 |
+
|
| 163 |
+
#### Hardware
|
| 164 |
+
|
| 165 |
+
[More Information Needed]
|
| 166 |
+
|
| 167 |
+
#### Software
|
| 168 |
+
|
| 169 |
+
[More Information Needed]
|
| 170 |
+
|
| 171 |
+
## Citation [optional]
|
| 172 |
+
|
| 173 |
+
<!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
|
| 174 |
+
|
| 175 |
+
**BibTeX:**
|
| 176 |
+
|
| 177 |
+
[More Information Needed]
|
| 178 |
+
|
| 179 |
+
**APA:**
|
| 180 |
+
|
| 181 |
+
[More Information Needed]
|
| 182 |
+
|
| 183 |
+
## Glossary [optional]
|
| 184 |
+
|
| 185 |
+
<!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
|
| 186 |
+
|
| 187 |
+
[More Information Needed]
|
| 188 |
+
|
| 189 |
+
## More Information [optional]
|
| 190 |
+
|
| 191 |
+
[More Information Needed]
|
| 192 |
+
|
| 193 |
+
## Model Card Authors [optional]
|
| 194 |
+
|
| 195 |
+
[More Information Needed]
|
| 196 |
+
|
| 197 |
+
## Model Card Contact
|
| 198 |
+
|
| 199 |
+
[More Information Needed]
|
config.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"ZZJRabbit2ForCausalLM"
|
| 4 |
+
],
|
| 5 |
+
"auto_map": {
|
| 6 |
+
"AutoConfig": "zzjrabbit2.ZZJRabbit2Config",
|
| 7 |
+
"AutoModelForCausalLM": "zzjrabbit2.ZZJRabbit2ForCausalLM"
|
| 8 |
+
},
|
| 9 |
+
"hidden_size": 1024,
|
| 10 |
+
"model_type": "zzjrabbit2",
|
| 11 |
+
"num_attention_heads": 8,
|
| 12 |
+
"num_layers": 12,
|
| 13 |
+
"torch_dtype": "float32",
|
| 14 |
+
"transformers_version": "4.53.0",
|
| 15 |
+
"vocab_size": 30000
|
| 16 |
+
}
|
generation_config.json
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_from_model_config": true,
|
| 3 |
+
"transformers_version": "4.53.0"
|
| 4 |
+
}
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a4eaf6a6e3d0e23f67802ba3ff210f3f10df59c61270ee0435134f03d4cf1dc8
|
| 3 |
+
size 682448056
|
special_tokens_map.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"mask_token": {
|
| 3 |
+
"content": "0",
|
| 4 |
+
"lstrip": false,
|
| 5 |
+
"normalized": false,
|
| 6 |
+
"rstrip": false,
|
| 7 |
+
"single_word": false
|
| 8 |
+
},
|
| 9 |
+
"pad_token": {
|
| 10 |
+
"content": "0",
|
| 11 |
+
"lstrip": false,
|
| 12 |
+
"normalized": false,
|
| 13 |
+
"rstrip": false,
|
| 14 |
+
"single_word": false
|
| 15 |
+
}
|
| 16 |
+
}
|
tokenizer.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"added_tokens_decoder": {
|
| 3 |
+
"15": {
|
| 4 |
+
"content": "0",
|
| 5 |
+
"lstrip": false,
|
| 6 |
+
"normalized": false,
|
| 7 |
+
"rstrip": false,
|
| 8 |
+
"single_word": false,
|
| 9 |
+
"special": true
|
| 10 |
+
}
|
| 11 |
+
},
|
| 12 |
+
"auto_map": {
|
| 13 |
+
"AutoTokenizer": [
|
| 14 |
+
"zzjrabbit2.ZZJRabbit2Tokenizer",
|
| 15 |
+
null
|
| 16 |
+
]
|
| 17 |
+
},
|
| 18 |
+
"clean_up_tokenization_spaces": false,
|
| 19 |
+
"extra_special_tokens": {},
|
| 20 |
+
"mask_token": "0",
|
| 21 |
+
"model_max_length": 1000000000000000019884624838656,
|
| 22 |
+
"pad_token": "0",
|
| 23 |
+
"tokenizer_class": "ZZJRabbit2Tokenizer"
|
| 24 |
+
}
|
zzjrabbit2.py
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Optional, Union
|
| 2 |
+
from transformers import PreTrainedTokenizer, PreTrainedModel, PretrainedConfig, GenerationMixin
|
| 3 |
+
from transformers.modeling_outputs import BaseModelOutput, CausalLMOutput
|
| 4 |
+
from tokenizers import Tokenizer
|
| 5 |
+
import torch.nn as nn
|
| 6 |
+
import torch
|
| 7 |
+
import os.path
|
| 8 |
+
import math
|
| 9 |
+
|
| 10 |
+
class ZZJRabbit2Config(PretrainedConfig):
|
| 11 |
+
model_type = "zzjrabbit2"
|
| 12 |
+
|
| 13 |
+
def __init__(self, num_layers: int = 12, num_attention_heads: int = 8, vocab_size: int = 10000, hidden_size: int = 1024, **kwargs):
|
| 14 |
+
self.num_layers = num_layers
|
| 15 |
+
self.num_attention_heads = num_attention_heads
|
| 16 |
+
self.vocab_size = vocab_size
|
| 17 |
+
self.hidden_size = hidden_size
|
| 18 |
+
assert hidden_size % num_attention_heads == 0
|
| 19 |
+
super().__init__(**kwargs)
|
| 20 |
+
|
| 21 |
+
class ZZJRabbit2PE(nn.Module):
|
| 22 |
+
def __init__(self, hidden_size: int, max_len: int = 32768):
|
| 23 |
+
super().__init__()
|
| 24 |
+
pe = torch.zeros(max_len, hidden_size)
|
| 25 |
+
position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1)
|
| 26 |
+
div_term = torch.exp(torch.arange(0, hidden_size, 2).float() * (-math.log(10000.0) / hidden_size))
|
| 27 |
+
pe[:, 0::2] = torch.sin(position * div_term)
|
| 28 |
+
pe[:, 1::2] = torch.cos(position * div_term)
|
| 29 |
+
pe = pe.unsqueeze(0).transpose(0, 1)
|
| 30 |
+
self.register_buffer("pe", pe)
|
| 31 |
+
|
| 32 |
+
def forward(self, x: torch.Tensor):
|
| 33 |
+
return x + self.pe[:x.size(0), :]
|
| 34 |
+
|
| 35 |
+
class ZZJRabbit2Attention(nn.Module):
|
| 36 |
+
def __init__(self, config: ZZJRabbit2Config):
|
| 37 |
+
super().__init__()
|
| 38 |
+
self.config = config
|
| 39 |
+
self.head_dim = config.hidden_size // config.num_attention_heads
|
| 40 |
+
self.q_proj = nn.Linear(config.hidden_size, config.hidden_size)
|
| 41 |
+
self.k_proj = nn.Linear(config.hidden_size, config.hidden_size)
|
| 42 |
+
self.v_proj = nn.Linear(config.hidden_size, config.hidden_size)
|
| 43 |
+
self.out_proj = nn.Linear(config.hidden_size, config.hidden_size)
|
| 44 |
+
self.dropout = nn.Dropout(0.1)
|
| 45 |
+
|
| 46 |
+
def forward(self, x: torch.Tensor, key_padding_mask: Optional[torch.BoolTensor] = None, attn_mask: Optional[torch.BoolTensor] = None):
|
| 47 |
+
batch_size = x.size(0)
|
| 48 |
+
Q = self.q_proj(x).view(batch_size, -1, self.config.num_attention_heads, self.head_dim).transpose(1, 2)
|
| 49 |
+
K = self.k_proj(x).view(batch_size, -1, self.config.num_attention_heads, self.head_dim).transpose(1, 2)
|
| 50 |
+
V = self.v_proj(x).view(batch_size, -1, self.config.num_attention_heads, self.head_dim).transpose(1, 2)
|
| 51 |
+
scores = torch.matmul(Q, K.transpose(-2, -1)) / torch.sqrt(torch.tensor(self.head_dim, dtype=torch.float32))
|
| 52 |
+
if key_padding_mask is not None:
|
| 53 |
+
scores = scores.masked_fill(key_padding_mask.view(batch_size, 1, 1, -1), float("-inf"))
|
| 54 |
+
if attn_mask is not None:
|
| 55 |
+
scores = scores.masked_fill(attn_mask, float("-inf"))
|
| 56 |
+
attn_weights = nn.functional.softmax(scores, dim=-1)
|
| 57 |
+
attn_weights = self.dropout(attn_weights)
|
| 58 |
+
context = torch.matmul(attn_weights, V)
|
| 59 |
+
context = context.transpose(1, 2).contiguous()
|
| 60 |
+
context = context.view(batch_size, -1, self.config.hidden_size)
|
| 61 |
+
return self.out_proj(context)
|
| 62 |
+
|
| 63 |
+
class ZZJRabbit2Layer(nn.Module):
|
| 64 |
+
def __init__(self, config: ZZJRabbit2Config):
|
| 65 |
+
super().__init__()
|
| 66 |
+
self.attn = ZZJRabbit2Attention(config)
|
| 67 |
+
self.l1 = nn.Linear(config.hidden_size, config.hidden_size)
|
| 68 |
+
self.l2 = nn.Linear(config.hidden_size, config.hidden_size)
|
| 69 |
+
self.activate = nn.ReLU()
|
| 70 |
+
self.norm = nn.RMSNorm(config.hidden_size)
|
| 71 |
+
|
| 72 |
+
def forward(self, x: torch.Tensor, attention_mask: Optional[torch.Tensor] = None) -> torch.Tensor:
|
| 73 |
+
key_padding_mask = None
|
| 74 |
+
attn_mask = None
|
| 75 |
+
if self.training:
|
| 76 |
+
attn_mask = torch.gt(torch.triu(torch.ones(x.size(-2), x.size(-2), device=x.device), 1), 0)
|
| 77 |
+
if attention_mask is not None:
|
| 78 |
+
key_padding_mask = torch.lt(attention_mask, 1)
|
| 79 |
+
attn = self.attn(
|
| 80 |
+
x,
|
| 81 |
+
key_padding_mask=key_padding_mask,
|
| 82 |
+
attn_mask=attn_mask,
|
| 83 |
+
)[0]
|
| 84 |
+
x = self.norm(x + attn)
|
| 85 |
+
o = self.l1(x)
|
| 86 |
+
o = self.activate(o)
|
| 87 |
+
o = self.l2(o)
|
| 88 |
+
return self.norm(x + o)
|
| 89 |
+
|
| 90 |
+
class ZZJRabbit2Model(PreTrainedModel):
|
| 91 |
+
config_class = ZZJRabbit2Config
|
| 92 |
+
|
| 93 |
+
def __init__(self, config: ZZJRabbit2Config, **kwargs):
|
| 94 |
+
super().__init__(config, **kwargs)
|
| 95 |
+
self.config = config
|
| 96 |
+
self.emb = nn.Embedding(config.vocab_size, config.hidden_size)
|
| 97 |
+
self.pe = ZZJRabbit2PE(config.hidden_size)
|
| 98 |
+
self.layers = nn.ModuleList([ZZJRabbit2Layer(config) for _ in range(config.num_layers)])
|
| 99 |
+
|
| 100 |
+
def forward(self, input_ids: torch.Tensor, return_dict: Optional[bool] = None, attention_mask: Optional[torch.Tensor] = None, **kwargs):
|
| 101 |
+
res = self.emb(input_ids)
|
| 102 |
+
res = self.pe(res)
|
| 103 |
+
res = res
|
| 104 |
+
for l in self.layers:
|
| 105 |
+
res = l(res, attention_mask)
|
| 106 |
+
if not return_dict:
|
| 107 |
+
return (res,)
|
| 108 |
+
else:
|
| 109 |
+
return BaseModelOutput(res)
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
class ZZJRabbit2ForCausalLM(PreTrainedModel, GenerationMixin):
|
| 113 |
+
config_class = ZZJRabbit2Config
|
| 114 |
+
def __init__(self, config, **kwargs):
|
| 115 |
+
super().__init__(config, **kwargs)
|
| 116 |
+
self.model = ZZJRabbit2Model(config, **kwargs)
|
| 117 |
+
self.l = nn.Linear(config.hidden_size, config.vocab_size)
|
| 118 |
+
|
| 119 |
+
def forward(self, input_ids: torch.Tensor, return_dict: Optional[bool] = None, labels: Optional[torch.Tensor] = None, attention_mask: Optional[torch.Tensor] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **kwargs):
|
| 120 |
+
# print(input_ids, return_dict, labels, attention_mask, logits_to_keep, kwargs)
|
| 121 |
+
hidden = self.model(input_ids=input_ids, attention_mask=attention_mask)[0]
|
| 122 |
+
logits = self.l(hidden[:, slice(-logits_to_keep, None) if isinstance(logits_to_keep, int) else logits_to_keep, :])
|
| 123 |
+
if labels is not None:
|
| 124 |
+
loss = self.loss_function(logits=logits, labels=labels, vocab_size=self.config.vocab_size, **kwargs)
|
| 125 |
+
print(loss)
|
| 126 |
+
if not return_dict:
|
| 127 |
+
return (loss, logits) if labels is not None else (logits,)
|
| 128 |
+
else:
|
| 129 |
+
return CausalLMOutput(logits=logits, loss=loss) if labels is not None else CausalLMOutput(logits=logits)
|
| 130 |
+
|
| 131 |
+
@classmethod
|
| 132 |
+
def can_generate(cls):
|
| 133 |
+
return True
|
| 134 |
+
|
| 135 |
+
def prepare_inputs_for_generation(self, input_ids, **kwargs):
|
| 136 |
+
return {"input_ids": input_ids}
|
| 137 |
+
|
| 138 |
+
class ZZJRabbit2Tokenizer(PreTrainedTokenizer):
|
| 139 |
+
vocab_files_names = {"tokenizers_file": "tokenizer.json"}
|
| 140 |
+
|
| 141 |
+
def __init__(self, tokenizers_file, **kwargs):
|
| 142 |
+
self.internal = Tokenizer.from_file(tokenizers_file)
|
| 143 |
+
super().__init__(**kwargs)
|
| 144 |
+
|
| 145 |
+
def get_vocab(self):
|
| 146 |
+
return {self.internal.id_to_token(i): i for i in range(self.vocab_size)}
|
| 147 |
+
|
| 148 |
+
def tokenize(self, text, **kwargs):
|
| 149 |
+
return self.internal.encode(text).tokens
|
| 150 |
+
|
| 151 |
+
def convert_tokens_to_ids(self, tokens):
|
| 152 |
+
return self.internal.token_to_id(tokens) if isinstance(tokens, str) else [self.internal.token_to_id(t) for t in tokens]
|
| 153 |
+
|
| 154 |
+
def decode(self, tokens, skip_special_tokens=True, **kwargs):
|
| 155 |
+
if isinstance(tokens, torch.Tensor):
|
| 156 |
+
tokens = tokens.tolist()
|
| 157 |
+
return self.internal.decode(tokens, skip_special_tokens=skip_special_tokens)
|
| 158 |
+
|
| 159 |
+
@property
|
| 160 |
+
def vocab_size(self):
|
| 161 |
+
return self.internal.get_vocab_size()
|
| 162 |
+
|
| 163 |
+
def save_vocabulary(self, path, *args, **kwargs) -> tuple[str]:
|
| 164 |
+
p = os.path.join(path, "tokenizer.json")
|
| 165 |
+
self.internal.save(p)
|
| 166 |
+
return (p,)
|