Exemplo n.º 1
0
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)
Exemplo n.º 2
0
def XDLFBunchComputeSize(args,
                         outChans,
                         inChans,
                         kernH,
                         kernW,
                         isxdnnv3=False):

    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)

    return size
Exemplo n.º 3
0
def XDLFBunchComputeSizeLatestRepl(args,
                                   outChans,
                                   inChans,
                                   kernH,
                                   kernW,
                                   srcFullSectNum,
                                   srcReplSectNum,
                                   srcReplUnitNum,
                                   isxdnnv3=False):

    if isxdnnv3 == "True":
        size = xdnn.v3computeWeightsBiasQuantSize(kernW, kernH, outChans,
                                                  int(srcFullSectNum),
                                                  int(srcReplSectNum),
                                                  int(srcReplUnitNum), False)
        size = size * 2
    else:
        size = xdnn.computeWeightsBiasQuantSize(\
                    kernW, kernH, inChans, outChans, True if args['quantizecfg'] else False)

    return size
Exemplo n.º 4
0
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