Skip to content

Commit

Permalink
refactor: minor code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
yijun-lee committed Dec 30, 2024
1 parent d487b5d commit d3f00e0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
10 changes: 7 additions & 3 deletions src/transformers/integrations/ggml.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
Integration with GGML / The file is copied and adapted from https://github.com/99991/pygguf
with extra methods beings exposed
"""

from array import array

import numpy as np
Expand Down Expand Up @@ -147,7 +148,7 @@
".output.": ".lm_head.",
"output_norm": "ln_f",
},
"t5": {
"t5": {
"token_embd": "shared",
"dec.blk.{bid}.attn_q": "decoder.block.{bid}.layer.0.SelfAttention.q",
"dec.blk.{bid}.attn_k": "decoder.block.{bid}.layer.0.SelfAttention.k",
Expand Down Expand Up @@ -276,7 +277,7 @@
"attn_k": "self_attn.k_proj",
"attn_output": "self_attn.o_proj",
"output_norm": "model.norm",
}
},
}


Expand Down Expand Up @@ -374,7 +375,7 @@
"vocab_size": "vocab_size",
"attention.layer_norm_epsilon": "layer_norm_epsilon",
},
"t5": {
"t5": {
"context_length": "n_positions",
"block_count": "num_layers",
"feed_forward_length": "d_ff",
Expand Down Expand Up @@ -770,6 +771,7 @@ def converted(self) -> Tokenizer:
tokenizer = super().converted(vocab, merges)
return tokenizer


class GGUFT5Converter(T5Converter):
def __init__(self, tokenizer_dict):
# set dummy data to avoid unnecessary merges calculation
Expand Down Expand Up @@ -832,6 +834,7 @@ def converted(self) -> Tokenizer:

return tokenizer


class GGUFGemmaConverter(GemmaConverter):
def __init__(self, tokenizer_dict):
# set dummy data to avoid unnecessary merges calculation
Expand Down Expand Up @@ -896,6 +899,7 @@ def converted(self) -> Tokenizer:

return tokenizer


GGUF_TO_FAST_CONVERTERS = {
"llama": GGUFLlamaConverter,
"qwen2": GGUFQwen2Converter,
Expand Down
6 changes: 4 additions & 2 deletions src/transformers/modeling_gguf_pytorch_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,17 +237,19 @@ def process(self, weights, name, **kwargs):
weights = np.log(-weights)
return GGUFTensor(weights, name, {})


class Gemma2TensorProcessor(TensorProcessor):
def __init__(self, config=None):
super().__init__(config=config)

#ref: https://github.com/ggerganov/llama.cpp/blob/d79d8f39b4da6deca4aea8bf130c6034c482b320/convert_hf_to_gguf.py#L3191
#ref: https://github.com/huggingface/transformers/blob/fc37f38915372c15992b540dfcbbe00a916d4fc6/src/transformers/models/gemma/modeling_gemma.py#L89
# ref: https://github.com/ggerganov/llama.cpp/blob/d79d8f39b4da6deca4aea8bf130c6034c482b320/convert_hf_to_gguf.py#L3191
# ref: https://github.com/huggingface/transformers/blob/fc37f38915372c15992b540dfcbbe00a916d4fc6/src/transformers/models/gemma/modeling_gemma.py#L89
def process(self, weights, name, **kwargs):
if "norm.weight" in name:
weights = weights - 1
return GGUFTensor(weights, name, {})


TENSOR_PROCESSORS = {
"llama": LlamaTensorProcessor,
"qwen2moe": Qwen2MoeTensorProcessor,
Expand Down

0 comments on commit d3f00e0

Please sign in to comment.