Beispiel #1
0
    def test_collect_passes(self):
        A = Placeholder(plaidml.DType.FLOAT32, [10, 10])
        B = Placeholder(plaidml.DType.FLOAT32, [10, 10])
        C = dot(A, B)
        program = Program('collect_passes', [A, B], [C])
        program.compile(debug=True)

        first_pass = program.passes[0]
        self.assertEqual('tile', first_pass[0])
Beispiel #2
0
 def test_placeholder(self):
     I = Placeholder(plaidml.DType.INT32)
     program = Program('placeholder_noshape', [I], [I])
     self.assertEqual(str(I.compute_shape()), "si32")
     I = Placeholder(plaidml.DType.INT32, [1, 1])
     program = Program('placeholder_noname', [I], [I])
     self.assertEqual(str(I.compute_shape()), "1x1xsi32")
     I = Placeholder(plaidml.DType.INT32, [1, 1], name='I')
     program = Program('placeholder_with_name', [I], [I])
     self.assertEqual(str(I.compute_shape()), "1x1xsi32")
 def test_gemv2(self):
     A = Placeholder(plaidml.DType.FLOAT32, [1, 7])
     B = Placeholder(plaidml.DType.FLOAT32, [7])
     C = Placeholder(plaidml.DType.FLOAT32, [7])
     O = gemv2(A, B, C, 4, 5)
     program = Program('gemv', [A, B, C], [O])
     self.runProgram(program)
