<commit_before>
import halide
import autotune
import random

def get_bounds(root_func, scope):
    """
    Returns map of func name => list of bounds for each dim, with -1 indicating unbounded. Does not do bound inference.
    """
    bounds = {}
    varlist = {}
    for (name, f) in halide.all_funcs(root_func).items():
        varlist[name] = halide.func_varlist(f)
        bounds[name] = [-1] * len(varlist[name])
    if 'tune_constraints' in scope:
        constraints = autotune.Schedule.fromstring(root_func, scope['tune_constraints'])
    else:
        return bounds
    for (name, L) in constraints.d.items():
        for x in L:
            if isinstance(x, autotune.FragmentBound):
                try:
                    i = varlist[name].index(x.var)
                except IndexError:
                    raise ValueError('could not find var %s to bound in func %s (varlist is %r)' % (x.var, name, varlist[name]))
                bounds[name][i] = x.size
    
    return bounds

def get_xy(f, bounds):
    """
    Given func object and bounds returned by get_bounds(), return vars suitable for interpretation as (x, y).
    
    If this is not possible then just returns the var list for func.
    """
    varlist = halide.func_varlist(f)
    bounds = bounds[f.name()]
    assert len(varlist) == len(bounds), (len(varlist), len(bounds))
    allowed = [True]*len(varlist)
    for i in range(len(bounds)):
        if bounds[i] < 10 and bounds[i] >= 0:
            allowed[i] = False

    ans = []
    for i in range(len(bounds)-1):
        if allowed[i] and allowed[i+1]:
            ans.append((varlist[i], varlist[i+1]))
    if len(ans) > 0:
        return random.choice(ans)
    raise varlist
    
def test():
#    from examples.interpolate import filter_func
    from examples.camera_pipe import filter_func
    (input, output, evaluate, scope) = filter_func()
    bounds = get_bounds(output, scope)
    print bounds
    print get_xy(output, bounds)
#    print get_xy(halide.all_funcs(output)['ux6'], bounds)

if __name__ == '__main__':
    test()
<commit_msg>py bindings -- fix bound issue<commit_after>
import halide
import autotune
import random

def get_bounds(root_func, scope):
    """
    Returns map of func name => list of bounds for each dim, with -1 indicating unbounded. Does not do bound inference.
    """
    bounds = {}
    varlist = {}
    for (name, f) in halide.all_funcs(root_func).items():
        varlist[name] = halide.func_varlist(f)
        bounds[name] = [-1] * len(varlist[name])
    if 'tune_constraints' in scope:
        constraints = autotune.Schedule.fromstring(root_func, scope['tune_constraints'])
    else:
        return bounds
    for (name, L) in constraints.d.items():
        for x in L:
            if isinstance(x, autotune.FragmentBound):
                try:
                    i = varlist[name].index(x.var)
                except IndexError:
                    raise ValueError('could not find var %s to bound in func %s (varlist is %r)' % (x.var, name, varlist[name]))
                bounds[name][i] = x.size
    
    return bounds

def get_xy(f, bounds):
    """
    Given func object and bounds returned by get_bounds(), return vars suitable for interpretation as (x, y).
    
    If this is not possible then just returns the var list for func.
    """
    varlist = halide.func_varlist(f)
    bounds = bounds[f.name()]
    assert len(varlist) == len(bounds), (len(varlist), len(bounds))
    allowed = [True]*len(varlist)
    for i in range(len(bounds)):
        if bounds[i] < 10 and bounds[i] >= 0:
            allowed[i] = False

    ans = []
    for i in range(len(bounds)-1):
        if allowed[i] and allowed[i+1]:
            ans.append((varlist[i], varlist[i+1]))
    if len(ans) > 0:
        return random.choice(ans)
    return varlist
    
def test():
#    from examples.interpolate import filter_func
    from examples.camera_pipe import filter_func
    (input, output, evaluate, scope) = filter_func()
    bounds = get_bounds(output, scope)
    print bounds
    print get_xy(output, bounds)
#    print get_xy(halide.all_funcs(output)['ux6'], bounds)

if __name__ == '__main__':
    test()

{'fraction_non_alphanumeric': 0.11936720997123682, 'fraction_numerical': 0.004793863854266539, 'mean_word_length': 2.578902229845626, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '3078481', 'n_tokens_mistral': 1317, 'n_tokens_neox': 1295, 'n_words': 390}