MCPcopy Create free account
hub / github.com/apache/tvm / test_slice

Function test_slice

tests/python/tirx/test_layout.py:1548–1655  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1546
1547
1548def test_slice():
1549 def verify_slice(layout, shape, region, sliced):
1550 r_shape = [r[1] - r[0] for r in region]
1551 r_size = functools.reduce(operator.mul, [r[1] - r[0] for r in region])
1552
1553 def get_region_coord(u):
1554 coord = []
1555 for r in reversed(region):
1556 coord.append(u % (r[1] - r[0]))
1557 u //= r[1] - r[0]
1558 return coord[::-1]
1559
1560 def get_shape_coord(r_coord, region):
1561 return [region[i][0] + r_coord[i] for i in range(len(region))]
1562
1563 analyzer = Analyzer()
1564
1565 for u in range(r_size):
1566 r_coord = get_region_coord(u)
1567 s_coord = get_shape_coord(r_coord, region)
1568 a = layout.apply(*s_coord, shape=shape)["m"]
1569 b = sliced.apply(*r_coord, shape=r_shape)["m"]
1570 assert analyzer.simplify(a == b)
1571
1572 def case1():
1573 layout = TileLayout(S[(8, 8) : (8, 1)])
1574 shape = [64]
1575 region = [(5, 8)]
1576 sliced = layout.slice(shape, region).canonicalize()
1577 assert sliced is not None
1578 verify_slice(layout, shape, region, sliced)
1579
1580 region = [tvm.ir.Range(5, 8)]
1581 sliced_2 = layout.slice(shape, region).canonicalize()
1582 assert sliced_2 is not None
1583 assert_structural_equal(sliced, sliced_2)
1584
1585 case1()
1586
1587 def case2():
1588 # Choose begin and extent to satisfy midpoint condition
1589 layout = TileLayout(S[(4, 4, 4, 4) : (64, 4, 16, 1)])
1590 shape = [16, 16]
1591 region = [(2, 3), (6, 10)]
1592 sliced = layout.slice(shape, region).canonicalize()
1593 assert sliced is not None
1594 verify_slice(layout, shape, region, sliced)
1595
1596 case2()
1597
1598 def case3():
1599 layout = TileLayout(S[(2, 8, 3, 8) : (192, 8, 64, 1)])
1600 shape = [16, 24]
1601 region = [(2, 6), (4, 12)]
1602 sliced = layout.slice(shape, region).canonicalize()
1603 assert sliced is not None
1604 verify_slice(layout, shape, region, sliced)
1605

Callers

nothing calls this directly

Calls 7

case1Function · 0.85
case2Function · 0.85
case3Function · 0.85
case4Function · 0.85
case_swizzle_sliceFunction · 0.85
case_compose_sliceFunction · 0.85
case_compose_slice_2dFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…