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 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