未验证 提交 ee9ea48d 编写于 作者: L Leo Chen 提交者: GitHub

clean includes of tensor.h (#44928)

* clean tensor.h

* fix gather_nd
上级 3f234db0
......@@ -15,6 +15,8 @@
#include "paddle/fluid/eager/nan_inf_utils.h"
#include "paddle/fluid/framework/details/nan_inf_utils_detail.h"
#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/core/selected_rows.h"
......
......@@ -83,26 +83,27 @@ if(WITH_GPU)
windows_symbolic(tensor_util SRCS tensor_util.cu)
nv_library(
tensor
SRCS tensor.cc .tensor_util.cu
SRCS .tensor_util.cu
DEPS place memory data_type device_context dense_tensor)
add_dependencies(tensor tensor_util)
else()
nv_library(
tensor
SRCS tensor.cc tensor_util.cu
DEPS place memory data_type device_context profiler dense_tensor)
SRCS tensor_util.cu
DEPS place memory data_type device_context dense_tensor)
endif()
elseif(WITH_ROCM)
hip_library(
tensor
SRCS tensor.cc tensor_util.cu
DEPS place memory data_type device_context profiler dense_tensor)
SRCS tensor_util.cu
DEPS place memory data_type device_context dense_tensor)
else()
cc_library(
tensor
SRCS tensor.cc tensor_util.cc
DEPS place memory data_type device_context profiler dense_tensor)
SRCS tensor_util.cc
DEPS place memory data_type device_context dense_tensor)
endif()
# target_link(tensor profiler)
cc_test(
tensor_test
......@@ -1167,9 +1168,6 @@ cc_library(
op_meta_info
phi_api)
#cc_binary(test_executor SRCS test_executor.cc DEPS executor op_registry ${GLOB_OP_LIB} ${GLOB_OPERATOR_DEPS} )
#cc_binary(new_executor SRCS new_exec_test.cc DEPS operator op_registry executor ${GLOB_OP_LIB} ${GLOB_OPERATOR_DEPS} profiler)
set(FLUID_FRAMEWORK_MODULES
proto_desc
memory
......
......@@ -17,12 +17,9 @@
#include <string>
#include "paddle/fluid/framework/tensor.h"
#include "paddle/fluid/platform/complex.h"
#include "paddle/fluid/platform/place.h"
namespace phi {
class DenseTensor;
} // namespace phi
namespace paddle {
namespace framework {
namespace details {
......
......@@ -17,6 +17,8 @@
#include <glog/logging.h>
#include <gtest/gtest.h>
#include "paddle/fluid/platform/device/gpu/gpu_info.h"
namespace paddle {
namespace framework {
......
/* Copyright (c) 2016 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/framework/tensor.h"
DECLARE_bool(use_stream_safe_cuda_allocator);
namespace paddle {
namespace memory {
namespace allocation {
class Allocation;
} // namespace allocation
} // namespace memory
} // namespace paddle
namespace paddle {
namespace framework {} // namespace framework
} // namespace paddle
......@@ -14,63 +14,15 @@ limitations under the License. */
#pragma once
#include <cstdint>
#include <cstring>
#include <memory>
#include <typeindex>
#include <utility>
#include <vector>
#include "paddle/fluid/framework/data_layout.h"
#include "paddle/fluid/framework/framework.pb.h"
#include "paddle/fluid/framework/data_type.h"
#include "paddle/fluid/framework/mixed_vector.h"
#include "paddle/fluid/memory/memory.h"
#include "paddle/fluid/platform/device_context.h"
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/platform/place.h"
#include "paddle/phi/core/ddim.h"
#include "paddle/phi/core/dense_tensor.h"
namespace paddle {
namespace framework {
using LoD = std::vector<paddle::framework::Vector<size_t>>;
/*
NOTE(liym27): [ What is TensorInplaceVersion used for? ]
TensorInplaceVersion is a version counter and every Tensor has a version
counter. It's used to check whether an inplace operation will result in an
incorrect gradient calculation. Version is incremented when the data of the
Variable is modified in place.
- Question: In what scenarios will version counters be shared?
- Answer: When two Variables/VarBases share the same C++ Tensor(its Allocation
may change), both of them share the same version counter. For examples:
1. `z = paddle.assign(input=x, output=y)`, `z` shares the same version counter
of `y` because z and y is the same VarBase;
2. `y = x.detach()`, `y` shares the same version counter of `x`.
- Question: In what scenarios will version counters NOT be shared?
- Answer: Replacing a `Variable`'s data by calling `Tensor::ShareDataWith(...)`
or `Tensor::ShareBufferWith(...)`. Because they share the same Allocation but
not framework::Tensor.
- Question: Why put the inplace_version_counter_ in framework::Tensor instead
of Allocation or Variable?
- Answer:
1. Tensor can call ResetHolder() to reset the corresponding Allocation so that
the inplace_version_counter_ changes if it's in Allocation, which will lead to
confusing information about inplace version.
2. If inplace_version_counter_ is in Variable, different VariableWrappers
should be able to share the same Variable. However, a VariableWrapper hold a
Variable object but not a pointer.
*/
using Tensor = phi::DenseTensor;
} // namespace framework
} // namespace paddle
#include "paddle/fluid/framework/tensor_impl.h"
/* Copyright (c) 2016 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 "paddle/fluid/framework/data_type.h"
#include "paddle/fluid/memory/memcpy.h"
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/platform/float16.h"
namespace paddle {
namespace framework {
inline Tensor ReshapeToMatrix(const Tensor& src, int num_col_dims) {
int rank = src.dims().size();
PADDLE_ENFORCE_GE(
rank,
2,
platform::errors::InvalidArgument(
"'ReshapeToMatrix()' is only used for flatten high rank "
"tensors to matrixs. The dimensions of Tensor must be "
"greater or equal than 2. "
"But received dimensions of Tensor is %d",
rank));
if (rank == 2) {
return src;
}
Tensor res;
res.ShareDataWith(src);
res.Resize(phi::flatten_to_2d(src.dims(), num_col_dims));
return res;
}
} // namespace framework
} // namespace paddle
......@@ -13,6 +13,7 @@
// limitations under the License.
#include "paddle/fluid/framework/tensor.h"
#include "paddle/fluid/framework/tensor_util.h"
#include <gtest/gtest.h>
......
......@@ -34,6 +34,7 @@ limitations under the License. */
#include "paddle/fluid/platform/device/mlu/device_context.h"
#endif
#include "paddle/fluid/memory/memory.h"
#include "paddle/phi/core/dense_tensor.h"
namespace paddle {
......@@ -580,6 +581,26 @@ inline void TensorToVector(const Tensor& src, std::vector<bool>* dst) {
std::ostream& operator<<(std::ostream& os, const LoD& lod);
inline Tensor ReshapeToMatrix(const Tensor& src, int num_col_dims) {
int rank = src.dims().size();
PADDLE_ENFORCE_GE(
rank,
2,
platform::errors::InvalidArgument(
"'ReshapeToMatrix()' is only used for flatten high rank "
"tensors to matrixs. The dimensions of Tensor must be "
"greater or equal than 2. "
"But received dimensions of Tensor is %d",
rank));
if (rank == 2) {
return src;
}
Tensor res;
res.ShareDataWith(src);
res.Resize(phi::flatten_to_2d(src.dims(), num_col_dims));
return res;
}
} // namespace framework
} // namespace paddle
......
......@@ -23,6 +23,7 @@
#include "NvInferRuntimeCommon.h"
#include "paddle/fluid/inference/tensorrt/plugin/gather_nd_op_plugin.h"
#include "paddle/fluid/platform/place.h"
#include "paddle/phi/backends/gpu/gpu_helper.h"
namespace paddle {
namespace inference {
......
......@@ -13,6 +13,8 @@ See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/fluid/framework/tensor.h"
#include "paddle/fluid/platform/device_context.h"
#include "paddle/fluid/platform/float16.h"
#pragma once
......
......@@ -17,6 +17,7 @@ limitations under the License. */
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/tensor.h"
#include "paddle/fluid/platform/float16.h"
#include "paddle/phi/core/hostdevice.h"
namespace paddle {
......
......@@ -20,6 +20,7 @@ limitations under the License. */
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/tensor.h"
#include "paddle/fluid/operators/math/sampler.h"
#include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/core/ddim.h"
namespace paddle {
......
......@@ -19,7 +19,11 @@ limitations under the License. */
#include "paddle/fluid/framework/tensor.h"
#include "paddle/fluid/operators/jit/kernels.h"
#include "paddle/fluid/operators/math/cpu_vec.h"
#include "paddle/fluid/platform/bfloat16.h"
#include "paddle/fluid/platform/cpu_info.h"
#include "paddle/fluid/platform/float16.h"
#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/backends/gpu/gpu_context.h"
namespace paddle {
namespace operators {
......
......@@ -14,6 +14,7 @@ limitations under the License. */
#pragma once
#include "paddle/fluid/framework/tensor.h"
#include "paddle/fluid/platform/device_context.h"
namespace paddle {
namespace operators {
......
......@@ -22,7 +22,7 @@ limitations under the License. */
/* @jim19930609: Move to MKLDNN_Tensor in the future
*/
#ifdef PADDLE_WITH_MKLDNN
#include "dnnl.hpp"
#include "dnnl.hpp" // NOLINT
#endif
namespace phi {
......@@ -174,6 +174,36 @@ class DenseTensor : public TensorBase,
/* Temporarily put InplaceVersion inside DenseTensor.
Will move to AutogradMeta as soon as we switch to Eager Dygraph.
*/
/*
NOTE(liym27): [ What is TensorInplaceVersion used for? ]
TensorInplaceVersion is a version counter and every Tensor has a version
counter. It's used to check whether an inplace operation will result in an
incorrect gradient calculation. Version is incremented when the data of the
Variable is modified in place.
- Question: In what scenarios will version counters be shared?
- Answer: When two Variables/VarBases share the same C++ Tensor(its Allocation
may change), both of them share the same version counter. For examples:
1. `z = paddle.assign(input=x, output=y)`, `z` shares the same version
counter of `y` because z and y is the same VarBase;
2. `y = x.detach()`, `y` shares the same version counter of `x`.
- Question: In what scenarios will version counters NOT be shared?
- Answer: Replacing a `Variable`'s data by calling
`Tensor::ShareDataWith(...)` or `Tensor::ShareBufferWith(...)`. Because they
share the same Allocation but not framework::Tensor.
- Question: Why put the inplace_version_counter_ in framework::Tensor instead
of Allocation or Variable?
- Answer:
1. Tensor can call ResetHolder() to reset the corresponding Allocation so
that the inplace_version_counter_ changes if it's in Allocation, which will
lead to confusing information about inplace version.
2. If inplace_version_counter_ is in Variable, different VariableWrappers
should be able to share the same Variable. However, a VariableWrapper hold a
Variable object but not a pointer.
*/
class InplaceVersion {
public:
bool IsUnique() const { return inplace_version_ == 0; }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册