Skip to content

Commit

Permalink
remove tensor signature and backend set member
Browse files Browse the repository at this point in the history
  • Loading branch information
chenwhql committed Oct 22, 2021
1 parent 252fb79 commit 19b1095
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 85 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/operators/mean_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ using EigenVector = framework::EigenVector<T, MajorType, IndexType>;
* 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 <typename DeviceContext, typename T>
Expand Down
4 changes: 2 additions & 2 deletions paddle/pten/common/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions paddle/pten/hapi/include/backend_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
44 changes: 15 additions & 29 deletions paddle/pten/hapi/include/tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,17 @@ limitations under the License. */
#include <utility>

#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
Expand All @@ -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
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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<AutogradMetaInterface> autograd_meta) {
void set_autograd_meta(std::shared_ptr<AbstractAutogradMeta> autograd_meta) {
autograd_meta_ = std::move(autograd_meta);
}

Expand Down Expand Up @@ -244,7 +234,7 @@ class Tensor final {
std::shared_ptr<pten::TensorBase> impl_;

/**
* [ Why need abstract AutogradMetaInterface here? ]
* [ Why need abstract AbstractAutogradMeta here? ]
*
* Dynamic graphs need to hold backward information
*
Expand All @@ -254,17 +244,13 @@ class Tensor final {
* information, not Tensor data description-related information.
* 2. Kernel calculation does not require AutogradMeta.
*/
std::shared_ptr<AutogradMetaInterface> autograd_meta_{nullptr};
std::shared_ptr<AbstractAutogradMeta> 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<TensorSignature> signature_{nullptr};
std::string name_;
};

} // namespace experimental
Expand Down
45 changes: 0 additions & 45 deletions paddle/pten/hapi/include/tensor_signature.h

This file was deleted.

1 change: 0 additions & 1 deletion paddle/pten/hapi/lib/creation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ Tensor full_like(const Tensor& x,
std::make_shared<pten::DenseTensor>(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);
Expand Down
16 changes: 15 additions & 1 deletion paddle/pten/hapi/lib/kernel_dispatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -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? ]
Expand All @@ -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;
Expand Down Expand Up @@ -102,7 +116,7 @@ struct KernelKeyParser : ArgsIterator<KernelKeyParser> {
// 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();
Expand Down
1 change: 0 additions & 1 deletion paddle/pten/hapi/lib/linalg.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ Tensor dot(const Tensor& x, const Tensor& y) {
std::make_shared<pten::DenseTensor>(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);
Expand Down
1 change: 0 additions & 1 deletion paddle/pten/hapi/lib/manipulation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ Tensor flatten(const Tensor& x, int start_axis, int stop_axis) {
std::make_shared<pten::DenseTensor>(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);
Expand Down
1 change: 0 additions & 1 deletion paddle/pten/hapi/lib/math.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ Tensor mean(const Tensor& x) {
std::make_shared<pten::DenseTensor>(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);
Expand Down

1 comment on commit 19b1095

@paddle-bot-old
Copy link

@paddle-bot-old paddle-bot-old bot commented on 19b1095 Oct 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🕵️ CI failures summary

🔍 PR: #34425 Commit ID: 19b1095 contains failed CI.

🔹 Failed: PR-CI-APPROVAL

approve_failed
2021-10-22 12:06:29 正在保存至: “bk.txt”
2021-10-22 12:06:29 0K 100% 2.86M=0s
2021-10-22 12:06:29 2021-10-22 12:06:29 (2.86 MB/s) - 已保存 “bk.txt” [5/5])
2021-10-22 12:06:36 ****************
2021-10-22 12:06:37 0. You must have one RD (lanxianghit (Recommend), phlrain or luotao1) approval for changing the FLAGS, which manages the environment variables.
2021-10-22 12:06:37 1. You must have Dianhai approval for change 20+ files or add than 1000+ lines of content.
2021-10-22 12:06:37 2. You must have one RD (XiaoguangHu01,chenwhql,zhiqiu,Xreki,luotao1) approval for paddle/fluid/framework/operator.h, which manages the underlying code for fluid.
2021-10-22 12:06:37 3. You must have one RD (zhiqiu (Recommend) , phlrain) approval for the changes of paddle/fluid/pybind/op_function_generator.cc, which manages the logic of automatic generating op functions for dygraph.
2021-10-22 12:06:37 4. You must have one RD (XiaoguangHu01,chenwhql,zhiqiu,Xreki,luotao1) approval for the usage of const_cast.
2021-10-22 12:06:37 5. You must have one RD (Avin0323(Recommend) or zhouwei25 or wanghuancoder or luotao1) approval for modifying unity_build_rule.cmake which the rules of Unity Build.
2021-10-22 12:06:37 There are 6 approved errors.
2021-10-22 12:06:37 ****************
2021-10-22 12:06:37 + EXCODE=6
2021-10-22 12:06:37 + echo 'EXCODE: 6'
2021-10-22 12:06:37 EXCODE: 6
2021-10-22 12:06:37 + echo 'ipipe_log_param_EXCODE: 6'
2021-10-22 12:06:37 ipipe_log_param_EXCODE: 6
2021-10-22 12:06:37 + exit 6

Please sign in to comment.