Ejemplo n.º 1
0
def disable_v2_behavior():
  """Disables TensorFlow 2.x behaviors.

  This function can be called at the beginning of the program (before `Tensors`,
  `Graphs` or other structures have been created, and before devices have been
  initialized. It switches all global behaviors that are different between
  TensorFlow 1.x and 2.x to behave as intended for 1.x.

  User can call this function to disable 2.x behavior during complex migrations.
  """
  tf2.disable()
  ops.disable_eager_execution()
  tensor_shape.disable_v2_tensorshape()  # Also switched by tf2
  variable_scope.disable_resource_variables()
  ops.disable_tensor_equality()
  # Disables TensorArrayV2 and control flow V2.
  control_flow_v2_toggles.disable_control_flow_v2()
  # Make sure internal uses of tf.data symbols map to V1 versions.
  dataset_ops.Dataset = dataset_ops.DatasetV1
  readers.FixedLengthRecordDataset = readers.FixedLengthRecordDatasetV1
  readers.TFRecordDataset = readers.TFRecordDatasetV1
  readers.TextLineDataset = readers.TextLineDatasetV1
  counter.Counter = counter.CounterV1
  interleave_ops.choose_from_datasets = interleave_ops.choose_from_datasets_v1
  interleave_ops.sample_from_datasets = interleave_ops.sample_from_datasets_v1
  random_ops.RandomDataset = random_ops.RandomDatasetV1
  exp_readers.CsvDataset = exp_readers.CsvDatasetV1
  exp_readers.SqlDataset = exp_readers.SqlDatasetV1
  exp_readers.make_batched_features_dataset = (
      exp_readers.make_batched_features_dataset_v1)
  exp_readers.make_csv_dataset = exp_readers.make_csv_dataset_v1
Ejemplo n.º 2
0
def disable_v2_behavior():
    """Disables TensorFlow 2.x behaviors.

    This function can be called at the beginning of the program
    (before `Tensors`, `Graphs` or other structures have been created,
    and before devices have been initialized. It switches all global
    behaviors that are different between TensorFlow 1.x and 2.x to
    behave as intended for 1.x.

    User can call this function to disable 2.x behavior during complex
    migrations.
    """

    # _v2_behavior_usage_gauge.get_cell("disable").set(True)
    tf2.disable()
    ops.disable_eager_execution()
    tensor_shape.disable_v2_tensorshape()  # Also switched by tf2

    # FIXME[bug]:
    #   Warning: disable_resource_variables (from
    #   tensorflow.python.ops.variable_scope) is deprecated and will be
    #   removed in a future version.
    #   Instructions for updating:
    #     non-resource variables are not supported in the long term
    #
    # The function tf.compat.v1.disable_resource_variables() is
    # depreciated instead you can mention use_resource=False in
    # tf.get_variable() which will be forced to true when eager excecution
    # is enabled by default in Tensorflow 2.x.
    # variable_scope.disable_resource_variables()

    ops.disable_tensor_equality()
    # Disables TensorArrayV2 and control flow V2.
    control_flow_v2_toggles.disable_control_flow_v2()
    # Make sure internal uses of tf.data symbols map to V1 versions.
    dataset_ops.Dataset = dataset_ops.DatasetV1
    readers.FixedLengthRecordDataset = readers.FixedLengthRecordDatasetV1
    readers.TFRecordDataset = readers.TFRecordDatasetV1
    readers.TextLineDataset = readers.TextLineDatasetV1
    counter.Counter = counter.CounterV1
    interleave_ops.choose_from_datasets = \
        interleave_ops.choose_from_datasets_v1
    interleave_ops.sample_from_datasets = \
        interleave_ops.sample_from_datasets_v1
    random_ops.RandomDataset = random_ops.RandomDatasetV1
    exp_readers.CsvDataset = exp_readers.CsvDatasetV1
    exp_readers.SqlDataset = exp_readers.SqlDatasetV1
    exp_readers.make_batched_features_dataset = (
        exp_readers.make_batched_features_dataset_v1)
    exp_readers.make_csv_dataset = exp_readers.make_csv_dataset_v1
