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

【Hackathon4】No5 nextafter #52544

Merged
merged 33 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
ce32044
test
enkilee Apr 4, 2023
f260eb5
fix
enkilee Apr 4, 2023
96f562f
add unittest
enkilee Apr 4, 2023
f277329
Merge branch 'PaddlePaddle:develop' into hackathon4_no5_nextafter
enkilee Apr 4, 2023
798cc11
add gpu file
enkilee Apr 5, 2023
f45c1ba
Merge branch 'hackathon4_no5_nextafter' of /~https://github.com/enkilee…
enkilee Apr 5, 2023
3c4f1f5
Merge branch 'PaddlePaddle:develop' into hackathon4_no5_nextafter
enkilee Apr 5, 2023
bb6a646
Merge branch 'PaddlePaddle:develop' into hackathon4_no5_nextafter
enkilee Apr 5, 2023
4e77142
fix
enkilee Apr 5, 2023
aee37a2
Merge branch 'hackathon4_no5_nextafter' of /~https://github.com/enkilee…
enkilee Apr 5, 2023
28781c9
fix
enkilee Apr 5, 2023
3bddb9b
Merge branch 'PaddlePaddle:develop' into hackathon4_no5_nextafter
enkilee Apr 5, 2023
6b88bfc
fix gpu
enkilee Apr 6, 2023
9d4b923
add Optest
enkilee Apr 18, 2023
6ad9198
add fp32 optest
enkilee Apr 18, 2023
233fa1d
fix
enkilee Apr 21, 2023
7cbffb0
fix
enkilee Apr 21, 2023
4dbb5fc
fix
enkilee Apr 21, 2023
5e736ca
fix
enkilee Apr 21, 2023
a0ef1da
fix
enkilee Apr 21, 2023
88ec297
fix
enkilee Apr 21, 2023
38cf25c
re
enkilee Apr 21, 2023
20b587f
fix
enkilee Apr 22, 2023
de13f4b
fix
enkilee Apr 22, 2023
d0068ef
fix
enkilee Apr 22, 2023
e8db15f
fix
enkilee Apr 22, 2023
ef9aac7
fix
enkilee Apr 25, 2023
458071a
fix
enkilee Apr 25, 2023
4e433d2
add test
enkilee Apr 25, 2023
fe308ea
fix
enkilee Apr 25, 2023
9cd6e14
fix
enkilee Apr 25, 2023
6cdfa21
fix
enkilee Apr 25, 2023
03478c3
fix
enkilee Apr 25, 2023
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
6 changes: 6 additions & 0 deletions paddle/phi/api/yaml/op_compat.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1393,6 +1393,12 @@
extra :
attrs : [bool use_mkldnn = false]

- op : nextafter
inputs :
{x: X, y : Y}
outputs :
out: Out

enkilee marked this conversation as resolved.
Show resolved Hide resolved
- op : nll_loss
backward : nll_loss_grad
inputs :
Expand Down
8 changes: 8 additions & 0 deletions paddle/phi/api/yaml/ops.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,14 @@
data_transform :
skip_transform : out_size, size_tensor, scale_tensor

- op : nextafter
args : (Tensor x, Tensor y)
output : Tensor(out)
infer_meta :
func : NextafterInferMeta
enkilee marked this conversation as resolved.
Show resolved Hide resolved
kernel :
func : nextafter

- op : nll_loss
args : (Tensor input, Tensor label, Tensor weight, int64_t ignore_index = -100, str reduction = "mean")
output : Tensor(out), Tensor(total_weight)
Expand Down
9 changes: 9 additions & 0 deletions paddle/phi/infermeta/binary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2282,6 +2282,15 @@ void MvInferMeta(const MetaTensor& x, const MetaTensor& vec, MetaTensor* out) {
out->share_lod(x);
}

void NextafterInferMeta(const MetaTensor& x,
enkilee marked this conversation as resolved.
Show resolved Hide resolved
const MetaTensor& y,
MetaTensor* out) {
auto x_dims = x.dims();
out->set_dims(x_dims);
out->set_dtype(x.dtype());
out->share_lod(x);
}

