コード例 #1
0
def test_schedules(verbose=False, test_random=False):
    #random_module.seed(int(sys.argv[1]) if len(sys.argv)>1 else 0)
    halide.exit_on_signal()
    f = halide.Func('f')
    x = halide.Var('x')
    y = halide.Var('y')
    c = halide.Var('c')
    g = halide.Func('g')
    v = halide.Var('v')
    input = halide.UniformImage(halide.UInt(16), 3)
    int_t = halide.Int(32)
    f[x, y, c] = input[
        halide.clamp(x, halide.cast(int_t, 0
                                    ), halide.cast(int_t,
                                                   input.width() - 1)),
        halide.clamp(y, halide.cast(int_t, 0
                                    ), halide.cast(int_t,
                                                   input.height() - 1)),
        halide.clamp(c, halide.cast(int_t, 0), halide.cast(int_t, 2))]
    #g[v] = f[v,v]
    g[x, y, c] = f[x, y, c] + 1
    assert sorted(halide.all_vars(g).keys()) == sorted(['x', 'y',
                                                        'c'])  #, 'v'])

    if verbose:
        print halide.func_varlist(f)
        print 'caller_vars(f) =', caller_vars(g, f)
        print 'caller_vars(g) =', caller_vars(g, g)


#    validL = list(valid_schedules(g, f, 4))
#    validL = [repr(_x) for _x in validL]
#
#    for L in sorted(validL):
#        print repr(L)
    T0 = time.time()
    if not test_random:
        random = True  #False
        nvalid_determ = 0
        for L in schedules_func(g, f, 0, 3):
            nvalid_determ += 1
            if verbose:
                print L
        nvalid_random = 0
        for i in range(100):
            for L in schedules_func(
                    g, f, 0, DEFAULT_MAX_DEPTH, random=True
            ):  #sorted([repr(_x) for _x in valid_schedules(g, f, 3)]):
                if verbose and 0:
                    print L  #repr(L)
                nvalid_random += 1
    s = []
    for i in range(400):
        d = random_schedule(g, 0, DEFAULT_MAX_DEPTH)
        si = str(d)
        s.append(si)
        if verbose:
            print 'Schedule:', si

        d.apply()
        evaluate = d.test((36, 36, 3), input)
        print 'evaluate'
        evaluate()
        if test_random:
            print 'Success'
            sys.exit()
    T1 = time.time()

    s = '\n'.join(s)
    assert 'f.chunk(_c0)' in s
    assert 'f.root().vectorize' in s
    assert 'f.root().unroll' in s
    assert 'f.root().split' in s
    assert 'f.root().tile' in s
    assert 'f.root().parallel' in s
    assert 'f.root().transpose' in s

    assert nvalid_random == 100
    if verbose:
        print 'generated in %.3f secs' % (T1 - T0)
    print 'random_schedule: OK'
コード例 #2
0
def runCommand(cmd, cfg, hl_cfg, limit, test, func_d, func, scope, cache={}):
    print 'runcommand ---------------------------------------------------------'
