<commit_before>import time
import cudarray as ca


def profile(neuralnet, X, Y, reps=100):
    neuralnet._setup(X, Y)
    layer_input = ca.array(X)
    for layer_idx, layer in enumerate(neuralnet.layers[:-1]):
        start_time = time.time()
        for _ in range(reps):
            layer.fprop(layer_input, 'train')
        end_time = time.time()
        fprop_duration = float(end_time - start_time)/reps

        layer_output = layer.fprop(layer_input, 'train')
        start_time = time.time()
        for _ in range(reps):
            layer.bprop(layer_output)
        end_time = time.time()
        bprop_duration = float(end_time - start_time)/reps

        print('%s:   \tfprop: %.6f s \t bprop: %.6f s'
              % (layer.name, fprop_duration, bprop_duration))
        layer_input = layer_output<commit_msg>misc.profile(): cleanup and added total duration<commit_after>import time
import cudarray as ca


def avg_running_time(fun, reps):
    # Memory allocation forces GPU synchronization
    ca.empty(1)
    start_time = time.time()
    for _ in range(reps):
        fun()
    ca.empty(1)
    duration = time.time() - start_time
    return float(time.time() - start_time) / reps


def profile(neuralnet, X, Y, reps=50):
    neuralnet._setup(X, Y)
    X = ca.array(X)
    Y = ca.array(Y)
    layer_input = X
    total_duration = 0
    for layer_idx, layer in enumerate(neuralnet.layers[:-1]):
        def fprop():
            layer.fprop(layer_input, 'train')
        fprop_duration = avg_running_time(fprop, reps)
        layer_output = layer.fprop(layer_input, 'train')

        def bprop():
            layer.bprop(layer_output)
        bprop_duration = avg_running_time(bprop, reps)
        print('%s:   \tfprop(): %.6f s \t bprop(): %.6f s'
              % (layer.name, fprop_duration, bprop_duration))
        layer_input = layer_output
        total_duration += fprop_duration + bprop_duration
    print('total_duration: %.6f s' % total_duration)

    def nn_bprop():
        neuralnet._bprop(X, Y)
    nn_bprop_duration = avg_running_time(nn_bprop, reps)
    print('neuralnet._bprop(): %.6f s' % nn_bprop_duration)
{'fraction_non_alphanumeric': 0.11839026672905943, 'fraction_numerical': 0.007487131492746842, 'mean_word_length': 2.698961937716263, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5287300', 'n_tokens_mistral': 739, 'n_tokens_neox': 717, 'n_words': 159}