Skip to content

Commit

Permalink
【Hackathon 6th Fundable Projects 3 No.192】hinge_loss (#65160)
Browse files Browse the repository at this point in the history
* Fix

* Fix

* Fix

* Fix
  • Loading branch information
co63oc authored Jun 20, 2024
1 parent 62200e4 commit 21dd906
Show file tree
Hide file tree
Showing 13 changed files with 212 additions and 233 deletions.
164 changes: 0 additions & 164 deletions paddle/fluid/operators/hinge_loss_op.cc

This file was deleted.

69 changes: 0 additions & 69 deletions paddle/fluid/operators/hinge_loss_op.h

This file was deleted.

35 changes: 35 additions & 0 deletions paddle/phi/infermeta/binary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2197,6 +2197,41 @@ void GridSampleBaseInferMeta(const MetaTensor& x,
out->share_lod(x);
}

void HingeLossInferMeta(const MetaTensor& logits,
const MetaTensor& labels,
MetaTensor* loss) {
const auto& pred_dims = logits.dims();
const auto& label_dims = labels.dims();

PADDLE_ENFORCE_EQ(
pred_dims,
label_dims,
phi::errors::InvalidArgument(
"The Input(input) and Input(label) should have the same "
"shape, but received input shape [%s] != label shape [%s]",
pred_dims,
label_dims));

PADDLE_ENFORCE_EQ(
pred_dims.size(),
2,
phi::errors::InvalidArgument("Input(input) rank should be 2, "
"but received input rank(%d) != 2",
pred_dims.size()));

PADDLE_ENFORCE_EQ(pred_dims[1],
1,
phi::errors::InvalidArgument(
"The second dimension of Input(input) should be 1, "
"as each row of input contains a real value, "
"but received second dimension of input (%d) != 1",
pred_dims[1]));

loss->set_dims({pred_dims[0], 1});
loss->share_lod(logits);
loss->set_dtype(logits.dtype());
}

void HuberLossInferMeta(const MetaTensor& input,
const MetaTensor& label,
float delta,
Expand Down
4 changes: 4 additions & 0 deletions paddle/phi/infermeta/binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,10 @@ void GridSampleBaseInferMeta(const MetaTensor& x,
MetaTensor* out,
MetaConfig config = MetaConfig());

void HingeLossInferMeta(const MetaTensor& logits,
const MetaTensor& labels,
MetaTensor* loss);

void HuberLossInferMeta(const MetaTensor& input_meta,
const MetaTensor& label_meta,
float delta,
Expand Down
19 changes: 19 additions & 0 deletions paddle/phi/kernels/cpu/hinge_loss_grad_kernel.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) 2024 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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/impl/hinge_loss_kernel_impl.h"

PD_REGISTER_KERNEL(
hinge_loss_grad, CPU, ALL_LAYOUT, phi::HingeLossGradKernel, float) {}
18 changes: 18 additions & 0 deletions paddle/phi/kernels/cpu/hinge_loss_kernel.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) 2024 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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/impl/hinge_loss_kernel_impl.h"

PD_REGISTER_KERNEL(hinge_loss, CPU, ALL_LAYOUT, phi::HingeLossKernel, float) {}
19 changes: 19 additions & 0 deletions paddle/phi/kernels/gpu/hinge_loss_grad_kernel.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) 2024 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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/impl/hinge_loss_kernel_impl.h"

PD_REGISTER_KERNEL(
hinge_loss_grad, GPU, ALL_LAYOUT, phi::HingeLossGradKernel, float) {}
18 changes: 18 additions & 0 deletions paddle/phi/kernels/gpu/hinge_loss_kernel.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) 2024 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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/impl/hinge_loss_kernel_impl.h"

PD_REGISTER_KERNEL(hinge_loss, GPU, ALL_LAYOUT, phi::HingeLossKernel, float) {}
Loading

0 comments on commit 21dd906

Please sign in to comment.