prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Enhance `ContrastiveLoss` to avoid warning
  Call ContrastiveLoss will see a warning message:
  ```
  To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
  ```
  
  Simple code to reproduce this issue:
  ```
  from monai.losses import ContrastiveLoss
  import torch
  
  inp = torch.randn([2, 10])
  target = torch.randn([2, 10])
  loss = ContrastiveLoss(batch_size=2)
  loss(inp, target)
  ```
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `monai/losses/contrastive.py`
  Content:
  ```
  1 # Copyright (c) MONAI Consortium
  2 # Licensed under the Apache License, Version 2.0 (the "License");
  3 # you may not use this file except in compliance with the License.
  4 # You may obtain a copy of the License at
  5 #     http://www.apache.org/licenses/LICENSE-2.0
  6 # Unless required by applicable law or agreed to in writing, software
  7 # distributed under the License is distributed on an "AS IS" BASIS,
  8 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9 # See the License for the specific language governing permissions and
  10 # limitations under the License.
  11 
  12 import torch
  13 from torch.nn import functional as F
  14 from torch.nn.modules.loss import _Loss
  15 
  16 from monai.utils import deprecated_arg
  17 
  18 
  19 class ContrastiveLoss(_Loss):
  20 
  21     """
  22     Compute the Contrastive loss defined in:
  23 
  24         Chen, Ting, et al. "A simple framework for contrastive learning of visual representations." International
  25         conference on machine learning. PMLR, 2020. (http://proceedings.mlr.press/v119/chen20j.html)
  26 
  27     Adapted from:
  28         https://github.com/Sara-Ahmed/SiT/blob/1aacd6adcd39b71efc903d16b4e9095b97dda76f/losses.py#L5
  29 
  30     """
  31 
  32     @deprecated_arg(name="reduction", since="0.8", msg_suffix="`reduction` is no longer supported.")
  33     def __init__(self, temperature: float = 0.5, batch_size: int = 1, reduction="sum") -> None:
  34         """
  35         Args:
  36             temperature: Can be scaled between 0 and 1 for learning from negative samples, ideally set to 0.5.
  37             batch_size: The number of samples.
  38 
  39         Raises:
  40             ValueError: When an input of dimension length > 2 is passed
  41             ValueError: When input and target are of different shapes
  42 
  43         .. deprecated:: 0.8.0
  44 
  45             `reduction` is no longer supported.
  46 
  47         """
  48         super().__init__()
  49 
  50         self.batch_size = batch_size
  51         self.temperature = temperature
  52 
  53     def forward(self, input: torch.Tensor, target: torch.Tensor) -> torch.Tensor:
  54         """
  55         Args:
  56             input: the shape should be B[F].
  57             target: the shape should be B[F].
  58         """
  59         if len(target.shape) > 2 or len(input.shape) > 2:
  60             raise ValueError(
  61                 f"Either target or input has dimensions greater than 2 where target "
  62                 f"shape is ({target.shape}) and input shape is ({input.shape})"
  63             )
  64 
  65         if target.shape != input.shape:
  66             raise ValueError(f"ground truth has differing shape ({target.shape}) from input ({input.shape})")
  67 
  68         temperature_tensor = torch.tensor(self.temperature).to(input.device)
  69 
  70         norm_i = F.normalize(input, dim=1)
  71         norm_j = F.normalize(target, dim=1)
  72 
  73         negatives_mask = ~torch.eye(self.batch_size * 2, self.batch_size * 2, dtype=torch.bool)
  74         negatives_mask = torch.tensor(negatives_mask, dtype=torch.float)
  75         negatives_mask = torch.clone(torch.as_tensor(negatives_mask)).to(input.device)
  76 
  77         repr = torch.cat([norm_i, norm_j], dim=0)
  78         sim_matrix = F.cosine_similarity(repr.unsqueeze(1), repr.unsqueeze(0), dim=2)
  79         sim_ij = torch.diag(sim_matrix, self.batch_size)
  80         sim_ji = torch.diag(sim_matrix, -self.batch_size)
  81 
  82         positives = torch.cat([sim_ij, sim_ji], dim=0)
  83         nominator = torch.exp(positives / temperature_tensor)
  84         denominator = negatives_mask * torch.exp(sim_matrix / temperature_tensor)
  85 
  86         loss_partial = -torch.log(nominator / torch.sum(denominator, dim=1))
  87 
  88         return torch.sum(loss_partial) / (2 * self.batch_size)
  89
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/monai/losses/contrastive.py b/monai/losses/contrastive.py
  --- a/monai/losses/contrastive.py
  +++ b/monai/losses/contrastive.py
  @@ -65,14 +65,13 @@
           if target.shape != input.shape:
               raise ValueError(f"ground truth has differing shape ({target.shape}) from input ({input.shape})")
   
  -        temperature_tensor = torch.tensor(self.temperature).to(input.device)
  +        temperature_tensor = torch.as_tensor(self.temperature).to(input.device)
   
           norm_i = F.normalize(input, dim=1)
           norm_j = F.normalize(target, dim=1)
   
           negatives_mask = ~torch.eye(self.batch_size * 2, self.batch_size * 2, dtype=torch.bool)
  -        negatives_mask = torch.tensor(negatives_mask, dtype=torch.float)
  -        negatives_mask = torch.clone(torch.as_tensor(negatives_mask)).to(input.device)
  +        negatives_mask = torch.clone(negatives_mask.type(torch.float)).to(input.device)
   
           repr = torch.cat([norm_i, norm_j], dim=0)
           sim_matrix = F.cosine_similarity(repr.unsqueeze(1), repr.unsqueeze(0), dim=2)
  
verification_info: {"golden_diff": "diff --git a/monai/losses/contrastive.py b/monai/losses/contrastive.py\n--- a/monai/losses/contrastive.py\n+++ b/monai/losses/contrastive.py\n@@ -65,14 +65,13 @@\n         if target.shape != input.shape:\n             raise ValueError(f\"ground truth has differing shape ({target.shape}) from input ({input.shape})\")\n \n-        temperature_tensor = torch.tensor(self.temperature).to(input.device)\n+        temperature_tensor = torch.as_tensor(self.temperature).to(input.device)\n \n         norm_i = F.normalize(input, dim=1)\n         norm_j = F.normalize(target, dim=1)\n \n         negatives_mask = ~torch.eye(self.batch_size * 2, self.batch_size * 2, dtype=torch.bool)\n-        negatives_mask = torch.tensor(negatives_mask, dtype=torch.float)\n-        negatives_mask = torch.clone(torch.as_tensor(negatives_mask)).to(input.device)\n+        negatives_mask = torch.clone(negatives_mask.type(torch.float)).to(input.device)\n \n         repr = torch.cat([norm_i, norm_j], dim=0)\n         sim_matrix = F.cosine_similarity(repr.unsqueeze(1), repr.unsqueeze(0), dim=2)\n", "issue": "Enhance `ContrastiveLoss` to avoid warning\nCall ContrastiveLoss will see a warning message:\r\n```\r\nTo copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).\r\n```\r\n\r\nSimple code to reproduce this issue:\r\n```\r\nfrom monai.losses import ContrastiveLoss\r\nimport torch\r\n\r\ninp = torch.randn([2, 10])\r\ntarget = torch.randn([2, 10])\r\nloss = ContrastiveLoss(batch_size=2)\r\nloss(inp, target)\r\n```\n", "before_files": [{"content": "# Copyright (c) MONAI Consortium\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#     http://www.apache.org/licenses/LICENSE-2.0\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nimport torch\nfrom torch.nn import functional as F\nfrom torch.nn.modules.loss import _Loss\n\nfrom monai.utils import deprecated_arg\n\n\nclass ContrastiveLoss(_Loss):\n\n    \"\"\"\n    Compute the Contrastive loss defined in:\n\n        Chen, Ting, et al. \"A simple framework for contrastive learning of visual representations.\" International\n        conference on machine learning. PMLR, 2020. (http://proceedings.mlr.press/v119/chen20j.html)\n\n    Adapted from:\n        https://github.com/Sara-Ahmed/SiT/blob/1aacd6adcd39b71efc903d16b4e9095b97dda76f/losses.py#L5\n\n    \"\"\"\n\n    @deprecated_arg(name=\"reduction\", since=\"0.8\", msg_suffix=\"`reduction` is no longer supported.\")\n    def __init__(self, temperature: float = 0.5, batch_size: int = 1, reduction=\"sum\") -> None:\n        \"\"\"\n        Args:\n            temperature: Can be scaled between 0 and 1 for learning from negative samples, ideally set to 0.5.\n            batch_size: The number of samples.\n\n        Raises:\n            ValueError: When an input of dimension length > 2 is passed\n            ValueError: When input and target are of different shapes\n\n        .. deprecated:: 0.8.0\n\n            `reduction` is no longer supported.\n\n        \"\"\"\n        super().__init__()\n\n        self.batch_size = batch_size\n        self.temperature = temperature\n\n    def forward(self, input: torch.Tensor, target: torch.Tensor) -> torch.Tensor:\n        \"\"\"\n        Args:\n            input: the shape should be B[F].\n            target: the shape should be B[F].\n        \"\"\"\n        if len(target.shape) > 2 or len(input.shape) > 2:\n            raise ValueError(\n                f\"Either target or input has dimensions greater than 2 where target \"\n                f\"shape is ({target.shape}) and input shape is ({input.shape})\"\n            )\n\n        if target.shape != input.shape:\n            raise ValueError(f\"ground truth has differing shape ({target.shape}) from input ({input.shape})\")\n\n        temperature_tensor = torch.tensor(self.temperature).to(input.device)\n\n        norm_i = F.normalize(input, dim=1)\n        norm_j = F.normalize(target, dim=1)\n\n        negatives_mask = ~torch.eye(self.batch_size * 2, self.batch_size * 2, dtype=torch.bool)\n        negatives_mask = torch.tensor(negatives_mask, dtype=torch.float)\n        negatives_mask = torch.clone(torch.as_tensor(negatives_mask)).to(input.device)\n\n        repr = torch.cat([norm_i, norm_j], dim=0)\n        sim_matrix = F.cosine_similarity(repr.unsqueeze(1), repr.unsqueeze(0), dim=2)\n        sim_ij = torch.diag(sim_matrix, self.batch_size)\n        sim_ji = torch.diag(sim_matrix, -self.batch_size)\n\n        positives = torch.cat([sim_ij, sim_ji], dim=0)\n        nominator = torch.exp(positives / temperature_tensor)\n        denominator = negatives_mask * torch.exp(sim_matrix / temperature_tensor)\n\n        loss_partial = -torch.log(nominator / torch.sum(denominator, dim=1))\n\n        return torch.sum(loss_partial) / (2 * self.batch_size)\n", "path": "monai/losses/contrastive.py"}], "after_files": [{"content": "# Copyright (c) MONAI Consortium\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#     http://www.apache.org/licenses/LICENSE-2.0\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nimport torch\nfrom torch.nn import functional as F\nfrom torch.nn.modules.loss import _Loss\n\nfrom monai.utils import deprecated_arg\n\n\nclass ContrastiveLoss(_Loss):\n\n    \"\"\"\n    Compute the Contrastive loss defined in:\n\n        Chen, Ting, et al. \"A simple framework for contrastive learning of visual representations.\" International\n        conference on machine learning. PMLR, 2020. (http://proceedings.mlr.press/v119/chen20j.html)\n\n    Adapted from:\n        https://github.com/Sara-Ahmed/SiT/blob/1aacd6adcd39b71efc903d16b4e9095b97dda76f/losses.py#L5\n\n    \"\"\"\n\n    @deprecated_arg(name=\"reduction\", since=\"0.8\", msg_suffix=\"`reduction` is no longer supported.\")\n    def __init__(self, temperature: float = 0.5, batch_size: int = 1, reduction=\"sum\") -> None:\n        \"\"\"\n        Args:\n            temperature: Can be scaled between 0 and 1 for learning from negative samples, ideally set to 0.5.\n            batch_size: The number of samples.\n\n        Raises:\n            ValueError: When an input of dimension length > 2 is passed\n            ValueError: When input and target are of different shapes\n\n        .. deprecated:: 0.8.0\n\n            `reduction` is no longer supported.\n\n        \"\"\"\n        super().__init__()\n\n        self.batch_size = batch_size\n        self.temperature = temperature\n\n    def forward(self, input: torch.Tensor, target: torch.Tensor) -> torch.Tensor:\n        \"\"\"\n        Args:\n            input: the shape should be B[F].\n            target: the shape should be B[F].\n        \"\"\"\n        if len(target.shape) > 2 or len(input.shape) > 2:\n            raise ValueError(\n                f\"Either target or input has dimensions greater than 2 where target \"\n                f\"shape is ({target.shape}) and input shape is ({input.shape})\"\n            )\n\n        if target.shape != input.shape:\n            raise ValueError(f\"ground truth has differing shape ({target.shape}) from input ({input.shape})\")\n\n        temperature_tensor = torch.as_tensor(self.temperature).to(input.device)\n\n        norm_i = F.normalize(input, dim=1)\n        norm_j = F.normalize(target, dim=1)\n\n        negatives_mask = ~torch.eye(self.batch_size * 2, self.batch_size * 2, dtype=torch.bool)\n        negatives_mask = torch.clone(negatives_mask.type(torch.float)).to(input.device)\n\n        repr = torch.cat([norm_i, norm_j], dim=0)\n        sim_matrix = F.cosine_similarity(repr.unsqueeze(1), repr.unsqueeze(0), dim=2)\n        sim_ij = torch.diag(sim_matrix, self.batch_size)\n        sim_ji = torch.diag(sim_matrix, -self.batch_size)\n\n        positives = torch.cat([sim_ij, sim_ji], dim=0)\n        nominator = torch.exp(positives / temperature_tensor)\n        denominator = negatives_mask * torch.exp(sim_matrix / temperature_tensor)\n\n        loss_partial = -torch.log(nominator / torch.sum(denominator, dim=1))\n\n        return torch.sum(loss_partial) / (2 * self.batch_size)\n", "path": "monai/losses/contrastive.py"}]}