integration.py 2.17 KB
Newer Older
Hongkun Yu's avatar
Hongkun Yu committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Copyright 2021 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

Hongkun Yu's avatar
Hongkun Yu committed
15
"""Helper code to run complete models from within python."""
16
17
18
19

import os
import shutil
import sys
20
import tempfile
21

22
from absl import flags
23
from absl.testing import flagsaver
24
25
26

from official.utils.flags import core as flags_core

27

28
@flagsaver.flagsaver
Hongkun Yu's avatar
Hongkun Yu committed
29
30
31
32
33
def run_synthetic(main,
                  tmp_root,
                  extra_flags=None,
                  synth=True,
                  train_epochs=1,
34
                  epochs_between_evals=1):
35
36
37
38
39
40
  """Performs a minimal run of a model.

    This function is intended to test for syntax errors throughout a model. A
  very limited run is performed using synthetic data.

  Args:
Karmel Allison's avatar
Karmel Allison committed
41
    main: The primary function used to exercise a code path. Generally this
42
      function is "<MODULE>.main(argv)".
43
    tmp_root: Root path for the temp directory created by the test class.
Karmel Allison's avatar
Karmel Allison committed
44
    extra_flags: Additional flags passed by the caller of this function.
45
    synth: Use synthetic data.
46
47
    train_epochs: Value of the --train_epochs flag.
    epochs_between_evals: Value of the --epochs_between_evals flag.
48
49
50
51
  """

  extra_flags = [] if extra_flags is None else extra_flags

52
  model_dir = tempfile.mkdtemp(dir=tmp_root)
53

54
  args = [sys.argv[0], "--model_dir", model_dir] + extra_flags
55
56
57
58

  if synth:
    args.append("--use_synthetic_data")

59
60
61
62
63
64
  if train_epochs is not None:
    args.extend(["--train_epochs", str(train_epochs)])

  if epochs_between_evals is not None:
    args.extend(["--epochs_between_evals", str(epochs_between_evals)])

65
  try:
66
67
    flags_core.parse_flags(argv=args)
    main(flags.FLAGS)
68
69
70
  finally:
    if os.path.exists(model_dir):
      shutil.rmtree(model_dir)