Beispiel #4
0
 def test_defract_long(self):
     shape = [1, 3, 3, 1]
     I = Placeholder(plaidml.DType.FLOAT32, shape, name='I')
     K = Placeholder(plaidml.DType.FLOAT32, shape, name='K')
     n, x0, x1, c0, c1, co, ci, k0, k1 = TensorIndexes(9)
     O = Contraction() \
         .outShape(1, 5, 5, 1) \
         .outAccess(n, x0, x1, co) \
         .sum(I[n, (x0 + k0 - 1) // 2, (x1 + k1 - 1) // 2, ci] * K[2 - k0, 2 - k1, co, ci]) \
         .build()
     program = Program('defract_long', [I, K], [O])
     self.checkProgram(program, [
         np.array([[
             [[1], [3], [-1]],
             [[0], [2], [4]],
             [[1], [-1], [-2]],
         ]]),
         np.array([[
             [[2], [3], [4]],
             [[6], [-3], [-1]],
             [[-1], [-2], [1]],
         ]]),
     ], [[[
         [[0], [0], [0], [0], [0]],
         [[0], [4], [12], [6], [24]],
         [[0], [0], [0], [0], [0]],
         [[6], [-3], [-6], [-3], [-12]],
         [[0], [0], [0], [0], [0]],
     ]]])
Beispiel #5
0
 def test_unique_names(self):
     A = Placeholder(plaidml.DType.FLOAT32, name='A')
     B = Placeholder(plaidml.DType.FLOAT32, name='B')
     C0 = Placeholder(plaidml.DType.FLOAT32, name='C')
     C1 = Placeholder(plaidml.DType.FLOAT32, name='C')
     program = Program('unique_names', [A, B, C0, C1], [A + B + C0 + C1])
     self.runProgram(program)
 def test_gemm(self):
     A = Placeholder(plaidml.DType.FLOAT32, [7, 7])
     B = Placeholder(plaidml.DType.FLOAT32, [7, 7])
     C = Placeholder(plaidml.DType.FLOAT32, [7, 7])
     O = gemm(A, B, C)
     program = Program('gemm', [A, B, C], [O])
     self.runProgram(program)
Beispiel #7
0
 def test_defract_short(self):
     I = Placeholder(plaidml.DType.FLOAT32, [3], name='I')
     i, j = TensorIndexes(2)
     O = Contraction().outShape(6).outAccess(i).sum(I[(i - 1) // 2]).build()
     program = Program('defract_short_test', [I], [O])
     self.checkProgram(program, [
         np.array([1, 2, 3]),
     ], [
         [0, 1, 0, 2, 0, 3],
     ])
Beispiel #8
0
 def test_lars_momentum4d(self):
     X_shape = TensorShape(plaidml.DType.FLOAT32, [4, 7, 3, 9])
     LR_Shape = TensorShape(plaidml.DType.FLOAT32)
     X = Placeholder(X_shape)
     Grad = Placeholder(X_shape)
     Veloc = Placeholder(X_shape)
     LR = Placeholder(LR_Shape)
     R = lars_momentum(X, Grad, Veloc, LR, 1. / 1024., 1. / 2048., 1. / 8.)
     program = Program('lars_momentum4d', [X, Grad, Veloc, LR], R)
     self.runProgram(program)
Beispiel #9
0
    def test_two_outputs(self):
        I = Placeholder(plaidml.DType.FLOAT32, [3], name='I')
        program1 = Program('two_outputs', [I], [I, I])
        self.checkProgram(program1, [
            np.array([1, 2, 3]),
        ], [
            [1, 2, 3],
            [1, 2, 3],
        ])

        O1 = I
        O2 = I
        program2 = Program('two_outputs', [I], [O1, O2])
        self.assertMultiLineEqual(str(program1), str(program2))
        self.checkProgram(program2, [
            np.array([1, 2, 3]),
        ], [
            [1, 2, 3],
            [1, 2, 3],
        ])
Beispiel #10
0
 def test_defract(self):
     I = Placeholder(plaidml.DType.FLOAT32, [3], name='I')
     K = Placeholder(plaidml.DType.FLOAT32, [3], name='K')
     i, j = TensorIndexes(2)
     O = Contraction().outShape(5).outAccess(i).sum(I[(i - j + 1) // 2] * K[j]).build()
     program = Program('defract_test', [I, K], [O])
     self.checkProgram(program, [
         np.array([1, 2, 3]),
         np.array([1, 2, 3]),
     ], [
         [2, 5, 4, 9, 6],
     ])
Beispiel #11
0
    def test_eltwise(self):
        A = Placeholder(plaidml.DType.FLOAT32, [3])
        B = Placeholder(plaidml.DType.FLOAT32, [3])
        O = A + B

        program = Program('eltwise_add', (A, B), [O])
        self.checkProgram(program, [
            np.array([1, 2, 3]),
            np.array([3, 2, 1]),
        ], [
            [4, 4, 4],
        ])
Beispiel #12
0
    def test_lens(self):
        I = Placeholder(plaidml.DType.FLOAT32, [1, 224, 224, 3])
        K = Placeholder(plaidml.DType.FLOAT32, [3, 3, 3, 32])
        O = conv_2d(I, K, I_layout='NHWC', K_layout='HWCK')
        program = Program('conv2d_nhwc', [I, K], [O])
        self.runProgram(program)

        I = Placeholder(plaidml.DType.FLOAT32, [1, 3, 224, 224])
        K = Placeholder(plaidml.DType.FLOAT32, [3, 32, 7, 7])
        O = conv_2d(I, K, I_layout='NCHW', K_layout='CKHW')
        program = Program('conv2d_nchw', [I, K], [O])
        self.runProgram(program)

        def transpose(I, layout='MN'):
            lens = TensorLens(layout, 'MN')
            I = I.use(lens)
            M, N = TensorDims(2)
            i, j = TensorIndexes(2)
            I.bind_dims(M, N)
            return Contraction(lens).outShape(N, M).outAccess(j, i).assign(I[i, j]).build()

        input = np.array([
            [1, 2, 3],
            [4, 5, 6],
        ])
        expected = [
            [1, 4],
            [2, 5],
            [3, 6],
        ]

        I = Placeholder(plaidml.DType.FLOAT32, [2, 3])
        O = transpose(I, 'MN')
        program = Program('transpose_mn', [I], [O])
        self.checkProgram(program, [input], [expected])

        I = Placeholder(plaidml.DType.FLOAT32, [2, 3])
        O = transpose(I, 'NM')
        program = Program('transpose_nm', [I], [O])
        self.checkProgram(program, [input], [expected])
Beispiel #13
0
 def test_repeat_elts(self):
     I = Placeholder(plaidml.DType.FLOAT32, [10, 10, 10])
     N0, N1, N2 = TensorDims(3)
     n0, n1, n2, k = TensorIndexes(4)
     I.bind_dims(N0, N1, N2)
     O = Contraction() \
         .outShape(N0, 3 * N1, N2) \
         .outAccess(n0, 3 * n1 + k, n2) \
         .assign(I[n0, n1, n2]) \
         .add_constraint(k < 3) \
         .build()
     program = Program('repeat_elts', [I], [O])
     self.runProgram(program)
Beispiel #14
0
 def test_use_default(self):
     P = Placeholder(plaidml.DType.FLOAT32, [1, 7, 10, 10])
     I = Placeholder(plaidml.DType.FLOAT32, [1, 10, 10])
     B, N1, N2 = TensorDims(3)
     b, i1, i2 = TensorIndexes(3)
     I.bind_dims(B, N1, N2)
     O = Contraction() \
         .outShape(B, 7, N1, N2) \
         .outAccess(b, 3, i1, i2) \
         .init(P) \
         .assign(I[b, i1, i2]) \
         .build()
     program = Program('use_default', [I, P], [O])
     self.runProgram(program)
Beispiel #15
0
 def test_argsort_1d_dup(self):
     I = Placeholder(plaidml.DType.FLOAT32, [20])
     O = argsort(I, axis=0, direction=SortDirection.ASC)
     program = Program('argsort_1d_dup', [I], [O])
     input = np.array([
         81.69,
         81.69,
         27.74,
         43.69,
         55.79,
         56.79,
         57.52,
         7.11,
         39.48,
         5.9,
         14.81,
         66.23,
         20.25,
         66.05,
         64.5,
         71.07,
         67.6,
         54.42,
         87.59,
         80.02,
     ])
     expected = [
         9,
         7,
         10,
         12,
         2,
         8,
         3,
         17,
         4,
         5,
         6,
         14,
         13,
         11,
         16,
         15,
         19,
         0,
         1,
         18,
     ]
     self.checkProgram(program, [input], [expected])
Beispiel #16
0
 def test_argsort_1d_int(self):
     I = Placeholder(plaidml.DType.FLOAT32, [20])
     O = argsort(I, axis=0, direction=SortDirection.ASC)
     program = Program('argsort_1d_int', [I], [O])
     input = np.array([
         81,
         95,
         27,
         43,
         55,
         56,
         57,
         5,
         39,
         7,
         14,
         67,
         20,
         66,
         64,
         71,
         68,
         54,
         87,
         80,
     ])
     expected = [
         7,
         9,
         10,
         12,
         2,
         8,
         3,
         17,
         4,
         5,
         6,
         14,
         13,
         11,
         16,
         15,
         19,
         0,
         18,
         1,
     ]
     self.checkProgram(program, [input], [expected])
Beispiel #17
0
    def test_assignment_exceptions(self):
        A = Placeholder(plaidml.DType.FLOAT32, [5, 1], name='A')
        B = Placeholder(plaidml.DType.FLOAT32, [1, 5], name='B')
        L, M, N = TensorDims(3)
        i, j, k = TensorIndexes(3)
        A.bind_dims(L, M)
        B.bind_dims(M, N)
        O = Contraction().outShape(L, N).outAccess(i, j).assign(A[i, k] * B[k, j]).build()
        program = Program('assignment_non_exception', [A, B], [O])
        self.checkProgram(program, [
            np.array([[1], [2], [3], [4], [5]]),
            np.array([1, 2, 3, 4, 5]),
        ], [[
            [1., 2., 3., 4., 5.],
            [2., 4., 6., 8., 10.],
            [3., 6., 9., 12., 15.],
            [4., 8., 12., 16., 20.],
            [5., 10., 15., 20., 25.],
        ]])

        O = Contraction().outShape(L, N).outAccess(i, j).assign(B[i, k] * A[k, j]).build()
        with self.assertRaises(plaidml.Error) as cm:
            program = Program('assignment_exception', [A, B], [O])
        self.assertTrue("illegal assignment aggregation" in str(cm.exception))
Beispiel #18
0
 def test_mnist_mlp(self):
     # model.add(Dense(512, activation='relu', input_shape=(784,)))
     I = Placeholder(plaidml.DType.FLOAT32, [1, 784])
     K1 = Placeholder(plaidml.DType.FLOAT32, [784, 512])
     B1 = Placeholder(plaidml.DType.FLOAT32, [512])
     D1 = relu(dot(I, K1) + B1)
     # model.add(Dense(512, activation='relu'))
     K2 = Placeholder(plaidml.DType.FLOAT32, [512, 512])
     B2 = Placeholder(plaidml.DType.FLOAT32, [512])
     D2 = relu(dot(D1, K2) + B2)
     # model.add(Dense(10, activation='softmax'))
     K3 = Placeholder(plaidml.DType.FLOAT32, [512, 10])
     B3 = Placeholder(plaidml.DType.FLOAT32, [10])
     D3 = softmax(dot(D2, K3) + B3)
     program = Program('mnist_mlp', [I, K1, B1, K2, B2, K3, B3], [D3])
     self.runProgram(program)
Beispiel #19
0
    def test_argsort_3d_axis_0_asc(self):
        I = Placeholder(plaidml.DType.FLOAT32, [3, 4, 5])
        O = argsort(I, axis=0, direction=SortDirection.ASC)
        program = Program('argsort_3d_axis_0_asc', [I], [O])
        input = np.array([
            [
                [0.508, 0.001, 0.833, 0.186, 0.960],
                [0.405, 0.621, 0.183, 0.769, 0.331],
                [0.726, 0.678, 0.027, 0.789, 0.544],
                [0.151, 0.453, 0.512, 0.513, 0.451],
            ],
            [
                [0.875, 0.089, 0.909, 0.353, 0.829],
                [0.238, 0.511, 0.619, 0.214, 0.818],
                [0.085, 0.713, 0.649, 0.373, 0.654],
                [0.615, 0.865, 0.268, 0.713, 0.171],
            ],
            [
                [0.218, 0.272, 0.702, 0.621, 0.224],
                [0.236, 0.746, 0.508, 0.189, 0.503],
                [0.177, 0.096, 0.466, 0.228, 0.759],
                [0.771, 0.567, 0.594, 0.211, 0.183],
            ],
        ])
        expected = [
            [
                [2, 0, 2, 0, 2],
                [2, 1, 0, 2, 0],
                [1, 2, 0, 2, 0],
                [0, 0, 1, 2, 1],
            ],
            [
                [0, 1, 0, 1, 1],
                [1, 0, 2, 1, 2],
                [2, 0, 2, 1, 1],
                [1, 2, 0, 0, 2],
            ],
            [
                [1, 2, 1, 2, 0],
                [0, 2, 1, 0, 1],
                [0, 1, 1, 0, 2],
                [2, 1, 2, 1, 0],
            ],
        ]

        self.checkProgram(program, [input], [expected])
Beispiel #20
0
    def test_constant_add(self):

        def makeBuffer(shape, data):
            buf = plaidml.Buffer(shape)
            buf.copy_from_ndarray(data)
            return buf

        shape = TensorShape(plaidml.DType.INT32, [4])
        a_buf = makeBuffer(shape, np.array([4, 3, 2, 1]))
        b_buf = makeBuffer(shape, np.array([1, 2, 3, 4]))
        A = Constant(a_buf, name="A")
        B = Constant(b_buf, name="B")
        O = A + B
        program = Program('const_add', [], [O])
        self.checkProgram(program, [], [[5, 5, 5, 5]])
        self.assertEqual(len(program.inputs), 0)
        self.assertEqual(len(program.outputs), 1)
Beispiel #21
0
    def test_higher_precision_constants(self):
        I = Placeholder(plaidml.DType.FLOAT32, [3, 3])
        O = I + 1 + 2.0

        program = Program('higher_precision_constants', [I], [O])
        self.checkProgram(program, [
            np.array([
                [1, 2, 3],
                [4, 5, 6],
                [7, 8, 9],
            ]),
        ], [
            [
                [4, 5, 6],
                [7, 8, 9],
                [10, 11, 12],
            ],
        ])
Beispiel #22
0
def cg_solve(np_A, np_r, timing=False, resthrsh=RESIDUAL_THRESHOLD):
    N = len(np_r)

    np_x = np.zeros(N)
    np_rsq = np.matmul(np_r, np_r)
    np_p = np_r.copy()

    dtype = plaidml.DType.FLOAT32

    A = edsl.Placeholder(dtype, np_A.shape)
    X = edsl.Placeholder(dtype, np_x.shape)
    R = edsl.Placeholder(dtype, np_r.shape)
    P = edsl.Placeholder(dtype, np_p.shape)
    RSQ = edsl.Placeholder(dtype, np_rsq.shape)

    Ap = matmul(A, P)
    alpha = RSQ / op.sum(P * Ap, axis=0)
    OX = X + alpha * P
    OR = R - alpha * Ap
    RSQN = op.sum(OR * OR, axis=0)
    OP = OR + RSQN / RSQ * P

    Is = [A, P, RSQ, X, R]
    Os = [OX, OR, RSQN, OP]
    program = Program('las_step', Is, Os)

    t0 = time.time()
    inputs = [np_A, np_p, np_rsq, np_x, np_r]
    for tt in range(1):
        outputs = plaidml.exec.run(program, inputs)
        if outputs[2] < resthrsh:
            break
        else:
            inputs[1] = outputs[3]
            inputs[2] = outputs[2]
            inputs[3] = outputs[0]
            inputs[4] = outputs[1]

    tm = time.time() - t0

    np_x = outputs[0]
    if timing:
        return np_x, tm
    return np_x
Beispiel #23
0
 def test_argsort_2d_axis_1(self):
     I = Placeholder(plaidml.DType.FLOAT32, [5, 4])
     O = argsort(I, axis=1)
     program = Program('argsort_2d_axis_1', [I], [O])
     input = np.array([
         [81.69, 95.74, 27.74, 43.69],
         [55.79, 56.79, 57.52, 5.9],
         [39.48, 7.11, 14.81, 66.23],
         [20.25, 66.05, 64.5, 71.07],
         [67.6, 54.42, 87.59, 80.02],
     ])
     expected = [
         [2, 3, 0, 1],
         [3, 0, 1, 2],
         [1, 2, 0, 3],
         [0, 2, 1, 3],
         [1, 0, 3, 2],
     ]
     self.checkProgram(program, [input], [expected])
Beispiel #24
0
    def test_funky_names(self):
        '''Exercises fix for plaidml bug #241

        Now that we emit keras layer names as 'pid' attribute values, in order
        to help link tile code back to its origin while debugging, we must
        reformat those names as valid tile identifiers. If we are doing that,
        this test will pass, otherwise we'll get a syntax error.'''

        I = Placeholder(plaidml.DType.FLOAT32, [3], name='I')
        K = Placeholder(plaidml.DType.FLOAT32, [3], name='K')
        i, j = TensorIndexes(2)
        O = Contraction().outShape(5).outAccess(i).sum(I[(i - j + 1) // 2] * K[j]).build()
        program = Program('this-is-not an identifier', [I, K], [O])
        self.checkProgram(program, [
            np.array([1, 2, 3]),
            np.array([1, 2, 3]),
        ], [
            [2, 5, 4, 9, 6],
        ])
Beispiel #25
0
    def test_broadcast_cmp(self):
        A = Placeholder(plaidml.DType.INT64, [3, 4])
        B = Placeholder(plaidml.DType.INT64, [3, 1])
        O = cast(A >= B, DType.INT64)

        program = Program('broadcast_cmp', [A, B], [O])
        self.checkProgram(program, [
            np.array([
                [0, 1, 2, 3],
                [4, 5, 6, 7],
                [8, 9, 10, 11],
            ]),
            np.array([
                [0],
                [6],
                [12],
            ]),
        ], [
            [
                [1, 1, 1, 1],
                [0, 0, 1, 1],
                [0, 0, 0, 0],
            ],
        ])
Beispiel #26
0
 def test_mnist_cnn(self):
     # model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=input_shape))
     I = Placeholder(plaidml.DType.FLOAT32, [1, 224, 224, 1])
     K1 = Placeholder(plaidml.DType.FLOAT32, [3, 3, 1, 32])
     B1 = Placeholder(plaidml.DType.FLOAT32, [32])
     C1 = relu(conv_2d(I, K1) + B1)
     # model.add(Conv2D(64, (3, 3), activation='relu'))
     K2 = Placeholder(plaidml.DType.FLOAT32, [3, 3, 32, 64])
     B2 = Placeholder(plaidml.DType.FLOAT32, [64])
     C2 = relu(conv_2d(C1, K2) + B2)
     # model.add(MaxPooling2D(pool_size=(2, 2)))
     P1 = max_pool_2d(C2)
     # model.add(Flatten())
     F = flatten(P1)
     self.assertEqual(str(F.compute_shape()), '1x774400xf32')
     K3 = Placeholder(plaidml.DType.FLOAT32, [774400, 128])
     B3 = Placeholder(plaidml.DType.FLOAT32, [128])
     D1 = relu(dot(F, K3) + B3)
     # model.add(Dense(num_classes, activation='softmax'))
     K4 = Placeholder(plaidml.DType.FLOAT32, [128, 100])
     B4 = Placeholder(plaidml.DType.FLOAT32, [100])
     D2 = softmax(dot(D1, K4) + B4)
     program = Program('mnist_cnn', [I, K1, B1, K2, B2, K3, B3, K4, B4], [D2])
     self.runProgram(program)
Beispiel #27
0
 def test_identity(self):
     I = Placeholder(plaidml.DType.FLOAT32, [3], name='I')
     program = Program('identity', [I], [I])
     self.checkProgram(program, [np.array([(1, 2, 3)])], [[1, 2, 3]])
Beispiel #28
0
 def test_arg_max(self):
     I = Placeholder(plaidml.DType.FLOAT32, [1, 10, 10])
     O = arg_max(I)
     program = Program('arg_max', [I], [O])
     self.assertEqual(str(O.compute_shape()), '1x10xui32')
     self.runProgram(program)
Beispiel #29
0
 def test_global_min(self):
     I = Placeholder(plaidml.DType.FLOAT32, [10, 10, 10], name='I')
     O = global_min(I)
     program = Program('global_min', [I], [O])
     self.runProgram(program)
Beispiel #30
0
 def test_trace(self):
     I = Placeholder(plaidml.DType.FLOAT32, [3, 3])
     O = trace(I, 'msg')
     program = Program('trace', [I], [O])