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

* WIP refactoring pipeline tests - switching to fast tokenizers * fix dialog pipeline and fill-mask * refactoring pipeline tests backbone * make large tests slow * fix tests (tf Bart inactive for now) * fix doc... * clean up for merge * fixing tests - remove bart from summarization until there is TF * fix quality and RAG * Add new translation pipeline tests - fix JAX tests * only slow for dialog * Fixing the missing TF-BART imports in modeling_tf_auto * spin out pipeline tests in separate CI job * adding pipeline test to CI YAML * add slow pipeline tests * speed up tf and pt join test to avoid redoing all the standalone pt and tf tests * Update src/transformers/tokenization_utils_base.py Co-authored-by: Sam Shleifer <sshleifer@gmail.com> * Update src/transformers/pipelines.py Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com> * Update src/transformers/pipelines.py Co-authored-by: Lysandre Debut <lysandre@huggingface.co> * Update src/transformers/testing_utils.py Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com> * add require_torch and require_tf in is_pt_tf_cross_test Co-authored-by: Sam Shleifer <sshleifer@gmail.com> Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com> Co-authored-by: Lysandre Debut <lysandre@huggingface.co>
94 lines
4.4 KiB
Python
94 lines
4.4 KiB
Python
import unittest
|
|
|
|
from transformers import Conversation, pipeline
|
|
from transformers.testing_utils import require_torch, slow, torch_device
|
|
|
|
from .test_pipelines_common import MonoInputPipelineCommonMixin
|
|
|
|
|
|
DEFAULT_DEVICE_NUM = -1 if torch_device == "cpu" else 0
|
|
|
|
|
|
class TextGenerationPipelineTests(MonoInputPipelineCommonMixin, unittest.TestCase):
|
|
pipeline_task = "conversational"
|
|
small_models = [] # Models tested without the @slow decorator
|
|
large_models = ["microsoft/DialoGPT-medium"] # Models tested with the @slow decorator
|
|
valid_inputs = [Conversation("Hi there!"), [Conversation("Hi there!"), Conversation("How are you?")]]
|
|
invalid_inputs = ["Hi there!", Conversation()]
|
|
|
|
def _test_pipeline(
|
|
self, nlp
|
|
): # e overide the default test method to check that the output is a `Conversation` object
|
|
self.assertIsNotNone(nlp)
|
|
|
|
mono_result = nlp(self.valid_inputs[0])
|
|
self.assertIsInstance(mono_result, Conversation)
|
|
|
|
multi_result = nlp(self.valid_inputs[1])
|
|
self.assertIsInstance(multi_result, list)
|
|
self.assertIsInstance(multi_result[0], Conversation)
|
|
# Inactive conversations passed to the pipeline raise a ValueError
|
|
self.assertRaises(ValueError, nlp, self.valid_inputs[1])
|
|
|
|
for bad_input in self.invalid_inputs:
|
|
self.assertRaises(Exception, nlp, bad_input)
|
|
self.assertRaises(Exception, nlp, self.invalid_inputs)
|
|
|
|
@require_torch
|
|
@slow
|
|
def test_integration_torch_conversation(self):
|
|
# When
|
|
nlp = pipeline(task="conversational", device=DEFAULT_DEVICE_NUM)
|
|
conversation_1 = Conversation("Going to the movies tonight - any suggestions?")
|
|
conversation_2 = Conversation("What's the last book you have read?")
|
|
# Then
|
|
self.assertEqual(len(conversation_1.past_user_inputs), 0)
|
|
self.assertEqual(len(conversation_2.past_user_inputs), 0)
|
|
# When
|
|
result = nlp([conversation_1, conversation_2], do_sample=False, max_length=1000)
|
|
# Then
|
|
self.assertEqual(result, [conversation_1, conversation_2])
|
|
self.assertEqual(len(result[0].past_user_inputs), 1)
|
|
self.assertEqual(len(result[1].past_user_inputs), 1)
|
|
self.assertEqual(len(result[0].generated_responses), 1)
|
|
self.assertEqual(len(result[1].generated_responses), 1)
|
|
self.assertEqual(result[0].past_user_inputs[0], "Going to the movies tonight - any suggestions?")
|
|
self.assertEqual(result[0].generated_responses[0], "The Big Lebowski")
|
|
self.assertEqual(result[1].past_user_inputs[0], "What's the last book you have read?")
|
|
self.assertEqual(result[1].generated_responses[0], "The Last Question")
|
|
# When
|
|
conversation_2.add_user_input("Why do you recommend it?")
|
|
result = nlp(conversation_2, do_sample=False, max_length=1000)
|
|
# Then
|
|
self.assertEqual(result, conversation_2)
|
|
self.assertEqual(len(result.past_user_inputs), 2)
|
|
self.assertEqual(len(result.generated_responses), 2)
|
|
self.assertEqual(result.past_user_inputs[1], "Why do you recommend it?")
|
|
self.assertEqual(result.generated_responses[1], "It's a good book.")
|
|
|
|
@require_torch
|
|
@slow
|
|
def test_integration_torch_conversation_truncated_history(self):
|
|
# When
|
|
nlp = pipeline(task="conversational", min_length_for_response=24, device=DEFAULT_DEVICE_NUM)
|
|
conversation_1 = Conversation("Going to the movies tonight - any suggestions?")
|
|
# Then
|
|
self.assertEqual(len(conversation_1.past_user_inputs), 0)
|
|
# When
|
|
result = nlp(conversation_1, do_sample=False, max_length=36)
|
|
# Then
|
|
self.assertEqual(result, conversation_1)
|
|
self.assertEqual(len(result.past_user_inputs), 1)
|
|
self.assertEqual(len(result.generated_responses), 1)
|
|
self.assertEqual(result.past_user_inputs[0], "Going to the movies tonight - any suggestions?")
|
|
self.assertEqual(result.generated_responses[0], "The Big Lebowski")
|
|
# When
|
|
conversation_1.add_user_input("Is it an action movie?")
|
|
result = nlp(conversation_1, do_sample=False, max_length=36)
|
|
# Then
|
|
self.assertEqual(result, conversation_1)
|
|
self.assertEqual(len(result.past_user_inputs), 2)
|
|
self.assertEqual(len(result.generated_responses), 2)
|
|
self.assertEqual(result.past_user_inputs[1], "Is it an action movie?")
|
|
self.assertEqual(result.generated_responses[1], "It's a comedy.")
|