<commit_before>import pylab as pl
import matplotlib as mpl
import itertools
import sys
import pickle
import timeit
from em import *

class EMAutotuner(object):

    def __init__(self, variant_param_space, input_param_space):
        fromfile = np.recfromcsv('IS1000a.csv', names=None, dtype=np.float32)
        self.orig_X = fromfile
        self.input_list = [] #tuple (M, X)
        self.shaped_Xs = {}
        self.variant_param_space = variant_param_space
        self.variant_param_space_size = sum([len(v) for v in self.variant_param_space.values()])
        self.input_param_space = input_param_space
        self.generate_shaped_inputs(input_param_space.keys(), input_param_space.values(), {})

    def add_to_input_list(self, param_dict):
        D = int(param_dict['D'])
        N = int(param_dict['N'])
        M = int(param_dict['M'])
        new_X = self.shaped_Xs.setdefault((D,N), np.resize(self.orig_X,(N,D)))
        self.input_list.append((M, D, N, new_X))

    def generate_shaped_inputs(self, key_arr, val_arr_arr, current):
        idx = len(current)
        name = key_arr[idx]
        for v in val_arr_arr[idx]:
            current[name]  = v
            if idx == len(key_arr)-1:
                self.add_to_input_list(current)
            else:
                self.generate_shaped_inputs(key_arr, val_arr_arr, current)
        del current[name]

    def test_point(self, input_tuple):
        self.gmm = GMM(input_tuple[0], input_tuple[1], self.variant_param_space)
        likelihood = self.gmm.train(input_tuple[3])

    def search_space(self):
        for i in self.input_list:
            print "M=%d, D=%d, N=%d" % i[:3]
            for j in range(0, self.variant_param_space_size):
                self.test_point(i)
            self.gmm.asp_mod.save_func_variant_timings("train")    

if __name__ == '__main__':
    variant_param_space = {
            'num_blocks_estep': ['16'],
            'num_threads_estep': ['512'],
            'num_threads_mstep': ['256','512'],
            'num_event_blocks': ['32','128'],
            'max_num_dimensions': ['50'],
            'max_num_clusters': ['128'],
            'diag_only': ['0'],
            'max_iters': ['10'],
            'min_iters': ['10'],
            'covar_version_name': ['V1', 'V2A', 'V2B', 'V3']
    }
    input_param_space =  {
            'D': np.arange(2, 40, 6),
            'N': np.arange(10000, 100001, 20000),
            'M': np.arange(1, 101, 20)
    }
    emt = EMAutotuner(variant_param_space, input_param_space)
    emt.search_space()
 <commit_msg>Added better parameters to autotuner<commit_after>import pylab as pl
import matplotlib as mpl
import itertools
import sys
import pickle
import timeit
from em import *

class EMAutotuner(object):

    def __init__(self, variant_param_space, input_param_space, ifile_name, func_name, device_id, max_iters_per_point):
        fromfile = np.recfromcsv('IS1000a.csv', names=None, dtype=np.float32)
        self.orig_X = fromfile
        self.ifile_name = ifile_name
        self.func_name = func_name
        self.device_id = device_id
        self.input_list = [] #tuple (M, X)
        self.shaped_Xs = {}
        self.variant_param_space = variant_param_space
        self.variant_param_space_size = max_iters_per_point or reduce(lambda x, y: x*y, [len(v) for v in self.variant_param_space.values()])
        self.input_param_space = input_param_space
        self.generate_shaped_inputs(input_param_space.keys(), input_param_space.values(), {})
        self.gmm = GMM(1, 1, self.variant_param_space, self.device_id)
        mod = self.gmm.get_asp_mod()

    def add_to_input_list(self, param_dict):
        D = int(param_dict['D'])
        N = int(param_dict['N'])
        M = int(param_dict['M'])
        new_X = self.shaped_Xs.setdefault((D,N), np.resize(self.orig_X,(N,D)))
        self.input_list.append((M, D, N, new_X))

    def generate_shaped_inputs(self, key_arr, val_arr_arr, current):
        idx = len(current)
        name = key_arr[idx]
        for v in val_arr_arr[idx]:
            current[name]  = v
            if idx == len(key_arr)-1:
                self.add_to_input_list(current)
            else:
                self.generate_shaped_inputs(key_arr, val_arr_arr, current)
        del current[name]

    def test_point(self, input_tuple):
        self.gmm = GMM(input_tuple[0], input_tuple[1])
        likelihood = getattr(self.gmm, self.func_name)(input_tuple[3])

    def search_space(self):
        if self.ifile_name:
            self.gmm.asp_mod.restore_func_variant_timings(self.func_name, self.ifile_name)
        for i in self.input_list:
            print "M=%d, D=%d, N=%d" % i[:3]
            for j in range(0, self.variant_param_space_size):
                self.test_point(i)
            self.gmm.asp_mod.save_func_variant_timings(self.func_name)    

if __name__ == '__main__':
    ifile_name = None #"data/285.210x16.train.vardump"
    func_name = "train"
    device_id = 0
    max_iters_per_point = None #1
    variant_param_space = {
            'num_blocks_estep': ['16'],
            'num_threads_estep': ['512'],
            'num_threads_mstep': ['256','512'],
            'num_event_blocks': ['32','128'],
            'max_num_dimensions': ['50'],
            'max_num_clusters': ['128'],
            'diag_only': ['0'],
            'max_iters': ['10'],
            'min_iters': ['10'],
            'covar_version_name': ['V1', 'V2A', 'V2B', 'V3']
    }
    input_param_space =  {
            'D': np.arange(2, 40, 6),
            'N': np.arange(10000, 100001, 20000),
            'M': np.arange(1, 101, 20)
    }
    emt = EMAutotuner(variant_param_space, input_param_space, ifile_name, func_name, device_id, max_iters_per_point)
    emt.search_space()
 
{'fraction_non_alphanumeric': 0.1303894297635605, 'fraction_numerical': 0.02573018080667594, 'mean_word_length': 2.6159648020113138, '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': '3908663', 'n_tokens_mistral': 2096, 'n_tokens_neox': 1996, 'n_words': 365}