Skip to content
Merged
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
41 changes: 41 additions & 0 deletions unsloth/import_fixes.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,47 @@ def __getattr__(self, name):
warnings.filterwarnings("ignore", message = "`int4_weight_only` is deprecated")
warnings.filterwarnings("ignore", message = "`int8_weight_only` is deprecated")

# TorchAO deprecated import paths (https://github.com/pytorch/ao/issues/2752)
warnings.filterwarnings(
"ignore",
message = r"Importing.*from torchao\.dtypes.*is deprecated",
category = DeprecationWarning,
)
warnings.filterwarnings(
"ignore",
message = r"Importing BlockSparseLayout from torchao\.dtypes is deprecated",
Comment on lines +131 to +134
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

This warning filter is redundant. The more general filter on lines 127-130 (r"Importing.*from torchao\.dtypes.*is deprecated") already covers this specific case. Removing this will make the code slightly cleaner.

category = DeprecationWarning,
)

# SWIG builtin type warnings (from bitsandbytes/triton SWIG bindings)
warnings.filterwarnings(
"ignore",
message = r"builtin type Swig.*has no __module__ attribute",
category = DeprecationWarning,
)

# Triton autotuner deprecation (https://github.com/triton-lang/triton/pull/4496)
warnings.filterwarnings(
"ignore",
message = r"warmup, rep, and use_cuda_graph parameters are deprecated",
category = DeprecationWarning,
)

# Python 3.12+ multiprocessing fork warning in multi-threaded processes
warnings.filterwarnings(
"ignore",
message = r".*multi-threaded.*use of fork\(\) may lead to deadlocks",
category = DeprecationWarning,
)

# Resource warnings from internal socket/file operations
warnings.filterwarnings(
"ignore", message = r"unclosed.*socket", category = ResourceWarning
)
warnings.filterwarnings(
"ignore", message = r"unclosed file.*dev/null", category = ResourceWarning
)


# Fix up AttributeError: 'MessageFactory' object has no attribute 'GetPrototype'
# MUST do this at the start primarily due to tensorflow causing issues
Expand Down
8 changes: 6 additions & 2 deletions unsloth/models/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1152,8 +1152,12 @@ def has_internet(host = "8.8.8.8", port = 53, timeout = 3):
return False
try:
socket.setdefaulttimeout(timeout)
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
return True
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.connect((host, port))
return True
finally:
sock.close()
Comment on lines +1155 to +1160
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

While the try...finally block correctly closes the socket, you can make this code more idiomatic and concise by using a with statement. Sockets support the context manager protocol, which automatically handles closing the resource, even if errors occur.

        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
            sock.connect((host, port))
            return True

except socket.error as ex:
return False

Expand Down