void PReluInferMeta(const MetaTensor& x,
const MetaTensor& alpha,
const std::string& data_format,
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 @@ -366,6 +366,10 @@ void MatrixRankTolInferMeta(const MetaTensor& x,

void MvInferMeta(const MetaTensor& x, const MetaTensor& vec, MetaTensor* out);

void NextafterInferMeta(const MetaTensor& x,
const MetaTensor& y,
MetaTensor* out);
enkilee marked this conversation as resolved.
Show resolved Hide resolved

void PReluInferMeta(const MetaTensor& x,
const MetaTensor& alpha,
const std::string& data_format,
Expand Down
22 changes: 22 additions & 0 deletions paddle/phi/kernels/cpu/nextafter_kernel.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) 2022 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/kernels/nextafter_kernel.h"

#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/impl/nextafter_kernel_impl.h"

PD_REGISTER_KERNEL(
nextafter, CPU, ALL_LAYOUT, phi::NextafterKernel, float, double) {}
22 changes: 22 additions & 0 deletions paddle/phi/kernels/gpu/nextafter_kernel.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) 2021 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/kernels/nextafter_kernel.h"

#include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/impl/nextafter_kernel_impl.h"

PD_REGISTER_KERNEL(
nextafter, GPU, ALL_LAYOUT, phi::NextafterKernel, float, double) {}
84 changes: 84 additions & 0 deletions paddle/phi/kernels/impl/nextafter_kernel_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/* Copyright (c) 2022 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. */

#pragma once
#include <algorithm>
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/core/device_context.h"
#include "paddle/phi/kernels/funcs/for_range.h"
#include "paddle/phi/kernels/funcs/math.h"
#include "paddle/phi/kernels/nextafter_kernel.h"
namespace phi {
template <typename T>
struct NextafterOut {
using type = T;
};

template <>
struct NextafterOut<int32_t> {
using type = double;
};

template <>
struct NextafterOut<int64_t> {
using type = double;
};
template <typename T>
struct NextafterFunctor {
NextafterFunctor(const T* x,
const T* y,
typename NextafterOut<T>::type* out,
int64_t numel)
: x_(x), y_(y), out_(out), numel_(numel) {}

HOSTDEVICE void operator()(int64_t idx) const {
out_[idx] = static_cast<typename NextafterOut<T>::type>(
::nextafter(static_cast<float>(x_[idx]), static_cast<float>(y_[idx])));
enkilee marked this conversation as resolved.
Show resolved Hide resolved
}
const T* x_;
const T* y_;
typename NextafterOut<T>::type* out_;
int64_t numel_;
};
template <>
struct NextafterFunctor<double> {
NextafterFunctor(const double* x, const double* y, double* out, int64_t numel)
: x_(x), y_(y), out_(out), numel_(numel) {}

HOSTDEVICE void operator()(int64_t idx) const {
out_[idx] = ::nextafter(x_[idx], y_[idx]);
enkilee marked this conversation as resolved.
Show resolved Hide resolved
}

const double* x_;
const double* y_;
double* out_;
int64_t numel_;
};

template <typename T, typename Context>
void NextafterKernel(const Context& ctx,
const DenseTensor& x,
const DenseTensor& y,
DenseTensor* out) {
auto* out_data = ctx.template Alloc<T>(out);
auto x_data = x.data<T>();
auto y_data = y.data<T>();
auto x_numel = x.numel();

phi::funcs::ForRange<Context> for_range(ctx, x_numel);
phi::NextafterFunctor<T> functor(x_data, y_data, out_data, x_numel);
for_range(functor);
}

} // namespace phi
28 changes: 28 additions & 0 deletions paddle/phi/kernels/nextafter_kernel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

// Copyright (c) 2022 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.

#pragma once

#include "paddle/phi/core/dense_tensor.h"

namespace phi {

template <typename T, typename Context>
void NextafterKernel(const Context& dev_ctx,
const DenseTensor& x,
const DenseTensor& y,
DenseTensor* out);

} // namespace phi
2 changes: 2 additions & 0 deletions python/paddle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@
from .tensor.math import trapezoid # noqa: F401
from .tensor.math import cumulative_trapezoid # noqa: F401
from .tensor.math import vander # noqa: F401
from .tensor.math import nextafter # noqa: F401

from .tensor.random import bernoulli # noqa: F401
from .tensor.random import poisson # noqa: F401
Expand Down Expand Up @@ -687,4 +688,5 @@
'cumulative_trapezoid',
'polar',
'vander',
'nextafter',
]
90 changes: 90 additions & 0 deletions python/paddle/fluid/tests/unittests/test_nextafter_op.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Copyright (c) 2023 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.

