@@ -18,7 +18,11 @@ def test_lod_tensor_to_array_level_0(self):
18
18
tensor .set_lod ([[0 , 3 , 9 , 10 ]])
19
19
expect = map (lambda x : numpy .array (x ).astype ('int32' ),
20
20
[[3 , 0 , 9 ], [4 , 1 ], [5 , 2 ], [6 ], [7 ], [8 ]])
21
- self .main (tensor = tensor , expect_array = expect , expect_lod = [] * 6 )
21
+ self .main (
22
+ tensor = tensor ,
23
+ expect_array = expect ,
24
+ expect_lod = [] * 6 ,
25
+ expect_max_len = 6 )
22
26
23
27
def test_lod_tensor_to_array_level_0_empty_seq (self ):
24
28
tensor = core .LoDTensor ()
@@ -27,7 +31,11 @@ def test_lod_tensor_to_array_level_0_empty_seq(self):
27
31
tensor .set_lod ([[0 , 3 , 9 , 9 , 10 ]])
28
32
expect = map (lambda x : numpy .array (x ).astype ('int32' ),
29
33
[[3 , 0 , 9 ], [4 , 1 ], [5 , 2 ], [6 ], [7 ], [8 ]])
30
- self .main (tensor = tensor , expect_array = expect , expect_lod = [] * 6 )
34
+ self .main (
35
+ tensor = tensor ,
36
+ expect_array = expect ,
37
+ expect_lod = [] * 6 ,
38
+ expect_max_len = 6 )
31
39
32
40
def test_lod_tensor_to_array_level_1 (self ):
33
41
tensor = core .LoDTensor ()
@@ -44,7 +52,11 @@ def test_lod_tensor_to_array_level_1(self):
44
52
]
45
53
46
54
lod = [[[0 , 2 , 5 ]], [[0 , 6 , 12 ]], [[0 , 3 ]]]
47
- self .main (tensor = tensor , expect_array = expect , expect_lod = lod )
55
+ self .main (
56
+ tensor = tensor ,
57
+ expect_array = expect ,
58
+ expect_lod = lod ,
59
+ expect_max_len = 3 )
48
60
49
61
def test_lod_tensor_to_array_level_1_empty_seq (self ):
50
62
tensor = core .LoDTensor ()
@@ -63,7 +75,11 @@ def test_lod_tensor_to_array_level_1_empty_seq(self):
63
75
]
64
76
65
77
lod = [[[0 , 5 , 8 , 8 , 15 ]], [[0 , 2 , 6 , 7 , 8 ]], [[0 , 2 , 6 ]], [[0 , 2 ]]]
66
- self .main (tensor = tensor , expect_array = expect , expect_lod = lod )
78
+ self .main (
79
+ tensor = tensor ,
80
+ expect_array = expect ,
81
+ expect_lod = lod ,
82
+ expect_max_len = 4 )
67
83
68
84
def test_lod_tensor_to_array_level_2 (self ):
69
85
tensor = core .LoDTensor ()
@@ -80,22 +96,33 @@ def test_lod_tensor_to_array_level_2(self):
80
96
]
81
97
lod = [[[0 , 1 , 3 , 4 ], [0 , 1 , 4 , 8 , 12 ]],
82
98
[[0 , 4 , 7 ], [0 , 1 , 5 , 9 , 17 , 21 , 27 , 31 ]], [[0 , 2 ], [0 , 6 , 7 ]]]
83
- self .main (tensor = tensor , expect_array = expect , expect_lod = lod )
99
+ self .main (
100
+ tensor = tensor ,
101
+ expect_array = expect ,
102
+ expect_lod = lod ,
103
+ expect_max_len = 3 )
84
104
85
105
def test_lod_tensor_to_array_level_2_skip_level (self ):
86
106
tensor = core .LoDTensor ()
87
107
tensor .set (
88
108
numpy .arange (50 ).reshape (50 , 1 ).astype ('int32' ), self .place ())
89
109
tensor .set_lod ([[0 , 2 , 5 , 6 ], [0 , 2 , 5 , 6 , 10 , 12 , 13 ],
90
110
[0 , 3 , 7 , 11 , 17 , 21 , 22 , 23 , 27 , 31 , 39 , 45 , 46 , 50 ]])
91
- self .main (tensor = tensor , expect_array = None , expect_lod = None , level = 1 )
92
-
93
- def main (self , tensor , expect_array , expect_lod , level = 0 ):
111
+ self .main (
112
+ tensor = tensor ,
113
+ expect_array = None ,
114
+ expect_lod = None ,
115
+ expect_max_len = 4 ,
116
+ level = 1 )
117
+
118
+ def main (self , tensor , expect_array , expect_lod , expect_max_len , level = 0 ):
94
119
place = self .place ()
95
120
program = Program ()
96
121
x = layers .data (name = 'x' , shape = [10 ], main_program = program )
97
122
x .persistable = True
98
123
table = layers .lod_rank_table (x , level = level , main_program = program )
124
+ max_len = layers .max_sequence_len (table , main_program = program )
125
+ max_len .persistable = True
99
126
array = layers .lod_tensor_to_array (x , table , main_program = program )
100
127
array .persistable = True
101
128
@@ -110,6 +137,10 @@ def main(self, tensor, expect_array, expect_lod, level=0):
110
137
self .check_array_same (array , expect_array , expect_lod )
111
138
self .check_tensor_same (scope .find_var (result .name ).get_tensor (), tensor )
112
139
140
+ self .assertEqual (
141
+ numpy .array (scope .find_var (max_len .name ).get_tensor ())[0 ],
142
+ expect_max_len )
143
+
113
144
def check_array_same (self , array , expect_tensor , expect_lod ):
114
145
self .assertEqual (len (expect_tensor ), len (array ))
115
146
for i , exp in enumerate (zip (expect_tensor , expect_lod )):
0 commit comments