Ejemplo n.º 1
0
def compare_CaP_OaA():
    printf(
        "  hybd1 complexity | CaP complexity | folding | spatial complexity",
        type=None)
    sum_complexity1 = 0
    sum_complexity2 = 0
    sum_baseline = 0
    for layer in layers:
        complexity_baseline = op.op_count_spatial(*layer)
        complexity1 = op.op_count_fft(*layer, folding_1D=1)
        layer[4] = 16
        complexity2, folding_opt = complexity_CaP(layer)
        sum_complexity1 += complexity1
        sum_complexity2 += complexity2
        sum_baseline += complexity_baseline
        printf("{} | {} | {} | {}",
               complexity1 / 1e9,
               complexity2 / 1e9,
               folding_opt,
               complexity_baseline / 1e9,
               type=None)
    printf("sum: hybd1 vs. CaP vs. spatial")
    printf("{} ({}) {} ({}) {}", sum_complexity1 / 1e9,
           sum_complexity1 / sum_baseline, sum_complexity2 / 1e9,
           sum_complexity2 / sum_baseline, sum_baseline)
Ejemplo n.º 2
0
def core_fft_size_folding(f_in,
                          f_out,
                          l_img,
                          l_kern,
                          range_N=None,
                          range_folding=None,
                          name=''):
    """
	design space explore (brute force) on a given CNN layer.
	this function gives the optimal cnfiguration of FFT size and folding factor for each layer.
	"""
    N_min_power = 1
    N_max_power = 8
    folding_min = 1
    folding_max = 20
    range_N = ((range_N is not None) and [np.array(range_N)]
               or [4**np.arange(N_min_power, N_max_power + 1)])[0]
    range_folding = ((range_folding is not None) and [np.array(range_folding)]
                     or [np.arange(folding_min, folding_max + 1)])[0]
    range_N, range_folding = np.meshgrid(range_N, range_folding)
    range_ops = op.op_count_fft(f_in,
                                f_out,
                                l_img,
                                l_kern,
                                range_N,
                                None,
                                None,
                                folding_1D=range_folding) / 1e6

    range_ops[range_ops == 0.] = float('Inf')
    min_args = np.unravel_index(range_ops.argmin(), range_ops.shape)
    return range_ops.min(), range_N[min_args], range_folding[min_args]
Ejemplo n.º 3
0
def plot_fft_size_folding(f_in,
                          f_out,
                          l_img,
                          l_kern,
                          range_N=None,
                          range_folding=None,
                          name=''):
    """
	3D plotter
	"""
    N_min_power = 1
    N_max_power = 4
    folding_min = 1
    folding_max = 10
    title = '{}_N_{}_{}_fd_{}_{}'.format(name, N_min_power, N_max_power,
                                         folding_min, folding_max)
    range_N = range_N and range_N or 4**np.arange(N_min_power, N_max_power + 1)
    range_folding = range_folding and range_folding or np.arange(
        folding_min, folding_max + 1)
    dots = np.zeros((3, len(range_N), len(range_folding)))
    X = range_N
    Y = range_folding
    X, Y = np.meshgrid(X, Y)
    Z = op.op_count_fft(
        f_in, f_out, l_img, l_kern, X, None, None, folding_1D=Y) / 1e6

    #import pdb; pdb.set_trace()
    # the plot won't contain invalid data unless N is too small (smaller than l_kern)
    fig = plt.figure()
    ax = fig.gca(projection='3d')

    surf = ax.plot_surface(X,
                           Y,
                           Z,
                           cmap=cm.coolwarm,
                           rstride=1,
                           cstride=1,
                           linewidth=0,
                           antialiased=False)
    ax.set_zlim(Z.min(), 0.5 * Z.max())
    #ax.zaxis.set_major_locator(LinearLocator(10))
    #ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
    fig.colorbar(surf, shrink=0.5, aspect=5)

    ax.set_xlabel('FFT size')
    ax.set_ylabel('folding')
    ax.set_zlabel('# ops')
    #plt.show()
    plt.savefig('plots/{}.png'.format(title))
    Z[Z == 0.] = float('Inf')
    min_args = np.unravel_index(Z.argmin(), Z.shape)
    return X[min_args], Y[min_args]
Ejemplo n.º 4
0
def complexity_CaP(layer):
    K = (layer[3] + layer[2] - 1) / (layer[4] - layer[3] + 1)
    folding_opt = (layer[4] - layer[3] + 1) / math.gcd(layer[2] + layer[3] - 1,
                                                       layer[4] - layer[3] + 1)
    complexity = op.op_count_fft(*layer, folding_1D=folding_opt)
    return complexity, folding_opt
Ejemplo n.º 5
0
def plot_fixed_len_FFT():
    bars = {16: [], 32: [], 64: [], 128: []}
    for FFT_fixed in list(bars.keys()):
        for i, layer in enumerate(layers):
            layer[4] = FFT_fixed
            _op_oaa = op.op_count_fft(*layer) / 1e9
            bars[FFT_fixed] += [_op_oaa]

    lines = {"spatial": [], "var_fft": [], "native_fft": []}
    for i, layer in enumerate(layers):
        _op_spatial = op.op_count_spatial(*layer) / 1e9
        lines["spatial"] += [_op_spatial]
    layers[0][4] = 32  # 32: OaA
    layers[1][4] = 16  # 16: OaA
    layers[2][4] = 32  # 32: Native
    layers[3][4] = 16  # 16: Native
    layers[4][4] = 16  # 16: Native
    lines["var_fft"] += [op.op_count_fft(*layers[0]) / 1e9]
    lines["var_fft"] += [op.op_count_fft(*layers[1]) / 1e9]
    lines["var_fft"] += [op.op_count_fft(*layers[2]) / 1e9]
    lines["var_fft"] += [op.op_count_fft(*layers[3]) / 1e9]
    lines["var_fft"] += [op.op_count_fft(*layers[4]) / 1e9]
    #import pdb;pdb.set_trace()

    conv_layers = np.arange(len(layers)) + 1
    fig1 = plt.figure(1)
    ax = plt.subplot(111)
    ax.set_aspect(0.6)
    #ax.set_title("Effect of variable length FFT", fontsize=20)
    ax.set_xlabel("Convolution layers", fontsize=16)
    ax.set_ylabel("Giga Operations", fontsize=16)
    ax.set_ylim([0, 4.8])
    ax.set_xlim([0.5, 5.5])

    box = ax.get_position()
    ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])

    ax.xaxis.set_major_locator(MaxNLocator(integer=True))
    line_spatial, = ax.plot(conv_layers,
                            lines["spatial"],
                            '-o',
                            label='Spatial')
    line_var_fft, = ax.plot(conv_layers,
                            lines["var_fft"],
                            '--^',
                            label='FFT-hybd',
                            color='G',
                            markersize=10,
                            linewidth=2)

    cmap = plt.get_cmap("autumn")  #.cm.gist_ncar
    colors = [cmap(i) for i in np.linspace(0, 1, len(layers))]
    bar_width = 0.1
    for i, FFT_fixed in enumerate(sorted(list(bars.keys()))):
        ax.bar(conv_layers + bar_width * (i - 2),
               bars[FFT_fixed],
               bar_width,
               color=colors[i],
               label='OaA-{}'.format(FFT_fixed),
               edgecolor="none")

    ax.legend(loc='center left',
              bbox_to_anchor=(1, 0.5),
              fancybox=True,
              shadow=True,
              ncol=1)
    #plt.show()
    plt.savefig("plots/algo_I.pdf", bbox_inches='tight')

    _op_spatial_tot = sum(lines['spatial'])
    for K in list(bars.keys()):
        printf("[FFT-{}]: {}, {}x", K, sum(bars[K]),
               _op_spatial_tot / sum(bars[K]))
    printf("[FFT-hybd]: {}, {}x", sum(lines["var_fft"]),
           _op_spatial_tot / sum(lines["var_fft"]))