mirror of
https://github.com/huggingface/text-generation-inference.git
synced 2025-06-19 15:52:08 +00:00
patch qkv_rot
This commit is contained in:
parent
cd5d0a96ba
commit
45eacb782d
794
Cargo.lock
generated
794
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -37,7 +37,7 @@ struct Args {
|
|||||||
max_waiting_tokens: usize,
|
max_waiting_tokens: usize,
|
||||||
#[clap(default_value = "3000", long, short, env)]
|
#[clap(default_value = "3000", long, short, env)]
|
||||||
port: u16,
|
port: u16,
|
||||||
#[clap(default_value = "/tmp/text-generation-0", long, env)]
|
#[clap(default_value = "/tmp/text-generation-server-0", long, env)]
|
||||||
master_shard_uds_path: String,
|
master_shard_uds_path: String,
|
||||||
#[clap(default_value = "bigscience/bloom", long, env)]
|
#[clap(default_value = "bigscience/bloom", long, env)]
|
||||||
tokenizer_name: String,
|
tokenizer_name: String,
|
||||||
|
@ -21,18 +21,21 @@
|
|||||||
import torch
|
import torch
|
||||||
import torch.distributed
|
import torch.distributed
|
||||||
|
|
||||||
from torch.nn import functional as F
|
|
||||||
|
|
||||||
from torch import nn
|
from torch import nn
|
||||||
from transformers.activations import ACT2FN
|
from transformers.activations import ACT2FN
|
||||||
|
|
||||||
|
from text_generation_server.models.custom_modeling.tensor_parallel import (
|
||||||
|
TensorParallelEmbedding,
|
||||||
|
TensorParallelRowLinear,
|
||||||
|
TensorParallelColumnLinear,
|
||||||
|
)
|
||||||
|
from text_generation_server.models.custom_modeling.linear import FastLinear
|
||||||
|
from text_generation_server.models.custom_modeling.rotary import PositionRotaryEmbedding
|
||||||
|
|
||||||
# Flash attention imports
|
# Flash attention imports
|
||||||
import rotary_emb
|
|
||||||
import flash_attn_cuda
|
import flash_attn_cuda
|
||||||
import dropout_layer_norm
|
import dropout_layer_norm
|
||||||
|
|
||||||
from flash_attn.layers.rotary import RotaryEmbedding
|
|
||||||
|
|
||||||
|
|
||||||
class LlamaRMSNorm(nn.Module):
|
class LlamaRMSNorm(nn.Module):
|
||||||
def __init__(self, hidden_size, eps=1e-6):
|
def __init__(self, hidden_size, eps=1e-6):
|
||||||
@ -84,182 +87,6 @@ class LlamaRMSNorm(nn.Module):
|
|||||||
return normed_hidden_states, res
|
return normed_hidden_states, res
|
||||||
|
|
||||||
|
|
||||||
class FastLinear(nn.Linear):
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
in_features: int,
|
|
||||||
out_features: int,
|
|
||||||
bias: bool = True,
|
|
||||||
device=None,
|
|
||||||
dtype=None,
|
|
||||||
) -> None:
|
|
||||||
super(FastLinear, self).__init__(in_features, out_features, bias, device, dtype)
|
|
||||||
|
|
||||||
def transpose_weight(self):
|
|
||||||
self.weight = nn.Parameter(self.weight.T)
|
|
||||||
|
|
||||||
def forward(self, input: torch.Tensor) -> torch.Tensor:
|
|
||||||
if self.bias is not None:
|
|
||||||
return torch.addmm(self.bias, input, self.weight)
|
|
||||||
return torch.matmul(input, self.weight)
|
|
||||||
|
|
||||||
|
|
||||||
class TensorParallelColumnLinear(FastLinear):
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
in_features,
|
|
||||||
out_features,
|
|
||||||
process_group: torch.distributed.ProcessGroup,
|
|
||||||
bias=True,
|
|
||||||
device=None,
|
|
||||||
dtype=None,
|
|
||||||
):
|
|
||||||
self.process_group = process_group
|
|
||||||
self.tp_world_size = process_group.size()
|
|
||||||
assert out_features % self.tp_world_size == 0
|
|
||||||
out_features = out_features // self.tp_world_size
|
|
||||||
|
|
||||||
super().__init__(
|
|
||||||
in_features=in_features,
|
|
||||||
out_features=out_features,
|
|
||||||
bias=bias,
|
|
||||||
device=device,
|
|
||||||
dtype=dtype,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class TensorParallelRowLinear(FastLinear):
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
in_features,
|
|
||||||
out_features,
|
|
||||||
process_group: torch.distributed.ProcessGroup,
|
|
||||||
reduce=True,
|
|
||||||
bias=True,
|
|
||||||
device=None,
|
|
||||||
dtype=None,
|
|
||||||
):
|
|
||||||
self.process_group = process_group
|
|
||||||
self.tp_world_size = process_group.size()
|
|
||||||
self.reduce = reduce
|
|
||||||
assert in_features % self.tp_world_size == 0
|
|
||||||
in_features = in_features // self.tp_world_size
|
|
||||||
|
|
||||||
super().__init__(
|
|
||||||
in_features=in_features,
|
|
||||||
out_features=out_features,
|
|
||||||
bias=bias,
|
|
||||||
device=device,
|
|
||||||
dtype=dtype,
|
|
||||||
)
|
|
||||||
|
|
||||||
def forward(self, input: torch.Tensor) -> torch.Tensor:
|
|
||||||
out = super(TensorParallelRowLinear, self).forward(input)
|
|
||||||
if self.reduce:
|
|
||||||
torch.distributed.all_reduce(out, group=self.process_group)
|
|
||||||
|
|
||||||
return out
|
|
||||||
|
|
||||||
|
|
||||||
class TensorParallelEmbedding(nn.Embedding):
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
num_embeddings,
|
|
||||||
embedding_dim,
|
|
||||||
process_group: torch.distributed.ProcessGroup,
|
|
||||||
padding_idx=None,
|
|
||||||
max_norm=None,
|
|
||||||
norm_type=2.0,
|
|
||||||
scale_grad_by_freq=False,
|
|
||||||
sparse=False,
|
|
||||||
_weight=None,
|
|
||||||
device=None,
|
|
||||||
dtype=None,
|
|
||||||
):
|
|
||||||
self.process_group = process_group
|
|
||||||
self.tp_rank = process_group.rank()
|
|
||||||
self.tp_world_size = process_group.size()
|
|
||||||
|
|
||||||
self.original_num_embeddings = num_embeddings
|
|
||||||
|
|
||||||
assert num_embeddings % self.tp_world_size == 0
|
|
||||||
block_size = num_embeddings // self.tp_world_size
|
|
||||||
# inputs in `[min_id, max_id[` are handled by `self` to get embeddings
|
|
||||||
self.min_id = self.tp_rank * block_size
|
|
||||||
self.max_id = (self.tp_rank + 1) * block_size
|
|
||||||
|
|
||||||
# Additional entry that will map to zero
|
|
||||||
# Used for masking
|
|
||||||
self.null_idx = block_size
|
|
||||||
|
|
||||||
super().__init__(
|
|
||||||
block_size,
|
|
||||||
embedding_dim,
|
|
||||||
padding_idx=padding_idx,
|
|
||||||
max_norm=max_norm,
|
|
||||||
norm_type=norm_type,
|
|
||||||
scale_grad_by_freq=scale_grad_by_freq,
|
|
||||||
sparse=sparse,
|
|
||||||
_weight=_weight,
|
|
||||||
device=device,
|
|
||||||
dtype=dtype,
|
|
||||||
)
|
|
||||||
|
|
||||||
def add_null_idx(self):
|
|
||||||
"""Additional 0 entry used for masking"""
|
|
||||||
self.weight = nn.Parameter(F.pad(self.weight, (0, 0, 0, 1)))
|
|
||||||
|
|
||||||
def forward(self, input: torch.Tensor) -> torch.Tensor:
|
|
||||||
# default all out of bounds values to `self.null_idx` that will then be mapped to 0
|
|
||||||
# translate for [0, self.max_id - self.min_id[
|
|
||||||
input = torch.where(
|
|
||||||
(self.min_id > input) | (input >= self.max_id),
|
|
||||||
self.null_idx,
|
|
||||||
input - self.min_id,
|
|
||||||
)
|
|
||||||
out = super().forward(input)
|
|
||||||
torch.distributed.all_reduce(out, group=self.process_group)
|
|
||||||
return out
|
|
||||||
|
|
||||||
|
|
||||||
class PositionRotaryEmbedding(RotaryEmbedding):
|
|
||||||
def _update_cos_sin_cache(self, dtype, device, seqlen):
|
|
||||||
# Reset the tables if the sequence length has changed,
|
|
||||||
# or if we're on a new device (possibly due to tracing for instance)
|
|
||||||
if (
|
|
||||||
seqlen > self._seq_len_cached
|
|
||||||
or self._cos_cached.device != device
|
|
||||||
or self._cos_cached.dtype != dtype
|
|
||||||
):
|
|
||||||
self._seq_len_cached = seqlen
|
|
||||||
t = torch.arange(seqlen, device=device, dtype=self.inv_freq.dtype)
|
|
||||||
freqs = torch.outer(t, self.inv_freq.to(device=t.device))
|
|
||||||
self._cos_cached = torch.cos(freqs).to(dtype)
|
|
||||||
self._sin_cached = torch.sin(freqs).to(dtype)
|
|
||||||
|
|
||||||
def get_cos_sin(self, position_ids: torch.Tensor, max_s: int, dtype: torch.dtype):
|
|
||||||
"""
|
|
||||||
Return cos and sin for the asked position ids
|
|
||||||
"""
|
|
||||||
|
|
||||||
self._update_cos_sin_cache(dtype, position_ids.device, max_s)
|
|
||||||
|
|
||||||
cos = torch.index_select(self._cos_cached, 0, position_ids)
|
|
||||||
sin = torch.index_select(self._sin_cached, 0, position_ids)
|
|
||||||
return cos.unsqueeze(1), sin.unsqueeze(1)
|
|
||||||
|
|
||||||
def forward(self, qkv: torch.Tensor, cos: torch.Tensor, sin: torch.Tensor):
|
|
||||||
rotary_dim = cos.shape[-1]
|
|
||||||
q1 = qkv[:, 0, :, :rotary_dim]
|
|
||||||
q2 = qkv[:, 0, :, rotary_dim : 2 * rotary_dim]
|
|
||||||
k1 = qkv[:, 1, :, :rotary_dim]
|
|
||||||
k2 = qkv[:, 1, :, rotary_dim : 2 * rotary_dim]
|
|
||||||
|
|
||||||
rotary_emb.apply_rotary(q1, q2, cos, sin, q1, q2, False)
|
|
||||||
rotary_emb.apply_rotary(k1, k2, cos, sin, k1, k2, False)
|
|
||||||
return qkv
|
|
||||||
|
|
||||||
|
|
||||||
class FlashLlamaAttention(torch.nn.Module):
|
class FlashLlamaAttention(torch.nn.Module):
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
@ -314,12 +141,12 @@ class FlashLlamaAttention(torch.nn.Module):
|
|||||||
layer_past[...] = qkv_rot[:, 1:]
|
layer_past[...] = qkv_rot[:, 1:]
|
||||||
|
|
||||||
# output
|
# output
|
||||||
attn_output = torch.empty_like(qkv[:, 0])
|
attn_output = torch.empty_like(qkv_rot[:, 0])
|
||||||
# flash attention
|
# flash attention
|
||||||
flash_attn_cuda.fwd(
|
flash_attn_cuda.fwd(
|
||||||
qkv[:, 0],
|
qkv_rot[:, 0],
|
||||||
qkv[:, 1],
|
qkv_rot[:, 1],
|
||||||
qkv[:, 2],
|
qkv_rot[:, 2],
|
||||||
attn_output,
|
attn_output,
|
||||||
cu_seqlens,
|
cu_seqlens,
|
||||||
cu_seqlens,
|
cu_seqlens,
|
||||||
@ -369,7 +196,12 @@ class LlamaMLP(nn.Module):
|
|||||||
self.act = (
|
self.act = (
|
||||||
ACT2FN[act]
|
ACT2FN[act]
|
||||||
if "gelu" not in act
|
if "gelu" not in act
|
||||||
else lambda x: torch.nn.functional.gelu(x, approximate="tanh")
|
else lambda x: torch.nn.functional.gelu(
|
||||||
|
x,
|
||||||
|
approximate="tanh"
|
||||||
|
if act in ["gelu_fast", "gelu_pytorch_tanh"]
|
||||||
|
else None,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
if process_group is None:
|
if process_group is None:
|
||||||
|
@ -21,20 +21,23 @@
|
|||||||
import torch
|
import torch
|
||||||
import torch.distributed
|
import torch.distributed
|
||||||
|
|
||||||
from torch.nn import functional as F
|
|
||||||
|
|
||||||
from torch import nn
|
from torch import nn
|
||||||
from transformers.activations import ACT2FN
|
from transformers.activations import ACT2FN
|
||||||
from transformers.modeling_utils import PreTrainedModel
|
from transformers.modeling_utils import PreTrainedModel
|
||||||
from transformers.models.gpt_neox import GPTNeoXConfig
|
from transformers.models.gpt_neox import GPTNeoXConfig
|
||||||
|
|
||||||
|
from text_generation_server.models.custom_modeling.tensor_parallel import (
|
||||||
|
TensorParallelEmbedding,
|
||||||
|
TensorParallelRowLinear,
|
||||||
|
TensorParallelColumnLinear,
|
||||||
|
)
|
||||||
|
from text_generation_server.models.custom_modeling.linear import FastLinear
|
||||||
|
from text_generation_server.models.custom_modeling.rotary import PositionRotaryEmbedding
|
||||||
|
|
||||||
# Flash attention imports
|
# Flash attention imports
|
||||||
import rotary_emb
|
|
||||||
import flash_attn_cuda
|
import flash_attn_cuda
|
||||||
import dropout_layer_norm
|
import dropout_layer_norm
|
||||||
|
|
||||||
from flash_attn.layers.rotary import RotaryEmbedding
|
|
||||||
|
|
||||||
|
|
||||||
class FastLayerNorm(nn.LayerNorm):
|
class FastLayerNorm(nn.LayerNorm):
|
||||||
def forward(self, hidden_states, residual=None):
|
def forward(self, hidden_states, residual=None):
|
||||||
@ -72,184 +75,6 @@ class FastLayerNorm(nn.LayerNorm):
|
|||||||
return normed_hidden_states, residual
|
return normed_hidden_states, residual
|
||||||
|
|
||||||
|
|
||||||
class FastLinear(nn.Linear):
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
in_features: int,
|
|
||||||
out_features: int,
|
|
||||||
bias: bool = True,
|
|
||||||
device=None,
|
|
||||||
dtype=None,
|
|
||||||
) -> None:
|
|
||||||
super(FastLinear, self).__init__(in_features, out_features, bias, device, dtype)
|
|
||||||
|
|
||||||
def transpose_weight(self):
|
|
||||||
self.weight = nn.Parameter(self.weight.T)
|
|
||||||
|
|
||||||
def forward(self, input: torch.Tensor) -> torch.Tensor:
|
|
||||||
if self.bias is not None:
|
|
||||||
return torch.addmm(self.bias, input, self.weight)
|
|
||||||
return torch.matmul(input, self.weight)
|
|
||||||
|
|
||||||
|
|
||||||
class TensorParallelColumnLinear(FastLinear):
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
in_features,
|
|
||||||
out_features,
|
|
||||||
process_group: torch.distributed.ProcessGroup,
|
|
||||||
bias=True,
|
|
||||||
device=None,
|
|
||||||
dtype=None,
|
|
||||||
):
|
|
||||||
self.process_group = process_group
|
|
||||||
self.tp_world_size = process_group.size()
|
|
||||||
assert out_features % self.tp_world_size == 0
|
|
||||||
out_features = out_features // self.tp_world_size
|
|
||||||
|
|
||||||
super().__init__(
|
|
||||||
in_features=in_features,
|
|
||||||
out_features=out_features,
|
|
||||||
bias=bias,
|
|
||||||
device=device,
|
|
||||||
dtype=dtype,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class TensorParallelRowLinear(FastLinear):
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
in_features,
|
|
||||||
out_features,
|
|
||||||
process_group: torch.distributed.ProcessGroup,
|
|
||||||
reduce=True,
|
|
||||||
bias=True,
|
|
||||||
device=None,
|
|
||||||
dtype=None,
|
|
||||||
):
|
|
||||||
self.process_group = process_group
|
|
||||||
self.tp_world_size = process_group.size()
|
|
||||||
self.reduce = reduce
|
|
||||||
assert in_features % self.tp_world_size == 0
|
|
||||||
in_features = in_features // self.tp_world_size
|
|
||||||
|
|
||||||
super().__init__(
|
|
||||||
in_features=in_features,
|
|
||||||
out_features=out_features,
|
|
||||||
bias=bias,
|
|
||||||
device=device,
|
|
||||||
dtype=dtype,
|
|
||||||
)
|
|
||||||
|
|
||||||
def forward(self, input: torch.Tensor) -> torch.Tensor:
|
|
||||||
out = super(TensorParallelRowLinear, self).forward(input)
|
|
||||||
if self.reduce:
|
|
||||||
torch.distributed.all_reduce(out, group=self.process_group)
|
|
||||||
|
|
||||||
return out
|
|
||||||
|
|
||||||
|
|
||||||
class TensorParallelEmbedding(nn.Embedding):
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
num_embeddings,
|
|
||||||
embedding_dim,
|
|
||||||
process_group: torch.distributed.ProcessGroup,
|
|
||||||
padding_idx=None,
|
|
||||||
max_norm=None,
|
|
||||||
norm_type=2.0,
|
|
||||||
scale_grad_by_freq=False,
|
|
||||||
sparse=False,
|
|
||||||
_weight=None,
|
|
||||||
device=None,
|
|
||||||
dtype=None,
|
|
||||||
):
|
|
||||||
self.process_group = process_group
|
|
||||||
self.tp_rank = process_group.rank()
|
|
||||||
self.tp_world_size = process_group.size()
|
|
||||||
|
|
||||||
self.original_num_embeddings = num_embeddings
|
|
||||||
|
|
||||||
assert num_embeddings % self.tp_world_size == 0
|
|
||||||
block_size = num_embeddings // self.tp_world_size
|
|
||||||
# inputs in `[min_id, max_id[` are handled by `self` to get embeddings
|
|
||||||
self.min_id = self.tp_rank * block_size
|
|
||||||
self.max_id = (self.tp_rank + 1) * block_size
|
|
||||||
|
|
||||||
# Additional entry that will map to zero
|
|
||||||
# Used for masking
|
|
||||||
self.null_idx = block_size
|
|
||||||
|
|
||||||
super().__init__(
|
|
||||||
block_size,
|
|
||||||
embedding_dim,
|
|
||||||
padding_idx=padding_idx,
|
|
||||||
max_norm=max_norm,
|
|
||||||
norm_type=norm_type,
|
|
||||||
scale_grad_by_freq=scale_grad_by_freq,
|
|
||||||
sparse=sparse,
|
|
||||||
_weight=_weight,
|
|
||||||
device=device,
|
|
||||||
dtype=dtype,
|
|
||||||
)
|
|
||||||
|
|
||||||
def add_null_idx(self):
|
|
||||||
"""Additional 0 entry used for masking"""
|
|
||||||
self.weight = nn.Parameter(F.pad(self.weight, (0, 0, 0, 1)))
|
|
||||||
|
|
||||||
def forward(self, input: torch.Tensor) -> torch.Tensor:
|
|
||||||
# default all out of bounds values to `self.null_idx` that will then be mapped to 0
|
|
||||||
# translate for [0, self.max_id - self.min_id[
|
|
||||||
input = torch.where(
|
|
||||||
(self.min_id > input) | (input >= self.max_id),
|
|
||||||
self.null_idx,
|
|
||||||
input - self.min_id,
|
|
||||||
)
|
|
||||||
out = super().forward(input)
|
|
||||||
torch.distributed.all_reduce(out, group=self.process_group)
|
|
||||||
return out
|
|
||||||
|
|
||||||
|
|
||||||
class PositionRotaryEmbedding(RotaryEmbedding):
|
|
||||||
def _update_cos_sin_cache(self, dtype, device, seqlen):
|
|
||||||
# Reset the tables if the sequence length has changed,
|
|
||||||
# or if we're on a new device (possibly due to tracing for instance)
|
|
||||||
if (
|
|
||||||
seqlen > self._seq_len_cached
|
|
||||||
or self._cos_cached.device != device
|
|
||||||
or self._cos_cached.dtype != dtype
|
|
||||||
):
|
|
||||||
self._seq_len_cached = seqlen
|
|
||||||
t = torch.arange(seqlen, device=device, dtype=self.inv_freq.dtype)
|
|
||||||
# Don't do einsum, it converts fp32 to fp16
|
|
||||||
# freqs = torch.einsum("i,j->ij", t, self.inv_freq)
|
|
||||||
freqs = torch.outer(t, self.inv_freq.to(device=t.device))
|
|
||||||
self._cos_cached = torch.cos(freqs).to(dtype)
|
|
||||||
self._sin_cached = torch.sin(freqs).to(dtype)
|
|
||||||
|
|
||||||
def get_cos_sin(self, position_ids: torch.Tensor, max_s: int, dtype: torch.dtype):
|
|
||||||
"""
|
|
||||||
Return cos and sin for the asked position ids
|
|
||||||
"""
|
|
||||||
|
|
||||||
self._update_cos_sin_cache(dtype, position_ids.device, max_s)
|
|
||||||
|
|
||||||
cos = torch.index_select(self._cos_cached, 0, position_ids)
|
|
||||||
sin = torch.index_select(self._sin_cached, 0, position_ids)
|
|
||||||
return cos.unsqueeze(1), sin.unsqueeze(1)
|
|
||||||
|
|
||||||
def forward(self, qkv: torch.Tensor, cos: torch.Tensor, sin: torch.Tensor):
|
|
||||||
rotary_dim = cos.shape[-1]
|
|
||||||
q1 = qkv[:, 0, :, :rotary_dim]
|
|
||||||
q2 = qkv[:, 0, :, rotary_dim : 2 * rotary_dim]
|
|
||||||
k1 = qkv[:, 1, :, :rotary_dim]
|
|
||||||
k2 = qkv[:, 1, :, rotary_dim : 2 * rotary_dim]
|
|
||||||
|
|
||||||
rotary_emb.apply_rotary(q1, q2, cos, sin, q1, q2, False)
|
|
||||||
rotary_emb.apply_rotary(k1, k2, cos, sin, k1, k2, False)
|
|
||||||
return qkv
|
|
||||||
|
|
||||||
|
|
||||||
class FlashNeoxAttention(torch.nn.Module):
|
class FlashNeoxAttention(torch.nn.Module):
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
@ -376,7 +201,12 @@ class FlashMLP(nn.Module):
|
|||||||
self.act = (
|
self.act = (
|
||||||
ACT2FN[act]
|
ACT2FN[act]
|
||||||
if "gelu" not in act
|
if "gelu" not in act
|
||||||
else lambda x: torch.nn.functional.gelu(x, approximate="tanh")
|
else lambda x: torch.nn.functional.gelu(
|
||||||
|
x,
|
||||||
|
approximate="tanh"
|
||||||
|
if act in ["gelu_fast", "gelu_pytorch_tanh"]
|
||||||
|
else None,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
if process_group is None:
|
if process_group is None:
|
||||||
|
@ -0,0 +1,22 @@
|
|||||||
|
import torch
|
||||||
|
from torch import nn
|
||||||
|
|
||||||
|
|
||||||
|
class FastLinear(nn.Linear):
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
in_features: int,
|
||||||
|
out_features: int,
|
||||||
|
bias: bool = True,
|
||||||
|
device=None,
|
||||||
|
dtype=None,
|
||||||
|
) -> None:
|
||||||
|
super(FastLinear, self).__init__(in_features, out_features, bias, device, dtype)
|
||||||
|
|
||||||
|
def transpose_weight(self):
|
||||||
|
self.weight = nn.Parameter(self.weight.T)
|
||||||
|
|
||||||
|
def forward(self, input: torch.Tensor) -> torch.Tensor:
|
||||||
|
if self.bias is not None:
|
||||||
|
return torch.addmm(self.bias, input, self.weight)
|
||||||
|
return torch.matmul(input, self.weight)
|
@ -0,0 +1,42 @@
|
|||||||
|
import torch
|
||||||
|
import rotary_emb
|
||||||
|
|
||||||
|
from flash_attn.layers.rotary import RotaryEmbedding
|
||||||
|
|
||||||
|
|
||||||
|
class PositionRotaryEmbedding(RotaryEmbedding):
|
||||||
|
def _update_cos_sin_cache(self, dtype, device, seqlen):
|
||||||
|
# Reset the tables if the sequence length has changed,
|
||||||
|
# or if we're on a new device (possibly due to tracing for instance)
|
||||||
|
if (
|
||||||
|
seqlen > self._seq_len_cached
|
||||||
|
or self._cos_cached.device != device
|
||||||
|
or self._cos_cached.dtype != dtype
|
||||||
|
):
|
||||||
|
self._seq_len_cached = seqlen
|
||||||
|
t = torch.arange(seqlen, device=device, dtype=self.inv_freq.dtype)
|
||||||
|
freqs = torch.outer(t, self.inv_freq.to(device=t.device))
|
||||||
|
self._cos_cached = torch.cos(freqs).to(dtype)
|
||||||
|
self._sin_cached = torch.sin(freqs).to(dtype)
|
||||||
|
|
||||||
|
def get_cos_sin(self, position_ids: torch.Tensor, max_s: int, dtype: torch.dtype):
|
||||||
|
"""
|
||||||
|
Return cos and sin for the asked position ids
|
||||||
|
"""
|
||||||
|
|
||||||
|
self._update_cos_sin_cache(dtype, position_ids.device, max_s)
|
||||||
|
|
||||||
|
cos = torch.index_select(self._cos_cached, 0, position_ids)
|
||||||
|
sin = torch.index_select(self._sin_cached, 0, position_ids)
|
||||||
|
return cos.unsqueeze(1), sin.unsqueeze(1)
|
||||||
|
|
||||||
|
def forward(self, qkv: torch.Tensor, cos: torch.Tensor, sin: torch.Tensor):
|
||||||
|
rotary_dim = cos.shape[-1]
|
||||||
|
q1 = qkv[:, 0, :, :rotary_dim]
|
||||||
|
q2 = qkv[:, 0, :, rotary_dim : 2 * rotary_dim]
|
||||||
|
k1 = qkv[:, 1, :, :rotary_dim]
|
||||||
|
k2 = qkv[:, 1, :, rotary_dim : 2 * rotary_dim]
|
||||||
|
|
||||||
|
rotary_emb.apply_rotary(q1, q2, cos, sin, q1, q2, False)
|
||||||
|
rotary_emb.apply_rotary(k1, k2, cos, sin, k1, k2, False)
|
||||||
|
return qkv
|
@ -0,0 +1,124 @@
|
|||||||
|
import torch
|
||||||
|
import torch.distributed
|
||||||
|
from torch import nn
|
||||||
|
from torch.nn import functional as F
|
||||||
|
|
||||||
|
from text_generation_server.models.custom_modeling.linear import FastLinear
|
||||||
|
|
||||||
|
|
||||||
|
class TensorParallelColumnLinear(FastLinear):
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
in_features,
|
||||||
|
out_features,
|
||||||
|
process_group: torch.distributed.ProcessGroup,
|
||||||
|
bias=True,
|
||||||
|
device=None,
|
||||||
|
dtype=None,
|
||||||
|
):
|
||||||
|
self.process_group = process_group
|
||||||
|
self.tp_world_size = process_group.size()
|
||||||
|
assert out_features % self.tp_world_size == 0
|
||||||
|
out_features = out_features // self.tp_world_size
|
||||||
|
|
||||||
|
super().__init__(
|
||||||
|
in_features=in_features,
|
||||||
|
out_features=out_features,
|
||||||
|
bias=bias,
|
||||||
|
device=device,
|
||||||
|
dtype=dtype,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TensorParallelRowLinear(FastLinear):
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
in_features,
|
||||||
|
out_features,
|
||||||
|
process_group: torch.distributed.ProcessGroup,
|
||||||
|
reduce=True,
|
||||||
|
bias=True,
|
||||||
|
device=None,
|
||||||
|
dtype=None,
|
||||||
|
):
|
||||||
|
self.process_group = process_group
|
||||||
|
self.tp_world_size = process_group.size()
|
||||||
|
self.reduce = reduce
|
||||||
|
assert in_features % self.tp_world_size == 0
|
||||||
|
in_features = in_features // self.tp_world_size
|
||||||
|
|
||||||
|
super().__init__(
|
||||||
|
in_features=in_features,
|
||||||
|
out_features=out_features,
|
||||||
|
bias=bias,
|
||||||
|
device=device,
|
||||||
|
dtype=dtype,
|
||||||
|
)
|
||||||
|
|
||||||
|
def forward(self, input: torch.Tensor) -> torch.Tensor:
|
||||||
|
out = super(TensorParallelRowLinear, self).forward(input)
|
||||||
|
if self.reduce:
|
||||||
|
torch.distributed.all_reduce(out, group=self.process_group)
|
||||||
|
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
class TensorParallelEmbedding(nn.Embedding):
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
num_embeddings,
|
||||||
|
embedding_dim,
|
||||||
|
process_group: torch.distributed.ProcessGroup,
|
||||||
|
padding_idx=None,
|
||||||
|
max_norm=None,
|
||||||
|
norm_type=2.0,
|
||||||
|
scale_grad_by_freq=False,
|
||||||
|
sparse=False,
|
||||||
|
_weight=None,
|
||||||
|
device=None,
|
||||||
|
dtype=None,
|
||||||
|
):
|
||||||
|
self.process_group = process_group
|
||||||
|
self.tp_rank = process_group.rank()
|
||||||
|
self.tp_world_size = process_group.size()
|
||||||
|
|
||||||
|
self.original_num_embeddings = num_embeddings
|
||||||
|
|
||||||
|
assert num_embeddings % self.tp_world_size == 0
|
||||||
|
block_size = num_embeddings // self.tp_world_size
|
||||||
|
# inputs in `[min_id, max_id[` are handled by `self` to get embeddings
|
||||||
|
self.min_id = self.tp_rank * block_size
|
||||||
|
self.max_id = (self.tp_rank + 1) * block_size
|
||||||
|
|
||||||
|
# Additional entry that will map to zero
|
||||||
|
# Used for masking
|
||||||
|
self.null_idx = block_size
|
||||||
|
|
||||||
|
super().__init__(
|
||||||
|
block_size,
|
||||||
|
embedding_dim,
|
||||||
|
padding_idx=padding_idx,
|
||||||
|
max_norm=max_norm,
|
||||||
|
norm_type=norm_type,
|
||||||
|
scale_grad_by_freq=scale_grad_by_freq,
|
||||||
|
sparse=sparse,
|
||||||
|
_weight=_weight,
|
||||||
|
device=device,
|
||||||
|
dtype=dtype,
|
||||||
|
)
|
||||||
|
|
||||||
|
def add_null_idx(self):
|
||||||
|
"""Additional 0 entry used for masking"""
|
||||||
|
self.weight = nn.Parameter(F.pad(self.weight, (0, 0, 0, 1)))
|
||||||
|
|
||||||
|
def forward(self, input: torch.Tensor) -> torch.Tensor:
|
||||||
|
# default all out of bounds values to `self.null_idx` that will then be mapped to 0
|
||||||
|
# translate for [0, self.max_id - self.min_id[
|
||||||
|
input = torch.where(
|
||||||
|
(self.min_id > input) | (input >= self.max_id),
|
||||||
|
self.null_idx,
|
||||||
|
input - self.min_id,
|
||||||
|
)
|
||||||
|
out = super().forward(input)
|
||||||
|
torch.distributed.all_reduce(out, group=self.process_group)
|
||||||
|
return out
|
@ -11,6 +11,8 @@ from typing import Optional, Tuple, List
|
|||||||
from text_generation_server.models import FlashCausalLM
|
from text_generation_server.models import FlashCausalLM
|
||||||
from text_generation_server.models.custom_modeling.flash_llama_modeling import (
|
from text_generation_server.models.custom_modeling.flash_llama_modeling import (
|
||||||
FlashLlamaForCausalLM,
|
FlashLlamaForCausalLM,
|
||||||
|
)
|
||||||
|
from text_generation_server.models.custom_modeling.tensor_parallel import (
|
||||||
TensorParallelEmbedding,
|
TensorParallelEmbedding,
|
||||||
TensorParallelRowLinear,
|
TensorParallelRowLinear,
|
||||||
TensorParallelColumnLinear,
|
TensorParallelColumnLinear,
|
||||||
|
@ -8,12 +8,14 @@ from transformers import AutoTokenizer, AutoConfig
|
|||||||
from typing import Optional, Tuple, List
|
from typing import Optional, Tuple, List
|
||||||
|
|
||||||
from text_generation_server.models import FlashCausalLM
|
from text_generation_server.models import FlashCausalLM
|
||||||
from text_generation_server.models.custom_modeling.flash_neox_modeling import (
|
from text_generation_server.models.custom_modeling.tensor_parallel import (
|
||||||
FlashGPTNeoXForCausalLM,
|
|
||||||
TensorParallelEmbedding,
|
TensorParallelEmbedding,
|
||||||
TensorParallelRowLinear,
|
TensorParallelRowLinear,
|
||||||
TensorParallelColumnLinear,
|
TensorParallelColumnLinear,
|
||||||
)
|
)
|
||||||
|
from text_generation_server.models.custom_modeling.flash_neox_modeling import (
|
||||||
|
FlashGPTNeoXForCausalLM,
|
||||||
|
)
|
||||||
from text_generation_server.utils import (
|
from text_generation_server.utils import (
|
||||||
initialize_torch_distributed,
|
initialize_torch_distributed,
|
||||||
weight_files,
|
weight_files,
|
||||||
|
Loading…
Reference in New Issue
Block a user