mirror of
https://github.com/huggingface/transformers.git
synced 2025-07-04 05:10:06 +06:00

* stash commit * Experiment 1: Try just Gemma * Experiment 1: Just try Gemma * make fixup * Trigger tests * stash commit * Try adding Gemma3 as well * make fixup * Correct attrib names * Correct pipeline model mapping * Add in all_model_classes for Gemma1 again * Move the pipeline model mapping around again * make fixup * Revert Gemma3 changes since it's a VLM * Let's try Falcon * Correct attributes * Correct attributes * Let's try just overriding get_config() for now * Do Nemotron too * And Llama! * Do llama/persimmon * Correctly skip tests * Fix Persimmon * Include Phimoe * Fix Gemma2 * Set model_tester_class correctly * Add GLM * More models! * models models models * make fixup * Add Qwen3 + Qwen3MoE * Correct import * make fixup * Add the QuestionAnswering classes * Add the QuestionAnswering classes * Move pipeline mapping to the right place * Jetmoe too * Stop RoPE testing models with no RoPE * Fix up JetMOE a bit * Fix up JetMOE a bit * Can we just force pad_token_id all the time? * make fixup * fix starcoder2 * Move pipeline mapping * Fix RoPE skipping * Fix RecurrentGemma tests * Fix Falcon tests * Add MoE attributes * Fix values for RoPE testing * Make sure we set bos_token_id and eos_token_id in an appropriate range * make fixup * Fix GLM4 * Add mamba attributes * Revert bits of JetMOE * Re-add the JetMOE skips * Update tests/causal_lm_tester.py Co-authored-by: Arthur <48595927+ArthurZucker@users.noreply.github.com> * Add licence --------- Co-authored-by: Arthur <48595927+ArthurZucker@users.noreply.github.com>
167 lines
7.1 KiB
Python
167 lines
7.1 KiB
Python
# Copyright 2024 BigCode and The 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.
|
|
"""Testing suite for the PyTorch Starcoder2 model."""
|
|
|
|
import unittest
|
|
|
|
import pytest
|
|
|
|
from transformers import Starcoder2Config, is_torch_available
|
|
from transformers.testing_utils import (
|
|
require_bitsandbytes,
|
|
require_flash_attn,
|
|
require_torch,
|
|
require_torch_accelerator,
|
|
require_torch_gpu,
|
|
slow,
|
|
torch_device,
|
|
)
|
|
|
|
|
|
if is_torch_available():
|
|
import torch
|
|
|
|
from transformers import (
|
|
AutoTokenizer,
|
|
Starcoder2ForCausalLM,
|
|
Starcoder2ForSequenceClassification,
|
|
Starcoder2ForTokenClassification,
|
|
Starcoder2Model,
|
|
)
|
|
|
|
from ...causal_lm_tester import CausalLMModelTest, CausalLMModelTester
|
|
|
|
|
|
class Starcoder2ModelTester(CausalLMModelTester):
|
|
config_class = Starcoder2Config
|
|
if is_torch_available():
|
|
base_model_class = Starcoder2Model
|
|
causal_lm_class = Starcoder2ForCausalLM
|
|
sequence_class = Starcoder2ForSequenceClassification
|
|
token_class = Starcoder2ForTokenClassification
|
|
|
|
|
|
@require_torch
|
|
class Starcoder2ModelTest(CausalLMModelTest, unittest.TestCase):
|
|
all_model_classes = (
|
|
(Starcoder2Model, Starcoder2ForCausalLM, Starcoder2ForSequenceClassification, Starcoder2ForTokenClassification)
|
|
if is_torch_available()
|
|
else ()
|
|
)
|
|
test_headmasking = False
|
|
test_pruning = False
|
|
model_tester_class = Starcoder2ModelTester
|
|
pipeline_model_mapping = (
|
|
{
|
|
"feature-extraction": Starcoder2Model,
|
|
"text-classification": Starcoder2ForSequenceClassification,
|
|
"token-classification": Starcoder2ForTokenClassification,
|
|
"text-generation": Starcoder2ForCausalLM,
|
|
}
|
|
if is_torch_available()
|
|
else {}
|
|
)
|
|
|
|
@require_flash_attn
|
|
@require_torch_gpu
|
|
@pytest.mark.flash_attn_test
|
|
@slow
|
|
def test_flash_attn_2_inference_equivalence_right_padding(self):
|
|
self.skipTest(reason="Starcoder2 flash attention does not support right padding")
|
|
|
|
|
|
@slow
|
|
@require_torch_accelerator
|
|
class Starcoder2IntegrationTest(unittest.TestCase):
|
|
def test_starcoder2_batched_generation_sdpa(self):
|
|
EXPECTED_TEXT = [
|
|
"Hello my name is Younes and I am a student at the University of Liverpool. I am currently studying for my MSc in Computer Science. I am interested in the field of Machine Learning and I am currently working on",
|
|
"def hello_world():\n\treturn 'Hello World!'\n\n@app.route('/hello/<name>')\ndef hello_name(name):\n\treturn 'Hello %s!' % name\n\n@app",
|
|
]
|
|
model_id = "bigcode/starcoder2-7b"
|
|
|
|
model = Starcoder2ForCausalLM.from_pretrained(
|
|
model_id, torch_dtype=torch.float16, device_map="auto", attn_implementation="sdpa"
|
|
)
|
|
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
|
tokenizer.pad_token = tokenizer.eos_token
|
|
|
|
text = ["Hello my name is Younes and", "def hello_world():"]
|
|
inputs = tokenizer(text, return_tensors="pt", padding=True).to(torch_device)
|
|
|
|
output = model.generate(**inputs, max_new_tokens=40, do_sample=False)
|
|
output_text = tokenizer.batch_decode(output, skip_special_tokens=True)
|
|
self.assertEqual(EXPECTED_TEXT, output_text)
|
|
|
|
def test_starcoder2_batched_generation_eager(self):
|
|
EXPECTED_TEXT = [
|
|
"Hello my name is Younes and I am a student at the University of Liverpool. I am currently studying for my MSc in Computer Science. I am interested in the field of Machine Learning and I am currently working on",
|
|
"def hello_world():\n\treturn 'Hello World!'\n\n@app.route('/hello/<name>')\ndef hello_name(name):\n\treturn 'Hello %s!' % name\n\n@app",
|
|
]
|
|
model_id = "bigcode/starcoder2-7b"
|
|
|
|
model = Starcoder2ForCausalLM.from_pretrained(
|
|
model_id, torch_dtype=torch.float16, device_map="auto", attn_implementation="eager"
|
|
)
|
|
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
|
tokenizer.pad_token = tokenizer.eos_token
|
|
|
|
text = ["Hello my name is Younes and", "def hello_world():"]
|
|
inputs = tokenizer(text, return_tensors="pt", padding=True).to(torch_device)
|
|
|
|
output = model.generate(**inputs, max_new_tokens=40, do_sample=False)
|
|
output_text = tokenizer.batch_decode(output, skip_special_tokens=True)
|
|
self.assertEqual(EXPECTED_TEXT, output_text)
|
|
|
|
@require_flash_attn
|
|
@pytest.mark.flash_attn_test
|
|
def test_starcoder2_batched_generation_fa2(self):
|
|
EXPECTED_TEXT = [
|
|
"Hello my name is Younes and I am a student at the University of Liverpool. I am currently studying for my MSc in Computer Science. I am interested in the field of Machine Learning and I am currently working on",
|
|
"def hello_world():\n\treturn 'Hello World!'\n\n@app.route('/hello/<name>')\ndef hello_name(name):\n\treturn 'Hello %s!' % name\n\n@app",
|
|
]
|
|
model_id = "bigcode/starcoder2-7b"
|
|
|
|
model = Starcoder2ForCausalLM.from_pretrained(
|
|
model_id, torch_dtype=torch.float16, device_map="auto", attn_implementation="flash_attention_2"
|
|
)
|
|
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
|
tokenizer.pad_token = tokenizer.eos_token
|
|
|
|
text = ["Hello my name is Younes and", "def hello_world():"]
|
|
inputs = tokenizer(text, return_tensors="pt", padding=True).to(torch_device)
|
|
|
|
output = model.generate(**inputs, max_new_tokens=40, do_sample=False)
|
|
output_text = tokenizer.batch_decode(output, skip_special_tokens=True)
|
|
self.assertEqual(EXPECTED_TEXT, output_text)
|
|
|
|
@require_bitsandbytes
|
|
def test_starcoder2_batched_generation_4bit(self):
|
|
EXPECTED_TEXT = [
|
|
'Hello my name is Younes and I am a student at the University of Maryland. I am currently working on a project that is related to the topic of "How to make a game". I am currently working on a project',
|
|
'def hello_world():\n\treturn "Hello World"\n\n@app.route(\'/hello/<name>\')\ndef hello_name(name):\n\treturn "Hello " + name\n\n@app.route',
|
|
]
|
|
model_id = "bigcode/starcoder2-7b"
|
|
|
|
model = Starcoder2ForCausalLM.from_pretrained(model_id, load_in_4bit=True)
|
|
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
|
tokenizer.pad_token = tokenizer.eos_token
|
|
|
|
text = ["Hello my name is Younes and", "def hello_world():"]
|
|
inputs = tokenizer(text, return_tensors="pt", padding=True).to(torch_device)
|
|
|
|
output = model.generate(**inputs, max_new_tokens=40, do_sample=False)
|
|
output_text = tokenizer.batch_decode(output, skip_special_tokens=True)
|
|
self.assertEqual(EXPECTED_TEXT, output_text)
|