Gemma first implementation

This commit is contained in:
City
2024-12-13 01:04:31 +01:00
parent c52d82758a
commit 0f567fc4a7
9 changed files with 2025 additions and 2 deletions
+2
View File
@@ -301,6 +301,8 @@ class SanaMS(Sana):
"""
bs = x.shape[0]
y = context
if len(y.shape) == 3:
y = y.unsqueeze(1)
self.h, self.w = x.shape[-2] // self.patch_size, x.shape[-1] // self.patch_size
if self.use_pe:
x = self.x_embedder(x)
+4 -2
View File
@@ -1,10 +1,12 @@
import os
import torch
from comfy import sd1_clip
import comfy.text_encoders.t5
import comfy.text_encoders.sd3_clip
import comfy.model_management
from transformers import T5TokenizerFast
import torch
import os
class T5XXLModel(comfy.text_encoders.sd3_clip.T5XXLModel):
def __init__(self, **kwargs):
+35
View File
@@ -0,0 +1,35 @@
{
"architectures": [
"Gemma2ForCausalLM"
],
"attention_bias": false,
"attention_dropout": 0.0,
"attn_logit_softcapping": 50.0,
"bos_token_id": 2,
"cache_implementation": "hybrid",
"eos_token_id": [
1,
107
],
"final_logit_softcapping": 30.0,
"head_dim": 256,
"hidden_act": "gelu_pytorch_tanh",
"hidden_activation": "gelu_pytorch_tanh",
"hidden_size": 2304,
"initializer_range": 0.02,
"intermediate_size": 9216,
"max_position_embeddings": 8192,
"model_type": "gemma2",
"num_attention_heads": 8,
"num_hidden_layers": 26,
"num_key_value_heads": 4,
"pad_token_id": 0,
"query_pre_attn_scalar": 256,
"rms_norm_eps": 1e-06,
"rope_theta": 10000.0,
"sliding_window": 4096,
"torch_dtype": "bfloat16",
"transformers_version": "4.42.4",
"use_cache": true,
"vocab_size": 256000
}
+379
View File
@@ -0,0 +1,379 @@
# Copyright 2024 Google Inc. HuggingFace Inc. team. All rights reserved.
#
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import torch
import torch.nn as nn
import importlib
def rotate_half(x):
x1 = x[..., : x.shape[-1] // 2]
x2 = x[..., x.shape[-1] // 2 :]
return torch.cat((-x2, x1), dim=-1)
def apply_rotary_pos_emb(q, k, cos, sin, position_ids=None, unsqueeze_dim=1):
cos = cos.unsqueeze(unsqueeze_dim)
sin = sin.unsqueeze(unsqueeze_dim)
q_embed = (q * cos) + (rotate_half(q) * sin)
k_embed = (k * cos) + (rotate_half(k) * sin)
return q_embed, k_embed
def repeat_kv(hidden_states, n_rep):
batch, num_key_value_heads, slen, head_dim = hidden_states.shape
if n_rep == 1:
return hidden_states
hidden_states = hidden_states[:, :, None, :, :].expand(batch, num_key_value_heads, n_rep, slen, head_dim)
return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim)
def sdpa_attention_forward(config, query, key, value, mask=None, **kwargs):
key = repeat_kv(key, config["num_key_value_groups"])
value = repeat_kv(value, config["num_key_value_groups"])
causal_mask = mask
if mask is not None:
causal_mask = causal_mask[:, :, :, : key.shape[-2]]
# SDPA with memory-efficient backend is currently (torch==2.1.2) bugged with non-contiguous inputs with custom attn_mask,
# Reference: https://github.com/pytorch/pytorch/issues/112577.
if query.device.type == "cuda" and causal_mask is not None:
query = query.contiguous()
key = key.contiguous()
value = value.contiguous()
# We dispatch to SDPA's Flash Attention or Efficient kernels via this `is_causal` if statement instead of an inline conditional assignment
# in SDPA to support both torch.compile's dynamic shapes and full graph options. An inline conditional prevents dynamic shapes from compiling.
is_causal = True if causal_mask is None and query.shape[1] > 1 else False
attn_output = torch.nn.functional.scaled_dot_product_attention(
query,
key,
value,
attn_mask=causal_mask,
dropout_p=0.0,
is_causal=is_causal,
scale=config["scaling"],
)
attn_output = attn_output.transpose(1, 2).contiguous()
return attn_output, None
def eager_attention_forward(config, query, key, value, mask, **kwargs):
key_states = repeat_kv(key, config["num_key_value_groups"])
value_states = repeat_kv(value, config["num_key_value_groups"])
attn_weights = torch.matmul(query, key_states.transpose(2, 3)) * config["scaling"]
if config["attn_logit_softcapping"] is not None:
attn_weights = attn_weights / config["attn_logit_softcapping"]
attn_weights = torch.tanh(attn_weights)
attn_weights = attn_weights * config["attn_logit_softcapping"]
if mask is not None: # no matter the length, we just slice it
causal_mask = mask[:, :, :, : key_states.shape[-2]]
attn_weights = attn_weights + causal_mask
# upcast attention to fp32
attn_weights = nn.functional.softmax(attn_weights, dim=-1, dtype=torch.float32).to(query.dtype)
#attn_weights = nn.functional.dropout(attn_weights, p=0, training=config.training)
attn_output = torch.matmul(attn_weights, value_states)
attn_output = attn_output.transpose(1, 2).contiguous()
return attn_output, attn_weights
# torch 2.0 can't pass scale arg to sdpa
if int((torch.__version__).split(".")[1]) >= 1:
attention_forward = sdpa_attention_forward
else:
attention_forward = eager_attention_forward
class Gemma2RMSNorm(nn.Module):
def __init__(self, dim, eps=1e-6):
super().__init__()
self.eps = eps
self.weight = nn.Parameter(torch.zeros(dim))
def _norm(self, x):
return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps)
def forward(self, x):
output = self._norm(x.float())
# Llama does x.to(float16) * w whilst Gemma2 is (x * w).to(float16)
# See https://github.com/huggingface/transformers/pull/29402
output = output * (1.0 + self.weight.float())
return output.type_as(x)
def extra_repr(self):
return f"{tuple(self.weight.shape)}, eps={self.eps}"
class Gemma2MLP(nn.Module):
def __init__(self, config, dtype=None, device=None, operations=None):
super().__init__()
self.config = config
self.hidden_size = config["hidden_size"]
self.intermediate_size = config["intermediate_size"]
self.gate_proj = operations.Linear(self.hidden_size, self.intermediate_size, bias=False, dtype=dtype, device=device)
self.up_proj = operations.Linear(self.hidden_size, self.intermediate_size, bias=False, dtype=dtype, device=device)
self.down_proj = operations.Linear(self.intermediate_size, self.hidden_size, bias=False, dtype=dtype, device=device)
if config["hidden_activation"] != "gelu_pytorch_tanh":
raise NotImplementedError("Unknown act mode")
self.act_fn = torch.nn.GELU()
def forward(self, x):
return self.down_proj(self.act_fn(self.gate_proj(x)) * self.up_proj(x))
class Gemma2RotaryEmbedding(nn.Module):
def __init__(self, dim, max_position_embeddings=2048, base=10000):
super().__init__()
self.dim = dim
self.max_position_embeddings = max_position_embeddings
self.base = base
inv_freq = 1.0 / (self.base ** (torch.arange(0, self.dim, 2, dtype=torch.int64).float() / self.dim))
self.register_buffer("inv_freq", tensor=inv_freq, persistent=False)
@torch.no_grad()
def forward(self, x, position_ids, seq_len=None):
# x: [bs, num_attention_heads, seq_len, head_size]
self.inv_freq.to(x.device)
inv_freq_expanded = self.inv_freq[None, :, None].float().expand(position_ids.shape[0], -1, 1)
position_ids_expanded = position_ids[:, None, :].float()
# Force float32 since bfloat16 loses precision on long contexts
# See https://github.com/huggingface/transformers/pull/29285
device_type = x.device.type
device_type = device_type if isinstance(device_type, str) and device_type != "mps" else "cpu"
with torch.autocast(device_type=device_type, enabled=False):
freqs = (inv_freq_expanded.float() @ position_ids_expanded.float()).transpose(1, 2)
emb = torch.cat((freqs, freqs), dim=-1)
cos = emb.cos()
sin = emb.sin()
return cos.to(dtype=x.dtype), sin.to(dtype=x.dtype)
class Gemma2Attention(nn.Module):
"""Multi-headed attention from 'Attention Is All You Need' paper"""
def __init__(self, config, layer_idx=None, dtype=None, device=None, operations=None):
super().__init__()
self.config = config
self.layer_idx = layer_idx
self.attention_dropout = config["attention_dropout"]
self.hidden_size = config["hidden_size"]
self.num_heads = config["num_attention_heads"]
self.head_dim = config["head_dim"]
self.num_key_value_heads = config["num_key_value_heads"]
self.num_key_value_groups = self.num_heads // self.num_key_value_heads
self.max_position_embeddings = config["max_position_embeddings"]
self.rope_theta = config["rope_theta"]
self.is_causal = True
self.scaling = config["query_pre_attn_scalar"]**-0.5
self.sliding_window = config["sliding_window"] if not bool(layer_idx % 2) else None
self.attn_logit_softcapping = config["attn_logit_softcapping"]
if self.hidden_size % self.num_heads != 0:
raise ValueError(
f"hidden_size must be divisible by num_heads (got `hidden_size`: {self.hidden_size}"
f" and `num_heads`: {self.num_heads})."
)
self.q_proj = operations.Linear(self.hidden_size, self.num_heads * self.head_dim, bias=config["attention_bias"], dtype=dtype, device=device)
self.k_proj = operations.Linear(self.hidden_size, self.num_key_value_heads * self.head_dim, bias=config["attention_bias"], dtype=dtype, device=device)
self.v_proj = operations.Linear(self.hidden_size, self.num_key_value_heads * self.head_dim, bias=config["attention_bias"], dtype=dtype, device=device)
self.o_proj = operations.Linear(self.num_heads * self.head_dim, self.hidden_size, bias=config["attention_bias"], dtype=dtype, device=device)
self.rotary_emb = Gemma2RotaryEmbedding(
self.head_dim,
max_position_embeddings=self.max_position_embeddings,
base=self.rope_theta,
)
def forward(self, hidden_states, attention_mask=None, position_ids=None, past_key_value=None, output_attentions=False, use_cache=False, cache_position= None):
bsz, q_len, _ = hidden_states.size()
query_states = self.q_proj(hidden_states)
key_states = self.k_proj(hidden_states)
value_states = self.v_proj(hidden_states)
query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
key_states = key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
value_states = value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
cos, sin = self.rotary_emb(value_states, position_ids)
query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin)
if past_key_value is not None:
# sin and cos are specific to RoPE models; cache_position needed for the static cache
cache_kwargs = {
"sin": sin,
"cos": cos,
"sliding_window": self.sliding_window,
"cache_position": cache_position,
}
key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs)
config = {
"scaling": self.scaling,
"num_key_value_groups": self.num_key_value_groups,
"max_position_embeddings": self.max_position_embeddings,
"attn_logit_softcapping": self.attn_logit_softcapping,
}
attn_output, attn_weights = attention_forward(config, query_states, key_states, value_states, attention_mask, output_attentions=output_attentions)
attn_output = attn_output.reshape(bsz, q_len, -1).contiguous()
attn_output = self.o_proj(attn_output)
if not output_attentions:
attn_weights = None
return attn_output, attn_weights, past_key_value
class Gemma2DecoderLayer(nn.Module):
def __init__(self, config, layer_idx, dtype=None, device=None, operations=None):
super().__init__()
self.hidden_size = config["hidden_size"]
self.config = config
self.is_sliding = not bool(layer_idx % 2)
self.self_attn = Gemma2Attention(config=config, layer_idx=layer_idx, dtype=dtype, device=device, operations=operations)
self.mlp = Gemma2MLP(config, dtype=dtype, device=device, operations=operations)
self.input_layernorm = Gemma2RMSNorm(config["hidden_size"], eps=config["rms_norm_eps"])
self.post_attention_layernorm = Gemma2RMSNorm(config["hidden_size"], eps=config["rms_norm_eps"])
self.pre_feedforward_layernorm = Gemma2RMSNorm(config["hidden_size"], eps=config["rms_norm_eps"])
self.post_feedforward_layernorm = Gemma2RMSNorm(config["hidden_size"], eps=config["rms_norm_eps"])
self.sliding_window = config["sliding_window"]
def forward(self, hidden_states, attention_mask=None, position_ids=None, past_key_value=None, output_attentions=False, use_cache=False, cache_position=None):
if self.is_sliding and attention_mask is not None: # efficient SDPA and no padding
# # Flash-attn is a 2D tensor
# if self.config["_attn_implementation == "flash_attention_2":
# if past_key_value is not None: # when decoding
# attention_mask = attention_mask[:, -self.sliding_window :]
# else:
min_dtype = torch.finfo(hidden_states.dtype).min
sliding_window_mask = torch.tril(
torch.ones_like(attention_mask, dtype=torch.bool), diagonal=-self.sliding_window
)
attention_mask = torch.where(sliding_window_mask, min_dtype, attention_mask)
if attention_mask.shape[-1] <= 1: # when decoding
attention_mask = attention_mask[:, :, :, -self.sliding_window :]
residual = hidden_states
hidden_states = self.input_layernorm(hidden_states)
# Self Attention
hidden_states, self_attn_weights, present_key_value = self.self_attn(
hidden_states=hidden_states,
attention_mask=attention_mask,
position_ids=position_ids,
past_key_value=past_key_value,
output_attentions=output_attentions,
use_cache=use_cache,
cache_position=cache_position,
)
hidden_states = self.post_attention_layernorm(hidden_states)
hidden_states = residual + hidden_states
residual = hidden_states
hidden_states = self.pre_feedforward_layernorm(hidden_states)
hidden_states = self.mlp(hidden_states)
hidden_states = self.post_feedforward_layernorm(hidden_states)
hidden_states = residual + hidden_states
outputs = (hidden_states,)
if output_attentions:
outputs += (self_attn_weights,)
if use_cache:
outputs += (present_key_value,)
return outputs
def prepare_causal_mask(input_tensor, attention_mask):
dtype, device = input_tensor.dtype, input_tensor.device
batch_size=input_tensor.shape[0]
sequence_length = input_tensor.shape[1]
target_length = attention_mask.shape[-1] if attention_mask is not None else input_tensor.shape[1]
min_dtype = torch.finfo(dtype).min
causal_mask = torch.full(
(sequence_length, target_length), fill_value=min_dtype, dtype=dtype, device=device
)
if sequence_length != 1:
causal_mask = torch.triu(causal_mask, diagonal=1)
#causal_mask *= torch.arange(target_length, device=device) > cache_position.reshape(-1, 1)
causal_mask = causal_mask[None, None, :, :].expand(batch_size, 1, -1, -1)
if attention_mask is not None:
causal_mask = causal_mask.clone() # copy to contiguous memory for in-place edit
mask_length = attention_mask.shape[-1]
padding_mask = causal_mask[:, :, :, :mask_length] + attention_mask[:, None, None, :]
padding_mask = padding_mask == 0
causal_mask[:, :, :, :mask_length] = causal_mask[:, :, :, :mask_length].masked_fill(
padding_mask, min_dtype
)
return causal_mask
class Gemma2Model(torch.nn.Module):
def __init__(self, config_dict, dtype, device, operations):
super().__init__()
self.padding_idx = 0
self.hidden_size = config_dict["hidden_size"]
self.embed_tokens = operations.Embedding(config_dict["vocab_size"], self.hidden_size, self.padding_idx, device=device, dtype=dtype)
self.num_layers = config_dict["num_hidden_layers"]
self.layers = nn.ModuleList(
[Gemma2DecoderLayer(config_dict, layer_idx, dtype=dtype, device=device, operations=operations) for layer_idx in range(config_dict["num_hidden_layers"])]
)
self.norm = Gemma2RMSNorm(self.hidden_size, eps=config_dict["rms_norm_eps"])
def get_input_embeddings(self):
return self.embed_tokens
def set_input_embeddings(self, value):
self.embed_tokens = value
def forward(self, input_ids=None, attention_mask=None, position_ids=None, intermediate_output=None, final_layer_norm_intermediate=False, *args, **kwargs):
inputs_embeds = self.embed_tokens(input_ids, out_dtype=kwargs.get("dtype", torch.float32))
hidden_states = inputs_embeds
intermediate = None
if attention_mask is not None and position_ids is None:
position_ids = attention_mask.long().cumsum(-1) - 1
position_ids.masked_fill_(attention_mask == 0, 1)
# normalized
# Gemma2 downcasts the below to float16, causing sqrt(3072)=55.4256 to become 55.5
# See https://github.com/huggingface/transformers/pull/29402
normalizer = torch.tensor(self.hidden_size**0.5, dtype=hidden_states.dtype)
hidden_states = hidden_states * normalizer
causal_mask = prepare_causal_mask(inputs_embeds, attention_mask)
if intermediate_output is not None:
if intermediate_output < 0:
intermediate_output = len(self.layers) + intermediate_output
for i, decoder_layer in enumerate(self.layers):
layer_outputs = decoder_layer(
hidden_states,
attention_mask=causal_mask,
position_ids=position_ids,
past_key_value=None,
output_attentions=False,
use_cache=False,
)
if i == intermediate_output:
intermediate = hidden_states.clone()
hidden_states = layer_outputs[0]
hidden_states = self.norm(hidden_states)
if intermediate is not None and final_layer_norm_intermediate:
intermediate = self.norm(intermediate)
return hidden_states, intermediate
+46
View File
@@ -0,0 +1,46 @@
import os
import torch
from comfy import sd1_clip
import comfy.model_management
from .gemma import Gemma2Model
from transformers import GemmaTokenizer as TFGemmaTokenizer
class GemmaClipModel(sd1_clip.SDClipModel):
def __init__(self, device="cpu", dtype=None, model_options={}, **kwargs):
textmodel_json_config = os.path.join(os.path.dirname(os.path.realpath(__file__)), "config.json")
special_tokens = {"start": 2, "end": 1, "pad": 0}
super().__init__(device=device, layer="last", layer_idx=None, textmodel_json_config=textmodel_json_config, dtype=dtype, special_tokens=special_tokens, model_class=Gemma2Model, enable_attention_masks=True, return_attention_masks=False, model_options=model_options)
class SanaClipModel(sd1_clip.SD1ClipModel):
def __init__(self, device="cpu", dtype=None, model_options={}):
super().__init__(device=device, dtype=dtype, name="gemma", clip_model=GemmaClipModel, model_options=model_options)
class GemmaTokenizer(sd1_clip.SDTokenizer):
def __init__(self, embedding_directory=None, tokenizer_data={}):
tokenizer_path = os.path.join(
os.path.dirname(os.path.dirname(os.path.realpath(__file__))),
"tokenizers", "gemma_tokenizer",
)
# TODO: reenable proper logic here - needs comfy version 44db978 or higher
super().__init__(tokenizer_path, embedding_directory=embedding_directory, pad_with_end=False, embedding_size=2304, embedding_key='gemma', tokenizer_class=TFGemmaTokenizer, has_start_token=False, pad_to_max_length=True, max_length=300, min_length=1)
self.start_token = 2
self.end_token = 1
self.pad_token = 0
def tokenize_with_weights(self, text, return_word_ids=False):
# TODO: see above, this is still just a wrapper for now
tokens = self.tokenizer(
text,
max_length=300,
padding="max_length",
truncation=True,
return_tensors="pt"
)
batched_tokens = [(x.item(), 1.0) for x in tokens.input_ids[0]]
return [batched_tokens]
class SanaTokenizer(sd1_clip.SD1Tokenizer):
def __init__(self, embedding_directory=None, tokenizer_data={}):
super().__init__(embedding_directory=embedding_directory, tokenizer_data=tokenizer_data, clip_name="gemma", tokenizer=GemmaTokenizer)
+9
View File
@@ -4,8 +4,10 @@ from enum import Enum
import comfy.sd
import comfy.utils
import comfy.text_encoders
import comfy.model_management
from .pixart.tenc import pixart_te, PixArtTokenizer
from .sana.tenc import SanaClipModel, SanaTokenizer
class TencType(Enum):
# offset in case we ever integrate w/ original
@@ -44,6 +46,8 @@ def load_text_encoder_state_dicts(state_dicts=[], embedding_directory=None, clip
for i in range(len(clip_data)):
if "transformer.resblocks.0.ln_1.weight" in clip_data[i]:
clip_data[i] = comfy.utils.clip_text_transformers_convert(clip_data[i], "", "")
elif "model.layers.25.post_feedforward_layernorm.weight" in clip_data[i]:
clip_data[i] = {k[len("model."):]:v for k,v in clip_data[i].items()}
else:
if "text_projection" in clip_data[i]:
clip_data[i]["text_projection.weight"] = clip_data[i]["text_projection"].transpose(0, 1) #old models saved with the CLIPSave node
@@ -54,6 +58,11 @@ def load_text_encoder_state_dicts(state_dicts=[], embedding_directory=None, clip
if clip_type == TencType.PixArt:
clip_target.clip = pixart_te(**comfy.sd.t5xxl_detect(clip_data))
clip_target.tokenizer = PixArtTokenizer
elif clip_type == TencType.Sana:
clip_target.clip = SanaClipModel
clip_target.tokenizer = SanaTokenizer
else:
raise NotImplementedError(f"Unknown tenc: {clip_type}")
parameters = 0
tokenizer_data = {}
@@ -0,0 +1,34 @@
{
"additional_special_tokens": [
"<start_of_turn>",
"<end_of_turn>"
],
"bos_token": {
"content": "<bos>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false
},
"eos_token": {
"content": "<eos>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false
},
"pad_token": {
"content": "<pad>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false
},
"unk_token": {
"content": "<unk>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false
}
}
File diff suppressed because it is too large Load Diff