Ejemplo 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)
Ejemplo n.º 2
0
    def test_init_ext(self):
        ''' Initial with external layers. '''
        self.network.add_ext('e0', InputLayer(3, 224))
        self.network.add_ext('e1', InputLayer(6, 224))

        e0_layout = DataLayout(frngs=(FmapRange(
            (0, 0, 0, 0),
            FmapPosition(b=self.batch_size,
                         n=self.network['e0'].nofm,
                         h=self.network['e0'].hofm,
                         w=self.network['e0'].wofm)), ),
                               regions=self.input_layout.regions,
                               parts=self.input_layout.parts)
        e1_layout = DataLayout(frngs=(FmapRange(
            (0, 0, 0, 0),
            FmapPosition(b=self.batch_size,
                         n=self.network['e1'].nofm,
                         h=self.network['e1'].hofm,
                         w=self.network['e1'].wofm)), ),
                               regions=self.input_layout.regions,
                               parts=self.input_layout.parts)

        ext_layout_dict = {'e0': e0_layout, 'e1': e1_layout}

        df = NNDataflowScheme(self.network, self.input_layout, ext_layout_dict)

        self.assertIn('e0', df.ext_layout_dict)
        self.assertIn('e1', df.ext_layout_dict)
        self.assertEqual(df.ext_layout_dict['e0'], e0_layout)
        self.assertEqual(df.ext_layout_dict['e1'], e1_layout)
Ejemplo n.º 3
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')
Ejemplo n.º 4
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)
Ejemplo n.º 5
0
    def test_invalid_parts(self):
        ''' Invalid parts. '''
        with self.assertRaisesRegexp(ValueError, 'DataLayout: .*part.*'):
            _ = DataLayout(frngs=(self.frng1, ),
                           regions=(self.region1, ),
                           parts=(PartitionScheme(order=range(pe.NUM),
                                                  pdims=(PhyDim2(1, 1),
                                                         PhyDim2(1, 2),
                                                         PhyDim2(1, 1),
                                                         PhyDim2(2, 1))), ))

        with self.assertRaisesRegexp(ValueError, 'DataLayout: .*part.*'):
            _ = DataLayout(frngs=(self.frng1,
                                  FmapRange((0, 4, 0, 0), (4, 8, 16, 16))),
                           regions=(self.region2, self.region1),
                           parts=(self.part1, self.part2))
    def setUp(self):

        self.resource = Resource(
            proc_region=NodeRegion(origin=PhyDim2(0, 0),
                                   dim=PhyDim2(1, 1),
                                   type=NodeRegion.PROC),
            dram_region=NodeRegion(origin=PhyDim2(0, 0),
                                   dim=PhyDim2(1, 1),
                                   type=NodeRegion.DRAM),
            src_data_region=NodeRegion(origin=PhyDim2(0, 0),
                                       dim=PhyDim2(1, 1),
                                       type=NodeRegion.DRAM),
            dst_data_region=NodeRegion(origin=PhyDim2(0, 0),
                                       dim=PhyDim2(1, 1),
                                       type=NodeRegion.DRAM),
            dim_array=PhyDim2(16, 16),
            size_gbuf=65536,
            size_regf=64,
            array_bus_width=float('inf'),
            dram_bandwidth=float('inf'),
            no_time_mux=False)

        self.none_cstr = SchedulingConstraint()

        part = PartitionScheme(order=range(pe.NUM), pdims=[(1, 1)] * pe.NUM)
        self.ifmap_layout = DataLayout(
            frngs=(FmapRange((0, 0, 0, 0), (2, 4, 16, 16)), ),
            regions=(self.resource.src_data_region, ),
            parts=(part, ))

        self.sched_seq = (2, 0, 0)
    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
    def test_ofmap_local(self):
        ''' With locally stored ofmaps. '''
        layer = self.layers['BASE']

        part = PartitionScheme(order=(pe.BATP, pe.INPP, pe.OUTP, pe.OFMP),
                               pdims=((4, 1), (1, 1), (1, 4), (1, 1)))

        nr = NodeRegion(origin=PhyDim2(3, 3),
                        dim=part.dim(),
                        type=NodeRegion.PROC)

        ilayout = self._make_data_layout(layer.nifm, layer.hifm, layer.wifm,
                                         PhyDim2(-3, -3), (1, 1), (1, 1),
                                         PhyDim2(1, 1))

        olayout = DataLayout(frngs=(FmapRange(
            (0, ) * 4,
            (self.batch_size, layer.nofm, layer.hofm, layer.wofm)), ),
                             regions=(nr, ),
                             parts=(part, ))

        filter_nodes = frozenset([PhyDim2(3, -3)])

        nhops = partition.unit_nhops_to_proc_region(layer, self.batch_size, nr,
                                                    part, filter_nodes,
                                                    ilayout, olayout,
                                                    self.options['BASE'])

        self.assertEqual(nhops[de.OFM], 0)
