chendl/compositional_test
1
1# Copyright 2020 The HuggingFace Team. All rights reserved.2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14 15import unittest16 17from transformers import (18 MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,19 TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,20 SummarizationPipeline,21 TFPreTrainedModel,22 pipeline,23)24from transformers.testing_utils import get_gpu_count, is_pipeline_test, require_tf, require_torch, slow, torch_device25from transformers.tokenization_utils import TruncationStrategy26 27from .test_pipelines_common import ANY28 29 30DEFAULT_DEVICE_NUM = -1 if torch_device == "cpu" else 031 32 33@is_pipeline_test34class SummarizationPipelineTests(unittest.TestCase):35 model_mapping = MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING36 tf_model_mapping = TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING37 38 def get_test_pipeline(self, model, tokenizer, processor):39 summarizer = SummarizationPipeline(model=model, tokenizer=tokenizer)40 return summarizer, ["(CNN)The Palestinian Authority officially became", "Some other text"]41 42 def run_pipeline_test(self, summarizer, _):43 model = summarizer.model44 45 outputs = summarizer("(CNN)The Palestinian Authority officially became")46 self.assertEqual(outputs, [{"summary_text": ANY(str)}])47 48 outputs = summarizer(49 "(CNN)The Palestinian Authority officially became ",50 num_beams=2,51 min_length=2,52 max_length=5,53 )54 self.assertEqual(outputs, [{"summary_text": ANY(str)}])55 56 # Some models (Switch Transformers, LED, T5, LongT5, etc) can handle long sequences.57 model_can_handle_longer_seq = [58 "SwitchTransformersConfig",59 "T5Config",60 "LongT5Config",61 "LEDConfig",62 "PegasusXConfig",63 "FSMTConfig",64 "M2M100Config",65 "ProphetNetConfig", # positional embeddings up to a fixed maximum size (otherwise clamping the values)66 ]67 if model.config.__class__.__name__ not in model_can_handle_longer_seq:68 # Too long and exception is expected.69 # For TF models, if the weights are initialized in GPU context, we won't get expected index error from70 # the embedding layer.71 if not (72 isinstance(model, TFPreTrainedModel)73 and get_gpu_count() > 074 and len(summarizer.model.trainable_weights) > 075 ):76 with self.assertRaises(Exception):77 outputs = summarizer("This " * 1000)78 outputs = summarizer("This " * 1000, truncation=TruncationStrategy.ONLY_FIRST)79 80 @require_torch81 def test_small_model_pt(self):82 summarizer = pipeline(task="summarization", model="sshleifer/tiny-mbart", framework="pt")83 outputs = summarizer("This is a small test")84 self.assertEqual(85 outputs,86 [87 {88 "summary_text": "เข้าไปเข้าไปเข้าไปเข้าไปเข้าไปเข้าไปเข้าไปเข้าไปเข้าไปเข้าไปเข้าไปเข้าไปเข้าไปเข้าไปเข้าไปเข้าไปเข้าไปเข้าไป"89 }90 ],91 )92 93 @require_tf94 def test_small_model_tf(self):95 summarizer = pipeline(task="summarization", model="sshleifer/tiny-mbart", framework="tf")96 outputs = summarizer("This is a small test")97 self.assertEqual(98 outputs,99 [100 {101 "summary_text": "เข้าไปเข้าไปเข้าไปเข้าไปเข้าไปเข้าไปเข้าไปเข้าไปเข้าไปเข้าไปเข้าไปเข้าไปเข้าไปเข้าไปเข้าไปเข้าไปเข้าไปเข้าไป"102 }103 ],104 )105 106 @require_torch107 @slow108 def test_integration_torch_summarization(self):109 summarizer = pipeline(task="summarization", device=DEFAULT_DEVICE_NUM)110 cnn_article = (111 " (CNN)The Palestinian Authority officially became the 123rd member of the International Criminal Court on"112 " Wednesday, a step that gives the court jurisdiction over alleged crimes in Palestinian territories. The"113 " formal accession was marked with a ceremony at The Hague, in the Netherlands, where the court is based."114 " The Palestinians signed the ICC's founding Rome Statute in January, when they also accepted its"115 ' jurisdiction over alleged crimes committed "in the occupied Palestinian territory, including East'116 ' Jerusalem, since June 13, 2014." Later that month, the ICC opened a preliminary examination into the'117 " situation in Palestinian territories, paving the way for possible war crimes investigations against"118 " Israelis. As members of the court, Palestinians may be subject to counter-charges as well. Israel and"119 " the United States, neither of which is an ICC member, opposed the Palestinians' efforts to join the"120 " body. But Palestinian Foreign Minister Riad al-Malki, speaking at Wednesday's ceremony, said it was a"121 ' move toward greater justice. "As Palestine formally becomes a State Party to the Rome Statute today, the'122 ' world is also a step closer to ending a long era of impunity and injustice," he said, according to an'123 ' ICC news release. "Indeed, today brings us closer to our shared goals of justice and peace." Judge'124 " Kuniko Ozaki, a vice president of the ICC, said acceding to the treaty was just the first step for the"125 ' Palestinians. "As the Rome Statute today enters into force for the State of Palestine, Palestine'126 " acquires all the rights as well as responsibilities that come with being a State Party to the Statute."127 ' These are substantive commitments, which cannot be taken lightly," she said. Rights group Human Rights'128 ' Watch welcomed the development. "Governments seeking to penalize Palestine for joining the ICC should'129 " immediately end their pressure, and countries that support universal acceptance of the court's treaty"130 ' should speak out to welcome its membership," said Balkees Jarrah, international justice counsel for the'131 " group. \"What's objectionable is the attempts to undermine international justice, not Palestine's"132 ' decision to join a treaty to which over 100 countries around the world are members." In January, when'133 " the preliminary ICC examination was opened, Israeli Prime Minister Benjamin Netanyahu described it as an"134 ' outrage, saying the court was overstepping its boundaries. The United States also said it "strongly"'135 " disagreed with the court's decision. \"As we have said repeatedly, we do not believe that Palestine is a"136 ' state and therefore we do not believe that it is eligible to join the ICC," the State Department said in'137 ' a statement. It urged the warring sides to resolve their differences through direct negotiations. "We'138 ' will continue to oppose actions against Israel at the ICC as counterproductive to the cause of peace,"'139 " it said. But the ICC begs to differ with the definition of a state for its purposes and refers to the"140 ' territories as "Palestine." While a preliminary examination is not a formal investigation, it allows the'141 " court to review evidence and determine whether to investigate suspects on both sides. Prosecutor Fatou"142 ' Bensouda said her office would "conduct its analysis in full independence and impartiality." The war'143 " between Israel and Hamas militants in Gaza last summer left more than 2,000 people dead. The inquiry"144 " will include alleged war crimes committed since June. The International Criminal Court was set up in"145 " 2002 to prosecute genocide, crimes against humanity and war crimes. CNN's Vasco Cotovio, Kareem Khadder"146 " and Faith Karimi contributed to this report."147 )148 expected_cnn_summary = (149 " The Palestinian Authority becomes the 123rd member of the International Criminal Court . The move gives"150 " the court jurisdiction over alleged crimes in Palestinian territories . Israel and the United States"151 " opposed the Palestinians' efforts to join the court . Rights group Human Rights Watch welcomes the move,"152 " says governments seeking to penalize Palestine should end pressure ."153 )154 result = summarizer(cnn_article)155 self.assertEqual(result[0]["summary_text"], expected_cnn_summary)156 