Пример #1
0
def main():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawDescriptionHelpFormatter,
        description="""Convert a TensorFlow Python file from 1.x to 2.0

Simple usage:
  tf_upgrade_v2.py --infile foo.py --outfile bar.py
  tf_upgrade_v2.py --infile foo.ipynb --outfile bar.ipynb
  tf_upgrade_v2.py --intree ~/code/old --outtree ~/code/new
""")
    parser.add_argument(
        "--infile",
        dest="input_file",
        help="If converting a single file, the name of the file "
        "to convert")
    parser.add_argument(
        "--outfile",
        dest="output_file",
        help="If converting a single file, the output filename.")
    parser.add_argument(
        "--intree",
        dest="input_tree",
        help="If converting a whole tree of files, the directory "
        "to read from (relative or absolute).")
    parser.add_argument("--outtree",
                        dest="output_tree",
                        help="If converting a whole tree of files, the output "
                        "directory (relative or absolute).")
    parser.add_argument(
        "--copyotherfiles",
        dest="copy_other_files",
        help=("If converting a whole tree of files, whether to "
              "copy the other files."),
        type=bool,
        default=True)
    parser.add_argument("--inplace",
                        dest="in_place",
                        help=("If converting a set of files, whether to "
                              "allow the conversion to be performed on the "
                              "input files."),
                        action="store_true")
    parser.add_argument(
        "--import_rename",
        dest="import_rename",
        help=("Whether to rename import to compact.v2 explicitly."),
        action="store_true")
    parser.add_argument("--reportfile",
                        dest="report_filename",
                        help=("The name of the file where the report log is "
                              "stored."
                              "(default: %(default)s)"),
                        default="report.txt")
    parser.add_argument(
        "--mode",
        dest="mode",
        choices=[_DEFAULT_MODE, _SAFETY_MODE],
        help=("Upgrade script mode. Supported modes:\n"
              "%s: Perform only straightforward conversions to upgrade to "
              "2.0. In more difficult cases, switch to use compat.v1.\n"
              "%s: Keep 1.* code intact and import compat.v1 "
              "module." % (_DEFAULT_MODE, _SAFETY_MODE)),
        default=_DEFAULT_MODE)
    parser.add_argument(
        "--print_all",
        dest="print_all",
        help="Print full log to stdout instead of just printing errors",
        action="store_true")
    args = parser.parse_args()

    if args.mode == _SAFETY_MODE:
        change_spec = tf_upgrade_v2_safety.TFAPIChangeSpec()
    else:
        change_spec = tf_upgrade_v2.TFAPIChangeSpec(args.import_rename)
    upgrade = ast_edits.ASTCodeUpgrader(change_spec)

    report_text = None
    report_filename = args.report_filename
    files_processed = 0
    if args.input_file:
        if not args.in_place and not args.output_file:
            raise ValueError(
                "--outfile=<output file> argument is required when converting a "
                "single file.")
        if args.in_place and args.output_file:
            raise ValueError(
                "--outfile argument is invalid when when converting in place")
        output_file = args.input_file if args.in_place else args.output_file
        files_processed, report_text, errors = process_file(
            args.input_file, output_file, upgrade)
        errors = {args.input_file: errors}
        files_processed = 1
    elif args.input_tree:
        if not args.in_place and not args.output_tree:
            raise ValueError(
                "--outtree=<output directory> argument is required when converting a "
                "file tree.")
        if args.in_place and args.output_tree:
            raise ValueError(
                "--outtree argument is invalid when when converting in place")
        output_tree = args.input_tree if args.in_place else args.output_tree
        files_processed, report_text, errors = upgrade.process_tree(
            args.input_tree, output_tree, args.copy_other_files)
    else:
        parser.print_help()
    if report_text:
        num_errors = 0
        report = []
        for f in errors:
            if errors[f]:
                num_errors += len(errors[f])
                report.append(six.ensure_str("-" * 80) + "\n")
                report.append("File: %s\n" % f)
                report.append(six.ensure_str("-" * 80) + "\n")
                report.append("\n".join(errors[f]) + "\n")

        report = ("TensorFlow 2.0 Upgrade Script\n"
                  "-----------------------------\n"
                  "Converted %d files\n" % files_processed +
                  "Detected %d issues that require attention" % num_errors +
                  "\n" + six.ensure_str("-" * 80) + "\n") + "".join(report)
        detailed_report_header = six.ensure_str("=" * 80) + "\n"
        detailed_report_header += "Detailed log follows:\n\n"
        detailed_report_header += six.ensure_str("=" * 80) + "\n"

        with open(report_filename, "w") as report_file:
            report_file.write(report)
            report_file.write(detailed_report_header)
            report_file.write(six.ensure_str(report_text))

        if args.print_all:
            print(report)
            print(detailed_report_header)
            print(report_text)
        else:
            print(report)
        print("\nMake sure to read the detailed log %r\n" % report_filename)
