def compare_with_tensorflow(device_type, input_shape, axis, keepdims):
    assert (device_type in ['gpu', 'cpu'])
    flow.clear_default_session()
    func_config = flow.FunctionConfig()
    func_config.default_data_type(flow.float)

    @flow.global_function(type='train', function_config=func_config)
    def ReduceMeanJob():
        with flow.scope.placement(device_type, '0:0'):
            x = flow.get_variable('x', shape=input_shape, dtype=flow.float, initializer=flow.random_uniform_initializer(minval=(- 10), maxval=10), trainable=True)
            loss = flow.math.reduce_mean(x, axis=axis, keepdims=keepdims)
            loss = flow.identity(loss)
            flow.optimizer.SGD(flow.optimizer.PiecewiseConstantScheduler([], [0.0001]), momentum=0).minimize(loss)
            flow.watch(x, test_global_storage.Setter('x'))
            flow.watch_diff(x, test_global_storage.Setter('x_diff'))
            flow.watch(loss, test_global_storage.Setter('loss'))
            flow.watch_diff(loss, test_global_storage.Setter('loss_diff'))
            return loss
    check_point = flow.train.CheckPoint()
    check_point.init()
    of_out = ReduceMeanJob().get()
    with tf.GradientTape(persistent=True) as tape:
        x = tf.Variable(test_global_storage.Get('x'))
        tf_out = tf.math.reduce_mean(x, axis=axis, keepdims=keepdims)
    loss_diff = test_global_storage.Get('loss_diff')
    tf_x_diff = tape.gradient(tf_out, x, loss_diff)
    assert np.allclose(of_out.numpy(), tf_out.numpy(), rtol=1e-05, atol=1e-05)
    assert np.allclose(test_global_storage.Get('x_diff'), tf_x_diff.numpy(), rtol=1e-05, atol=1e-05)
def compare_with_tensorflow(device_type, input_shape, axis, keepdims):
    assert (device_type in ['gpu', 'cpu'])
    flow.clear_default_session()
    func_config = flow.FunctionConfig()
    func_config.default_data_type(flow.float)

    @flow.global_function(type='train', function_config=func_config)
    def ReduceMeanJob():
        with flow.scope.placement(device_type, '0:0'):
            x = flow.get_variable('x', shape=input_shape, dtype=flow.float, initializer=flow.random_uniform_initializer(minval=(- 10), maxval=10), trainable=True)
            loss = flow.math.reduce_mean(x, axis=axis, keepdims=keepdims)
            loss = flow.identity(loss)
            flow.optimizer.SGD(flow.optimizer.PiecewiseConstantScheduler([], [0.0001]), momentum=0).minimize(loss)
            flow.watch(x, test_global_storage.Setter('x'))
            flow.watch_diff(x, test_global_storage.Setter('x_diff'))
            flow.watch(loss, test_global_storage.Setter('loss'))
            flow.watch_diff(loss, test_global_storage.Setter('loss_diff'))
            return loss
    check_point = flow.train.CheckPoint()
    check_point.init()
    of_out = ReduceMeanJob().get()
    with tf.GradientTape(persistent=True) as tape:
        x = tf.Variable(test_global_storage.Get('x'))
        tf_out = tf.math.reduce_mean(x, axis=axis, keepdims=keepdims)
    loss_diff = test_global_storage.Get('loss_diff')
    tf_x_diff = tape.gradient(tf_out, x, loss_diff)
    assert np.allclose(of_out.numpy(), tf_out.numpy(), rtol=1e-05, atol=1e-05)
    assert np.allclose(test_global_storage.Get('x_diff'), tf_x_diff.numpy(), rtol=1e-05, atol=1e-05)