Skip to content

Conversation

@xiaoqianWX
Copy link
Member

No description provided.

Copilot AI review requested due to automatic review settings January 15, 2026 09:28
@xiaoqianWX xiaoqianWX changed the base branch from main to feat/torch-mps-support January 15, 2026 09:29
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds support for Apple Silicon MPS (Metal Performance Shaders) devices in addition to the existing CUDA support, enabling the AI-VIS library to run on Apple Silicon hardware. The changes centralize device selection and cache management in a new torch_utils module, replacing hard-coded CUDA-specific logic throughout the codebase.

Changes:

  • Added a new torch_utils.py module with centralized device selection (get_torch_device) and cache management (empty_torch_cache) functions
  • Refactored RealESRGANer initialization to use the new device selection utility instead of inline CUDA checks
  • Updated RealESRGANModel to use device-agnostic tensor creation methods (.to(device) instead of .cuda())
  • Replaced direct torch.cuda.empty_cache() calls with the new backend-agnostic empty_torch_cache() function

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
aivis/torch_utils.py New module providing centralized device selection and cache management with MPS support
aivis/models/realesrgan/utils.py Refactored to use centralized device selection and removed redundant model assignment
aivis/models/realesrgan/models/realesrgan_model.py Replaced .cuda() calls with .to(self.device) and added device parameter to tensor creation
aivis/aivis.py Updated to use centralized device utilities instead of inline CUDA checks

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

return torch.device("cpu")

if torch.cuda.is_available():
return torch.device("cuda")
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

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

The function doesn't handle specific CUDA device IDs. When gpu_id is set to a specific device like "0" or "1", the function returns torch.device("cuda") without the device index, which may not match the intended device. The function should return torch.device(f"cuda:{gpu_id}") when CUDA is available and a specific gpu_id is provided.

Suggested change
return torch.device("cuda")
cuda_device_str = "cuda" if gpu_id is None else f"cuda:{gpu_id}"
return torch.device(cuda_device_str)

Copilot uses AI. Check for mistakes.
model.eval()
self.model = model.to(self.device)
self.model.eval()
self.model = self.model.to(self.device)
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

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

The model is being reassigned after calling .to(self.device). The assignment is unnecessary since .to() is an in-place operation for module parameters. Line 71 should just be self.model.to(self.device) without the assignment, or keep the assignment but remove the redundancy with line 53.

Suggested change
self.model = self.model.to(self.device)
self.model.to(self.device)

Copilot uses AI. Check for mistakes.
Comment on lines +63 to +66
try:
mps.empty_cache()
except Exception:
pass
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

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

The broad exception handler silently swallows all exceptions without logging. Consider either narrowing the exception type or adding logging to help diagnose issues with MPS cache clearing, especially since this is a new feature.

Copilot uses AI. Check for mistakes.

if model is not None:
self.model = model.to(self.device)
self.model = model
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

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

This assignment at line 53 is redundant since the model is reassigned at line 71 after loading state dict and moving to device. The assignment at line 53 should be removed as the model needs to be moved to the target device after state dict loading anyway.

Copilot uses AI. Check for mistakes.
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 78d5404320

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +37 to +38
if gpu_id is None or str(gpu_id) == "-1":
return torch.device("cpu")

Choose a reason for hiding this comment

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

P2 Badge Preserve CUDA auto-selection when gpu_id is omitted

The new get_torch_device treats gpu_id=None as a hard disable and immediately returns CPU, which means callers that omit gpu_id (e.g., RealESRGANer defaults it to None) will never use CUDA even when it is available. Previously, the default path selected CUDA automatically, so this is a regression that can silently force CPU inference and significantly slow down workloads unless every caller now passes a non-None gpu_id.

Useful? React with 👍 / 👎.

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