Skip to content

It is recommended to remove the "device" parameter from the code #70

Description

@Voiddd16
class AUCMLoss(torch.nn.Module):
    def __init__(self, 
                 margin=1.0, 
                 imratio=None,
                 version='v1',
                 device=None):
        super(AUCMLoss, self).__init__()
        if not device:
            self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
        else:
            self.device =  device

The lightning framework supports the use of multiple devices, and users who do not use the framework can use the to method to move to the desired device, and the "device" parameter makes the lightning framework use RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:2 and cuda:0!
So I suggest removing the "device" parameter

Activity

  1. Voiddd16 commented on Aug 2, 2025

    @Voiddd16
    Author

    In addition, the tensors defined in the class will not be actively transferred to "device", and they should be changed to "nn. Parameter", the content comes from AI, please fully verify before adopting

    example:
    origin:

    self.a = torch.zeros(1, dtype=torch.float32, requires_grad=True)
    self.b = torch.zeros(1, dtype=torch.float32,  requires_grad=True) 
    self.alpha = torch.zeros(1, dtype=torch.float32, requires_grad=True)
    

    change

    self.a = nn.Parameter(torch.zeros(1, dtype=torch.float32))
    self.b = nn.Parameter(torch.zeros(1, dtype=torch.float32))
    self.alpha = nn.Parameter(torch.zeros(1, dtype=torch.float32))
    
  2. optmai commented on Aug 11, 2025

    @optmai
    Collaborator

    Thank you for the suggestion! We will look into this issue and improve the library.

  3. reopened this on Mar 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions