Exemple #1
0
def random_test():
    def top_func(dtype = hcl.Int()):
        
        def random(number):
            number[0] = 78
            
        number = hcl.placeholder((64,), "number")
    
        s = hcl.create_schedule([number], random)
        return hcl.build(s)
    
    
    
    np_number = np.random.randint(2, size = (64,))
    np_count = hcl.cast_np(np.zeros((1,)), dtype = hcl.Int())
    
    hcl_count = hcl.asarray(np_count)
    #hcl_count = 0
    hcl_number = hcl.asarray(np_number)
    dtype = hcl.Int()
    f = top_func(dtype)
    f(hcl_number)
    
    number = hcl_number.asnumpy()
    print(number)
    print(hcl_count)
Exemple #2
0
def popcount_test():
    def top_func():
        
        def popcount64(number):
            count= hcl.scalar(0, "count")
            with hcl.for_(0, 64) as i:
                count.v += number[i]
            hcl.return_(count.v)
            
        def uh(number):
            x = hcl.compute((1,), lambda x: popcount64(number), "uh")
            return x
        
        number = hcl.placeholder((64,), "number")
    
        s = hcl.create_schedule([number], uh)
        return hcl.build(s)
    
    
    
    np_number = np.random.randint(2, size = (DIGIT_WIDTH,))
    np_count = hcl.cast_np(np.zeros((1,)), dtype = hcl.Int())
    
    hcl_count = hcl.asarray(np_count)
    #hcl_count = 0
    hcl_number = hcl.asarray(np_number)
    f = top_func()
    f(hcl_number, hcl_count)
    
    number = hcl_number.asnumpy()
    print(number)
    print(hcl_count)
Exemple #3
0
def test_zeros():
    Tdtype = hcl.Int()
    Rdtype = hcl.Int()

    np_training = hcl.cast_np(
        np.full((NUM_TRAINING * DIGIT_WIDTH, ), 1, dtype=int), Tdtype)
    np_test_set = hcl.cast_np(
        np.full((NUM_TEST * DIGIT_WIDTH, ), 2, dtype=int), Tdtype)
    np_results = hcl.cast_np(np.full((NUM_TEST, ), 7, dtype=int), Tdtype)

    hcl_training = hcl.asarray(np_training, dtype=Tdtype)
    hcl_test_set = hcl.asarray(np_test_set, dtype=Tdtype)
    hcl_results = hcl.asarray(np_results, dtype=Tdtype)

    dtype = hcl.Int()
    f = top_digit_rec(dtype)

    f(hcl_training, hcl_test_set, hcl_results)
Exemple #4
0
    def _test_dtype(dtype):
        hcl.init(dtype)
        A = hcl.placeholder((100, ))
        B = hcl.placeholder((100, ))

        def kernel(A, B):
            C = hcl.compute(A.shape, lambda x: A[x] + B[x])
            D = hcl.compute(A.shape, lambda x: A[x] - B[x])
            E = hcl.compute(A.shape, lambda x: A[x] * B[x])
            # division is not recommended
            #F = hcl.compute(A.shape, lambda x: A[x] / B[x])
            #return C, D, E, F
            return C, D, E

        s = hcl.create_schedule([A, B], kernel)
        f = hcl.build(s)

        np_A = np.random.rand(*A.shape) + 0.1
        np_B = np.random.rand(*B.shape) + 0.1
        hcl_A = hcl.asarray(np_A)
        hcl_B = hcl.asarray(np_B)
        hcl_C = hcl.asarray(np.zeros(A.shape))
        hcl_D = hcl.asarray(np.zeros(A.shape))
        hcl_E = hcl.asarray(np.zeros(A.shape))
        #hcl_F = hcl.asarray(np.zeros(A.shape))
        #f(hcl_A, hcl_B, hcl_C, hcl_D, hcl_E, hcl_F)
        f(hcl_A, hcl_B, hcl_C, hcl_D, hcl_E)

        np_C = hcl.cast_np(hcl_A.asnumpy() + hcl_B.asnumpy(), dtype)
        np_D = hcl.cast_np(hcl_A.asnumpy() - hcl_B.asnumpy(), dtype)
        np_E = hcl.cast_np(hcl_A.asnumpy() * hcl_B.asnumpy(), dtype)
        #np_F = hcl.cast_np(hcl_A.asnumpy() / hcl_B.asnumpy(), dtype)

        assert np.allclose(np_C, hcl_C.asnumpy())
        assert np.allclose(np_D, hcl_D.asnumpy())
        assert np.allclose(np_E, hcl_E.asnumpy())
