|
18 | 18 | __all__ = ['create_lod_tensor', 'create_random_int_lodtensor'] |
19 | 19 |
|
20 | 20 |
|
21 | | -def _validate_lod(lod, tensor_height=-1): |
22 | | - """Check whether the input length-based lod info is valid. |
23 | | -
|
24 | | - There are several things to check: |
25 | | - 1. lod should be a list of lists. Empty list is fine. |
26 | | - 2. The length of each sublist (a lod level) should be at least one. |
27 | | - 3. Each element in each lod level should be an integer greater than 0. |
28 | | - 4. The sum of one lod level should be equal to the length of the next lod level. |
29 | | - 5. The sum of the last lod level should be equal to the tensor height. |
30 | | - Bypass this check if user does not provide tensor_height as input. |
31 | | -
|
32 | | - Args: |
33 | | - lod: the length-based lod info, e.g., [[2, 3], [2, 1, 2, 3, 4]]. |
34 | | - tensor_height: the outermost dimension of the tensor with which the input |
35 | | - lod is associated with. |
36 | | -
|
37 | | - Returns: |
38 | | - A boolean indicating whether the input lod is valid or not. |
39 | | - """ |
40 | | - assert isinstance(lod, list), "lod should be a list" |
41 | | - # Empty lod is fine |
42 | | - if len(lod) == 0: |
43 | | - return True |
44 | | - |
45 | | - lod_sum = [] |
46 | | - for level in lod: |
47 | | - assert isinstance(level, list), "each item in lod should be a list" |
48 | | - # Each level of lod should have at least one length info |
49 | | - if len(level) < 1: |
50 | | - return False |
51 | | - level_sum = 0 |
52 | | - for lod_len in level: |
53 | | - # Each length in a level should be > 0 |
54 | | - if lod_len <= 0: |
55 | | - return False |
56 | | - level_sum += lod_len |
57 | | - lod_sum.append(level_sum) |
58 | | - |
59 | | - for idx, val in enumerate(lod_sum[:-1]): |
60 | | - # Each level's sum should be equal to |
61 | | - # the number of items in the next level |
62 | | - if val != len(lod[idx + 1]): |
63 | | - return False |
64 | | - |
65 | | - if tensor_height == -1: |
66 | | - return True |
67 | | - else: |
68 | | - # Last level's sum should be equal to the tensor height |
69 | | - return lod_sum[-1] == tensor_height |
70 | | - |
71 | | - |
72 | | -def _convert_lod(lod): |
73 | | - """Convert a length-based lod to a offset-based lod. |
74 | | -
|
75 | | - If the length-based lod is [[2, 3], [2, 1, 2, 3, 4]], |
76 | | - then the offset-based lod is [[0, 2, 5], [0, 2, 3, 5, 8, 12]]. |
77 | | -
|
78 | | - Args: |
79 | | - lod: a length-based lod info. |
80 | | -
|
81 | | - Returns: |
82 | | - A list of lists as the offset-based lod converted to from the input lod. |
83 | | - """ |
84 | | - new_lod = [] |
85 | | - for level in lod: |
86 | | - cur_len = 0 |
87 | | - new_level = [cur_len] |
88 | | - for lod_len in level: |
89 | | - cur_len += lod_len |
90 | | - new_level.append(cur_len) |
91 | | - new_lod.append(new_level) |
92 | | - return new_lod |
93 | | - |
94 | | - |
95 | 21 | def create_lod_tensor(data, lod, place): |
96 | 22 | """Create a lod tensor from a numpy array, a list, or an existing lod tensor. |
97 | 23 |
|
@@ -139,11 +65,11 @@ def create_lod_tensor(data, lod, place): |
139 | 65 | flattened_data = flattened_data.reshape([len(flattened_data), 1]) |
140 | 66 | return create_lod_tensor(flattened_data, lod, place) |
141 | 67 | elif isinstance(data, np.ndarray): |
142 | | - assert _validate_lod(lod, |
143 | | - data.shape[0]), "the provided lod info is invalid" |
144 | 68 | tensor = core.LoDTensor() |
145 | 69 | tensor.set(data, place) |
146 | | - tensor.set_lod(_convert_lod(lod)) |
| 70 | + tensor.set_recursive_sequence_lengths(lod) |
| 71 | + assert tensor.has_valid_recursive_sequence_lengths( |
| 72 | + ), "the provided lod info is invalid" |
147 | 73 | return tensor |
148 | 74 | else: |
149 | 75 | raise TypeError( |
@@ -181,9 +107,8 @@ def create_random_int_lodtensor(lod, base_shape, place, low, high): |
181 | 107 | A fluid LoDTensor object with tensor data and lod info. |
182 | 108 | """ |
183 | 109 | assert isinstance(base_shape, list), "base_shape should be a list" |
184 | | - converted_lod = _convert_lod(lod) |
185 | 110 | # append the total number of basic elements to the front of its shape |
186 | | - overall_shape = [converted_lod[-1][-1]] + base_shape |
| 111 | + overall_shape = [sum(lod[-1])] + base_shape |
187 | 112 | # the range of integer data elements is [low, high] |
188 | 113 | data = np.random.random_integers(low, high, overall_shape).astype("int64") |
189 | 114 | return create_lod_tensor(data, lod, place) |
0 commit comments