Upload folder using huggingface_hub
Browse files- README.md +3 -9
- __pycache__/transformer_chat.cpython-312.pyc +0 -0
- __pycache__/transformer_chat_config.cpython-312.pyc +0 -0
- built_transformer/__pycache__/decoders.cpython-312.pyc +0 -0
- config.json +19 -0
- model.safetensors +3 -0
- save_hf_model.py +30 -0
- special_tokens_map.json +7 -0
- tokenization/save_hf_tokenizer.py +29 -0
- tokenizer_config.json +60 -0
- transformer_chat.py +72 -72
- transformer_chat_config.py +31 -0
README.md
CHANGED
|
@@ -1,9 +1,3 @@
|
|
| 1 |
-
---
|
| 2 |
-
license: apache-2.0
|
| 3 |
-
|
| 4 |
-
- tuetschek/atis
|
| 5 |
-
language:
|
| 6 |
-
- en
|
| 7 |
-
pipeline_tag: text-generation
|
| 8 |
-
library_name: transformers
|
| 9 |
-
---
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
__pycache__/transformer_chat.cpython-312.pyc
ADDED
|
Binary file (8 kB). View file
|
|
|
__pycache__/transformer_chat_config.cpython-312.pyc
ADDED
|
Binary file (1.25 kB). View file
|
|
|
built_transformer/__pycache__/decoders.cpython-312.pyc
CHANGED
|
Binary files a/built_transformer/__pycache__/decoders.cpython-312.pyc and b/built_transformer/__pycache__/decoders.cpython-312.pyc differ
|
|
|
config.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"TransformerChatbot"
|
| 4 |
+
],
|
| 5 |
+
"d_ff": 2048,
|
| 6 |
+
"d_model": 512,
|
| 7 |
+
"dropout": 0.1,
|
| 8 |
+
"max_len": 5000,
|
| 9 |
+
"max_turns": 16,
|
| 10 |
+
"model_type": "transformer-chat",
|
| 11 |
+
"num_decoder_layers": 6,
|
| 12 |
+
"num_encoder_layers": 6,
|
| 13 |
+
"num_heads": 8,
|
| 14 |
+
"num_roles": 2,
|
| 15 |
+
"num_slots": 22,
|
| 16 |
+
"torch_dtype": "float32",
|
| 17 |
+
"transformers_version": "4.52.4",
|
| 18 |
+
"vocab_size": 25000
|
| 19 |
+
}
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:64652386a8c09a9a7da5d5f5490ef5c9c32ab8025133373bd37fe22061ab9f3a
|
| 3 |
+
size 412838448
|
save_hf_model.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformer_chat_config import TransformerChatConfig
|
| 2 |
+
from transformer_chat import TransformerChatbot
|
| 3 |
+
import torch
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
config = TransformerChatConfig(
|
| 7 |
+
vocab_size=25000,
|
| 8 |
+
d_model=512,
|
| 9 |
+
num_heads=8,
|
| 10 |
+
d_ff=2048,
|
| 11 |
+
num_encoder_layers=6,
|
| 12 |
+
num_decoder_layers=6,
|
| 13 |
+
num_roles=2,
|
| 14 |
+
max_turns=16,
|
| 15 |
+
num_slots=22,
|
| 16 |
+
dropout=0.1,
|
| 17 |
+
max_len=5000
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
model = TransformerChatbot(config)
|
| 21 |
+
|
| 22 |
+
# Load trained weights
|
| 23 |
+
model.load_state_dict(torch.load("atis_transformer.pt", map_location="cpu"))
|
| 24 |
+
|
| 25 |
+
# Save model and config in HuggingFace format
|
| 26 |
+
save_dir = os.path.join("..", "my-hf-chatbot")
|
| 27 |
+
model.save_pretrained(save_dir)
|
| 28 |
+
config.save_pretrained(save_dir)
|
| 29 |
+
|
| 30 |
+
print(f"Model and config saved to {save_dir}")
|
special_tokens_map.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cls_token": "[CLS]",
|
| 3 |
+
"mask_token": "[MASK]",
|
| 4 |
+
"pad_token": "[PAD]",
|
| 5 |
+
"sep_token": "[SEP]",
|
| 6 |
+
"unk_token": "[UNK]"
|
| 7 |
+
}
|
tokenization/save_hf_tokenizer.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import PreTrainedTokenizerFast, AutoTokenizer
|
| 2 |
+
|
| 3 |
+
# Load the tokenizer.json
|
| 4 |
+
hf_tokenizer = PreTrainedTokenizerFast(tokenizer_file="tokenizer.json")
|
| 5 |
+
|
| 6 |
+
# Set special tokens
|
| 7 |
+
hf_tokenizer.pad_token = "[PAD]"
|
| 8 |
+
hf_tokenizer.unk_token = "[UNK]"
|
| 9 |
+
hf_tokenizer.cls_token = "[CLS]"
|
| 10 |
+
hf_tokenizer.sep_token = "[SEP]"
|
| 11 |
+
hf_tokenizer.mask_token = "[MASK]"
|
| 12 |
+
|
| 13 |
+
# Save in HuggingFace format
|
| 14 |
+
hf_tokenizer.save_pretrained("../my-hf-chatbot")
|
| 15 |
+
|
| 16 |
+
# Test loading
|
| 17 |
+
loaded_tokenizer = AutoTokenizer.from_pretrained("../my-hf-chatbot")
|
| 18 |
+
print("Loaded tokenizer vocab size:", len(loaded_tokenizer))
|
| 19 |
+
print("Special tokens:")
|
| 20 |
+
print("PAD:", loaded_tokenizer.pad_token, loaded_tokenizer.pad_token_id)
|
| 21 |
+
print("UNK:", loaded_tokenizer.unk_token, loaded_tokenizer.unk_token_id)
|
| 22 |
+
print("CLS:", loaded_tokenizer.cls_token, loaded_tokenizer.cls_token_id)
|
| 23 |
+
print("SEP:", loaded_tokenizer.sep_token, loaded_tokenizer.sep_token_id)
|
| 24 |
+
print("MASK:", loaded_tokenizer.mask_token, loaded_tokenizer.mask_token_id)
|
| 25 |
+
|
| 26 |
+
# Test encoding
|
| 27 |
+
sample = "Let's test this tokenizer."
|
| 28 |
+
enc = loaded_tokenizer(sample)
|
| 29 |
+
print("Encoded tokens:", enc.tokens())
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"added_tokens_decoder": {
|
| 3 |
+
"0": {
|
| 4 |
+
"content": "[UNK]",
|
| 5 |
+
"lstrip": false,
|
| 6 |
+
"normalized": false,
|
| 7 |
+
"rstrip": false,
|
| 8 |
+
"single_word": false,
|
| 9 |
+
"special": true
|
| 10 |
+
},
|
| 11 |
+
"1": {
|
| 12 |
+
"content": "[PAD]",
|
| 13 |
+
"lstrip": false,
|
| 14 |
+
"normalized": false,
|
| 15 |
+
"rstrip": false,
|
| 16 |
+
"single_word": false,
|
| 17 |
+
"special": true
|
| 18 |
+
},
|
| 19 |
+
"2": {
|
| 20 |
+
"content": "[CLS]",
|
| 21 |
+
"lstrip": false,
|
| 22 |
+
"normalized": false,
|
| 23 |
+
"rstrip": false,
|
| 24 |
+
"single_word": false,
|
| 25 |
+
"special": true
|
| 26 |
+
},
|
| 27 |
+
"3": {
|
| 28 |
+
"content": "[SEP]",
|
| 29 |
+
"lstrip": false,
|
| 30 |
+
"normalized": false,
|
| 31 |
+
"rstrip": false,
|
| 32 |
+
"single_word": false,
|
| 33 |
+
"special": true
|
| 34 |
+
},
|
| 35 |
+
"4": {
|
| 36 |
+
"content": "[MASK]",
|
| 37 |
+
"lstrip": false,
|
| 38 |
+
"normalized": false,
|
| 39 |
+
"rstrip": false,
|
| 40 |
+
"single_word": false,
|
| 41 |
+
"special": true
|
| 42 |
+
}
|
| 43 |
+
},
|
| 44 |
+
"clean_up_tokenization_spaces": false,
|
| 45 |
+
"cls_token": "[CLS]",
|
| 46 |
+
"extra_special_tokens": {},
|
| 47 |
+
"mask_token": "[MASK]",
|
| 48 |
+
"max_length": 128,
|
| 49 |
+
"model_max_length": 1000000000000000019884624838656,
|
| 50 |
+
"pad_to_multiple_of": null,
|
| 51 |
+
"pad_token": "[PAD]",
|
| 52 |
+
"pad_token_type_id": 0,
|
| 53 |
+
"padding_side": "right",
|
| 54 |
+
"sep_token": "[SEP]",
|
| 55 |
+
"stride": 0,
|
| 56 |
+
"tokenizer_class": "PreTrainedTokenizer",
|
| 57 |
+
"truncation_side": "right",
|
| 58 |
+
"truncation_strategy": "longest_first",
|
| 59 |
+
"unk_token": "[UNK]"
|
| 60 |
+
}
|
transformer_chat.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
import torch
|
| 2 |
import torch.nn as nn
|
| 3 |
import math
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# Import neccessary layers
|
| 6 |
from built_transformer.embeddings import Embeddings
|
|
@@ -9,69 +11,53 @@ from built_transformer.decoders import Decoder, DecoderLayer
|
|
| 9 |
from built_transformer.positional_encodings import PositionalEncoding
|
| 10 |
from built_transformer.slot_classifier import SlotClassifier
|
| 11 |
|
| 12 |
-
class TransformerChatbot(
|
| 13 |
"""
|
| 14 |
Unified Transformer-based chatbot model that combines:
|
| 15 |
- Joint token/role/turn embeddings
|
| 16 |
- Encoder-decoder architecture with attention
|
| 17 |
- Slot-filling classification
|
| 18 |
-
- Generation capabilities
|
| 19 |
"""
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
d_ff: int = 2048,
|
| 26 |
-
num_encoder_layers: int = 6,
|
| 27 |
-
num_decoder_layers: int = 6,
|
| 28 |
-
num_roles: int = 2,
|
| 29 |
-
max_turns: int = 16,
|
| 30 |
-
num_slots: int = 4,
|
| 31 |
-
dropout: float = 0.1,
|
| 32 |
-
max_len: int = 5000
|
| 33 |
-
):
|
| 34 |
-
super().__init__()
|
| 35 |
-
|
| 36 |
# Embeddings for tokens, roles, and turns
|
| 37 |
self.embed = Embeddings(
|
| 38 |
-
char=vocab_size,
|
| 39 |
-
dimension_for_model=d_model,
|
| 40 |
-
num_of_roles=num_roles,
|
| 41 |
-
max_turns=max_turns
|
| 42 |
)
|
| 43 |
-
|
| 44 |
# Positional encoding
|
| 45 |
-
self.pos_enc = PositionalEncoding(d_model, dropout, max_len)
|
| 46 |
-
|
| 47 |
# Encoder stack
|
| 48 |
self.encoder = Encoder(
|
| 49 |
-
vocab_size=vocab_size,
|
| 50 |
-
dimension_of_model=d_model,
|
| 51 |
-
num_of_heads=num_heads,
|
| 52 |
-
num_layers=num_encoder_layers,
|
| 53 |
-
dim_feedforward=d_ff,
|
| 54 |
-
dropout=dropout,
|
| 55 |
-
max_len=max_len,
|
| 56 |
-
num_of_roles=num_roles,
|
| 57 |
-
max_turns=max_turns
|
| 58 |
)
|
| 59 |
-
|
| 60 |
# Decoder stack
|
| 61 |
self.decoder = Decoder(
|
| 62 |
-
vocab_size=vocab_size,
|
| 63 |
-
dimension_for_model=d_model,
|
| 64 |
-
num_layers=num_decoder_layers,
|
| 65 |
-
num_of_heads=num_heads,
|
| 66 |
-
dim_feedforward=d_ff,
|
| 67 |
-
dropout=dropout,
|
| 68 |
-
max_len=max_len
|
| 69 |
)
|
| 70 |
-
|
| 71 |
# Output projections
|
| 72 |
-
self.out_proj = nn.Linear(d_model, vocab_size)
|
| 73 |
-
self.slot_classifier = SlotClassifier(d_model, num_slots)
|
| 74 |
-
|
| 75 |
# Initialize parameters
|
| 76 |
self._init_parameters()
|
| 77 |
|
|
@@ -90,8 +76,8 @@ class TransformerChatbot(nn.Module):
|
|
| 90 |
# Copy the old embedding weights to the new structure
|
| 91 |
state_dict['encoder.embed.lut.weight'] = old_embed_weight
|
| 92 |
# Initialize role and turn embeddings with correct sizes
|
| 93 |
-
state_dict['encoder.embed.lut_roles.weight'] = torch.zeros(
|
| 94 |
-
state_dict['encoder.embed.lut_turns.weight'] = torch.zeros(
|
| 95 |
state_dict['encoder.embed.norm.weight'] = torch.ones(old_embed_weight.size(1))
|
| 96 |
state_dict['encoder.embed.norm.bias'] = torch.zeros(old_embed_weight.size(1))
|
| 97 |
|
|
@@ -145,34 +131,51 @@ class TransformerChatbot(nn.Module):
|
|
| 145 |
|
| 146 |
def forward(
|
| 147 |
self,
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
|
|
|
| 156 |
):
|
| 157 |
"""
|
| 158 |
-
|
| 159 |
Args:
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
Returns:
|
| 169 |
gen_logits: [B, T, vocab_size] generation logits
|
| 170 |
slot_logits: [B, num_slots] slot classification logits
|
| 171 |
"""
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
enc_out = self.encode(src_tokens, src_roles, src_turns, src_mask)
|
| 174 |
-
|
| 175 |
-
# Decode target sequence
|
| 176 |
gen_logits = self.decode(
|
| 177 |
tgt_tokens,
|
| 178 |
enc_out,
|
|
@@ -181,9 +184,6 @@ class TransformerChatbot(nn.Module):
|
|
| 181 |
src_mask,
|
| 182 |
tgt_mask
|
| 183 |
)
|
| 184 |
-
|
| 185 |
-
# Use first position of encoder output for slot classification
|
| 186 |
cls_rep = enc_out[:, 0, :]
|
| 187 |
slot_logits = self.slot_classifier(cls_rep)
|
| 188 |
-
|
| 189 |
-
return gen_logits, slot_logits
|
|
|
|
| 1 |
import torch
|
| 2 |
import torch.nn as nn
|
| 3 |
import math
|
| 4 |
+
from transformers import PreTrainedModel
|
| 5 |
+
from transformer_chat_config import TransformerChatConfig
|
| 6 |
|
| 7 |
# Import neccessary layers
|
| 8 |
from built_transformer.embeddings import Embeddings
|
|
|
|
| 11 |
from built_transformer.positional_encodings import PositionalEncoding
|
| 12 |
from built_transformer.slot_classifier import SlotClassifier
|
| 13 |
|
| 14 |
+
class TransformerChatbot(PreTrainedModel):
|
| 15 |
"""
|
| 16 |
Unified Transformer-based chatbot model that combines:
|
| 17 |
- Joint token/role/turn embeddings
|
| 18 |
- Encoder-decoder architecture with attention
|
| 19 |
- Slot-filling classification
|
| 20 |
+
- Generation capabilities (newly added, hopefully compatible with HuggingFace)
|
| 21 |
"""
|
| 22 |
+
config_class = TransformerChatConfig
|
| 23 |
+
|
| 24 |
+
def __init__(self, config: TransformerChatConfig):
|
| 25 |
+
super().__init__(config)
|
| 26 |
+
self.config = config
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
# Embeddings for tokens, roles, and turns
|
| 28 |
self.embed = Embeddings(
|
| 29 |
+
char=config.vocab_size,
|
| 30 |
+
dimension_for_model=config.d_model,
|
| 31 |
+
num_of_roles=config.num_roles,
|
| 32 |
+
max_turns=config.max_turns
|
| 33 |
)
|
|
|
|
| 34 |
# Positional encoding
|
| 35 |
+
self.pos_enc = PositionalEncoding(config.d_model, config.dropout, config.max_len)
|
|
|
|
| 36 |
# Encoder stack
|
| 37 |
self.encoder = Encoder(
|
| 38 |
+
vocab_size=config.vocab_size,
|
| 39 |
+
dimension_of_model=config.d_model,
|
| 40 |
+
num_of_heads=config.num_heads,
|
| 41 |
+
num_layers=config.num_encoder_layers,
|
| 42 |
+
dim_feedforward=config.d_ff,
|
| 43 |
+
dropout=config.dropout,
|
| 44 |
+
max_len=config.max_len,
|
| 45 |
+
num_of_roles=config.num_roles,
|
| 46 |
+
max_turns=config.max_turns
|
| 47 |
)
|
|
|
|
| 48 |
# Decoder stack
|
| 49 |
self.decoder = Decoder(
|
| 50 |
+
vocab_size=config.vocab_size,
|
| 51 |
+
dimension_for_model=config.d_model,
|
| 52 |
+
num_layers=config.num_decoder_layers,
|
| 53 |
+
num_of_heads=config.num_heads,
|
| 54 |
+
dim_feedforward=config.d_ff,
|
| 55 |
+
dropout=config.dropout,
|
| 56 |
+
max_len=config.max_len
|
| 57 |
)
|
|
|
|
| 58 |
# Output projections
|
| 59 |
+
self.out_proj = nn.Linear(config.d_model, config.vocab_size)
|
| 60 |
+
self.slot_classifier = SlotClassifier(config.d_model, config.num_slots)
|
|
|
|
| 61 |
# Initialize parameters
|
| 62 |
self._init_parameters()
|
| 63 |
|
|
|
|
| 76 |
# Copy the old embedding weights to the new structure
|
| 77 |
state_dict['encoder.embed.lut.weight'] = old_embed_weight
|
| 78 |
# Initialize role and turn embeddings with correct sizes
|
| 79 |
+
state_dict['encoder.embed.lut_roles.weight'] = torch.zeros(self.config.num_roles, old_embed_weight.size(1))
|
| 80 |
+
state_dict['encoder.embed.lut_turns.weight'] = torch.zeros(self.config.max_turns, old_embed_weight.size(1))
|
| 81 |
state_dict['encoder.embed.norm.weight'] = torch.ones(old_embed_weight.size(1))
|
| 82 |
state_dict['encoder.embed.norm.bias'] = torch.zeros(old_embed_weight.size(1))
|
| 83 |
|
|
|
|
| 131 |
|
| 132 |
def forward(
|
| 133 |
self,
|
| 134 |
+
input_ids=None,
|
| 135 |
+
decoder_input_ids=None,
|
| 136 |
+
attention_mask=None,
|
| 137 |
+
decoder_attention_mask=None,
|
| 138 |
+
src_roles=None,
|
| 139 |
+
tgt_roles=None,
|
| 140 |
+
src_turns=None,
|
| 141 |
+
tgt_turns=None,
|
| 142 |
+
**kwargs
|
| 143 |
):
|
| 144 |
"""
|
| 145 |
+
HuggingFace-compatible forward method.
|
| 146 |
Args:
|
| 147 |
+
input_ids: [B, S] source token IDs
|
| 148 |
+
decoder_input_ids: [B, T] target token IDs
|
| 149 |
+
attention_mask: [B, 1, 1, S] source mask
|
| 150 |
+
decoder_attention_mask: [B, 1, T, T] target mask
|
| 151 |
+
src_roles: [B, S] source role IDs
|
| 152 |
+
tgt_roles: [B, T] target role IDs
|
| 153 |
+
src_turns: [B, S] source turn IDs
|
| 154 |
+
tgt_turns: [B, T] target turn IDs
|
| 155 |
Returns:
|
| 156 |
gen_logits: [B, T, vocab_size] generation logits
|
| 157 |
slot_logits: [B, num_slots] slot classification logits
|
| 158 |
"""
|
| 159 |
+
src_tokens = input_ids
|
| 160 |
+
tgt_tokens = decoder_input_ids
|
| 161 |
+
src_mask = attention_mask
|
| 162 |
+
tgt_mask = decoder_attention_mask
|
| 163 |
+
|
| 164 |
+
# Infer roles and turns if not provided
|
| 165 |
+
if src_tokens is not None:
|
| 166 |
+
batch_size, src_len = src_tokens.shape
|
| 167 |
+
if src_roles is None:
|
| 168 |
+
src_roles = torch.zeros_like(src_tokens)
|
| 169 |
+
if src_turns is None:
|
| 170 |
+
src_turns = torch.arange(src_len, device=src_tokens.device).unsqueeze(0).expand(batch_size, src_len)
|
| 171 |
+
if tgt_tokens is not None:
|
| 172 |
+
batch_size, tgt_len = tgt_tokens.shape
|
| 173 |
+
if tgt_roles is None:
|
| 174 |
+
tgt_roles = torch.zeros_like(tgt_tokens)
|
| 175 |
+
if tgt_turns is None:
|
| 176 |
+
tgt_turns = torch.arange(tgt_len, device=tgt_tokens.device).unsqueeze(0).expand(batch_size, tgt_len)
|
| 177 |
+
|
| 178 |
enc_out = self.encode(src_tokens, src_roles, src_turns, src_mask)
|
|
|
|
|
|
|
| 179 |
gen_logits = self.decode(
|
| 180 |
tgt_tokens,
|
| 181 |
enc_out,
|
|
|
|
| 184 |
src_mask,
|
| 185 |
tgt_mask
|
| 186 |
)
|
|
|
|
|
|
|
| 187 |
cls_rep = enc_out[:, 0, :]
|
| 188 |
slot_logits = self.slot_classifier(cls_rep)
|
| 189 |
+
return {"gen_logits": gen_logits, "slot_logits": slot_logits}
|
|
|
transformer_chat_config.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import PretrainedConfig
|
| 2 |
+
|
| 3 |
+
class TransformerChatConfig(PretrainedConfig):
|
| 4 |
+
model_type = "transformer-chat"
|
| 5 |
+
def __init__(
|
| 6 |
+
self,
|
| 7 |
+
vocab_size=30522,
|
| 8 |
+
d_model=512,
|
| 9 |
+
num_heads=8,
|
| 10 |
+
d_ff=2048,
|
| 11 |
+
num_encoder_layers=6,
|
| 12 |
+
num_decoder_layers=6,
|
| 13 |
+
num_roles=2,
|
| 14 |
+
max_turns=16,
|
| 15 |
+
num_slots=4,
|
| 16 |
+
dropout=0.1,
|
| 17 |
+
max_len=5000,
|
| 18 |
+
**kwargs
|
| 19 |
+
):
|
| 20 |
+
super().__init__(**kwargs)
|
| 21 |
+
self.vocab_size = vocab_size
|
| 22 |
+
self.d_model = d_model
|
| 23 |
+
self.num_heads = num_heads
|
| 24 |
+
self.d_ff = d_ff
|
| 25 |
+
self.num_encoder_layers = num_encoder_layers
|
| 26 |
+
self.num_decoder_layers = num_decoder_layers
|
| 27 |
+
self.num_roles = num_roles
|
| 28 |
+
self.max_turns = max_turns
|
| 29 |
+
self.num_slots = num_slots
|
| 30 |
+
self.dropout = dropout
|
| 31 |
+
self.max_len = max_len
|