Skip to content

Commit 5c73a6e

Browse files
authored
[Unify Tensors PR #5] framework::Tensor inherits from DenseTensor,test=allcases (PaddlePaddle#38632)
* Added shared_ptr<Allocation> member & corresponding interfaces to Storage * Removed original pten::Allocation from Storage and adjusted the interfaces accordingly * Fixed issues with storage offset * Used place to malloc allocation for TensorStorage * [Unify Tensors PR #3]Ported framework::Tensor interfaces to pten::DenseTensor * Fixed issues with place * Added comments * Moved mutable_data with stream argument to DenseTensor * Added set_offset interface * Fixed CI issues,test=allcases * [Unify Tensors PR #4] Port LoDTensor interfaces to DenseTensor * Removed friend class EigenTensor/EigenMatrix/EigenVector from Tensor * Modified framework::Tensor to inherit from DenseTensor * Reverted changes too pten_layout() interface * Removed friend classes * Rearranged cfunction calls from tensor.data<void>() to tensor.data() * Fixed CI issues * Fixed lite issues * Fixed data() interface issues,test=allcases * Resolved IsInitialized() issues * Fixed ResetHolder() issues * Fixed MKLDNN & Storage issues * Resolved ShareBufferWith() issues * Fixed LoD issues
1 parent 046553c commit 5c73a6e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+251
-731
lines changed

paddle/fluid/distributed/service/brpc_utils.cc

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -103,19 +103,17 @@ void SerializeLodTensor(framework::Variable* var,
103103
if (platform::is_cpu_place(tensor->place())) {
104104
auto data_len = tensor->numel() * framework::SizeOfType(tensor->type());
105105
iobuf->append(reinterpret_cast<const char*>(&data_len), 8);
106-
iobuf->append(reinterpret_cast<const char*>(tensor->data<void>()),
107-
data_len);
106+
iobuf->append(reinterpret_cast<const char*>(tensor->data()), data_len);
108107
} else {
109108
#ifdef PADDLE_WITH_CUDA
110109
char* temp_ptr =
111110
new char[tensor->numel() * framework::SizeOfType(tensor->type())];
112111
auto stream =
113112
reinterpret_cast<const platform::CUDADeviceContext&>(ctx).stream();
114-
memory::Copy(platform::CPUPlace(), temp_ptr,
115-
BOOST_GET_CONST(platform::CUDAPlace, tensor->place()),
116-
tensor->data<void>(),
117-
tensor->numel() * framework::SizeOfType(tensor->type()),
118-
stream);
113+
memory::Copy(
114+
platform::CPUPlace(), temp_ptr,
115+
BOOST_GET_CONST(platform::CUDAPlace, tensor->place()), tensor->data(),
116+
tensor->numel() * framework::SizeOfType(tensor->type()), stream);
119117
auto data_len = tensor->numel() * framework::SizeOfType(tensor->type());
120118
iobuf->append(reinterpret_cast<const char*>(&data_len), 8);
121119
iobuf->append(reinterpret_cast<const char*>(temp_ptr), data_len);
@@ -147,19 +145,17 @@ void SerializeSelectedRows(framework::Variable* var,
147145
if (platform::is_cpu_place(tensor->place())) {
148146
auto data_len = tensor->numel() * framework::SizeOfType(tensor->type());
149147
iobuf->append(reinterpret_cast<const char*>(&data_len), 8);
150-
iobuf->append(reinterpret_cast<const char*>(tensor->data<void>()),
151-
data_len);
148+
iobuf->append(reinterpret_cast<const char*>(tensor->data()), data_len);
152149
} else {
153150
#ifdef PADDLE_WITH_CUDA
154151
char* temp_ptr =
155152
new char[tensor->numel() * framework::SizeOfType(tensor->type())];
156153
auto stream =
157154
reinterpret_cast<const platform::CUDADeviceContext&>(ctx).stream();
158-
memory::Copy(platform::CPUPlace(), temp_ptr,
159-
BOOST_GET_CONST(platform::CUDAPlace, tensor->place()),
160-
tensor->data<void>(),
161-
tensor->numel() * framework::SizeOfType(tensor->type()),
162-
stream);
155+
memory::Copy(
156+
platform::CPUPlace(), temp_ptr,
157+
BOOST_GET_CONST(platform::CUDAPlace, tensor->place()), tensor->data(),
158+
tensor->numel() * framework::SizeOfType(tensor->type()), stream);
163159
auto data_len = tensor->numel() * framework::SizeOfType(tensor->type());
164160
iobuf->append(reinterpret_cast<const char*>(&data_len), 8);
165161
iobuf->append(reinterpret_cast<const char*>(temp_ptr), data_len);

paddle/fluid/distributed/service/heter_client.cc

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ int GetMicroId(const platform::DeviceContext& ctx,
3434
auto micro_id = -1;
3535
auto* tensor = var->GetMutable<framework::LoDTensor>();
3636
if (platform::is_cpu_place(tensor->place())) {
37-
auto data = reinterpret_cast<const float*>(tensor->data<void>());
37+
auto data = reinterpret_cast<const float*>(tensor->data());
3838
micro_id = static_cast<int>(data[0]);
3939
} else {
4040
#ifdef PADDLE_WITH_CUDA
@@ -43,11 +43,10 @@ int GetMicroId(const platform::DeviceContext& ctx,
4343
char* temp_ptr = temp.data();
4444
auto stream =
4545
reinterpret_cast<const platform::CUDADeviceContext&>(ctx).stream();
46-
memory::Copy(platform::CPUPlace(), temp_ptr,
47-
BOOST_GET_CONST(platform::CUDAPlace, tensor->place()),
48-
tensor->data<void>(),
49-
tensor->numel() * framework::SizeOfType(tensor->type()),
50-
stream);
46+
memory::Copy(
47+
platform::CPUPlace(), temp_ptr,
48+
BOOST_GET_CONST(platform::CUDAPlace, tensor->place()), tensor->data(),
49+
tensor->numel() * framework::SizeOfType(tensor->type()), stream);
5150
float* temp_ptr_float = reinterpret_cast<float*>(temp_ptr);
5251
micro_id = static_cast<int>(temp_ptr_float[0]);
5352
#endif

paddle/fluid/distributed/service/heter_server.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ class RequestSendAndRecvHandler final : public HeterRequestHandler {
240240
platform::errors::InvalidArgument(
241241
"Not find variable microbatch_id in scope."));
242242
auto* tensor = var->GetMutable<framework::LoDTensor>();
243-
auto data = reinterpret_cast<const float*>(tensor->data<void>());
243+
auto data = reinterpret_cast<const float*>(tensor->data());
244244
auto micro_id = static_cast<int>(data[0]);
245245

246246
int minibatch_index = micro_id / 10;

paddle/fluid/framework/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ endif()
9191
cc_test(copy_same_tensor_test SRCS copy_same_tensor_test.cc DEPS tensor)
9292

9393
cc_test(eigen_test SRCS eigen_test.cc DEPS tensor)
94-
cc_library(mixed_vector SRCS mixed_vector.cc DEPS device_context)
94+
cc_library(mixed_vector SRCS mixed_vector.cc DEPS device_context place memory)
9595

9696
if(WITH_GPU)
9797
nv_test(mixed_vector_test SRCS mixed_vector_test.cc mixed_vector_test.cu DEPS mixed_vector place memory device_context tensor)

paddle/fluid/framework/copy_same_tensor_test.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ static bool CopySameTensorTestMain(const DDim &dims,
7777
TensorCopySync(src_tensor, platform::CPUPlace(), &dst_cpu_tensor);
7878
}
7979

80-
const void *ground_truth_ptr = src_cpu_tensor.data<void>();
81-
const void *result_ptr = dst_cpu_tensor.data<void>();
80+
const void *ground_truth_ptr = src_cpu_tensor.data();
81+
const void *result_ptr = dst_cpu_tensor.data();
8282
size_t byte_num = product(dims) * sizeof(T);
8383
return std::memcmp(ground_truth_ptr, result_ptr, byte_num) == 0;
8484
}

paddle/fluid/framework/data_transform.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ void TransformData(const OpKernelType &expected_kernel_type,
4545
Tensor out;
4646
const DataLayout lin = kernel_type_for_var.data_layout_;
4747
const DataLayout lout = expected_kernel_type.data_layout_;
48-
4948
// do layout transform
5049
if (NeedTransformLayout(lout, lin)) {
5150
#ifdef PADDLE_WITH_MKLDNN

paddle/fluid/framework/details/all_reduce_op_handle.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ void AllReduceOpHandle::AllReduceImpl(
153153
"The place type of tensors of the same variable "
154154
"in different local scopes should be equal."));
155155

156-
lod_tensor_data.emplace_back(lod_tensor.data<void>());
156+
lod_tensor_data.emplace_back(lod_tensor.data());
157157
places.emplace_back(lod_tensor.place());
158158

159159
VLOG(10) << "place:" << i << ", input_name:" << in_var_handles[i]->name()
@@ -225,7 +225,7 @@ void AllReduceOpHandle::AllReduceFunc(
225225
->GetMutable<LoDTensor>();
226226

227227
// Reduce All Tensor to trg in CPU
228-
ReduceBufferData func(lod_tensor_data, trg.data<void>(), numel);
228+
ReduceBufferData func(lod_tensor_data, trg.data(), numel);
229229
VisitDataType(trg.type(), func);
230230

231231
for (size_t i = 1; i < local_exec_scopes_.size(); ++i) {
@@ -235,9 +235,9 @@ void AllReduceOpHandle::AllReduceFunc(
235235

236236
size_t size = numel * SizeOfType(trg.type());
237237
RunAndRecordEvent(p, [&trg, var, p, size] {
238-
auto dst_ptr = var->GetMutable<framework::LoDTensor>()->data<void>();
238+
auto dst_ptr = var->GetMutable<framework::LoDTensor>()->data();
239239
platform::CPUPlace cpu_place;
240-
memory::Copy(cpu_place, dst_ptr, cpu_place, trg.data<void>(), size);
240+
memory::Copy(cpu_place, dst_ptr, cpu_place, trg.data(), size);
241241
});
242242
}
243243
}

paddle/fluid/framework/details/broadcast_op_handle.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ void BroadcastOpHandle::BroadcastOneVar(
101101

102102
void *send_recv_buffer = nullptr;
103103
if (root_id == dst_id) {
104-
send_recv_buffer = const_cast<void *>(in_tensor.data<void>());
104+
send_recv_buffer = const_cast<void *>(in_tensor.data());
105105
out_handle = out_var_handle;
106106
} else {
107107
send_recv_buffer = VariableVisitor::GetMutableTensor(out_var)
@@ -162,7 +162,7 @@ void BroadcastOpHandle::BroadcastOneVar(
162162

163163
void *send_recv_buffer = nullptr;
164164
if (root_id == dst_id) {
165-
send_recv_buffer = const_cast<void *>(in_tensor.data<void>());
165+
send_recv_buffer = const_cast<void *>(in_tensor.data());
166166
out_handle = out_var_handle;
167167
} else {
168168
send_recv_buffer = VariableVisitor::GetMutableTensor(out_var)

paddle/fluid/framework/details/fused_all_reduce_op_handle.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,17 +220,17 @@ void FusedAllReduceOpHandle::FusedAllReduceFunc(
220220
g_tensor.begin(), g_tensor.end(),
221221
[](const std::pair<std::string, const LoDTensor *> &grad1,
222222
const std::pair<std::string, const LoDTensor *> &grad2) -> bool {
223-
return grad1.second->data<void>() < grad2.second->data<void>();
223+
return grad1.second->data() < grad2.second->data();
224224
});
225225

226226
size_t size_of_dtype = framework::SizeOfType(dtype);
227227
for (size_t k = 1; k < g_tensor.size(); ++k) {
228-
const void *cur_address = g_tensor.at(k - 1).second->data<void>();
228+
const void *cur_address = g_tensor.at(k - 1).second->data();
229229
int64_t len = g_tensor.at(k - 1).second->numel();
230230
auto offset = platform::Alignment(len * size_of_dtype, places_[0]);
231231
void *infer_next_address = reinterpret_cast<void *>(
232232
reinterpret_cast<uintptr_t>(cur_address) + offset);
233-
const void *next_address = g_tensor.at(k).second->data<void>();
233+
const void *next_address = g_tensor.at(k).second->data();
234234

235235
VLOG(10) << string::Sprintf(
236236
"Input[%d](%s) address: 0X%02x, Input[%d](%s) address: 0X%02x, Infer "
@@ -267,7 +267,7 @@ void FusedAllReduceOpHandle::FusedAllReduceFunc(
267267
std::vector<const void *> lod_tensor_data;
268268
lod_tensor_data.reserve(place_num);
269269
for (size_t scope_idx = 0; scope_idx < place_num; ++scope_idx) {
270-
auto data = grads_tensor.at(scope_idx).at(0).second->data<void>();
270+
auto data = grads_tensor.at(scope_idx).at(0).second->data();
271271
lod_tensor_data.emplace_back(data);
272272
}
273273
std::vector<std::string> grad_var_names;

paddle/fluid/framework/details/reduce_op_handle.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ void ReduceOpHandle::RunImpl() {
159159
VisitDataType(lod_tensors[0]->type(), func);
160160

161161
auto trg = out_var->GetMutable<framework::LoDTensor>();
162-
if (reduce_sum_trg.data<void>() != trg->data<void>()) {
162+
if (reduce_sum_trg.data() != trg->data()) {
163163
TensorCopy(reduce_sum_trg, platform::CPUPlace(), trg);
164164
}
165165
}
@@ -181,7 +181,7 @@ void ReduceOpHandle::RunImpl() {
181181
int dev_id = BOOST_GET_CONST(platform::CUDAPlace, p).device;
182182
auto &nccl_ctx = nccl_ctxs_->at(dev_id);
183183

184-
void *buffer = const_cast<void *>(lod_tensor.data<void>());
184+
void *buffer = const_cast<void *>(lod_tensor.data());
185185
void *recvbuffer = nullptr;
186186
if (root_id == dev_id) {
187187
recvbuffer =
@@ -227,7 +227,7 @@ void ReduceOpHandle::RunImpl() {
227227
int dev_id = BOOST_GET_CONST(platform::XPUPlace, p).device;
228228
auto &bkcl_ctx = bkcl_ctxs_->at(dev_id);
229229

230-
void *buffer = const_cast<void *>(lod_tensor.data<void>());
230+
void *buffer = const_cast<void *>(lod_tensor.data());
231231
void *recvbuffer = nullptr;
232232
if (root_id == dev_id) {
233233
recvbuffer =

0 commit comments

Comments
 (0)