tensorflow/models

View on GitHub
official/nlp/data/create_pretraining_data.py

Summary

Maintainability
F
5 days
Test Coverage

Function create_instances_from_document has a Cognitive Complexity of 56 (exceeds 5 allowed). Consider refactoring.
Open

def create_instances_from_document(
    all_documents, document_index, max_seq_length, short_seq_prob,
    masked_lm_prob, max_predictions_per_seq, vocab_words, rng,
    do_whole_word_mask=False,
    max_ngram_size=None):
Severity: Minor
Found in official/nlp/data/create_pretraining_data.py - About 1 day to fix

Cognitive Complexity

Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

A method's cognitive complexity is based on a few simple rules:

  • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
  • Code is considered more complex for each "break in the linear flow of the code"
  • Code is considered more complex when "flow breaking structures are nested"

Further reading

File create_pretraining_data.py has 503 lines of code (exceeds 250 allowed). Consider refactoring.
Open

# Copyright 2024 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
Severity: Major
Found in official/nlp/data/create_pretraining_data.py - About 1 day to fix

    Function write_instance_to_example_files has a Cognitive Complexity of 24 (exceeds 5 allowed). Consider refactoring.
    Open

    def write_instance_to_example_files(instances, tokenizer, max_seq_length,
                                        max_predictions_per_seq, output_files,
                                        gzip_compress, use_v2_feature_names):
      """Creates TF example files from `TrainingInstance`s."""
      writers = []
    Severity: Minor
    Found in official/nlp/data/create_pretraining_data.py - About 3 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function _masking_ngrams has a Cognitive Complexity of 21 (exceeds 5 allowed). Consider refactoring.
    Open

    def _masking_ngrams(grams, max_ngram_size, max_masked_tokens, rng):
      """Create a list of masking {1, ..., n}-grams from a list of one-grams.
    
      This is an extension of 'whole word masking' to mask multiple, contiguous
      words such as (e.g., "the red boat").
    Severity: Minor
    Found in official/nlp/data/create_pretraining_data.py - About 2 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function create_masked_lm_predictions has a Cognitive Complexity of 16 (exceeds 5 allowed). Consider refactoring.
    Open

    def create_masked_lm_predictions(tokens, masked_lm_prob,
                                     max_predictions_per_seq, vocab_words, rng,
                                     do_whole_word_mask,
                                     max_ngram_size=None):
      """Creates the predictions for the masked LM objective."""
    Severity: Minor
    Found in official/nlp/data/create_pretraining_data.py - About 2 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function create_training_instances has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
    Open

    def create_training_instances(
        input_files,
        tokenizer,
        processor_text_fn,
        max_seq_length,
    Severity: Minor
    Found in official/nlp/data/create_pretraining_data.py - About 1 hr to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function create_training_instances has 11 arguments (exceeds 4 allowed). Consider refactoring.
    Open

    def create_training_instances(
    Severity: Major
    Found in official/nlp/data/create_pretraining_data.py - About 1 hr to fix

      Function create_instances_from_document has 10 arguments (exceeds 4 allowed). Consider refactoring.
      Open

      def create_instances_from_document(
      Severity: Major
      Found in official/nlp/data/create_pretraining_data.py - About 1 hr to fix

        Function _tokens_to_grams has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
        Open

        def _tokens_to_grams(tokens):
          """Reconstitue grams (words) from `tokens`.
        
          E.g.,
             tokens: ['[CLS]', 'That', 'lit', '##tle', 'blue', 'tru', '##ck', '[SEP]']
        Severity: Minor
        Found in official/nlp/data/create_pretraining_data.py - About 1 hr to fix

        Cognitive Complexity

        Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

        A method's cognitive complexity is based on a few simple rules:

        • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
        • Code is considered more complex for each "break in the linear flow of the code"
        • Code is considered more complex when "flow breaking structures are nested"

        Further reading

        Function create_masked_lm_predictions has 7 arguments (exceeds 4 allowed). Consider refactoring.
        Open

        def create_masked_lm_predictions(tokens, masked_lm_prob,
        Severity: Major
        Found in official/nlp/data/create_pretraining_data.py - About 50 mins to fix

          Function write_instance_to_example_files has 7 arguments (exceeds 4 allowed). Consider refactoring.
          Open

          def write_instance_to_example_files(instances, tokenizer, max_seq_length,
          Severity: Major
          Found in official/nlp/data/create_pretraining_data.py - About 50 mins to fix

            Avoid deeply nested control flow statements.
            Open

                      for j in range(random_start, len(random_document)):
                        tokens_b.extend(random_document[j])
                        if len(tokens_b) >= target_b_length:
                          break
                      # We didn't actually use these segments so we "put them back" so
            Severity: Major
            Found in official/nlp/data/create_pretraining_data.py - About 45 mins to fix

              Function truncate_seq_pair has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
              Open

              def truncate_seq_pair(tokens_a, tokens_b, max_num_tokens, rng):
                """Truncates a pair of sequences to a maximum sequence length."""
                while True:
                  total_length = len(tokens_a) + len(tokens_b)
                  if total_length <= max_num_tokens:
              Severity: Minor
              Found in official/nlp/data/create_pretraining_data.py - About 45 mins to fix

              Cognitive Complexity

              Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

              A method's cognitive complexity is based on a few simple rules:

              • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
              • Code is considered more complex for each "break in the linear flow of the code"
              • Code is considered more complex when "flow breaking structures are nested"

              Further reading

              Avoid deeply nested control flow statements.
              Open

                        for _ in range(10):
                          random_document_index = rng.randint(0, len(all_documents) - 1)
                          if random_document_index != document_index:
                            break
              
              
              Severity: Major
              Found in official/nlp/data/create_pretraining_data.py - About 45 mins to fix

                Avoid deeply nested control flow statements.
                Open

                          for j in range(a_end, len(current_chunk)):
                            tokens_b.extend(current_chunk[j])
                        truncate_seq_pair(tokens_a, tokens_b, max_num_tokens, rng)
                Severity: Major
                Found in official/nlp/data/create_pretraining_data.py - About 45 mins to fix

                  Function __init__ has 5 arguments (exceeds 4 allowed). Consider refactoring.
                  Open

                    def __init__(self, tokens, segment_ids, masked_lm_positions, masked_lm_labels,
                  Severity: Minor
                  Found in official/nlp/data/create_pretraining_data.py - About 35 mins to fix

                    Similar blocks of code found in 2 locations. Consider refactoring.
                    Open

                        while len(masked_lm_positions) < max_predictions_per_seq:
                          masked_lm_positions.append(0)
                          masked_lm_ids.append(0)
                          masked_lm_weights.append(0.0)
                    Severity: Major
                    Found in official/nlp/data/create_pretraining_data.py and 1 other location - About 1 hr to fix
                    official/nlp/data/create_pretraining_data.py on lines 151..154

                    Duplicated Code

                    Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                    Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                    When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                    Tuning

                    This issue has a mass of 41.

                    We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                    The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                    If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                    See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                    Refactorings

                    Further Reading

                    Similar blocks of code found in 2 locations. Consider refactoring.
                    Open

                        while len(input_ids) < max_seq_length:
                          input_ids.append(0)
                          input_mask.append(0)
                          segment_ids.append(0)
                    Severity: Major
                    Found in official/nlp/data/create_pretraining_data.py and 1 other location - About 1 hr to fix
                    official/nlp/data/create_pretraining_data.py on lines 164..167

                    Duplicated Code

                    Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                    Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                    When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                    Tuning

                    This issue has a mass of 41.

                    We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                    The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                    If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                    See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                    Refactorings

                    Further Reading

                    Similar blocks of code found in 4 locations. Consider refactoring.
                    Open

                    if __name__ == "__main__":
                      flags.mark_flag_as_required("input_file")
                      flags.mark_flag_as_required("output_file")
                      app.run(main)
                    Severity: Major
                    Found in official/nlp/data/create_pretraining_data.py and 3 other locations - About 55 mins to fix
                    official/legacy/bert/run_squad.py on lines 145..148
                    official/recommendation/create_ncf_data.py on lines 111..114
                    research/attention_ocr/python/model_export.py on lines 195..198

                    Duplicated Code

                    Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                    Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                    When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                    Tuning

                    This issue has a mass of 37.

                    We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                    The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                    If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                    See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                    Refactorings

                    Further Reading

                    Similar blocks of code found in 2 locations. Consider refactoring.
                    Open

                      def __init__(self, tokens, segment_ids, masked_lm_positions, masked_lm_labels,
                                   is_random_next):
                        self.tokens = tokens
                        self.segment_ids = segment_ids
                        self.is_random_next = is_random_next
                    Severity: Minor
                    Found in official/nlp/data/create_pretraining_data.py and 1 other location - About 50 mins to fix
                    official/legacy/xlnet/classifier_utils.py on lines 40..50

                    Duplicated Code

                    Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                    Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                    When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                    Tuning

                    This issue has a mass of 36.

                    We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                    The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                    If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                    See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                    Refactorings

                    Further Reading

                    Similar blocks of code found in 2 locations. Consider refactoring.
                    Open

                        s += "masked_lm_labels: %s\n" % (" ".join(
                            [tokenization.printable_text(x) for x in self.masked_lm_labels]))
                    Severity: Minor
                    Found in official/nlp/data/create_pretraining_data.py and 1 other location - About 45 mins to fix
                    official/nlp/data/create_pretraining_data.py on lines 117..118

                    Duplicated Code

                    Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                    Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                    When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                    Tuning

                    This issue has a mass of 35.

                    We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                    The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                    If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                    See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                    Refactorings

                    Further Reading

                    Similar blocks of code found in 2 locations. Consider refactoring.
                    Open

                        s += "tokens: %s\n" % (" ".join(
                            [tokenization.printable_text(x) for x in self.tokens]))
                    Severity: Minor
                    Found in official/nlp/data/create_pretraining_data.py and 1 other location - About 45 mins to fix
                    official/nlp/data/create_pretraining_data.py on lines 123..124

                    Duplicated Code

                    Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                    Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                    When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                    Tuning

                    This issue has a mass of 35.

                    We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                    The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                    If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                    See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                    Refactorings

                    Further Reading

                    Similar blocks of code found in 2 locations. Consider refactoring.
                    Open

                        s += "masked_lm_positions: %s\n" % (" ".join(
                            [str(x) for x in self.masked_lm_positions]))
                    Severity: Minor
                    Found in official/nlp/data/create_pretraining_data.py and 1 other location - About 35 mins to fix
                    official/nlp/data/create_pretraining_data.py on lines 119..119

                    Duplicated Code

                    Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                    Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                    When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                    Tuning

                    This issue has a mass of 33.

                    We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                    The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                    If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                    See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                    Refactorings

                    Further Reading

                    Similar blocks of code found in 2 locations. Consider refactoring.
                    Open

                        s += "segment_ids: %s\n" % (" ".join([str(x) for x in self.segment_ids]))
                    Severity: Minor
                    Found in official/nlp/data/create_pretraining_data.py and 1 other location - About 35 mins to fix
                    official/nlp/data/create_pretraining_data.py on lines 121..122

                    Duplicated Code

                    Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                    Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                    When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                    Tuning

                    This issue has a mass of 33.

                    We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                    The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                    If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                    See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                    Refactorings

                    Further Reading

                    There are no issues that match your filters.

                    Category
                    Status