#    sys.exit(0)
    
    numtests = NUMTESTS
    
    schedule_str = hl_cfg.str(cfg)
    if schedule_str in cache:
        return cache[schedule_str]
    #print 'run |%s|, %s' % (cmd, ' '.join(str(x) for x in cfg.values.items()))
    if verbose:
        print '----------------------------------------------------------------'
        print 'run', cmd
        print schedule_str
    #timefile = '_time_out.txt'
    #schedulefile = '_schedule.h'
    
    #if os.path.exists(timefile):
    #    os.remove(timefile)
    #f = open(schedulefile, 'wt')
    #f.write(schedule_str)
    #f.close()
    #return pbutil.executeRun(['python', 'halide_schedule.py', '~schedule.txt']+cmd, config.metrics)
    #status = system_timelimit(('python halide_schedule.py %s %s %s %d' % (schedulefile, cmd, timefile, 1)), limit if limit is not None else 10*60.0)
    limit_t = limit if limit is not None else 10*60.0

    for value in func_d.values():
        value.reset()
    for line in schedule_str.split('\n'):
        #(funcname, rest) = line.split('.',1)
        #line = 'func_d["%s"].' % funcname + rest
        print 'exec |%s|' % line
        exec line in scope

    #"""
    halide.exit_on_signal()
    
    try:
        with Watchdog(limit_t):
            func.compileJIT()
    except Watchdog:
        raise TimingRunTimeout
    #"""
    """
    try:
        func.compileJIT()
    except:
        traceback.print_exc()
        raise TimingRunFailed
    """
    #if status == TIMEOUT:
    #    raise TimingRunTimeout
    Lans = []
    for i in range(numtests):
        #status = system_timelimit(('python halide_schedule.py %s %s %s %d' % (schedulefile, cmd, timefile, 0)), limit if limit is not None else 10*60.0)
        #if status == TIMEOUT:
        #    raise TimingRunTimeout
        #"""
        try:
            with Watchdog(limit_t):
                ans = test(func)
        except Watchdog:
            raise TimingRunTimeout
        #"""
        #ans = test(func)
        assert isinstance(ans, (float, int))
        #try:
        #    f = open(timefile, 'rt')
        #except:
        #    raise TimingRunFailed
        #s = f.read()
        #print s
        #if 'HL_NUMTHREADS not defined.' in s:
        #    raise ValueError('HL_NUMTHREADS not defined')

        #L = s.strip().split('\n')
        #f.close()
        
        #try:
        #    #assert len(L) >= 1
        #    ans = float(L[-1])
        #except:
        #    raise TimingRunFailed
        Lans.append(ans)
    
    if verbose:
        print '   => %f secs, sigma=%f' % (numpy.mean(Lans), numpy.std(Lans))
        print '----------------------------------------------------------------'
        print 
    cache[schedule_str] = Lans
    return Lans
コード例 #3
0
ファイル: autotune.py プロジェクト: nzinfo/Halide
def test_schedules(verbose=False, test_random=False):
    #random_module.seed(int(sys.argv[1]) if len(sys.argv)>1 else 0)
    halide.exit_on_signal()
    f = halide.Func('f')
    x = halide.Var('x')
    y = halide.Var('y')
    c = halide.Var('c')
    g = halide.Func('g')
    v = halide.Var('v')
    input = halide.UniformImage(halide.UInt(16), 3)
    int_t = halide.Int(32)
    f[x,y,c] = input[halide.clamp(x,halide.cast(int_t,0),halide.cast(int_t,input.width()-1)),
                     halide.clamp(y,halide.cast(int_t,0),halide.cast(int_t,input.height()-1)),
                     halide.clamp(c,halide.cast(int_t,0),halide.cast(int_t,2))]
    #g[v] = f[v,v]
    g[x,y,c] = f[x,y,c]+1
    assert sorted(halide.all_vars(g).keys()) == sorted(['x', 'y', 'c']) #, 'v'])

    if verbose:
        print halide.func_varlist(f)
        print 'caller_vars(f) =', caller_vars(g, f)
        print 'caller_vars(g) =', caller_vars(g, g)
    
