Skip to content

Commit

Permalink
[Unify Tensors PR #7] Merged LoDTensor with Tensor, test=allcases (Pa…
Browse files Browse the repository at this point in the history
…ddlePaddle#38880)

* Merged LoDTensor with Tensor,test=allcases

* Patched python level LoDTensor

* Fixed example code failure

* Polished function names, removed duplicated forward declarations
  • Loading branch information
jim19930609 authored Jan 15, 2022
1 parent a887914 commit 88966b2
Show file tree
Hide file tree
Showing 54 changed files with 203 additions and 343 deletions.
2 changes: 1 addition & 1 deletion paddle/fluid/distributed/fleet.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ limitations under the License. */

namespace paddle {
namespace framework {
class LoDTensor;
class Tensor;
class Scope;
class SelectedRows;
class Variable;
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/distributed/service/brpc_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ limitations under the License. */
namespace paddle {
namespace framework {
class Variable;
class LoDTensor;
class Tensor;
} // namespace framework
} // namespace paddle

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class PSClient;
class PSServer;
} // namespace distributed
namespace framework {
class LoDTensor;
class Tensor;
class Variable;
} // namespace framework
} // namespace paddle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class PSClient;
class PSServer;
} // namespace distributed
namespace framework {
class LoDTensor;
class Tensor;
class Variable;
} // namespace framework
} // namespace paddle
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/framework/data_feed.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ DECLARE_bool(enable_slotrecord_reset_shrink);
namespace paddle {
namespace framework {
class DataFeedDesc;
class LoDTensor;
class Tensor;
class Scope;
class Variable;
} // namespace framework
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/framework/details/fetch_async_op_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

namespace paddle {
namespace framework {
class LoDTensor;
class Tensor;

namespace ir {
class Node;
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/framework/details/variable_visitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

namespace paddle {
namespace framework {
class LoDTensor;
class Tensor;
class Variable;
} // namespace framework
} // namespace paddle
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/framework/device_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License. */
namespace paddle {
namespace framework {

class LoDTensor;
class Tensor;
class Scope;

void DeviceWorker::SetRootScope(Scope* root_scope) { root_scope_ = root_scope; }
Expand Down
3 changes: 1 addition & 2 deletions paddle/fluid/framework/device_worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ limitations under the License. */

namespace paddle {
namespace framework {
class LoDTensor;
class Tensor;
class ProgramDesc;
class Scope;
class Tensor;
} // namespace framework
namespace platform {
class DeviceContext;
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/framework/downpour_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License. */

namespace paddle {
namespace framework {
class LoDTensor;
class Tensor;
class Variable;
} // namespace framework
} // namespace paddle
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/framework/feed_fetch_method.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ limitations under the License. */
namespace paddle {
namespace framework {

class LoDTensor;
class Tensor;
class Variable;

void SetFeedVariable(Scope* scope, const LoDTensor& input,
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/framework/feed_fetch_method.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ limitations under the License. */
namespace paddle {
namespace framework {

class LoDTensor;
class Tensor;
class Scope;

void SetFeedVariable(Scope* scope, const LoDTensor& input,
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/framework/ir/conv_affine_channel_fuse_pass.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

namespace paddle {
namespace framework {
class LoDTensor;
class Tensor;
class Scope;
} // namespace framework
} // namespace paddle
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/framework/ir/conv_bn_fuse_pass.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

namespace paddle {
namespace framework {
class LoDTensor;
class Tensor;
class Scope;
} // namespace framework
} // namespace paddle
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/framework/ir/delete_dropout_op_pass.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace paddle {
namespace framework {
class LoDTensor;
class Tensor;
} // namespace framework
} // namespace paddle

Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/framework/ir/delete_quant_dequant_op_pass.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

namespace paddle {
namespace framework {
class LoDTensor;
class Tensor;
} // namespace framework
} // namespace paddle

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ limitations under the License. */

namespace paddle {
namespace framework {
class LoDTensor;
class Tensor;
} // namespace framework
} // namespace paddle

Expand Down
28 changes: 0 additions & 28 deletions paddle/fluid/framework/lod_tensor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,6 @@ class DeviceContext;
namespace paddle {
namespace framework {

std::ostream &operator<<(std::ostream &os, const LoD &lod) {
os << "{";
for (auto &v : lod) {
os << "{";
bool is_first = true;
for (auto &i : v) {
if (is_first) {
os << i;
is_first = false;
} else {
os << ", " << i;
}
}
os << "}";
}
os << "}";

return os;
}

std::ostream &operator<<(std::ostream &os, const LoDTensor &t) {
if (t.lod().size() > 0) {
os << " - lod: " << t.lod() << "\n";
}
os << static_cast<Tensor>(t);
return os;
}

std::string LoDToString(const LoD &lod) {
std::ostringstream stream;
stream << lod;
Expand Down
24 changes: 2 additions & 22 deletions paddle/fluid/framework/lod_tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ limitations under the License. */
#include "paddle/fluid/platform/place.h"

namespace paddle {
namespace framework {
class LoDTensor;
} // namespace framework
namespace platform {
class DeviceContext;
} // namespace platform
Expand All @@ -39,6 +36,8 @@ class DeviceContext;
namespace paddle {
namespace framework {

using LoDTensor = paddle::framework::Tensor;

/*
* LoD is short for Level of Details.
*
Expand All @@ -56,9 +55,6 @@ namespace framework {
*/
using LoD = std::vector<Vector<size_t>>;

std::ostream& operator<<(std::ostream& os, const LoD& lod);
std::ostream& operator<<(std::ostream& os, const LoDTensor& t);

std::string LoDToString(const LoD& lod);

LoD SliceInLevel(const LoD& in, size_t level, size_t elem_begin,
Expand Down Expand Up @@ -102,22 +98,6 @@ bool CheckLoD(const LoD& in, int tensor_height = -1);
*/
bool CheckAbsLoD(const LoD& in, int tensor_height = -1);

/*
* LoDTensor (Level of details Tensor)
* see https://en.wikipedia.org/wiki/Level_of_details for reference.
*/
class LoDTensor : public Tensor {
public:
using Tensor::Tensor;

// Split LoDTensor and copy to each place specified in places.
std::vector<LoDTensor> SplitLoDTensor(
const std::vector<platform::Place> places) const;

void MergeLoDTensor(const std::vector<const LoDTensor*>& lod_tensors,
platform::Place place);
};

/*
* Expand the `source` to fit the LoD of `lod`. For example, a `source`
* LoDTensor is
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/framework/naive_executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace framework {
* Simple, intuitive and effective. Only single thread is supported, and
* currently designed for inference.
*/
class LoDTensor;
class Tensor;
class ProgramDesc;
class Scope;

Expand Down
12 changes: 1 addition & 11 deletions paddle/fluid/framework/operator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ limitations under the License. */

namespace paddle {
namespace framework {
class LoDTensor;
class Tensor;
} // namespace framework
} // namespace paddle
#ifdef PADDLE_WITH_XPU
Expand Down Expand Up @@ -555,11 +555,6 @@ Variable* ExecutionContext::OutputVar(const std::string& name) const {
return it->second.empty() ? nullptr : it->second[0];
}

template <>
const Tensor* ExecutionContext::Input<Tensor>(const std::string& name) const {
return Input<LoDTensor>(name);
}

template <>
const std::vector<const Tensor*> ExecutionContext::MultiInput<Tensor>(
const std::string& name) const {
Expand All @@ -584,11 +579,6 @@ const std::vector<const Tensor*> ExecutionContext::MultiInput<Tensor>(
return res;
}

template <>
Tensor* ExecutionContext::Output<Tensor>(const std::string& name) const {
return Output<LoDTensor>(name);
}

template <>
std::vector<Tensor*> ExecutionContext::MultiOutput<Tensor>(
const std::string& name) const {
Expand Down
6 changes: 0 additions & 6 deletions paddle/fluid/framework/operator.h
Original file line number Diff line number Diff line change
Expand Up @@ -479,16 +479,10 @@ class ExecutionArgumentMappingContext : public pten::ArgumentMappingContext {
const ExecutionContext& ctx_;
};

template <>
const Tensor* ExecutionContext::Input<Tensor>(const std::string& name) const;

template <>
const std::vector<const Tensor*> ExecutionContext::MultiInput<Tensor>(
const std::string& name) const;

template <>
Tensor* ExecutionContext::Output<Tensor>(const std::string& name) const;

template <>
std::vector<Tensor*> ExecutionContext::MultiOutput<Tensor>(
const std::string& name) const;
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/framework/pull_dense_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License. */
namespace paddle {
namespace framework {

class LoDTensor;
class Tensor;
class Scope;
class Variable;

Expand Down
9 changes: 8 additions & 1 deletion paddle/fluid/framework/tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace paddle {

namespace framework {

class LoDTensor;
using LoD = std::vector<paddle::framework::Vector<size_t>>;

/*
NOTE(liym27): [ What is TensorInplaceVersion used for? ]
Expand Down Expand Up @@ -74,6 +74,13 @@ class Tensor : public pten::DenseTensor {
using DenseTensor = pten::DenseTensor;
using DenseTensor::DenseTensor;

// Split Tensor and copy to each place specified in places.
std::vector<Tensor> SplitLoDTensor(
const std::vector<platform::Place> places) const;

void MergeLoDTensor(const std::vector<const Tensor*>& lod_tensors,
platform::Place place);

/*! The internal of two tensors share the same memory block. */
Tensor& ShareDataWith(const Tensor& src);

Expand Down
24 changes: 24 additions & 0 deletions paddle/fluid/framework/tensor_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1428,7 +1428,31 @@ std::ostream& print_tensor<paddle::platform::complex<double>>(
return os;
}

std::ostream& operator<<(std::ostream& os, const LoD& lod) {
os << "{";
for (auto& v : lod) {
os << "{";
bool is_first = true;
for (auto& i : v) {
if (is_first) {
os << i;
is_first = false;
} else {
os << ", " << i;
}
}
os << "}";
}
os << "}";

return os;
}

std::ostream& operator<<(std::ostream& os, const Tensor& t) {
if (t.lod().size() > 0) {
os << " - lod: " << t.lod() << "\n";
}

os << " - place: " << t.place() << "\n";
os << " - shape: [" << t.dims() << "]\n";
os << " - layout: " << DataLayoutToString(t.layout()) << "\n";
Expand Down
4 changes: 3 additions & 1 deletion paddle/fluid/framework/tensor_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ limitations under the License. */
namespace paddle {
namespace framework {

std::ostream& operator<<(std::ostream& os, const LoD& lod);
std::ostream& operator<<(std::ostream& os, const Tensor& t);

class PrintOptions {
public:
static PrintOptions& Instance() {
Expand Down Expand Up @@ -494,6 +497,5 @@ inline void TensorToVector(const Tensor& src, std::vector<bool>* dst) {
delete[] array;
}

std::ostream& operator<<(std::ostream& os, const Tensor& t);
} // namespace framework
} // namespace paddle
2 changes: 1 addition & 1 deletion paddle/fluid/framework/trainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace paddle {
namespace framework {

class Dataset;
class LoDTensor;
class Tensor;
class ProgramDesc;
class PullDenseWorker;
class Scope;
Expand Down
Loading

0 comments on commit 88966b2

Please sign in to comment.