Commit ·
b5843e3
1
Parent(s): ff0948a
Upload LanceAI
Browse files- config.json +5 -1
- generation_config.json +1 -1
- lance_ai_model.py +62 -0
config.json
CHANGED
|
@@ -2,11 +2,15 @@
|
|
| 2 |
"architectures": [
|
| 3 |
"LanceAI"
|
| 4 |
],
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
"hidden_size": 2048,
|
| 6 |
"model_type": "lance_ai",
|
| 7 |
"num_heads": 16,
|
| 8 |
"num_layers": 24,
|
| 9 |
"torch_dtype": "float32",
|
| 10 |
-
"transformers_version": "4.
|
| 11 |
"vocab_size": 100277
|
| 12 |
}
|
|
|
|
| 2 |
"architectures": [
|
| 3 |
"LanceAI"
|
| 4 |
],
|
| 5 |
+
"auto_map": {
|
| 6 |
+
"AutoConfig": "lance_ai_model.LanceAIConfig",
|
| 7 |
+
"AutoModelForCausalLM": "lance_ai_model.LanceAI"
|
| 8 |
+
},
|
| 9 |
"hidden_size": 2048,
|
| 10 |
"model_type": "lance_ai",
|
| 11 |
"num_heads": 16,
|
| 12 |
"num_layers": 24,
|
| 13 |
"torch_dtype": "float32",
|
| 14 |
+
"transformers_version": "4.51.3",
|
| 15 |
"vocab_size": 100277
|
| 16 |
}
|
generation_config.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
{
|
| 2 |
"_from_model_config": true,
|
| 3 |
-
"transformers_version": "4.
|
| 4 |
}
|
|
|
|
| 1 |
{
|
| 2 |
"_from_model_config": true,
|
| 3 |
+
"transformers_version": "4.51.3"
|
| 4 |
}
|
lance_ai_model.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
from transformers import PreTrainedModel, PretrainedConfig, GenerationMixin
|
| 4 |
+
from transformers.modeling_outputs import CausalLMOutputWithPast
|
| 5 |
+
from transformers.models.auto.configuration_auto import CONFIG_MAPPING
|
| 6 |
+
from transformers.models.auto.modeling_auto import MODEL_FOR_CAUSAL_LM_MAPPING
|
| 7 |
+
|
| 8 |
+
class LanceAIConfig(PretrainedConfig):
|
| 9 |
+
model_type = "lance_ai"
|
| 10 |
+
def __init__(self, vocab_size=50257, hidden_size=2048, num_layers=24, num_heads=16, architectures=["LanceAI"], **kwargs):
|
| 11 |
+
super().__init__(**kwargs)
|
| 12 |
+
self.vocab_size = vocab_size
|
| 13 |
+
self.hidden_size = hidden_size
|
| 14 |
+
self.num_layers = num_layers
|
| 15 |
+
self.num_heads = num_heads
|
| 16 |
+
self.architectures = architectures
|
| 17 |
+
|
| 18 |
+
class LanceAI(PreTrainedModel, GenerationMixin):
|
| 19 |
+
config_class = LanceAIConfig
|
| 20 |
+
|
| 21 |
+
def __init__(self, config):
|
| 22 |
+
super().__init__(config)
|
| 23 |
+
self.embedding = nn.Embedding(config.vocab_size, config.hidden_size)
|
| 24 |
+
|
| 25 |
+
self.encoder = nn.TransformerEncoder(
|
| 26 |
+
nn.TransformerEncoderLayer(d_model=config.hidden_size, nhead=config.num_heads),
|
| 27 |
+
num_layers=config.num_layers
|
| 28 |
+
)
|
| 29 |
+
self.decoder = nn.TransformerDecoder(
|
| 30 |
+
nn.TransformerDecoderLayer(d_model=config.hidden_size, nhead=config.num_heads),
|
| 31 |
+
num_layers=config.num_layers
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size)
|
| 35 |
+
self.loss_fct = nn.CrossEntropyLoss(ignore_index=-100)
|
| 36 |
+
|
| 37 |
+
self.init_weights()
|
| 38 |
+
|
| 39 |
+
def forward(self, input_ids=None, labels=None, inputs_embeds=None, return_dict=True):
|
| 40 |
+
embeddings = self.embedding(input_ids) if inputs_embeds is None else inputs_embeds
|
| 41 |
+
encoder_output = self.encoder(embeddings)
|
| 42 |
+
decoder_output = self.decoder(embeddings, encoder_output)
|
| 43 |
+
|
| 44 |
+
logits = self.lm_head(decoder_output)
|
| 45 |
+
loss = None
|
| 46 |
+
|
| 47 |
+
if labels is not None:
|
| 48 |
+
shift_logits = logits[..., :-1, :].contiguous()
|
| 49 |
+
shift_labels = labels[..., 1:].contiguous()
|
| 50 |
+
shift_labels = torch.clamp(shift_labels, max=self.config.vocab_size - 1)
|
| 51 |
+
loss = self.loss_fct(shift_logits.view(-1, self.config.vocab_size), shift_labels.view(-1))
|
| 52 |
+
|
| 53 |
+
if return_dict:
|
| 54 |
+
return CausalLMOutputWithPast(loss=loss, logits=logits)
|
| 55 |
+
|
| 56 |
+
return (loss, logits) if loss is not None else logits
|
| 57 |
+
|
| 58 |
+
# Register with Hugging Face
|
| 59 |
+
CONFIG_MAPPING.register("lance_ai", LanceAIConfig)
|
| 60 |
+
MODEL_FOR_CAUSAL_LM_MAPPING.register(LanceAIConfig, LanceAI)
|
| 61 |
+
LanceAIConfig.register_for_auto_class("AutoConfig")
|
| 62 |
+
LanceAI.register_for_auto_class("AutoModelForCausalLM")
|