Ejemplo n.º 3
0
 def testConvertCase(self):
   """Tests that a v1 case() construction converts properly."""
   with ops.Graph().as_default():
     with variable_scope.variable_scope("", use_resource=False):
       control_flow_v2_toggles.disable_control_flow_v2()
       x = variable_scope.get_variable("x", initializer=1.0)
       y = variable_scope.get_variable("y", initializer=2.0)
       _ = control_flow_ops.case([(gen_math_ops.less(x, y), lambda: x)],
                                 default=lambda: y)
     with session_lib.Session() as sess:
       sess.run(variables.global_variables_initializer())
       variable_graph_def = sess.graph.as_graph_def()
       constant_graph_def = (
           convert_to_constants
           .convert_variables_to_constants_from_session_graph(
               sess, variable_graph_def, ["case/cond/Merge"]))
       self._assertGraphContains(
           constant_graph_def, """
           node {
             name: "x" op: "Const"
             attr { key: "dtype" value { type: DT_FLOAT } }
             attr {
               key: "value"
               value { tensor { dtype: DT_FLOAT tensor_shape{} float_val: 1 }}}
           }
           node {
             name: "y" op: "Const"
             attr { key: "dtype" value { type: DT_FLOAT } }
             attr {
               key: "value"
               value { tensor { dtype: DT_FLOAT tensor_shape{} float_val: 2 }}}
           }
           node {name: "x/read" op: "Identity" input: "x"}
           node {name: "y/read" op: "Identity" input: "y"}
           node {name: "Less" op: "Less" input: "x/read" input: "y/read"}
           node {name: "case/cond/pred_id" op: "Identity" input: "Less"}
           node {
             name: "case/cond/Switch_1" op: "Switch"
             input: "case/cond/pred_id" input: "x/read"
           }
           node {
             name: "case/cond/Switch_2" op: "Switch"
             input: "case/cond/pred_id" input: "y/read"
           }
           node {
             name: "case/cond/Merge" op: "Merge"
             input: "case/cond/Switch_2" input: "case/cond/Switch_1:1"
             attr {key: "T" value {type: DT_FLOAT}}
           }""")
Ejemplo n.º 4
0
def disable_v2_behavior():
    """Disables TensorFlow 2.x behaviors.

  This function can be called at the beginning of the program (before `Tensors`,
  `Graphs` or other structures have been created, and before devices have been
  initialized. It switches all global behaviors that are different between
  TensorFlow 1.x and 2.x to behave as intended for 1.x.

  User can call this function to disable 2.x behavior during complex migrations.
  """
    tf2.disable()
    ops.disable_eager_execution()
    tensor_shape.disable_v2_tensorshape()  # Also switched by tf2
    variable_scope.disable_resource_variables()
    # Disables TensorArrayV2 and control flow V2.
    control_flow_v2_toggles.disable_control_flow_v2()
