smallcloudteam
commited on
Commit
•
f54e655
1
Parent(s):
319a8e1
add module
Browse files- codify/__init__.py +57 -0
- codify/configuration_codify.py +152 -0
- codify/modeling_codify.py +773 -0
- codify/tokenization_codify_fast.py +95 -0
codify/__init__.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import TYPE_CHECKING
|
2 |
+
|
3 |
+
from transformers.utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available
|
4 |
+
|
5 |
+
|
6 |
+
_import_structure = {
|
7 |
+
"configuration_codify": ["CODIFY_PRETRAINED_CONFIG_ARCHIVE_MAP", "CodifyConfig", "CodifyOnnxConfig"],
|
8 |
+
}
|
9 |
+
try:
|
10 |
+
if not is_tokenizers_available():
|
11 |
+
raise OptionalDependencyNotAvailable()
|
12 |
+
except OptionalDependencyNotAvailable:
|
13 |
+
pass
|
14 |
+
else:
|
15 |
+
_import_structure["tokenization_codify_fast"] = ["CodifyTokenizerFast"]
|
16 |
+
|
17 |
+
try:
|
18 |
+
if not is_torch_available():
|
19 |
+
raise OptionalDependencyNotAvailable()
|
20 |
+
except OptionalDependencyNotAvailable:
|
21 |
+
pass
|
22 |
+
else:
|
23 |
+
_import_structure["modeling_codify"] = [
|
24 |
+
"CODIFY_PRETRAINED_MODEL_ARCHIVE_LIST",
|
25 |
+
"CodifyForCausalLM",
|
26 |
+
"CodifyModel",
|
27 |
+
"CodifyPreTrainedModel",
|
28 |
+
]
|
29 |
+
|
30 |
+
if TYPE_CHECKING:
|
31 |
+
from .configuration_codify import CODIFY_PRETRAINED_CONFIG_ARCHIVE_MAP, CodifyConfig, CodifyOnnxConfig
|
32 |
+
|
33 |
+
try:
|
34 |
+
if not is_tokenizers_available():
|
35 |
+
raise OptionalDependencyNotAvailable()
|
36 |
+
except OptionalDependencyNotAvailable:
|
37 |
+
pass
|
38 |
+
else:
|
39 |
+
from .tokenization_codify_fast import CodifyTokenizerFast
|
40 |
+
|
41 |
+
try:
|
42 |
+
if not is_torch_available():
|
43 |
+
raise OptionalDependencyNotAvailable()
|
44 |
+
except OptionalDependencyNotAvailable:
|
45 |
+
pass
|
46 |
+
else:
|
47 |
+
from .modeling_codify import (
|
48 |
+
CODIFY_PRETRAINED_MODEL_ARCHIVE_LIST,
|
49 |
+
CodifyForCausalLM,
|
50 |
+
CodifyModel,
|
51 |
+
CodifyPreTrainedModel,
|
52 |
+
)
|
53 |
+
|
54 |
+
else:
|
55 |
+
import sys
|
56 |
+
|
57 |
+
sys.modules[__name__] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
|
codify/configuration_codify.py
ADDED
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from collections import OrderedDict
|
2 |
+
from typing import TYPE_CHECKING, Any, List, Mapping, Optional
|
3 |
+
|
4 |
+
from packaging import version
|
5 |
+
|
6 |
+
from transformers import is_torch_available
|
7 |
+
|
8 |
+
if TYPE_CHECKING:
|
9 |
+
from transformers import PreTrainedTokenizer, TensorType
|
10 |
+
|
11 |
+
from transformers.configuration_utils import PretrainedConfig
|
12 |
+
from transformers.onnx import OnnxConfigWithPast, PatchingSpec
|
13 |
+
from transformers.utils import logging
|
14 |
+
|
15 |
+
logger = logging.get_logger(__name__)
|
16 |
+
|
17 |
+
CODIFY_PRETRAINED_CONFIG_ARCHIVE_MAP = {
|
18 |
+
"smallcloudai/codify_medium_multi": "https://huggingface.co/smallcloudai/codify_medium_multi/blob/main/config.json",
|
19 |
+
"smallcloudai/codify_3b_multi": "https://huggingface.co/smallcloudai/codify_3b_multi/blob/main/config.json",
|
20 |
+
}
|
21 |
+
|
22 |
+
|
23 |
+
class CodifyConfig(PretrainedConfig):
|
24 |
+
model_type = "codify"
|
25 |
+
keys_to_ignore_at_inference = ["past_key_values"]
|
26 |
+
attribute_map = {
|
27 |
+
"num_hidden_layers": "L",
|
28 |
+
"num_attention_heads": "attn_heads",
|
29 |
+
"hidden_size": "E",
|
30 |
+
}
|
31 |
+
|
32 |
+
def __init__(
|
33 |
+
self,
|
34 |
+
vocab_size=51305,
|
35 |
+
layer_norm_epsilon=1e-5,
|
36 |
+
initializer_range=0.02,
|
37 |
+
use_cache=True,
|
38 |
+
bos_token_id=1,
|
39 |
+
eos_token_id=2,
|
40 |
+
mlp_mult=4,
|
41 |
+
tie_word_embeddings=False,
|
42 |
+
**kwargs,
|
43 |
+
):
|
44 |
+
self.vocab_size = vocab_size
|
45 |
+
self.mlp_mult = mlp_mult
|
46 |
+
self.layer_norm_epsilon = layer_norm_epsilon
|
47 |
+
self.initializer_range = initializer_range
|
48 |
+
self.use_cache = use_cache
|
49 |
+
|
50 |
+
self.bos_token_id = bos_token_id
|
51 |
+
self.eos_token_id = eos_token_id
|
52 |
+
|
53 |
+
super().__init__(bos_token_id=bos_token_id, eos_token_id=eos_token_id,
|
54 |
+
tie_word_embeddings=tie_word_embeddings, **kwargs)
|
55 |
+
|
56 |
+
|
57 |
+
class CodifyOnnxConfig(OnnxConfigWithPast):
|
58 |
+
torch_onnx_minimum_version = version.parse("1.12")
|
59 |
+
|
60 |
+
def __init__(
|
61 |
+
self,
|
62 |
+
config: PretrainedConfig,
|
63 |
+
task: str = "default",
|
64 |
+
patching_specs: List[PatchingSpec] = None,
|
65 |
+
use_past: bool = False,
|
66 |
+
):
|
67 |
+
super().__init__(config, task=task, patching_specs=patching_specs, use_past=use_past)
|
68 |
+
if not getattr(self._config, "pad_token_id", None):
|
69 |
+
# TODO: how to do that better?
|
70 |
+
self._config.pad_token_id = 0
|
71 |
+
|
72 |
+
@property
|
73 |
+
def inputs(self) -> Mapping[str, Mapping[int, str]]:
|
74 |
+
common_inputs = OrderedDict({"input_ids": {0: "batch", 1: "sequence"}})
|
75 |
+
if self.use_past:
|
76 |
+
# BLOOM stores values on dynamic axis 2. For more details see: https://github.com/huggingface/transformers/pull/18344
|
77 |
+
self.fill_with_past_key_values_(common_inputs, direction="inputs", inverted_values_shape=True)
|
78 |
+
common_inputs["attention_mask"] = {0: "batch", 1: "past_sequence + sequence"}
|
79 |
+
else:
|
80 |
+
common_inputs["attention_mask"] = {0: "batch", 1: "sequence"}
|
81 |
+
|
82 |
+
return common_inputs
|
83 |
+
|
84 |
+
@property
|
85 |
+
def num_layers(self) -> int:
|
86 |
+
return self._config.num_hidden_layers
|
87 |
+
|
88 |
+
@property
|
89 |
+
def num_attention_heads(self) -> int:
|
90 |
+
return self._config.n_head
|
91 |
+
|
92 |
+
@property
|
93 |
+
def atol_for_validation(self) -> float:
|
94 |
+
return 1e-3
|
95 |
+
|
96 |
+
def generate_dummy_inputs(
|
97 |
+
self,
|
98 |
+
tokenizer: "PreTrainedTokenizer",
|
99 |
+
batch_size: int = -1,
|
100 |
+
seq_length: int = -1,
|
101 |
+
is_pair: bool = False,
|
102 |
+
framework: Optional["TensorType"] = None,
|
103 |
+
) -> Mapping[str, Any]:
|
104 |
+
common_inputs = super(OnnxConfigWithPast, self).generate_dummy_inputs(
|
105 |
+
tokenizer, batch_size=batch_size, seq_length=seq_length, is_pair=is_pair, framework=framework
|
106 |
+
)
|
107 |
+
|
108 |
+
# We need to order the input in the way they appears in the forward()
|
109 |
+
ordered_inputs = OrderedDict({"input_ids": common_inputs["input_ids"]})
|
110 |
+
|
111 |
+
# Need to add the past_keys
|
112 |
+
if self.use_past:
|
113 |
+
if not is_torch_available():
|
114 |
+
raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed.")
|
115 |
+
else:
|
116 |
+
import torch
|
117 |
+
|
118 |
+
batch, seqlen = common_inputs["input_ids"].shape
|
119 |
+
# Not using the same length for past_key_values
|
120 |
+
past_key_values_length = seqlen + 2
|
121 |
+
head_dim = self._config.hidden_size // self.num_attention_heads
|
122 |
+
past_key_shape = (
|
123 |
+
batch * self.num_attention_heads,
|
124 |
+
head_dim,
|
125 |
+
past_key_values_length,
|
126 |
+
)
|
127 |
+
past_value_shape = (
|
128 |
+
batch * self.num_attention_heads,
|
129 |
+
past_key_values_length,
|
130 |
+
head_dim,
|
131 |
+
)
|
132 |
+
ordered_inputs["past_key_values"] = [
|
133 |
+
(torch.zeros(past_key_shape), torch.zeros(past_value_shape)) for _ in range(self.num_layers)
|
134 |
+
]
|
135 |
+
|
136 |
+
ordered_inputs["attention_mask"] = common_inputs["attention_mask"]
|
137 |
+
if self.use_past:
|
138 |
+
mask_dtype = ordered_inputs["attention_mask"].dtype
|
139 |
+
ordered_inputs["attention_mask"] = torch.cat(
|
140 |
+
[ordered_inputs["attention_mask"], torch.ones(batch, past_key_values_length, dtype=mask_dtype)], dim=1
|
141 |
+
)
|
142 |
+
|
143 |
+
return ordered_inputs
|
144 |
+
|
145 |
+
@property
|
146 |
+
def default_onnx_opset(self) -> int:
|
147 |
+
return 13
|
148 |
+
|
149 |
+
|
150 |
+
from transformers import AutoConfig
|
151 |
+
|
152 |
+
AutoConfig.register(CodifyConfig.model_type, CodifyConfig)
|
codify/modeling_codify.py
ADDED
@@ -0,0 +1,773 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import math
|
2 |
+
import warnings
|
3 |
+
from typing import Optional, Tuple, Union
|
4 |
+
|
5 |
+
import torch
|
6 |
+
import torch.utils.checkpoint
|
7 |
+
from torch import nn
|
8 |
+
from torch.nn import CrossEntropyLoss, LayerNorm
|
9 |
+
from torch.nn import functional as F
|
10 |
+
from transformers.file_utils import add_code_sample_docstrings, add_start_docstrings, \
|
11 |
+
add_start_docstrings_to_model_forward
|
12 |
+
from transformers.modeling_outputs import (
|
13 |
+
BaseModelOutputWithPast,
|
14 |
+
CausalLMOutputWithPast,
|
15 |
+
)
|
16 |
+
from transformers.modeling_utils import PreTrainedModel
|
17 |
+
from transformers.utils import logging
|
18 |
+
|
19 |
+
from .configuration_codify import CodifyConfig
|
20 |
+
|
21 |
+
logger = logging.get_logger(__name__)
|
22 |
+
|
23 |
+
_CHECKPOINT_FOR_DOC = "smallcloudai/codify_medium_multi"
|
24 |
+
_CONFIG_FOR_DOC = "CodifyConfig"
|
25 |
+
_TOKENIZER_FOR_DOC = "CodifyTokenizerFast"
|
26 |
+
|
27 |
+
|
28 |
+
CODIFY_PRETRAINED_MODEL_ARCHIVE_LIST = [
|
29 |
+
"smallcloudai/codify_medium_multi",
|
30 |
+
"smallcloudai/codify_3b_multi"
|
31 |
+
]
|
32 |
+
|
33 |
+
def _make_causal_mask(
|
34 |
+
input_ids_shape: torch.Size, device: torch.device, past_key_values_length: int
|
35 |
+
) -> torch.BoolTensor:
|
36 |
+
"""
|
37 |
+
Make causal mask used for self-attention.
|
38 |
+
"""
|
39 |
+
batch_size, target_length = input_ids_shape
|
40 |
+
mask = torch.empty((target_length, target_length + past_key_values_length), dtype=torch.bool, device=device)
|
41 |
+
# ONNX doesn't support `torch.Tensor.triu` properly, thus we use this workaround
|
42 |
+
seq_ids = torch.arange(target_length, device=device)
|
43 |
+
mask[:, past_key_values_length:] = seq_ids[:, None] < seq_ids[None, :]
|
44 |
+
|
45 |
+
if past_key_values_length > 0:
|
46 |
+
mask[:, :past_key_values_length] = False
|
47 |
+
|
48 |
+
expanded_mask = mask[None, None, :, :].expand(batch_size, 1, target_length, target_length + past_key_values_length)
|
49 |
+
return expanded_mask
|
50 |
+
|
51 |
+
|
52 |
+
def _expand_mask(mask: torch.Tensor, tgt_length: int) -> torch.BoolTensor:
|
53 |
+
"""
|
54 |
+
Expands attention_mask from `[batch_size, src_length]` to `[batch_size, 1, tgt_length, src_length]`.
|
55 |
+
"""
|
56 |
+
batch_size, src_length = mask.shape
|
57 |
+
tgt_length = tgt_length if tgt_length is not None else src_length
|
58 |
+
|
59 |
+
expanded_mask = ~(mask[:, None, None, :].to(torch.bool))
|
60 |
+
return expanded_mask.expand(batch_size, 1, tgt_length, src_length)
|
61 |
+
|
62 |
+
|
63 |
+
def build_alibi_tensor(attention_mask: torch.Tensor, num_heads: int, dtype: torch.dtype) -> torch.Tensor:
|
64 |
+
"""
|
65 |
+
Link to paper: https://arxiv.org/abs/2108.12409 Alibi tensor is not causal as the original paper mentions, it
|
66 |
+
relies on a translation invariance of softmax for quick implementation: with l being a tensor, and a fixed value
|
67 |
+
`softmax(l+a) = softmax(l)`. Based on
|
68 |
+
https://github.com/ofirpress/attention_with_linear_biases/blob/a35aaca144e0eb6b789dfcb46784c4b8e31b7983/fairseq/models/transformer.py#L742
|
69 |
+
TODO @thomasw21 this doesn't work as nicely due to the masking strategy, and so masking varies slightly.
|
70 |
+
|
71 |
+
Args:
|
72 |
+
Returns tensor shaped (batch_size * num_heads, 1, max_seq_len)
|
73 |
+
attention_mask (`torch.Tensor`):
|
74 |
+
Token-wise attention mask, this should be of shape (batch_size, max_seq_len).
|
75 |
+
num_heads (`int`, *required*):
|
76 |
+
number of heads
|
77 |
+
dtype (`torch.dtype`, *optional*, default=`torch.bfloat16`):
|
78 |
+
dtype of the output tensor
|
79 |
+
"""
|
80 |
+
batch_size, seq_length = attention_mask.shape
|
81 |
+
closest_power_of_2 = 2 ** math.floor(math.log2(num_heads))
|
82 |
+
base = torch.tensor(
|
83 |
+
2 ** (-(2 ** -(math.log2(closest_power_of_2) - 3))), device=attention_mask.device, dtype=torch.float32
|
84 |
+
)
|
85 |
+
powers = torch.arange(1, 1 + closest_power_of_2, device=attention_mask.device, dtype=torch.int32)
|
86 |
+
slopes = torch.pow(base, powers)
|
87 |
+
|
88 |
+
if closest_power_of_2 != num_heads:
|
89 |
+
extra_base = torch.tensor(
|
90 |
+
2 ** (-(2 ** -(math.log2(2 * closest_power_of_2) - 3))), device=attention_mask.device, dtype=torch.float32
|
91 |
+
)
|
92 |
+
num_remaining_heads = min(closest_power_of_2, num_heads - closest_power_of_2)
|
93 |
+
extra_powers = torch.arange(1, 1 + 2 * num_remaining_heads, 2, device=attention_mask.device, dtype=torch.int32)
|
94 |
+
slopes = torch.cat([slopes, torch.pow(extra_base, extra_powers)], dim=0)
|
95 |
+
|
96 |
+
# Note: alibi will added to the attention bias that will be applied to the query, key product of attention
|
97 |
+
# => therefore alibi will have to be of shape (batch_size, num_heads, query_length, key_length)
|
98 |
+
# => here we set (batch_size=1, num_heads=num_heads, query_length=1, key_length=max_length)
|
99 |
+
# => the query_length dimension will then be broadcasted correctly
|
100 |
+
# This is more or less identical to T5's relative position bias:
|
101 |
+
# https://github.com/huggingface/transformers/blob/f681437203baa7671de3174b0fa583c349d9d5e1/src/transformers/models/t5/modeling_t5.py#L527
|
102 |
+
arange_tensor = ((attention_mask.cumsum(dim=-1)) * attention_mask)[:, None, :]
|
103 |
+
alibi = slopes[..., None] * arange_tensor
|
104 |
+
return alibi.reshape(batch_size * num_heads, 1, seq_length).to(dtype)
|
105 |
+
|
106 |
+
|
107 |
+
|
108 |
+
def codify_gelu_forward(x: torch.Tensor) -> torch.Tensor:
|
109 |
+
"""
|
110 |
+
Custom bias GELU function. Adapted from Megatron-DeepSpeed code. Here we use a simple implementation (inference) to
|
111 |
+
make the model jitable.
|
112 |
+
|
113 |
+
Args:
|
114 |
+
x (`torch.tensor`, *required*):
|
115 |
+
input hidden states
|
116 |
+
"""
|
117 |
+
return x * 0.5 * (1.0 + torch.tanh(0.79788456 * x * (1 + 0.044715 * x * x)))
|
118 |
+
|
119 |
+
|
120 |
+
def codify_gelu_back(g: torch.Tensor, x: torch.Tensor) -> torch.Tensor:
|
121 |
+
"""
|
122 |
+
gradient of tanh approximation of gelu gradient of actual gelu is: 0.5 * (1. + torch.erf(x * 0.70710678)) +
|
123 |
+
0.3989423 * x * torch.exp(-0.5 * x * x)
|
124 |
+
|
125 |
+
Args:
|
126 |
+
g (`torch.tensor`, *required*):
|
127 |
+
gradient output tensor
|
128 |
+
x (`torch.tensor`, *required*):
|
129 |
+
input tensor
|
130 |
+
"""
|
131 |
+
x = x[0] # x is a tuple of 1 element, needs to unpack it first
|
132 |
+
tanh_out = torch.tanh(0.79788456 * x * (1 + 0.044715 * x * x))
|
133 |
+
# sqrt(2/pi) * 3 * 0.044715 -> 0.1070322243
|
134 |
+
ff = 0.5 * x * ((1 - tanh_out * tanh_out) * (0.79788456 + 0.1070322243 * x * x)) + 0.5 * (1 + tanh_out)
|
135 |
+
return ff * g
|
136 |
+
|
137 |
+
|
138 |
+
class GeLUFunction(torch.autograd.Function):
|
139 |
+
@staticmethod
|
140 |
+
def forward(ctx, input: torch.Tensor) -> torch.Tensor:
|
141 |
+
ctx.save_for_backward(input)
|
142 |
+
return codify_gelu_forward(input)
|
143 |
+
|
144 |
+
@staticmethod
|
145 |
+
def backward(ctx, grad_output: torch.Tensor) -> torch.Tensor:
|
146 |
+
input = ctx.saved_tensors
|
147 |
+
tmp = codify_gelu_back(grad_output, input)
|
148 |
+
return tmp
|
149 |
+
|
150 |
+
|
151 |
+
class CodifyGelu(nn.Module):
|
152 |
+
def __init__(self):
|
153 |
+
super().__init__()
|
154 |
+
|
155 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
156 |
+
if self.training:
|
157 |
+
return GeLUFunction.apply(x)
|
158 |
+
else:
|
159 |
+
return codify_gelu_forward(x)
|
160 |
+
|
161 |
+
|
162 |
+
class CodifyAttention(nn.Module):
|
163 |
+
def __init__(self, config: CodifyConfig):
|
164 |
+
super().__init__()
|
165 |
+
|
166 |
+
self.hidden_size = config.hidden_size
|
167 |
+
self.num_heads = config.num_attention_heads
|
168 |
+
self.head_dim = self.hidden_size // self.num_heads
|
169 |
+
self.split_size = self.hidden_size
|
170 |
+
|
171 |
+
if self.head_dim * self.num_heads != self.hidden_size:
|
172 |
+
raise ValueError(
|
173 |
+
f"`hidden_size` must be divisible by num_heads (got `hidden_size`: {self.hidden_size} and `num_heads`:"
|
174 |
+
f" {self.num_heads})."
|
175 |
+
)
|
176 |
+
|
177 |
+
# Layer-wise attention scaling
|
178 |
+
# 8.0 = self.head_dim
|
179 |
+
self.inv_norm_factor = 8.0 / self.head_dim
|
180 |
+
self.beta = 1.0
|
181 |
+
|
182 |
+
self.query_key_value = nn.Linear(self.hidden_size, 3 * self.hidden_size, bias=True)
|
183 |
+
self.dense = nn.Linear(self.hidden_size, self.hidden_size)
|
184 |
+
|
185 |
+
def _split_heads(self, fused_qkv: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
|
186 |
+
"""
|
187 |
+
Split the last dimension into (num_heads, head_dim) without making any copies, results share same memory
|
188 |
+
storage as `fused_qkv`
|
189 |
+
|
190 |
+
Args:
|
191 |
+
fused_qkv (`torch.tensor`, *required*): [batch_size, seq_length, num_heads * 3 * head_dim]
|
192 |
+
|
193 |
+
Returns:
|
194 |
+
query: [batch_size, seq_length, num_heads, head_dim] key: [batch_size, seq_length, num_heads, head_dim]
|
195 |
+
value: [batch_size, seq_length, num_heads, head_dim]
|
196 |
+
"""
|
197 |
+
batch_size, seq_length, _ = fused_qkv.shape
|
198 |
+
q, k, v = fused_qkv.chunk(3, dim=-1)
|
199 |
+
return q.view(batch_size, seq_length, self.num_heads, self.head_dim),\
|
200 |
+
k.view(batch_size, seq_length, self.num_heads, self.head_dim),\
|
201 |
+
v.view(batch_size, seq_length, self.num_heads, self.head_dim)
|
202 |
+
|
203 |
+
def _merge_heads(self, x: torch.Tensor) -> torch.Tensor:
|
204 |
+
"""
|
205 |
+
Merge heads together over the last dimenstion
|
206 |
+
|
207 |
+
Args:
|
208 |
+
x: (`torch.tensor`, *required*): [batch_size * num_heads, seq_length, head_dim]
|
209 |
+
|
210 |
+
Returns:
|
211 |
+
torch.tensor: [batch_size, seq_length, num_heads * head_dim]
|
212 |
+
"""
|
213 |
+
# What we want to achieve is:
|
214 |
+
# batch_size * num_heads, seq_length, head_dim -> batch_size, seq_length, num_heads * head_dim
|
215 |
+
batch_size_and_num_heads, seq_length, _ = x.shape
|
216 |
+
batch_size = batch_size_and_num_heads // self.num_heads
|
217 |
+
|
218 |
+
# First view to decompose the batch size
|
219 |
+
# batch_size * num_heads, seq_length, head_dim -> batch_size, num_heads, seq_length, head_dim
|
220 |
+
x = x.view(batch_size, self.num_heads, seq_length, self.head_dim)
|
221 |
+
|
222 |
+
# batch_size, num_heads, seq_length, head_dim -> batch_size, seq_length, num_heads, head_dim
|
223 |
+
x = x.permute(0, 2, 1, 3)
|
224 |
+
|
225 |
+
# batch_size, seq_length, num_heads, head_dim -> batch_size, seq_length, num_heads * head_dim
|
226 |
+
return x.reshape(batch_size, seq_length, self.num_heads * self.head_dim)
|
227 |
+
|
228 |
+
def forward(
|
229 |
+
self,
|
230 |
+
hidden_states: torch.Tensor,
|
231 |
+
alibi: torch.Tensor,
|
232 |
+
attention_mask: torch.Tensor,
|
233 |
+
layer_past: Optional[Tuple[torch.Tensor, torch.Tensor]] = None,
|
234 |
+
head_mask: Optional[torch.Tensor] = None,
|
235 |
+
use_cache: bool = False,
|
236 |
+
output_attentions: bool = False,
|
237 |
+
):
|
238 |
+
fused_qkv = self.query_key_value(hidden_states) # [batch_size, seq_length, 3 x hidden_size]
|
239 |
+
|
240 |
+
# 3 x [batch_size, seq_length, num_heads, head_dim]
|
241 |
+
(query_layer, key_layer, value_layer) = self._split_heads(fused_qkv)
|
242 |
+
|
243 |
+
batch_size, q_length, _, _ = query_layer.shape
|
244 |
+
|
245 |
+
query_layer = query_layer.transpose(1, 2).reshape(batch_size * self.num_heads, q_length, self.head_dim)
|
246 |
+
key_layer = key_layer.permute(0, 2, 3, 1).reshape(batch_size * self.num_heads, self.head_dim, q_length)
|
247 |
+
value_layer = value_layer.transpose(1, 2).reshape(batch_size * self.num_heads, q_length, self.head_dim)
|
248 |
+
if layer_past is not None:
|
249 |
+
past_key, past_value = layer_past
|
250 |
+
# concatenate along seq_length dimension:
|
251 |
+
# - key: [batch_size * self.num_heads, head_dim, kv_length]
|
252 |
+
# - value: [batch_size * self.num_heads, kv_length, head_dim]
|
253 |
+
key_layer = torch.cat((past_key, key_layer), dim=2)
|
254 |
+
value_layer = torch.cat((past_value, value_layer), dim=1)
|
255 |
+
|
256 |
+
_, _, kv_length = key_layer.shape
|
257 |
+
|
258 |
+
if use_cache is True:
|
259 |
+
present = (key_layer, value_layer)
|
260 |
+
else:
|
261 |
+
present = None
|
262 |
+
|
263 |
+
# [batch_size * num_heads, q_length, kv_length]
|
264 |
+
# we use `torch.Tensor.baddbmm` instead of `torch.baddbmm` as the latter isn't supported by TorchScript v1.11
|
265 |
+
matmul_result = alibi.baddbmm(
|
266 |
+
batch1=query_layer,
|
267 |
+
batch2=key_layer,
|
268 |
+
beta=self.beta,
|
269 |
+
alpha=self.inv_norm_factor,
|
270 |
+
)
|
271 |
+
|
272 |
+
# change view to [batch_size, num_heads, q_length, kv_length]
|
273 |
+
attention_scores = matmul_result.view(batch_size, self.num_heads, q_length, kv_length)
|
274 |
+
|
275 |
+
# cast attention scores to fp32, compute scaled softmax and cast back to initial dtype - [batch_size, num_heads, q_length, kv_length]
|
276 |
+
input_dtype = attention_scores.dtype
|
277 |
+
# `float16` has a minimum value of -65504.0, whereas `bfloat16` and `float32` have a minimum value of `-3.4e+38`
|
278 |
+
if input_dtype == torch.float16:
|
279 |
+
attention_scores = attention_scores.to(torch.float)
|
280 |
+
attn_weights = torch.masked_fill(attention_scores, attention_mask, torch.finfo(attention_scores.dtype).min)
|
281 |
+
attention_probs = F.softmax(attn_weights, dim=-1, dtype=torch.float32).to(input_dtype)
|
282 |
+
|
283 |
+
if head_mask is not None:
|
284 |
+
attention_probs = attention_probs * head_mask
|
285 |
+
|
286 |
+
# change view [batch_size x num_heads, q_length, kv_length]
|
287 |
+
attention_probs_reshaped = attention_probs.view(batch_size * self.num_heads, q_length, kv_length)
|
288 |
+
|
289 |
+
# matmul: [batch_size * num_heads, q_length, head_dim]
|
290 |
+
context_layer = torch.bmm(attention_probs_reshaped, value_layer)
|
291 |
+
|
292 |
+
# change view [batch_size, num_heads, q_length, head_dim]
|
293 |
+
context_layer = self._merge_heads(context_layer)
|
294 |
+
|
295 |
+
output_tensor = self.dense(context_layer)
|
296 |
+
outputs = (output_tensor, present)
|
297 |
+
if output_attentions:
|
298 |
+
outputs += (attention_probs,)
|
299 |
+
|
300 |
+
return outputs
|
301 |
+
|
302 |
+
|
303 |
+
class CodifyMLP(nn.Module):
|
304 |
+
def __init__(self, config: CodifyConfig):
|
305 |
+
super().__init__()
|
306 |
+
hidden_size = config.hidden_size
|
307 |
+
self.dense_h_to_4h = nn.Linear(hidden_size, config.mlp_mult * hidden_size)
|
308 |
+
self.gelu_impl = CodifyGelu()
|
309 |
+
self.dense_4h_to_h = nn.Linear(config.mlp_mult * hidden_size, hidden_size)
|
310 |
+
|
311 |
+
def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
|
312 |
+
hidden_states = self.gelu_impl(self.dense_h_to_4h(hidden_states))
|
313 |
+
output = self.dense_4h_to_h(hidden_states)
|
314 |
+
return output
|
315 |
+
|
316 |
+
|
317 |
+
class CodifyBlock(nn.Module):
|
318 |
+
def __init__(self, config: CodifyConfig):
|
319 |
+
super().__init__()
|
320 |
+
hidden_size = config.hidden_size
|
321 |
+
|
322 |
+
self.input_layernorm = LayerNorm(hidden_size, eps=config.layer_norm_epsilon)
|
323 |
+
self.num_heads = config.num_attention_heads
|
324 |
+
self.self_attention = CodifyAttention(config)
|
325 |
+
self.post_attention_layernorm = LayerNorm(hidden_size, eps=config.layer_norm_epsilon)
|
326 |
+
self.mlp = CodifyMLP(config)
|
327 |
+
|
328 |
+
def forward(
|
329 |
+
self,
|
330 |
+
hidden_states: torch.Tensor,
|
331 |
+
alibi: torch.Tensor,
|
332 |
+
attention_mask: torch.Tensor,
|
333 |
+
layer_past: Optional[Tuple[torch.Tensor, torch.Tensor]] = None,
|
334 |
+
head_mask: Optional[torch.Tensor] = None,
|
335 |
+
use_cache: bool = False,
|
336 |
+
output_attentions: bool = False,
|
337 |
+
):
|
338 |
+
# hidden_states: [batch_size, seq_length, hidden_size]
|
339 |
+
|
340 |
+
# Layer norm at the beginning of the transformer layer.
|
341 |
+
layernorm_output = self.input_layernorm(hidden_states)
|
342 |
+
|
343 |
+
# Self attention.
|
344 |
+
attn_outputs = self.self_attention(
|
345 |
+
layernorm_output,
|
346 |
+
layer_past=layer_past,
|
347 |
+
attention_mask=attention_mask,
|
348 |
+
alibi=alibi,
|
349 |
+
head_mask=head_mask,
|
350 |
+
use_cache=use_cache,
|
351 |
+
output_attentions=output_attentions,
|
352 |
+
)
|
353 |
+
|
354 |
+
attention_output = attn_outputs[0]
|
355 |
+
outputs = attn_outputs[1:]
|
356 |
+
|
357 |
+
attention_mix = attention_output + hidden_states
|
358 |
+
layernorm_output = self.post_attention_layernorm(attention_mix)
|
359 |
+
|
360 |
+
# MLP.
|
361 |
+
output = self.mlp(layernorm_output)
|
362 |
+
output = output + attention_output + hidden_states
|
363 |
+
|
364 |
+
if use_cache:
|
365 |
+
outputs = (output,) + outputs
|
366 |
+
else:
|
367 |
+
outputs = (output,) + outputs[1:]
|
368 |
+
|
369 |
+
return outputs # hidden_states, present, attentions
|
370 |
+
|
371 |
+
class CodifyPreTrainedModel(PreTrainedModel):
|
372 |
+
_keys_to_ignore_on_load_missing = [r"h.*.self_attention.scale_mask_softmax.causal_mask", r"lm_head.weight"]
|
373 |
+
"""
|
374 |
+
An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
|
375 |
+
models.
|
376 |
+
"""
|
377 |
+
|
378 |
+
config_class = CodifyConfig
|
379 |
+
base_model_prefix = "transformer"
|
380 |
+
supports_gradient_checkpointing = True
|
381 |
+
_no_split_modules = ["CodifyBlock"]
|
382 |
+
|
383 |
+
def __init__(self, *inputs, **kwargs):
|
384 |
+
super().__init__(*inputs, **kwargs)
|
385 |
+
|
386 |
+
def _init_weights(self, module: nn.Module):
|
387 |
+
"""Initialize the weights."""
|
388 |
+
if isinstance(module, nn.Linear):
|
389 |
+
# Slightly different from the TF version which uses truncated_normal for initialization
|
390 |
+
# cf https://github.com/pytorch/pytorch/pull/5617
|
391 |
+
module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
|
392 |
+
if module.bias is not None:
|
393 |
+
module.bias.data.zero_()
|
394 |
+
elif isinstance(module, nn.Embedding):
|
395 |
+
module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
|
396 |
+
if module.padding_idx is not None:
|
397 |
+
module.weight.data[module.padding_idx].zero_()
|
398 |
+
elif isinstance(module, LayerNorm):
|
399 |
+
module.bias.data.zero_()
|
400 |
+
module.weight.data.fill_(1.0)
|
401 |
+
|
402 |
+
def _set_gradient_checkpointing(self, module: nn.Module, value: bool = False):
|
403 |
+
if isinstance(module, CodifyModel):
|
404 |
+
module.gradient_checkpointing = value
|
405 |
+
|
406 |
+
@staticmethod
|
407 |
+
def _convert_to_standard_cache(
|
408 |
+
past_key_value: Tuple[Tuple[torch.Tensor, torch.Tensor]], batch_size: int
|
409 |
+
) -> Tuple[Tuple[torch.Tensor, torch.Tensor]]:
|
410 |
+
"""
|
411 |
+
Standardizes the format of the cache so as to match most implementations, i.e. to tuple(tuple([batch_size,
|
412 |
+
num_heads, ...]))
|
413 |
+
"""
|
414 |
+
batch_size_times_num_heads, head_dim, seq_length = past_key_value[0][0].shape
|
415 |
+
num_heads = batch_size_times_num_heads // batch_size
|
416 |
+
# key: [batch_size * num_heads, head_dim, seq_length] -> [batch_size, num_heads, head_dim, seq_length]
|
417 |
+
# value: [batch_size * num_heads, seq_length, head_dim] -> [batch_size, num_heads, seq_length, head_dim]
|
418 |
+
return tuple(
|
419 |
+
(
|
420 |
+
layer_past[0].view(batch_size, num_heads, head_dim, seq_length),
|
421 |
+
layer_past[1].view(batch_size, num_heads, seq_length, head_dim),
|
422 |
+
)
|
423 |
+
for layer_past in past_key_value
|
424 |
+
)
|
425 |
+
|
426 |
+
@staticmethod
|
427 |
+
def _convert_to_codify_cache(
|
428 |
+
past_key_value: Tuple[Tuple[torch.Tensor, torch.Tensor]]
|
429 |
+
) -> Tuple[Tuple[torch.Tensor, torch.Tensor]]:
|
430 |
+
batch_size, num_heads, head_dim, seq_length = past_key_value[0][0].shape
|
431 |
+
batch_size_times_num_heads = batch_size * num_heads
|
432 |
+
# key: [batch_size, num_heads, head_dim, seq_length] -> [batch_size * num_heads, head_dim, seq_length]
|
433 |
+
# value: [batch_size, num_heads, seq_length, head_dim] -> [batch_size * num_heads, seq_length, head_dim]
|
434 |
+
return tuple(
|
435 |
+
(
|
436 |
+
layer_past[0].view(batch_size_times_num_heads, head_dim, seq_length),
|
437 |
+
layer_past[1].view(batch_size_times_num_heads, seq_length, head_dim),
|
438 |
+
)
|
439 |
+
for layer_past in past_key_value
|
440 |
+
)
|
441 |
+
|
442 |
+
class CodifyModel(CodifyPreTrainedModel):
|
443 |
+
def __init__(self, config: CodifyConfig):
|
444 |
+
super().__init__(config)
|
445 |
+
|
446 |
+
self.embed_dim = config.hidden_size
|
447 |
+
self.num_heads = config.num_attention_heads
|
448 |
+
|
449 |
+
# Embedding
|
450 |
+
self.word_embeddings = nn.Embedding(config.vocab_size, self.embed_dim)
|
451 |
+
|
452 |
+
# Transformer blocks
|
453 |
+
self.h = nn.ModuleList([CodifyBlock(config) for _ in range(config.num_hidden_layers)])
|
454 |
+
|
455 |
+
# Final Layer Norm
|
456 |
+
self.ln_f = LayerNorm(self.embed_dim, eps=config.layer_norm_epsilon)
|
457 |
+
|
458 |
+
self.gradient_checkpointing = False
|
459 |
+
|
460 |
+
# Initialize weights and apply final processing
|
461 |
+
self.post_init()
|
462 |
+
|
463 |
+
def get_input_embeddings(self):
|
464 |
+
return self.word_embeddings
|
465 |
+
|
466 |
+
def _prepare_attn_mask(
|
467 |
+
self, attention_mask: torch.Tensor, input_shape: Tuple[int, int], past_key_values_length: int
|
468 |
+
) -> torch.BoolTensor:
|
469 |
+
# create causal mask
|
470 |
+
# [batch_size, seq_length] -> [batch_size, 1, tgt_length, src_length]
|
471 |
+
combined_attention_mask = None
|
472 |
+
device = attention_mask.device
|
473 |
+
_, src_length = input_shape
|
474 |
+
|
475 |
+
if src_length > 1:
|
476 |
+
combined_attention_mask = _make_causal_mask(
|
477 |
+
input_shape, device=device, past_key_values_length=past_key_values_length
|
478 |
+
)
|
479 |
+
|
480 |
+
# [batch_size, seq_length] -> [batch_size, 1, tgt_length, src_length]
|
481 |
+
expanded_attn_mask = _expand_mask(attention_mask, tgt_length=src_length)
|
482 |
+
combined_attention_mask = (
|
483 |
+
expanded_attn_mask if combined_attention_mask is None else expanded_attn_mask | combined_attention_mask
|
484 |
+
)
|
485 |
+
|
486 |
+
return combined_attention_mask
|
487 |
+
|
488 |
+
def set_input_embeddings(self, new_embeddings: torch.Tensor):
|
489 |
+
self.word_embeddings = new_embeddings
|
490 |
+
|
491 |
+
@add_code_sample_docstrings(
|
492 |
+
processor_class=_TOKENIZER_FOR_DOC,
|
493 |
+
checkpoint=_CHECKPOINT_FOR_DOC,
|
494 |
+
output_type=BaseModelOutputWithPast,
|
495 |
+
config_class=_CONFIG_FOR_DOC,
|
496 |
+
)
|
497 |
+
def forward(
|
498 |
+
self,
|
499 |
+
input_ids: Optional[torch.LongTensor] = None,
|
500 |
+
past_key_values: Optional[Tuple[Tuple[torch.Tensor, torch.Tensor], ...]] = None,
|
501 |
+
attention_mask: Optional[torch.Tensor] = None,
|
502 |
+
head_mask: Optional[torch.LongTensor] = None,
|
503 |
+
inputs_embeds: Optional[torch.LongTensor] = None,
|
504 |
+
use_cache: Optional[bool] = None,
|
505 |
+
output_attentions: Optional[bool] = None,
|
506 |
+
output_hidden_states: Optional[bool] = None,
|
507 |
+
return_dict: Optional[bool] = None,
|
508 |
+
**deprecated_arguments
|
509 |
+
) -> Union[Tuple[torch.Tensor, ...], BaseModelOutputWithPast]:
|
510 |
+
if deprecated_arguments.pop("position_ids", False) is not False:
|
511 |
+
# `position_ids` could have been `torch.Tensor` or `None` so defaulting pop to `False` allows to detect if users were passing explicitly `None`
|
512 |
+
warnings.warn(
|
513 |
+
"`position_ids` have no functionality in Codify and will be removed in v5.0.0. You can safely ignore"
|
514 |
+
" passing `position_ids`.",
|
515 |
+
FutureWarning,
|
516 |
+
)
|
517 |
+
if len(deprecated_arguments) > 0:
|
518 |
+
raise ValueError(f"Got unexpected arguments: {deprecated_arguments}")
|
519 |
+
|
520 |
+
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
521 |
+
output_hidden_states = (
|
522 |
+
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
523 |
+
)
|
524 |
+
use_cache = use_cache if use_cache is not None else self.config.use_cache
|
525 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
526 |
+
|
527 |
+
if input_ids is not None and inputs_embeds is not None:
|
528 |
+
raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
|
529 |
+
elif input_ids is not None:
|
530 |
+
batch_size, seq_length = input_ids.shape
|
531 |
+
elif inputs_embeds is not None:
|
532 |
+
batch_size, seq_length, _ = inputs_embeds.shape
|
533 |
+
else:
|
534 |
+
raise ValueError("You have to specify either input_ids or inputs_embeds")
|
535 |
+
|
536 |
+
if past_key_values is None:
|
537 |
+
past_key_values = tuple([None] * len(self.h))
|
538 |
+
|
539 |
+
# Prepare head mask if needed
|
540 |
+
# 1.0 in head_mask indicate we keep the head
|
541 |
+
# attention_probs has shape batch_size x num_heads x N x N
|
542 |
+
# head_mask has shape n_layer x batch x num_heads x N x N
|
543 |
+
head_mask = self.get_head_mask(head_mask, self.config.num_hidden_layers)
|
544 |
+
|
545 |
+
if inputs_embeds is None:
|
546 |
+
inputs_embeds = self.word_embeddings(input_ids)
|
547 |
+
|
548 |
+
hidden_states = inputs_embeds
|
549 |
+
|
550 |
+
presents = () if use_cache else None
|
551 |
+
all_self_attentions = () if output_attentions else None
|
552 |
+
all_hidden_states = () if output_hidden_states else None
|
553 |
+
|
554 |
+
# Compute alibi tensor: check build_alibi_tensor documentation
|
555 |
+
seq_length_with_past = seq_length
|
556 |
+
past_key_values_length = 0
|
557 |
+
if past_key_values[0] is not None:
|
558 |
+
past_key_values_length = past_key_values[0][0].shape[2]
|
559 |
+
seq_length_with_past = seq_length_with_past + past_key_values_length
|
560 |
+
if attention_mask is None:
|
561 |
+
attention_mask = torch.ones((batch_size, seq_length_with_past), device=hidden_states.device)
|
562 |
+
else:
|
563 |
+
attention_mask = attention_mask.to(hidden_states.device)
|
564 |
+
|
565 |
+
alibi = build_alibi_tensor(attention_mask, self.num_heads, dtype=hidden_states.dtype)
|
566 |
+
|
567 |
+
causal_mask = self._prepare_attn_mask(
|
568 |
+
attention_mask,
|
569 |
+
input_shape=(batch_size, seq_length),
|
570 |
+
past_key_values_length=past_key_values_length,
|
571 |
+
)
|
572 |
+
|
573 |
+
for i, (block, layer_past) in enumerate(zip(self.h, past_key_values)):
|
574 |
+
|
575 |
+
if output_hidden_states:
|
576 |
+
all_hidden_states = all_hidden_states + (hidden_states,)
|
577 |
+
|
578 |
+
if self.gradient_checkpointing and self.training:
|
579 |
+
|
580 |
+
if use_cache:
|
581 |
+
logger.warning(
|
582 |
+
"`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..."
|
583 |
+
)
|
584 |
+
use_cache = False
|
585 |
+
|
586 |
+
def create_custom_forward(module):
|
587 |
+
def custom_forward(*inputs):
|
588 |
+
# None for past_key_value
|
589 |
+
return module(*inputs, use_cache=use_cache, output_attentions=output_attentions)
|
590 |
+
|
591 |
+
return custom_forward
|
592 |
+
|
593 |
+
outputs = torch.utils.checkpoint.checkpoint(
|
594 |
+
create_custom_forward(block),
|
595 |
+
hidden_states,
|
596 |
+
alibi,
|
597 |
+
causal_mask,
|
598 |
+
head_mask[i],
|
599 |
+
)
|
600 |
+
else:
|
601 |
+
outputs = block(
|
602 |
+
hidden_states,
|
603 |
+
layer_past=layer_past,
|
604 |
+
attention_mask=causal_mask,
|
605 |
+
head_mask=head_mask[i],
|
606 |
+
use_cache=use_cache,
|
607 |
+
output_attentions=output_attentions,
|
608 |
+
alibi=alibi,
|
609 |
+
)
|
610 |
+
|
611 |
+
hidden_states = outputs[0]
|
612 |
+
if use_cache is True:
|
613 |
+
presents = presents + (outputs[1],)
|
614 |
+
|
615 |
+
if output_attentions:
|
616 |
+
all_self_attentions = all_self_attentions + (outputs[2 if use_cache else 1],)
|
617 |
+
|
618 |
+
# Add last hidden state
|
619 |
+
hidden_states = self.ln_f(hidden_states)
|
620 |
+
|
621 |
+
if output_hidden_states:
|
622 |
+
all_hidden_states = all_hidden_states + (hidden_states,)
|
623 |
+
|
624 |
+
if not return_dict:
|
625 |
+
return tuple(v for v in [hidden_states, presents, all_hidden_states, all_self_attentions] if v is not None)
|
626 |
+
|
627 |
+
return BaseModelOutputWithPast(
|
628 |
+
last_hidden_state=hidden_states,
|
629 |
+
past_key_values=presents,
|
630 |
+
hidden_states=all_hidden_states,
|
631 |
+
attentions=all_self_attentions,
|
632 |
+
)
|
633 |
+
|
634 |
+
|
635 |
+
class CodifyForCausalLM(CodifyPreTrainedModel):
|
636 |
+
_keys_to_ignore_on_load_missing = [r"h.*.self_attention.scale_mask_softmax.causal_mask", r"lm_head.weight"]
|
637 |
+
|
638 |
+
def __init__(self, config: CodifyConfig):
|
639 |
+
super().__init__(config)
|
640 |
+
self.transformer = CodifyModel(config)
|
641 |
+
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
|
642 |
+
|
643 |
+
# Initialize weights and apply final processing
|
644 |
+
self.post_init()
|
645 |
+
|
646 |
+
def get_output_embeddings(self):
|
647 |
+
return self.lm_head
|
648 |
+
|
649 |
+
def set_output_embeddings(self, new_embeddings: torch.Tensor):
|
650 |
+
self.lm_head = new_embeddings
|
651 |
+
|
652 |
+
def prepare_inputs_for_generation(
|
653 |
+
self,
|
654 |
+
input_ids: torch.LongTensor,
|
655 |
+
past: Optional[torch.Tensor] = None,
|
656 |
+
attention_mask: Optional[torch.Tensor] = None,
|
657 |
+
**kwargs
|
658 |
+
) -> dict:
|
659 |
+
# only last token for input_ids if past is not None
|
660 |
+
if past:
|
661 |
+
input_ids = input_ids[:, -1].unsqueeze(-1)
|
662 |
+
|
663 |
+
if past[0][0].shape[0] == input_ids.shape[0]:
|
664 |
+
past = self._convert_to_codify_cache(past)
|
665 |
+
|
666 |
+
return {
|
667 |
+
"input_ids": input_ids,
|
668 |
+
"past_key_values": past,
|
669 |
+
"use_cache": kwargs.get("use_cache"),
|
670 |
+
"attention_mask": attention_mask,
|
671 |
+
}
|
672 |
+
|
673 |
+
@add_code_sample_docstrings(
|
674 |
+
processor_class=_TOKENIZER_FOR_DOC,
|
675 |
+
checkpoint=_CHECKPOINT_FOR_DOC,
|
676 |
+
output_type=CausalLMOutputWithPast,
|
677 |
+
config_class=_CONFIG_FOR_DOC,
|
678 |
+
)
|
679 |
+
def forward(
|
680 |
+
self,
|
681 |
+
input_ids: Optional[torch.LongTensor] = None,
|
682 |
+
past_key_values: Optional[Tuple[Tuple[torch.Tensor, torch.Tensor], ...]] = None,
|
683 |
+
attention_mask: Optional[torch.Tensor] = None,
|
684 |
+
head_mask: Optional[torch.Tensor] = None,
|
685 |
+
inputs_embeds: Optional[torch.Tensor] = None,
|
686 |
+
labels: Optional[torch.Tensor] = None,
|
687 |
+
use_cache: Optional[bool] = None,
|
688 |
+
output_attentions: Optional[bool] = None,
|
689 |
+
output_hidden_states: Optional[bool] = None,
|
690 |
+
return_dict: Optional[bool] = None,
|
691 |
+
**deprecated_arguments
|
692 |
+
) -> Union[Tuple[torch.Tensor], CausalLMOutputWithPast]:
|
693 |
+
r"""
|
694 |
+
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
695 |
+
Labels for language modeling. Note that the labels **are shifted** inside the model, i.e. you can set
|
696 |
+
`labels = input_ids` Indices are selected in `[-100, 0, ..., config.vocab_size]` All labels set to `-100`
|
697 |
+
are ignored (masked), the loss is only computed for labels in `[0, ..., config.vocab_size]`
|
698 |
+
"""
|
699 |
+
if deprecated_arguments.pop("position_ids", False) is not False:
|
700 |
+
# `position_ids` could have been `torch.Tensor` or `None` so defaulting pop to `False` allows to detect if users were passing explicitly `None`
|
701 |
+
warnings.warn(
|
702 |
+
"`position_ids` have no functionality in Codify and will be removed in v5.0.0. You can safely ignore"
|
703 |
+
" passing `position_ids`.",
|
704 |
+
FutureWarning,
|
705 |
+
)
|
706 |
+
if len(deprecated_arguments) > 0:
|
707 |
+
raise ValueError(f"Got unexpected arguments: {deprecated_arguments}")
|
708 |
+
|
709 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
710 |
+
|
711 |
+
transformer_outputs = self.transformer(
|
712 |
+
input_ids,
|
713 |
+
past_key_values=past_key_values,
|
714 |
+
attention_mask=attention_mask,
|
715 |
+
head_mask=head_mask,
|
716 |
+
inputs_embeds=inputs_embeds,
|
717 |
+
use_cache=use_cache,
|
718 |
+
output_attentions=output_attentions,
|
719 |
+
output_hidden_states=output_hidden_states,
|
720 |
+
return_dict=return_dict,
|
721 |
+
)
|
722 |
+
hidden_states = transformer_outputs[0]
|
723 |
+
|
724 |
+
lm_logits = self.lm_head(hidden_states / 2.0)
|
725 |
+
|
726 |
+
loss = None
|
727 |
+
if labels is not None:
|
728 |
+
# Shift so that tokens < n predict n
|
729 |
+
shift_logits = lm_logits[..., :-1, :].contiguous()
|
730 |
+
shift_labels = labels[..., 1:].contiguous()
|
731 |
+
batch_size, seq_length, vocab_size = shift_logits.shape
|
732 |
+
# Flatten the tokens
|
733 |
+
loss_fct = CrossEntropyLoss()
|
734 |
+
loss = loss_fct(
|
735 |
+
shift_logits.view(batch_size * seq_length, vocab_size), shift_labels.view(batch_size * seq_length)
|
736 |
+
)
|
737 |
+
|
738 |
+
if not return_dict:
|
739 |
+
output = (lm_logits,) + transformer_outputs[1:]
|
740 |
+
return ((loss,) + output) if loss is not None else output
|
741 |
+
|
742 |
+
return CausalLMOutputWithPast(
|
743 |
+
loss=loss,
|
744 |
+
logits=lm_logits,
|
745 |
+
past_key_values=transformer_outputs.past_key_values,
|
746 |
+
hidden_states=transformer_outputs.hidden_states,
|
747 |
+
attentions=transformer_outputs.attentions,
|
748 |
+
)
|
749 |
+
|
750 |
+
def _reorder_cache(
|
751 |
+
self, past: Tuple[Tuple[torch.Tensor, torch.Tensor], ...], beam_idx: torch.LongTensor
|
752 |
+
) -> Tuple[Tuple[torch.Tensor, torch.Tensor], ...]:
|
753 |
+
"""
|
754 |
+
This function is used to re-order the `past_key_values` cache if [`~PreTrainedModel.beam_search`] or
|
755 |
+
[`~PreTrainedModel.beam_sample`] is called. This is required to match `past_key_values` with the correct
|
756 |
+
beam_idx at every generation step.
|
757 |
+
|
758 |
+
Output shares the same memory storage as `past`.
|
759 |
+
"""
|
760 |
+
standardized_past = self._convert_to_standard_cache(past, batch_size=len(beam_idx))
|
761 |
+
|
762 |
+
# Get a copy of `beam_idx` on all the devices where we need those indices.
|
763 |
+
device_to_beam_idx = {
|
764 |
+
past_state.device: beam_idx.to(past_state.device) for layer_past in past for past_state in layer_past
|
765 |
+
}
|
766 |
+
reordered_past = tuple(
|
767 |
+
(
|
768 |
+
layer_past[0].index_select(0, device_to_beam_idx[layer_past[0].device]),
|
769 |
+
layer_past[1].index_select(0, device_to_beam_idx[layer_past[0].device]),
|
770 |
+
)
|
771 |
+
for layer_past in standardized_past
|
772 |
+
)
|
773 |
+
return self._convert_to_codify_cache(reordered_past)
|
codify/tokenization_codify_fast.py
ADDED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
from typing import TYPE_CHECKING, List, Optional, Tuple
|
3 |
+
|
4 |
+
from tokenizers import pre_tokenizers
|
5 |
+
|
6 |
+
from transformers.tokenization_utils_base import BatchEncoding
|
7 |
+
from transformers.tokenization_utils_fast import PreTrainedTokenizerFast
|
8 |
+
from transformers.utils import logging
|
9 |
+
|
10 |
+
|
11 |
+
if TYPE_CHECKING:
|
12 |
+
from transformers.pipelines.conversational import Conversation
|
13 |
+
|
14 |
+
|
15 |
+
logger = logging.get_logger(__name__)
|
16 |
+
|
17 |
+
VOCAB_FILES_NAMES = {"tokenizer_file": "tokenizer.json"}
|
18 |
+
|
19 |
+
PRETRAINED_VOCAB_FILES_MAP = {
|
20 |
+
"tokenizer_file": {
|
21 |
+
"smallcloudai/codify_medium_multi": "https://huggingface.co/smallcloudai/codify_medium_multi/blob/main/tokenizer.json",
|
22 |
+
"smallcloudai/codify_3b_multi": "https://huggingface.co/smallcloudai/codify_3b_multi/blob/main/tokenizer.json",
|
23 |
+
},
|
24 |
+
}
|
25 |
+
|
26 |
+
|
27 |
+
class CodifyTokenizerFast(PreTrainedTokenizerFast):
|
28 |
+
vocab_files_names = VOCAB_FILES_NAMES
|
29 |
+
pretrained_vocab_files_map = PRETRAINED_VOCAB_FILES_MAP
|
30 |
+
model_input_names = ["input_ids", "attention_mask"]
|
31 |
+
slow_tokenizer_class = None
|
32 |
+
|
33 |
+
def __init__(
|
34 |
+
self,
|
35 |
+
vocab_file=None,
|
36 |
+
merges_file=None,
|
37 |
+
tokenizer_file=None,
|
38 |
+
unk_token="<|endoftext|>",
|
39 |
+
bos_token="<|endoftext|>",
|
40 |
+
eos_token="<|endoftext|>",
|
41 |
+
add_prefix_space=False,
|
42 |
+
**kwargs
|
43 |
+
):
|
44 |
+
super().__init__(
|
45 |
+
vocab_file,
|
46 |
+
merges_file,
|
47 |
+
tokenizer_file=tokenizer_file,
|
48 |
+
unk_token=unk_token,
|
49 |
+
bos_token=bos_token,
|
50 |
+
eos_token=eos_token,
|
51 |
+
add_prefix_space=add_prefix_space,
|
52 |
+
**kwargs,
|
53 |
+
)
|
54 |
+
pre_tok_state = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__())
|
55 |
+
if pre_tok_state.get("add_prefix_space", add_prefix_space) != add_prefix_space:
|
56 |
+
pre_tok_class = getattr(pre_tokenizers, pre_tok_state.pop("type"))
|
57 |
+
pre_tok_state["add_prefix_space"] = add_prefix_space
|
58 |
+
self.backend_tokenizer.pre_tokenizer = pre_tok_class(**pre_tok_state)
|
59 |
+
|
60 |
+
self.add_prefix_space = add_prefix_space
|
61 |
+
|
62 |
+
def _batch_encode_plus(self, *args, **kwargs) -> BatchEncoding:
|
63 |
+
is_split_into_words = kwargs.get("is_split_into_words", False)
|
64 |
+
if not (self.add_prefix_space or not is_split_into_words):
|
65 |
+
raise Exception(
|
66 |
+
f"You need to instantiate {self.__class__.__name__} with add_prefix_space=True to use it with"
|
67 |
+
" pretokenized inputs."
|
68 |
+
)
|
69 |
+
|
70 |
+
return super()._batch_encode_plus(*args, **kwargs)
|
71 |
+
|
72 |
+
def _encode_plus(self, *args, **kwargs) -> BatchEncoding:
|
73 |
+
is_split_into_words = kwargs.get("is_split_into_words", False)
|
74 |
+
|
75 |
+
if not (self.add_prefix_space or not is_split_into_words):
|
76 |
+
raise Exception(
|
77 |
+
f"You need to instantiate {self.__class__.__name__} with add_prefix_space=True to use it with"
|
78 |
+
" pretokenized inputs."
|
79 |
+
)
|
80 |
+
|
81 |
+
return super()._encode_plus(*args, **kwargs)
|
82 |
+
|
83 |
+
def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]:
|
84 |
+
files = self._tokenizer.model.save(save_directory, name=filename_prefix)
|
85 |
+
return tuple(files)
|
86 |
+
|
87 |
+
def _build_conversation_input_ids(self, conversation: "Conversation") -> List[int]:
|
88 |
+
"""This corresponds to DialoGPT variants of models."""
|
89 |
+
input_ids = []
|
90 |
+
for is_user, text in conversation.iter_texts():
|
91 |
+
input_ids.extend(self.encode(text, add_special_tokens=False) + [self.eos_token_id])
|
92 |
+
|
93 |
+
if len(input_ids) > self.model_max_length:
|
94 |
+
input_ids = input_ids[-self.model_max_length :]
|
95 |
+
return input_ids
|