Skip to content
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

Cleanup #20

Merged
merged 1 commit into from
Mar 12, 2025
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
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ lint.ignore = [
"N802",
"N812",
"PD901",
"PLR0911",
"PLR0912",
"PLR0913",
"PLR0915",
Expand Down
52 changes: 0 additions & 52 deletions tools/compare_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,58 +120,6 @@ def get_system_summary(results_dir: Path) -> str:
"",
]

# Extract GPU information from each library's results
gpu_info = {}
for file_path in result_files:
with file_path.open() as f:
lib_data = json.load(f)

library = file_path.stem.replace("_results", "")
lib_metadata = lib_data.get("metadata", {})

# Check for GPU info in thread_settings
if "thread_settings" in lib_metadata:
thread_settings = lib_metadata["thread_settings"]

# For GPU-based libraries (Kornia, TorchVision)
if "pytorch" in thread_settings:
pytorch_settings = thread_settings["pytorch"]

# Handle string representation
if isinstance(pytorch_settings, str):
import re

# Check if GPU is available
if "gpu_available': True" in pytorch_settings:
# Try to extract GPU name
gpu_name_match = re.search(r"gpu_name': '([^']+)'", pytorch_settings)
if gpu_name_match:
gpu_info[library] = gpu_name_match.group(1)
else:
# Try to extract GPU device
gpu_device_match = re.search(r"gpu_device': ([^,}]+)", pytorch_settings)
if gpu_device_match:
gpu_info[library] = f"GPU {gpu_device_match.group(1)}"
else:
gpu_info[library] = "GPU (details unknown)"
# Handle dictionary representation
elif isinstance(pytorch_settings, dict):
if pytorch_settings.get("gpu_available", False):
gpu_name = pytorch_settings.get("gpu_name")
if gpu_name:
gpu_info[library] = gpu_name
else:
gpu_device = pytorch_settings.get("gpu_device", "Unknown")
gpu_info[library] = f"GPU {gpu_device}"

# Add GPU information if available
if gpu_info:
summary.append("### GPU Information")
summary.append("")
for library, gpu in sorted(gpu_info.items()):
summary.append(f"- {library.capitalize()}: {gpu}")
summary.append("")