Ejemplo n.º 5
0
 def testConvertV2ResourceCase(self):
   """Tests that a v2 case() with resource variables converts properly."""
   with ops.Graph().as_default():
     with variable_scope.variable_scope("", use_resource=True):
       control_flow_v2_toggles.enable_control_flow_v2()
       x = variable_scope.get_variable("x", initializer=1.0)
       y = variable_scope.get_variable("y", initializer=2.0)
       _ = control_flow_ops.case([(gen_math_ops.less(x, y), lambda: x)],
                                 default=lambda: y)
       control_flow_v2_toggles.disable_control_flow_v2()
     with session_lib.Session() as sess:
       sess.run(variables.global_variables_initializer())
       variable_graph_def = sess.graph.as_graph_def()
       constant_graph_def = (
           convert_to_constants
           .convert_variables_to_constants_from_session_graph(
               sess, variable_graph_def, ["case/cond"]))
       self._assertGraphContains(
           constant_graph_def, """
           node {name: "x" op: "Const"}
           node {name: "y" op: "Const"}
           node {
             name: "case/cond" op: "If" input: "Less" input: "x" input: "y"
             attr {key: "Tcond" value {type: DT_BOOL}}
             attr {key: "Tin" value {list {type: DT_FLOAT type: DT_FLOAT}}}
             attr {key: "Tout" value {list {type: DT_FLOAT}}}
           }
           library {
             function {
               signature {
                 name: "case_cond_false_frozen_0"
                 input_arg {name: "placeholder" type: DT_FLOAT}
                 input_arg {name: "readvariableop_y" type: DT_FLOAT}
                 output_arg {name: "readvariableop" type: DT_FLOAT}
               }
             }
             function {
               signature {
                 name: "case_cond_true_frozen_0"
                 input_arg {name: "placeholder" type: DT_FLOAT}
                 input_arg {name: "readvariableop_x" type: DT_FLOAT}
                 output_arg {name: "readvariableop" type: DT_FLOAT}
               }
             }
           }""")
Ejemplo n.º 6
0
def disable_v2_behavior():
    """Disables TensorFlow 2.x behaviors.

  This function can be called at the beginning of the program (before `Tensors`,
  `Graphs` or other structures have been created, and before devices have been
  initialized. It switches all global behaviors that are different between
  TensorFlow 1.x and 2.x to behave as intended for 1.x.

  User can call this function to disable 2.x behavior during complex migrations.

  @compatibility(TF2)
  Using this function indicates that your software is not compatible
  with eager execution and `tf.function` in TF2.

  To migrate to TF2, rewrite your code to be compatible with eager execution.
  Please refer to the [migration guide]
  (https://www.tensorflow.org/guide/migrate) for additional resource on the
  topic.
  @end_compatibility
  """
    _v2_behavior_usage_gauge.get_cell("disable").set(True)
    tf2.disable()
    ops.disable_eager_execution()
    tensor_shape.disable_v2_tensorshape()  # Also switched by tf2
    variable_scope.disable_resource_variables()
    ops.disable_tensor_equality()
    # Disables TensorArrayV2 and control flow V2.
    control_flow_v2_toggles.disable_control_flow_v2()
    # Make sure internal uses of tf.data symbols map to V1 versions.
    dataset_ops.Dataset = dataset_ops.DatasetV1
    readers.FixedLengthRecordDataset = readers.FixedLengthRecordDatasetV1
    readers.TFRecordDataset = readers.TFRecordDatasetV1
    readers.TextLineDataset = readers.TextLineDatasetV1
    counter.Counter = counter.CounterV1
    interleave_ops.choose_from_datasets = interleave_ops.choose_from_datasets_v1
    interleave_ops.sample_from_datasets = interleave_ops.sample_from_datasets_v1
    random_ops.RandomDataset = random_ops.RandomDatasetV1
    exp_readers.CsvDataset = exp_readers.CsvDatasetV1
    exp_readers.SqlDataset = exp_readers.SqlDatasetV1
    exp_readers.make_batched_features_dataset = (
        exp_readers.make_batched_features_dataset_v1)
    exp_readers.make_csv_dataset = exp_readers.make_csv_dataset_v1
Ejemplo n.º 7
0
  def testV2BehaviorLogging(self):
    with self.assertLogs(level='INFO') as logs:
      try:
        ops.enable_eager_execution()
      # Ignore this exception to test log output successfully
      except ValueError as e:
        if 'must be called at program startup' not in str(e):
          raise e

    self.assertIn('Enabling eager execution', ''.join(logs.output))
    with self.assertLogs(level='INFO') as logs:
      ops.disable_eager_execution()
    self.assertIn('Disabling eager execution', ''.join(logs.output))

    with self.assertLogs(level='INFO') as logs:
      tensor_shape.enable_v2_tensorshape()
    self.assertIn('Enabling v2 tensorshape', ''.join(logs.output))
    with self.assertLogs(level='INFO') as logs:
      tensor_shape.disable_v2_tensorshape()
    self.assertIn('Disabling v2 tensorshape', ''.join(logs.output))

    with self.assertLogs(level='INFO') as logs:
      variable_scope.enable_resource_variables()
    self.assertIn('Enabling resource variables', ''.join(logs.output))
    with self.assertLogs(level='INFO') as logs:
      variable_scope.disable_resource_variables()
    self.assertIn('Disabling resource variables', ''.join(logs.output))

    with self.assertLogs(level='INFO') as logs:
      ops.enable_tensor_equality()
    self.assertIn('Enabling tensor equality', ''.join(logs.output))
    with self.assertLogs(level='INFO') as logs:
      ops.disable_tensor_equality()
    self.assertIn('Disabling tensor equality', ''.join(logs.output))

    with self.assertLogs(level='INFO') as logs:
      control_flow_v2_toggles.enable_control_flow_v2()
    self.assertIn('Enabling control flow v2', ''.join(logs.output))
    with self.assertLogs(level='INFO') as logs:
      control_flow_v2_toggles.disable_control_flow_v2()
    self.assertIn('Disabling control flow v2', ''.join(logs.output))
Ejemplo n.º 8
0
 def tearDown(self):
     if not self._enabled:
         control_flow_v2_toggles.disable_control_flow_v2()
     super(WhileV2Test, self).tearDown()
Ejemplo n.º 9
0
# Enforce abspath
if sys.argv and os.path.exists(sys.argv[0]) and not os.path.isabs(sys.argv[0]):
    logging.error(
        'AutoDist requires the script path "{}" to be an absolute path to be shared across workers. '
        'Now exit.'.format(sys.argv[0]))
    sys.exit(1)

# Runtime compatibility checking
COMPAT_TF_VERSIONS = [1.15, 2.2]
float_major_minor_tf_version = float(
    version.VERSION[:version.VERSION.rfind('.')])
if not COMPAT_TF_VERSIONS[
        0] <= float_major_minor_tf_version <= COMPAT_TF_VERSIONS[1]:
    logging.error(
        'AutoDist is only compatible with `tensorflow-gpu>={}, <={}`, but the current version is {}'
        .format(COMPAT_TF_VERSIONS[0], COMPAT_TF_VERSIONS[1],
                float_major_minor_tf_version))
    sys.exit(1)
logging.debug('AutoDist is now running on TensorFlow {}'.format(
    version.VERSION))

# Disable tensorflow control flow version 2 (which AutoDist does not support as of now).
# Use control flow version 1 instead.
control_flow_v2_toggles.disable_control_flow_v2()
logging.warning(
    'AutoDist has disabled TensorFlow control_flow_v2 in favor of control_flow_v1'
)

PatchTensorFlow.patch_optimizers()
Ejemplo n.º 10
0
  def testConvertV2UnconvertedResourceNestedCase(self):
    """Tests unconverted variable propagation through nested functions."""
    with ops.Graph().as_default():
      with variable_scope.variable_scope("", use_resource=True):
        control_flow_v2_toggles.enable_control_flow_v2()
        x = variable_scope.get_variable("x", initializer=1.0)
        y = variable_scope.get_variable("y", initializer=2.0)
        z = variable_scope.get_variable("z", initializer=3.0)
        # pylint: disable=g-long-lambda
        _ = control_flow_ops.case(
            [(gen_math_ops.less(x, y), lambda: x)],
            default=lambda: control_flow_ops.case(
                [(gen_math_ops.less(z, y), lambda: z)], default=lambda: y))
        # pylint: enable=g-long-lambda
        control_flow_v2_toggles.disable_control_flow_v2()
      with session_lib.Session() as sess:
        sess.run(variables.global_variables_initializer())
        variable_graph_def = sess.graph.as_graph_def()
        constant_graph_def = (
            convert_to_constants
            .convert_variables_to_constants_from_session_graph(
                sess,
                variable_graph_def, ["case/cond"],
                variable_names_denylist=["y"]))
        self._assertGraphContains(
            constant_graph_def, """
            node {name: "x" op: "Const"}
            node {name: "y" op: "VarHandleOp"}
            node {name: "z" op: "Const"}

            node {name: "Less/ReadVariableOp" op: "Identity" input: "x"}
            node {name: "Less/ReadVariableOp_1" op: "ReadVariableOp" input: "y"}

            node {
              name: "case/cond" op: "If"
              input: "x" input: "z" input: "y"
              attr {
                key: "Tin"
                value {list
                  {type: DT_FLOAT type: DT_FLOAT type: DT_RESOURCE}}}
              attr {
                key: "_read_only_resource_inputs"
                value {list {i: 1 i: 2 i: 3}}}
              attr {key: "then_branch"
                    value {func {name: "case_cond_true_frozen_0"}}}
              attr {key: "else_branch"
                    value {func {name: "case_cond_false_frozen_0"}}}
              attr {key: "output_shapes" value {list {shape {}}}}
            }
            library {
              function {
                signature {
                  name: "case_cond_true_frozen_0"
                  input_arg {name: "placeholder" type: DT_FLOAT}
                  input_arg {name: "placeholder_1" type: DT_RESOURCE}
                  input_arg {name: "readvariableop_x" type: DT_FLOAT}
                  output_arg {name: "readvariableop" type: DT_FLOAT}
                  is_stateful: true
                }

                node_def {name: "ReadVariableOp" op: "Identity"
                  input: "readvariableop_x"}}

              function {
                signature {
                  name: "case_cond_false_frozen_0"
                  input_arg {name: "placeholder" type: DT_FLOAT}
                  input_arg {name: "less_readvariableop_1_y" type: DT_RESOURCE}
                  input_arg {name: "less_readvariableop_z" type: DT_FLOAT}
                  output_arg {name: "case_cond_identity" type: DT_FLOAT}
                  is_stateful: true
                }

                node_def {name: "Less/ReadVariableOp_1" op: "ReadVariableOp"
                  input: "less_readvariableop_1_y"}

                node_def {name: "Less/ReadVariableOp" op: "Identity"
                  input: "less_readvariableop_z"}

                node_def {name: "case/cond" op: "If"
                  input: "less_readvariableop_z"
                  input: "less_readvariableop_1_y"
                  attr {
                    key: "Tin"
                    value {list {type: DT_FLOAT type: DT_RESOURCE}}}
                  attr {key: "then_branch"
                        value {func {name: "case_cond_true_frozen_1"}}}
                  attr {key: "else_branch"
                        value {func {name: "case_cond_false_frozen_1"}}}
                  attr {
                    key: "_read_only_resource_inputs"
                    value {list {i: 1 i: 2}}}}}

              function {
                signature {
                  name: "case_cond_false_frozen_1"
                  input_arg {name: "placeholder" type: DT_FLOAT}
                  input_arg {name: "readvariableop_y" type: DT_RESOURCE}
                  output_arg {name: "readvariableop" type: DT_FLOAT}
                  is_stateful: true
                }

                node_def {name: "ReadVariableOp" op: "ReadVariableOp"
                  input: "readvariableop_y"}}

              function {
                signature {
                  name: "case_cond_true_frozen_1"
                  input_arg {name: "placeholder" type: DT_RESOURCE}
                  input_arg {name: "readvariableop_z" type: DT_FLOAT}
                  output_arg {name: "readvariableop" type: DT_FLOAT}
                  is_stateful: true
                }

                node_def {name: "ReadVariableOp" op: "Identity"
                  input: "readvariableop_z"}}}""")