Skip to content
Merged
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
9 changes: 8 additions & 1 deletion tilelang/utils/device.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import torch

IS_CUDA = torch.cuda.is_available()
IS_MPS = torch.mps.is_available()

IS_MPS = False
try:
IS_MPS = torch.backends.mps.is_available()
except AttributeError:
print("MPS backend is not available in this PyTorch build.")
except Exception as e:
print(f"An unexpected error occurred while checking MPS availability: {e}")
Comment on lines +5 to +11
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

Replace print statements with proper logging.

The guarded initialization approach is good for compatibility across PyTorch builds. However, using print() for error messages is problematic in production environments:

  • Error messages go to stdout rather than being captured in application logs
  • No log levels to filter or route messages
  • Difficult to trace in production monitoring systems

Apply this diff to use Python's logging module:

+import logging
+
+logger = logging.getLogger(__name__)
+
 import torch
 
 IS_CUDA = torch.cuda.is_available()
 
 IS_MPS = False
 try:
     IS_MPS = torch.backends.mps.is_available()
 except AttributeError:
-    print("MPS backend is not available in this PyTorch build.")
+    logger.info("MPS backend is not available in this PyTorch build.")
 except Exception as e:
-    print(f"An unexpected error occurred while checking MPS availability: {e}")
+    logger.warning("An unexpected error occurred while checking MPS availability: %s", e)

Regarding the static analysis warning (BLE001): While catching Exception is generally too broad, it may be acceptable here for cross-version compatibility. However, consider whether truly unexpected exceptions should be allowed to propagate rather than being silently logged.

Run the following script to verify if logging is already used elsewhere in the codebase:


🏁 Script executed:

#!/bin/bash
# Description: Check if logging module is used in the codebase

# Search for logging imports and usage patterns
echo "=== Checking for logging imports ==="
rg -n "^import logging|^from logging import" --type py

echo -e "\n=== Checking for logger instances ==="
rg -n "logger\s*=" --type py -A 2

echo -e "\n=== Checking for existing print statements in utils ==="
rg -n "print\(" tilelang/utils/ --type py

Length of output: 7219


Replace print statements with logging for consistency with project standards.

The codebase consistently uses logging.getLogger(__name__) across 25+ modules. The print() statements in device.py at lines 9 and 11 are inconsistent with this established pattern and should use logging instead.

Apply this refactor:

+import logging
+
+logger = logging.getLogger(__name__)
+
 import torch
 
 IS_CUDA = torch.cuda.is_available()
 
 IS_MPS = False
 try:
     IS_MPS = torch.backends.mps.is_available()
 except AttributeError:
-    print("MPS backend is not available in this PyTorch build.")
+    logger.info("MPS backend is not available in this PyTorch build.")
 except Exception as e:
-    print(f"An unexpected error occurred while checking MPS availability: {e}")
+    logger.warning("An unexpected error occurred while checking MPS availability: %s", e)

The broad Exception catch is acceptable for cross-version compatibility with different PyTorch builds, but consider if truly unexpected exceptions should propagate rather than be silently logged.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
IS_MPS = False
try:
IS_MPS = torch.backends.mps.is_available()
except AttributeError:
print("MPS backend is not available in this PyTorch build.")
except Exception as e:
print(f"An unexpected error occurred while checking MPS availability: {e}")
import logging
logger = logging.getLogger(__name__)
import torch
IS_CUDA = torch.cuda.is_available()
IS_MPS = False
try:
IS_MPS = torch.backends.mps.is_available()
except AttributeError:
logger.info("MPS backend is not available in this PyTorch build.")
except Exception as e:
logger.warning("An unexpected error occurred while checking MPS availability: %s", e)
🧰 Tools
🪛 Ruff (0.14.0)

10-10: Do not catch blind exception: Exception

(BLE001)

🤖 Prompt for AI Agents
In tilelang/utils/device.py around lines 5 to 11, replace the two print() calls
with the module logger obtained via logging.getLogger(__name__); create a logger
= logging.getLogger(__name__) near the top of the file if not already present,
then change the first print to logger.debug or logger.info (e.g.,
logger.debug("MPS backend is not available in this PyTorch build.")) and change
the second print in the broad exception handler to logger.exception or
logger.error including the exception message (e.g., logger.exception("An
unexpected error occurred while checking MPS availability: %s", e)); keep the
existing exception handling semantics but ensure messages use the logger for
consistency.



def get_current_device():
Expand Down
Loading