forked from PaddlePaddle/Paddle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b459b82
commit a30b9fb
Showing
8 changed files
with
262 additions
and
58 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 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,90 @@ | ||
/* 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 <string> | ||
#include <unordered_map> | ||
|
||
#include "paddle/fluid/operators/data/random_flip_op.h" | ||
#include "paddle/fluid/framework/op_version_registry.h" | ||
|
||
namespace paddle { | ||
namespace operators { | ||
namespace data { | ||
|
||
using framework::OpKernelType; | ||
using framework::Tensor; | ||
|
||
class RandomFlipOp : public framework::OperatorWithKernel { | ||
public: | ||
using framework::OperatorWithKernel::OperatorWithKernel; | ||
|
||
void InferShape(framework::InferShapeContext* ctx) const override { | ||
PADDLE_ENFORCE_EQ( | ||
ctx->HasInput("X"), true, | ||
platform::errors::NotFound("Input(X) of RandomFlipOp should not be null.")); | ||
PADDLE_ENFORCE_EQ(ctx->HasOutput("Out"), true, | ||
platform::errors::NotFound( | ||
"Output(Out) of RandomFlipOp should not be null.")); | ||
|
||
auto x_dims = ctx->GetInputDim("X"); | ||
ctx->SetOutputDim("Out", framework::make_ddim({x_dims[0], 1})); | ||
ctx->ShareLoD("X", "Out"); | ||
} | ||
|
||
framework::OpKernelType GetExpectedKernelType( | ||
const framework::ExecutionContext& ctx) const { | ||
auto input_data_type = OperatorWithKernel::IndicateVarDataType(ctx, "X"); | ||
return framework::OpKernelType(input_data_type, | ||
platform::CPUPlace()); | ||
} | ||
}; | ||
|
||
class RandomFlipOpMaker : public framework::OpProtoAndCheckerMaker { | ||
public: | ||
void Make() override { | ||
AddInput("X", "(Tensor), The input tensor of flip op."); | ||
AddOutput("Out", "(Tensor), The output tensor in shape of [N, 1], N is " | ||
"the batch size of X, bool data indicates whether to " | ||
"perform flip in this sample."); | ||
AddAttr<float>("probability", "The probability to flip each sample.") | ||
.SetDefault(0.5); | ||
AddAttr<int>("seed", "The seed for uniform random generator") | ||
.SetDefault(0); | ||
AddComment(R"DOC( | ||
Random Flip Operator. | ||
)DOC"); | ||
} | ||
}; | ||
|
||
class RandomFlipOpInferVarType : public framework::PassInDtypeAndVarTypeToOutput { | ||
protected: | ||
std::unordered_map<std::string, std::string>& GetInputOutputWithSameType() | ||
const override { | ||
static std::unordered_map<std::string, std::string> m{{"X", /*->*/ "Out"}}; | ||
return m; | ||
} | ||
}; | ||
|
||
} // namespace data | ||
} // namespace operators | ||
} // namespace paddle | ||
|
||
namespace ops = paddle::operators::data; | ||
namespace plat = paddle::platform; | ||
REGISTER_OPERATOR(random_flip, ops::RandomFlipOp, ops::RandomFlipOpMaker, ops::RandomFlipOpInferVarType); | ||
|
||
REGISTER_OP_CPU_KERNEL( | ||
random_flip, ops::RandomFlipCPUKernel<float>, | ||
ops::RandomFlipCPUKernel<double>, | ||
ops::RandomFlipCPUKernel<uint8_t>); |
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,86 @@ | ||
/* 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 <map> | ||
#include <random> | ||
#include <utility> | ||
|
||
#include "paddle/fluid/framework/op_registry.h" | ||
#include "paddle/fluid/framework/operator.h" | ||
|
||
namespace paddle { | ||
namespace operators { | ||
namespace data { | ||
|
||
using Tensor = framework::Tensor; | ||
|
||
constexpr size_t dim_bitset_size = 64; | ||
|
||
class RandomFlipGenerator { | ||
public: | ||
RandomFlipGenerator(int seed, float prob) | ||
: distribution_(prob), | ||
seed_(seed) { | ||
if (seed != 0) rng_.seed(seed); | ||
else rng_.seed(time(0)); | ||
} | ||
|
||
~RandomFlipGenerator() = default; | ||
|
||
bool Generate() { return distribution_(rng_); } | ||
|
||
private: | ||
std::bernoulli_distribution distribution_; | ||
int seed_; | ||
std::mt19937 rng_; | ||
}; | ||
|
||
std::map<int, std::unique_ptr<RandomFlipGenerator>> seed_to_generator_; | ||
|
||
static RandomFlipGenerator* CreateRandomFlipGenerator(int seed, float prob) { | ||
auto iter = seed_to_generator_.find(seed); | ||
if (iter == seed_to_generator_.end()) { | ||
seed_to_generator_[seed] = std::unique_ptr<RandomFlipGenerator>( | ||
new RandomFlipGenerator(seed, prob)); | ||
} | ||
|
||
return seed_to_generator_[seed].get(); | ||
} | ||
|
||
template <typename T> | ||
class RandomFlipCPUKernel : public framework::OpKernel<T> { | ||
public: | ||
void Compute(const framework::ExecutionContext& ctx) const override { | ||
LOG(ERROR) << "RandomFlipCPUKernel enter"; | ||
const Tensor* x = ctx.Input<Tensor>("X"); | ||
Tensor* out = ctx.Output<Tensor>("Out"); | ||
|
||
auto prob = ctx.Attr<float>("probability"); | ||
auto seed = ctx.Attr<int>("seed"); | ||
|
||
auto* data = out->mutable_data<bool>(ctx.GetPlace()); | ||
auto* generator = CreateRandomFlipGenerator(seed, prob); | ||
for (int64_t i = 0; i < x->dims()[0]; i++) { | ||
data[i] = generator->Generate() ? 1 : 0; | ||
} | ||
LOG(ERROR) << "RandomFlipCPUKernel finish"; | ||
} | ||
}; | ||
|
||
} // namespace data | ||
} // namespace operators | ||
} // namespace paddle |
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
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