Skip to content

Refactor image editing script for clarity and performance#4

Open
kollarsandor wants to merge 1 commit into
inclusionAI:mainfrom
kollarsandor:patch-2
Open

Refactor image editing script for clarity and performance#4
kollarsandor wants to merge 1 commit into
inclusionAI:mainfrom
kollarsandor:patch-2

Conversation

@kollarsandor

Copy link
Copy Markdown

Refactor argument parsing and improve error handling. Update image encoding and model loading processes for better clarity and performance.

Refactor argument parsing and improve error handling. Update image encoding and model loading processes for better clarity and performance.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the image editing script to improve robustness, input validation, and memory management. It introduces specialized argument parsing, path normalization, and detailed verification for image tokens and grid metadata. Additionally, model loading and inference have been optimized with better error handling and resource cleanup. A security concern was identified in the weight-loading utility, where a broad exception handler could potentially allow the execution of unsafe code by bypassing security checks.

Comment thread scripts/image_edit.py
Comment on lines +263 to +269
def _torch_load(path):
try:
return torch.load(path, map_location="cpu", weights_only=True)
except TypeError:
return torch.load(path, map_location="cpu")
except Exception:
return torch.load(path, map_location="cpu", weights_only=False)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security-medium medium

The broad except Exception block that falls back to weights_only=False bypasses the security benefits of weights_only=True. If the first attempt fails due to unsafe content (e.g., a pickle.UnpicklingError when loading untrusted classes), the second attempt will execute it anyway, potentially leading to arbitrary code execution. It is safer to only catch TypeError to handle older PyTorch versions and let other exceptions propagate.

Suggested change
def _torch_load(path):
try:
return torch.load(path, map_location="cpu", weights_only=True)
except TypeError:
return torch.load(path, map_location="cpu")
except Exception:
return torch.load(path, map_location="cpu", weights_only=False)
def _torch_load(path):
try:
return torch.load(path, map_location="cpu", weights_only=True)
except TypeError:
# Fallback for older PyTorch versions (< 1.13) which do not support weights_only
return torch.load(path, map_location="cpu")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant