def test_eager_input(test_case):
        flow.clear_default_session()
        flow.enable_eager_execution()
        func_config = flow.FunctionConfig()
        func_config.default_logical_view(flow.scope.mirrored_view())
        input = np.random.rand(2, 5).astype(np.single)
        output = np.maximum(input, 0)

        @flow.global_function(function_config=func_config)
        def foo_job(x_def: oft.ListNumpy.Placeholder(shape=(2, 5),
                                                     dtype=flow.float)):
            y = flow.math.relu(x_def)
            test_case.assertTrue(np.allclose(y.numpy(0), output))

        foo_job([input])
    def test_eager_input_fixed(test_case):
        flow.clear_default_session()
        flow.enable_eager_execution()
        func_config = flow.FunctionConfig()
        func_config.default_logical_view(flow.scope.mirrored_view())
        input = np.arange(10).astype(np.single)
        output = input + 1.0

        @flow.global_function(function_config=func_config)
        def foo_job(x_def: oft.Numpy.Placeholder(shape=(10, ),
                                                 dtype=flow.float)):
            y = x_def + flow.constant(1.0, shape=(1, ), dtype=flow.float)
            test_case.assertTrue(np.allclose(y.numpy(0), output))

        foo_job(input)
    def test_eager_output(test_case):
        flow.clear_default_session()
        flow.enable_eager_execution()
        func_config = flow.FunctionConfig()
        func_config.default_logical_view(flow.scope.mirrored_view())

        @flow.global_function(function_config=func_config)
        def foo_job():
            x = flow.constant(1, shape=(2, 5), dtype=flow.float)
            return x

        ret = foo_job().get()
        test_case.assertTrue(
            np.array_equal(np.ones(shape=(2, 5), dtype=np.single),
                           ret.numpy_list()[0]))
    def test_eager_input_output(test_case):
        flow.clear_default_session()
        flow.enable_eager_execution()
        func_config = flow.FunctionConfig()
        func_config.default_logical_view(flow.scope.mirrored_view())
        input = np.random.rand(5, 4).astype(np.single)
        output = input * 2.0

        @flow.global_function(function_config=func_config)
        def foo_job(x_def: oft.ListNumpy.Placeholder(shape=(5, 4),
                                                     dtype=flow.float)):
            y = x_def * flow.constant(2.0, shape=(1, ), dtype=flow.float)
            return y

        ret = foo_job([input]).get()
        test_case.assertTrue(np.allclose(output, ret.numpy_list()[0]))
    def test_eager_multi_input(test_case):
        flow.clear_default_session()
        flow.enable_eager_execution()
        func_config = flow.FunctionConfig()
        func_config.default_logical_view(flow.scope.mirrored_view())
        input_1 = np.random.rand(3, 4).astype(np.single)
        input_2 = np.array([2]).astype(np.single)
        output = input_1 * input_2

        @flow.global_function(function_config=func_config)
        def foo_job(
                x_def: oft.ListNumpy.Placeholder(shape=(3, 4),
                                                 dtype=flow.float),
                y_def: oft.ListNumpy.Placeholder(shape=(1, ),
                                                 dtype=flow.float),
        ):
            y = x_def * y_def
            test_case.assertTrue(np.allclose(y.numpy(0), output))

        foo_job([input_1], [input_2])
Esempio n. 6
0
    def test_2d_gpu_variable(test_case):
        flow.enable_eager_execution()
        flow.config.gpu_device_num(2)
        device_name = "0:0-1"

        @flow.global_function(type="train", function_config=flow.FunctionConfig())
        def Foo():
            with flow.scope.placement("gpu", device_name):
                w = flow.get_variable(
                    "w",
                    shape=(10,),
                    dtype=flow.float,
                    initializer=flow.constant_initializer(0),
                )
                print(w.numpy(0))
            flow.optimizer.SGD(
                flow.optimizer.PiecewiseConstantScheduler([], [0.1]), momentum=0
            ).minimize(w)

        Foo()
        Foo()
    def test_lazy_input_output(test_case):
        flow.clear_default_session()
        flow.enable_eager_execution(False)
        func_config = flow.FunctionConfig()
        func_config.default_logical_view(flow.scope.mirrored_view())

        @flow.global_function(function_config=func_config)
        def foo_job(input_def: oft.Numpy.Placeholder(shape=(2, 5))):
            var = flow.get_variable(
                name="var",
                shape=(2, 5),
                dtype=flow.float,
                initializer=flow.ones_initializer(),
            )
            input_def = flow.cast_to_current_logical_view(input_def)
            var = flow.cast_to_current_logical_view(var)
            output = var + input_def
            return output

        input = np.arange(10).reshape(2, 5).astype(np.single)
        ret = foo_job(input).get()
        output = input + np.ones(shape=(2, 5), dtype=np.single)
        test_case.assertTrue(np.array_equal(output, ret.numpy()))
    def test_eager_multi_output(test_case):
        flow.clear_default_session()
        flow.enable_eager_execution()
        func_config = flow.FunctionConfig()
        func_config.default_logical_view(flow.scope.mirrored_view())

        @flow.global_function(function_config=func_config)
        def foo_job():
            x = flow.constant(1, shape=(2, 5), dtype=flow.float)
            y = flow.get_variable(
                name="var",
                shape=(64, 4),
                dtype=flow.float,
                initializer=flow.zeros_initializer(),
            )
            return (x, y)

        (x, y) = foo_job().get()
        test_case.assertTrue(
            np.array_equal(np.ones(shape=(2, 5), dtype=np.single),
                           x.numpy_list()[0]))
        test_case.assertTrue(
            np.array_equal(np.zeros(shape=(64, 4), dtype=np.single),
                           y.numpy()))
Esempio n. 9
0
 def setUp(self):
     flow.clear_default_session()
     flow.enable_eager_execution(False)
Esempio n. 10
0
 def setUp(self):
     global _unittest_env_initilized
     global _unittest_worker_initilized
     if has_node_list():
         assert node_size() > 1
         if _unittest_worker_initilized == False:
             master_port = os.getenv("ONEFLOW_TEST_MASTER_PORT")
             assert master_port, "env var ONEFLOW_TEST_MASTER_PORT not set"
             flow.env.ctrl_port(int(master_port))
             data_port = os.getenv("ONEFLOW_TEST_DATA_PORT")
             if data_port:
                 flow.env.data_port(int(data_port))
             if enable_init_by_host_list():
                 flow.env.machine(node_list())
                 data_port = os.getenv("ONEFLOW_TEST_DATA_PORT")
                 print("initializing worker...")
                 for machine in env_util.default_env_proto.machine:
                     if machine.id == 0:
                         pass
                     else:
                         launch_worker_via_agent(
                             host=machine.addr, env_proto=env_util.default_env_proto
                         )
             else:
                 ctrl_port = os.getenv("ONEFLOW_TEST_CTRL_PORT")
                 config_rank_ctrl_port = -1
                 if ctrl_port:
                     config_rank_ctrl_port = int(ctrl_port)
                 if has_world_size():
                     config_world_size = world_size()
                 else:
                     config_world_size = 0
                 config_node_size = -1
                 env_node_size = os.getenv("ONEFLOW_TEST_NODE_SIZE")
                 if env_node_size:
                     config_node_size = int(env_node_size)
                 bootstrap_conf_list = flow.env.init_bootstrap_confs(
                     node_list(),
                     int(master_port),
                     config_world_size,
                     config_rank_ctrl_port,
                     config_node_size,
                 )
                 worker_env_proto = EnvProto()
                 worker_env_proto.CopyFrom(env_util.default_env_proto)
                 worker_env_proto.ClearField("ctrl_bootstrap_conf")
                 for bootstrap_conf in bootstrap_conf_list:
                     if bootstrap_conf.rank == 0:
                         continue
                     assert bootstrap_conf.HasField("host")
                     worker_env_proto.ctrl_bootstrap_conf.CopyFrom(bootstrap_conf)
                     launch_worker_via_agent(
                         host=bootstrap_conf.host, env_proto=worker_env_proto
                     )
             _unittest_worker_initilized = True
     elif device_num() > 1 and enable_multi_process():
         master_port = find_free_port()
         flow.env.ctrl_port(master_port)
         config_world_size = device_num()
         bootstrap_conf_list = flow.env.init_bootstrap_confs(
             ["127.0.0.1"], master_port, config_world_size
         )
         env_proto = env_util.default_env_proto
         assert (
             len(env_proto.machine) == 1
             and env_proto.HasField("ctrl_bootstrap_conf") == 1
         )
         run_dir = os.getenv("HOME") + "/oneflow_temp/" + str(uuid.uuid1())
         run_dir = os.path.abspath(os.path.expanduser(run_dir))
         if not os.path.exists(run_dir):
             os.makedirs(run_dir)
         for rank in range(1, config_world_size):
             worker_env_proto = EnvProto()
             worker_env_proto.CopyFrom(env_proto)
             worker_env_proto.ctrl_bootstrap_conf.rank = rank
             worker_env_proto.cpp_logging_conf.log_dir = (
                 run_dir + "/log_" + str(rank)
             )
             env_file = NamedTemporaryFile(delete=False)
             if sys.version_info >= (3, 0):
                 env_file.write(pbtxt.MessageToString(worker_env_proto).encode())
             else:
                 env_file.write(pbtxt.MessageToString(worker_env_proto))
             env_file.close()
             if not os.path.exists(run_dir + "/log_" + str(rank)):
                 os.mkdir(run_dir + "/log_" + str(rank))
             os.system(
                 "cp "
                 + env_file.name
                 + " "
                 + run_dir
                 + "/log_"
                 + str(rank)
                 + "/env_proto_"
                 + str(rank)
                 + ".proto"
             )
             oneflow_cmd = (
                 "python3 -m oneflow.compatible.single_client --start_worker"
                 + " --env_proto="
                 + run_dir
                 + "/log_"
                 + str(rank)
                 + "/"
                 + "env_proto_"
                 + str(rank)
                 + ".proto"
             )
             subprocess.Popen(
                 oneflow_cmd,
                 stdout=subprocess.DEVNULL,
                 stderr=subprocess.DEVNULL,
                 shell=True,
             )
             os.remove(env_file.name)
         atexit.register(
             flow.deprecated.delete_worker_of_multi_process, run_dir=run_dir
         )
     log_dir = os.getenv("ONEFLOW_TEST_LOG_DIR")
     if log_dir:
         flow.env.log_dir(log_dir)
     if _unittest_env_initilized == False:
         flow.env.init()
         _unittest_env_initilized = True
     flow.clear_default_session()
     flow.enable_eager_execution(eager_execution_enabled())
     flow.experimental.enable_typing_check(typing_check_enabled())
Esempio n. 11
0
 def setUp(self):
     super().setUp()
     flow.enable_eager_execution(True)