Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion unsloth_zoo/llama_cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ def install_llama_cpp(

print_outputs = []
missing_packages, system_type = check_build_requirements()
sudo = do_we_need_sudo()
sudo = do_we_need_sudo(system_type)
kwargs = {"sudo" : sudo, "print_output" : print_output, "print_outputs" : print_outputs, "system_type": system_type}

if not missing_packages:
Expand Down
17 changes: 12 additions & 5 deletions unsloth_zoo/patching_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,22 @@ def patch_compiling_bitsandbytes():
# Disable dynamo on Linear4bit, Linear8bit and other future modules
if os.environ.get("UNSLOTH_ENABLE_LOGGING", "0") == "1":
print("Unsloth: Bitsandbytes < 0.46.0 does not support torch.compile - disabling.")
# Use a namespace dictionary to properly store imported modules
namespace = {}
for x in ["bitsandbytes.nn.modules", "peft.tuners.lora.bnb",]:
exec(f"import {x}", globals(), locals())
layers = dir(eval(x))
try:
exec(f"import {x}", namespace)
except ImportError:
# peft may not be installed, skip silently
continue
module = eval(x, namespace)
layers = dir(module)
for fx in layers:
try: layer = eval(f"{x}.{fx}")
try: layer = getattr(module, fx)
except: continue
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Using a bare except is generally discouraged as it can catch unexpected errors, including SystemExit and KeyboardInterrupt, making the program harder to debug and interrupt. Per PEP 8, it's better to catch specific exceptions. In this case, since accessing attributes from dir() can sometimes raise various exceptions if they are computed properties, catching Exception would be a safer and more explicit choice.

Suggested change
except: continue
except Exception: continue

if not hasattr(layer, "forward"): continue
if hasattr(eval(f"{x}.{fx}.forward"), "__wrapped__"): continue
exec(f"{x}.{fx}.forward = torch._disable_dynamo({x}.{fx}.forward)", globals(), locals())
if hasattr(layer.forward, "__wrapped__"): continue
layer.forward = torch._disable_dynamo(layer.forward)
pass
pass
pass
Expand Down
6 changes: 5 additions & 1 deletion unsloth_zoo/vision_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,11 @@ def __init__(
self.pad_to_multiple_of = pad_to_multiple_of
self.snap_to_patch_size = snap_to_patch_size
try:
self.patch_size = model.config.vision_config.patch_size
patch_size = model.config.vision_config.patch_size
# Handle tuple patch sizes (e.g., (14, 14) for InternVL3) - use first element
if isinstance(patch_size, (tuple, list)):
patch_size = patch_size[0]
self.patch_size = patch_size
except:
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The use of a bare except is not ideal as it can hide unexpected errors and goes against PEP 8 guidelines. The code in the try block is likely to raise AttributeError if model.config.vision_config.patch_size does not exist, or IndexError if patch_size is an empty list or tuple. Specifying these exceptions makes the code more robust and easier to understand.

Suggested change
except:
except (AttributeError, IndexError):

if hasattr(model.config, 'vision_config') and hasattr(model.config.vision_config, 'model_type'):
lower_name = model.config.vision_config.model_type.lower()
Expand Down