From 380de6da77355b96e6e8bfb96b59904c24a2e809 Mon Sep 17 00:00:00 2001 From: superjomn Date: Sat, 6 Apr 2019 21:04:44 +0800 Subject: [PATCH] make kernel works --- paddle/fluid/lite/core/CMakeLists.txt | 3 +- paddle/fluid/lite/core/context.h | 2 +- paddle/fluid/lite/core/kernel.h | 2 + paddle/fluid/lite/core/kernel_test.cc | 1 - paddle/fluid/lite/core/op_lite.h | 31 ++++-- paddle/fluid/lite/core/op_lite_test.cc | 21 +++- paddle/fluid/lite/core/op_registry.cc | 53 +++++++++- paddle/fluid/lite/core/op_registry.h | 98 ++++++++---------- paddle/fluid/lite/core/target_wrapper.h | 14 ++- paddle/fluid/lite/core/tensor.h | 6 +- paddle/fluid/lite/core/tensor_test.cc | 31 ++++++ paddle/fluid/lite/core/variable.h | 2 +- paddle/fluid/lite/kernels/host/CMakeLists.txt | 2 + paddle/fluid/lite/kernels/host/fc_compute.cc | 16 ++- paddle/fluid/lite/kernels/host/fc_compute.h | 6 +- .../lite/kernels/host/fc_compute_test.cc | 99 +++++++++++++++++++ paddle/fluid/lite/model_parser/runtime.h | 9 +- paddle/fluid/lite/operators/CMakeLists.txt | 4 +- paddle/fluid/lite/operators/fc_op.h | 23 +++-- paddle/fluid/lite/operators/relu_op.cc | 2 +- paddle/fluid/lite/operators/relu_op.h | 3 +- paddle/fluid/lite/utils/factory.h | 12 ++- paddle/fluid/lite/utils/varient.h | 2 +- 23 files changed, 344 insertions(+), 98 deletions(-) create mode 100644 paddle/fluid/lite/core/tensor_test.cc create mode 100644 paddle/fluid/lite/kernels/host/fc_compute_test.cc diff --git a/paddle/fluid/lite/core/CMakeLists.txt b/paddle/fluid/lite/core/CMakeLists.txt index b656c494ddf..5ad83283ee4 100644 --- a/paddle/fluid/lite/core/CMakeLists.txt +++ b/paddle/fluid/lite/core/CMakeLists.txt @@ -1,11 +1,12 @@ cc_library(executor_lite SRCS executor.cc) -cc_library(op_lite SRCS op_lite.cc) cc_library(memory_lite SRCS memory.cc) cc_library(tensor_lite SRCS tensor.cc DEPS memory_lite) cc_library(variable_lite SRCS variable.cc) cc_library(op_registry_lite SRCS op_registry.cc) cc_library(scope_lite SRCS scope.cc) +cc_library(op_lite SRCS op_lite.cc DEPS scope_lite op_registry_lite) cc_test(test_scope_lite SRCS scope_test.cc DEPS scope_lite) cc_test(test_kernel_lite SRCS kernel_test.cc DEPS target_wrapper_x86) cc_test(test_op_lite SRCS op_lite_test.cc DEPS op_lite) +cc_test(test_tensor_lite SRCS tensor_test.cc) diff --git a/paddle/fluid/lite/core/context.h b/paddle/fluid/lite/core/context.h index a9908922bd8..abf00e53d08 100644 --- a/paddle/fluid/lite/core/context.h +++ b/paddle/fluid/lite/core/context.h @@ -15,7 +15,7 @@ #pragma once #include #include -#include "target_wrapper.h" +#include "paddle/fluid/lite/core/target_wrapper.h" namespace paddle { namespace lite { diff --git a/paddle/fluid/lite/core/kernel.h b/paddle/fluid/lite/core/kernel.h index af1adb9338c..2966df4d3f0 100644 --- a/paddle/fluid/lite/core/kernel.h +++ b/paddle/fluid/lite/core/kernel.h @@ -65,6 +65,8 @@ class OpKernel : public KernelBase { public: virtual void Run() { CHECK(false) << "Not Implemented"; } + void Touch() {} + OpKernel() = default; virtual ~OpKernel() = default; diff --git a/paddle/fluid/lite/core/kernel_test.cc b/paddle/fluid/lite/core/kernel_test.cc index 325744aeee2..7be5f8714c4 100644 --- a/paddle/fluid/lite/core/kernel_test.cc +++ b/paddle/fluid/lite/core/kernel_test.cc @@ -31,7 +31,6 @@ class SomeKernel : public OpKernel { TargetType target() const override { return TARGET(kHost); } PrecisionType precision() const override { return PRECISION(kFloat); } - }; TEST(Kernel, test) { diff --git a/paddle/fluid/lite/core/op_lite.h b/paddle/fluid/lite/core/op_lite.h index fa72d95051a..3ba849eb960 100644 --- a/paddle/fluid/lite/core/op_lite.h +++ b/paddle/fluid/lite/core/op_lite.h @@ -60,11 +60,20 @@ class OpLite : public Registry { struct Place { TargetType target{TARGET(kHost)}; PrecisionType precision{PRECISION(kFloat)}; + + Place(TargetType target, PrecisionType precision) + : target(target), precision(precision) {} }; OpLite() = default; - OpLite(std::unique_ptr &&x) : op_context_(std::move(x)) {} + OpLite(const std::string &type) : op_type_(type) {} + OpLite(std::unique_ptr &&x, const std::vector &valid_places) + : op_context_(std::move(x)), valid_places_(valid_places) {} + void SetValidPlaces(const std::vector &places) { + valid_places_ = places; + } + const std::vector &valid_places() const { return valid_places_; } // Check the shape. virtual bool CheckShape() const { return true; } // Inference the outputs' shape. @@ -79,20 +88,27 @@ class OpLite : public Registry { RecordOutputEvents(); return true; } - // Build the operator, attach it with the runtime environment. - virtual bool Build(const framework::OpDesc &opdesc, lite::Scope *scope) = 0; + + // Attach it with the runtime environment. + virtual bool Attach(const framework::OpDesc &opdesc, lite::Scope *scope) = 0; + // Human-readable information. virtual std::string DebugString() const = 0; const Place &kernel_place() const { return kernel_place_; } - protected: void PickKernel(const std::vector &valid_places, KernelStrategy kernel_strategy = KernelStrategy::kStatic); + virtual ~OpLite() = default; + + protected: // Specify the kernel to run by default. This will specify the value of // `kernel_place_`. - virtual void StaticPickKernel(const std::vector &valid_targets) = 0; + virtual void StaticPickKernel(const std::vector &valid_targets) { + auto kernels = CreateKernels(valid_targets); + kernel_ = std::move(kernels.front()); + } // Wait until all the inputs' events are ready. void SyncInputEvents() {} @@ -105,13 +121,12 @@ class OpLite : public Registry { std::vector> CreateKernels( const std::vector &places); - virtual ~OpLite() = default; - protected: std::unique_ptr op_context_; - Place kernel_place_; std::unique_ptr kernel_; std::string op_type_; + std::vector valid_places_; + Place kernel_place_{TARGET(kHost), PRECISION(kFloat)}; }; } // namespace lite diff --git a/paddle/fluid/lite/core/op_lite_test.cc b/paddle/fluid/lite/core/op_lite_test.cc index 0892da86606..eaa709afb74 100644 --- a/paddle/fluid/lite/core/op_lite_test.cc +++ b/paddle/fluid/lite/core/op_lite_test.cc @@ -1,13 +1,24 @@ -#include +// 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 "paddle/fluid/lite/core/op_lite.h" +#include namespace paddle { namespace lite { -TEST(OpLite, test) { - - -} +TEST(OpLite, test) {} } // namespace lite } // namespace paddle diff --git a/paddle/fluid/lite/core/op_registry.cc b/paddle/fluid/lite/core/op_registry.cc index 6556f2e6112..df1470a8b6f 100644 --- a/paddle/fluid/lite/core/op_registry.cc +++ b/paddle/fluid/lite/core/op_registry.cc @@ -12,4 +12,55 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "paddle/fluid/lite/core/op_registry.h" \ No newline at end of file +#include "paddle/fluid/lite/core/op_registry.h" + +namespace paddle { +namespace lite { + +std::unique_ptr KernelRegistry::Create(const std::string &op_type, + TargetType target, + PrecisionType precision) { +#define CREATE_KERNEL(target__) \ + switch (precision) { \ + case PRECISION(kFloat): \ + return Create(op_type); \ + default: \ + CHECK(false) << "not supported kernel place yet"; \ + } + + switch (target) { + case TARGET(kHost): { + CREATE_KERNEL(kHost); + } break; + case TARGET(kX86): { + CREATE_KERNEL(kX86); + } break; + case TARGET(kCUDA): { + CREATE_KERNEL(kCUDA); + } break; + default: + CHECK(false) << "not supported kernel place"; + } + +#undef CREATE_KERNEL +} + +KernelRegistry::KernelRegistry() { +#define INIT_FOR(target__, precision__) \ + registries_[KernelRegistry::GetKernelOffset()] \ + .set \ + *>(&KernelRegistryForTarget::Global()); + // Currently, just register 2 kernel targets. + INIT_FOR(kHost, kFloat); +#undef INIT_FOR +} + +KernelRegistry &KernelRegistry::Global() { + static auto *x = new KernelRegistry; + return *x; +} + +} // namespace lite +} // namespace paddle \ No newline at end of file diff --git a/paddle/fluid/lite/core/op_registry.h b/paddle/fluid/lite/core/op_registry.h index bf3bacb346c..2f122592563 100644 --- a/paddle/fluid/lite/core/op_registry.h +++ b/paddle/fluid/lite/core/op_registry.h @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +#pragma once #include #include #include @@ -62,28 +63,9 @@ class KernelRegistry final { KernelRegistryForTarget * // >; - KernelRegistry() { -/* -using kernel_target_t = - KernelRegistryForTarget; -registries_[0].set( - &KernelRegistryForTarget::Global()); - */ -#define INIT_FOR(target__, precision__) \ - registries_[KernelRegistry::GetKernelOffset()] \ - .set \ - *>(&KernelRegistryForTarget::Global()); - // Currently, just register 2 kernel targets. - INIT_FOR(kHost, kFloat); -#undef INIT_FOR - } + KernelRegistry(); - static KernelRegistry &Global() { - static auto *x = new KernelRegistry; - return *x; - } + static KernelRegistry &Global(); template void Register(const std::string &name, @@ -105,31 +87,7 @@ registries_[0].set( std::unique_ptr Create(const std::string &op_type, TargetType target, - PrecisionType precision) { -#define CREATE_KERNEL(target__) \ - switch (precision) { \ - case PRECISION(kFloat): \ - return Create(op_type); \ - default: \ - CHECK(false) << "not supported kernel place yet"; \ - } - - switch (target) { - case TARGET(kHost): { - CREATE_KERNEL(kHost); - } break; - case TARGET(kX86): { - CREATE_KERNEL(kX86); - } break; - case TARGET(kCUDA): { - CREATE_KERNEL(kCUDA); - } break; - default: - CHECK(false) << "not supported kernel place"; - } - -#undef CREATE_KERNEL - } + PrecisionType precision); // Get a kernel registry offset in all the registries. template @@ -137,8 +95,21 @@ registries_[0].set( return kNumTargets * static_cast(Target) + static_cast(Precision); } + std::string DebugString() const { + std::stringstream ss; + + ss << "KernelCreator:" << std::endl; + ss << registries_[GetKernelOffset()] + .get< + KernelRegistryForTarget *>() + ->DebugString(); + ss << std::endl; + return ss.str(); + } + private: - std::array registries_; + mutable std::array + registries_; }; template @@ -146,6 +117,8 @@ class KernelRegistor : public lite::Registor { public: KernelRegistor(const std::string op_type) : Registor([&] { + LOG(INFO) << "Register kernel " << op_type << " for " + << TargetToStr(target) << " " << PrecisionToStr(precision); KernelRegistry::Global().Register( op_type, [&]() -> std::unique_ptr { return std::unique_ptr(new KernelType); @@ -169,18 +142,27 @@ class KernelRegistor : public lite::Registor { // Kernel registry #define LITE_KERNEL_REGISTER(op_type__, target__, precision__) \ - op_type__##target__##precision__##__registor__ + op_type__##__##target__##__##precision__##__registor__ #define LITE_KERNEL_REGISTER_INSTANCE(op_type__, target__, precision__) \ - op_type__##target__##precision__##__registor__instance__ + op_type__##__##target__##__##precision__##__registor__instance__ #define LITE_KERNEL_REGISTER_FAKE(op_type__, target__, precision__) \ - LITE_KERNEL_REGISTER_INSTANCE(op_type__, target__, precision__)##__fake__ + LITE_KERNEL_REGISTER_INSTANCE(op_type__, target__, precision__) + +#define REGISTER_LITE_KERNEL(op_type__, target__, precision__, KernelClass) \ + static paddle::lite::KernelRegistor \ + LITE_KERNEL_REGISTER_INSTANCE(op_type__, target__, \ + precision__)(#op_type__); \ + static KernelClass LITE_KERNEL_INSTANCE(op_type__, target__, precision__); \ + int touch_##op_type__##target__##precision__() { \ + LITE_KERNEL_INSTANCE(op_type__, target__, precision__).Touch(); \ + return 0; \ + } -#define REGISTER_LITE_KERNEL(op_type__, target__, precision__, KernelClass) \ - static paddle::lite::KernelRegistor \ - LITE_KERNEL_REGISTER_INSTANCE(op_type__, target__, \ - precision__)(#op_type__); +#define USE_LITE_KERNEL(op_type__, target__, precision__) \ + extern int touch_##op_type__##target__##precision__(); \ + int LITE_KERNEL_REGISTER_FAKE(op_type__, target__, precision__) \ + __attribute__((unused)) = touch_##op_type__##target__##precision__(); -#define USE_LITE_KERNEL(op_type__, target__, precision__) \ - int LITE_KERNEL_REGISTER_FAKE(op_type__, target__, precision__)((unused)) = \ - LITE_KERNEL_REGISTER(op_type__, target__, precision__).Touch(); +#define LITE_KERNEL_INSTANCE(op_type__, target__, precision__) \ + op_type__##target__##precision__ diff --git a/paddle/fluid/lite/core/target_wrapper.h b/paddle/fluid/lite/core/target_wrapper.h index c7bd2ce2172..2eb8c762abf 100644 --- a/paddle/fluid/lite/core/target_wrapper.h +++ b/paddle/fluid/lite/core/target_wrapper.h @@ -43,6 +43,16 @@ enum class PrecisionType { kFloat = 0, kInt8, kLastAsPlaceHolder }; constexpr int kNumPrecisions = PRECISION_VAL(kLastAsPlaceHolder) - PRECISION_VAL(kFloat); +static const std::string target2string[] = {"host", "x86", "cuda"}; +static const std::string& TargetToStr(TargetType target) { + return target2string[static_cast(target)]; +} + +static const std::string precision2string[] = {"float, int8"}; +static const std::string& PrecisionToStr(PrecisionType precision) { + return precision2string[static_cast(precision)]; +} + // Event sync for multi-stream devices like CUDA and OpenCL. // For the devices without support of stream, leave it empty. template @@ -76,8 +86,8 @@ class TargetWrapper { static void StreamSync(const stream_t& stream) {} - static void* Malloc(size_t size) { return nullptr; } - static void Free(void* ptr) {} + static void* Malloc(size_t size) { return new char[size]; } + static void Free(void* ptr) { delete[] static_cast(ptr); } static void MemcpySync(void* dst, void* src, size_t size, IoDirection dir) {} static void MemcpyAsync(void* dst, void* src, size_t size, diff --git a/paddle/fluid/lite/core/tensor.h b/paddle/fluid/lite/core/tensor.h index 68a76ed18cc..62e4301faf5 100644 --- a/paddle/fluid/lite/core/tensor.h +++ b/paddle/fluid/lite/core/tensor.h @@ -47,6 +47,10 @@ static int product(const DDim& dims) { [](int a, int b) { return a * b; }); } +static int product(DDim::const_iterator begin, DDim::const_iterator end) { + return std::accumulate(begin, end, 1, [](int a, int b) { return a * b; }); +} + static DDim flatten_to_2d(const DDim& dims, int col) { return DDim({product(SliceDims(dims, 0, col)), product(SliceDims(dims, col, dims.size()))}); @@ -73,7 +77,7 @@ class Tensor { template T* mutable_data() { - buffer_.ResetLazy(target_, product(dims_)); + buffer_.ResetLazy(target_, product(dims_) * sizeof(T)); return static_cast(buffer_.data()); } diff --git a/paddle/fluid/lite/core/tensor_test.cc b/paddle/fluid/lite/core/tensor_test.cc new file mode 100644 index 00000000000..8b264562028 --- /dev/null +++ b/paddle/fluid/lite/core/tensor_test.cc @@ -0,0 +1,31 @@ +// 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 "paddle/fluid/lite/core/tensor.h" +#include + +namespace paddle { +namespace lite { + +TEST(tensor, test) { + Tensor tensor; + tensor.Resize({1, 8}); + + for (int i = 0; i < 8; i++) { + tensor.mutable_data()[i] = i; + } +} + +} // namespace lite +} // namespace paddle diff --git a/paddle/fluid/lite/core/variable.h b/paddle/fluid/lite/core/variable.h index b7eaa995788..63c2505ab90 100644 --- a/paddle/fluid/lite/core/variable.h +++ b/paddle/fluid/lite/core/variable.h @@ -28,7 +28,7 @@ class Variable { template T* GetMutable() { - blob_.set(); + if (!blob_.is()) blob_.set(); return &blob_.get(); } diff --git a/paddle/fluid/lite/kernels/host/CMakeLists.txt b/paddle/fluid/lite/kernels/host/CMakeLists.txt index 87952539c28..ab297fefa82 100644 --- a/paddle/fluid/lite/kernels/host/CMakeLists.txt +++ b/paddle/fluid/lite/kernels/host/CMakeLists.txt @@ -1,2 +1,4 @@ cc_library(fc_compute_host SRCS fc_compute.cc DEPS tensor_lite) cc_library(relu_compute_host SRCS relu_compute.cc DEPS tensor_lite) + +cc_test(test_fc_compute SRCS fc_compute_test.cc DEPS fc_compute_host fc_op_lite) diff --git a/paddle/fluid/lite/kernels/host/fc_compute.cc b/paddle/fluid/lite/kernels/host/fc_compute.cc index ef0fed843c2..f2f3863ca76 100644 --- a/paddle/fluid/lite/kernels/host/fc_compute.cc +++ b/paddle/fluid/lite/kernels/host/fc_compute.cc @@ -26,12 +26,16 @@ void FcCompute::Run() { using matrix_t = Eigen::Matrix; using matrix_map_t = Eigen::Map; - auto& param = this->param(); + auto& param = this->param(); - CHECK_EQ(param.in_mat_dims.size(), 2UL); + CHECK_GE(param.input->dims().size(), 2UL); CHECK_EQ(param.output->dims().size(), 2UL); - Eigen::Map input(param.input->data(), - param.in_mat_dims[0], param.in_mat_dims[1]); + Eigen::Map input( + param.input->data(), + product(param.input->dims().begin(), + param.input->dims().begin() + param.in_num_col_dims), + product(param.input->dims().begin() + param.in_num_col_dims, + param.input->dims().end())); Eigen::Map weight(param.w->data(), param.w->dims()[0], param.w->dims()[1]); matrix_map_t output(param.output->mutable_data(), @@ -47,6 +51,10 @@ void FcCompute::Run() { } } +TargetType FcCompute::target() const { return TARGET(kHost); } + +PrecisionType FcCompute::precision() const { return PRECISION(kFloat); } + } // namespace host } // namespace kernels } // namespace lite diff --git a/paddle/fluid/lite/kernels/host/fc_compute.h b/paddle/fluid/lite/kernels/host/fc_compute.h index 37316feba14..6daae48e726 100644 --- a/paddle/fluid/lite/kernels/host/fc_compute.h +++ b/paddle/fluid/lite/kernels/host/fc_compute.h @@ -14,6 +14,7 @@ #pragma once #include "paddle/fluid/lite/core/kernel.h" +#include "paddle/fluid/lite/kernels/fc_compute.h" #include "paddle/fluid/lite/operators/fc_op.h" namespace paddle { @@ -21,12 +22,15 @@ namespace lite { namespace kernels { namespace host { -class FcCompute final : public OpKernel { +class FcCompute : public OpKernel { public: using param_t = operators::FcParam; void Run() override; + TargetType target() const override; + PrecisionType precision() const override; + virtual ~FcCompute() = default; }; diff --git a/paddle/fluid/lite/kernels/host/fc_compute_test.cc b/paddle/fluid/lite/kernels/host/fc_compute_test.cc new file mode 100644 index 00000000000..bc412e80c81 --- /dev/null +++ b/paddle/fluid/lite/kernels/host/fc_compute_test.cc @@ -0,0 +1,99 @@ +// 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 "paddle/fluid/lite/kernels/host/fc_compute.h" +#include +#include +#include "paddle/fluid/lite/core/op_registry.h" + +namespace paddle { +namespace lite { +namespace kernels { +namespace host { + +TEST(fc_host, init) { + FcCompute fc; + ASSERT_EQ(fc.precision(), PRECISION(kFloat)); + ASSERT_EQ(fc.target(), TARGET(kHost)); +} + +TEST(fc_host, algorithm) { + using matrix_t = Eigen::Matrix; + using matrix_map_t = Eigen::Map; + + // dim 10, 20 + std::vector input(10 * 20); + std::vector w(20 * 20); + std::vector output(10 * 20); + + Eigen::Map input_mat(input.data(), 10, 20); + Eigen::Map weight_mat(w.data(), 20, 20); + matrix_map_t output_mat(output.data(), 10, 20); + + output_mat = weight_mat.transpose() * input_mat; +} + +TEST(fc_host, compute) { + FcCompute fc; + operators::FcParam param; + + Tensor x; + Tensor w; + Tensor bias; + Tensor output; + + x.Resize({1, 10, 20}); + w.Resize({20, 20}); + bias.Resize({1, 10}); + output.Resize({10, 20}); + + auto* x_data = x.mutable_data(); + auto* w_data = w.mutable_data(); + auto* bias_data = bias.mutable_data(); + auto* output_data = output.mutable_data(); + + for (int i = 0; i < 10 * 20; i++) x_data[i] = i; + for (int i = 0; i < 20 * 20; i++) w_data[i] = i; + for (int i = 0; i < 10; i++) bias_data[i] = i; + for (int i = 0; i < 10 * 20; i++) output_data[i] = 0; + + param.in_num_col_dims = 2; + param.input = &x; + param.w = &w; + param.bias = &bias; + param.output = &output; + param.in_mat_dims = x.dims(); + + fc.SetParam(param); + fc.Run(); + + LOG(INFO) << "x"; + for (int i = 0; i < 10 * 20; i++) LOG(INFO) << x_data[i]; + + LOG(INFO) << "output:"; + for (int i = 0; i < 10 * 20; i++) LOG(INFO) << output.data()[i]; +} + +TEST(fc, retrive_op) { + auto fc = + KernelRegistry::Global().Create("fc"); + ASSERT_TRUE(fc.get()); +} + +} // namespace host +} // namespace kernels +} // namespace lite +} // namespace paddle + +USE_LITE_KERNEL(fc, kHost, kFloat); diff --git a/paddle/fluid/lite/model_parser/runtime.h b/paddle/fluid/lite/model_parser/runtime.h index 31a2bf3e36f..3a96989e175 100644 --- a/paddle/fluid/lite/model_parser/runtime.h +++ b/paddle/fluid/lite/model_parser/runtime.h @@ -74,7 +74,7 @@ class LoDTensorArrayDesc { class VarType { public: framework::proto::VarType::Type type; - any desc; + variant desc; void Parse(const framework::proto::VarType& proto); }; @@ -95,7 +95,7 @@ class OpDesc { std::string op_type; std::map> inputs; std::map> outputs; - std::map attrs; + std::map> attrs; }; class BlockDesc { @@ -109,5 +109,10 @@ class BlockDesc { std::vector ops; }; +class ProgramDesc { + public: + void Parse(const framework::proto::ProgramDesc& desc); +}; + } // namespace lite } // namespace paddle diff --git a/paddle/fluid/lite/operators/CMakeLists.txt b/paddle/fluid/lite/operators/CMakeLists.txt index 954c8d2ba33..e13c2e61818 100644 --- a/paddle/fluid/lite/operators/CMakeLists.txt +++ b/paddle/fluid/lite/operators/CMakeLists.txt @@ -1,5 +1,5 @@ -cc_library(fc_op_lite SRCS fc_op.cc DEPS op_lite op_params_lite tensor_lite) +cc_library(fc_op_lite SRCS fc_op.cc DEPS op_lite op_params_lite tensor_lite proto_desc) cc_library(relu_op_lite SRCS relu_op.cc DEPS op_lite) cc_library(op_params_lite SRCS op_params.cc DEPS tensor_lite) -cc_test(test_fc_op_lite SRCS fc_op_test.cc DEPS fc_op_lite) +cc_test(test_fc_op_lite SRCS fc_op_test.cc DEPS fc_op_lite fc_compute_host) diff --git a/paddle/fluid/lite/operators/fc_op.h b/paddle/fluid/lite/operators/fc_op.h index c2a7e9a8685..523d68c0ea1 100644 --- a/paddle/fluid/lite/operators/fc_op.h +++ b/paddle/fluid/lite/operators/fc_op.h @@ -29,32 +29,43 @@ class FcOpLite : public OpLite { public: FcOpLite() {} + FcOpLite(const std::string &type) : OpLite(type) {} + bool CheckShape() const override; bool InferShape() const override; - bool Run() override { return false; } + bool Run() override { + CHECK(kernel_); + kernel_->Run(); + return true; + } // TODO(Superjomn) replace framework::OpDesc with a lite one. - bool Build(const framework::OpDesc& op_desc, lite::Scope* scope) override { + bool Attach(const framework::OpDesc &op_desc, lite::Scope *scope) override { auto input = op_desc.Input("Input").front(); auto W = op_desc.Input("W").front(); - auto bias = op_desc.Input("bias").front(); - auto out = op_desc.Output("bias").front(); + auto bias = op_desc.Input("Bias").front(); + auto out = op_desc.Output("Out").front(); param_.input = scope->FindVar(input)->GetMutable(); param_.w = scope->FindVar(W)->GetMutable(); param_.bias = scope->FindVar(bias)->GetMutable(); param_.output = scope->FindVar(out)->GetMutable(); param_.in_num_col_dims = - boost::any_cast(op_desc.GetAttr("in_num_col_dims")); + boost::get(op_desc.GetAttr("in_num_col_dims")); + + kernel_->SetParam(param_); return true; } std::string DebugString() const override { return "fc"; } - void StaticPickKernel(const std::vector& valid_targets) override {} + void StaticPickKernel(const std::vector &valid_targets) override { + auto kernels = CreateKernels(valid_targets); + kernel_ = std::move(kernels.front()); + } private: mutable FcParam param_; diff --git a/paddle/fluid/lite/operators/relu_op.cc b/paddle/fluid/lite/operators/relu_op.cc index 189cc16b1c6..aac127d45cb 100644 --- a/paddle/fluid/lite/operators/relu_op.cc +++ b/paddle/fluid/lite/operators/relu_op.cc @@ -33,7 +33,7 @@ bool ReluOp::InferShape() const { bool ReluOp::Run() { return false; } -bool ReluOp::Build(const framework::OpDesc &opdesc, framework::Scope *scope) { +bool ReluOp::Attach(const framework::OpDesc &opdesc, framework::Scope *scope) { return false; } diff --git a/paddle/fluid/lite/operators/relu_op.h b/paddle/fluid/lite/operators/relu_op.h index b2c32f530fd..c2486559308 100644 --- a/paddle/fluid/lite/operators/relu_op.h +++ b/paddle/fluid/lite/operators/relu_op.h @@ -37,7 +37,8 @@ class ReluOp : public OpLite { bool Run() override; - bool Build(const framework::OpDesc& opdesc, framework::Scope* scope) override; + bool Attach(const framework::OpDesc& opdesc, + framework::Scope* scope) override; std::string DebugString() const override { return "tanh"; } diff --git a/paddle/fluid/lite/utils/factory.h b/paddle/fluid/lite/utils/factory.h index 680e9635c02..37f2c2293cb 100644 --- a/paddle/fluid/lite/utils/factory.h +++ b/paddle/fluid/lite/utils/factory.h @@ -13,7 +13,9 @@ // limitations under the License. #pragma once +#include #include +#include #include namespace paddle { @@ -52,10 +54,18 @@ class Factory { item_ptr_t Create(const std::string& op_type) const { auto it = creators_.find(op_type); - CHECK(it != creators_.end()); + CHECK(it != creators_.end()) << "no item called " << op_type; return it->second(); } + std::string DebugString() const { + std::stringstream ss; + for (const auto& item : creators_) { + ss << " - " << item.first << std::endl; + } + return ss.str(); + } + protected: std::unordered_map creators_; }; diff --git a/paddle/fluid/lite/utils/varient.h b/paddle/fluid/lite/utils/varient.h index 3a3296976c3..ab13a647ec3 100644 --- a/paddle/fluid/lite/utils/varient.h +++ b/paddle/fluid/lite/utils/varient.h @@ -93,7 +93,7 @@ struct variant { return *this; } template - void is() { + bool is() { return (type_id == typeid(T).hash_code()); } -- GitLab