def test_tuple_deriv(self): """Test tuples work via derivatives""" A = tile.Value.from_ndims(2) B = tile.Value.from_ndims(2) out_dims = (A.shape.dims[0], B.shape.dims[1]) out_shape = tile.Shape(tile.common_dtype(A.shape.dtype, B.shape.dtype), out_dims) out = tile.Operation( """ function (A[I, K], B[K, J]) -> (O) { T = tuple(A, B); C = element(T, 0); D = element(T, 1); O[i, j : I, J] = +(C[i, k] * D[k, j]); } """, [('A', A), ('B', B)], [('O', out_shape)]).outputs['O'] tot = op.summation(out, [0, 1]) dA = op.gradients(tot, [A])[0] func = tile.compose(self._ctx, self._dev, inputs=[('A', A), ('B', B)], outputs=[('DA', dA)]) invoker = plaidml.Invoker(self._ctx, func) invoker.set_input('A', self.make_inited_tensor((3, 3))) invoker.set_input('B', self.make_inited_tensor((3, 3))) output = self.make_output_tensor(invoker.get_output_shape('DA')) invoker.set_output('DA', output) invoker.invoke()
def testSameMaxPool(self): I = K.variable(np.array([1., 2., 3., 4., 5.])) code = """function (I[N]) -> (O) { O[i: (N + 1) / 2] = >(I[2 * i + j]), j < 2; }""" value = tile.Operation(code, [('I', I)], [('O', tile.Shape(I.shape.dtype, ((I.shape.dims[0] + 1) // 2,)))], name='ValidMaxpool') \ .sole_output() npt.assert_allclose(value.eval(), np.array([2., 4., 5.]))
def testSkipping(self): I = K.variable(np.array([[1., 2.], [3., 4.], [5., -4.], [-5., 6.], [-7., 9.]])) code = """function (I[N, M]) -> (O) { O[2 * i: N] = +(I[2 * i, j]); }""" value = tile.Operation(code, [('I', I)], [('O', tile.Shape(I.shape.dtype, (5,)))], name='Skip') \ .sole_output() npt.assert_allclose(value.eval(), np.array([3., 0., 1., 0., 2.]))
def testMaxOverAxis(self): I = K.variable(np.array([[1., 2., 3.], [4., 5., 6.]])) code = """function (I[M, N]) -> (O) { O[n: N] = >(I[m, n]); }""" value = tile.Operation(code, [('I', I)], [('O', tile.Shape(I.shape.dtype, (I.shape.dims[1],)))], name='MaxOverAxis') \ .sole_output() reference = K.max(I, axis=0) npt.assert_allclose(value.eval(), reference.eval())
def testCumSum(self): I = K.variable(np.array([1., 2., 3., 4., 5., 6.])) code = """function (I[N]) -> (O) { O[i: N] = +(I[i - j]), j < N; }""" value = tile.Operation(code, [('I', I)], [('O', tile.Shape(I.shape.dtype, (6,)))], name='CumulativeSum') \ .sole_output() code2 = """function (I[N]) -> (O) { O[i: N] = +(I[k]), i - k < N; }""" value2 = tile.Operation(code, [('I', I)], [('O', tile.Shape(I.shape.dtype, (6,)))], name='CumulativeSum2') \ .sole_output() reference = K.cumsum(I) npt.assert_allclose(value.eval(), reference.eval()) npt.assert_allclose(value2.eval(), reference.eval())
def testBrokenMaxPool(self): I = K.variable(np.array([1., 2., 3., 4., 5.])) code = """function (I[N]) -> (O) { O[i: N / 2] = >(I[2 * i + j]); }""" value = tile.Operation(code, [('I', I)], [('O', tile.Shape(I.shape.dtype, (I.shape.dims[0] // 2,)))], name='BrokenMaxpool') \ .sole_output() reference = K.max(I) npt.assert_allclose(value.eval(), [5.] * 2)
def testMeanOverAxis(self): I = K.variable(np.array([[1., 2., 3.], [4., 5., 6.]])) code = """function (I[X, Y]) -> (O) { Sum[y: Y] = +(I[x, y]); O = Sum / X; }""" value = tile.Operation(code, [('I', I)], [('O', tile.Shape(I.shape.dtype, (I.shape.dims[1],)))], name='MeanOverAxis') \ .sole_output() reference = K.mean(I, axis=0) npt.assert_allclose(value.eval(), reference.eval())
def testMatMul(self): A = K.variable(np.array([[1., 2., 3.], [4., 5., 6.]])) B = K.variable(np.array([[1., -2.], [-3., 4.], [5., -6.]])) code = """function (A[M, L], B[L, N]) -> (C) { C[i, j: M, N] = +(A[i, k] * B[k, j]); }""" value = tile.Operation(code, [('A', A), ('B', B)], [('C', tile.Shape(A.shape.dtype, (2, 2)))], name='MatMul') \ .sole_output() reference = K.dot(A, B) npt.assert_allclose(value.eval(), reference.eval())
def testConv1D(self): I = K.variable(m(2, 8, 3)) kernel = K.variable(m(3, 3, 2)) code = """function (I[N, L, CI], K[LK, CI, CO]) -> (O) { O[n, x, co: N, L - LK + 1, CO] = +(I[n, x + k, ci] * K[k, ci, co]); }""" value = tile.Operation(code, [('I', I), ('K', kernel)], [('O', tile.Shape(I.shape.dtype, (2, 6, 2)))], name='CumulativeSum') \ .sole_output() reference = K.conv1d(I, kernel, padding='valid') npt.assert_allclose(value.eval(), reference.eval())
def testGlobalMean(self): I = K.variable(np.array([[1., 2., 3.], [4., 5., 6.]])) code = """function (I[X, Y]) -> (O) { Sum[] = +(I[x, y]); O = Sum / (X * Y); }""" value = tile.Operation(code, [('I', I)], [('O', tile.Shape(I.shape.dtype, tuple()))], name='GlobalMean') \ .sole_output() reference = K.mean(I, axis=[0, 1]) npt.assert_allclose(value.eval(), reference.eval())
def testGlobalMin(self): I = K.variable(np.array([[[1., 2., 3.], [4., 5., 6.]]])) code = """function (I) -> (O) { Neg = -I; O_Neg[] = >(Neg[i, j, k]); O = -O_Neg; }""" value = tile.Operation(code, [('I', I)], [('O', tile.Shape(I.shape.dtype, tuple()))], name='GlobalMin') \ .sole_output() reference = K.min(I, axis=[0, 1, 2]) npt.assert_allclose(value.eval(), reference.eval())
def testDilatedConv2D(self): I = K.variable(m(2, 6, 10, 3)) kernel = K.variable(m(3, 2, 3, 2)) code = """function (I[N, Lx, Ly, CI], K[LKx, LKy, CI, CO]) -> (O) { O[n, x, y, co: N, Lx - 2 * (LKx - 1), Ly - 3 * (LKy - 1), CO] = +(I[n, x + 2 * kx, y + 3 * ky, ci] * K[kx, ky, ci, co]); }""" value = tile.Operation(code, [('I', I), ('K', kernel)], [('O', tile.Shape(I.shape.dtype, (2, 2, 7, 2)))], name='CumulativeSum') \ .sole_output() reference = K.conv2d(I, kernel, padding='valid', dilation_rate=(2, 3)) npt.assert_allclose(value.eval(), reference.eval())
def __init__(self, lhs, rhs): lmax = ismax(lhs.source.op.value, axes=(lhs.source.op.axis, )) rmax = ismax(rhs.source.op.value, axes=(rhs.source.op.axis, )) and_shape = tile.Shape( plaidml.DType.INT32, tile.broadcast_dims(lmax.shape.dims, rmax.shape.dims)) and_op = tile.Operation( 'function (L, R) -> (O) { O = L ? (R ? 1 : 0) : 0; }', [('L', lmax), ('R', rmax)], [('O', and_shape)]) sum_val = summation(and_op.output_tuple[0], axes=(lhs.source.op.axis, ), keepdims=True) eq_shape = tile.Shape(plaidml.DType.BOOLEAN, sum_val.shape.dims) super(Equal_ArgMax, self).__init__('function (I) -> (O) { O = 0 < I; }', [('I', sum_val)], [('O', eq_shape)])