#    validL = list(valid_schedules(g, f, 4))
#    validL = [repr(_x) for _x in validL]
#    
#    for L in sorted(validL):
#        print repr(L)
    T0 = time.time()
    if not test_random:
        random = True #False
        nvalid_determ = 0
        for L in schedules_func(g, f, 0, 3):
            nvalid_determ += 1
            if verbose:
                print L
        nvalid_random = 0
        for i in range(100):
            for L in schedules_func(g, f, 0, DEFAULT_MAX_DEPTH, random=True): #sorted([repr(_x) for _x in valid_schedules(g, f, 3)]):
                if verbose and 0:
                    print L#repr(L)
                nvalid_random += 1
    s = []
    for i in range(400):
        d = random_schedule(g, 0, DEFAULT_MAX_DEPTH)
        si = str(d)
        s.append(si)
        if verbose:
            print 'Schedule:', si

        d.apply()
        evaluate = d.test((36, 36, 3), input)
        print 'evaluate'
        evaluate()
        if test_random:
            print 'Success'
            sys.exit()
    T1 = time.time()
    
    s = '\n'.join(s)
    assert 'f.chunk(_c0)' in s
    assert 'f.root().vectorize' in s
    assert 'f.root().unroll' in s
    assert 'f.root().split' in s
    assert 'f.root().tile' in s
    assert 'f.root().parallel' in s
    assert 'f.root().transpose' in s

    assert nvalid_random == 100
    if verbose:
        print 'generated in %.3f secs' % (T1-T0)
    print 'random_schedule: OK'
コード例 #4
0
def runCommand(cmd, cfg, hl_cfg, limit, test, func_d, func, scope, cache={}):
    print 'runcommand ---------------------------------------------------------'
#    sys.exit(0)
    
    numtests = NUMTESTS
    
    schedule_str = hl_cfg.str(cfg)
    if schedule_str in cache:
        return cache[schedule_str]
    #print 'run |%s|, %s' % (cmd, ' '.join(str(x) for x in cfg.values.items()))
    if verbose:
        print '----------------------------------------------------------------'
        print 'run', cmd
        print schedule_str
    #timefile = '_time_out.txt'
    #schedulefile = '_schedule.h'
    
    #if os.path.exists(timefile):
    #    os.remove(timefile)
    #f = open(schedulefile, 'wt')
    #f.write(schedule_str)
    #f.close()
    #return pbutil.executeRun(['python', 'halide_schedule.py', '~schedule.txt']+cmd, config.metrics)
    #status = system_timelimit(('python halide_schedule.py %s %s %s %d' % (schedulefile, cmd, timefile, 1)), limit if limit is not None else 10*60.0)
    limit_t = limit if limit is not None else 10*60.0

    for value in func_d.values():
        value.reset()
    for line in schedule_str.split('\n'):
        #(funcname, rest) = line.split('.',1)
        #line = 'func_d["%s"].' % funcname + rest
        print 'exec |%s|' % line
        exec line in scope

    #"""
    halide.exit_on_signal()
    
    try:
        with Watchdog(limit_t):
            func.compileJIT()
    except Watchdog:
        raise TimingRunTimeout
    #"""
    """
    try:
        func.compileJIT()
    except:
        traceback.print_exc()
        raise TimingRunFailed
    """
    #if status == TIMEOUT:
    #    raise TimingRunTimeout
    Lans = []
    for i in range(numtests):
        #status = system_timelimit(('python halide_schedule.py %s %s %s %d' % (schedulefile, cmd, timefile, 0)), limit if limit is not None else 10*60.0)
        #if status == TIMEOUT:
        #    raise TimingRunTimeout
        #"""
        try:
            with Watchdog(limit_t):
                ans = test(func)
        except Watchdog:
            raise TimingRunTimeout
        #"""
        #ans = test(func)
        assert isinstance(ans, (float, int))
        #try:
        #    f = open(timefile, 'rt')
        #except:
        #    raise TimingRunFailed
        #s = f.read()
        #print s
        #if 'HL_NUMTHREADS not defined.' in s:
        #    raise ValueError('HL_NUMTHREADS not defined')

        #L = s.strip().split('\n')
        #f.close()
        
        #try:
        #    #assert len(L) >= 1
        #    ans = float(L[-1])
        #except:
        #    raise TimingRunFailed
        Lans.append(ans)
    
    if verbose:
        print '   => %f secs, sigma=%f' % (numpy.mean(Lans), numpy.std(Lans))
        print '----------------------------------------------------------------'
        print 
    cache[schedule_str] = Lans
    return Lans