forked from PaddlePaddle/Paddle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheager.cc
More file actions
1574 lines (1473 loc) · 62.4 KB
/
eager.cc
File metadata and controls
1574 lines (1473 loc) · 62.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* 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. */
// disable numpy compile error
#include "paddle/fluid/pybind/eager.h"
#include <Python.h>
#include <string>
#include <vector>
#include "paddle/fluid/eager/accumulation/accumulation_node.h"
#include "paddle/fluid/eager/api/all.h"
#include "paddle/fluid/eager/autograd_meta.h"
#include "paddle/fluid/eager/utils.h"
#include "paddle/fluid/framework/convert_utils.h"
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/pybind/eager_utils.h"
#include "paddle/phi/common/data_type.h"
#include "paddle/phi/core/compat/convert_utils.h"
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/core/memory/allocation/allocator.h"
#include "paddle/phi/core/memory/memcpy.h"
#include "pybind11/detail/internals.h"
#include "pybind11/numpy.h"
#include "pybind11/pybind11.h"
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#include "paddle/fluid/framework/phi_utils.h"
#include "paddle/fluid/framework/python_headers.h"
#include "paddle/fluid/pybind/exception.h"
#include "paddle/fluid/pybind/tensor_py.h"
#include "paddle/phi/api/lib/data_transform.h"
#include "paddle/phi/core/distributed/auto_parallel/dist_tensor.h"
#include "paddle/phi/core/distributed/auto_parallel/placement_types.h"
#include "paddle/phi/core/distributed/auto_parallel/process_mesh.h"
#include "paddle/phi/core/string_tensor.h"
using phi::distributed::DistTensor;
using phi::distributed::DistTensorMeta;
using phi::distributed::Placement;
using phi::distributed::Placements;
using phi::distributed::ProcessMesh;
using phi::distributed::TensorDistAttr;
using phi::distributed::auto_parallel::str_join;
namespace paddle::pybind {
namespace py = ::pybind11;
extern PyTypeObject* p_tensor_type;
extern PyTypeObject* p_string_tensor_type; // For StringTensor
extern PyTypeObject* g_vartype_pytype;
extern PyTypeObject* g_data_type_pytype;
extern PyTypeObject* g_framework_tensor_pytype;
PyObject* TensorNew(PyTypeObject* type, PyObject* args, PyObject* kwargs) {
PyObject* obj = type->tp_alloc(type, 0);
if (obj) {
auto v = reinterpret_cast<TensorObject*>(obj);
new (&(v->tensor)) paddle::Tensor();
}
return obj;
}
// TODO(jiabin): Overload this once we need more constructor in Python
void EmptyTensorInitializer(TensorObject* self,
const std::string& name,
const phi::Place& place,
bool persistable = false,
int stop_gradient = -1,
paddle::DataType dtype = paddle::DataType::FLOAT32,
const std::vector<int>& dims = {0},
framework::proto::VarType::Type var_type =
paddle::framework::proto::VarType::DENSE_TENSOR,
ProcessMesh* process_mesh = nullptr,
Placements* placements = nullptr) {
auto ddims = common::make_ddim(dims);
self->tensor.set_name(name);
auto autograd_meta = egr::EagerUtils::autograd_meta(&(self->tensor));
autograd_meta->SetPersistable(persistable);
if (stop_gradient != -1) {
autograd_meta->SetStopGradient(static_cast<bool>(stop_gradient));
}
if (process_mesh != nullptr) {
#ifdef PADDLE_WITH_DISTRIBUTE
VLOG(6) << "in EmptyTensorInitializer, create DistTensor";
self->tensor.set_impl(std::make_shared<DistTensor>());
#else
PADDLE_THROW(common::errors::Unavailable(
"The tensor-based initialization of (Dist)Tensor is not supported "
"in the current PaddlePaddle, please recompile and install "
"PaddlePaddle "
"with the option of `WITH_DISTRIBUTE=ON`."));
#endif
} else {
VLOG(6) << "in EmptyTensorInitializer, create DenseTensor";
if (var_type == paddle::framework::proto::VarType::DENSE_TENSOR) {
// TODO(jiabin): Maybe support LegacyLoD later
std::shared_ptr<phi::DenseTensor> dense_tensor = nullptr;
if (dims.size() == 1 && dims[0] == 0) {
std::shared_ptr<phi::Allocation> allocation_ptr = nullptr;
dense_tensor = std::make_shared<phi::DenseTensor>(
allocation_ptr, phi::DenseTensorMeta(dtype, ddims));
} else {
// TODO(dev): we need enhance check for ddims.
dense_tensor = std::make_shared<phi::DenseTensor>(
std::make_shared<phi::Allocation>(),
phi::DenseTensorMeta(dtype, ddims));
}
self->tensor.set_impl(dense_tensor);
} else if (var_type == paddle::framework::proto::VarType::SELECTED_ROWS) {
std::shared_ptr<phi::SelectedRows> tensor =
std::make_shared<phi::SelectedRows>();
self->tensor.set_impl(tensor);
}
}
if (!autograd_meta->GetMutableGradNode()) {
autograd_meta->SetGradNode(
std::make_shared<egr::GradNodeAccumulation>(self->tensor));
VLOG(3) << "Tensor(" << name
<< ") have not GradNode, add GradNodeAccumulation("
<< autograd_meta->GradNode() << ") for it.";
}
}
void EmptyStringTensorInitializer(TensorObject* self,
const std::string& name,
const phi::Place& place,
const std::vector<int>& dims = {}) {
auto ddims = common::make_ddim(dims);
self->tensor.set_name(name);
// Note(zhoushunjie): Only support CPUPlace when create StringTensor
auto actual_place = CPUPlace();
// Allocate memory
paddle::experimental::DefaultAllocator string_allocator(actual_place);
std::shared_ptr<phi::StringTensor> string_tensor =
std::make_shared<phi::StringTensor>(&string_allocator,
phi::StringTensorMeta{ddims});
if (common::product(ddims) > 0) {
string_tensor->mutable_data(actual_place);
}
self->tensor.set_impl(string_tensor);
}
void InitTensorWithNumpyValue(TensorObject* self,
const py::object& array,
const phi::Place& place,
bool zero_copy = false) {
PADDLE_ENFORCE_EQ(
self->tensor.defined(),
true,
common::errors::Unavailable(
"Calling InitTensorWithNumpyValue of Eager Tensor without "
"EmptyTensorInitializer is "
"forbidden. Please check your code and make sure you new a "
"eager tensor before init it with NumPy."));
phi::DenseTensor* impl_ptr =
static_cast<phi::DenseTensor*>(self->tensor.impl().get());
if (phi::is_cpu_place(place)) {
SetTensorFromPyArray<CPUPlace>(impl_ptr, array, place, zero_copy);
} else if (phi::is_xpu_place(place)) {
#if defined(PADDLE_WITH_XPU)
phi::backends::xpu::SetXPUDeviceId(place.device);
VLOG(4) << "CurrentDeviceId: "
<< phi::backends::xpu::GetXPUCurrentDeviceId() << " from "
<< static_cast<int>(place.device);
#else
PADDLE_THROW(common::errors::PreconditionNotMet(
"PaddlePaddle should compile with XPU if use XPUPlace."));
#endif
SetTensorFromPyArray<phi::XPUPlace>(impl_ptr, array, place, zero_copy);
} else if (phi::is_gpu_place(place)) {
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
phi::backends::gpu::SetDeviceId(place.device);
VLOG(4) << "CurrentDeviceId: " << phi::backends::gpu::GetCurrentDeviceId()
<< " from " << static_cast<int>(place.device);
#else
PADDLE_THROW(common::errors::PreconditionNotMet(
"PaddlePaddle should compile with GPU if use CUDAPlace."));
#endif
SetTensorFromPyArray<GPUPlace>(impl_ptr, array, place, zero_copy);
} else if (phi::is_cuda_pinned_place(place)) {
SetTensorFromPyArray<phi::GPUPinnedPlace>(
impl_ptr, array, place, zero_copy);
} else if (phi::is_xpu_pinned_place(place)) {
SetTensorFromPyArray<phi::XPUPinnedPlace>(
impl_ptr, array, place, zero_copy);
} else if (phi::is_custom_place(place)) {
#if defined(PADDLE_WITH_CUSTOM_DEVICE)
phi::DeviceManager::SetDevice(place);
VLOG(4) << "CurrentDeviceId: "
<< phi::DeviceManager::GetDevice(place.GetDeviceType()) << " from "
<< static_cast<int>(place.device);
#else
PADDLE_THROW(common::errors::PreconditionNotMet(
"PaddlePaddle should compile with CUSTOM_DEVICE if use CustomPlace."));
#endif
SetTensorFromPyArray<phi::CustomPlace>(impl_ptr, array, place, zero_copy);
} else {
PADDLE_THROW(common::errors::InvalidArgument(
"Place should be one of "
"CPUPlace/XPUPlace/CUDAPlace/"
"CUDAPinnedPlace/XPUPinnedPlace/CustomPlace"));
}
}
void InitStringTensorWithNumpyValue(TensorObject* self, const py::object& obj) {
PADDLE_ENFORCE_EQ(
self->tensor.defined(),
true,
common::errors::Fatal(
"Calling InitStringTensorWithNumpyValue of Eager StringTensor "
"without "
"EmptyStringTensorInitializer is "
"forbidden. Please check your code and make sure you new a "
"eager tensor before init it with NumPy."));
phi::StringTensor* impl_ptr =
static_cast<phi::StringTensor*>(self->tensor.impl().get());
phi::Place place = impl_ptr->place();
auto array = obj.cast<py::array>();
if (phi::is_cpu_place(place)) {
SetStringTensorFromPyArray<CPUPlace>(impl_ptr, array, place);
} else {
PADDLE_THROW(common::errors::InvalidArgument(
"StringTensor only support CPUPlace now, but receive %s",
place.DebugString()));
}
}
void InitDistTensorWithTensor(TensorObject* self,
const paddle::Tensor& src,
const phi::Place& place,
const std::string& name,
const ProcessMesh& process_mesh,
const Placements& placements) {
#ifdef PADDLE_WITH_DISTRIBUTE
PADDLE_ENFORCE_EQ(src.is_dense_tensor(),
true,
common::errors::InvalidArgument(
"DistTensor can only initialize by DenseTensor"));
self->tensor.set_name(name);
VLOG(4) << "Do TensorCopy from DenseTensor to DistTensor.";
if (place == src.place()) {
std::shared_ptr<phi::DenseTensor> tensor =
std::static_pointer_cast<phi::DenseTensor>(src.impl());
self->tensor.set_impl(
std::make_shared<DistTensor>(tensor, process_mesh, placements));
VLOG(4) << "Same place, do ShareDataWith for DistTensor.";
} else {
std::shared_ptr<phi::DenseTensor> tensor;
if (src.initialized()) {
tensor = std::static_pointer_cast<phi::DenseTensor>(
src.copy_to(place, true).impl());
} else {
// lazy init branch. The src tensor is on undefined place.
PADDLE_ENFORCE(
src.place().GetType() == phi::AllocationType::UNDEFINED,
common::errors::InvalidArgument("Only undefined place is support for "
"uninitialized input tensor."));
tensor = std::static_pointer_cast<phi::DenseTensor>(src.impl());
}
self->tensor.set_impl(
std::make_shared<DistTensor>(tensor, process_mesh, placements));
VLOG(4) << "Different place, do TensorCopy for DistTensor.";
}
if (src.get_autograd_meta()) {
egr::EagerUtils::autograd_meta(&(self->tensor))
->SetPersistable(
egr::EagerUtils::unsafe_autograd_meta(src)->Persistable());
} else {
egr::EagerUtils::autograd_meta(&(self->tensor))->SetPersistable(false);
}
#else
PADDLE_THROW(common::errors::Unavailable(
"The tensor-based initialization of (Dist)Tensor is not supported "
"in the current PaddlePaddle, please recompile and install PaddlePaddle "
"with the option of `WITH_DISTRIBUTE=ON`."));
#endif
}
void InitDistTensorWithTensor(TensorObject* self,
const paddle::Tensor& local_tensor,
const std::vector<int>& global_dims,
const phi::Place& place,
const std::string& name,
const ProcessMesh& process_mesh,
const Placements& placements) {
#ifdef PADDLE_WITH_DISTRIBUTE
PADDLE_ENFORCE_EQ(local_tensor.is_dense_tensor(),
true,
common::errors::InvalidArgument(
"DistTensor can only initialize by DenseTensor"));
self->tensor.set_name(name);
auto global_ddims = common::make_ddim(global_dims);
VLOG(4) << "Do TensorCopy from DenseTensor to DistTensor.";
if (place == local_tensor.place()) {
std::shared_ptr<phi::DenseTensor> tensor =
std::static_pointer_cast<phi::DenseTensor>(local_tensor.impl());
self->tensor.set_impl(std::make_shared<DistTensor>(
tensor, global_ddims, process_mesh, placements));
VLOG(4) << "Same place, do ShareDataWith for DistTensor.";
} else {
std::shared_ptr<phi::DenseTensor> tensor =
std::static_pointer_cast<phi::DenseTensor>(
local_tensor.copy_to(place, true).impl());
self->tensor.set_impl(std::make_shared<DistTensor>(
tensor, global_ddims, process_mesh, placements));
VLOG(4) << "Different place, do TensorCopy for DistTensor.";
}
if (local_tensor.get_autograd_meta()) {
egr::EagerUtils::autograd_meta(&(self->tensor))
->SetPersistable(
egr::EagerUtils::unsafe_autograd_meta(local_tensor)->Persistable());
} else {
egr::EagerUtils::autograd_meta(&(self->tensor))->SetPersistable(false);
}
#else
PADDLE_THROW(common::errors::Unavailable(
"The tensor-based initialization of (Dist)Tensor is not supported "
"in the current PaddlePaddle, please recompile and install PaddlePaddle "
"with the option of `WITH_DISTRIBUTE=ON`."));
#endif
}
void InitTensorWithTensor(TensorObject* self,
const paddle::Tensor& src,
const phi::Place& place,
const std::string& name) {
self->tensor.set_name(name);
if (place == src.place()) {
self->tensor.set_impl(src.impl());
VLOG(4) << "Same place, do ShareDataWith";
} else {
self->tensor.set_impl(src.copy_to(place, true).impl());
VLOG(4) << "Different place, do TensorCopy";
}
if (src.get_autograd_meta()) {
egr::EagerUtils::autograd_meta(&(self->tensor))
->SetPersistable(
egr::EagerUtils::unsafe_autograd_meta(src)->Persistable());
} else {
egr::EagerUtils::autograd_meta(&(self->tensor))->SetPersistable(false);
}
}
void InitTensorWithFrameworkTensor(TensorObject* self,
const phi::DenseTensor& src,
const phi::Place& place,
const std::string& name) {
self->tensor.set_name(name);
if (place == src.place()) {
self->tensor.set_impl(std::make_shared<phi::DenseTensor>(src));
VLOG(4) << "Same place, do ShareDataWith";
} else {
auto temp = paddle::Tensor(std::make_shared<phi::DenseTensor>(src));
self->tensor.set_impl(temp.copy_to(place, true).impl());
VLOG(4) << "Different place, do TensorCopy";
}
egr::EagerUtils::autograd_meta(&(self->tensor))->SetPersistable(false);
}
void InitStringTensorWithStringTensor(TensorObject* self,
const paddle::Tensor& src,
const phi::Place& place,
const std::string& name) {
self->tensor.set_name(name);
auto impl = std::static_pointer_cast<phi::StringTensor>(src.impl());
self->tensor.set_impl(impl);
VLOG(4)
<< "Do ShareDataWith when using StringTensor to initialize StringTensor";
}
py::object ParsePyArray(
std::unordered_map<std::string, PyObject*> kws_map,
std::unordered_map<std::string, Py_ssize_t> kw_order_map,
PyObject* args,
bool flag_kwargs,
Py_ssize_t args_num) {
py::object numpy_value = py::object();
if (kw_order_map["value"] <= args_num) {
numpy_value = py::reinterpret_borrow<py::object>(
py::handle(PyTuple_GET_ITEM(args, kw_order_map["value"] - 1)));
} else {
if (flag_kwargs && kws_map["value"] != nullptr) {
numpy_value =
py::reinterpret_borrow<py::object>(py::handle(kws_map["value"]));
} else {
PADDLE_THROW(common::errors::InvalidArgument(
"The first expected arguments is {value: PyArray}, "
"but could not parse the first argument {value: PyArray} "
"successfully. "
"Please check your input first and make sure you are on the right "
"way."));
}
}
return numpy_value;
}
phi::Place ParsePlace(std::unordered_map<std::string, PyObject*> kws_map,
std::unordered_map<std::string, Py_ssize_t> kw_order_map,
PyObject* args,
bool flag_kwargs,
Py_ssize_t args_num) {
phi::Place place = egr::Controller::Instance().GetExpectedPlace();
if (kw_order_map["place"] <= args_num) {
place = CastPyArg2Place(PyTuple_GET_ITEM(args, kw_order_map["place"] - 1),
kw_order_map["place"] - 1);
} else {
if (flag_kwargs && kws_map["place"] != nullptr) {
place = CastPyArg2Place(kws_map["place"], 0);
} else {
// default
return place;
}
}
return place;
}
ProcessMesh ParseProcessMeshArgs(
std::unordered_map<std::string, PyObject*> kws_map,
std::unordered_map<std::string, Py_ssize_t> kw_order_map,
PyObject* args,
bool flag_kwargs,
Py_ssize_t args_num) {
ProcessMesh process_mesh;
if (kw_order_map["process_mesh"] <= args_num) {
process_mesh = CastPyArg2ProcessMesh(
PyTuple_GET_ITEM(args, kw_order_map["process_mesh"] - 1),
kw_order_map["process_mesh"] - 1);
} else if (flag_kwargs && kws_map["process_mesh"] != nullptr) {
process_mesh = CastPyArg2ProcessMesh(kws_map["process_mesh"], 0);
}
return process_mesh;
}
Placements ParsePlacementsArgs(
std::unordered_map<std::string, PyObject*> kws_map,
std::unordered_map<std::string, Py_ssize_t> kw_order_map,
PyObject* args,
bool flag_kwargs,
Py_ssize_t args_num) {
Placements placements;
const std::string& placements_key = "placements";
if (kw_order_map[placements_key] <= args_num) { // NOLINT
placements = CastPyArg2VectorOfPlacement(
PyTuple_GET_ITEM(args, kw_order_map[placements_key] - 1),
kw_order_map[placements_key] - 1);
} else if (flag_kwargs && kws_map[placements_key] != nullptr) {
placements = CastPyArg2VectorOfPlacement(kws_map[placements_key], 0);
}
return placements;
}
std::vector<int> ParseDimsArgs(
std::unordered_map<std::string, PyObject*> kws_map,
std::unordered_map<std::string, Py_ssize_t> kw_order_map,
PyObject* args,
bool flag_kwargs,
Py_ssize_t args_num) {
std::vector<int> dims;
const std::string& dims_key = "dims";
if (kw_order_map[dims_key] <= args_num) {
dims = CastPyArg2VectorOfInt(
PyTuple_GET_ITEM(args, kw_order_map[dims_key] - 1),
kw_order_map[dims_key] - 1);
} else if (flag_kwargs && kws_map[dims_key] != nullptr) {
dims = CastPyArg2VectorOfInt(kws_map[dims_key], 0);
}
return dims;
}
// boolean arguments: zero_copy, stop_gradient, persistable
int ParseBooleanArgs(std::string key,
std::unordered_map<std::string, PyObject*> kws_map,
std::unordered_map<std::string, Py_ssize_t> kw_order_map,
PyObject* args,
bool flag_kwargs,
Py_ssize_t args_num) {
int res = -1;
if (kw_order_map[key] <= args_num) {
res = static_cast<int>(CastPyArg2AttrBoolean(
PyTuple_GET_ITEM(args, kw_order_map[key] - 1), kw_order_map[key] - 1));
} else {
if (flag_kwargs && kws_map[key] != nullptr) {
res = static_cast<int>(CastPyArg2AttrBoolean(kws_map[key], 0));
}
}
return res;
}
std::string ParseName(std::unordered_map<std::string, PyObject*> kws_map,
std::unordered_map<std::string, Py_ssize_t> kw_order_map,
PyObject* args,
bool flag_kwargs,
Py_ssize_t args_num,
std::string unique_name_prefix = "generated_tensor") {
std::string act_name = "";
if (kw_order_map["name"] <= args_num) {
PyObject* name_obj = PyTuple_GET_ITEM(args, kw_order_map["name"] - 1);
if (name_obj == Py_None) {
act_name =
egr::Controller::Instance().GenerateUniqueName(unique_name_prefix);
} else {
act_name = CastPyArg2AttrString(name_obj, kw_order_map["name"] - 1);
}
} else {
if (flag_kwargs) {
if ((kws_map["name"] == NULL) || (kws_map["name"] == Py_None)) {
act_name =
egr::Controller::Instance().GenerateUniqueName(unique_name_prefix);
} else {
act_name = CastPyArg2AttrString(kws_map["name"], 0);
}
} else {
act_name =
egr::Controller::Instance().GenerateUniqueName(unique_name_prefix);
}
}
return act_name;
}
// initialize Tensor by PyArray(first argument is PyArray,
// mix args and kwargs) automatically.
void AutoInitTensorByPyArray(TensorObject* py_tensor_ptr,
std::unordered_map<std::string, PyObject*> kws_map,
PyObject* args,
bool flag_kwargs,
Py_ssize_t args_num) {
// The first argument of the Tensor constructor is PyArray,
// there are 6 arguments to construct the new Tensor,
// kw_order_map's key is every arguments of the constructor,
// kw_order_map's value is the position of the arguments respectively.
// If u want to update this constructor with new arguments,
// need to update this map and to add or change related code.
std::unordered_map<std::string, Py_ssize_t> kw_order_map{
{"value", 1},
{"place", 2},
{"persistable", 3},
{"zero_copy", 4},
{"name", 5},
{"stop_gradient", 6}};
py::object numpy_value = py::object();
phi::Place place = egr::Controller::Instance().GetExpectedPlace();
bool persistable = false;
bool zero_copy = false;
std::string act_name = "";
int stop_gradient = -1;
numpy_value =
ParsePyArray(kws_map, kw_order_map, args, flag_kwargs, args_num);
place = ParsePlace(kws_map, kw_order_map, args, flag_kwargs, args_num);
persistable =
(1 ==
ParseBooleanArgs(
"persistable", kws_map, kw_order_map, args, flag_kwargs, args_num));
zero_copy =
(1 ==
ParseBooleanArgs(
"zero_copy", kws_map, kw_order_map, args, flag_kwargs, args_num));
act_name = ParseName(kws_map, kw_order_map, args, flag_kwargs, args_num);
stop_gradient = ParseBooleanArgs(
"stop_gradient", kws_map, kw_order_map, args, flag_kwargs, args_num);
EmptyTensorInitializer(
py_tensor_ptr, act_name, place, persistable, stop_gradient);
InitTensorWithNumpyValue(py_tensor_ptr, numpy_value, place, zero_copy);
}
// initialize Tensor by Tensor or phi::DenseTensor (mix args and
// kwargs) automatically.
void AutoInitTensorByTensor(TensorObject* py_tensor_ptr,
std::unordered_map<std::string, PyObject*> kws_map,
PyObject* args,
bool flag_kwargs,
Py_ssize_t args_num,
bool init_by_egr_tensor = true) {
// The first argument of the Tensor constructor is Tensor or
// framework Tensor,
// there are 6 arguments to construct the new Tensor,
// kw_order_map's key is every arguments of the constructor,
// kw_order_map's value is the position of the arguments respectively.
// If u want to update this constructor with new arguments,
// need to update this map and to add or change related code.
std::unordered_map<std::string, Py_ssize_t> kw_order_map{{"value", 1},
{"place", 2},
{"name", 3},
{"dims", 4},
{"process_mesh", 5},
{"placements", 6}};
phi::Place place = egr::Controller::Instance().GetExpectedPlace();
std::string act_name = "";
place = ParsePlace(kws_map, kw_order_map, args, flag_kwargs, args_num);
act_name = ParseName(kws_map, kw_order_map, args, flag_kwargs, args_num);
if (init_by_egr_tensor) {
paddle::Tensor src_tensor;
if (kw_order_map["value"] <= args_num) {
src_tensor =
CastPyArg2Tensor(PyTuple_GET_ITEM(args, kw_order_map["value"] - 1),
kw_order_map["value"] - 1);
} else {
if (flag_kwargs && kws_map["value"] != nullptr) {
src_tensor = CastPyArg2Tensor(kws_map["value"], 0);
} else {
PADDLE_THROW(common::errors::InvalidArgument(
"The first expected kwargs is {value: Tensor}, "
"but could not parse the first argument {value: Tensor} "
"successfully. "
"Please check your input first and make sure you are on the right "
"way."));
}
}
ProcessMesh process_mesh = ParseProcessMeshArgs(
kws_map, kw_order_map, args, flag_kwargs, args_num);
if (!process_mesh.empty()) {
auto placements = ParsePlacementsArgs(
kws_map, kw_order_map, args, flag_kwargs, args_num);
auto global_dims =
ParseDimsArgs(kws_map, kw_order_map, args, flag_kwargs, args_num);
if (!global_dims.empty()) {
InitDistTensorWithTensor(py_tensor_ptr,
src_tensor,
global_dims,
place,
act_name,
process_mesh,
placements);
} else {
InitDistTensorWithTensor(py_tensor_ptr,
src_tensor,
place,
act_name,
process_mesh,
placements);
}
} else {
InitTensorWithTensor(py_tensor_ptr, src_tensor, place, act_name);
}
} else {
// init by framework tensor
phi::DenseTensor src_tensor;
if (kw_order_map["value"] <= args_num) {
src_tensor = CastPyArg2FrameworkTensor(
PyTuple_GET_ITEM(args, kw_order_map["value"] - 1),
kw_order_map["value"] - 1);
} else {
if (flag_kwargs && kws_map["value"] != nullptr) {
src_tensor = CastPyArg2FrameworkTensor(kws_map["value"], 0);
} else {
PADDLE_THROW(common::errors::InvalidArgument(
"The first expected arguments is {value: phi::DenseTensor}, "
"but could not parse the first argument {value: phi::DenseTensor} "
"successfully. "
"Please check your input first and make sure you are on the right "
"way."));
}
}
InitTensorWithFrameworkTensor(py_tensor_ptr, src_tensor, place, act_name);
}
}
void AutoInitStringTensorByPyArray(
TensorObject* py_tensor_ptr,
std::unordered_map<std::string, PyObject*> kws_map,
PyObject* args,
bool flag_kwargs,
Py_ssize_t args_num) {
// The first argument of the StringTensor constructor is PyArray,
// there are 4 arguments to construct the new StringTensor,
// kw_order_map's key is every arguments of the constructor,
// kw_order_map's value is the position of the arguments respectively.
// If u want to update this constructor with new arguments,
// need to update this map and to add or change related code.
std::unordered_map<std::string, Py_ssize_t> kw_order_map{{"value", 1},
{"name", 2}};
py::object numpy_value = py::object();
phi::Place place = egr::Controller::Instance().GetExpectedPlace();
std::string act_name = "";
numpy_value =
ParsePyArray(kws_map, kw_order_map, args, flag_kwargs, args_num);
act_name = ParseName(kws_map,
kw_order_map,
args,
flag_kwargs,
args_num,
"generated_string_tensor");
EmptyStringTensorInitializer(py_tensor_ptr, act_name, place);
InitStringTensorWithNumpyValue(py_tensor_ptr, numpy_value);
}
void AutoInitStringTensorByStringTensor(
TensorObject* py_tensor_ptr,
std::unordered_map<std::string, PyObject*> kws_map,
PyObject* args,
bool flag_kwargs,
Py_ssize_t args_num) {
// The first argument of the Tensor constructor is StringTensor,
// there are 3 arguments to construct the new StringTensor,
// kw_order_map's key is every arguments of the constructor,
// kw_order_map's value is the position of the arguments respectively.
// If u want to update this constructor with new arguments,
// need to update this map and to add or change related code.
std::unordered_map<std::string, Py_ssize_t> kw_order_map{{"value", 1},
{"name", 2}};
phi::Place place = egr::Controller::Instance().GetExpectedPlace();
std::string act_name = "";
act_name = ParseName(kws_map,
kw_order_map,
args,
flag_kwargs,
args_num,
"generated_string_tensor");
paddle::Tensor src_tensor;
if (kw_order_map["value"] <= args_num) {
src_tensor =
CastPyArg2Tensor(PyTuple_GET_ITEM(args, kw_order_map["value"] - 1),
kw_order_map["value"] - 1);
} else {
if (flag_kwargs && kws_map["value"] != nullptr) {
src_tensor = CastPyArg2Tensor(kws_map["value"], 0);
} else {
PADDLE_THROW(common::errors::InvalidArgument(
"The first expected kwargs is {value: Tensor}, "
"but could not parse the first argument {value: Tensor} "
"successfully. "
"Please check your input first and make sure you are on the right "
"way."));
}
}
InitStringTensorWithStringTensor(py_tensor_ptr, src_tensor, place, act_name);
}
PyDoc_STRVAR( // NOLINT
TensorDoc,
R"DOC(Tensor($self, /, value, place, persistable, zero_copy, name, stop_gradient, dims, dtype, type)
--
Tensor is the basic data structure in PaddlePaddle. There are some ways to create a Tensor:
- Use the existing ``data`` to create a Tensor, please refer to :ref:`api_paddle_to_tensor`.
- Create a Tensor with a specified ``shape``, please refer to :ref:`api_paddle_ones`,
:ref:`api_paddle_zeros`, :ref:`api_paddle_full`.
- Create a Tensor with the same ``shape`` and ``dtype`` as other Tensor, please refer to
:ref:`api_paddle_ones_like`, :ref:`api_paddle_zeros_like`, :ref:`api_paddle_full_like`.
)DOC");
/** We should have init function with signature:
* 1.
* def __init__ ()
* 2.
* (should have at least five parameter, five parameters create DenseTensor,
* seven parameters create DistTensor)
* def __init__ (
* ** dtype: paddle::DataType,
* ** dims: vector<int>,
* ** name: std::string,
* ** type: paddle::framework::proto::VarType::DENSE_TENSOR,
* ** persistable: bool,
* ** process_mesh: phi::distributed::ProcessMesh,
* ** placements: std::vector<Placement>)
* 3. (multi-place)
* (should have at least one parameter, one parameter equals to case 4, zero
* parameter equals to case 1)
* def __init__ (
* ** value: ndarray,
* ** place: phi::Place,
* ** persistable: bool,
* ** zero_copy: bool,
* ** name: std::string,
* ** stop_gradient: bool)
* 4.
* def __init__ (
* ** value: ndarray)
* 5.
* def __init__ (
* ** tensor: Tensor)
* 6. (multi-place)
* (should have at least one parameter, one parameter equals to case 5, zero
* parameter equals to case 1.)
* def __init__ (
* ** global_tensor: Tensor,
* ** place: phi::Place,
* ** name: std::string,
* ** process_mesh: phi::distributed::ProcessMesh,
* ** placements: std::vector<Placement>)
* 7. (multi-place)
* (should have at least one parameter, one parameter equals to case 5, zero
* parameter equals to case 1.)
* def __init__ (
* ** local_tensor: Tensor,
* ** global_dims: vector<int>,
* ** name: std::string,
* ** process_mesh: phi::distributed::ProcessMesh,
* ** placements: std::vector<Placement>)
* 8. (multi-place) (should have at least one parameter, one parameter similar
* to case 5, zero parameter equals to case 1.)
* def __init__ (
* ** tensor: FrameworkTensor,
* ** place: phi::Place,
* ** name: std::string)
* **/
int TensorInit(PyObject* self, PyObject* args, PyObject* kwargs) {
EAGER_TRY
SetPythonStack();
// set a flag to record use kwargs or not
bool flag_kwargs = false;
if (kwargs && PyList_Size(PyDict_Keys(kwargs))) flag_kwargs = true;
// all kwargs
PyObject* kw_zero_copy = nullptr;
PyObject* kw_persistable = nullptr;
PyObject* kw_stop_gradient = nullptr;
PyObject* kw_value = nullptr; // receive PyArray or Tensor
PyObject* kw_place = nullptr;
PyObject* kw_name = nullptr;
PyObject* kw_dims = nullptr;
PyObject* kw_dtype = nullptr;
PyObject* kw_type = nullptr;
PyObject* kw_process_mesh = nullptr;
PyObject* kw_placements = nullptr;
// the keywords argument
static char* kwlist[] = {const_cast<char*>("value"), // NOLINT
const_cast<char*>("place"),
const_cast<char*>("persistable"),
const_cast<char*>("zero_copy"),
const_cast<char*>("name"),
const_cast<char*>("stop_gradient"),
const_cast<char*>("dims"),
const_cast<char*>("dtype"),
const_cast<char*>("type"),
const_cast<char*>("process_mesh"),
const_cast<char*>("placements"),
nullptr};
// 'O' Store a Python object (without any conversion) in a C object pointer,
// '|' Indicates that the remaining arguments in the Python argument list are
// optional.
// PyArg_ParseTupleAndKeywords can Parse the parameters of a function that
// takes both positional and keyword parameters into local variables,
// which enhance case2, case3, case4, case5, case6, case7.
bool flag_ = PyArg_ParseTupleAndKeywords(args,
kwargs,
"|OOOOOOOOOOO",
kwlist,
&kw_value,
&kw_place,
&kw_persistable,
&kw_zero_copy,
&kw_name,
&kw_stop_gradient,
&kw_dims,
&kw_dtype,
&kw_type,
&kw_process_mesh,
&kw_placements);
// helper map
std::unordered_map<std::string, PyObject*> kws_map{
{"value", kw_value},
{"place", kw_place},
{"persistable", kw_persistable},
{"zero_copy", kw_zero_copy},
{"name", kw_name},
{"stop_gradient", kw_stop_gradient},
{"dims", kw_dims},
{"dtype", kw_dtype},
{"type", kw_type},
{"process_mesh", kw_process_mesh},
{"placements", kw_placements}};
PADDLE_ENFORCE_EQ(
flag_,
true,
common::errors::PreconditionNotMet(
"Could not parse args and kwargs successfully, "
"please check your input first and make "
"sure you are on the right way. "
"The expected arguments as follow: ("
"value, place, persistable, zero_copy, "
"name, stop_gradient, dims, dtype, type, process_mesh, placements)"));
PADDLE_ENFORCE_NOT_NULL(
self,
common::errors::Fatal(
"Calling __init__ of Eager Tensor without __new__ is "
"forbidden. Please check your code and make sure you new a "
"eager tensor before init it."));
auto py_tensor_ptr = reinterpret_cast<TensorObject*>(self);
Py_ssize_t args_num = PyTuple_Size(args);
VLOG(6) << " args_num: " << args_num;
// args_num = 0, means that there is no position arguments.
if (args_num == (Py_ssize_t)0) {
if (!flag_kwargs) {
// case 1
VLOG(6) << "Calling case1's initializer.";
EmptyTensorInitializer(
py_tensor_ptr,
egr::Controller::Instance().GenerateUniqueName("generated_tensor"),
egr::Controller::Instance().GetExpectedPlace());
return 0;
} else { // no position args, all arguments are kwargs
if (kw_value != nullptr) {
if (pybind11::detail::npy_api::get().PyArray_Check_(kw_value)) {
VLOG(6) << "Calling case3's or case4's initializer";
AutoInitTensorByPyArray(
py_tensor_ptr, kws_map, args, flag_kwargs, args_num);
return 0;
} else if (PyObject_TypeCheck(kw_value, p_tensor_type)) {
VLOG(6) << "Calling case5's or case6's or case7's initializer";
AutoInitTensorByTensor(
py_tensor_ptr, kws_map, args, flag_kwargs, args_num);
return 0;
} else if (PyObject_TypeCheck(kw_value, g_framework_tensor_pytype)) {
VLOG(6) << "Calling case8's initializer.";
AutoInitTensorByTensor(py_tensor_ptr,
kws_map,
args,
flag_kwargs,
args_num,
/* false means not init by egr tensor*/ false);
return 0;
} else {
PADDLE_THROW(common::errors::InvalidArgument(
"Could not parse the first keyword argument successfully, "
"the first keyword argument is value, but it should be PyArray "
"or Tensor or phi::DenseTensor. "
"Please check your input first and make sure you are on the "
"right way."));
}
} else if (kw_dtype != nullptr &&
(PyObject_TypeCheck(kw_dtype, g_data_type_pytype) ||
PyObject_TypeCheck(kw_dtype, g_vartype_pytype))) {
// TODO(jeff41404): until the default value of FLAGS_deable_ir_appi is
// True, can delete `PyObject_TypeCheck(kw_dtype, g_vartype_pytype)`
// Retain it during the transitional period.
VLOG(6) << "Calling case2's initializer";
PADDLE_ENFORCE_NOT_NULL(
kw_dims,
common::errors::InvalidArgument(
"Calling __init__ of Eager Tensor with NULL dims is "
"forbidden. Please check your code and make sure you new a "
"dims before calling this constructor."));
PADDLE_ENFORCE_NOT_NULL(
kw_name,
common::errors::InvalidArgument(
"Calling __init__ of Eager Tensor with NULL name is "
"forbidden. Please check your code and make sure you new a "
"name before calling this constructor."));
PADDLE_ENFORCE_NOT_NULL(
kw_dtype,
common::errors::InvalidArgument(
"Calling __init__ of Eager Tensor with NULL dtype is "
"forbidden. Please check your code and make sure you new a "
"dtype before calling this constructor."));
PADDLE_ENFORCE_NOT_NULL(
kw_persistable,
common::errors::InvalidArgument(
"Calling __init__ of Eager Tensor with NULL persistable is "
"forbidden. Please check your code and make sure you new a "
"persistable before calling this constructor."));
paddle::DataType dtype = CastPyArg2DataType(kw_dtype, "TensorInit", 0);
std::vector<int> dims = CastPyArg2VectorOfInt(kw_dims, 0);
std::string act_name = "";
if (kw_name == Py_None) {