2222from transformers import AutoTokenizer
2323from transformers .pipelines import Pipeline
2424
25- from optimum .pipelines import pipeline
25+ from optimum .pipelines import pipeline as optimum_pipeline
2626from optimum .utils .testing_utils import remove_directory
2727
2828
29+ DEVICE = "cpu"
2930GENERATE_KWARGS = {"max_new_tokens" : 10 , "min_new_tokens" : 5 , "do_sample" : True }
3031
3132
@@ -49,7 +50,7 @@ def _create_dummy_audio(self) -> Dict[str, Any]:
4950
5051 def test_text_classification_pipeline (self ):
5152 """Test text classification ORT pipeline"""
52- pipe = pipeline (task = "text-classification" , accelerator = "ort" )
53+ pipe = optimum_pipeline (task = "text-classification" , accelerator = "ort" , device = DEVICE )
5354 self .assertIsInstance (pipe , Pipeline )
5455 text = self ._create_dummy_text ()
5556 result = pipe (text )
@@ -61,7 +62,7 @@ def test_text_classification_pipeline(self):
6162
6263 def test_token_classification_pipeline (self ):
6364 """Test token classification ORT pipeline"""
64- pipe = pipeline (task = "token-classification" , accelerator = "ort" )
65+ pipe = optimum_pipeline (task = "token-classification" , accelerator = "ort" , device = DEVICE )
6566 self .assertIsInstance (pipe , Pipeline )
6667 text = self ._create_dummy_text ()
6768 result = pipe (text )
@@ -74,7 +75,7 @@ def test_token_classification_pipeline(self):
7475
7576 def test_question_answering_pipeline (self ):
7677 """Test question answering ORT pipeline"""
77- pipe = pipeline (task = "question-answering" , accelerator = "ort" )
78+ pipe = optimum_pipeline (task = "question-answering" , accelerator = "ort" , device = DEVICE )
7879 self .assertIsInstance (pipe , Pipeline )
7980 question = "What animal jumps?"
8081 context = "The quick brown fox jumps over the lazy dog."
@@ -88,7 +89,7 @@ def test_question_answering_pipeline(self):
8889
8990 def test_fill_mask_pipeline (self ):
9091 """Test fill mask ORT pipeline"""
91- pipe = pipeline (task = "fill-mask" , accelerator = "ort" )
92+ pipe = optimum_pipeline (task = "fill-mask" , accelerator = "ort" , device = DEVICE )
9293 self .assertIsInstance (pipe , Pipeline )
9394 text = "The weather is <mask> today."
9495 result = pipe (text )
@@ -100,7 +101,7 @@ def test_fill_mask_pipeline(self):
100101
101102 def test_feature_extraction_pipeline (self ):
102103 """Test feature extraction ORT pipeline"""
103- pipe = pipeline (task = "feature-extraction" , accelerator = "ort" )
104+ pipe = optimum_pipeline (task = "feature-extraction" , accelerator = "ort" , device = DEVICE )
104105 self .assertIsInstance (pipe , Pipeline )
105106 text = self ._create_dummy_text ()
106107 result = pipe (text )
@@ -111,7 +112,7 @@ def test_feature_extraction_pipeline(self):
111112
112113 def test_text_generation_pipeline (self ):
113114 """Test text generation ORT pipeline"""
114- pipe = pipeline (task = "text-generation" , accelerator = "ort" )
115+ pipe = optimum_pipeline (task = "text-generation" , accelerator = "ort" , device = DEVICE )
115116 self .assertIsInstance (pipe , Pipeline )
116117 text = "The future of AI is"
117118 result = pipe (text , ** GENERATE_KWARGS )
@@ -123,7 +124,7 @@ def test_text_generation_pipeline(self):
123124
124125 def test_summarization_pipeline (self ):
125126 """Test summarization ORT pipeline"""
126- pipe = pipeline (task = "summarization" , accelerator = "ort" )
127+ pipe = optimum_pipeline (task = "summarization" , accelerator = "ort" , device = DEVICE )
127128 self .assertIsInstance (pipe , Pipeline )
128129 text = "The quick brown fox jumps over the lazy dog."
129130 result = pipe (text , ** GENERATE_KWARGS )
@@ -134,7 +135,7 @@ def test_summarization_pipeline(self):
134135
135136 def test_translation_pipeline (self ):
136137 """Test translation ORT pipeline"""
137- pipe = pipeline (task = "translation_en_to_de" , accelerator = "ort" )
138+ pipe = optimum_pipeline (task = "translation_en_to_de" , accelerator = "ort" , device = DEVICE )
138139 self .assertIsInstance (pipe , Pipeline )
139140 text = "Hello, how are you?"
140141 result = pipe (text , ** GENERATE_KWARGS )
@@ -145,7 +146,7 @@ def test_translation_pipeline(self):
145146
146147 def test_text2text_generation_pipeline (self ):
147148 """Test text2text generation ORT pipeline"""
148- pipe = pipeline (task = "text2text-generation" , accelerator = "ort" )
149+ pipe = optimum_pipeline (task = "text2text-generation" , accelerator = "ort" , device = DEVICE )
149150 self .assertIsInstance (pipe , Pipeline )
150151 text = "translate English to German: Hello, how are you?"
151152 result = pipe (text , ** GENERATE_KWARGS )
@@ -156,7 +157,7 @@ def test_text2text_generation_pipeline(self):
156157
157158 def test_zero_shot_classification_pipeline (self ):
158159 """Test zero shot classification ORT pipeline"""
159- pipe = pipeline (task = "zero-shot-classification" , accelerator = "ort" )
160+ pipe = optimum_pipeline (task = "zero-shot-classification" , accelerator = "ort" , device = DEVICE )
160161 self .assertIsInstance (pipe , Pipeline )
161162 text = "This is a great movie with excellent acting."
162163 candidate_labels = ["positive" , "negative" , "neutral" ]
@@ -169,7 +170,7 @@ def test_zero_shot_classification_pipeline(self):
169170
170171 def test_image_classification_pipeline (self ):
171172 """Test image classification ORT pipeline"""
172- pipe = pipeline (task = "image-classification" , accelerator = "ort" )
173+ pipe = optimum_pipeline (task = "image-classification" , accelerator = "ort" , device = DEVICE )
173174 self .assertIsInstance (pipe , Pipeline )
174175 image = self ._create_dummy_image ()
175176 result = pipe (image )
@@ -181,7 +182,7 @@ def test_image_classification_pipeline(self):
181182
182183 def test_image_segmentation_pipeline (self ):
183184 """Test image segmentation ORT pipeline"""
184- pipe = pipeline (task = "image-segmentation" , accelerator = "ort" )
185+ pipe = optimum_pipeline (task = "image-segmentation" , accelerator = "ort" , device = DEVICE )
185186 self .assertIsInstance (pipe , Pipeline )
186187 image = self ._create_dummy_image ()
187188 result = pipe (image )
@@ -194,7 +195,7 @@ def test_image_segmentation_pipeline(self):
194195
195196 def test_image_to_text_pipeline (self ):
196197 """Test image to text ORT pipeline"""
197- pipe = pipeline (task = "image-to-text" , accelerator = "ort" )
198+ pipe = optimum_pipeline (task = "image-to-text" , accelerator = "ort" , device = DEVICE )
198199 self .assertIsInstance (pipe , Pipeline )
199200 image = self ._create_dummy_image ()
200201 result = pipe (image , generate_kwargs = GENERATE_KWARGS )
@@ -205,7 +206,7 @@ def test_image_to_text_pipeline(self):
205206
206207 def test_image_to_image_pipeline (self ):
207208 """Test image to image ORT pipeline"""
208- pipe = pipeline (task = "image-to-image" , accelerator = "ort" )
209+ pipe = optimum_pipeline (task = "image-to-image" , accelerator = "ort" , device = DEVICE )
209210 self .assertIsInstance (pipe , Pipeline )
210211 image = self ._create_dummy_image ()
211212 result = pipe (image )
@@ -215,7 +216,7 @@ def test_image_to_image_pipeline(self):
215216 # TODO: Enable when fixed in optimum-onnx
216217 # def test_automatic_speech_recognition_pipeline(self):
217218 # """Test automatic speech recognition ORT pipeline"""
218- # pipe = pipeline (task="automatic-speech-recognition", accelerator="ort")
219+ # pipe = optimum_pipeline (task="automatic-speech-recognition", accelerator="ort", device=DEVICE )
219220 # audio = self._create_dummy_audio()
220221 # result = pipe(audio, generate_kwargs=GENERATE_KWARGS)
221222
@@ -224,7 +225,7 @@ def test_image_to_image_pipeline(self):
224225
225226 def test_audio_classification_pipeline (self ):
226227 """Test audio classification ORT pipeline"""
227- pipe = pipeline (task = "audio-classification" , accelerator = "ort" )
228+ pipe = optimum_pipeline (task = "audio-classification" , accelerator = "ort" , device = DEVICE )
228229 self .assertIsInstance (pipe , Pipeline )
229230 audio = self ._create_dummy_audio ()
230231 result = pipe (audio )
@@ -240,7 +241,7 @@ def test_pipeline_with_ort_model(self):
240241
241242 tokenizer = AutoTokenizer .from_pretrained ("distilbert-base-cased" )
242243 model = ORTModelForFeatureExtraction .from_pretrained ("distilbert-base-cased" , export = True )
243- pipe = pipeline (task = "feature-extraction" , model = model , tokenizer = tokenizer , accelerator = "ort" )
244+ pipe = optimum_pipeline (task = "feature-extraction" , model = model , tokenizer = tokenizer , accelerator = "ort" )
244245 self .assertIsInstance (pipe , Pipeline )
245246 text = self ._create_dummy_text ()
246247 result = pipe (text )
@@ -251,7 +252,9 @@ def test_pipeline_with_ort_model(self):
251252
252253 def test_pipeline_with_model_id (self ):
253254 """Test ORT pipeline with a custom model id"""
254- pipe = pipeline (task = "feature-extraction" , model = "distilbert-base-cased" , accelerator = "ort" )
255+ pipe = optimum_pipeline (
256+ task = "feature-extraction" , model = "distilbert-base-cased" , accelerator = "ort" , device = DEVICE
257+ )
255258 self .assertIsInstance (pipe , Pipeline )
256259 text = self ._create_dummy_text ()
257260 result = pipe (text )
@@ -262,13 +265,13 @@ def test_pipeline_with_model_id(self):
262265 def test_pipeline_with_invalid_task (self ):
263266 """Test ORT pipeline with an unsupported task"""
264267 with self .assertRaises (KeyError ) as context :
265- _ = pipeline (task = "invalid-task" , accelerator = "ort" )
268+ _ = optimum_pipeline (task = "invalid-task" , accelerator = "ort" , device = DEVICE )
266269 self .assertIn ("Unknown task invalid-task" , str (context .exception ))
267270
268271 def test_pipeline_with_invalid_accelerator (self ):
269272 """Test ORT pipeline with an unsupported accelerator"""
270273 with self .assertRaises (ValueError ) as context :
271- _ = pipeline (task = "feature-extraction" , accelerator = "invalid-accelerator" )
274+ _ = optimum_pipeline (task = "feature-extraction" , accelerator = "invalid-accelerator" , device = DEVICE )
272275 self .assertIn ("Accelerator invalid-accelerator not recognized" , str (context .exception ))
273276
274277 def tearDown (self ):
0 commit comments