Exemple #5
0
def knn_vote_top():
    def test_knn_vote():
    
        def knn_vote(labels, max_label):
            max_vote = hcl.scalar(0)
            #max_label = hcl.compute((1,), lambda x: 0, "max_label")
              
            votes = hcl.compute((10,), lambda x: 0, "votes")
            with hcl.for_(0, K_CONST) as i:
                votes[labels[i]] += 1
    
            with hcl.for_(0, 10) as i:
                with hcl.if_(votes[i] > max_vote.v):
                    max_vote.v = votes[i]
                    max_label[0] = i
                    
            #return max_label
                
        labels = hcl.placeholder((K_CONST,), "labels")
        max_label = hcl.placeholder((1,), "max_label")
        s = hcl.create_schedule([labels, max_label], knn_vote)
        return hcl.build(s)
        
    np_labels = np.array([2,1,2])
    np_votes = hcl.cast_np(np.zeros((10,), dtype = int), hcl.Int()) 
    np_max_label = np.array([0])
    
    hcl_labels = hcl.asarray(np_labels)
    hcl_votes = hcl.asarray(np_votes)
    hcl_max_label = hcl.asarray(np_max_label)
    hcl_label_out = 0
    
    g = test_knn_vote()
    g(hcl_labels, hcl_max_label)
    
    labels = hcl_labels.asnumpy()
    votes = hcl_votes.asnumpy()
    max_label = hcl_max_label.asnumpy()
    
    print(labels)
    print(max_label)
    print(votes)
    print(hcl_label_out)
Exemple #6
0
    def test_kernel(dtype, size):
        hcl.init(dtype)

        np_A = numpy.random.rand(*size)
        py_A = np_A.tolist()

        def kernel():
            cp1 = hcl.const_tensor(np_A)
            cp2 = hcl.const_tensor(py_A)
            return hcl.compute(np_A.shape,
                               lambda *x: cp1[x] + cp2[x],
                               dtype=hcl.Float())

        O = hcl.placeholder(np_A.shape)
        s = hcl.create_schedule([], kernel)
        f = hcl.build(s)

        np_O = numpy.zeros(np_A.shape)
        hcl_O = hcl.asarray(np_O, dtype=hcl.Float())

        f(hcl_O)

        np_A = hcl.cast_np(np_A, dtype)
        assert numpy.allclose(hcl_O.asnumpy(), np_A * 2, 1, 1e-5)
Exemple #7
0
update = SgdLR.M.update

d1, d2 = s[dot].split(dot.axis[1], factor=PAR_FACTOR)
g1, g2 = s[gradient].split(gradient.axis[0], factor=PAR_FACTOR)
u1, u2 = s[update].split(update.axis[0], factor=PAR_FACTOR)
s[dot].unroll(d2)
s[gradient].unroll(g2)
s[update].unroll(u2)

#print hcl.lower(s, [data, label, theta, lut])

f = hcl.build(s, f.inputs)

print "Reading data and preprocessing data ..."

np_data = hcl.cast_np(np.loadtxt("data/shuffledfeats.dat"), DTYPE)
np_label = hcl.cast_np(np.loadtxt("data/shuffledlabels.dat"), LTYPE)
np_theta = hcl.cast_np(np.zeros(NUM_FEATURES), FTYPE)
np_lut = hcl.cast_np(np.array(lut_), FTYPE)

print "Finishing prerpocessing data"

np_train_data = np_data[:NUM_FEATURES * NUM_TRAINING]
np_train_label = np_label[:NUM_TRAINING]
np_test_data = np_data[NUM_FEATURES * NUM_TRAINING:]
np_test_label = np_label[NUM_TRAINING:]

np_vdata = hcl.pack_np(np_train_data, DTYPE, MTYPE)
np_vlabel = hcl.pack_np(np_train_label, LTYPE, MTYPE)
np_vtheta = hcl.pack_np(np_theta, FTYPE, MTYPE)
Exemple #8
0
def test():

    Ddtype = hcl.Float()  #Fixed(40,20)

    np_lut = hcl.cast_np(lut, dtype=Ddtype)
    np_data = hcl.cast_np(np.loadtxt("shuffledfeats.dat"), dtype=Ddtype)
    np_label = hcl.cast_np(np.loadtxt("shuffledlabels.dat"), dtype=Ddtype)

    np_theta = hcl.cast_np(np.zeros((NUM_FEATURES, ), dtype=default_reg_type),
                           Ddtype)

    hcl_lut = hcl.asarray(np_lut, dtype=Ddtype)
    hcl_data = hcl.asarray(np_data, dtype=Ddtype)
    hcl_label = hcl.asarray(np_label, dtype=Ddtype)
    hcl_theta_out = hcl.asarray(np_theta, dtype=Ddtype)

    f = top_function(Ddtype)

    f(hcl_lut, hcl_data, hcl_label, hcl_theta_out)

    np_theta_out = hcl_theta_out.asnumpy()
    np_data = hcl_data.asnumpy()
    np_label = hcl_label.asnumpy()

    #np.set_printoptions(threshold=np.inf)

    print(np_theta_out)
    print(np_lut)
    print(np_data)
    print(np_label)

    np_train_data = np_data[:NUM_FEATURES * NUM_TRAINING]
    np_train_label = np_label[:NUM_TRAINING]

    train_error = 0.0
    for i in range(NUM_TRAINING):
        data = np_train_data[i * NUM_FEATURES:(i + 1) * NUM_FEATURES]
        dot = 0.0
        for j in range(NUM_FEATURES):
            dot += data[j] * np_theta_out[j]

        result = 1.0 if dot > 0 else 0.0

        if result != np_train_label[i]:
            train_error += 1.0

    print("training error rate")
    #print(train_error)
    print(train_error / NUM_TRAINING * 100)

    np_test_data = np_data[NUM_FEATURES * NUM_TRAINING:]
    np_test_label = np_label[NUM_TRAINING:]

    test_error = 0.0
    for i in range(NUM_TESTING):
        data = np_test_data[i * NUM_FEATURES:(i + 1) * NUM_FEATURES]
        dot = 0.0
        for j in range(NUM_FEATURES):
            dot += data[j] * np_theta_out[j]

        result = 1.0 if dot > 0 else 0.0

        if result != np_test_label[i]:
            test_error += 1.0
    print("testing error rate")
    #print(test_error)
    print(test_error / NUM_TESTING * 100)