Пример #2
0
def main():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawDescriptionHelpFormatter,
        description="""Convert a TensorFlow Python file to 2.0

Simple usage:
  tf_upgrade_v2.py --infile foo.py --outfile bar.py
  tf_upgrade_v2.py --intree ~/code/old --outtree ~/code/new
""")
    parser.add_argument(
        "--infile",
        dest="input_file",
        help="If converting a single file, the name of the file "
        "to convert")
    parser.add_argument(
        "--outfile",
        dest="output_file",
        help="If converting a single file, the output filename.")
    parser.add_argument(
        "--intree",
        dest="input_tree",
        help="If converting a whole tree of files, the directory "
        "to read from (relative or absolute).")
    parser.add_argument("--outtree",
                        dest="output_tree",
                        help="If converting a whole tree of files, the output "
                        "directory (relative or absolute).")
    parser.add_argument(
        "--copyotherfiles",
        dest="copy_other_files",
        help=("If converting a whole tree of files, whether to "
              "copy the other files."),
        type=bool,
        default=True)
    parser.add_argument(
        "--inplace",
        dest="in_place",
        help=("If converting a whole tree of files, whether to "
              "allow the conversion to be performed on the "
              "files in the input tree."),
        type=bool,
        default=False)
    parser.add_argument("--reportfile",
                        dest="report_filename",
                        help=("The name of the file where the report log is "
                              "stored."
                              "(default: %(default)s)"),
                        default="report.txt")
    args = parser.parse_args()

    upgrade = ast_edits.ASTCodeUpgrader(tf_upgrade_v2.TFAPIChangeSpec())
    report_text = None
    report_filename = args.report_filename
    files_processed = 0
    if args.input_file:
        if not args.output_file:
            raise ValueError(
                "--outfile=<output file> argument is required when converting a "
                "single file.")
        files_processed, report_text, errors = upgrade.process_file(
            args.input_file, args.output_file)
        files_processed = 1
    elif args.input_tree:
        if not args.output_tree:
            raise ValueError(
                "--outtree=<output directory> argument is required when converting a "
                "file tree.")
        files_processed, report_text, errors = upgrade.process_tree(
            args.input_tree, args.output_tree, args.copy_other_files,
            args.in_place)
    else:
        parser.print_help()
    if report_text:
        open(report_filename, "w").write(report_text)
        print("TensorFlow 2.0 Upgrade Script")
        print("-----------------------------")
        print("Converted %d files\n" % files_processed)
        print("Detected %d errors that require attention" % len(errors))
        print("-" * 80)
        print("\n".join(errors))
        print("\nMake sure to read the detailed log %r\n" % report_filename)
Пример #3
0
def main():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawDescriptionHelpFormatter,
        description="""Convert a TensorFlow Python file from 1.x to 2.0

Simple usage:
  tf_upgrade_v2.py --infile foo.py --outfile bar.py
  tf_upgrade_v2.py --infile foo.ipynb --outfile bar.ipynb
  tf_upgrade_v2.py --intree ~/code/old --outtree ~/code/new
""")
    parser.add_argument(
        "--infile",
        dest="input_file",
        help="If converting a single file, the name of the file "
        "to convert")
    parser.add_argument(
        "--outfile",
        dest="output_file",
        help="If converting a single file, the output filename.")
    parser.add_argument(
        "--intree",
        dest="input_tree",
        help="If converting a whole tree of files, the directory "
        "to read from (relative or absolute).")
    parser.add_argument("--outtree",
                        dest="output_tree",
                        help="If converting a whole tree of files, the output "
                        "directory (relative or absolute).")
    parser.add_argument(
        "--copyotherfiles",
        dest="copy_other_files",
        help=("If converting a whole tree of files, whether to "
              "copy the other files."),
        type=bool,
        default=True)
    parser.add_argument("--inplace",
                        dest="in_place",
                        help=("If converting a set of files, whether to "
                              "allow the conversion to be performed on the "
                              "input files."),
                        action="store_true")
    parser.add_argument("--reportfile",
                        dest="report_filename",
                        help=("The name of the file where the report log is "
                              "stored."
                              "(default: %(default)s)"),
                        default="report.txt")
    args = parser.parse_args()

    upgrade = ast_edits.ASTCodeUpgrader(tf_upgrade_v2.TFAPIChangeSpec())
    report_text = None
    report_filename = args.report_filename
    files_processed = 0
    if args.input_file:
        if not args.in_place and not args.output_file:
            raise ValueError(
                "--outfile=<output file> argument is required when converting a "
                "single file.")
        if args.in_place and args.output_file:
            raise ValueError(
                "--outfile argument is invalid when when converting in place")
        output_file = args.input_file if args.in_place else args.output_file
        files_processed, report_text, errors = process_file(
            args.input_file, output_file, upgrade)
        errors = {args.input_file: errors}
        files_processed = 1
    elif args.input_tree:
        if not args.in_place and not args.output_tree:
            raise ValueError(
                "--outtree=<output directory> argument is required when converting a "
                "file tree.")
        if args.in_place and args.output_tree:
            raise ValueError(
                "--outtree argument is invalid when when converting in place")
        output_tree = args.input_tree if args.in_place else args.output_tree
        files_processed, report_text, errors = upgrade.process_tree(
            args.input_tree, output_tree, args.copy_other_files)
    else:
        parser.print_help()
    if report_text:
        num_errors = 0
        report = []
        for f in errors:
            if errors[f]:
                num_errors += len(errors[f])
                report.append("-" * 80 + "\n")
                report.append("File: %s\n" % f)
                report.append("-" * 80 + "\n")
                report.append("\n".join(errors[f]) + "\n")

        report = ("TensorFlow 2.0 Upgrade Script\n"
                  "-----------------------------\n"
                  "Converted %d files\n" % files_processed +
                  "Detected %d issues that require attention" % num_errors +
                  "\n" + "-" * 80 + "\n") + "".join(report)
        with open(report_filename, "w") as report_file:
            report_file.write(report)
            report_file.write("=" * 80 + "\n")
            report_file.write("Detailed log follows:\n\n")
            report_file.write("=" * 80 + "\n")
            report_file.write(report_text)

        print(report)
        print("\nMake sure to read the detailed log %r\n" % report_filename)
