-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
new api trunc, test=develop #33371
new api trunc, test=develop #33371
Conversation
Thanks for your contribution! |
paddle/fluid/operators/trunc_op.cu
Outdated
__global__ void TruncGrad(const T* dout, T* dx, int N) { | ||
CUDA_KERNEL_LOOP(index, N) { dx[index] = 0.0; } | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The input argument dx is not needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, thanks!
|
||
void Apply(GradOpPtr<T> retv) const override { | ||
retv->SetType("trunc_grad"); | ||
retv->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can register NoNeedBufferVars for X@GRAD to save memory, see https://www.paddlepaddle.org.cn/documentation/docs/zh/guides/07_new_op/op_notes_cn.html#id6 for details.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, thanks!
paddle/fluid/operators/trunc_op.cu
Outdated
const T* x_data = x->data<T>(); | ||
T* out_data = out->mutable_data<T>(context.GetPlace()); | ||
|
||
int numel = x->numel(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
int -> int64_t, since there are tensors of huge size.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, thanks!
python/paddle/tensor/math.py
Outdated
# [0., 0.]]) | ||
''' | ||
if in_dygraph_mode(): | ||
out = _varbase_creator(dtype=x.dtype) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, thanks!
python/paddle/tensor/math.py
Outdated
attrs = {} | ||
|
||
helper = LayerHelper("trunc", **locals()) | ||
check_variable_and_dtype(x, 'X', ['float16', 'float32', 'float64'], 'trunc') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems float16 is not supported in c++ code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, thanks!
python/paddle/tensor/math.py
Outdated
Examples: | ||
.. code-block:: python | ||
import paddle | ||
import numpy as np |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use paddle.rand()
directly instead of numpy.random.random
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, thanks!
self.outputs = {'Out': (np.trunc(self.inputs['X']))} | ||
|
||
def init_dtype_type(self): | ||
pass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not put self.dtype = np.float64 here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, thanks!
paddle/fluid/operators/trunc_op.h
Outdated
/* 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. */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are blank lines in copyright.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it changed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now it is changed, done, thanks!
paddle/fluid/operators/trunc_op.cu
Outdated
namespace operators { | ||
|
||
template <typename T> | ||
class truncFunctor { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
class truncFunctor { | |
class TruncFunctor { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, thanks!
paddle/fluid/operators/trunc_op.cu
Outdated
public: | ||
__device__ truncFunctor(const T x) : _x(x) {} | ||
__device__ T operator()() { return trunc(_x); } | ||
const T _x; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
x -> x, plz refer to google code stype: https://google.github.io/styleguide/cppguide.html.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, thanks
paddle/fluid/operators/trunc_op.cu
Outdated
|
||
template <typename T> | ||
__global__ void TruncGrad(T* dx, int64_t N) { | ||
CUDA_KERNEL_LOOP(index, N) { dx[index] = 0.0; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CUDA_KERNEL_LOOP(index, N) { dx[index] = 0.0; } | |
CUDA_KERNEL_LOOP(index, N) { | |
dx[index] = static_cast<T>(0.0); | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, thanks
paddle/fluid/operators/trunc_op.cu
Outdated
dim3 blockSize(256); | ||
dim3 gridSize((numel + blockSize.x - 1) / blockSize.x); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
theads = platform::PADDLE_CUDA_NUM_THREADS;
blocks = (numel + PADDLE_CUDA_NUM_THREADS - 1) / PADDLE_CUDA_NUM_THREADS
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, thanks!
paddle/fluid/operators/trunc_op.h
Outdated
/* 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. */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it changed?
@@ -855,6 +855,44 @@ def add_n(inputs, name=None): | |||
return out | |||
|
|||
|
|||
def trunc(input, name=None): | |||
''' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
新增API需要文档预览截图,并且需要同时提交中文文档
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(1)新增API文档预览截图:
(2)中文文档pr:
PaddlePaddle/docs#3585
|
||
paddle.set_device('cpu') | ||
input = paddle.rand([2,2],'float32') | ||
output = paddle.trunc(input) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
没有给出原始tensor内容,可能会影响用户对行为的理解
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已添加原始tensor内容,谢谢!
python/paddle/tensor/math.py
Outdated
|
||
import paddle | ||
|
||
paddle.set_device('cpu') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
为什么要特意设置CPU?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
没有必要特意设置CPU,已删除,谢谢!
paddle.set_device('cpu') | ||
input = paddle.rand([2,2],'float32') | ||
output = paddle.trunc(input) | ||
print(output) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
注释给出的输出与代码不符,可以改为print(output.numpy()),或者把注释信息改为tensor的输出
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已将注释信息改为tensor的输出,谢谢!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
python/paddle/tensor/math.py
Outdated
This API is used to returns a new tensor with the truncated integer values of input. | ||
|
||
Args: | ||
input (Tensor): The input tensor, it's data type should be int, float, double. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
data type 应具体一点,如 int8、int16、int32、float32 等;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
PR types
New features
PR changes
APIs
Describe
Create a new paddle API: paddle.trunc(x, name=none).
对一个输入tensor,返回一个新的tensor,其中包含输入的截断整数值。支持的数据类型包括:int32、int64、float、double。
2.新增内容综述:
(1)新增两个Op:正向Op:trunc、对应的反向Op:trunc_grad;
(2)新增python API:paddle.trunc();
(3)新增单测文件:test_trunc_op.py;
3.API使用示例:
4.文档预览截图: