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

Function get_ndtuple

imperative/python/megengine/utils/tuple_function.py:6–24  ·  view source on GitHub ↗

r"""Converts possibly 1D tuple to nd tuple. Args: allow_zero: whether to allow zero tuple value

(value, *, n, allow_zero=True)

Source from the content-addressed store, hash-verified

4
5
6def get_ndtuple(value, *, n, allow_zero=True):
7 r"""Converts possibly 1D tuple to nd tuple.
8
9 Args:
10 allow_zero: whether to allow zero tuple value
11 """
12 if not isinstance(value, collections.abc.Iterable):
13 value = int(value)
14 value = tuple([value for i in range(n)])
15 else:
16 assert len(value) == n, "tuple len is not equal to n: {}".format(value)
17 spatial_axis = map(int, value)
18 value = tuple(spatial_axis)
19 if allow_zero:
20 minv = 0
21 else:
22 minv = 1
23 assert min(value) >= minv, "invalid value: {}".format(value)
24 return value
25
26
27_single = functools.partial(get_ndtuple, n=1, allow_zero=True)

Callers

nothing calls this directly

Calls 2

minFunction · 0.50
formatMethod · 0.45

Tested by

no test coverage detected