Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
4501abd6
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
4501abd6
编写于
2月 17, 2022
作者:
S
Sing_chan
提交者:
GitHub
2月 17, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
move trunc to pten (#39543)
* move trunc to pten * modify according to YuanRisheng's comment
上级
a3247ab5
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
299 addition
and
178 deletion
+299
-178
paddle/fluid/operators/trunc_op.cc
paddle/fluid/operators/trunc_op.cc
+2
-8
paddle/fluid/operators/trunc_op.cu
paddle/fluid/operators/trunc_op.cu
+0
-115
paddle/fluid/operators/trunc_op.h
paddle/fluid/operators/trunc_op.h
+0
-55
paddle/pten/kernels/cpu/trunc_grad_kernel.cc
paddle/pten/kernels/cpu/trunc_grad_kernel.cc
+40
-0
paddle/pten/kernels/cpu/trunc_kernel.cc
paddle/pten/kernels/cpu/trunc_kernel.cc
+39
-0
paddle/pten/kernels/gpu/trunc_grad_kernel.cu
paddle/pten/kernels/gpu/trunc_grad_kernel.cu
+54
-0
paddle/pten/kernels/gpu/trunc_kernel.cu
paddle/pten/kernels/gpu/trunc_kernel.cu
+81
-0
paddle/pten/kernels/trunc_grad_kernel.h
paddle/pten/kernels/trunc_grad_kernel.h
+26
-0
paddle/pten/kernels/trunc_kernel.h
paddle/pten/kernels/trunc_kernel.h
+26
-0
paddle/pten/ops/compat/trunc_sig.cc
paddle/pten/ops/compat/trunc_sig.cc
+31
-0
未找到文件。
paddle/fluid/operators/trunc_op.cc
浏览文件 @
4501abd6
...
@@ -12,7 +12,8 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
...
@@ -12,7 +12,8 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
See the License for the specific language governing permissions and
limitations under the License. */
limitations under the License. */
#include "paddle/fluid/operators/trunc_op.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/operator.h"
namespace
paddle
{
namespace
paddle
{
namespace
operators
{
namespace
operators
{
...
@@ -80,10 +81,3 @@ REGISTER_OPERATOR(trunc, ops::TruncOp, ops::TruncOpMaker,
...
@@ -80,10 +81,3 @@ REGISTER_OPERATOR(trunc, ops::TruncOp, ops::TruncOpMaker,
ops
::
TruncGradOpMaker
<
paddle
::
imperative
::
OpBase
>
);
ops
::
TruncGradOpMaker
<
paddle
::
imperative
::
OpBase
>
);
REGISTER_OPERATOR
(
trunc_grad
,
ops
::
TruncGradOp
);
REGISTER_OPERATOR
(
trunc_grad
,
ops
::
TruncGradOp
);
REGISTER_OP_CPU_KERNEL
(
trunc
,
ops
::
TruncKernel
<
float
>
,
ops
::
TruncKernel
<
double
>
,
ops
::
TruncKernel
<
int
>
,
ops
::
TruncKernel
<
int64_t
>
);
REGISTER_OP_CPU_KERNEL
(
trunc_grad
,
ops
::
TruncGradKernel
<
float
>
,
ops
::
TruncGradKernel
<
double
>
,
ops
::
TruncGradKernel
<
int
>
,
ops
::
TruncGradKernel
<
int64_t
>
);
paddle/fluid/operators/trunc_op.cu
已删除
100644 → 0
浏览文件 @
a3247ab5
/* 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/fluid/operators/trunc_op.h"
#include "paddle/fluid/platform/device/gpu/gpu_info.h"
#include "paddle/fluid/platform/device/gpu/gpu_primitives.h"
namespace
paddle
{
namespace
operators
{
using
platform
::
PADDLE_CUDA_NUM_THREADS
;
template
<
typename
T
>
class
TruncFunctor
{
public:
__device__
TruncFunctor
(
const
T
x
)
:
x_
(
x
)
{}
__device__
T
operator
()()
{
return
trunc
(
x_
);
}
public:
const
T
x_
;
};
template
<
>
class
TruncFunctor
<
int
>
{
public:
__device__
TruncFunctor
(
const
int
x
)
:
x_
(
x
)
{}
__device__
int
operator
()()
{
return
x_
;
}
public:
const
int
x_
;
};
template
<
>
class
TruncFunctor
<
int64_t
>
{
public:
__device__
TruncFunctor
(
const
int64_t
x
)
:
x_
(
x
)
{}
__device__
int64_t
operator
()()
{
return
x_
;
}
public:
const
int64_t
x_
;
};
template
<
typename
T
>
__global__
void
Trunc
(
const
T
*
x
,
T
*
out
,
int64_t
N
)
{
CUDA_KERNEL_LOOP
(
index
,
N
)
{
TruncFunctor
<
T
>
functor
(
x
[
index
]);
out
[
index
]
=
functor
();
}
}
template
<
typename
T
>
__global__
void
TruncGrad
(
T
*
dx
,
int64_t
N
)
{
CUDA_KERNEL_LOOP
(
index
,
N
)
{
dx
[
index
]
=
static_cast
<
T
>
(
0.0
);
}
}
template
<
typename
T
>
class
TruncCUDAKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
*
x
=
context
.
Input
<
Tensor
>
(
"X"
);
auto
*
out
=
context
.
Output
<
Tensor
>
(
"Out"
);
const
auto
*
x_data
=
x
->
data
<
T
>
();
auto
*
out_data
=
out
->
mutable_data
<
T
>
(
context
.
GetPlace
());
int64_t
numel
=
x
->
numel
();
int
theads
=
PADDLE_CUDA_NUM_THREADS
;
int
blocks
=
(
numel
+
theads
-
1
)
/
theads
;
Trunc
<<<
blocks
,
theads
>>>
(
x_data
,
out_data
,
numel
);
}
};
template
<
typename
T
>
class
TruncCUDAGradKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
*
dout
=
context
.
Input
<
Tensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
*
dx
=
context
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"X"
));
const
auto
*
dout_data
=
dout
->
data
<
T
>
();
auto
*
dx_data
=
dx
->
mutable_data
<
T
>
(
context
.
GetPlace
());
int64_t
numel
=
dout
->
numel
();
int
theads
=
PADDLE_CUDA_NUM_THREADS
;
int
blocks
=
(
numel
+
theads
-
1
)
/
theads
;
TruncGrad
<<<
blocks
,
theads
>>>
(
dx_data
,
numel
);
}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_CUDA_KERNEL
(
trunc
,
ops
::
TruncCUDAKernel
<
float
>
,
ops
::
TruncCUDAKernel
<
double
>
,
ops
::
TruncCUDAKernel
<
int
>
,
ops
::
TruncCUDAKernel
<
int64_t
>
);
REGISTER_OP_CUDA_KERNEL
(
trunc_grad
,
ops
::
TruncCUDAGradKernel
<
float
>
,
ops
::
TruncCUDAGradKernel
<
double
>
,
ops
::
TruncCUDAGradKernel
<
int
>
,
ops
::
TruncCUDAGradKernel
<
int64_t
>
);
paddle/fluid/operators/trunc_op.h
已删除
100644 → 0
浏览文件 @
a3247ab5
/* 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. */
#pragma once
#include <math.h>
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/operator.h"
namespace
paddle
{
namespace
operators
{
using
Tensor
=
framework
::
Tensor
;
template
<
typename
T
>
class
TruncKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
const
Tensor
*
x
=
context
.
Input
<
Tensor
>
(
"X"
);
Tensor
*
out
=
context
.
Output
<
Tensor
>
(
"Out"
);
size_t
numel
=
x
->
numel
();
const
T
*
x_data
=
x
->
data
<
T
>
();
T
*
out_data
=
out
->
mutable_data
<
T
>
(
context
.
GetPlace
());
for
(
size_t
i
=
0
;
i
<
numel
;
i
++
)
{
out_data
[
i
]
=
trunc
(
x_data
[
i
]);
}
}
};
template
<
typename
T
>
class
TruncGradKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
*
dx
=
context
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"X"
));
T
*
dx_data
=
dx
->
mutable_data
<
T
>
(
context
.
GetPlace
());
int
numel
=
dx
->
numel
();
memset
(
dx_data
,
0.0
,
numel
*
sizeof
(
T
));
}
};
}
// namespace operators
}
// namespace paddle
paddle/pten/kernels/cpu/trunc_grad_kernel.cc
0 → 100644
浏览文件 @
4501abd6
// 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/pten/kernels/trunc_grad_kernel.h"
#include "paddle/pten/backends/cpu/cpu_context.h"
#include "paddle/pten/core/kernel_registry.h"
namespace
pten
{
template
<
typename
T
,
typename
Context
>
void
TruncGradKernel
(
const
Context
&
dev_ctx
,
const
DenseTensor
&
out_grad
,
DenseTensor
*
in_grad
)
{
T
*
dx_data
=
dev_ctx
.
template
Alloc
<
T
>(
in_grad
);
int
numel
=
in_grad
->
numel
();
memset
(
dx_data
,
0.0
,
numel
*
sizeof
(
T
));
}
}
// namespace pten
PT_REGISTER_KERNEL
(
trunc_grad
,
CPU
,
ALL_LAYOUT
,
pten
::
TruncGradKernel
,
float
,
double
,
int
,
int64_t
)
{}
paddle/pten/kernels/cpu/trunc_kernel.cc
0 → 100644
浏览文件 @
4501abd6
// 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 <math.h>
#include "paddle/pten/backends/cpu/cpu_context.h"
#include "paddle/pten/core/kernel_registry.h"
#include "paddle/pten/kernels/trunc_kernel.h"
namespace
pten
{
template
<
typename
T
,
typename
Context
>
void
TruncKernel
(
const
Context
&
dev_ctx
,
const
DenseTensor
&
x
,
DenseTensor
*
out
)
{
size_t
numel
=
x
.
numel
();
const
T
*
x_data
=
x
.
data
<
T
>
();
T
*
out_data
=
dev_ctx
.
template
Alloc
<
T
>(
out
);
for
(
size_t
i
=
0
;
i
<
numel
;
i
++
)
{
out_data
[
i
]
=
trunc
(
x_data
[
i
]);
}
}
}
// namespace pten
PT_REGISTER_KERNEL
(
trunc
,
CPU
,
ALL_LAYOUT
,
pten
::
TruncKernel
,
float
,
double
,
int
,
int64_t
)
{}
paddle/pten/kernels/gpu/trunc_grad_kernel.cu
0 → 100644
浏览文件 @
4501abd6
// 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/fluid/platform/device/gpu/gpu_primitives.h"
#include "paddle/pten/backends/gpu/gpu_context.h"
#include "paddle/pten/backends/gpu/gpu_info.h"
#include "paddle/pten/core/kernel_registry.h"
#include "paddle/pten/kernels/trunc_grad_kernel.h"
namespace
pten
{
using
paddle
::
platform
::
PADDLE_CUDA_NUM_THREADS
;
template
<
typename
T
>
__global__
void
TruncGrad
(
T
*
dx
,
int64_t
N
)
{
CUDA_KERNEL_LOOP
(
index
,
N
)
{
dx
[
index
]
=
static_cast
<
T
>
(
0.0
);
}
}
template
<
typename
T
,
typename
Context
>
void
TruncGradKernel
(
const
Context
&
dev_ctx
,
const
DenseTensor
&
out_grad
,
DenseTensor
*
in_grad
)
{
const
auto
*
out_grad_data
=
out_grad
.
data
<
T
>
();
T
*
in_grad_data
=
dev_ctx
.
template
Alloc
<
T
>(
in_grad
);
int64_t
numel
=
out_grad
.
numel
();
int
theads
=
PADDLE_CUDA_NUM_THREADS
;
int
blocks
=
(
numel
+
theads
-
1
)
/
theads
;
TruncGrad
<<<
blocks
,
theads
>>>
(
in_grad_data
,
numel
);
}
}
// namespace pten
PT_REGISTER_KERNEL
(
trunc_grad
,
GPU
,
ALL_LAYOUT
,
pten
::
TruncGradKernel
,
float
,
double
,
int
,
int64_t
)
{}
paddle/pten/kernels/gpu/trunc_kernel.cu
0 → 100644
浏览文件 @
4501abd6
// 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/fluid/platform/device/gpu/gpu_primitives.h"
#include "paddle/pten/backends/gpu/gpu_context.h"
#include "paddle/pten/backends/gpu/gpu_info.h"
#include "paddle/pten/core/kernel_registry.h"
#include "paddle/pten/kernels/trunc_kernel.h"
namespace
pten
{
using
paddle
::
platform
::
PADDLE_CUDA_NUM_THREADS
;
template
<
typename
T
>
class
TruncFunctor
{
public:
__device__
TruncFunctor
(
const
T
x
)
:
x_
(
x
)
{}
__device__
T
operator
()()
{
return
trunc
(
x_
);
}
public:
const
T
x_
;
};
template
<
>
class
TruncFunctor
<
int
>
{
public:
__device__
TruncFunctor
(
const
int
x
)
:
x_
(
x
)
{}
__device__
int
operator
()()
{
return
x_
;
}
public:
const
int
x_
;
};
template
<
>
class
TruncFunctor
<
int64_t
>
{
public:
__device__
TruncFunctor
(
const
int64_t
x
)
:
x_
(
x
)
{}
__device__
int64_t
operator
()()
{
return
x_
;
}
public:
const
int64_t
x_
;
};
template
<
typename
T
>
__global__
void
Trunc
(
const
T
*
x
,
T
*
out
,
int64_t
N
)
{
CUDA_KERNEL_LOOP
(
index
,
N
)
{
TruncFunctor
<
T
>
functor
(
x
[
index
]);
out
[
index
]
=
functor
();
}
}
template
<
typename
T
,
typename
Context
>
void
TruncKernel
(
const
Context
&
dev_ctx
,
const
DenseTensor
&
x
,
DenseTensor
*
out
)
{
const
auto
*
x_data
=
x
.
data
<
T
>
();
auto
*
out_data
=
dev_ctx
.
template
Alloc
<
T
>(
out
);
int64_t
numel
=
x
.
numel
();
int
theads
=
PADDLE_CUDA_NUM_THREADS
;
int
blocks
=
(
numel
+
theads
-
1
)
/
theads
;
Trunc
<<<
blocks
,
theads
>>>
(
x_data
,
out_data
,
numel
);
}
}
// namespace pten
PT_REGISTER_KERNEL
(
trunc
,
GPU
,
ALL_LAYOUT
,
pten
::
TruncKernel
,
float
,
double
,
int
,
int64_t
)
{}
paddle/pten/kernels/trunc_grad_kernel.h
0 → 100644
浏览文件 @
4501abd6
// 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/pten/core/dense_tensor.h"
namespace
pten
{
template
<
typename
T
,
typename
Context
>
void
TruncGradKernel
(
const
Context
&
dev_ctx
,
const
DenseTensor
&
out_grad
,
DenseTensor
*
in_grad
);
}
// namespace pten
paddle/pten/kernels/trunc_kernel.h
0 → 100644
浏览文件 @
4501abd6
// 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/pten/core/dense_tensor.h"
namespace
pten
{
template
<
typename
T
,
typename
Context
>
void
TruncKernel
(
const
Context
&
dev_ctx
,
const
DenseTensor
&
x
,
DenseTensor
*
out
);
}
// namespace pten
paddle/pten/ops/compat/trunc_sig.cc
0 → 100644
浏览文件 @
4501abd6
// 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/pten/core/compat/op_utils.h"
namespace
pten
{
KernelSignature
TruncOpArgumentMapping
(
const
ArgumentMappingContext
&
ctx
)
{
return
KernelSignature
(
"trunc"
,
{
"X"
},
{},
{
"Out"
});
}
KernelSignature
TruncGradOpArgumentMapping
(
const
ArgumentMappingContext
&
ctx
)
{
return
KernelSignature
(
"trunc_grad"
,
{
GradVarName
(
"Out"
)},
{},
{
GradVarName
(
"X"
)});
}
}
// namespace pten
PT_REGISTER_ARG_MAPPING_FN
(
trunc
,
pten
::
TruncOpArgumentMapping
);
PT_REGISTER_ARG_MAPPING_FN
(
trunc_grad
,
pten
::
TruncGradOpArgumentMapping
);
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录