code: def _find_first_loss_violation(
      losses: torch.tensor,
      device: Union[str, torch.device, torch.cuda.device],
  ) -> torch.tensor:
      """
      Find the index of the first violation where ``1 - positive_score + negative_score`` is greater
      than 0.
  
      """
      # set all negative losses to 0 and all positive losses to 1
      losses[losses < 0] = 0
      losses[losses > 0] = 1
  
      # after this, maximum value will be the first non-zero (bad) loss
      reverse_indices = torch.arange(losses.shape[1], 0, -1).to(device)
      min_index_of_good_loss = losses * reverse_indices
  
      # report the first loss that is positive here (not 0-based indexed)
      number_of_tries = torch.argmax(
          min_index_of_good_loss, 1, keepdim=True
      ).flatten()
  
      return number_of_tries
  
imports: from typing import Dict, Optional, Union
  import torch
  from collie.loss.metadata_utils import ideal_difference_from_metadata
label_desc: Use image node_id 2 for calling a global function with example usage: _find_first_loss_violation(losses, device) and returns: number_of_tries