Exemple #9
0
def update_knn_top():
    def test_update_knn():
    
        def popcount64(number):
                count= hcl.scalar(0, "count")
                with hcl.for_(0, 64) as i:
                    count.v += number[i]
                hcl.return_(count.v)
                
        def update_knn(train_inst, test_inst, dists, labels, label):
          
            #dist = hcl.scalar(0)
            diff = hcl.compute((DIGIT_WIDTH,), lambda x: test_inst[x] ^ train_inst[x], "diff")
            dist = hcl.compute((1,), lambda x: popcount64(diff), "dist")
        
            #max_dist = hcl.compute((1,), lambda x: 0, "max_dist")
            max_dist = hcl.scalar(0)
            max_dist_id = hcl.scalar(K_CONST + 1)
            
        
            with hcl.for_(0, K_CONST) as k:
                with hcl.if_(dists[k] > max_dist.v):
                    max_dist.v = dists[k]
                    max_dist_id.v = k
                
            with hcl.if_(dist[0] < max_dist.v):
                print("wat")
    #            dists[0] = dist[0]
                dists[max_dist_id.v] = dist[0]
                labels[max_dist_id.v] = label[0]
            hcl.print(dist)
            return dist
       
        test_inst = hcl.placeholder((DIGIT_WIDTH,), "test_inst")
        train_inst = hcl.placeholder((DIGIT_WIDTH,), "train_inst")
        dists = hcl.placeholder((K_CONST,), "dists")
        labels = hcl.placeholder((K_CONST,), "labels")
        label = hcl.placeholder((1,),"label")
        
        s = hcl.create_schedule([test_inst, train_inst, dists, labels, label], update_knn)
        return hcl.build(s)
    
    g = test_update_knn()
    
    #np_test_inst = np.random.randint(2, size = (DIGIT_WIDTH,))
    #np_test_inst = np.full((DIGIT_WIDTH,), 1)
    #np_train_inst = np.random.randint(2, size = (DIGIT_WIDTH,))
#    np_dists = np.array([20, 61, 60])
#    np_labels = np.array([9, 9, 9])
#    np_label = np.array([3])
    np_test_inst = hcl.cast_np(np.zeros((DIGIT_WIDTH,)), dtype = hcl.Int())
    np_train_inst = hcl.cast_np(np.zeros((DIGIT_WIDTH,)), dtype = hcl.Int())
    np_dists = np.array([0,0,0])
    np_labels = np.array([0,0,0])
    np_label = np.array([0])
    
    np_dist = hcl.cast_np(np.zeros((1,)), dtype = hcl.Int())
    
    hcl_test_inst = hcl.asarray(np_test_inst)
    hcl_train_inst = hcl.asarray(np_train_inst)
    hcl_dists = hcl.asarray(np_dists)
    hcl_labels = hcl.asarray(np_labels)
    hcl_label = hcl.asarray(np_label)
    
    hcl_dist = hcl.asarray(np_dist)
    
    g(hcl_test_inst, hcl_train_inst, hcl_dists, hcl_labels, hcl_label, hcl_dist)
      
    test_inst = hcl_test_inst.asnumpy()
    train_inst = hcl_train_inst.asnumpy()
    dists = hcl_dists.asnumpy()
    labels = hcl_labels.asnumpy()
    label = hcl_label.asnumpy()
    dist = hcl_dist.asnumpy()
Exemple #10
0
  hcl.init(dtype)
  
  def quantization(A):
      return hcl.compute(A.shape, lambda x: hcl.tanh(A[x]), "B")
  
  ##############################################################################
  # First, let's build the application without applying any quantization scheme.
  
  s = hcl.create_schedule([A], quantization)
  f = hcl.build(s)
  return f

import numpy as np

np_A = hcl.cast_np(np.zeros((10,), dtype = float), hcl.Fixed(10,2)) 

np_B = hcl.cast_np(np.zeros((10,), dtype = float), hcl.Fixed(10,2)) 

hcl_A = hcl.asarray(np_A, dtype = hcl.Fixed(10,2))
hcl_B = hcl.asarray(np_B, dtype = hcl.Fixed(10,2))

f = top(hcl.Fixed(10,2))

f(hcl_A, hcl_B)

np_A = hcl_A.asnumpy()
np_B = hcl_B.asnumpy()

print(np_A)
print('Before tanh')