We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
[Bug] Dice loss bug
环境：aistudio tesla v100
paddlepaddle=2.3.0
paddleseg=2.5.0

bug：dice loss 前向传播时，label转换为one-hot编码前，并未对ignore_index进行处理，当ignore_index值大于num_classes（比如ignore index为255，类别为19）时，报错cuda 719。原因为one-hot转换错误。 
代码链接：https://github.com/PaddlePaddle/PaddleSeg/blob/35a4c4d229df2d4a5ca724ad442bf5e0f75b4823/paddleseg/models/losses/dice_loss.py#L46

可将mask部分放到one-hot之前，然后将ignore_index赋值一个小于num_classes的值：
```python
 def forward(self, logits, labels):
        num_class = logits.shape[1]
        if self.weight is not None:
            assert num_class == len(self.weight), \
                "The lenght of weight should be euqal to the num class"
        if logits.shape != labels.shape:
            labels = labels.unsqueeze(axis=1)
            labels = F.interpolate(labels, size=logits.shape[2:], mode='nearest')
            labels = labels.squeeze(axis=1)
        logits = F.softmax(logits, axis=1)
        mask = labels != self.ignore_index
        mask = paddle.cast(paddle.unsqueeze(mask, 1), 'float32')
        labels[labels == self.ignore_index] = 0
        labels_one_hot = F.one_hot(labels, num_class)
        labels_one_hot = paddle.transpose(labels_one_hot, [0, 3, 1, 2])
        
        dice_loss = 0.0
        for i in range(num_class):
            dice_loss_i = dice_loss_helper(logits[:, i], labels_one_hot[:, i],
                                           mask, self.smooth, self.eps)
            if self.weight is not None:
                dice_loss_i *= self.weight[i]
            dice_loss += dice_loss_i
        dice_loss = dice_loss / num_class

        return dice_loss
```


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `paddleseg/models/losses/dice_loss.py`
Content:
```
1 # you may not use this file except in compliance with the License.
2 # You may obtain a copy of the License at
3 #
4 #    http://www.apache.org/licenses/LICENSE-2.0
5 #
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 paddle
13 from paddle import nn
14 import paddle.nn.functional as F
15 
16 from paddleseg.cvlibs import manager
17 
18 
19 @manager.LOSSES.add_component
20 class DiceLoss(nn.Layer):
21     """
22     The implements of the dice loss.
23 
24     Args:
25         weight (list[float], optional): The weight for each class. Default: None.
26         ignore_index (int64): ignore_index (int64, optional): Specifies a target value that
27             is ignored and does not contribute to the input gradient. Default ``255``.
28         smooth (float32): Laplace smoothing to smooth dice loss and accelerate convergence.
29             Default: 1.0
30     """
31 
32     def __init__(self, weight=None, ignore_index=255, smooth=1.0):
33         super().__init__()
34         self.weight = weight
35         self.ignore_index = ignore_index
36         self.smooth = smooth
37         self.eps = 1e-8
38 
39     def forward(self, logits, labels):
40         num_class = logits.shape[1]
41         if self.weight is not None:
42             assert num_class == len(self.weight), \
43                 "The lenght of weight should be euqal to the num class"
44 
45         logits = F.softmax(logits, axis=1)
46         labels_one_hot = F.one_hot(labels, num_class)
47         labels_one_hot = paddle.transpose(labels_one_hot, [0, 3, 1, 2])
48 
49         mask = labels != self.ignore_index
50         mask = paddle.cast(paddle.unsqueeze(mask, 1), 'float32')
51 
52         dice_loss = 0.0
53         for i in range(num_class):
54             dice_loss_i = dice_loss_helper(logits[:, i], labels_one_hot[:, i],
55                                            mask, self.smooth, self.eps)
56             if self.weight is not None:
57                 dice_loss_i *= self.weight[i]
58             dice_loss += dice_loss_i
59         dice_loss = dice_loss / num_class
60 
61         return dice_loss
62 
63 
64 def dice_loss_helper(logit, label, mask, smooth, eps):
65     assert logit.shape == label.shape, \
66         "The shape of logit and label should be the same"
67     logit = paddle.reshape(logit, [0, -1])
68     label = paddle.reshape(label, [0, -1])
69     mask = paddle.reshape(mask, [0, -1])
70     logit *= mask
71     label *= mask
72     intersection = paddle.sum(logit * label, axis=1)
73     cardinality = paddle.sum(logit + label, axis=1)
74     dice_loss = 1 - (2 * intersection + smooth) / (cardinality + smooth + eps)
75     dice_loss = dice_loss.mean()
76     return dice_loss
77
```
--- 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()