Пример #4
0
    def testV2KeywordArgNames(self):
        # This test converts a call of the form:
        # tf.foo(arg1=0, arg2=1, ...)
        # to 2.0. Then, checks that converted function has valid argument names.
        if not hasattr(tf.compat, "v2"):
            return
        v2_arg_exceptions = {
            "verify_shape_is_now_always_true",
            # These arguments should not be used, they just specify
            # that a function takes named arguments.
            "keyword_required",
            "_sentinel",
        }
        v1_name_exceptions = {
            "tf.print",  # requires print_function import
        }
        function_warnings = (tf_upgrade_v2.TFAPIChangeSpec().function_warnings)
        function_handles = (tf_upgrade_v2.TFAPIChangeSpec().function_handle)
        keyword_renames = (
            tf_upgrade_v2.TFAPIChangeSpec().function_keyword_renames)

        # Visitor that converts to V2 and checks V2 argument names.
        def conversion_visitor(unused_path, unused_parent, children):
            for child in children:
                _, attr = tf_decorator.unwrap(child[1])
                if not tf_inspect.isfunction(attr):
                    continue
                names_v1 = tf_export.get_v1_names(attr)
                arg_names_v1 = get_args(attr)

                for name in names_v1:
                    tf_name = "tf.%s" % name
                    if tf_name in function_warnings or tf_name in function_handles:
                        continue  # These require manual change
                    if tf_name in v1_name_exceptions:
                        continue
                    # Assert that arg names after converting to v2 are present in
                    # v2 function.
                    # 1. First, create an input of the form:
                    #    tf.foo(arg1=val1, arg2=val2, ...)
                    args = ",".join([
                        "%s=%d" % (from_name, from_index)
                        for from_index, from_name in enumerate(arg_names_v1)
                    ])
                    text_input = "%s(%s)" % (tf_name, args)
                    # 2. Convert the input to V2.
                    _, _, _, text = self._upgrade(text_input)
                    new_function_name, new_args = get_func_and_args_from_str(
                        text)
                    if new_function_name == "tf.compat.v1.%s" % name:
                        if tf_name in keyword_renames:
                            # If we rename arguments, new function must be available in 2.0.
                            # We should not be using compat.v1 in this case.
                            self.assertFalse(
                                "Function '%s' is not in 2.0 when converting\n%s\nto\n%s"
                                % (new_function_name, text_input, text))
                        continue
                    # 3. Verify V2 function and arguments.
                    args_v2 = get_args(self.v2_symbols[new_function_name])
                    args_v2.extend(v2_arg_exceptions)
                    for new_arg in new_args:
                        self.assertIn(
                            new_arg, args_v2,
                            "Invalid argument '%s' in 2.0 when converting\n%s\nto\n%s.\n"
                            "Supported arguments: %s" %
                            (new_arg, text_input, text, str(args_v2)))

        visitor = public_api.PublicAPIVisitor(conversion_visitor)
        visitor.do_not_descend_map["tf"].append("contrib")
        visitor.private_map["tf.compat"] = ["v1", "v2"]
        traverse.traverse(tf.compat.v1, visitor)
Пример #5
0
    parser.add_argument(
        "--copyotherfiles",
        dest="copy_other_files",
        help=("If converting a whole tree of files, whether to "
              "copy the other files."),
        type=bool,
        default=False)
    parser.add_argument("--reportfile",
                        dest="report_filename",
                        help=("The name of the file where the report log is "
                              "stored."
                              "(default: %(default)s)"),
                        default="report.txt")
    args = parser.parse_args()

    upgrade = ast_edits.ASTCodeUpgrader(tf_upgrade_v2.TFAPIChangeSpec())
    report_text = None
    report_filename = args.report_filename
    files_processed = 0
    if args.input_file:
        if not args.output_file:
            raise ValueError(
                "--outfile=<output file> argument is required when converting a "
                "single file.")
        files_processed, report_text, errors = upgrade.process_file(
            args.input_file, args.output_file)
        files_processed = 1
    elif args.input_tree:
        if not args.output_tree:
            raise ValueError(
                "--outtree=<output directory> argument is required when converting a "