Add standard Transformers inference adapter
Browse files- README.md +19 -1
- config.json +26 -6
- configuration_aurora.py +63 -0
- generation_config.json +8 -0
- modeling_aurora.py +80 -0
- special_tokens_map.json +5 -0
- tokenizer_config.json +9 -0
README.md
CHANGED
|
@@ -35,7 +35,25 @@ What is Python?<|im_end|>
|
|
| 35 |
<|im_start|>assistant
|
| 36 |
```
|
| 37 |
|
| 38 |
-
The model is a custom Aurora checkpoint
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
## What changed
|
| 41 |
|
|
|
|
| 35 |
<|im_start|>assistant
|
| 36 |
```
|
| 37 |
|
| 38 |
+
The model is a custom Aurora checkpoint. The included native Aurora runtime is the simplest path; a Transformers remote-code adapter is also provided below for normal Hub-style testing.
|
| 39 |
+
|
| 40 |
+
## Transformers / Hugging Face test
|
| 41 |
+
|
| 42 |
+
The repository also includes a Transformers remote-code adapter, so it can be loaded through the normal `AutoTokenizer` and `AutoModelForCausalLM` APIs:
|
| 43 |
+
|
| 44 |
+
```python
|
| 45 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 46 |
+
|
| 47 |
+
repo = "North-ML1/Aurora-Proelia-ChatML"
|
| 48 |
+
tokenizer = AutoTokenizer.from_pretrained(repo, trust_remote_code=True)
|
| 49 |
+
model = AutoModelForCausalLM.from_pretrained(repo, trust_remote_code=True)
|
| 50 |
+
messages = [{"role": "user", "content": "What is Python?"}]
|
| 51 |
+
inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt")
|
| 52 |
+
outputs = model.generate(inputs, max_new_tokens=96, do_sample=False)
|
| 53 |
+
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
|
| 54 |
+
```
|
| 55 |
+
|
| 56 |
+
`trust_remote_code=True` is required because Aurora is a custom architecture; inspect the repository code before enabling it in an untrusted environment.
|
| 57 |
|
| 58 |
## What changed
|
| 59 |
|
config.json
CHANGED
|
@@ -1,13 +1,33 @@
|
|
| 1 |
{
|
| 2 |
"_name_or_path": "North-ML1/Aurora-Proelia-ChatML",
|
| 3 |
-
"architectures": [
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
"model_type": "aurora",
|
| 7 |
"model_name": "Aurora Proelia ChatML",
|
| 8 |
"vocab_size": 16000,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
"max_position_embeddings": 2048,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
"torch_dtype": "float16",
|
| 11 |
-
"library_name": "aurora"
|
| 12 |
-
|
| 13 |
-
}\n
|
|
|
|
| 1 |
{
|
| 2 |
"_name_or_path": "North-ML1/Aurora-Proelia-ChatML",
|
| 3 |
+
"architectures": ["AuroraForCausalLM"],
|
| 4 |
+
"auto_map": {
|
| 5 |
+
"AutoConfig": "configuration_aurora.AuroraHFConfig",
|
| 6 |
+
"AutoModelForCausalLM": "modeling_aurora.AuroraForCausalLM"
|
| 7 |
+
},
|
| 8 |
"model_type": "aurora",
|
| 9 |
"model_name": "Aurora Proelia ChatML",
|
| 10 |
"vocab_size": 16000,
|
| 11 |
+
"hidden_size": 896,
|
| 12 |
+
"num_layers": 23,
|
| 13 |
+
"num_attention_heads": 14,
|
| 14 |
+
"num_key_value_heads": 2,
|
| 15 |
+
"intermediate_size": 2432,
|
| 16 |
+
"context_length": 2048,
|
| 17 |
"max_position_embeddings": 2048,
|
| 18 |
+
"rope_theta": 500000.0,
|
| 19 |
+
"rms_norm_eps": 0.00001,
|
| 20 |
+
"qk_norm": true,
|
| 21 |
+
"tie_word_embeddings": true,
|
| 22 |
+
"attention_bias": false,
|
| 23 |
+
"mlp_bias": false,
|
| 24 |
+
"dropout": 0.0,
|
| 25 |
+
"num_experts": 1,
|
| 26 |
+
"router_aux_loss_coef": 0.0,
|
| 27 |
+
"router_z_loss_coef": 0.0,
|
| 28 |
+
"router_noise_scale": 0.0,
|
| 29 |
+
"moe_capacity_factor": 0.0,
|
| 30 |
+
"router_use_gate_weight": false,
|
| 31 |
"torch_dtype": "float16",
|
| 32 |
+
"library_name": "aurora"
|
| 33 |
+
}
|
|
|
configuration_aurora.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from transformers import PretrainedConfig
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class AuroraHFConfig(PretrainedConfig):
|
| 7 |
+
model_type = "aurora"
|
| 8 |
+
|
| 9 |
+
def __init__(
|
| 10 |
+
self,
|
| 11 |
+
model_name: str = "Aurora Proelia ChatML",
|
| 12 |
+
vocab_size: int = 16000,
|
| 13 |
+
hidden_size: int = 896,
|
| 14 |
+
num_layers: int = 23,
|
| 15 |
+
num_attention_heads: int = 14,
|
| 16 |
+
num_key_value_heads: int = 2,
|
| 17 |
+
intermediate_size: int = 2432,
|
| 18 |
+
context_length: int = 2048,
|
| 19 |
+
rope_theta: float = 500000.0,
|
| 20 |
+
rms_norm_eps: float = 1.0e-5,
|
| 21 |
+
qk_norm: bool = True,
|
| 22 |
+
tie_word_embeddings: bool = True,
|
| 23 |
+
attention_bias: bool = False,
|
| 24 |
+
mlp_bias: bool = False,
|
| 25 |
+
dropout: float = 0.0,
|
| 26 |
+
num_experts: int = 1,
|
| 27 |
+
router_aux_loss_coef: float = 0.0,
|
| 28 |
+
router_z_loss_coef: float = 0.0,
|
| 29 |
+
router_noise_scale: float = 0.0,
|
| 30 |
+
moe_capacity_factor: float = 0.0,
|
| 31 |
+
router_use_gate_weight: bool = False,
|
| 32 |
+
**kwargs,
|
| 33 |
+
):
|
| 34 |
+
super().__init__(
|
| 35 |
+
tie_word_embeddings=tie_word_embeddings,
|
| 36 |
+
bos_token_id=kwargs.pop("bos_token_id", 1),
|
| 37 |
+
eos_token_id=kwargs.pop("eos_token_id", 2),
|
| 38 |
+
pad_token_id=kwargs.pop("pad_token_id", 0),
|
| 39 |
+
**kwargs,
|
| 40 |
+
)
|
| 41 |
+
self.model_name = model_name
|
| 42 |
+
self.vocab_size = vocab_size
|
| 43 |
+
self.hidden_size = hidden_size
|
| 44 |
+
self.num_layers = num_layers
|
| 45 |
+
self.num_attention_heads = num_attention_heads
|
| 46 |
+
self.num_key_value_heads = num_key_value_heads
|
| 47 |
+
self.intermediate_size = intermediate_size
|
| 48 |
+
self.context_length = context_length
|
| 49 |
+
self.max_position_embeddings = context_length
|
| 50 |
+
self.rope_theta = rope_theta
|
| 51 |
+
self.rms_norm_eps = rms_norm_eps
|
| 52 |
+
self.qk_norm = qk_norm
|
| 53 |
+
self.tie_word_embeddings = tie_word_embeddings
|
| 54 |
+
self.attention_bias = attention_bias
|
| 55 |
+
self.mlp_bias = mlp_bias
|
| 56 |
+
self.dropout = dropout
|
| 57 |
+
self.num_experts = num_experts
|
| 58 |
+
self.router_aux_loss_coef = router_aux_loss_coef
|
| 59 |
+
self.router_z_loss_coef = router_z_loss_coef
|
| 60 |
+
self.router_noise_scale = router_noise_scale
|
| 61 |
+
self.moe_capacity_factor = moe_capacity_factor
|
| 62 |
+
self.router_use_gate_weight = router_use_gate_weight
|
| 63 |
+
self.use_cache = False
|
generation_config.json
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"bos_token_id": 1,
|
| 3 |
+
"eos_token_id": 2,
|
| 4 |
+
"pad_token_id": 0,
|
| 5 |
+
"do_sample": false,
|
| 6 |
+
"max_new_tokens": 96,
|
| 7 |
+
"use_cache": false
|
| 8 |
+
}
|
modeling_aurora.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from typing import Optional
|
| 4 |
+
|
| 5 |
+
import torch
|
| 6 |
+
from torch import nn
|
| 7 |
+
from transformers import PreTrainedModel
|
| 8 |
+
from transformers.modeling_outputs import CausalLMOutputWithPast
|
| 9 |
+
|
| 10 |
+
from .configuration_aurora import AuroraHFConfig
|
| 11 |
+
from .aurora.config import AuroraConfig as NativeAuroraConfig
|
| 12 |
+
from .aurora.model import AuroraForCausalLM as NativeAuroraForCausalLM
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class AuroraForCausalLM(PreTrainedModel):
|
| 16 |
+
config_class = AuroraHFConfig
|
| 17 |
+
base_model_prefix = "aurora"
|
| 18 |
+
main_input_name = "input_ids"
|
| 19 |
+
_supports_cache_class = False
|
| 20 |
+
|
| 21 |
+
def __init__(self, config: AuroraHFConfig):
|
| 22 |
+
super().__init__(config)
|
| 23 |
+
native_config = NativeAuroraConfig(
|
| 24 |
+
model_name=config.model_name,
|
| 25 |
+
vocab_size=config.vocab_size,
|
| 26 |
+
hidden_size=config.hidden_size,
|
| 27 |
+
num_layers=config.num_layers,
|
| 28 |
+
num_attention_heads=config.num_attention_heads,
|
| 29 |
+
num_key_value_heads=config.num_key_value_heads,
|
| 30 |
+
intermediate_size=config.intermediate_size,
|
| 31 |
+
context_length=config.context_length,
|
| 32 |
+
rope_theta=config.rope_theta,
|
| 33 |
+
rms_norm_eps=config.rms_norm_eps,
|
| 34 |
+
qk_norm=config.qk_norm,
|
| 35 |
+
tie_word_embeddings=config.tie_word_embeddings,
|
| 36 |
+
attention_bias=config.attention_bias,
|
| 37 |
+
mlp_bias=config.mlp_bias,
|
| 38 |
+
dropout=config.dropout,
|
| 39 |
+
num_experts=config.num_experts,
|
| 40 |
+
router_aux_loss_coef=config.router_aux_loss_coef,
|
| 41 |
+
router_z_loss_coef=config.router_z_loss_coef,
|
| 42 |
+
router_noise_scale=config.router_noise_scale,
|
| 43 |
+
moe_capacity_factor=config.moe_capacity_factor,
|
| 44 |
+
router_use_gate_weight=config.router_use_gate_weight,
|
| 45 |
+
)
|
| 46 |
+
self.aurora = NativeAuroraForCausalLM(native_config)
|
| 47 |
+
|
| 48 |
+
def load_state_dict(self, state_dict, strict: bool = True, assign: bool = False, **kwargs):
|
| 49 |
+
# The raw Aurora export stores the native module at the repository root;
|
| 50 |
+
# the Transformers wrapper stores it below `aurora`.
|
| 51 |
+
remapped = {
|
| 52 |
+
(key if key.startswith("aurora.") else f"aurora.{key}"): value
|
| 53 |
+
for key, value in state_dict.items()
|
| 54 |
+
}
|
| 55 |
+
return super().load_state_dict(remapped, strict=strict, assign=assign, **kwargs)
|
| 56 |
+
|
| 57 |
+
def get_input_embeddings(self):
|
| 58 |
+
return self.aurora.embed_tokens
|
| 59 |
+
|
| 60 |
+
def set_input_embeddings(self, value):
|
| 61 |
+
self.aurora.embed_tokens = value
|
| 62 |
+
|
| 63 |
+
def get_output_embeddings(self):
|
| 64 |
+
return self.aurora.lm_head
|
| 65 |
+
|
| 66 |
+
def set_output_embeddings(self, new_embeddings):
|
| 67 |
+
self.aurora.lm_head = new_embeddings
|
| 68 |
+
|
| 69 |
+
def prepare_inputs_for_generation(self, input_ids, **kwargs):
|
| 70 |
+
return {"input_ids": input_ids}
|
| 71 |
+
|
| 72 |
+
def forward(
|
| 73 |
+
self,
|
| 74 |
+
input_ids: torch.LongTensor,
|
| 75 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 76 |
+
labels: Optional[torch.LongTensor] = None,
|
| 77 |
+
**kwargs,
|
| 78 |
+
) -> CausalLMOutputWithPast:
|
| 79 |
+
logits, loss = self.aurora(input_ids=input_ids, labels=labels)
|
| 80 |
+
return CausalLMOutputWithPast(loss=loss, logits=logits)
|
special_tokens_map.json
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"bos_token": "<bos>",
|
| 3 |
+
"eos_token": "<eos>",
|
| 4 |
+
"pad_token": "<pad>"
|
| 5 |
+
}
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"tokenizer_class": "PreTrainedTokenizerFast",
|
| 3 |
+
"bos_token": "<bos>",
|
| 4 |
+
"eos_token": "<eos>",
|
| 5 |
+
"pad_token": "<pad>",
|
| 6 |
+
"model_max_length": 2048,
|
| 7 |
+
"clean_up_tokenization_spaces": false,
|
| 8 |
+
"chat_template": "{% for message in messages %}<|im_start|>{{ message['role'] }}\\n{{ message['content'] }}<|im_end|>\\n{% endfor %}{% if add_generation_prompt %}<|im_start|>assistant\\n{% endif %}"
|
| 9 |
+
}
|