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

Function tile

imperative/python/megengine/functional/tensor.py:1164–1206  ·  view source on GitHub ↗

r"""Construct an array by repeating ``inp`` the number of times given by ``reps``. If reps has length d, the result will have dimension of ``max(d, inp.ndim)``. It is required that ``d >= inp.dim``. If ``inp.ndim < d``, ``inp`` is promoted to be ``d``-dimensional by prepending new axis.

(inp: Tensor, reps: Iterable[int])

Source from the content-addressed store, hash-verified

1162
1163
1164def tile(inp: Tensor, reps: Iterable[int]):
1165 r"""Construct an array by repeating ``inp`` the number of times given by ``reps``. If reps has length d,
1166 the result will have dimension of ``max(d, inp.ndim)``. It is required that ``d >= inp.dim``. If ``inp.ndim < d``,
1167 ``inp`` is promoted to be ``d``-dimensional by prepending new axis.
1168
1169 Args:
1170 inp: input tensor.
1171 reps: The number of repetitions of inp along each axis.
1172
1173 Returns:
1174 output tensor.
1175
1176
1177 Examples:
1178 >>> import numpy as np
1179 >>> x = Tensor([[1, 2], [3, 4]], np.int32)
1180 >>> F.tile(x, (2,1))
1181 Tensor([[1 2]
1182 [3 4]
1183 [1 2]
1184 [3 4]], dtype=int32, device=xpux:0)
1185 """
1186 shape = astensor1d(inp.shape, inp, dtype="int32", device=inp.device)
1187 reps = astensor1d(reps, inp, dtype="int32", device=inp.device)
1188 l_shape = len(shape)
1189 l_reps = len(reps)
1190 assert (
1191 l_reps >= l_shape
1192 ), "Number of dimensions of tiled dims can not be smaller than number of dimensions of tensor"
1193
1194 for i in range(l_shape):
1195 rep = reps[i + (l_reps - l_shape)]
1196 inp = _tile_one_dim(inp, rep, i)
1197
1198 if l_reps > l_shape:
1199 extra = reps[:-l_shape]
1200 extra_ones = ones_like(extra)
1201 base_shape = concat([extra_ones, shape])
1202 bcast_shape = concat([extra, shape])
1203 target_shape = concat([extra, shape])
1204 inp = broadcast_to(inp.reshape(base_shape), bcast_shape).reshape(target_shape)
1205
1206 return inp
1207
1208
1209def copy(inp, device=None):

Callers

nothing calls this directly

Calls 6

astensor1dFunction · 0.85
_tile_one_dimFunction · 0.85
ones_likeFunction · 0.70
concatFunction · 0.70
broadcast_toFunction · 0.70
reshapeMethod · 0.45

Tested by

no test coverage detected