|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @author DeepLink |
| 4 | + * @copyright (c) 2024, DeepLink. |
| 5 | + */ |
| 6 | + |
| 7 | +#include "../aclnn/acl_scalar.hpp" |
| 8 | +#include "../aclnn/adaptor.hpp" |
| 9 | + |
| 10 | +namespace impl { |
| 11 | +namespace ascend { |
| 12 | +diopiError_t diopiNLLLossV2(diopiContextHandle_t ctx, diopiTensorHandle_t out, diopiTensorHandle_t totalWeight, diopiConstTensorHandle_t input, |
| 13 | + diopiConstTensorHandle_t target, diopiConstTensorHandle_t weight, diopiReduction_t reduction, int64_t ignoreIndex) { |
| 14 | + if (input == nullptr) { |
| 15 | + return diopiSuccess; |
| 16 | + } |
| 17 | + |
| 18 | + AscendTensor inputAt(input); |
| 19 | + if (inputAt.numel() <= 0) { |
| 20 | + if (diopiReduction_t::ReductionMean == reduction) { |
| 21 | + DIOPI_ASCEND_CALL_ACLNN(aclnnInpalceFillScalar, ctx, out, std::nanf("")); |
| 22 | + } else if (diopiReduction_t::ReductionSum == reduction || diopiReduction_t::ReductionNone == reduction) { |
| 23 | + DIOPI_ASCEND_CALL_ACLNN(aclnnInpalceZero, ctx, out); |
| 24 | + } |
| 25 | + return diopiSuccess; |
| 26 | + } |
| 27 | + |
| 28 | + diopiTensorHandle_t weightTmp = const_cast<diopiTensorHandle_t>(weight); |
| 29 | + if (weightTmp == nullptr) { |
| 30 | + const int64_t channel = inputAt.dim() >= 4 ? inputAt.shape(1) : inputAt.shape(-1); |
| 31 | + std::vector<int64_t> weightSize{channel}; |
| 32 | + diopiSize_t weightShape = vectorToDiopiSize(weightSize); |
| 33 | + diopiRequireTensor(ctx, &weightTmp, &weightShape, nullptr, inputAt.dtype(), diopi_device); |
| 34 | + DIOPI_ASCEND_CALL_ACLNN(aclnnInplaceOne, ctx, weightTmp); |
| 35 | + } |
| 36 | + |
| 37 | + if (inputAt.dim() <= 2) { |
| 38 | + DIOPI_ASCEND_CALL_ACLNN(aclnnNLLLoss, ctx, input, target, weightTmp, reduction, ignoreIndex, out, totalWeight); |
| 39 | + } else if (inputAt.dim() == 4) { |
| 40 | + DIOPI_ASCEND_CALL_ACLNN(aclnnNLLLoss2d, ctx, input, target, weightTmp, reduction, ignoreIndex, out, totalWeight); |
| 41 | + } else { |
| 42 | + AscendTensor outAt(out); |
| 43 | + AscendTensor targetAt(target); |
| 44 | + AscendTensor inputView = inputAt.view({inputAt.shape(0), inputAt.shape(1), inputAt.numel() / inputAt.shape(0) / inputAt.shape(1), 1}); |
| 45 | + AscendTensor outView = (outAt.numel() > 1) ? outAt.view({outAt.shape(0), outAt.numel() / outAt.shape(0), 1}) : outAt; |
| 46 | + AscendTensor targetView = targetAt.view({targetAt.shape(0), targetAt.numel() / targetAt.shape(0), 1}); |
| 47 | + } |
| 48 | + |
| 49 | + return diopiSuccess; |
| 50 | +} |
| 51 | + |
| 52 | +diopiError_t diopiNLLLossV2Backward(diopiContextHandle_t ctx, diopiTensorHandle_t gradInput, diopiConstTensorHandle_t gradOutput, |
| 53 | + diopiConstTensorHandle_t input, diopiConstTensorHandle_t target, diopiConstTensorHandle_t weight, |
| 54 | + diopiConstTensorHandle_t totalWeight, diopiReduction_t reduction, int64_t ignoreIndex) { |
| 55 | + AscendTensor inputAt(input); |
| 56 | + AscendTensor gradInputAt(gradInput); |
| 57 | + if (input == nullptr || gradInput == nullptr || inputAt.numel() <= 0 || gradInputAt.numel() <= 0) { |
| 58 | + return diopiSuccess; |
| 59 | + } |
| 60 | + /* |
| 61 | + * A tensor representing the sum of weights for each element considered in the NLL loss computation. |
| 62 | + * In case a weight tensor is provided, total_weight represents the sum of weights for all the non-ignored indices in the target tensor. |
| 63 | + * When no weight tensor is provided, total_weight corresponds to the count of all non-ignored indices. |
| 64 | + */ |
| 65 | + diopiTensorHandle_t weightTmp = const_cast<diopiTensorHandle_t>(weight); |
| 66 | + if (weightTmp == nullptr) { |
| 67 | + const int64_t channel = inputAt.dim() >= 4 ? inputAt.shape(1) : inputAt.shape(-1); |
| 68 | + std::vector<int64_t> weightSize{channel}; |
| 69 | + diopiSize_t weightShape = vectorToDiopiSize(weightSize); |
| 70 | + diopiRequireTensor(ctx, &weightTmp, &weightShape, nullptr, inputAt.dtype(), diopi_device); |
| 71 | + DIOPI_ASCEND_CALL_ACLNN(aclnnInplaceOne, ctx, weightTmp); |
| 72 | + } |
| 73 | + |
| 74 | + if (inputAt.dim() <= 2) { |
| 75 | + DIOPI_ASCEND_CALL_ACLNN(aclnnNLLLossBackward, ctx, gradOutput, input, target, weightTmp, reduction, ignoreIndex, totalWeight, gradInput); |
| 76 | + } else if (inputAt.dim() == 4) { |
| 77 | + DIOPI_ASCEND_CALL_ACLNN(aclnnNLLLoss2dBackward, ctx, gradOutput, input, target, weightTmp, reduction, ignoreIndex, totalWeight, gradInput); |
| 78 | + } else { |
| 79 | + AscendTensor gradIputAt(gradInput); |
| 80 | + AscendTensor gradOutputAt(gradOutput); |
| 81 | + AscendTensor targetAt(target); |
| 82 | + |
| 83 | + AscendTensor inputView = inputAt.view({inputAt.shape(0), inputAt.shape(1), inputAt.numel() / inputAt.shape(0) / inputAt.shape(1), 1}); |
| 84 | + AscendTensor gradInputView = |
| 85 | + gradInputAt.view({gradInputAt.shape(0), gradInputAt.shape(1), gradInputAt.numel() / gradInputAt.shape(0) / gradInputAt.shape(1), 1}); |
| 86 | + AscendTensor gradOutputView; |
| 87 | + if (gradOutputAt.numel() > 1) { |
| 88 | + gradOutputView.view({gradOutputAt.shape(0), gradOutputAt.numel() / gradOutputAt.shape(0), 1}); |
| 89 | + } else { |
| 90 | + gradOutputView = gradOutputAt; |
| 91 | + } |
| 92 | + AscendTensor targetView = targetAt.view({targetAt.shape(0), targetAt.numel() / targetAt.shape(0), 1}); |
| 93 | + DIOPI_ASCEND_CALL_ACLNN( |
| 94 | + aclnnNLLLoss2dBackward, ctx, gradOutputView, inputView, targetView, weightTmp, reduction, ignoreIndex, totalWeight, gradInputView); |
| 95 | + } |
| 96 | + return diopiSuccess; |
| 97 | +} |
| 98 | + |
| 99 | +} // namespace ascend |
| 100 | +} // namespace impl |
0 commit comments