Esempio n. 1
0
def filter_stats(G, pnode, fnode, anode, channel_details=None, qrec=None):
    stats = {}
    if isinstance(anode, MultiplicativeBiasParameters):
        if anode.has_mul_bias:
            stats['mul_biases'] = mul_biases = astats(anode.mul_biases)
            mul_biases['qstats'] = calculate_qsnrs(anode.mul_biases,
                                                   mul_biases['ibits'],
                                                   force_ideal=False)
        elif qrec and qrec.ktype.startswith('scaled'):
            stats['mul_biases'] = mul_biases = astats(qrec.mul_biases_fps)
            mul_biases['qstats'] = calculate_qsnrs(qrec.mul_biases_fps,
                                                   mul_biases['ibits'],
                                                   force_ideal=False)
    in_edges = G.indexed_in_edges(pnode.name)
    qweights = in_edges[1].from_node.dqvalue
    qbiases = in_edges[2].from_node.dqvalue

    stats['biases'] = biases = astats(qbiases)
    biases['qstats'] = calculate_qsnrs(qbiases,
                                       biases['ibits'],
                                       force_ideal=False)

    stats['weights'] = weights = astats(
        qweights,
        channel_dim=anode.filter.get_order_idx('out_c'),
        channel_details=channel_details)
    weights['qstats'] = calculate_qsnrs(qweights,
                                        weights['ibits'],
                                        force_ideal=False)
    # store the statistics into the graph for later use
    anode.stats = stats
    stats['step_idx'] = pnode.step_idx
    return anode.stats
Esempio n. 2
0
def filter_stats(node):
    stats = {}
    if node.has_bias:
        stats['biases'] = biases = astats(node.biases)
        biases['qstats'] = calculate_qsnrs(node.biases,
                                           biases['ibits'],
                                           force_ideal=False)
    stats['weights'] = weights = astats(node.weights)
    weights['qstats'] = calculate_qsnrs(node.weights,
                                        weights['ibits'],
                                        force_ideal=False)
    # store the statistics into the graph for later use
    node.stats = stats
    return node.stats
Esempio n. 3
0
def balance_filter(pnode, precision_threshold=None, small_value_threshold=0.0000000001, fnode=None, G=None):
    node = pnode if fnode is None else fnode
    channel_dim = node.filter.get_order_idx('out_c')

    stats = astats(node.weights, channel_dim=channel_dim, channel_details=True)
    if precision_threshold:
        if not any(stat['avg_prec'] < precision_threshold for stat in stats['channel_stats']):
            LOG.info("layer %s doesn't have any weights below precision threshold", node.name)
        return False

    LOG.info("balancing weights of layer %s", node.name)
    if node.has_mul_bias and node.mul_biases is not None:
        mul_bias = node.mul_biases
    else:
        mul_bias = np.ones(node.filter.out_c, dtype=np.float32)

    scale = np.array([max(abs(stat['max']), abs(stat['min']))
                      for stat in stats['channel_stats']])
    # don't balance channels that are effectively zero
    threshold_idx = scale < small_value_threshold
    scale[threshold_idx] = 1
    weights_shape = [1 if idx != channel_dim else size
                     for idx, size in enumerate(node.weights.shape)]
    node.weights /= scale.reshape(weights_shape)
    if node.has_bias:
        node.biases /= scale

    mul_bias *= scale
    if G:
        G.changes.modify(pnode, 'has_mul_bias', True, fnode=fnode)
    else:
        node.has_mul_bias = True
    node.mul_biases = mul_bias
    return True
Esempio n. 4
0
def gather_stats(activation,
                 force_ideal=False,
                 channel_dim=None,
                 channel_details=None):
    stat = astats(activation,
                  channel_dim=channel_dim,
                  channel_details=channel_details)
    stat['qstats'] = calculate_qsnrs(activation, stat['ibits'], force_ideal)
    return stat
Esempio n. 5
0
def filter_stats(pnode, fnode, anode, channel_details=None, qrec=None):
    stats = {}
    if isinstance(anode, MultiplicativeBiasParameters):
        if anode.has_mul_bias:
            stats['mul_biases'] = mul_biases = astats(anode.mul_biases)
            mul_biases['qstats'] = calculate_qsnrs(anode.mul_biases,
                                                   mul_biases['ibits'],
                                                   force_ideal=False)
        elif isinstance(qrec, MultScalableFilterQuantizationRecord):
            stats['mul_biases'] = mul_biases = astats(qrec.mul_biases_fps)
            mul_biases['qstats'] = calculate_qsnrs(qrec.mul_biases_fps,
                                                   mul_biases['ibits'],
                                                   force_ideal=False)
    if anode.has_bias:
        if qrec:
            qbiases = qrec.prepare_biases(anode,
                                          anode.biases,
                                          anode.weights,
                                          ktype="float32")
        else:
            qbiases = anode.biases

        stats['biases'] = biases = astats(qbiases)
        biases['qstats'] = calculate_qsnrs(qbiases,
                                           biases['ibits'],
                                           force_ideal=False)
    if qrec:
        qweights = qrec.prepare_weights(anode, anode.weights, ktype="float32")
    else:
        qweights = anode.weights

    stats['weights'] = weights = astats(
        qweights,
        channel_dim=anode.filter.get_order_idx('out_c'),
        channel_details=channel_details)
    weights['qstats'] = calculate_qsnrs(qweights,
                                        weights['ibits'],
                                        force_ideal=False)
    # store the statistics into the graph for later use
    anode.stats = stats
    stats['step_idx'] = pnode.step_idx
    return anode.stats
Esempio n. 6
0
def gather_stats(activation, force_ideal=False):
    stat = astats(activation)
    stat['qstats'] = calculate_qsnrs(activation, stat['ibits'], force_ideal)
    return stat