-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pten] move bernoulli kernel to pten (#39590)
* move bernoulli kernel to pten * follow comments
- Loading branch information
Showing
6 changed files
with
206 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// 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" | ||
#include "paddle/pten/core/device_context.h" | ||
|
||
namespace pten { | ||
|
||
template <typename T, typename Context> | ||
void BernoulliKernel(const Context& ctx, | ||
const DenseTensor& x, | ||
DenseTensor* out); | ||
|
||
} // namespace pten |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// 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/bernoulli_kernel.h" | ||
#include <random> | ||
#include "paddle/pten/backends/cpu/cpu_context.h" | ||
#include "paddle/pten/core/kernel_registry.h" | ||
|
||
namespace pten { | ||
|
||
template <typename T> | ||
inline T BernoulliFunctor(T p, T rand) { | ||
PADDLE_ENFORCE_LE(p, | ||
1.0, | ||
pten::errors::OutOfRange( | ||
"The probability should be <= 1, but got %f", p)); | ||
PADDLE_ENFORCE_GE(p, | ||
0.0, | ||
pten::errors::OutOfRange( | ||
"The probability should be >= 0, but got %f", p)); | ||
return static_cast<T>(rand < p); | ||
} | ||
|
||
template <typename T, typename Context> | ||
void BernoulliKernel(const Context& ctx, | ||
const DenseTensor& x, | ||
DenseTensor* out) { | ||
auto numel = x.numel(); | ||
auto* x_data = x.data<T>(); | ||
T* out_data = ctx.template Alloc<T>(out); | ||
|
||
std::uniform_real_distribution<T> dist(0.0, 1.0); | ||
auto gen_ptr = ctx.GetGenerator(); | ||
auto engine = gen_ptr->GetCPUEngine(); | ||
|
||
for (int64_t i = 0; i < numel; ++i) { | ||
out_data[i] = BernoulliFunctor(x_data[i], dist(*engine)); | ||
} | ||
} | ||
|
||
} // namespace pten | ||
|
||
PT_REGISTER_KERNEL( | ||
bernoulli, CPU, ALL_LAYOUT, pten::BernoulliKernel, float, double) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// 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 <thrust/execution_policy.h> | ||
#include <thrust/random.h> | ||
#include <thrust/transform.h> | ||
#include <algorithm> | ||
#include <vector> | ||
#include "paddle/pten/backends/gpu/gpu_context.h" | ||
#include "paddle/pten/core/dense_tensor.h" | ||
#include "paddle/pten/core/kernel_registry.h" | ||
#include "paddle/pten/kernels/bernoulli_kernel.h" | ||
|
||
// See Note [ Why still include the fluid headers? ] | ||
#include "paddle/fluid/platform/transform.h" | ||
|
||
namespace pten { | ||
|
||
template <typename T> | ||
struct BernoulliCudaFunctor { | ||
unsigned int seed_; | ||
unsigned int offset_; | ||
__host__ __device__ BernoulliCudaFunctor(unsigned int seed, | ||
unsigned int offset) | ||
: seed_(seed), offset_(offset) {} | ||
|
||
__host__ __device__ T operator()(const unsigned int n, const T p) const { | ||
// NOTE(zhiqiu): currently, PADDLE_ENFORCE in cuda kernel may print several | ||
// lines of error messages if, and it should be refined. | ||
PADDLE_ENFORCE(p >= 0.0 && p <= 1.0, | ||
"The probability should be >=0 and <= 1, but got %f", | ||
p); | ||
thrust::minstd_rand rng; | ||
rng.seed(seed_); | ||
thrust::uniform_real_distribution<T> dist(0.0, 1.0); | ||
rng.discard(n + offset_); | ||
return static_cast<T>(dist(rng) < p); | ||
} | ||
}; | ||
|
||
template <typename T, typename Context> | ||
void BernoulliKernel(const Context& ctx, | ||
const DenseTensor& x, | ||
DenseTensor* out) { | ||
auto numel = x.numel(); | ||
auto* x_data = x.data<T>(); | ||
T* out_data = ctx.template Alloc<T>(out); | ||
|
||
auto gen_cuda = ctx.GetGenerator(); | ||
auto seed_offset = gen_cuda->IncrementOffset(1); | ||
int64_t gen_offset = numel * seed_offset.second; | ||
paddle::platform::Transform<pten::GPUContext> trans; | ||
thrust::counting_iterator<int64_t> index_sequence_begin(0); | ||
trans(ctx, | ||
index_sequence_begin, | ||
index_sequence_begin + numel, | ||
x_data, | ||
out_data, | ||
BernoulliCudaFunctor<T>(static_cast<int64_t>(seed_offset.first), | ||
static_cast<int64_t>(gen_offset))); | ||
} | ||
|
||
} // namespace pten | ||
|
||
PT_REGISTER_KERNEL( | ||
bernoulli, GPU, ALL_LAYOUT, pten::BernoulliKernel, float, double) {} |