Ejemplo n.º 9
0
    def test_view(self):
        ''' Get view. '''
        frm = self.frm.copy()
        frm.add(FmapRange((0, 0, 0, 16), (4, 4, 16, 20)),
                (PhyDim2(2, 2), PhyDim2(3, 3)))
        frm.add(FmapRange((0, 0, 16, 0), (4, 4, 20, 20)),
                (PhyDim2(1, 1), PhyDim2(3, 3), PhyDim2(5, 5)))
        dly = DataLayout(origin=PhyDim2(1, 1), frmap=frm, type=NodeRegion.DATA)

        cfr = dly.frmap.complete_fmap_range()
        counters = dly.frmap.rget_counter(cfr)
        nhops = dly.total_transfer_nhops(cfr, PhyDim2(1, 2))

        dly1 = dly.view(PhyDim2(-1, -1))
        self.assertEqual(dly1.origin, PhyDim2(0, 0), 'view: origin')
        self.assertEqual(dly1.type, dly.type, 'view: type')
        self.assertEqual(dly1.frmap.complete_fmap_range(), cfr,
                         'view: complete_fmap_range')
        self.assertDictEqual(dly1.frmap.rget_counter(cfr), counters,
                             'view: counter')
        self.assertEqual(
            dly1.total_transfer_nhops(cfr,
                                      PhyDim2(1, 2) + PhyDim2(-1, -1)), nhops,
            'view: nhops')

        dly2 = dly.view(PhyDim2(3, 1))
        self.assertEqual(dly2.type, dly.type, 'view: type')
        self.assertEqual(dly2.frmap.complete_fmap_range(), cfr,
                         'view: complete_fmap_range')
        self.assertDictEqual(dly2.frmap.rget_counter(cfr), counters,
                             'view: counter')
        self.assertEqual(
            dly2.total_transfer_nhops(cfr,
                                      PhyDim2(1, 2) + PhyDim2(3, 1)), nhops,
            'view: nhops')
Ejemplo n.º 10
0
    def setUp(self):

        self.scheme = OrderedDict([
            ('cost', 9.876 + 1.234),
            ('time', 123.4),
            ('ops', 1234),
            ('num_nodes', 4),
            ('cost_op', 1.234),
            ('cost_access', 9.876),
            ('cost_noc', 0),
            ('cost_static', 0),
            ('proc_time', 59),
            ('bus_time', 40),
            ('dram_time', 120),
            ('access', [[2, 3, 4], [30, 40, 50], [400, 500, 600],
                        [5000, 6000, 7000]]),
            ('remote_gbuf_access', [0, 0, 0]),
            ('total_nhops', [123, 456, 789]),
            ('fetch', [[1, 2, 1], [3, 4, 5]]),
        ])

        part = PartitionScheme(order=range(pe.NUM), pdims=[(1, 1)] * pe.NUM)
        self.ofmap_layout = DataLayout(
            frngs=(FmapRange((0, 0, 0, 0), (2, 4, 16, 16)), ),
            regions=(NodeRegion(origin=PhyDim2(0, 0),
                                dim=PhyDim2(1, 1),
                                type=NodeRegion.DRAM), ),
            parts=(part, ))

        self.sched_seq = (2, 0, 0)