summary.extend(
[
"### Benchmark Parameters",
Expand Down
76 changes: 37 additions & 39 deletions tools/compare_video_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,49 @@ def format_throughput(throughput: float, std: float | None = None, is_max: bool
if std is not None:
formatted = f"{formatted} ± {std:.2f}"

if is_max:
return f"**{formatted}**"

return formatted
return f"**{formatted}**" if is_max else formatted


def format_time(time_ms: float, std: float | None = None) -> str:
"""Format time value with optional standard deviation."""
return f"{time_ms:.2f} ± {std:.2f}" if std is not None else f"{time_ms:.2f}"


def extract_gpu_info(thread_settings: dict[str, Any]) -> str:
Copy link

Choose a reason for hiding this comment

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

issue (complexity): Consider extracting the regex-based GPU name extraction into its own helper function to simplify the nested conditional logic and improve readability through early returns and reduced nesting depth within the extract_gpu_info function, while maintaining all existing functionality.

Below is a suggestion to break up the nested logic by extracting the regex-based GPU name extraction into its own helper. This helps flatten conditionals while keeping functionality intact. For example:

def extract_gpu_name_from_string(pytorch_settings: str) -> str | None:
    import re
    gpu_name_match = re.search(r"gpu_name': '([^']+)'", pytorch_settings)
    if gpu_name_match:
        return gpu_name_match.group(1)
    gpu_device_match = re.search(r"gpu_device': ([^,}]+)", pytorch_settings)
    if gpu_device_match:
        return f"GPU {gpu_device_match.group(1)}"
    return None

def extract_gpu_info(thread_settings: dict[str, Any]) -> str:
    """Extract GPU information from thread settings."""
    if "pytorch" not in thread_settings:
        return "Unknown hardware"
    pytorch_settings = thread_settings["pytorch"]

    # Handle string format with early returns
    if isinstance(pytorch_settings, str):
        if "gpu_available': True" in pytorch_settings:
            info = extract_gpu_name_from_string(pytorch_settings)
            return info if info else "GPU (details unknown)"
        return "CPU (GPU not available)"

    # Handle dictionary format
    if pytorch_settings.get("gpu_available", False):
        gpu_name = pytorch_settings.get("gpu_name")
        if gpu_name:
            return gpu_name
        gpu_device = pytorch_settings.get("gpu_device", "Unknown")
        return f"GPU {gpu_device}"

    return "CPU (GPU not available)"

Actionable Steps:

  1. Extract Regex Logic: Create a helper like extract_gpu_name_from_string that covers the inline regex matches.
  2. Use Early Returns: Flatten the conditional flow by returning early when possible.
  3. Keep Functionality: Ensure that all branches remain intact while reducing nesting.

These steps help isolate complex parts and improve readability while keeping all existing functionality.

"""Extract GPU information from thread settings."""
if "pytorch" not in thread_settings:
return "Unknown hardware"

pytorch_settings = thread_settings["pytorch"]

# Handle the case where pytorch settings are stored as a string
if isinstance(pytorch_settings, str):
# Try to extract GPU info from the string
if "gpu_available': True" in pytorch_settings:
# Extract GPU name if available
if "gpu_name':" in pytorch_settings:
import re
Copy link

Choose a reason for hiding this comment

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

issue (code-quality): Use named expression to simplify assignment and conditional [×3] (use-named-expression)


gpu_name_match = re.search(r"gpu_name': '([^']+)'", pytorch_settings)
if gpu_name_match:
return gpu_name_match.group(1)
# Extract GPU device if name not available
gpu_device_match = re.search(r"gpu_device': ([^,}]+)", pytorch_settings)
if gpu_device_match:
return f"GPU {gpu_device_match.group(1)}"
return "GPU (details unknown)"
return "GPU (details unknown)"
return "CPU (GPU not available)"
# Handle the case where pytorch settings are a dictionary
if pytorch_settings.get("gpu_available", False):
gpu_name = pytorch_settings.get("gpu_name", None)
if gpu_name:
return gpu_name
gpu_device = pytorch_settings.get("gpu_device", "Unknown")
return f"GPU {gpu_device}"
return "CPU (GPU not available)"


def get_hardware_info(results: dict[str, dict[str, Any]]) -> dict[str, str]:
"""Extract hardware information from metadata."""
hardware_info = {}
Expand All @@ -59,41 +91,7 @@ def get_hardware_info(results: dict[str, dict[str, Any]]) -> dict[str, str]:

# For GPU-based libraries (Kornia, TorchVision, etc.)
elif "pytorch" in thread_settings:
# Handle the case where pytorch settings are stored as a string
pytorch_settings = thread_settings["pytorch"]
if isinstance(pytorch_settings, str):
# Try to extract GPU info from the string
if "gpu_available': True" in pytorch_settings:
# Extract GPU name if available
if "gpu_name':" in pytorch_settings:
import re

gpu_name_match = re.search(r"gpu_name': '([^']+)'", pytorch_settings)
if gpu_name_match:
gpu_name = gpu_name_match.group(1)
hardware_info[library] = gpu_name
else:
# Extract GPU device if name not available
gpu_device_match = re.search(r"gpu_device': ([^,}]+)", pytorch_settings)
if gpu_device_match:
gpu_device = gpu_device_match.group(1)
hardware_info[library] = f"GPU {gpu_device}"
else:
hardware_info[library] = "GPU (details unknown)"
else:
hardware_info[library] = "GPU (details unknown)"
else:
hardware_info[library] = "CPU (GPU not available)"
# Handle the case where pytorch settings are a dictionary
elif pytorch_settings.get("gpu_available", False):
gpu_name = pytorch_settings.get("gpu_name", None)
if gpu_name:
hardware_info[library] = gpu_name
else:
gpu_device = pytorch_settings.get("gpu_device", "Unknown")
hardware_info[library] = f"GPU {gpu_device}"
else:
hardware_info[library] = "CPU (GPU not available)"
hardware_info[library] = extract_gpu_info(thread_settings)
else:
hardware_info[library] = "Unknown hardware"
# Default hardware info if thread_settings not found
Expand Down
4 changes: 2 additions & 2 deletions tools/generate_speedup_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,9 @@ def main() -> None:
"std_speedup": speedups[args.reference_library].std() if not speedups.empty else 0,
"faster_transforms": len(speedups[speedups[args.reference_library] > 1.0]) if not speedups.empty else 0,
"max_speedup": speedups[args.reference_library].max() if not speedups.empty else 0,
"max_speedup_transform": speedups[args.reference_library].idxmax() if not speedups.empty else "N/A",
"max_speedup_transform": "N/A" if speedups.empty else speedups[args.reference_library].idxmax(),
"min_speedup": speedups[args.reference_library].min() if not speedups.empty else 0,
"min_speedup_transform": speedups[args.reference_library].idxmin() if not speedups.empty else "N/A",
"min_speedup_transform": "N/A" if speedups.empty else speedups[args.reference_library].idxmin(),
}

# Log summary
Expand Down