Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MLU] Pick#3764 #3785

Open
wants to merge 2 commits into
base: release/2.8.1
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions paddleseg/models/losses/ohem_cross_entropy_loss.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
Expand All @@ -19,6 +17,7 @@
from paddleseg.cvlibs import manager

_IS_NPU = "npu" in paddle.get_device()
_IS_MLU = "mlu" in paddle.get_device()


@manager.LOSSES.add_component
Expand Down Expand Up @@ -100,6 +99,20 @@ def forward(self, logit, label):
ignore_index=self.ignore_index,
reduction='none')
loss = loss.unsqueeze(1)
elif _IS_MLU:
# mlu kernel does not deal with th parameter ignore_index
# so here manual solve it
logit_trans = logit.transpose((0, 2, 3, 1)).flatten(0, 2)
mask = label != self.ignore_index
label = paddle.where(mask, label, paddle.zeros_like(label))
label_trans = label.transpose((0, 2, 3, 1)).flatten(0, 2)
loss = F.cross_entropy(logit_trans,
label_trans,
weight=self.weight,
reduction='none',
axis=-1)
loss = loss.reshape([n, h, w, 1]).transpose([0, 3, 1, 2])
loss = loss * mask.astype("float32")
else:
loss = F.cross_entropy(logit,
label,
Expand Down