Ejemplo n.º 11
0
 def test_complete_fmap_range(self):
     ''' Get complete_fmap_range. '''
     dl = DataLayout(frngs=(self.frng1,
                            FmapRange((0, 4, 0, 0), (4, 8, 16, 16))),
                     regions=(self.region1, self.region2),
                     parts=(self.part1, self.part2))
     self.assertEqual(dl.complete_fmap_range(),
                      FmapRange((0, 0, 0, 0), (4, 8, 16, 16)))
Ejemplo n.º 12
0
 def test_invalid_frmap_incomplete(self):
     ''' Invalid frmap incomplete. '''
     frm = self.frm.copy()
     frm.add(FmapRange((4, 4, 16, 16), (5, 5, 19, 19)), (PhyDim2(4, 4), ))
     with self.assertRaisesRegexp(ValueError, 'DataLayout: .*frmap.*'):
         _ = DataLayout(origin=PhyDim2(2, 3),
                        frmap=frm,
                        type=NodeRegion.DATA)
Ejemplo n.º 13
0
    def setUp(self):

        self.layers = {}
        self.layers['BASE'] = ConvLayer(8, 16, 28, 3)
        self.layers['POOL'] = PoolingLayer(16, 28, 2)
        self.layers['LR'] = LocalRegionLayer(16, 28, nreg=3, sreg=1)

        self.batch_size = 4

        self.cost = Cost(mac_op=1,
                         mem_hier=(200, 6, 2, 1),
                         noc_hop=50,
                         idl_unit=50)

        self.none_cstr = SchedulingConstraint()
        self.cstr = SchedulingConstraint(topofm=1, topbat=self.batch_size)

        self.resource = Resource(
            proc_region=NodeRegion(origin=PhyDim2(0, 0),
                                   dim=PhyDim2(4, 4),
                                   type=NodeRegion.PROC),
            dram_region=NodeRegion(origin=PhyDim2(0, 0),
                                   dim=PhyDim2(4, 1),
                                   type=NodeRegion.DRAM),
            src_data_region=NodeRegion(origin=PhyDim2(0, 0),
                                       dim=PhyDim2(4, 1),
                                       type=NodeRegion.DRAM),
            dst_data_region=NodeRegion(origin=PhyDim2(0, 0),
                                       dim=PhyDim2(4, 1),
                                       type=NodeRegion.DRAM),
            dim_array=PhyDim2(16, 16),
            size_gbuf=65536,
            size_regf=64,
            array_bus_width=float('inf'),
            dram_bandwidth=float('inf'),
            no_time_mux=False)

        self.options = Option(partition_hybrid=True,
                              partition_batch=True,
                              partition_ifmaps=True,
                              ntops=10)

        self.ifmap_layouts = {}
        part = PartitionScheme(order=(pe.INPP, pe.BATP, pe.OUTP, pe.OFMP),
                               pdims=((1, 2), (2, 1), (1, 2), (2, 1)))
        for wlkey in self.layers:
            input_layer = self.layers[wlkey].input_layer()
            self.ifmap_layouts[wlkey] = DataLayout(
                frngs=(FmapRange((0, 0, 0, 0),
                                 FmapPosition(b=self.batch_size,
                                              n=input_layer.nofm,
                                              h=input_layer.hofm,
                                              w=input_layer.wofm)), ),
                regions=(self.resource.src_data_region, ),
                parts=(part.projection(self.resource.src_data_region,
                                       appl2frng=True), ))

        self.sched_seq = (2, 0, 1)
