Add: Support for the Falcon2 11B architecture (#1886)

# What does this PR do?

<!--
Congratulations! You've made it this far! You're not quite done yet
though.

Once merged, your PR is going to appear in the release notes with the
title you set, so make sure it's a great title that fully reflects the
extent of your awesome contribution.

Then, please replace this with a description of the change and which
issue is fixed (if applicable). Please also include relevant motivation
and context. List any dependencies (if any) that are required for this
change.

Once you're done, someone will review your PR shortly (see the section
"Who can review?" below to tag some potential reviewers). They may
suggest changes to make the code even better. If no one reviewed your PR
after a week has passed, don't hesitate to post a new comment
@-mentioning the same persons---sometimes notifications get lost.
-->

<!-- Remove if not applicable -->

Add's support for the Falcon2 11B model architecture.


## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the
other checks if that's the case).
- [x] Did you read the [contributor
guideline](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#start-contributing-pull-requests),
      Pull Request section?
- [ ] Was this discussed/approved via a Github issue or the
[forum](https://discuss.huggingface.co/)? Please add a link
      to it if that's the case.
- [ ] Did you make sure to update the documentation with your changes?
Here are the
[documentation
guidelines](https://github.com/huggingface/transformers/tree/main/docs),
and
[here are tips on formatting
docstrings](https://github.com/huggingface/transformers/tree/main/docs#writing-source-documentation).
- [ ] Did you write any new necessary tests?


## Who can review?

Anyone in the community is free to review the PR once the tests have
passed. Feel free to tag
members/contributors who may be interested in your PR.

<!-- Your PR will be replied to more quickly if you can figure out the
right person to tag with @


@OlivierDehaene OR @Narsil

 -->

---------

Signed-off-by: Raphael Glon <oOraph@users.noreply.github.com>
Signed-off-by: Wang, Yi A <yi.a.wang@intel.com>
Co-authored-by: OlivierDehaene <olivier@huggingface.co>
Co-authored-by: Nicolas Patry <patry.nicolas@protonmail.com>
Co-authored-by: oOraph <13552058+oOraph@users.noreply.github.com>
Co-authored-by: Raphael Glon <oOraph@users.noreply.github.com>
Co-authored-by: Julien Chaumond <julien@huggingface.co>
Co-authored-by: OlivierDehaene <23298448+OlivierDehaene@users.noreply.github.com>
Co-authored-by: abhishek thakur <1183441+abhishekkrthakur@users.noreply.github.com>
Co-authored-by: Dong Shin <d0104.shin@gmail.com>
Co-authored-by: Christof Weickhardt <christof@weickhardt.ch>
Co-authored-by: Ikko Eltociear Ashimine <eltociear@gmail.com>
Co-authored-by: drbh <david.richard.holtz@gmail.com>
Co-authored-by: Lucain <lucain@huggingface.co>
Co-authored-by: fxmarty <9808326+fxmarty@users.noreply.github.com>
Co-authored-by: Moritz Laurer <41862082+MoritzLaurer@users.noreply.github.com>
Co-authored-by: dr3s <dr3s@users.noreply.github.com>
Co-authored-by: Wang, Yi <yi.a.wang@intel.com>
Co-authored-by: Morgan Funtowicz <funtowiczmo@gmail.com>
Co-authored-by: Maziyar Panahi <maziyar.panahi@iscpif.fr>
Co-authored-by: Brandon Royal <2762697+brandonroyal@users.noreply.github.com>
Co-authored-by: Mishig <mishig.davaadorj@coloradocollege.edu>
Co-authored-by: Martin Iglesias Goyanes <martinigoyanes@hotmail.com>
Co-authored-by: martini <martin.iglesiasgoyanes@adyen.com>
This commit is contained in:
Nilabhra Roy Chowdhury 2024-05-14 10:06:02 +02:00 committed by GitHub
parent d348d2b28f
commit 3136f27f36
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 72 additions and 32 deletions

View File

@ -18,9 +18,10 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from typing import List, Optional, Tuple
import torch import torch
import torch.distributed import torch.distributed
from torch import nn from torch import nn
from transformers.activations import ACT2FN from transformers.activations import ACT2FN
from typing import Optional, List, Tuple from typing import Optional, List, Tuple

View File

@ -1,26 +1,21 @@
from typing import List, Optional, Tuple
import torch import torch
import torch.distributed import torch.distributed
from torch import nn from torch import nn
from transformers.modeling_utils import PreTrainedModel
from transformers.configuration_utils import PretrainedConfig from transformers.configuration_utils import PretrainedConfig
from typing import Optional, List, Tuple from transformers.modeling_utils import PreTrainedModel
from text_generation_server.utils import paged_attention, flash_attn
from text_generation_server.utils.flash_attn import attention
from text_generation_server.layers import ( from text_generation_server.layers import (
TensorParallelRowLinear, SpeculativeHead,
TensorParallelColumnLinear, TensorParallelColumnLinear,
TensorParallelEmbedding, TensorParallelEmbedding,
SpeculativeHead, TensorParallelRowLinear,
get_linear, get_linear,
) )
from text_generation_server.layers.layernorm import ( from text_generation_server.layers.layernorm import FastLayerNorm
FastLayerNorm, from text_generation_server.layers.rotary import PositionRotaryEmbedding
) from text_generation_server.utils import flash_attn, paged_attention
from text_generation_server.layers.rotary import (
PositionRotaryEmbedding,
)
def load_row(config, prefix: str, weights, bias: bool): def load_row(config, prefix: str, weights, bias: bool):
@ -52,6 +47,7 @@ class RWConfig(PretrainedConfig):
hidden_size=64, hidden_size=64,
num_hidden_layers=None, num_hidden_layers=None,
num_attention_heads=None, num_attention_heads=None,
num_ln_in_prallel_attention=None,
layer_norm_epsilon=1e-5, layer_norm_epsilon=1e-5,
initializer_range=0.02, initializer_range=0.02,
use_cache=True, use_cache=True,
@ -65,6 +61,7 @@ class RWConfig(PretrainedConfig):
new_decoder_architecture=None, new_decoder_architecture=None,
bias=False, bias=False,
parallel_attn=False, parallel_attn=False,
rope_theta=10_000.0,
**kwargs, **kwargs,
): ):
if alibi: if alibi:
@ -75,6 +72,7 @@ class RWConfig(PretrainedConfig):
self.model_type = model_type self.model_type = model_type
self.alibi = False self.alibi = False
self.rotary = True self.rotary = True
self.rope_theta = rope_theta
self.vocab_size = vocab_size self.vocab_size = vocab_size
# Backward compatibility with n_embed kwarg # Backward compatibility with n_embed kwarg
@ -91,6 +89,7 @@ class RWConfig(PretrainedConfig):
else kwargs.pop("n_head", 8) else kwargs.pop("n_head", 8)
) )
self.layer_norm_epsilon = layer_norm_epsilon self.layer_norm_epsilon = layer_norm_epsilon
self.num_ln_in_parallel_attention = num_ln_in_prallel_attention
self.initializer_range = initializer_range self.initializer_range = initializer_range
self.use_cache = use_cache self.use_cache = use_cache
self.hidden_dropout = hidden_dropout self.hidden_dropout = hidden_dropout
@ -132,9 +131,13 @@ class FlashRWAttention(torch.nn.Module):
self.num_heads_kv = config.n_head_kv self.num_heads_kv = config.n_head_kv
self.hidden_size = config.hidden_size self.hidden_size = config.hidden_size
self.head_size = self.hidden_size // self.num_heads self.head_size = self.hidden_size // self.num_heads
self.rope_theta = config.rope_theta
self.rotary_emb = PositionRotaryEmbedding.static( self.rotary_emb = PositionRotaryEmbedding.static(
config=config, dim=self.head_size, base=10000.0, device=weights.device config=config,
dim=self.head_size,
base=self.rope_theta,
device=weights.device,
) )
self.softmax_scale = self.head_size ** (-0.5) self.softmax_scale = self.head_size ** (-0.5)
@ -244,9 +247,13 @@ class FlashRWLargeAttention(torch.nn.Module):
self.hidden_size = hidden_size self.hidden_size = hidden_size
self.head_size = hidden_size // num_heads self.head_size = hidden_size // num_heads
self.num_groups = num_groups self.num_groups = num_groups
self.rope_theta = config.rope_theta
self.rotary_emb = PositionRotaryEmbedding.static( self.rotary_emb = PositionRotaryEmbedding.static(
config=config, dim=self.head_size, base=10000.0, device=weights.device config=config,
dim=self.head_size,
base=self.rope_theta,
device=weights.device,
) )
self.softmax_scale = self.head_size ** (-0.5) self.softmax_scale = self.head_size ** (-0.5)
@ -257,7 +264,7 @@ class FlashRWLargeAttention(torch.nn.Module):
if process_group.size() > self.num_groups: if process_group.size() > self.num_groups:
raise NotImplementedError( raise NotImplementedError(
f"Tensor Parallelism is not implemented for world_size > n groups" "Tensor Parallelism is not implemented for world_size > n groups"
) )
if self.num_groups % process_group.size() != 0: if self.num_groups % process_group.size() != 0:
raise NotImplementedError( raise NotImplementedError(
@ -459,6 +466,7 @@ class FlashRWLayer(nn.Module):
max_s, max_s,
) )
if self.post_attention_layernorm is not None:
hidden_states, residual = self.post_attention_layernorm( hidden_states, residual = self.post_attention_layernorm(
hidden_states, residual hidden_states, residual
) )
@ -468,10 +476,18 @@ class FlashRWLayer(nn.Module):
return mlp_output, residual return mlp_output, residual
class FlashRWLargeLayer(nn.Module): class FlashRWLayerNorm(nn.Module):
def __init__(self, layer_id, config, weights): def __init__(self, config, prefix, weights):
super().__init__() super().__init__()
prefix = f"transformer.h.{layer_id}" self.num_ln = config.num_ln_in_parallel_attn
if self.num_ln == 1:
self.input_ln = FastLayerNorm.load(
prefix=f"{prefix}.input_layernorm",
weights=weights,
eps=config.layer_norm_epsilon,
)
elif self.num_ln == 2:
self.ln_attn = FastLayerNorm.load( self.ln_attn = FastLayerNorm.load(
prefix=f"{prefix}.ln_attn", prefix=f"{prefix}.ln_attn",
weights=weights, weights=weights,
@ -482,6 +498,29 @@ class FlashRWLargeLayer(nn.Module):
weights=weights, weights=weights,
eps=config.layer_norm_epsilon, eps=config.layer_norm_epsilon,
) )
else:
raise ValueError("Number of layer norms can either be 1 or 2.")
def forward(
self,
hidden_states,
residual,
):
if self.num_ln == 1:
ln_hidden_states, residual = self.input_ln(hidden_states, residual)
return ln_hidden_states, ln_hidden_states, residual
elif self.num_ln == 2:
ln_attn, residual = self.ln_attn(hidden_states, residual)
ln_mlp, _ = self.ln_mlp(residual)
return ln_attn, ln_mlp, residual
class FlashRWLargeLayer(nn.Module):
def __init__(self, layer_id, config, weights):
super().__init__()
prefix = f"transformer.h.{layer_id}"
self.ln_layer = FlashRWLayerNorm(config, prefix, weights)
self.self_attention = FlashRWLargeAttention( self.self_attention = FlashRWLargeAttention(
config, config,
@ -507,8 +546,8 @@ class FlashRWLargeLayer(nn.Module):
input_lengths, input_lengths,
max_s, max_s,
): ):
ln_attn, residual = self.ln_attn(hidden_states, residual) # Layer norm.
ln_mlp, _ = self.ln_mlp(residual) ln_attn, ln_mlp, residual = self.ln_layer(hidden_states, residual)
# Self attention. # Self attention.
attn_output = self.self_attention( attn_output = self.self_attention(