From 19b1095347aafd3f5a756464ad6d7e90a77522f8 Mon Sep 17 00:00:00 2001 From: Chen Weihang Date: Fri, 22 Oct 2021 04:03:25 +0000 Subject: [PATCH] remove tensor signature and backend set member --- .gitignore | 1 - paddle/fluid/operators/mean_op.h | 2 +- paddle/pten/common/backend.h | 4 +- paddle/pten/hapi/include/backend_set.h | 4 +- paddle/pten/hapi/include/tensor.h | 44 +++++++------------- paddle/pten/hapi/include/tensor_signature.h | 45 --------------------- paddle/pten/hapi/lib/creation.cc | 1 - paddle/pten/hapi/lib/kernel_dispatch.h | 16 +++++++- paddle/pten/hapi/lib/linalg.cc | 1 - paddle/pten/hapi/lib/manipulation.cc | 1 - paddle/pten/hapi/lib/math.cc | 1 - 11 files changed, 35 insertions(+), 85 deletions(-) delete mode 100644 paddle/pten/hapi/include/tensor_signature.h diff --git a/.gitignore b/.gitignore index 8a7b73d46c032..749832c3930cf 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,6 @@ paddle/fluid/API_DEV.spec paddle/fluid/API_PR.spec paddle/fluid/op_use_default_grad_maker_DEV.spec paddle/fluid/op_use_default_grad_maker_PR.spec -tools/__pycache__/static_mode_white_list.cpython-37.pyc *.DS_Store *.vs diff --git a/paddle/fluid/operators/mean_op.h b/paddle/fluid/operators/mean_op.h index 661ff41f10f85..9a8c2736589c9 100644 --- a/paddle/fluid/operators/mean_op.h +++ b/paddle/fluid/operators/mean_op.h @@ -49,7 +49,7 @@ using EigenVector = framework::EigenVector; * Currently, only the first two cases are adapted. * * The principle here is that the implementation in the kernel must reuse the - * corresponding functions in the Tensor compute library and cannot maintain + * corresponding functions in the Tensor Operation library and cannot maintain * two copies of the code. */ template diff --git a/paddle/pten/common/backend.h b/paddle/pten/common/backend.h index 6dc505fa2c5ca..9808b45b45c7c 100644 --- a/paddle/pten/common/backend.h +++ b/paddle/pten/common/backend.h @@ -28,8 +28,8 @@ namespace experimental { * but in order to make the boundary of the kernel clearer and the function * more specific, we need to distinguish the calculation method. * - * Such as the kernel for CUDA device, it can be a native CUDA kernel, - * or a kernel implemented by CUDNN library. + * Such as the kernel for CPU device, it can be a native CPU kernel, + * or a kernel implemented by MKLDNN library. * * Note(chenweihang): HIP is not needed now, we can added it if needed * in the future diff --git a/paddle/pten/hapi/include/backend_set.h b/paddle/pten/hapi/include/backend_set.h index a47cb76489375..00f59b45a188f 100644 --- a/paddle/pten/hapi/include/backend_set.h +++ b/paddle/pten/hapi/include/backend_set.h @@ -26,8 +26,8 @@ namespace experimental { * We use the backend to form a bit set to assist the runtime kernel selection, * and the higher backend bit has a higher priority. * - * A Tensor may belong to multiple backends at the same time, such CUDNN and - * CUDA. Only one backend value cannot + * A Tensor may belong to multiple backends at the same time, such CPU and + * MKLDNN. Only one backend value cannot */ class BackendSet final { public: diff --git a/paddle/pten/hapi/include/tensor.h b/paddle/pten/hapi/include/tensor.h index f915a06087017..393332eefa119 100644 --- a/paddle/pten/hapi/include/tensor.h +++ b/paddle/pten/hapi/include/tensor.h @@ -19,18 +19,17 @@ limitations under the License. */ #include #include "paddle/pten/core/tensor_base.h" -#include "paddle/pten/hapi/include/tensor_signature.h" /** * [ Why still include the fluid headers? ] * * We hope to organize the basic implementation of Tensor and the logic related * to Tensor computation into an independent library, which we call - * [Tensor Compute Library, pten], so we extract or rewrite the original + * [Tensor Operation Library, pten], so we extract or rewrite the original * Kernels. * * In the future, the training library, inference library and custom operators - * will link to this Tensor Compute library. + * will link to this Tensor Operation library. * * However, if we directly split the link relation, we need to make too many * changes, which will affect the stability of the framework, so here we still @@ -47,15 +46,15 @@ namespace experimental { class Tensor; -class AutogradMetaInterface { +class AbstractAutogradMeta { public: - // No AutogradMetaInterface should be created - virtual ~AutogradMetaInterface() {} + // No AbstractAutogradMeta should be created + virtual ~AbstractAutogradMeta() {} }; /** * Tensor is the API description of the basic data structure in the - * [ Paddle "Tensor CoMPuTe (pten)" Library ]. + * [ "Paddle Tensor Operation (pten)" Library ]. * * It is not limited to a simple n-dimensional array. * It contains a smart pointer to `TensorImpl`. The data description contained @@ -97,7 +96,6 @@ class Tensor final { if (impl_.get() == nullptr) { throw std::runtime_error("TensorImpl with nullptr is not supported"); } - signature_.reset(new TensorSignature(impl_->backend())); } /* Part 2: Dimension, DataType and DataLayout methods */ @@ -140,16 +138,8 @@ class Tensor final { /** * Backend judgment APIs, shield the concept of Backend. */ - BackendSet backend_set() const { return signature_->backend_set; } - void set_backend_set(const BackendSet& backend_set) { - if (signature_ == nullptr) { - signature_.reset(new TensorSignature()); - } - signature_->backend_set = backend_set; - } - - bool is_cpu() const { return signature_->backend_set.Has(Backend::CPU); } - bool is_cuda() const { return signature_->backend_set.Has(Backend::CUDA); } + bool is_cpu() const { return paddle::platform::is_cpu_place(place()); } + bool is_cuda() const { return paddle::platform::is_gpu_place(place()); } /** * Backend convert APIs. @@ -211,11 +201,11 @@ class Tensor final { } /* Part 7: Autograd methods */ - AutogradMetaInterface* get_autograd_meta() const { + AbstractAutogradMeta* get_autograd_meta() const { return autograd_meta_.get(); } - void set_autograd_meta(std::shared_ptr autograd_meta) { + void set_autograd_meta(std::shared_ptr autograd_meta) { autograd_meta_ = std::move(autograd_meta); } @@ -244,7 +234,7 @@ class Tensor final { std::shared_ptr impl_; /** - * [ Why need abstract AutogradMetaInterface here? ] + * [ Why need abstract AbstractAutogradMeta here? ] * * Dynamic graphs need to hold backward information * @@ -254,17 +244,13 @@ class Tensor final { * information, not Tensor data description-related information. * 2. Kernel calculation does not require AutogradMeta. */ - std::shared_ptr autograd_meta_{nullptr}; + std::shared_ptr autograd_meta_{nullptr}; /** - * TensorSignature is used to store auxiliary description information - * needed by Tensor. - * - * The currently stored information includes: - * 1. name: used for Debug analysis in the development of new dygraph. - * 2. backend_set: used by the API to determine the kernel backend. + * Tensor name: used for adapt original execution mechanism and debug analysis + * in the development of new dygraph. */ - std::shared_ptr signature_{nullptr}; + std::string name_; }; } // namespace experimental diff --git a/paddle/pten/hapi/include/tensor_signature.h b/paddle/pten/hapi/include/tensor_signature.h deleted file mode 100644 index ca20f9da75a84..0000000000000 --- a/paddle/pten/hapi/include/tensor_signature.h +++ /dev/null @@ -1,45 +0,0 @@ -/* 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 - -#include "paddle/pten/hapi/include/backend_set.h" - -namespace paddle { -namespace experimental { - -struct TensorSignature final { - std::string name{""}; - BackendSet backend_set{Backend::CPU}; - - TensorSignature() = default; - - // open default methods if needed - TensorSignature& operator=(const TensorSignature&) = delete; - TensorSignature& operator=(TensorSignature&&) = delete; - TensorSignature(const TensorSignature&) = delete; - TensorSignature(TensorSignature&&) = delete; - - explicit TensorSignature(const std::string& t_name) : name(t_name) {} - explicit TensorSignature(const Backend& t_backend) : backend_set(t_backend) {} - explicit TensorSignature(const BackendSet& t_backend_set) - : backend_set(t_backend_set) {} - TensorSignature(const std::string& t_name, const BackendSet& t_backend_set) - : name(t_name), backend_set(t_backend_set) {} -}; - -} // namespace experimental -} // namespace paddle diff --git a/paddle/pten/hapi/lib/creation.cc b/paddle/pten/hapi/lib/creation.cc index 5e32ffa59637d..046a76e13295b 100644 --- a/paddle/pten/hapi/lib/creation.cc +++ b/paddle/pten/hapi/lib/creation.cc @@ -56,7 +56,6 @@ Tensor full_like(const Tensor& x, std::make_shared(out_meta, pten::TensorStatus()); kernel_context.EmplaceBackOutput(dense_out); out.set_impl(dense_out); - out.set_backend_set(x.backend_set()); // 6. Call kernel kernel(&kernel_context); diff --git a/paddle/pten/hapi/lib/kernel_dispatch.h b/paddle/pten/hapi/lib/kernel_dispatch.h index 95410ee942012..d7190076bf3f6 100644 --- a/paddle/pten/hapi/lib/kernel_dispatch.h +++ b/paddle/pten/hapi/lib/kernel_dispatch.h @@ -24,6 +24,7 @@ limitations under the License. */ #include "paddle/pten/hapi/include/tensor.h" // TODO(chenweihang): split KernelName, Key, Kernel, Factory into diff files +#include "paddle/pten/core/convert_utils.h" #include "paddle/pten/core/kernel_factory.h" // See Note [ Why still include the fluid headers? ] @@ -39,6 +40,19 @@ using CUDAContext = paddle::platform::CUDADeviceContext; #endif namespace detail { +BackendSet GetTensorBackendSet(const Tensor& t) { + BackendSet backend_set(pten::TransToPtenBackend(t.place())); + switch (t.layout()) { + case DataLayout::MKLDNN: + backend_set = backend_set | BackendSet(Backend::MKLDNN); + break; + default: + // do nothing + break; + } + return backend_set; +} + std::size_t CountLeadingZeros(uint64_t val) { if (val == 0) { return 64; @@ -102,7 +116,7 @@ struct KernelKeyParser : ArgsIterator { // TODO(chenweihang): deal with multiple diff input Tensors // TODO(chenweihang): add global device guard method to set backend void operator()(const Tensor& x) { - key_set.backend_set = key_set.backend_set | x.backend_set(); + key_set.backend_set = key_set.backend_set | detail::GetTensorBackendSet(x); // TODO(chenweihang): selecte multi layout and dtype key_set.layout = x.layout(); key_set.dtype = x.type(); diff --git a/paddle/pten/hapi/lib/linalg.cc b/paddle/pten/hapi/lib/linalg.cc index f973696da49aa..1269702f28f91 100644 --- a/paddle/pten/hapi/lib/linalg.cc +++ b/paddle/pten/hapi/lib/linalg.cc @@ -56,7 +56,6 @@ Tensor dot(const Tensor& x, const Tensor& y) { std::make_shared(out_meta, pten::TensorStatus()); kernel_context.EmplaceBackOutput(dense_out); out.set_impl(dense_out); - out.set_backend_set(x.backend_set()); // 6. Call kernel kernel(&kernel_context); diff --git a/paddle/pten/hapi/lib/manipulation.cc b/paddle/pten/hapi/lib/manipulation.cc index c7c7f99f91afd..4b9b66b9df0bd 100644 --- a/paddle/pten/hapi/lib/manipulation.cc +++ b/paddle/pten/hapi/lib/manipulation.cc @@ -50,7 +50,6 @@ Tensor flatten(const Tensor& x, int start_axis, int stop_axis) { std::make_shared(out_meta, pten::TensorStatus()); kernel_context.EmplaceBackOutput(dense_out); out.set_impl(dense_out); - out.set_backend_set(x.backend_set()); // 6. Call kernel kernel(&kernel_context); diff --git a/paddle/pten/hapi/lib/math.cc b/paddle/pten/hapi/lib/math.cc index 178eb5ac1c07d..851a9bc155cdd 100644 --- a/paddle/pten/hapi/lib/math.cc +++ b/paddle/pten/hapi/lib/math.cc @@ -50,7 +50,6 @@ Tensor mean(const Tensor& x) { std::make_shared(out_meta, pten::TensorStatus()); kernel_context.EmplaceBackOutput(dense_out); out.set_impl(dense_out); - out.set_backend_set(x.backend_set()); // 6. Call kernel kernel(&kernel_context);