-
Notifications
You must be signed in to change notification settings - Fork 284
[wwb]: load transformers model first, then only trust_remote_code #2270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
eaidova
merged 7 commits into
openvinotoolkit:master
from
eaidova:ea/wwb_trust_remote_code_prior
May 28, 2025
+81
−43
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8fc6d5d
[wwb]: load transformers model first, then only trust_remote_code
eaidova fd90804
align remote code everywhere
eaidova 050f30e
Update tools/who_what_benchmark/examples/openvino_batched_eval.py
eaidova b142191
Merge branch 'master' into ea/wwb_trust_remote_code_prior
eaidova cfc64d8
align remote code everywhere
eaidova 8ac4322
fix conflict
eaidova 177ac04
Update tools/who_what_benchmark/whowhatbench/wwb.py
eaidova File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,13 +22,16 @@ def __init__(self, model, model_dir, model_type): | |
|
||
if model_type == "text" or model_type == "visual-text": | ||
try: | ||
self.config = AutoConfig.from_pretrained(model_dir, trust_remote_code=True) | ||
except Exception: | ||
self.config = AutoConfig.from_pretrained(model_dir) | ||
except Exception: | ||
self.config = AutoConfig.from_pretrained(model_dir, trust_remote_code=True) | ||
elif model_type == "text-to-image": | ||
from diffusers import DiffusionPipeline | ||
self.config = DiffusionPipeline.load_config( | ||
model_dir, trust_remote_code=True) | ||
try: | ||
self.config = DiffusionPipeline.load_config(model_dir) | ||
except Exception: | ||
self.config = DiffusionPipeline.load_config(model_dir, trust_remote_code=True) | ||
|
||
|
||
def __getattr__(self, attr): | ||
if attr in self.__dict__: | ||
|
@@ -89,7 +92,12 @@ def load_text_hf_pipeline(model_id, device): | |
model_kwargs = {} | ||
|
||
if not torch.cuda.is_available or device.lower() == "cpu": | ||
config = AutoConfig.from_pretrained(model_id, trust_remote_code=True) | ||
trust_remote_code = False | ||
try: | ||
config = AutoConfig.from_pretrained(model_id) | ||
except Exception: | ||
config = AutoConfig.from_pretrained(model_id, trust_remote_code=True) | ||
trust_remote_code = True | ||
is_gptq = False | ||
is_awq = False | ||
if getattr(config, "quantization_config", None): | ||
|
@@ -99,13 +107,19 @@ def load_text_hf_pipeline(model_id, device): | |
# infer in FP32 | ||
model_kwargs["torch_dtype"] = torch.float32 | ||
with mock_torch_cuda_is_available(is_gptq or is_awq): | ||
model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True, device_map="cpu", **model_kwargs) | ||
model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=trust_remote_code, device_map="cpu", **model_kwargs) | ||
if is_awq: | ||
model.is_awq = is_awq | ||
else: | ||
model = AutoModelForCausalLM.from_pretrained( | ||
model_id, trust_remote_code=True, device_map=device.lower(), **model_kwargs | ||
) | ||
try: | ||
model = AutoModelForCausalLM.from_pretrained( | ||
model_id, trust_remote_code=False, device_map=device.lower(), **model_kwargs | ||
) | ||
except Exception: | ||
model = AutoModelForCausalLM.from_pretrained( | ||
model_id, trust_remote_code=True, device_map=device.lower(), **model_kwargs | ||
) | ||
|
||
model.eval() | ||
return model | ||
|
||
|
@@ -126,7 +140,7 @@ def load_text_model( | |
from optimum.intel.openvino import OVModelForCausalLM | ||
try: | ||
model = OVModelForCausalLM.from_pretrained( | ||
model_id, trust_remote_code=True, device=device, ov_config=ov_config | ||
model_id, device=device, ov_config=ov_config | ||
) | ||
except Exception: | ||
try: | ||
|
@@ -177,23 +191,22 @@ def load_text2image_model( | |
elif use_hf: | ||
from diffusers import DiffusionPipeline | ||
logger.info("Using HF Transformers API") | ||
model = DiffusionPipeline.from_pretrained( | ||
model_id, trust_remote_code=True) | ||
try: | ||
model = DiffusionPipeline.from_pretrained(model_id) | ||
except Exception: | ||
model = DiffusionPipeline.from_pretrained(model_id, trust_remote_code=True) | ||
else: | ||
logger.info("Using Optimum API") | ||
from optimum.intel import OVPipelineForText2Image | ||
TEXT2IMAGEPipeline = OVPipelineForText2Image | ||
|
||
try: | ||
model = TEXT2IMAGEPipeline.from_pretrained( | ||
model_id, trust_remote_code=True, device=device, ov_config=ov_config, safety_checker=None, | ||
model_id, device=device, ov_config=ov_config, safety_checker=None, | ||
) | ||
except ValueError: | ||
config = AutoConfig.from_pretrained( | ||
model_id, trust_remote_code=True) | ||
model = TEXT2IMAGEPipeline.from_pretrained( | ||
model_id, | ||
config=config, | ||
trust_remote_code=True, | ||
use_cache=True, | ||
device=device, | ||
|
@@ -223,19 +236,25 @@ def load_visual_text_model( | |
): | ||
if use_hf: | ||
logger.info("Using HF Transformers API") | ||
config = AutoConfig.from_pretrained(model_id, trust_remote_code=True) | ||
trust_remote_code = False | ||
try: | ||
config = AutoConfig.from_pretrained(model_id, trust_remote_code=False) | ||
except Exception: | ||
config = AutoConfig.from_pretrained(model_id, trust_remote_code=True) | ||
trust_remote_code = True | ||
|
||
try: | ||
model = AutoModelForVision2Seq.from_pretrained( | ||
model_id, trust_remote_code=True, device_map=device.lower() | ||
model_id, trust_remote_code=trust_remote_code, device_map=device.lower() | ||
) | ||
except ValueError: | ||
try: | ||
model = AutoModel.from_pretrained( | ||
model_id, trust_remote_code=True, device_map=device.lower() | ||
model_id, trust_remote_code=trust_remote_code, device_map=device.lower() | ||
) | ||
except ValueError: | ||
model = AutoModelForCausalLM.from_pretrained( | ||
model_id, trust_remote_code=True, device_map=device.lower(), _attn_implementation="eager", use_flash_attention_2=False | ||
model_id, trust_remote_code=trust_remote_code, device_map=device.lower(), _attn_implementation="eager", use_flash_attention_2=False | ||
) | ||
model.eval() | ||
try: | ||
|
@@ -255,7 +274,7 @@ def load_visual_text_model( | |
from optimum.intel.openvino import OVModelForVisualCausalLM | ||
try: | ||
model = OVModelForVisualCausalLM.from_pretrained( | ||
model_id, trust_remote_code=True, device=device, ov_config=ov_config | ||
model_id, device=device, ov_config=ov_config | ||
) | ||
except ValueError: | ||
config = AutoConfig.from_pretrained(model_id, trust_remote_code=True) | ||
|
@@ -301,13 +320,11 @@ def load_imagetext2image_model( | |
from optimum.intel.openvino import OVPipelineForImage2Image | ||
try: | ||
model = OVPipelineForImage2Image.from_pretrained( | ||
model_id, trust_remote_code=True, device=device, ov_config=ov_config, safety_checker=None, | ||
model_id, device=device, ov_config=ov_config, safety_checker=None, | ||
) | ||
except ValueError: | ||
config = AutoConfig.from_pretrained(model_id, trust_remote_code=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @andreyanufr I do not know if this code branch can be reached at all for diffusers models, but loading config in such way does not work for diffusion models:
That is why I removed this line. |
||
model = OVPipelineForImage2Image.from_pretrained( | ||
model_id, | ||
config=config, | ||
trust_remote_code=True, | ||
use_cache=True, | ||
device=device, | ||
|
@@ -348,14 +365,12 @@ def load_inpainting_model( | |
from optimum.intel.openvino import OVPipelineForInpainting | ||
try: | ||
model = OVPipelineForInpainting.from_pretrained( | ||
model_id, trust_remote_code=True, device=device, ov_config=ov_config, safety_checker=None, | ||
model_id, device=device, ov_config=ov_config, safety_checker=None, | ||
) | ||
except ValueError as e: | ||
logger.error("Failed to load inpaiting pipeline. Details:\n", e) | ||
config = AutoConfig.from_pretrained(model_id, trust_remote_code=True) | ||
model = OVPipelineForInpainting.from_pretrained( | ||
model_id, | ||
config=config, | ||
trust_remote_code=True, | ||
use_cache=True, | ||
device=device, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.