def XDLFloadWeights(args, Weights, outChans, inChans, kernH, kernW, layerName, isxdnnv3=False): print("Loading weights/bias/quant_params to FPGA...") if isxdnnv3 == "True": size = xdnn.v3computeWeightsBiasQuantSize( kernW, kernH, outChans, int(math.ceil(float(inChans) / float(96))), 0, 0, False) size = size * 2 else: size = xdnn.computeWeightsBiasQuantSize(\ kernW, kernH, inChans, outChans, True if args['quantizecfg'] else False) blob = xdnn.makeWeightsBiasQuantBlob(size) bias = [0 for v in range(outChans)] if isxdnnv3 == "True": offset = xdnn.v3fillWeightsBiasQuantBlob(blob, 0, args['quantizecfg'], Weights, args['scaleA'], bias, args['scaleB'], kernW, kernH, inChans, outChans, layerName) else: offset = xdnn.fillWeightsBiasQuantBlob(blob, 0, args['quantizecfg'], Weights, args['scaleA'], bias, args['scaleB'], kernW, kernH, inChans, outChans, layerName) layer2OffsetMap = "%s:%d" % (layerName, 0) fps = xdnn.loadBlobToDdr(blob, size, layer2OffsetMap, int(args['PE'])) return (blob, fps)
def XDLFBunchloadWeightsBiasQuantLatestRepl(args, allLayerNames, allLayersWeightsBiasQuantizeKey, allConvLayerNamesParams, size, isxdnnv3): print("Loading weights/bias/quant_params to FPGA...") blob = xdnn.makeWeightsBiasQuantBlob(size) offset = 0 layer2OffsetMapStr = "" for layerName in allLayerNames: if layer2OffsetMapStr != "": layer2OffsetMapStr += "," layer2OffsetMapStr += "%s:%d" % (layerName, offset) if isxdnnv3 == "True": offset += xdnn.v3fillWeightsBiasQuantBlob( blob, offset, args['quantizecfg'], allLayersWeightsBiasQuantizeKey[layerName]['weights'], args['scaleA'], allLayersWeightsBiasQuantizeKey[layerName]['Bias'], args['scaleB'], allLayersWeightsBiasQuantizeKey[layerName]['kernW'], allLayersWeightsBiasQuantizeKey[layerName]['kernH'], allLayersWeightsBiasQuantizeKey[layerName]['inChans'], allLayersWeightsBiasQuantizeKey[layerName]['outChans'], int(allConvLayerNamesParams[layerName]['srcFullSectNum']), int(allConvLayerNamesParams[layerName]['srcReplSectNum']), int(allConvLayerNamesParams[layerName]['srcReplUnitNum']), int(allConvLayerNamesParams[layerName]['srcReplUnitWidth']), int(allConvLayerNamesParams[layerName]['convHalfRateMode']), layerName) else: offset += xdnn.fillWeightsBiasQuantBlob( blob, offset, args['quantizecfg'], allLayersWeightsBiasQuantizeKey[layerName]['weights'], args['scaleA'], allLayersWeightsBiasQuantizeKey[layerName]['Bias'], args['scaleB'], allLayersWeightsBiasQuantizeKey[layerName]['kernW'], allLayersWeightsBiasQuantizeKey[layerName]['kernH'], allLayersWeightsBiasQuantizeKey[layerName]['inChans'], allLayersWeightsBiasQuantizeKey[layerName]['outChans'], layerName) fps = xdnn.loadBlobToDdr(blob, size, layer2OffsetMapStr, int(args['PE'])) return (blob, fps)
def XDLFBunchloadWeightsBiasQuant(args, allLayerNames, allLayersWeightsBiasQuantizeKey, size, isxdnnv3): print("Loading weights/bias/quant_params to FPGA...") print("MNDBG total A size %d" % size) blob = xdnn.makeWeightsBiasQuantBlob(size) layer2OffsetMapStr = "" offset = 0 for layerName in allLayerNames: if layer2OffsetMapStr != "": layer2OffsetMapStr += "," layer2OffsetMapStr += "%s:%d" % (layerName, offset) if isxdnnv3 == "True": offset += xdnn.v3fillWeightsBiasQuantBlob( blob, offset, args['quantizecfg'], allLayersWeightsBiasQuantizeKey[layerName]['weights'], args['scaleA'], allLayersWeightsBiasQuantizeKey[layerName]['Bias'], args['scaleB'], allLayersWeightsBiasQuantizeKey[layerName]['kernW'], allLayersWeightsBiasQuantizeKey[layerName]['kernH'], allLayersWeightsBiasQuantizeKey[layerName]['inChans'], allLayersWeightsBiasQuantizeKey[layerName]['outChans'], layerName) else: offset += xdnn.fillWeightsBiasQuantBlob( blob, offset, args['quantizecfg'], allLayersWeightsBiasQuantizeKey[layerName]['weights'], args['scaleA'], allLayersWeightsBiasQuantizeKey[layerName]['Bias'], args['scaleB'], allLayersWeightsBiasQuantizeKey[layerName]['kernW'], allLayersWeightsBiasQuantizeKey[layerName]['kernH'], allLayersWeightsBiasQuantizeKey[layerName]['inChans'], allLayersWeightsBiasQuantizeKey[layerName]['outChans'], layerName) fps = xdnn.loadBlobToDdr(blob, size, layer2OffsetMapStr, int(args['PE'])) return (blob, fps)
def loadWeightsBiasQuant(args): print("Loading weights/bias/quant_params to FPGA...") size = 0 fi = 0 while True: fname = "%s/wbq_size_%d" % (args['datadir'], fi) if not os.path.isfile(fname): break with open(fname, 'r') as f: data = f.read() vals = data.strip().split(' ') vals = [int(v) for v in vals] if 'v2WeightsFormat' in args and args['v2WeightsFormat'] == 1: size += xdnn.computeWeightsBiasQuantSize(\ vals[0], vals[1], vals[2], vals[3], True if args['quantizecfg'] else False) else: size += xdnn.computeWeightsBiasQuantSize(\ vals[0], vals[0], vals[1], vals[2], True if args['quantizecfg'] else False) fi += 1 # print "ANDBG total A size %d" % size blob = xdnn.makeWeightsBiasQuantBlob(size) fi = 0 offset = 0 layer2OffsetMapStr = "" while True: fname = "%s/fwbqb_%d" % (args['datadir'], fi) if 'binaryFormatWeights' in args: if args['binaryFormatWeights'] == 1: fname = "%s/fwbqbbinary_%d.bin" % (args['datadir'], fi) if not os.path.isfile(fname): break layerName = None weights = None bias = None if 'v2WeightsFormat' in args and args['v2WeightsFormat'] == 1: kernWidth = None kernHeight = None else: kern = None inChans = None outChans = None if 'binaryFormatWeights' in args: if args['binaryFormatWeights'] == 1: if 'v2WeightsFormat' in args and args['v2WeightsFormat'] != 1: print( "Weights should be given in the v2WeightsFormat, KernSizex and KernSizey. If they are already\ in v2WeightsFormat, then set the argument v2WeightsFormat to 1" ) sys.exit(1) vals = [] (layerName, kernWidth, kernHeight, inChans, outChans, vals) = xdnn.readWeightsFile(fname) weights = [float(v) for v in vals] elif args['binaryFormatWeights'] == 0: with open(fname, 'r') as f: data = f.read() vals = data.strip().split(' ') layerName = vals[0] if 'v2WeightsFormat' in args and args[ 'v2WeightsFormat'] == 1: kernWidth = int(vals[1]) kernHeight = int(vals[2]) inChans = int(vals[3]) outChans = int(vals[4]) vals = vals[5:] else: kern = int(vals[1]) inChans = int(vals[2]) outChans = int(vals[3]) vals = vals[4:] weights = [float(v) for v in vals] else: with open(fname, 'r') as f: data = f.read() vals = data.strip().split(' ') layerName = vals[0] if 'v2WeightsFormat' in args and args['v2WeightsFormat'] == 1: kernWidth = int(vals[1]) kernHeight = int(vals[2]) inChans = int(vals[3]) outChans = int(vals[4]) vals = vals[5:] else: kern = int(vals[1]) inChans = int(vals[2]) outChans = int(vals[3]) vals = vals[4:] weights = [float(v) for v in vals] fname = "%s/fwbqb_bias_%d" % (args['datadir'], fi) with open(fname, 'r') as f: data = f.read() vals = data.strip().split(' ') if 'v2WeightsFormat' in args: if args['v2WeightsFormat'] == 1: vals = vals[5:] elif args['v2WeightsFormat'] == 0: vals = vals[4:] else: vals = vals[4:] bias = [float(v) for v in vals] if layer2OffsetMapStr != "": layer2OffsetMapStr += "," layer2OffsetMapStr += "%s:%d" % (layerName, offset) # print "ANDBG %d %s %d %d %d" % (offset, layerName, fi, len(weights), len(bias)) if 'v2WeightsFormat' in args and args['v2WeightsFormat'] == 1: offset += xdnn.fillWeightsBiasQuantBlob( blob, offset, args['quantizecfg'], weights, args['scaleA'], bias, args['scaleB'], kernWidth, kernHeight, inChans, outChans, layerName) else: offset += xdnn.fillWeightsBiasQuantBlob( blob, offset, args['quantizecfg'], weights, args['scaleA'], bias, args['scaleB'], kern, kern, inChans, outChans, layerName) fi += 1 xdnn.loadBlobToDdr(blob, size, layer2OffsetMapStr, int(args['PE'])) return blob
def loadWeightsBiasQuantv3(args): print("Loading weights/bias/quant_params to FPGA...") allConvLayerNames, allConvLayerNamesParams = parseCompilerFile( args['netcfg']) size = 0 fi = 0 while True: fname = "%s/fwbqb_%d" % (args['datadir'], fi) if not os.path.isfile(fname): break layerName = None weights = None bias = None kern = None inChans = None outChans = None with open(fname, 'r') as f: data = f.read() vals = data.strip().split(' ') layerName = vals[0] kern = int(vals[1]) assert kern == int(allConvLayerNamesParams[layerName]['kernW']) assert kern == int(allConvLayerNamesParams[layerName]['kernH']) inChans = int(vals[2]) assert inChans == int( allConvLayerNamesParams[layerName]['inChans']) outChans = int(vals[3]) # assert outChans == int(allConvLayerNamesParams[layerName]['outChans']) srcFullSectNum = int( allConvLayerNamesParams[layerName]['srcFullSectNum']) srcReplSectNum = int( allConvLayerNamesParams[layerName]['srcReplSectNum']) srcReplUnitNum = int( allConvLayerNamesParams[layerName]['srcReplUnitNum']) size += xdnn.v3computeWeightsBiasQuantSize(kern, kern, outChans, srcFullSectNum, srcReplSectNum, srcReplUnitNum, False) fi += 1 size = size * 2 blob = xdnn.makeWeightsBiasQuantBlob(size) fi = 0 offset = 0 layer2OffsetMapStr = "" while True: fname = "%s/fwbqb_%d" % (args['datadir'], fi) if not os.path.isfile(fname): break layerName = None weights = None bias = None kern = None inChans = None outChans = None with open(fname, 'r') as f: data = f.read() vals = data.strip().split(' ') layerName = vals[0] kern = int(vals[1]) assert kern == int(allConvLayerNamesParams[layerName]['kernW']) assert kern == int(allConvLayerNamesParams[layerName]['kernH']) inChans = int(vals[2]) assert inChans == int( allConvLayerNamesParams[layerName]['inChans']) outChans = int(vals[3]) # assert outChans == int(allConvLayerNamesParams[layerName]['outChans']) srcFullSectNum = int( allConvLayerNamesParams[layerName]['srcFullSectNum']) srcReplSectNum = int( allConvLayerNamesParams[layerName]['srcReplSectNum']) srcReplUnitNum = int( allConvLayerNamesParams[layerName]['srcReplUnitNum']) srcReplUnitWidth = int( allConvLayerNamesParams[layerName]['srcReplUnitWidth']) convHalfRateMode = int( allConvLayerNamesParams[layerName]['convHalfRateMode']) vals = vals[4:] weights = [float(v) for v in vals] fname = "%s/fwbqb_bias_%d" % (args['datadir'], fi) with open(fname, 'r') as f: data = f.read() vals = data.strip().split(' ') vals = vals[4:] bias = [float(v) for v in vals] if layer2OffsetMapStr != "": layer2OffsetMapStr += "," layer2OffsetMapStr += "%s:%d" % (layerName, offset) offset += xdnn.v3fillWeightsBiasQuantBlob( blob, offset, args['quantizecfg'], weights, args['scaleA'], bias, args['scaleB'], kern, kern, inChans, outChans, srcFullSectNum, srcReplSectNum, srcReplUnitNum, srcReplUnitWidth, convHalfRateMode, layerName) fi += 1 xdnn.loadBlobToDdr(blob, size, layer2OffsetMapStr, int(args['PE'])) return blob