3
3
import os
4
4
import json
5
5
import pandas as pd
6
+ from PIL import Image
6
7
import logging
7
8
from datasets import load_dataset
8
9
from diffusers import DiffusionPipeline
@@ -35,9 +36,14 @@ class GenAIModelWrapper:
35
36
A helper class to store additional attributes for GenAI models
36
37
"""
37
38
38
- def __init__ (self , model , model_dir ):
39
+ def __init__ (self , model , model_dir , model_type ):
39
40
self .model = model
40
- self .config = AutoConfig .from_pretrained (model_dir , trust_remote_code = True )
41
+ self .model_type = model_type
42
+
43
+ if model_type == "text" :
44
+ self .config = AutoConfig .from_pretrained (model_dir , trust_remote_code = True )
45
+ elif model_type == "text-to-image" :
46
+ self .config = DiffusionPipeline .load_config (model_dir , trust_remote_code = True )
41
47
42
48
def __getattr__ (self , attr ):
43
49
if attr in self .__dict__ :
@@ -53,40 +59,41 @@ def load_text_genai_pipeline(model_dir, device="CPU"):
53
59
logger .error ("Failed to import openvino_genai package. Please install it." )
54
60
exit (- 1 )
55
61
logger .info ("Using OpenVINO GenAI API" )
56
- return GenAIModelWrapper (openvino_genai .LLMPipeline (model_dir , device ), model_dir )
62
+ return GenAIModelWrapper (openvino_genai .LLMPipeline (model_dir , device ), model_dir , "text" )
57
63
58
64
59
65
def load_text_model (
60
66
model_id , device = "CPU" , ov_config = None , use_hf = False , use_genai = False
61
67
):
62
- if use_hf :
63
- logger .info ("Using HF Transformers API" )
64
- return AutoModelForCausalLM .from_pretrained (
65
- model_id , trust_remote_code = True , device_map = device .lower ()
66
- )
67
-
68
- if use_genai :
69
- return load_text_genai_pipeline (model_id , device )
70
-
71
68
if ov_config :
72
69
with open (ov_config ) as f :
73
70
ov_options = json .load (f )
74
71
else :
75
72
ov_options = None
76
- try :
77
- model = OVModelForCausalLM .from_pretrained (
78
- model_id , trust_remote_code = True , device = device , ov_config = ov_options
79
- )
80
- except ValueError :
81
- config = AutoConfig .from_pretrained (model_id , trust_remote_code = True )
82
- model = OVModelForCausalLM .from_pretrained (
83
- model_id ,
84
- config = config ,
85
- trust_remote_code = True ,
86
- use_cache = True ,
87
- device = device ,
88
- ov_config = ov_options ,
73
+
74
+ if use_hf :
75
+ logger .info ("Using HF Transformers API" )
76
+ model = AutoModelForCausalLM .from_pretrained (
77
+ model_id , trust_remote_code = True , device_map = device .lower ()
89
78
)
79
+ elif use_genai :
80
+ model = load_text_genai_pipeline (model_id , device )
81
+ else :
82
+ try :
83
+ model = OVModelForCausalLM .from_pretrained (
84
+ model_id , trust_remote_code = True , device = device , ov_config = ov_options
85
+ )
86
+ except ValueError :
87
+ config = AutoConfig .from_pretrained (model_id , trust_remote_code = True )
88
+ model = OVModelForCausalLM .from_pretrained (
89
+ model_id ,
90
+ config = config ,
91
+ trust_remote_code = True ,
92
+ use_cache = True ,
93
+ device = device ,
94
+ ov_config = ov_options ,
95
+ )
96
+
90
97
return model
91
98
92
99
@@ -95,6 +102,20 @@ def load_text_model(
95
102
}
96
103
97
104
105
+ def load_text2image_genai_pipeline (model_dir , device = "CPU" ):
106
+ try :
107
+ import openvino_genai
108
+ except ImportError :
109
+ logger .error ("Failed to import openvino_genai package. Please install it." )
110
+ exit (- 1 )
111
+ logger .info ("Using OpenVINO GenAI API" )
112
+ return GenAIModelWrapper (
113
+ openvino_genai .Text2ImagePipeline (model_dir , device ),
114
+ model_dir ,
115
+ "text-to-image"
116
+ )
117
+
118
+
98
119
def load_text2image_model (
99
120
model_type , model_id , device = "CPU" , ov_config = None , use_hf = False , use_genai = False
100
121
):
@@ -104,25 +125,28 @@ def load_text2image_model(
104
125
else :
105
126
ov_options = None
106
127
107
- if use_hf :
108
- return DiffusionPipeline .from_pretrained (model_id , trust_remote_code = True )
128
+ if use_genai :
129
+ model = load_text2image_genai_pipeline (model_id , device )
130
+ elif use_hf :
131
+ model = DiffusionPipeline .from_pretrained (model_id , trust_remote_code = True )
132
+ else :
133
+ TEXT2IMAGEPipeline = TEXT2IMAGE_TASK2CLASS [model_type ]
109
134
110
- TEXT2IMAGEPipeline = TEXT2IMAGE_TASK2CLASS [model_type ]
135
+ try :
136
+ model = TEXT2IMAGEPipeline .from_pretrained (
137
+ model_id , trust_remote_code = True , device = device , ov_config = ov_options
138
+ )
139
+ except ValueError :
140
+ config = AutoConfig .from_pretrained (model_id , trust_remote_code = True )
141
+ model = TEXT2IMAGEPipeline .from_pretrained (
142
+ model_id ,
143
+ config = config ,
144
+ trust_remote_code = True ,
145
+ use_cache = True ,
146
+ device = device ,
147
+ ov_config = ov_options ,
148
+ )
111
149
112
- try :
113
- model = TEXT2IMAGEPipeline .from_pretrained (
114
- model_id , trust_remote_code = True , device = device , ov_config = ov_options
115
- )
116
- except ValueError :
117
- config = AutoConfig .from_pretrained (model_id , trust_remote_code = True )
118
- model = TEXT2IMAGEPipeline .from_pretrained (
119
- model_id ,
120
- config = config ,
121
- trust_remote_code = True ,
122
- use_cache = True ,
123
- device = device ,
124
- ov_config = ov_options ,
125
- )
126
150
return model
127
151
128
152
@@ -278,6 +302,18 @@ def parse_args():
278
302
action = "store_true" ,
279
303
help = "Use LLMPipeline from transformers library to instantiate the model." ,
280
304
)
305
+ parser .add_argument (
306
+ "--image-size" ,
307
+ type = int ,
308
+ default = 512 ,
309
+ help = "Text-to-image specific parameter that defines the image resolution." ,
310
+ )
311
+ parser .add_argument (
312
+ "--num-inference-steps" ,
313
+ type = int ,
314
+ default = 4 ,
315
+ help = "Text-to-image specific parameter that defines the number of denoising steps." ,
316
+ )
281
317
282
318
return parser .parse_args ()
283
319
@@ -340,6 +376,18 @@ def genai_gen_answer(model, tokenizer, question, max_new_tokens, skip_question):
340
376
return out
341
377
342
378
379
+ def genai_gen_image (model , prompt , num_inference_steps , generator = None ):
380
+ image_tensor = model .generate (
381
+ prompt ,
382
+ width = model .resolution [0 ],
383
+ height = model .resolution [1 ],
384
+ num_inference_steps = num_inference_steps ,
385
+ random_generator = generator
386
+ )
387
+ image = Image .fromarray (image_tensor .data [0 ])
388
+ return image
389
+
390
+
343
391
def get_evaluator (base_model , args ):
344
392
# config = AutoConfig.from_pretrained(model_id, trust_remote_code=True)
345
393
# task = TasksManager.infer_task_from_model(config._name_or_path)
@@ -368,6 +416,10 @@ def get_evaluator(base_model, args):
368
416
gt_data = args .gt_data ,
369
417
test_data = prompts ,
370
418
num_samples = args .num_samples ,
419
+ resolution = (args .image_size , args .image_size ),
420
+ num_inference_steps = args .num_inference_steps ,
421
+ gen_image_fn = genai_gen_image if args .genai else None ,
422
+ is_genai = args .genai
371
423
)
372
424
else :
373
425
raise ValueError (f"Unsupported task: { task } " )
@@ -446,7 +498,7 @@ def main():
446
498
args .genai ,
447
499
)
448
500
all_metrics_per_question , all_metrics = evaluator .score (
449
- target_model , genai_gen_answer if args .genai else None
501
+ target_model , evaluator . get_generation_fn () if args .genai else None
450
502
)
451
503
logger .info ("Metrics for model: %s" , args .target_model )
452
504
logger .info (all_metrics )
0 commit comments