def setUp(self): # AlexNet. self.convlayers = OrderedDict() self.convlayers['conv1'] = ConvLayer(3, 96, 55, 11, 4) self.convlayers['conv2'] = ConvLayer(48, 256, 27, 5) self.convlayers['conv3'] = ConvLayer(256, 384, 13, 3) self.convlayers['conv4'] = ConvLayer(192, 384, 13, 3) self.convlayers['conv5'] = ConvLayer(192, 256, 13, 3) self.fclayers = {} self.fclayers['fc1'] = FCLayer(256, 4096, 6) self.fclayers['fc2'] = FCLayer(4096, 4096) self.fclayers['fc3'] = FCLayer(4096, 1000) # LocalRegionLayer. self.lrlayers = {} self.lrlayers['pool1'] = PoolingLayer(64, 7, 2) self.lrlayers['pool2'] = PoolingLayer(29, 13, 3) self.lrlayers['pool3'] = PoolingLayer(32, 7, 2, strd=3) self.lrlayers['lr1'] = LocalRegionLayer(32, 7, nreg=5, sreg=1) self.lrlayers['lr2'] = LocalRegionLayer(32, 7, nreg=5, sreg=1, strd=2) # Fake layers. self.fake_layers = {} # With irregular nifm/nofm. self.fake_layers['IRR'] = ConvLayer(255, 383, 13, 3) # With small numbers of fmaps. self.fake_layers['SM'] = ConvLayer(5, 3, 13, 3) # With large FIL height. self.fake_layers['LGFIL'] = ConvLayer(64, 64, 13, 22) # Resource. self.resource = {} proc_region = NodeRegion(origin=PhyDim2(0, 0), dim=PhyDim2(1, 1), type=NodeRegion.PROC) data_region = NodeRegion(origin=PhyDim2(0, 0), dim=PhyDim2(1, 1), type=NodeRegion.DRAM) # Eyeriss, ISSCC'16, JSSC'17. self.resource['BASE'] = Resource(proc_region=proc_region, dram_region=data_region, src_data_region=data_region, dst_data_region=data_region, dim_array=PhyDim2(12, 14), size_gbuf=108 * 1024, size_regf=520, array_bus_width=float('inf'), dram_bandwidth=float('inf'), no_time_mux=False)
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, unit_static=50) self.resource = Resource( proc_region=NodeRegion(origin=PhyDim2(0, 0), dim=PhyDim2(4, 4), type=NodeRegion.PROC), data_regions=(NodeRegion(origin=PhyDim2(0, 0), dim=PhyDim2(4, 1), type=NodeRegion.DATA),), dim_array=PhyDim2(16, 16), size_gbuf=65536, size_regf=64) 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: self.ifmap_layouts[wlkey] = partition.get_ofmap_layout( self.layers[wlkey].input_layer(), self.batch_size, part, self.resource.src_data_region())
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)
def test_repr(self): ''' __repr__. ''' # pylint: disable=eval-used for l in [ LocalRegionLayer(64, 28, 2, 1), LocalRegionLayer(64, [28, 14], 1, [2, 4]), LocalRegionLayer(64, [28, 14], 1, [2, 4], 7), LocalRegionLayer(64, 28, 1, 4, 7) ]: self.assertIn('LocalRegionLayer', repr(l)) self.assertEqual(eval(repr(l)), l) for l in [ PoolingLayer(64, 28, 2), PoolingLayer(64, 28, 3, strd=2), PoolingLayer(64, [28, 14], [3, 4], strd=[2, 3]) ]: self.assertIn('PoolingLayer', repr(l)) self.assertEqual(eval(repr(l)), l)
def test_data_loops(self): ''' Get data_loops. ''' dls = LocalRegionLayer.data_loops() self.assertEqual(dls[de.FIL], DataDimLoops()) self.assertEqual(dls[de.IFM], DataDimLoops(le.OFM, le.BAT)) self.assertEqual(dls[de.OFM], DataDimLoops(le.OFM, le.BAT)) llayer = LocalRegionLayer(64, 28, 2, 1) player = PoolingLayer(64, 28, 2) self.assertTupleEqual(PoolingLayer.data_loops(), dls) self.assertTupleEqual(llayer.data_loops(), dls) self.assertTupleEqual(player.data_loops(), dls)
def setUp(self): self.layers = {} self.layers['BASE'] = ConvLayer(64, 64, 28, 3) self.layers['FC'] = FCLayer(4096, 1000, 6) self.layers['POOL'] = PoolingLayer(32, 7, 3, strd=2) self.layers['LR'] = LocalRegionLayer(32, 7, nreg=5, sreg=1) # With irregular nifm/nofm. self.layers['IRR'] = ConvLayer(255, 383, 13, 3) # With small numbers of fmaps. self.layers['SM'] = ConvLayer(5, 3, 13, 3) # Super small networks. No partitioning schemes. self.layers['SSM1'] = ConvLayer(1, 1, 2, 3) self.layers['SSM2'] = FCLayer(2, 2) self.layers['SSM3'] = PoolingLayer(1, 2, 2) self.batch_size = 8 self.dim_nodes = {} self.dim_nodes['BASE'] = PhyDim2(4, 4) self.dim_nodes['LG'] = PhyDim2(10, 10) self.dim_nodes['PRIME'] = PhyDim2(3, 3) self.options = {} # Irrelevant options. optdict = {'ntops': 10000} self.options['BASE'] = Option(partition_hybrid=True, partition_batch=True, partition_ifmaps=True, **optdict) self.options['NOBATP'] = Option(partition_hybrid=True, partition_batch=False, partition_ifmaps=True, **optdict) self.options['NOINPP'] = Option(partition_hybrid=True, partition_batch=True, partition_ifmaps=False, **optdict) self.options['NOHYB'] = Option(partition_hybrid=False, partition_batch=True, partition_ifmaps=False, **optdict) self.options['ACCFWD'] = Option(partition_hybrid=True, partition_batch=True, partition_ifmaps=True, hw_access_forwarding=True, **optdict) self.options['BUFSHR'] = Option(partition_hybrid=True, partition_batch=True, partition_ifmaps=True, hw_gbuf_sharing=True, **optdict)
def test_ifmap(self): ''' Get ifmap. ''' clayer = ConvLayer(3, 64, [28, 14], 3, strd=2) inlayer = clayer.input_layer() self.assertIsInstance(inlayer, Layer) self.assertEqual(inlayer.nofm, clayer.nifm, 'ConvLayer: nifm') self.assertEqual(inlayer.hofm, clayer.hifm, 'ConvLayer: hifm') self.assertEqual(inlayer.wofm, clayer.wifm, 'ConvLayer: wifm') llayer = LocalRegionLayer(64, 28, 1, 3, strd=2) inlayer = llayer.input_layer() self.assertIsInstance(inlayer, Layer) self.assertEqual(inlayer.nofm, llayer.nifm, 'LocalRegionLayer: nifm') self.assertEqual(inlayer.hofm, llayer.hifm, 'LocalRegionLayer: hifm') self.assertEqual(inlayer.wofm, llayer.wifm, 'LocalRegionLayer: wifm')
def test_ifmap_size(self): ''' Get ifmap size. ''' clayer = ConvLayer(3, 64, [28, 14], 3, strd=2) self.assertEqual(clayer.ifmap_size(2), ((28 - 1) * 2 + 3) * ((14 - 1) * 2 + 3) * 2, 'ConvLayer: ifmap_size') self.assertEqual(clayer.total_ifmap_size(2), ((28 - 1) * 2 + 3) * ((14 - 1) * 2 + 3) * 3 * 2, 'ConvLayer: total_ifmap_size') llayer = LocalRegionLayer(64, 28, 1, 3, strd=2) self.assertEqual(llayer.ifmap_size(2), ((28 - 1) * 2 + 3)**2 * 2, 'LocalRegionLayer: ifmap_size') self.assertEqual(llayer.total_ifmap_size(2), ((28 - 1) * 2 + 3)**2 * 64 * 2, 'LocalRegionLayer: total_ifmap_size')
def test_match_io_fmap_range(self): ''' ofmap and ifmap range match. ''' 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'): for pidx in part.gen_pidx(): ofr = partition.part_layer_ofmap_range( layer, self.batch_size, part, pidx) ifr = partition.part_layer_ifmap_range( layer, self.batch_size, part, pidx) self.assertEqual(ofr.size('b'), ifr.size('b')) if isinstance(layer, ConvLayer): ol = ConvLayer(nifm=ifr.size('n'), nofm=ofr.size('n'), sofm=(ofr.size('h'), ofr.size('w')), sfil=(layer.hfil, layer.wfil), strd=(layer.htrd, layer.wtrd)) il = ol.input_layer() self.assertEqual(il.nofm, ifr.size('n')) elif isinstance(layer, LocalRegionLayer): nofm_beg, nofm_end = ofr.beg_end('n')[0] nifm_beg, nifm_end = ifr.beg_end('n')[0] self.assertEqual(nifm_beg, max(0, \ nofm_beg - layer.nreg // 2)) self.assertEqual(nifm_end, min(layer.nifm, \ nofm_end + layer.nreg - layer.nreg // 2)) ol = LocalRegionLayer( nofm=ofr.size('n'), sofm=(ofr.size('h'), ofr.size('w')), nreg=layer.nreg, sreg=(layer.hreg, layer.wreg), strd=(layer.htrd, layer.wtrd)) il = ol.input_layer() self.assertEqual(il.hofm, ifr.size('h')) self.assertEqual(il.wofm, ifr.size('w'))
def test_valid_args(self): ''' Valid argument. ''' clayer = ConvLayer(3, 64, 28, 3, strd=2) self.assertEqual(clayer.nifm, 3, 'nifm') self.assertEqual(clayer.nofm, 64, 'nofm') self.assertEqual(clayer.hofm, 28, 'hofm') self.assertEqual(clayer.wofm, 28, 'wofm') self.assertEqual(clayer.hfil, 3, 'hfil') self.assertEqual(clayer.wfil, 3, 'wfil') self.assertEqual(clayer.htrd, 2, 'htrd') self.assertEqual(clayer.wtrd, 2, 'wtrd') llayer = LocalRegionLayer(64, 28, 1, 3, strd=2) self.assertEqual(llayer.nofm, 64, 'nofm') self.assertEqual(llayer.hofm, 28, 'hofm') self.assertEqual(llayer.wofm, 28, 'wofm') self.assertEqual(llayer.nreg, 1, 'nreg') self.assertEqual(llayer.hreg, 3, 'hreg') self.assertEqual(llayer.wreg, 3, 'wreg') self.assertEqual(llayer.htrd, 2, 'htrd') self.assertEqual(llayer.wtrd, 2, 'wtrd')
def test_is_valid_padding_sifm(self): ''' is_valid_padding_sifm. ''' clayer = ConvLayer(3, 64, [28, 14], [3, 1], [2, 4]) self.assertTrue(clayer.is_valid_padding_sifm([28 * 2, 14 * 4])) self.assertTrue(clayer.is_valid_padding_sifm([27 * 2 + 3, 13 * 4 + 1])) self.assertFalse(clayer.is_valid_padding_sifm([28, 14])) self.assertFalse(clayer.is_valid_padding_sifm([28 * 2, 14])) self.assertTrue(clayer.is_valid_padding_sifm([27 * 2 + 3, 13 * 4 + 3])) flayer = FCLayer(2048, 4096, sfil=2) self.assertTrue(flayer.is_valid_padding_sifm(2)) self.assertTrue(flayer.is_valid_padding_sifm(1)) self.assertTrue(flayer.is_valid_padding_sifm([1, 2])) llayer = LocalRegionLayer(64, 28, 2, 1) self.assertTrue(llayer.is_valid_padding_sifm(28)) self.assertFalse(llayer.is_valid_padding_sifm(28 - 1)) self.assertFalse(llayer.is_valid_padding_sifm(28 + 1)) player = PoolingLayer(64, 28, [2, 3], strd=[3, 2]) self.assertTrue(player.is_valid_padding_sifm([28 * 3, 28 * 2])) self.assertTrue(player.is_valid_padding_sifm([27 * 3 + 2, 27 * 2 + 3]))
def test_mix_sreg(self): ''' Mix region of n-dimension and h/w-dimension. ''' with self.assertRaisesRegexp(ValueError, 'LocalRegionLayer: .*mix.*'): _ = LocalRegionLayer(64, 28, 2, 2)
def test_invalid_sreg(self): ''' Invalid region size. ''' with self.assertRaisesRegexp(ValueError, 'LocalRegionLayer: .*sreg.*'): _ = LocalRegionLayer(64, 28, 1, [2, 4, 6])
def test_region(self): ''' Get region size. ''' llayer = LocalRegionLayer(64, 28, 2, 1) self.assertEqual(llayer.region_size(), 2) llayer = LocalRegionLayer(64, 28, 1, [2, 4]) self.assertEqual(llayer.region_size(), 2 * 4)
def test_ops(self): ''' Get ops. ''' llayer = LocalRegionLayer(64, 28, 2, 1) self.assertEqual(llayer.ops_per_neuron(), 2) llayer = LocalRegionLayer(64, 28, 1, 3) self.assertEqual(llayer.ops_per_neuron(), 9)