Ejemplo n.º 14
0
 def test_merge_invalid_region_type(self):
     ''' Merge invalid region type. '''
     dly = DataLayout(origin=self.dly.origin,
                      frmap=self.dly.frmap,
                      type=NodeRegion.PROC)
     with self.assertRaisesRegexp(ValueError, 'DataLayout: .*type.*'):
         _ = self.dly.merge('|', dly)
     with self.assertRaisesRegexp(ValueError, 'DataLayout: .*type.*'):
         _ = self.dly.merge('+', dly)
Ejemplo n.º 15
0
    def test_is_in_region(self):
        ''' Whether is in region. '''
        nr1 = NodeRegion(dim=PhyDim2(2, 2),
                         origin=PhyDim2(1, 1),
                         type=NodeRegion.DATA)
        self.assertTrue(self.dly.is_in_region(nr1))
        nr2 = NodeRegion(dim=PhyDim2(3, 3),
                         origin=PhyDim2(0, 0),
                         type=NodeRegion.DATA)
        self.assertTrue(self.dly.is_in_region(nr2))
        nr3 = NodeRegion(dim=PhyDim2(2, 2),
                         origin=PhyDim2(0, 0),
                         type=NodeRegion.DATA)
        self.assertFalse(self.dly.is_in_region(nr3))
        nr4 = NodeRegion(dim=PhyDim2(3, 3),
                         origin=PhyDim2(0, 0),
                         type=NodeRegion.PROC)
        self.assertFalse(self.dly.is_in_region(nr4))

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

        nr4 = NodeRegion(dim=PhyDim2(3, 3),
                         origin=PhyDim2(1, 1),
                         type=NodeRegion.DATA)
        self.assertFalse(dly.is_in_region(nr4))
        nr5 = NodeRegion(dim=PhyDim2(4, 4),
                         origin=PhyDim2(1, 1),
                         type=NodeRegion.DATA)
        self.assertTrue(dly.is_in_region(nr5))

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

        self.assertFalse(dly.is_in_region(nr5))
        nr6 = NodeRegion(dim=PhyDim2(7, 7),
                         origin=PhyDim2(0, 0),
                         type=NodeRegion.DATA)
        self.assertTrue(dly.is_in_region(nr6))
Ejemplo n.º 16
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)
Ejemplo n.º 17
0
    def test_invalid_args(self):
        ''' Invalid arguments. '''
        with self.assertRaisesRegexp(TypeError, 'DataLayout: .*frngs.*'):
            _ = DataLayout(frngs=None,
                           regions=(self.region1, ),
                           parts=(self.part1, ))
        with self.assertRaisesRegexp(TypeError,
                                     'DataLayout: .*elements in frngs.*'):
            _ = DataLayout(frngs=(None, ),
                           regions=(self.region1, ),
                           parts=(self.part1, ))

        with self.assertRaisesRegexp(TypeError, 'DataLayout: .*regions.*'):
            _ = DataLayout(frngs=(self.frng1, ),
                           regions=None,
                           parts=(self.part1, ))
        with self.assertRaisesRegexp(TypeError,
                                     'DataLayout: .*elements in regions.*'):
            _ = DataLayout(frngs=(self.frng1, ),
                           regions=self.region1,
                           parts=(self.part1, ))

        with self.assertRaisesRegexp(TypeError, 'DataLayout: .*parts.*'):
            _ = DataLayout(frngs=(self.frng1, ),
                           regions=(self.region1, ),
                           parts=None)
        with self.assertRaisesRegexp(TypeError,
                                     'DataLayout: .*elements in parts.*'):
            _ = DataLayout(frngs=(self.frng1, ),
                           regions=(self.region1, ),
                           parts=self.part1)
