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

[NPU] sigmoid speed up. #1512

Merged
merged 1 commit into from
Jan 16, 2025
Merged
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
12 changes: 8 additions & 4 deletions backends/npu/kernels/sigmoid_cross_entropy_with_logits_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ void SigmoidCrossEntropyWithLogitsKernel(
phi::DenseTensor pos_weight_tensor;
phi::DenseTensorMeta weight_tensor_meta = {phi::DataType::FLOAT32, x.dims()};
weight_tensor.set_meta(weight_tensor_meta);
FillNpuTensorWithConstant<float>(&weight_tensor, dev_ctx, 1.0);
dev_ctx.template Alloc<float>(&weight_tensor);
EXEC_NPU_CMD(aclnnInplaceOne, dev_ctx, weight_tensor);
weight_tensor.Resize(x.dims());

if (pos_weight.get_ptr() == nullptr) {
pos_weight_tensor.set_meta(weight_tensor_meta);
FillNpuTensorWithConstant<float>(&pos_weight_tensor, dev_ctx, 1.0);
dev_ctx.template Alloc<float>(&pos_weight_tensor);
EXEC_NPU_CMD(aclnnInplaceOne, dev_ctx, pos_weight_tensor);
pos_weight_tensor.Resize(x.dims());
} else {
pos_weight_tensor = *pos_weight.get_ptr();
Expand Down Expand Up @@ -89,12 +91,14 @@ void SigmoidCrossEntropyWithLogitsGradKernel(
phi::DenseTensor pos_weight_tensor;
phi::DenseTensorMeta weight_tensor_meta = {phi::DataType::FLOAT32, x.dims()};
weight_tensor.set_meta(weight_tensor_meta);
FillNpuTensorWithConstant<float>(&weight_tensor, dev_ctx, 1.0);
dev_ctx.template Alloc<float>(&weight_tensor);
EXEC_NPU_CMD(aclnnInplaceOne, dev_ctx, weight_tensor);
weight_tensor.Resize(x.dims());

if (pos_weight.get_ptr() == nullptr) {
pos_weight_tensor.set_meta(weight_tensor_meta);
FillNpuTensorWithConstant<float>(&pos_weight_tensor, dev_ctx, 1.0);
dev_ctx.template Alloc<float>(&pos_weight_tensor);
EXEC_NPU_CMD(aclnnInplaceOne, dev_ctx, pos_weight_tensor);
pos_weight_tensor.Resize(x.dims());
} else {
pos_weight_tensor = *pos_weight.get_ptr();
Expand Down