(inp, nsplit_or_sections, axis)
| 143 | # if nsplit_or_sections is Sequence[int], means divide inputs into |
| 144 | # len(nsplit_or_sections) parts, and the i-th part has nsplit_or_sections[i] elements |
| 145 | def split(inp, nsplit_or_sections, axis): |
| 146 | from .indexing import index_with_slices |
| 147 | |
| 148 | ishape = inp.shape |
| 149 | if axis < 0: |
| 150 | axis = axis + len(ishape) |
| 151 | |
| 152 | if isinstance(nsplit_or_sections, int): |
| 153 | dimlen = ishape[axis] |
| 154 | assert dimlen % nsplit_or_sections == 0, "not an equal division" |
| 155 | sections = [dimlen // nsplit_or_sections] * nsplit_or_sections |
| 156 | else: |
| 157 | sections = nsplit_or_sections |
| 158 | |
| 159 | assert np.sum(sections) == ishape[axis], "error split param" |
| 160 | |
| 161 | slices = [] |
| 162 | start = 0 |
| 163 | for section in sections: |
| 164 | slices.append( |
| 165 | [ |
| 166 | None if idx != axis else slice(start, start + section, 1) |
| 167 | for idx in range(len(ishape)) |
| 168 | ] |
| 169 | ) |
| 170 | start = start + section |
| 171 | |
| 172 | return [index_with_slices(inp, slices[i]) for i in range(len(sections))] |
| 173 | |
| 174 | |
| 175 | @register_lower_rule(mops.Split, "Split") |
no test coverage detected