Ejemplo n.º 18
0
    def test_concat_unmatch(self):
        ''' Concat 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))
        ]:
            dl = DataLayout(frngs=(fr, ),
                            regions=(self.region1, ),
                            parts=(self.part1, ))

            with self.assertRaisesRegexp(ValueError, 'DataLayout: .*match.*'):
                _ = DataLayout.concat(self.dl1, dl)
    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)
Ejemplo n.º 20
0
    def setUp(self):
        self.frng1 = FmapRange((0, 0, 0, 0), (4, 4, 16, 16))
        self.region1 = NodeRegion(dim=PhyDim2(2, 2),
                                  origin=PhyDim2(1, 1),
                                  type=NodeRegion.DRAM)
        self.part1 = PartitionScheme(order=range(pe.NUM),
                                     pdims=(PhyDim2(1, 1), PhyDim2(2, 1),
                                            PhyDim2(1, 2), PhyDim2(1, 1)))

        self.frng2 = FmapRange((0, 0, 0, 0), (4, 3, 16, 16))
        self.region2 = NodeRegion(dim=PhyDim2(2, 1),
                                  origin=PhyDim2(0, 0),
                                  type=NodeRegion.DRAM)
        self.part2 = PartitionScheme(order=range(pe.NUM),
                                     pdims=(PhyDim2(2, 1), PhyDim2(1, 1),
                                            PhyDim2(1, 1), PhyDim2(1, 1)))

        self.dl1 = DataLayout(frngs=(self.frng1, ),
                              regions=(self.region1, ),
                              parts=(self.part1, ))
        self.dl2 = DataLayout(frngs=(self.frng2, ),
                              regions=(self.region2, ),
                              parts=(self.part2, ))
Ejemplo n.º 21
0
    def test_nhops_to(self):
        ''' Get nhops_to. '''
        fr = FmapRange((0, ) * 4, (4, 4, 16, 16))
        nhops = 2 * 4 * 8 * 16 * (5 + 6 + 6 + 7)
        self.assertEqual(self.dl1.nhops_to(fr, PhyDim2(-1, -2)), nhops)

        frng1 = FmapRange((0, 4, 0, 0), (4, 8, 16, 16))
        dl = DataLayout(frngs=(self.frng1, frng1),
                        regions=(self.region1, self.region2),
                        parts=(self.part1, self.part2))
        self.assertEqual(dl.nhops_to(fr, PhyDim2(-1, -2)), nhops)

        fr = FmapRange((0, ) * 4, (16, ) * 4)
        nhops += 2 * 4 * 16 * 16 * (3 + 4)
        self.assertEqual(dl.nhops_to(fr, PhyDim2(-1, -2)), nhops)
Ejemplo n.º 22
0
    def test_fmap_range_map(self):
        ''' Get fmap_range_map. '''
        dl = DataLayout(frngs=(self.frng1,
                               FmapRange((0, 4, 0, 0), (4, 8, 16, 16))),
                        regions=(self.region1, self.region2),
                        parts=(self.part1, self.part2))
        frmap = dl.fmap_range_map()

        self.assertEqual(frmap.complete_fmap_range(), dl.complete_fmap_range())
        self.assertSetEqual(
            set(frmap.items()),
            {(FmapRange((0, 0, 0, 0), (2, 4, 8, 16)), PhyDim2(1, 1)),
             (FmapRange((2, 0, 0, 0), (4, 4, 8, 16)), PhyDim2(1, 2)),
             (FmapRange((0, 0, 8, 0), (2, 4, 16, 16)), PhyDim2(2, 1)),
             (FmapRange((2, 0, 8, 0), (4, 4, 16, 16)), PhyDim2(2, 2)),
             (FmapRange((0, 4, 0, 0), (4, 6, 16, 16)), PhyDim2(0, 0)),
             (FmapRange((0, 6, 0, 0), (4, 8, 16, 16)), PhyDim2(1, 0))})
Ejemplo n.º 23
0
    def test_nhops_to_multidests_fwd(self):
        ''' Get nhops_to multiple destinations forwarding. '''
        fr = FmapRange((0, ) * 4, (4, 4, 16, 16))
        # First to (2, 2), then (2, 2) to (-1, -2), (-1, -2) to (-2, -3).
        nhops = 2 * 4 * 8 * 16 * (2 + 1 + 1 + 0) \
                + 2 * 4 * 8 * 16 * (4 * 7) \
                + 2 * 4 * 8 * 16 * (4 * 2)
        self.assertEqual(
            self.dl1.nhops_to(fr,
                              PhyDim2(-1, -2),
                              PhyDim2(-2, -3),
                              PhyDim2(2, 2),
                              forwarding=True), nhops)

        frng1 = FmapRange((0, 4, 0, 0), (4, 8, 16, 16))
        dl = DataLayout(frngs=(self.frng1, frng1),
                        regions=(self.region1, self.region2),
                        parts=(self.part1, self.part2))
        self.assertEqual(
            dl.nhops_to(fr,
                        PhyDim2(-1, -2),
                        PhyDim2(-2, -3),
                        PhyDim2(2, 2),
                        forwarding=True), nhops)

        nhops += 2 * 4 * 16 * 16 * ((3 + 4) + 2 * 7 + 2 * 2)
        fr = FmapRange((0, ) * 4, (16, ) * 4)
        self.assertEqual(
            dl.nhops_to(fr,
                        PhyDim2(-1, -2),
                        PhyDim2(-2, -3),
                        PhyDim2(2, 2),
                        forwarding=True), nhops)

        # (2, 2) to (3, 10) and (8, 4)
        nhops += 4 * 8 * 16 * 16 * (9 + 8)
        self.assertEqual(
            dl.nhops_to(fr,
                        PhyDim2(-1, -2),
                        PhyDim2(-2, -3),
                        PhyDim2(2, 2),
                        PhyDim2(3, 10),
                        PhyDim2(8, 4),
                        forwarding=True), nhops)
Ejemplo n.º 24
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 _make_data_layout(self, nfm, hfm, wfm, origin, bdim, ndim, dims):
        ''' Make a DataLayout instance. '''
        frng = FmapRange((0, ) * 4, (self.batch_size, nfm, hfm, wfm))

        region = NodeRegion(origin=origin, dim=dims, type=NodeRegion.DRAM)

        # From top to bottom: h, w, b, n.
        order = (pe.OFMP, pe.BATP, pe.OUTP, pe.INPP)
        pdims = [None] * pe.NUM

        pdims[pe.BATP] = PhyDim2(*bdim)
        pdims[pe.OUTP] = PhyDim2(*ndim)
        pdims[pe.OFMP] = PhyDim2(h=dims.h // bdim[0] // ndim[0],
                                 w=dims.w // bdim[1] // ndim[1])
        pdims[pe.INPP] = PhyDim2(1, 1)

        part = PartitionScheme(order=order, pdims=pdims)

        return DataLayout(frngs=(frng, ), regions=(region, ), parts=(part, ))
Ejemplo n.º 26
0
    def test_is_in(self):
        ''' Whether is_in. '''
        nr1 = self.region1
        self.assertTrue(self.dl1.is_in(nr1))

        # Extend dim.
        nr2 = NodeRegion(dim=PhyDim2(5, 5), origin=nr1.origin, type=nr1.type)
        self.assertTrue(self.dl1.is_in(nr2))

        # Move origin.
        nr3 = NodeRegion(origin=PhyDim2(0, 0), dim=nr2.dim, type=nr2.type)
        self.assertTrue(self.dl1.is_in(nr3))

        # Change type, not in.
        nr4 = NodeRegion(type=NodeRegion.PROC, origin=nr3.origin, dim=nr3.dim)
        self.assertFalse(self.dl1.is_in(nr4))

        # Move origin to not containing.
        nr5 = NodeRegion(origin=PhyDim2(0, 0), dim=nr1.dim, type=nr1.type)
        self.assertFalse(self.dl1.is_in(nr5))

        # Multi-cover.
        nr6_1 = NodeRegion(origin=PhyDim2(1, 1),
                           dim=PhyDim2(2, 1),
                           type=nr1.type)
        nr6_2 = NodeRegion(origin=PhyDim2(1, 2),
                           dim=PhyDim2(2, 1),
                           type=nr1.type)
        self.assertTrue(self.dl1.is_in(nr6_1, nr6_2))

        # Multiple fmap ranges.
        frng1 = FmapRange((0, 4, 0, 0), (4, 8, 16, 16))
        dl = DataLayout(frngs=(self.frng1, frng1),
                        regions=(self.region1, self.region2),
                        parts=(self.part1, self.part2))
        self.assertFalse(dl.is_in(self.region1))
        self.assertTrue(dl.is_in(self.region1, self.region2))
        self.assertTrue(
            dl.is_in(
                NodeRegion(origin=PhyDim2(0, 0),
                           dim=PhyDim2(50, 50),
                           type=self.region1.type)))
Ejemplo n.º 27
0
    def test_total_transfer_nhops(self):
        ''' Get total_transfer_nhops. '''
        fr = FmapRange((0, ) * 4, (4, 4, 16, 16))
        nhops = 2 * 4 * 8 * 16 * (5 + 6 + 6 + 7)
        self.assertEqual(self.dly.total_transfer_nhops(fr, PhyDim2(-1, -2)),
                         nhops, 'total_transfer_nhops')

        frm = self.frm.copy()
        frm.add(FmapRange((0, 0, 0, 16), (4, 4, 16, 20)),
                (PhyDim2(2, 2), PhyDim2(3, 3)))
        frm.add(FmapRange((0, 0, 16, 0), (4, 4, 20, 20)),
                (PhyDim2(1, 1), PhyDim2(3, 3), PhyDim2(5, 5)))
        dly = DataLayout(origin=PhyDim2(1, 1), frmap=frm, type=NodeRegion.DATA)

        self.assertEqual(dly.total_transfer_nhops(fr, PhyDim2(-1, -2)), nhops,
                         'total_transfer_nhops')

        nhops += 4 * 4 * 16 * 4 * (9 + 11) + 4 * 4 * 4 * 20 * (7 + 11 + 15)
        fr = FmapRange((0, ) * 4, (20, ) * 4)
        self.assertEqual(dly.total_transfer_nhops(fr, PhyDim2(-1, -2)), nhops,
                         'total_transfer_nhops')
Ejemplo n.º 28
0
    def test_nhops_to_multidests(self):
        ''' Get nhops_to multiple destinations. '''
        fr = FmapRange((0, ) * 4, (4, 4, 16, 16))
        nhops = 2 * 4 * 8 * 16 * (5 + 6 + 6 + 7) \
                + 2 * 4 * 8 * 16 * (7 + 8 + 8 + 9) \
                + 2 * 4 * 8 * 16 * (2 + 1 + 1 + 0)
        self.assertEqual(
            self.dl1.nhops_to(fr, PhyDim2(-1, -2), PhyDim2(-2, -3),
                              PhyDim2(2, 2)), nhops)

        frng1 = FmapRange((0, 4, 0, 0), (4, 8, 16, 16))
        dl = DataLayout(frngs=(self.frng1, frng1),
                        regions=(self.region1, self.region2),
                        parts=(self.part1, self.part2))
        self.assertEqual(
            dl.nhops_to(fr, PhyDim2(-1, -2), PhyDim2(-2, -3), PhyDim2(2, 2)),
            nhops)

        fr = FmapRange((0, ) * 4, (16, ) * 4)
        nhops += 2 * 4 * 16 * 16 * ((3 + 4) + (5 + 6) + (4 + 3))
        self.assertEqual(
            dl.nhops_to(fr, PhyDim2(-1, -2), PhyDim2(-2, -3), PhyDim2(2, 2)),
            nhops)
Ejemplo n.º 29
0
    def test_is_in_folded(self):
        ''' Whether is_in with folded regions. '''
        # (1, 1/2), (2/3, 0/1/2), (4, 1/2)
        nr1 = NodeRegion(origin=PhyDim2(1, 1),
                         dim=PhyDim2(1, 10),
                         type=self.region1.type,
                         wtot=3,
                         wbeg=2)
        # (1, 1/2), (2, 2)
        nr2 = NodeRegion(origin=PhyDim2(1, 1),
                         dim=PhyDim2(1, 3),
                         type=self.region1.type,
                         wtot=3,
                         wbeg=2)
        self.assertTrue(self.dl1.is_in(nr1))
        self.assertFalse(self.dl1.is_in(nr2))

        # (1-2, 2), (3-4/5-6/7-8, 0/1/2)
        region = NodeRegion(origin=PhyDim2(1, 2),
                            dim=PhyDim2(2, 10),
                            type=self.region1.type,
                            wtot=3,
                            wbeg=1)
        part = PartitionScheme(order=range(pe.NUM),
                               pdims=(PhyDim2(1, 5), PhyDim2(2, 1),
                                      PhyDim2(1, 2), PhyDim2(1, 1)))
        dl = DataLayout(frngs=self.dl1.frngs,
                        regions=(region, ),
                        parts=(part, ))
        # (1-2, 1/2), (3-4/5-6, -1/0/1/2), (7-8, 0/1/2)
        nr3 = NodeRegion(origin=PhyDim2(1, 1),
                         dim=PhyDim2(2, 13),
                         type=self.region1.type,
                         wtot=4,
                         wbeg=2)
        self.assertTrue(dl.is_in(nr3))
        self.assertFalse(dl.is_in(nr2))
Ejemplo n.º 30
0
    def test_invalid_frngs(self):
        ''' Invalid frngs. '''
        # No frngs.
        with self.assertRaisesRegexp(ValueError, 'DataLayout: .*frng.*'):
            _ = DataLayout(frngs=tuple(),
                           regions=(self.region1, ),
                           parts=(self.part1, ))

        # Not begin at 0.
        with self.assertRaisesRegexp(ValueError, 'DataLayout: .*frng.*'):
            _ = DataLayout(frngs=(FmapRange((0, 4, 0, 0),
                                            (4, 8, 16, 16)), self.frng1),
                           regions=(self.region1, self.region2),
                           parts=(self.part1, self.part2))

        # b, h, w mismatch.
        with self.assertRaisesRegexp(ValueError, 'DataLayout: .*frng.*'):
            _ = DataLayout(frngs=(self.frng1,
                                  FmapRange((0, 4, 0, 0), (3, 8, 16, 16))),
                           regions=(self.region1, self.region2),
                           parts=(self.part1, self.part2))
        with self.assertRaisesRegexp(ValueError, 'DataLayout: .*frng.*'):
            _ = DataLayout(frngs=(self.frng1,
                                  FmapRange((0, 4, 0, 0), (4, 8, 12, 16))),
                           regions=(self.region1, self.region2),
                           parts=(self.part1, self.part2))
        with self.assertRaisesRegexp(ValueError, 'DataLayout: .*frng.*'):
            _ = DataLayout(frngs=(self.frng1,
                                  FmapRange((0, 4, 0, 0), (4, 8, 16, 12))),
                           regions=(self.region1, self.region2),
                           parts=(self.part1, self.part2))

        # n discontinuous.
        with self.assertRaisesRegexp(ValueError, 'DataLayout: .*frng.*'):
            _ = DataLayout(frngs=(self.frng1,
                                  FmapRange((0, 5, 0, 0), (4, 8, 16, 16))),
                           regions=(self.region1, self.region2),
                           parts=(self.part1, self.part2))