Exemplo n.º 1
0
    def test_merge_unmatch(self):
        ''' Merge unmatch. '''
        for fr in [
                FmapRange((0, ) * 4, (4, 4, 10, 16)),
                FmapRange((0, ) * 4, (4, 4, 16, 32)),
                FmapRange((0, ) * 4, (3, 4, 16, 16))
        ]:
            frm = FmapRangeMap()
            frm.add(fr, (PhyDim2(1, 1), ))
            dly = DataLayout(origin=PhyDim2(-1, -1),
                             frmap=frm,
                             type=NodeRegion.DATA)

            with self.assertRaisesRegexp(ValueError, 'DataLayout: .*match.*'):
                _ = self.dly.merge('|', dly)
            with self.assertRaisesRegexp(ValueError, 'DataLayout: .*match.*'):
                _ = self.dly.merge('+', dly)

        frm = FmapRangeMap()
        frm.add(FmapRange((0, ) * 4, (4, 2, 16, 16)), (PhyDim2(1, 1), ))
        dly = DataLayout(origin=PhyDim2(-1, -1),
                         frmap=frm,
                         type=NodeRegion.DATA)

        with self.assertRaisesRegexp(ValueError, 'DataLayout: .*match.*'):
            _ = self.dly.merge('+', dly)
Exemplo n.º 2
0
 def test_invalid_frmap_value_type(self):
     ''' Invalid frmap value type. '''
     frm = FmapRangeMap()
     frm.add(FmapRange((0, ) * 4, (5, 5, 19, 19)), PhyDim2(4, 4))
     with self.assertRaisesRegexp(TypeError, 'DataLayout: .*frmap.*'):
         _ = DataLayout(origin=PhyDim2(2, 3),
                        frmap=frm,
                        type=NodeRegion.DATA)
     frm = FmapRangeMap()
     frm.add(FmapRange((0, ) * 4, (5, 5, 19, 19)), (PhyDim2(4, 4), 4))
     with self.assertRaisesRegexp(TypeError, 'DataLayout: .*frmap.*'):
         _ = DataLayout(origin=PhyDim2(2, 3),
                        frmap=frm,
                        type=NodeRegion.DATA)
    def test_o_full_layer(self):
        ''' ofmap full layer. '''
        for wlkey in self.layers:
            layer = self.layers[wlkey]

            for dnkey in self.dim_nodes:

                for part in self._gen_partition(wlkey=wlkey,
                                                dnkey=dnkey,
                                                optkey='NOINPP'):

                    frmap = FmapRangeMap()

                    for pidx in part.gen_pidx():

                        fr = partition.part_layer_ofmap_range(
                            layer, self.batch_size, part, pidx)
                        frmap.add(fr, 0)

                    self.assertTrue(frmap.is_complete())

                    cfr = frmap.complete_fmap_range()
                    self.assertEqual(cfr.size('b'), self.batch_size)
                    self.assertEqual(cfr.size('n'), layer.nofm)
                    self.assertEqual(cfr.size('h'), layer.hofm)
                    self.assertEqual(cfr.size('w'), layer.wofm)
    def _make_data_layout(self, nfm, hfm, wfm, origin, nums, dims):
        ''' Make a DataLayout instance. '''
        assert util.prod(nums) == dims.size()

        def _coord(idxs):
            # In the order of n, b, w, h, i.e., 1, 0, 3, 2.
            cflat = 0
            for i in [1, 0, 3, 2]:
                cflat = cflat * nums[i] + idxs[i]
            assert cflat < dims.size()
            return PhyDim2(*divmod(cflat, dims.w))

        sizes = (self.batch_size, nfm, hfm, wfm)

        frmap = FmapRangeMap()

        for idxs in itertools.product(*[range(n) for n in nums]):

            begs = [i * s // n for i, n, s in zip(idxs, nums, sizes)]
            ends = [(i + 1) * s // n for i, n, s in zip(idxs, nums, sizes)]

            frmap.add(FmapRange(begs, ends), (_coord(idxs), ))

        dl = DataLayout(frmap=frmap, origin=origin, type=NodeRegion.DATA)
        assert dl.frmap.complete_fmap_range().size() == util.prod(sizes)

        return dl
Exemplo n.º 5
0
    def test_merge(self):
        ''' Merge. '''
        fr = FmapRange((0, ) * 4, (30, ) * 4)

        frm = FmapRangeMap()
        frm.add(FmapRange((0, 0, 0, 0), (4, 1, 16, 16)),
                (PhyDim2(0, 0), PhyDim2(1, 1)))
        frm.add(FmapRange((0, 1, 0, 0), (4, 3, 16, 16)),
                (PhyDim2(1, 0), PhyDim2(2, 2)))
        dly = DataLayout(origin=PhyDim2(-1, -1),
                         frmap=frm,
                         type=NodeRegion.DATA)

        mdly1 = self.dly.merge('|', dly)
        mdly2 = dly.merge('|', self.dly)
        self.assertEqual(mdly1.type, self.dly.type, 'merge |: type')
        self.assertEqual(mdly2.type, dly.type, 'merge |: type')
        self.assertEqual(mdly1.frmap.complete_fmap_range(),
                         mdly2.frmap.complete_fmap_range(),
                         'merge |: complete_fmap_range')
        self.assertEqual(
            mdly1.frmap.complete_fmap_range().size(),
            self.frm.complete_fmap_range().size() +
            frm.complete_fmap_range().size(),
            'merge |: complete_fmap_range: size')
        self.assertEqual(mdly1.total_transfer_nhops(fr, PhyDim2(0, 0)),
                         mdly2.total_transfer_nhops(fr, PhyDim2(0, 0)),
                         'merge |: nhops')
        self.assertEqual(
            mdly1.total_transfer_nhops(fr, PhyDim2(0, 0)),
            self.dly.total_transfer_nhops(fr, PhyDim2(0, 0)) +
            dly.total_transfer_nhops(fr, PhyDim2(0, 0)), 'merge |: nhops')

        frm.add(FmapRange((0, 3, 0, 0), (4, 4, 16, 16)),
                (PhyDim2(1, 3), PhyDim2(2, 1), PhyDim2(-1, -2)))
        # Only type differs from self.dly.
        sdly = DataLayout(origin=self.dly.origin,
                          frmap=self.dly.frmap,
                          type=NodeRegion.PROC)
        dly = DataLayout(origin=PhyDim2(-1, -1),
                         frmap=frm,
                         type=NodeRegion.PROC)
        mdly1 = sdly.merge('+', dly)
        mdly2 = dly.merge('+', sdly)
        self.assertEqual(mdly1.type, sdly.type, 'merge +: type')
        self.assertEqual(mdly2.type, dly.type, 'merge +: type')
        self.assertEqual(mdly1.frmap.complete_fmap_range(),
                         mdly2.frmap.complete_fmap_range(),
                         'merge +: complete_fmap_range')
        self.assertEqual(mdly1.frmap.complete_fmap_range().size(),
                         self.frm.complete_fmap_range().size(),
                         'merge +: complete_fmap_range: size')
        self.assertEqual(mdly1.total_transfer_nhops(fr, PhyDim2(0, 0)),
                         mdly2.total_transfer_nhops(fr, PhyDim2(0, 0)),
                         'merge +: nhops')
        self.assertEqual(
            mdly1.total_transfer_nhops(fr, PhyDim2(0, 0)),
            sdly.total_transfer_nhops(fr, PhyDim2(0, 0)) +
            dly.total_transfer_nhops(fr, PhyDim2(0, 0)), 'merge +: nhops')
Exemplo n.º 6
0
 def setUp(self):
     self.frm = FmapRangeMap()
     self.frm.add(FmapRange((0, 0, 0, 0), (2, 4, 8, 16)), 0)
     self.frm.add(FmapRange((0, 0, 8, 0), (2, 4, 16, 16)), 1)
     self.frm.add(FmapRange((0, 4, 0, 0), (2, 8, 8, 16)), 2)
     self.frm.add(FmapRange((0, 4, 8, 0), (2, 8, 16, 16)), 3)
     self.frm.add(FmapRange((2, 0, 0, 0), (4, 4, 8, 16)), 4)
     self.frm.add(FmapRange((2, 0, 8, 0), (4, 4, 16, 16)), 5)
     self.frm.add(FmapRange((2, 4, 0, 0), (4, 8, 8, 16)), 6)
     self.frm.add(FmapRange((2, 4, 8, 0), (4, 8, 16, 16)), 7)
Exemplo n.º 7
0
    def test_complete_fmap_range(self):
        ''' Get complete_fmap_range. '''
        self.assertTrue(self.frm.is_complete(), 'is_complete')
        self.assertEqual(self.frm.complete_fmap_range().size(), 4 * 8 * 16 * 16,
                         'complete_fmap_range')

        fr = FmapRange((0, 0, 0, 0), (3, 5, 7, 9))
        frm = FmapRangeMap()
        frm.add(fr, 3.4)
        self.assertTrue(frm.is_complete(), 'is_complete')
        self.assertEqual(frm.complete_fmap_range(), fr, 'complete_fmap_range')
Exemplo n.º 8
0
 def setUp(self):
     self.frm = FmapRangeMap()
     self.frm.add(FmapRange((0, 0, 0, 0), (2, 4, 8, 16)), (PhyDim2(0, 0), ))
     self.frm.add(FmapRange((0, 0, 8, 0), (2, 4, 16, 16)),
                  (PhyDim2(1, 0), ))
     self.frm.add(FmapRange((2, 0, 0, 0), (4, 4, 8, 16)), (PhyDim2(0, 1), ))
     self.frm.add(FmapRange((2, 0, 8, 0), (4, 4, 16, 16)),
                  (PhyDim2(1, 1), ))
     self.dly = DataLayout(origin=PhyDim2(1, 1),
                           frmap=self.frm,
                           type=NodeRegion.DATA)
Exemplo n.º 9
0
    def test_is_complete_incomplete(self):
        ''' Get complete_fmap_range incomplete. '''
        self.frm.add(FmapRange((4, 8, 16, 16), (5, 9, 17, 17)), 10)
        self.assertFalse(self.frm.is_complete(), 'is_complete: incomplete')
        with self.assertRaisesRegex(ValueError, 'FmapRangeMap: .*complete.*'):
            _ = self.frm.complete_fmap_range()

        fr = FmapRange((1, 0, 0, 0), (3, 5, 7, 9))
        frm = FmapRangeMap()
        frm.add(fr, 3.4)
        self.assertFalse(frm.is_complete(), 'is_complete: incomplete')
        with self.assertRaisesRegex(ValueError, 'FmapRangeMap: .*complete.*'):
            _ = frm.complete_fmap_range()
    def setUp(self):

        self.resource = Resource(
            proc_region=NodeRegion(origin=PhyDim2(0, 0), dim=PhyDim2(1, 1),
                                   type=NodeRegion.PROC),
            data_regions=(NodeRegion(origin=PhyDim2(0, 0), dim=PhyDim2(1, 1),
                                     type=NodeRegion.DATA),),
            dim_array=PhyDim2(16, 16), size_gbuf=65536, size_regf=64)

        frmap = FmapRangeMap()
        frmap.add(FmapRange((0, 0, 0, 0), (2, 4, 16, 16)), (PhyDim2(0, 0),))
        self.ifmap_layout = DataLayout(origin=PhyDim2(0, 0), frmap=frmap,
                                       type=NodeRegion.DATA)
Exemplo n.º 11
0
    def setUp(self):

        self.dict_loop = OrderedDict([
            ('cost', 1.234),
            ('time', 123.4),
            ('ops', 1234),
            ('access', [[2, 3, 4], [30, 40, 50], [400, 500, 600],
                        [5000, 6000, 7000]]),
        ])
        self.dict_part = OrderedDict([
            ('cost', 9.876),
            ('total_nhops', [123, 456, 789]),
        ])

        frmap = FmapRangeMap()
        frmap.add(FmapRange((0, 0, 0, 0), (2, 4, 16, 16)), (PhyDim2(0, 0), ))
        self.ofmap_layout = DataLayout(origin=PhyDim2(0, 0),
                                       frmap=frmap,
                                       type=NodeRegion.DATA)
    def test_io_full_layer(self):
        ''' i/ofmap full layer. '''
        for wlkey in ['SM', 'POOL']:
            layer = self.layers[wlkey]

            for dnkey in self.dim_nodes:

                for part in self._gen_partition(wlkey=wlkey,
                                                dnkey=dnkey,
                                                optkey='NOINPP'):

                    # Remove ifmap point from full set.
                    ifp_set = set(
                        FmapRange(fp_beg=(0, 0, 0, 0),
                                  fp_end=(self.batch_size, layer.nifm,
                                          layer.hifm, layer.wifm)).range())
                    # Add ofmap ranges to a map.
                    ofrmap = FmapRangeMap()

                    for pidx in part.gen_pidx():

                        _, ifrng, ofrng = partition.proc_data_range(
                            layer, self.batch_size, part, pidx)

                        for ifp in ifrng.range():
                            ifp_set.discard(ifp)

                        ofrmap.add(ofrng, 0)

                    # Ifmap point set should be empty now.
                    self.assertFalse(ifp_set)

                    # Ofmap range map should be full now.
                    self.assertTrue(ofrmap.is_complete())
                    cfr = ofrmap.complete_fmap_range()
                    self.assertEqual(cfr.size('b'), self.batch_size)
                    self.assertEqual(cfr.size('n'), layer.nofm)
                    self.assertEqual(cfr.size('h'), layer.hofm)
                    self.assertEqual(cfr.size('w'), layer.wofm)