MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / split

Function split

imperative/python/megengine/xla/rules/tensor.py:145–172  ·  view source on GitHub ↗
(inp, nsplit_or_sections, axis)

Source from the content-addressed store, hash-verified

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
145def 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")

Callers 2

parampack_split_lowerFunction · 0.70
split_lowerFunction · 0.70

Calls 3

index_with_slicesFunction · 0.85
sumMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected