Exemplo n.º 1
0
 def test_train_delay(self):
     for est in self._estimators_for_tests():
         ex = Experiment(est, train_input_fn='train_input', eval_input_fn='eval_input')
         for delay in [0, 1, 3]:
             sheep = SheepCounter()
             with test.mock.patch.object(time, 'time', sheep.time):
                 with test.mock.patch.object(time, 'sleep', sheep.sleep):
                     ex.train(delay_secs=delay)
                     self.assertAlmostEqual(delay, sheep.time(), delta=1e-4)
Exemplo n.º 2
0
    def test_train_server_does_not_start_without_cluster_spec(
            self, mock_server):
        config = run_config_lib.RunConfig(master='host4:2222')
        for est in self._estimators_for_tests(config):
            ex = Experiment(est,
                            train_input_fn='train_input',
                            eval_input_fn='eval_input')
            ex.train()

            # The server should not have started because there was no ClusterSpec.
            self.assertFalse(mock_server.called)
Exemplo n.º 3
0
 def test_train_delay(self):
     for est in self._estimators_for_tests():
         ex = Experiment(est,
                         train_input_fn='train_input',
                         eval_input_fn='eval_input')
         for delay in [0, 1, 3]:
             sheep = SheepCounter()
             with test.mock.patch.object(time, 'time', sheep.time):
                 with test.mock.patch.object(time, 'sleep', sheep.sleep):
                     ex.train(delay_secs=delay)
                     self.assertAlmostEqual(delay, sheep.time(), delta=1e-4)
Exemplo n.º 4
0
    def test_train_server_does_not_start_without_cluster_spec(self, mock_server):
        config = run_config_lib.RunConfig(master='host4:2222')
        for est in self._estimators_for_tests(config):
            ex = Experiment(
                est,
                train_input_fn='train_input',
                eval_input_fn='eval_input')
            ex.train()

            # The server should not have started because there was no ClusterSpec.
            self.assertFalse(mock_server.called)
Exemplo n.º 5
0
 def test_train_server_does_not_start_with_empty_master(self, mock_server):
     tf_config = {'cluster': self._cluster_spec()}
     with test.mock.patch.dict('os.environ',
                               {'TF_CONFIG': json.dumps(tf_config)}):
         config = run_config_lib.RunConfig(master='')
     for est in self._estimators_for_tests(config):
         ex = Experiment(est,
                         train_input_fn='train_input',
                         eval_input_fn='eval_input')
         ex.train()
         # The server should not have started because master was the empty string.
         self.assertFalse(mock_server.called)
Exemplo n.º 6
0
 def test_train_server_does_not_start_with_empty_master(self, mock_server):
     tf_config = {'cluster': self._cluster_spec()}
     with test.mock.patch.dict('os.environ',
                               {'TF_CONFIG': json.dumps(tf_config)}):
         config = run_config_lib.RunConfig(master='')
     for est in self._estimators_for_tests(config):
         ex = Experiment(
             est,
             train_input_fn='train_input',
             eval_input_fn='eval_input')
         ex.train()
         # The server should not have started because master was the empty string.
         self.assertFalse(mock_server.called)
Exemplo n.º 7
0
    def test_train_default_delay(self):
        for task_id in [0, 1, 3]:
            tf_config = {'task': {'index': task_id}}
            with test.mock.patch.dict('os.environ',
                                      {'TF_CONFIG': json.dumps(tf_config)}):
                config = RunConfig()
            for est in self._estimators_for_tests(config):
                ex = Experiment(
                    est, train_input_fn='train_input', eval_input_fn='eval_input')

                sheep = SheepCounter()
                with test.mock.patch.object(time, 'time', sheep.time):
                    with test.mock.patch.object(time, 'sleep', sheep.sleep):
                        ex.train()
                        self.assertAlmostEqual(task_id * 5, sheep.time(), delta=1e-4)
Exemplo n.º 8
0
 def test_train(self):
     for est in self._estimators_for_tests():
         ex = Experiment(est, train_input_fn='train_input', train_steps='train_steps',
                         eval_input_fn='eval_input')
         fit_args = ex.train(delay_secs=0)
         self.assertEqual(1, est.fit_count)
         self.assertIn(('max_steps', 'train_steps'), fit_args)
         self.assertEqual(0, est.eval_count)
Exemplo n.º 9
0
    def test_train_default_delay(self):
        for task_id in [0, 1, 3]:
            tf_config = {'task': {'index': task_id}}
            with test.mock.patch.dict('os.environ',
                                      {'TF_CONFIG': json.dumps(tf_config)}):
                config = RunConfig()
            for est in self._estimators_for_tests(config):
                ex = Experiment(est,
                                train_input_fn='train_input',
                                eval_input_fn='eval_input')

                sheep = SheepCounter()
                with test.mock.patch.object(time, 'time', sheep.time):
                    with test.mock.patch.object(time, 'sleep', sheep.sleep):
                        ex.train()
                        self.assertAlmostEqual(task_id * 5,
                                               sheep.time(),
                                               delta=1e-4)
Exemplo n.º 10
0
 def test_train_raises_if_job_name_is_missing(self):
     tf_config = {
         'cluster': self._cluster_spec(),
         'environment': run_config_lib.Environment.CLOUD,
         'task': {
             'index': 1
         }
     }
     with test.mock.patch.dict('os.environ',
                               {'TF_CONFIG': json.dumps(tf_config)
                                }), self.assertRaises(ValueError):
         config = run_config_lib.RunConfig(
             master='host3:2222'  # Normally selected by task type.
         )
         for est in self._estimators_for_tests(config):
             ex = Experiment(est,
                             train_input_fn='train_input',
                             eval_input_fn='eval_input')
             ex.train()
Exemplo n.º 11
0
    def test_train_starts_server(self, mock_server):
        # Arrange.
        tf_config = {
            'cluster': self._cluster_spec(),
            'environment': run_config_lib.Environment.CLOUD,
            'task': {
                'type': run_config_lib.TaskType.WORKER,
                'index': 1
            }
        }
        with test.mock.patch.dict('os.environ',
                                  {'TF_CONFIG': json.dumps(tf_config)}):
            config = run_config_lib.RunConfig(master='host4:2222',
                                              num_cores=15,
                                              gpu_memory_fraction=0.314)

        for est in self._estimators_for_tests(config):
            ex = Experiment(est,
                            train_input_fn='train_input',
                            eval_input_fn='eval_input')

            # Act.
            # We want to make sure we discount the time it takes to start the server
            # in our accounting of the delay, so we set a small delay here.
            sheep = SheepCounter()
            with test.mock.patch.object(time, 'time', sheep.time):
                with test.mock.patch.object(time, 'sleep', sheep.sleep):
                    ex.train(delay_secs=1)
                    # Ensure that the delay takes into account the time to start server.
                    self.assertAlmostEqual(1, sheep.time(), delta=1e-4)

            # Assert.
            expected_config_proto = config_pb2.ConfigProto()
            expected_config_proto.inter_op_parallelism_threads = 15
            expected_config_proto.intra_op_parallelism_threads = 15
            expected_config_proto.gpu_options.per_process_gpu_memory_fraction = 0.314
            mock_server.assert_called_with(
                config.cluster_spec,
                job_name=run_config_lib.TaskType.WORKER,
                task_index=1,
                config=expected_config_proto,
                start=False)
            mock_server.assert_has_calls([test.mock.call().start()])
Exemplo n.º 12
0
 def test_train(self):
     for est in self._estimators_for_tests():
         ex = Experiment(est,
                         train_input_fn='train_input',
                         train_steps='train_steps',
                         eval_input_fn='eval_input')
         fit_args = ex.train(delay_secs=0)
         self.assertEqual(1, est.fit_count)
         self.assertIn(('max_steps', 'train_steps'), fit_args)
         self.assertEqual(0, est.eval_count)
Exemplo n.º 13
0
 def test_train_raises_if_job_name_is_missing(self):
     tf_config = {
         'cluster': self._cluster_spec(),
         'environment': run_config_lib.Environment.CLOUD,
         'task': {
             'index': 1
         }
     }
     with test.mock.patch.dict(
         'os.environ',
         {'TF_CONFIG': json.dumps(tf_config)}), self.assertRaises(ValueError):
         config = run_config_lib.RunConfig(
             master='host3:2222'  # Normally selected by task type.
         )
         for est in self._estimators_for_tests(config):
             ex = Experiment(
                 est,
                 train_input_fn='train_input',
                 eval_input_fn='eval_input')
             ex.train()
Exemplo n.º 14
0
    def test_train_starts_server(self, mock_server):
        # Arrange.
        tf_config = {
            'cluster': self._cluster_spec(),
            'environment': run_config_lib.Environment.CLOUD,
            'task': {
                'type': run_config_lib.TaskType.WORKER,
                'index': 1
            }
        }
        with test.mock.patch.dict('os.environ', {'TF_CONFIG': json.dumps(tf_config)}):
            config = run_config_lib.RunConfig(
                master='host4:2222', num_cores=15, gpu_memory_fraction=0.314)

        for est in self._estimators_for_tests(config):
            ex = Experiment(est, train_input_fn='train_input', eval_input_fn='eval_input')

            # Act.
            # We want to make sure we discount the time it takes to start the server
            # in our accounting of the delay, so we set a small delay here.
            sheep = SheepCounter()
            with test.mock.patch.object(time, 'time', sheep.time):
                with test.mock.patch.object(time, 'sleep', sheep.sleep):
                    ex.train(delay_secs=1)
                    # Ensure that the delay takes into account the time to start server.
                    self.assertAlmostEqual(1, sheep.time(), delta=1e-4)

            # Assert.
            expected_config_proto = config_pb2.ConfigProto()
            expected_config_proto.inter_op_parallelism_threads = 15
            expected_config_proto.intra_op_parallelism_threads = 15
            expected_config_proto.gpu_options.per_process_gpu_memory_fraction = 0.314
            mock_server.assert_called_with(
                config.cluster_spec,
                job_name=run_config_lib.TaskType.WORKER,
                task_index=1,
                config=expected_config_proto,
                start=False)
            mock_server.assert_has_calls([test.mock.call().start()])