Skip to content

Commit f081811

Browse files
authored
Fix (#65256)
1 parent 62bbf41 commit f081811

File tree

3 files changed

+93
-75
lines changed

3 files changed

+93
-75
lines changed

paddle/fluid/operators/log_loss_op_xpu.cc

Lines changed: 0 additions & 75 deletions
This file was deleted.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <memory>
16+
17+
#include "paddle/phi/backends/xpu/enforce_xpu.h"
18+
#include "paddle/phi/core/kernel_registry.h"
19+
namespace phi {
20+
21+
template <typename T, typename Context>
22+
void LogLossGradXPUKernel(const Context& dev_ctx,
23+
const DenseTensor& input,
24+
const DenseTensor& label,
25+
const DenseTensor& out_grad,
26+
float epsilon_in,
27+
DenseTensor* in_grad) {
28+
auto* predict = &input;
29+
auto* labels = &label;
30+
auto* dloss = &out_grad;
31+
auto* dpred = in_grad;
32+
if (dpred == nullptr) {
33+
return;
34+
}
35+
auto epsilon = static_cast<T>(epsilon_in);
36+
dev_ctx.template Alloc<T>(dpred);
37+
int n = predict->numel();
38+
int r = xpu::log_loss_grad(dev_ctx.x_context(),
39+
predict->data<T>(),
40+
labels->data<T>(),
41+
dloss->data<T>(),
42+
dpred->data<T>(),
43+
n,
44+
epsilon);
45+
PADDLE_ENFORCE_XDNN_SUCCESS(r, "log_loss_grad");
46+
}
47+
} // namespace phi
48+
49+
PD_REGISTER_KERNEL(
50+
log_loss_grad, XPU, ALL_LAYOUT, phi::LogLossGradXPUKernel, float) {}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <memory>
16+
17+
#include "paddle/phi/backends/xpu/enforce_xpu.h"
18+
#include "paddle/phi/core/kernel_registry.h"
19+
namespace phi {
20+
21+
template <typename T, typename Context>
22+
void LogLossXPUKernel(const Context& dev_ctx,
23+
const DenseTensor& input,
24+
const DenseTensor& label,
25+
float epsilon_in,
26+
DenseTensor* out) {
27+
auto* predict = &input;
28+
auto* labels = &label;
29+
auto* loss = out;
30+
auto epsilon = static_cast<T>(epsilon_in);
31+
dev_ctx.template Alloc<T>(loss);
32+
int n = predict->numel();
33+
int r = xpu::log_loss(dev_ctx.x_context(),
34+
predict->data<T>(),
35+
labels->data<T>(),
36+
loss->data<T>(),
37+
n,
38+
epsilon);
39+
PADDLE_ENFORCE_XDNN_SUCCESS(r, "log_loss");
40+
}
41+
} // namespace phi
42+
43+
PD_REGISTER_KERNEL(log_loss, XPU, ALL_LAYOUT, phi::LogLossXPUKernel, float) {}

0 commit comments

Comments
 (0)