import unittest

import numpy as np
from eager_op_test import OpTest

import paddle


def ref_nextafter(x, y):
out = np.nextafter(x, y)
return out


class TestNextafterAPI(unittest.TestCase):
def setUp(self):
self.x_np = np.random.rand(1, 2).astype('float32')
self.y_np = np.random.rand(1, 2).astype('float32')
self.place = (
paddle.CUDAPlace(0)
if paddle.is_compiled_with_cuda()
else paddle.CPUPlace()
)

def test_static_api(self):
paddle.enable_static()
with paddle.static.program_guard(paddle.static.Program()):
x = paddle.static.data(
name="X", shape=self.x_np.shape, dtype='float32'
)
y = paddle.static.data(
name="Y", shape=self.y_np.shape, dtype='float32'
)
out = paddle.nextafter(x, y)
exe = paddle.static.Executor(self.place)
res = exe.run(
feed={'X': self.x_np, 'Y': self.y_np}, fetch_list=[out]
)
out_ref = ref_nextafter(self.x_np, self.y_np)
np.testing.assert_allclose(out_ref, res[0], rtol=1e-05)

def test_dygraph_api(self):
paddle.disable_static(self.place)
x = paddle.to_tensor(self.x_np)
y = paddle.to_tensor(self.y_np)
out = paddle.nextafter(x, y)
out_ref = ref_nextafter(self.x_np, self.y_np)
np.testing.assert_allclose(out_ref, out.numpy(), rtol=1e-05)
paddle.enable_static()


class TestNextafterOP(OpTest):
def setUp(self):
self.op_type = "nextafter"
self.python_api = paddle.nextafter
self.init_dtype()

x = np.array([1, 2]).astype(self.dtype)
y = np.array([2, 1]).astype(self.dtype)
out = np.nextafter(x, y)
self.inputs = {'X': x, 'Y': y}
self.outputs = {'Out': out}

def test_check_output(self):
self.check_output()

def init_dtype(self):
self.dtype = np.float64


class TestNextafterOPFP32(TestNextafterOP):
def init_dtype(self):
self.dtype = np.float32


if __name__ == "__main__":
unittest.main()
35 changes: 35 additions & 0 deletions python/paddle/tensor/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -5415,3 +5415,38 @@ def vander(x, n=None, increasing=False, name=None):
res[:, 1:] = paddle.cumprod(res[:, 1:], dim=-1)
res = res[:, ::-1] if not increasing else res
return res


def nextafter(x, y, name=None):
r"""
Return the next floating-point value after input towards other, elementwise.
The shapes of input and other must be broadcastable.

Args:
x (Tensor): An N-D Tensor, the data type is float32, float64.
y (Tensor): An N-D Tensor, the data type is float32, float64.
name(str, optional):Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

Returns:
out (Tensor): An N-D Tensor, the shape and data type is the same with input.

Examples:
.. code-block:: python

import paddle
out = paddle.nextafter(paddle.to_tensor([1.0,2.0]),paddle.to_tensor([2.0,1.0]))
print(out)
#Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
# [1.00000012, 1.99999988])
"""
if in_dygraph_mode():
return _C_ops.nextafter(x, y)
else:
check_variable_and_dtype(x, 'x', ['float32', 'float64'], 'nextafter')
check_variable_and_dtype(y, 'y', ['float32', 'float64'], 'nextafter')
helper = LayerHelper('nextafter', **locals())
out = helper.create_variable_for_type_inference(dtype=paddle.float32)
helper.append_op(
type='nextafter', inputs={'X': x, 'Y': y}, outputs={'Out': out}
)
return out