```

diff --git a/paddleseg/models/losses/dice_loss.py b/paddleseg/models/losses/dice_loss.py
--- a/paddleseg/models/losses/dice_loss.py
+++ b/paddleseg/models/losses/dice_loss.py
@@ -42,13 +42,14 @@
             assert num_class == len(self.weight), \
                 "The lenght of weight should be euqal to the num class"
 
-        logits = F.softmax(logits, axis=1)
-        labels_one_hot = F.one_hot(labels, num_class)
-        labels_one_hot = paddle.transpose(labels_one_hot, [0, 3, 1, 2])
-
         mask = labels != self.ignore_index
         mask = paddle.cast(paddle.unsqueeze(mask, 1), 'float32')
 
+        labels[labels == self.ignore_index] = 0
+        labels_one_hot = F.one_hot(labels, num_class)
+        labels_one_hot = paddle.transpose(labels_one_hot, [0, 3, 1, 2])
+        logits = F.softmax(logits, axis=1)
+
         dice_loss = 0.0
         for i in range(num_class):
             dice_loss_i = dice_loss_helper(logits[:, i], labels_one_hot[:, i],

{"golden_diff": "diff --git a/paddleseg/models/losses/dice_loss.py b/paddleseg/models/losses/dice_loss.py\n--- a/paddleseg/models/losses/dice_loss.py\n+++ b/paddleseg/models/losses/dice_loss.py\n@@ -42,13 +42,14 @@\n             assert num_class == len(self.weight), \\\n                 \"The lenght of weight should be euqal to the num class\"\n \n-        logits = F.softmax(logits, axis=1)\n-        labels_one_hot = F.one_hot(labels, num_class)\n-        labels_one_hot = paddle.transpose(labels_one_hot, [0, 3, 1, 2])\n-\n         mask = labels != self.ignore_index\n         mask = paddle.cast(paddle.unsqueeze(mask, 1), 'float32')\n \n+        labels[labels == self.ignore_index] = 0\n+        labels_one_hot = F.one_hot(labels, num_class)\n+        labels_one_hot = paddle.transpose(labels_one_hot, [0, 3, 1, 2])\n+        logits = F.softmax(logits, axis=1)\n+\n         dice_loss = 0.0\n         for i in range(num_class):\n             dice_loss_i = dice_loss_helper(logits[:, i], labels_one_hot[:, i],\n", "issue": "[Bug] Dice loss bug\n\u73af\u5883\uff1aaistudio tesla v100\r\npaddlepaddle=2.3.0\r\npaddleseg=2.5.0\r\n\r\nbug\uff1adice loss \u524d\u5411\u4f20\u64ad\u65f6\uff0clabel\u8f6c\u6362\u4e3aone-hot\u7f16\u7801\u524d\uff0c\u5e76\u672a\u5bf9ignore_index\u8fdb\u884c\u5904\u7406\uff0c\u5f53ignore_index\u503c\u5927\u4e8enum_classes\uff08\u6bd4\u5982ignore index\u4e3a255\uff0c\u7c7b\u522b\u4e3a19\uff09\u65f6\uff0c\u62a5\u9519cuda 719\u3002\u539f\u56e0\u4e3aone-hot\u8f6c\u6362\u9519\u8bef\u3002 \r\n\u4ee3\u7801\u94fe\u63a5\uff1ahttps://github.com/PaddlePaddle/PaddleSeg/blob/35a4c4d229df2d4a5ca724ad442bf5e0f75b4823/paddleseg/models/losses/dice_loss.py#L46\r\n\r\n\u53ef\u5c06mask\u90e8\u5206\u653e\u5230one-hot\u4e4b\u524d\uff0c\u7136\u540e\u5c06ignore_index\u8d4b\u503c\u4e00\u4e2a\u5c0f\u4e8enum_classes\u7684\u503c\uff1a\r\n```python\r\n def forward(self, logits, labels):\r\n        num_class = logits.shape[1]\r\n        if self.weight is not None:\r\n            assert num_class == len(self.weight), \\\r\n                \"The lenght of weight should be euqal to the num class\"\r\n        if logits.shape != labels.shape:\r\n            labels = labels.unsqueeze(axis=1)\r\n            labels = F.interpolate(labels, size=logits.shape[2:], mode='nearest')\r\n            labels = labels.squeeze(axis=1)\r\n        logits = F.softmax(logits, axis=1)\r\n        mask = labels != self.ignore_index\r\n        mask = paddle.cast(paddle.unsqueeze(mask, 1), 'float32')\r\n        labels[labels == self.ignore_index] = 0\r\n        labels_one_hot = F.one_hot(labels, num_class)\r\n        labels_one_hot = paddle.transpose(labels_one_hot, [0, 3, 1, 2])\r\n        \r\n        dice_loss = 0.0\r\n        for i in range(num_class):\r\n            dice_loss_i = dice_loss_helper(logits[:, i], labels_one_hot[:, i],\r\n                                           mask, self.smooth, self.eps)\r\n            if self.weight is not None:\r\n                dice_loss_i *= self.weight[i]\r\n            dice_loss += dice_loss_i\r\n        dice_loss = dice_loss / num_class\r\n\r\n        return dice_loss\r\n```\r\n\n", "before_files": [{"content": "# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#    http://www.apache.org/licenses/LICENSE-2.0\n#\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 paddle\nfrom paddle import nn\nimport paddle.nn.functional as F\n\nfrom paddleseg.cvlibs import manager\n\n\n@manager.LOSSES.add_component\nclass DiceLoss(nn.Layer):\n    \"\"\"\n    The implements of the dice loss.\n\n    Args:\n        weight (list[float], optional): The weight for each class. Default: None.\n        ignore_index (int64): ignore_index (int64, optional): Specifies a target value that\n            is ignored and does not contribute to the input gradient. Default ``255``.\n        smooth (float32): Laplace smoothing to smooth dice loss and accelerate convergence.\n            Default: 1.0\n    \"\"\"\n\n    def __init__(self, weight=None, ignore_index=255, smooth=1.0):\n        super().__init__()\n        self.weight = weight\n        self.ignore_index = ignore_index\n        self.smooth = smooth\n        self.eps = 1e-8\n\n    def forward(self, logits, labels):\n        num_class = logits.shape[1]\n        if self.weight is not None:\n            assert num_class == len(self.weight), \\\n                \"The lenght of weight should be euqal to the num class\"\n\n        logits = F.softmax(logits, axis=1)\n        labels_one_hot = F.one_hot(labels, num_class)\n        labels_one_hot = paddle.transpose(labels_one_hot, [0, 3, 1, 2])\n\n        mask = labels != self.ignore_index\n        mask = paddle.cast(paddle.unsqueeze(mask, 1), 'float32')\n\n        dice_loss = 0.0\n        for i in range(num_class):\n            dice_loss_i = dice_loss_helper(logits[:, i], labels_one_hot[:, i],\n                                           mask, self.smooth, self.eps)\n            if self.weight is not None:\n                dice_loss_i *= self.weight[i]\n            dice_loss += dice_loss_i\n        dice_loss = dice_loss / num_class\n\n        return dice_loss\n\n\ndef dice_loss_helper(logit, label, mask, smooth, eps):\n    assert logit.shape == label.shape, \\\n        \"The shape of logit and label should be the same\"\n    logit = paddle.reshape(logit, [0, -1])\n    label = paddle.reshape(label, [0, -1])\n    mask = paddle.reshape(mask, [0, -1])\n    logit *= mask\n    label *= mask\n    intersection = paddle.sum(logit * label, axis=1)\n    cardinality = paddle.sum(logit + label, axis=1)\n    dice_loss = 1 - (2 * intersection + smooth) / (cardinality + smooth + eps)\n    dice_loss = dice_loss.mean()\n    return dice_loss\n", "path": "paddleseg/models/losses/dice_loss.py"}], "after_files": [{"content": "# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#    http://www.apache.org/licenses/LICENSE-2.0\n#\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 paddle\nfrom paddle import nn\nimport paddle.nn.functional as F\n\nfrom paddleseg.cvlibs import manager\n\n\n@manager.LOSSES.add_component\nclass DiceLoss(nn.Layer):\n    \"\"\"\n    The implements of the dice loss.\n\n    Args:\n        weight (list[float], optional): The weight for each class. Default: None.\n        ignore_index (int64): ignore_index (int64, optional): Specifies a target value that\n            is ignored and does not contribute to the input gradient. Default ``255``.\n        smooth (float32): Laplace smoothing to smooth dice loss and accelerate convergence.\n            Default: 1.0\n    \"\"\"\n\n    def __init__(self, weight=None, ignore_index=255, smooth=1.0):\n        super().__init__()\n        self.weight = weight\n        self.ignore_index = ignore_index\n        self.smooth = smooth\n        self.eps = 1e-8\n\n    def forward(self, logits, labels):\n        num_class = logits.shape[1]\n        if self.weight is not None:\n            assert num_class == len(self.weight), \\\n                \"The lenght of weight should be euqal to the num class\"\n\n        mask = labels != self.ignore_index\n        mask = paddle.cast(paddle.unsqueeze(mask, 1), 'float32')\n\n        labels[labels == self.ignore_index] = 0\n        labels_one_hot = F.one_hot(labels, num_class)\n        labels_one_hot = paddle.transpose(labels_one_hot, [0, 3, 1, 2])\n        logits = F.softmax(logits, axis=1)\n\n        dice_loss = 0.0\n        for i in range(num_class):\n            dice_loss_i = dice_loss_helper(logits[:, i], labels_one_hot[:, i],\n                                           mask, self.smooth, self.eps)\n            if self.weight is not None:\n                dice_loss_i *= self.weight[i]\n            dice_loss += dice_loss_i\n        dice_loss = dice_loss / num_class\n\n        return dice_loss\n\n\ndef dice_loss_helper(logit, label, mask, smooth, eps):\n    assert logit.shape == label.shape, \\\n        \"The shape of logit and label should be the same\"\n    logit = paddle.reshape(logit, [0, -1])\n    label = paddle.reshape(label, [0, -1])\n    mask = paddle.reshape(mask, [0, -1])\n    logit *= mask\n    label *= mask\n    intersection = paddle.sum(logit * label, axis=1)\n    cardinality = paddle.sum(logit + label, axis=1)\n    dice_loss = 1 - (2 * intersection + smooth) / (cardinality + smooth + eps)\n    dice_loss = dice_loss.mean()\n    return dice_loss\n", "path": "paddleseg/models/losses/dice_loss.py"}]}