未验证 提交 a17d7be3 编写于 作者: W Wangzheee 提交者: GitHub

[OP][HOST][KERNEL]Add uniform_random operator, and add host kernel for...

[OP][HOST][KERNEL]Add uniform_random operator, and add host kernel for uniform_random. test=develop (#4264)
上级 4466bea8
......@@ -27,6 +27,7 @@ add_kernel(conditional_block_compute_host Host extra SRCS conditional_block_comp
add_kernel(activation_grad_compute_host Host train SRCS activation_grad_compute.cc DEPS ${lite_kernel_deps})
add_kernel(pixel_shuffle_compute_host Host extra SRCS pixel_shuffle_compute.cc DEPS ${lite_kernel_deps})
add_kernel(one_hot_compute_host Host extra SRCS one_hot_compute.cc DEPS ${lite_kernel_deps})
add_kernel(uniform_random_compute_host Host extra SRCS uniform_random_compute.cc DEPS ${lite_kernel_deps})
if(LITE_BUILD_EXTRA AND LITE_WITH_x86)
lite_cc_test(test_where_index_compute_host SRCS where_index_compute.cc DEPS where_index_compute_host)
......
// Copyright (c) 2019 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 "lite/kernels/host/uniform_random_compute.h"
namespace paddle {
namespace lite {
namespace kernels {
namespace host {
template <typename T>
void UniformRandomKernelFunctor(Tensor* out, float min, float max, int seed) {
T* p_out_data = out->mutable_data<T>();
int64_t size = out->numel();
memset(p_out_data, 0, size * sizeof(T));
unsigned int out_seed = static_cast<unsigned int>(seed);
std::minstd_rand engine;
if (out_seed == 0) {
out_seed = std::random_device()();
}
engine.seed(out_seed);
std::uniform_real_distribution<T> dist(static_cast<T>(min),
static_cast<T>(max));
for (int64_t i = 0; i < size; ++i) {
p_out_data[i] = dist(engine);
}
}
void UniformRandomCompute::Run() {
auto& param = this->template Param<param_t>();
switch (param.dtype) {
case static_cast<int>(lite::core::FluidType::FP64):
UniformRandomKernelFunctor<double>(
param.Out, param.min, param.max, param.seed);
break;
case static_cast<int>(lite::core::FluidType::FP32):
UniformRandomKernelFunctor<float>(
param.Out, param.min, param.max, param.seed);
break;
default:
LOG(ERROR) << "Unsupported data type for uniform_random op:"
<< param.dtype;
}
}
} // namespace host
} // namespace kernels
} // namespace lite
} // namespace paddle
REGISTER_LITE_KERNEL(uniform_random,
kHost,
kAny,
kAny,
paddle::lite::kernels::host::UniformRandomCompute,
def)
.BindInput("ShapeTensor",
{LiteType::GetTensorTy(TARGET(kHost),
PRECISION(kAny),
DATALAYOUT(kAny))})
.BindOutput("Out",
{LiteType::GetTensorTy(TARGET(kHost),
PRECISION(kAny),
DATALAYOUT(kAny))})
.Finalize();
// Copyright (c) 2019 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 <random>
#include "lite/core/kernel.h"
#include "lite/core/op_registry.h"
namespace paddle {
namespace lite {
namespace kernels {
namespace host {
class UniformRandomCompute
: public KernelLite<TARGET(kHost), PRECISION(kAny), DATALAYOUT(kAny)> {
public:
using param_t = operators::UniformRandomParam;
void Run() override;
virtual ~UniformRandomCompute() = default;
};
} // namespace host
} // namespace kernels
} // namespace lite
} // namespace paddle
......@@ -747,6 +747,7 @@ struct SGDParam : ParamBase {
/// ----------------------- uniform_random operators ----------------------
struct UniformRandomParam : ParamBase {
const lite::Tensor* X{nullptr};
std::vector<int64_t> shape{};
float min{-1.0f};
float max{1.0f};
......
......@@ -23,7 +23,25 @@ namespace operators {
bool UniformRandomOpLite::CheckShape() const { return true; }
bool UniformRandomOpLite::InferShapeImpl() const {
param_.Out->Resize(param_.shape);
if (param_.X) {
if (param_.X->precision() == PrecisionType::kInt64) {
auto* new_data = param_.X->data<int64_t>();
std::vector<int64_t> new_shape(new_data, new_data + param_.X->numel());
param_.Out->Resize(new_shape);
} else if (param_.X->precision() == PrecisionType::kInt32) {
std::vector<int64_t> new_shape;
auto* new_data = param_.X->data<int32_t>();
for (int i = 0; i < param_.X->numel(); ++i) {
new_shape.push_back(static_cast<int64_t>(*(new_data + i)));
}
param_.Out->Resize(new_shape);
} else {
LOG(ERROR) << "The dtype of shape tensor must be int32 or int64.";
}
} else {
auto new_shape = param_.shape;
param_.Out->Resize(new_shape);
}
return true;
}
......@@ -34,6 +52,10 @@ bool UniformRandomOpLite::AttachImpl(const cpp::OpDesc& opdesc,
param_.max = opdesc.GetAttr<float>("max");
param_.seed = opdesc.GetAttr<int>("seed");
param_.dtype = opdesc.GetAttr<int>("dtype");
if (opdesc.HasInput("ShapeTensor")) {
auto X = opdesc.Input("ShapeTensor").front();
param_.X = scope->FindVar(X)->GetMutable<lite::Tensor>();
}
param_.Out = GetMutableVar<Tensor>(scope, opdesc.Output("Out").front());
return true;
}
......
......@@ -93,4 +93,5 @@ endif()
lite_cc_test(test_kernel_matmul_compute SRCS matmul_compute_test.cc DEPS arena_framework ${xpu_kernels} ${npu_kernels} ${huawei_ascend_npu_kernels} ${bm_kernels} ${x86_kernels} ${cuda_kernels} ${arm_kernels} ${lite_ops} ${host_kernels})
lite_cc_test(test_kernel_flatten_compute SRCS flatten_compute_test.cc DEPS arena_framework ${xpu_kernels} ${npu_kernels} ${huawei_ascend_npu_kernels} ${bm_kernels} ${x86_kernels} ${cuda_kernels} ${arm_kernels} ${lite_ops} ${host_kernels})
#lite_cc_test(test_kernel_crf_decoding_compute SRCS crf_decoding_compute_test.cc DEPS arena_framework ${xpu_kernels} ${npu_kernels} ${huawei_ascend_npu_kernels} ${bm_kernels} ${x86_kernels} ${cuda_kernels} ${arm_kernels} ${lite_ops} ${host_kernels})
lite_cc_test(test_uniform_random_compute SRCS uniform_random_compute_test.cc DEPS arena_framework ${lite_ops} ${host_kernels})
endif()
// Copyright (c) 2019 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 "lite/kernels/host/uniform_random_compute.h"
// #include <random>
#include "lite/kernels/host/uniform_random_compute.h"
#include <gflags/gflags.h>
#include <gtest/gtest.h>
#include "lite/core/context.h"
#include "lite/core/profile/timer.h"
#include "lite/operators/op_params.h"
#include "lite/tests/utils/naive_math_impl.h"
#include "lite/tests/utils/tensor_utils.h"
namespace paddle {
namespace lite {
namespace kernels {
namespace host {
TEST(uniformrandom, test) {
using T = double;
std::vector<int64_t> shape(2, 3);
float min = -5.0f;
float max = 10.0f;
int seed = 0;
int dtype = static_cast<int>(VarDescAPI::VarDataType::FP64);
lite::Tensor x, out;
x.Resize({1, 2});
auto* x_data = x.mutable_data<int64_t>();
x_data[0] = 2;
x_data[1] = 2;
out.Resize({x_data[0], x_data[1]});
UniformRandomCompute uniform_random;
paddle::lite::operators::UniformRandomParam param;
param.X = &x;
param.shape = shape;
param.min = min;
param.max = max;
param.seed = seed;
param.dtype = dtype;
param.Out = &out;
uniform_random.SetParam(param);
uniform_random.Run();
const double* outdata = out.data<double>();
for (int i = 0; i < out.numel(); i++) {
LOG(INFO) << "out.data: " << outdata[i];
}
}
} // namespace host
} // namespace kernels
} // namespace lite
} // namespace paddle
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册