Exemplo n.º 1
0
from deep500.frameworks import reference as d5ref
from deep500 import test_op_forward, test_op_gradient

if __name__ == '__main__':
    with tf.Session() as sess:
        # Create two matrices A and B
        A = np.random.rand(5, 40).astype(np.float64)
        B = np.random.rand(40, 5).astype(np.float64)

        # Native (TensorFlow) versions of A and B
        native_A = tf.Variable(A, expected_shape=A.shape, dtype=A.dtype)
        native_B = tf.Variable(B, expected_shape=B.shape, dtype=B.dtype)

        # Create a Deep500 Operator from the tf.matmul function.
        # The native TF operator accepts two inputs and one output, whose
        # tensor descriptors (i.e., shape and data type) are created below.
        op = d5tf.custom_op_from_native(
            tf.matmul, [d5ref.desc_from_tensor(A),
                        d5ref.desc_from_tensor(B)],
            [d5ref.desc_from_tensor(A @ B)])

        # Test forward (first using Deep500, which copies to/from numpy,
        # then natively through the framework)
        test_op_forward(op, [A, B], [A @ B])
        d5tf.test_nativeop_forward(tf.matmul, [native_A, native_B], [A @ B])

        # Test backward (gradient) using Deep500 and native tests.
        test_op_gradient(op, [A, B], eps=1e-7)
        d5tf.test_nativeop_gradient(tf.matmul, [native_A, native_B], eps=1e-7)
Exemplo n.º 2
0
                                 kernel_size=[test.r, test.s],
                                 strides=[test.hstride, test.wstride],
                                 data_format='channels_first',
                                 kernel_initializer=tf.constant_initializer(W))

        # Create a conv2d function that only receives one variable
        cnv2d = lambda x: tf.layers.conv2d(
            x,
            filters=test.k,
            kernel_size=[test.r, test.s],
            strides=[test.hstride, test.wstride],
            data_format='channels_first',
            kernel_initializer=tf.constant_initializer(W))

        times, = \
            d5tf.test_nativeop_forward(cnv2d, [var_X], [None],
                                       metrics=[d5.WallclockTime(RUNS*AVG_OVER, AVG_OVER)])
    except Exception as ex:
        print('Exception:', ex)
        times = [-1.0]
    # Clear memory
    tf.reset_default_graph()

    with open('0_d5tf_conv_deepbench.log', 'a') as fp:
        fp.writelines([
            '{test.n},{test.c},{test.h},{test.w},'
            '{test.k},{test.r},{test.s},'
            '{test.hstride},{test.wstride},'
            '{time:.15f}\n'.format(test=test, time=time) for time in times
        ])
Exemplo n.º 3
0
        if test.b_trans:
            B = np.random.rand(test.n, test.k).astype(np.float32)
            var_B = tf.Variable(B, name="var_B", dtype=tf.float32)
            Bmult = B.transpose()
        else:
            B = np.random.rand(test.k, test.n).astype(np.float32)
            var_B = tf.Variable(B, name="var_B", dtype=tf.float32)
            Bmult = B

        # Create a matmul function that only receives two variables
        mm = lambda a,b: tf.matmul(a,b, test.a_trans, test.b_trans)

        # Run Deep500 test
        l2err, maxerr, times = \
            d5tf.test_nativeop_forward(mm, [var_A, var_B], [Amult @ Bmult],
                                       metrics=[d5.L2Error(), d5.MaxError(), 
                                                d5.WallclockTime(RUNS*AVG_OVER, AVG_OVER)])
    except:
        l2err = -1.0
        maxerr = -1.0
        times = [-1.0]
    # Clear memory
    tf.reset_default_graph()

    with open('0_d5tf_gemm_deepbench.log', 'a') as fp:
        fp.writelines(['{test.m},{test.n},{test.k},'
                       '{test.a_trans},{test.b_trans},'
                       '{time:.15f},{l2err},{maxerr}\n'.format(test=test, 
                            time=time, l2err=l2err, maxerr=maxerr) 
                        for time in times])