From db5acada7bf8662fa6edaef4ced8b51c6ce1a4e4 Mon Sep 17 00:00:00 2001 From: Chujie Zheng Date: Thu, 23 May 2024 12:06:59 -0700 Subject: [PATCH 1/5] fix logits dtype --- src/transformers/pipelines/text_classification.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transformers/pipelines/text_classification.py b/src/transformers/pipelines/text_classification.py index 6521da098d4c..bc763c161487 100644 --- a/src/transformers/pipelines/text_classification.py +++ b/src/transformers/pipelines/text_classification.py @@ -202,7 +202,7 @@ def postprocess(self, model_outputs, function_to_apply=None, top_k=1, _legacy=Tr function_to_apply = ClassificationFunction.NONE outputs = model_outputs["logits"][0] - outputs = outputs.numpy() + outputs = outputs.float().numpy() if function_to_apply == ClassificationFunction.SIGMOID: scores = sigmoid(outputs) From 1afef635eff4cedd25bd92986b88c1f8b11d36d4 Mon Sep 17 00:00:00 2001 From: Chujie Zheng Date: Thu, 23 May 2024 12:07:48 -0700 Subject: [PATCH 2/5] Add bf16/fp16 tests for text_classification pipeline --- .../test_pipelines_text_classification.py | 50 ++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/tests/pipelines/test_pipelines_text_classification.py b/tests/pipelines/test_pipelines_text_classification.py index 7a33a41c0650..d71ceea275d2 100644 --- a/tests/pipelines/test_pipelines_text_classification.py +++ b/tests/pipelines/test_pipelines_text_classification.py @@ -13,6 +13,7 @@ # limitations under the License. import unittest +import torch from transformers import ( MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, @@ -20,7 +21,16 @@ TextClassificationPipeline, pipeline, ) -from transformers.testing_utils import is_pipeline_test, nested_simplify, require_tf, require_torch, slow, torch_device +from transformers.testing_utils import ( + is_pipeline_test, + nested_simplify, + require_tf, + require_torch, + require_torch_bf16, + require_torch_fp16, + slow, + torch_device +) from .test_pipelines_common import ANY @@ -106,6 +116,44 @@ def test_accepts_torch_device(self): outputs = text_classifier("This is great !") self.assertEqual(nested_simplify(outputs), [{"label": "LABEL_0", "score": 0.504}]) + @require_torch + @require_torch_bf16 + def test_accepts_torch_bf16(self): + text_classifier = pipeline( + task="text-classification", + model="hf-internal-testing/tiny-random-distilbert", + framework="pt", + device=torch.bfloat16, + ) + + outputs = text_classifier("This is great !") + self.assertEqual(nested_simplify(outputs), [{"label": "LABEL_0", "score": 0.504}]) + + @require_torch + @require_torch_fp16 + def test_accepts_torch_bf16(self): + text_classifier = pipeline( + task="text-classification", + model="hf-internal-testing/tiny-random-distilbert", + framework="pt", + device=torch.float16, + ) + + outputs = text_classifier("This is great !") + self.assertEqual(nested_simplify(outputs), [{"label": "LABEL_0", "score": 0.504}]) + + @require_torch + def test_accepts_torch_bf16(self): + text_classifier = pipeline( + task="text-classification", + model="hf-internal-testing/tiny-random-distilbert", + framework="pt", + device=torch.bfloat16, + ) + + outputs = text_classifier("This is great !") + self.assertEqual(nested_simplify(outputs), [{"label": "LABEL_0", "score": 0.504}]) + @require_tf def test_small_model_tf(self): text_classifier = pipeline( From d97bb29559b62c790bc9b78ec2800ebb9377fd67 Mon Sep 17 00:00:00 2001 From: Chujie Zheng Date: Thu, 23 May 2024 12:23:49 -0700 Subject: [PATCH 3/5] Update test_pipelines_text_classification.py --- .../test_pipelines_text_classification.py | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/tests/pipelines/test_pipelines_text_classification.py b/tests/pipelines/test_pipelines_text_classification.py index d71ceea275d2..9e83227c3a05 100644 --- a/tests/pipelines/test_pipelines_text_classification.py +++ b/tests/pipelines/test_pipelines_text_classification.py @@ -116,22 +116,8 @@ def test_accepts_torch_device(self): outputs = text_classifier("This is great !") self.assertEqual(nested_simplify(outputs), [{"label": "LABEL_0", "score": 0.504}]) - @require_torch - @require_torch_bf16 - def test_accepts_torch_bf16(self): - text_classifier = pipeline( - task="text-classification", - model="hf-internal-testing/tiny-random-distilbert", - framework="pt", - device=torch.bfloat16, - ) - - outputs = text_classifier("This is great !") - self.assertEqual(nested_simplify(outputs), [{"label": "LABEL_0", "score": 0.504}]) - - @require_torch @require_torch_fp16 - def test_accepts_torch_bf16(self): + def test_accepts_torch_fp16(self): text_classifier = pipeline( task="text-classification", model="hf-internal-testing/tiny-random-distilbert", @@ -142,7 +128,7 @@ def test_accepts_torch_bf16(self): outputs = text_classifier("This is great !") self.assertEqual(nested_simplify(outputs), [{"label": "LABEL_0", "score": 0.504}]) - @require_torch + @require_torch_bf16 def test_accepts_torch_bf16(self): text_classifier = pipeline( task="text-classification", From 3991e1810d1b0962ba7ef71487e7f74726520445 Mon Sep 17 00:00:00 2001 From: Chujie Zheng Date: Sat, 25 May 2024 21:07:34 -0700 Subject: [PATCH 4/5] fix --- tests/pipelines/test_pipelines_text_classification.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/pipelines/test_pipelines_text_classification.py b/tests/pipelines/test_pipelines_text_classification.py index 9e83227c3a05..445cd87377f9 100644 --- a/tests/pipelines/test_pipelines_text_classification.py +++ b/tests/pipelines/test_pipelines_text_classification.py @@ -13,6 +13,7 @@ # limitations under the License. import unittest + import torch from transformers import ( @@ -29,7 +30,7 @@ require_torch_bf16, require_torch_fp16, slow, - torch_device + torch_device, ) from .test_pipelines_common import ANY @@ -122,7 +123,7 @@ def test_accepts_torch_fp16(self): task="text-classification", model="hf-internal-testing/tiny-random-distilbert", framework="pt", - device=torch.float16, + torch_dtype=torch.float16, ) outputs = text_classifier("This is great !") @@ -134,7 +135,7 @@ def test_accepts_torch_bf16(self): task="text-classification", model="hf-internal-testing/tiny-random-distilbert", framework="pt", - device=torch.bfloat16, + torch_dtype=torch.bfloat16, ) outputs = text_classifier("This is great !") From 0f56fda21bb7db322428a7af654cc33d9638607d Mon Sep 17 00:00:00 2001 From: Chujie Zheng Date: Sat, 25 May 2024 21:20:34 -0700 Subject: [PATCH 5/5] fix --- tests/pipelines/test_pipelines_text_classification.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/pipelines/test_pipelines_text_classification.py b/tests/pipelines/test_pipelines_text_classification.py index 445cd87377f9..6e40f33fbb51 100644 --- a/tests/pipelines/test_pipelines_text_classification.py +++ b/tests/pipelines/test_pipelines_text_classification.py @@ -123,6 +123,7 @@ def test_accepts_torch_fp16(self): task="text-classification", model="hf-internal-testing/tiny-random-distilbert", framework="pt", + device=torch_device, torch_dtype=torch.float16, ) @@ -135,6 +136,7 @@ def test_accepts_torch_bf16(self): task="text-classification", model="hf-internal-testing/tiny-random-distilbert", framework="pt", + device=torch_device, torch_dtype=torch.bfloat16, )