tensorflow/models

View on GitHub
official/nlp/modeling/layers/relative_attention.py

Summary

Maintainability
F
4 days
Test Coverage

File relative_attention.py has 408 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: Minor
Found in official/nlp/modeling/layers/relative_attention.py - About 5 hrs to fix

    Function call has 12 arguments (exceeds 4 allowed). Consider refactoring.
    Open

      def call(self,  # pytype: disable=signature-mismatch  # overriding-parameter-count-checks
    Severity: Major
    Found in official/nlp/modeling/layers/relative_attention.py - About 1 hr to fix

      Function call has 12 arguments (exceeds 4 allowed). Consider refactoring.
      Open

        def call(self,
      Severity: Major
      Found in official/nlp/modeling/layers/relative_attention.py - About 1 hr to fix

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

          def compute_attention(self,
        Severity: Major
        Found in official/nlp/modeling/layers/relative_attention.py - About 1 hr to fix

          Function call has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
          Open

            def call(self,
                     content_stream,
                     content_attention_bias,
                     positional_attention_bias,
                     query_stream,
          Severity: Minor
          Found in official/nlp/modeling/layers/relative_attention.py - About 35 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

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

          def _build_proj_equation(free_dims, bound_dims, output_dims):
            """Builds an einsum equation for projections inside multi-head attention."""
            input_str = ""
            kernel_str = ""
            output_str = ""
          Severity: Major
          Found in official/nlp/modeling/layers/relative_attention.py and 1 other location - About 1 day to fix
          official/nlp/modeling/layers/reuse_attention.py on lines 79..105

          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 206.

          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

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

            x = tf.reshape(x, [x_size[0], x_size[1] - 1, x_size[2], x_size[3]])
          Severity: Major
          Found in official/nlp/modeling/layers/relative_attention.py and 1 other location - About 1 hr to fix
          official/legacy/xlnet/xlnet_modeling.py on lines 47..47

          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 47.

          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

              if state is not None and state.shape.ndims > 1:
                content_and_memory_stream = tf.concat([state, content_stream], 1)
              else:
                content_and_memory_stream = content_stream
          Severity: Major
          Found in official/nlp/modeling/layers/relative_attention.py and 1 other location - About 1 hr to fix
          official/legacy/xlnet/xlnet_modeling.py on lines 313..316

          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 46.

          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

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

            x = tf.reshape(x, [x_size[1], x_size[0], x_size[2], x_size[3]])
          Severity: Major
          Found in official/nlp/modeling/layers/relative_attention.py and 1 other location - About 1 hr to fix
          official/legacy/xlnet/xlnet_modeling.py on lines 45..45

          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 5 locations. Consider refactoring.
          Open

                if target_mapping is not None:
                  query = tf.einsum("bmnd,bml->blnd", query, target_mapping)
                  query_attention_output = self.compute_attention(
                      query=query,
                      key=key,
          Severity: Major
          Found in official/nlp/modeling/layers/relative_attention.py and 4 other locations - About 1 hr to fix
          official/nlp/modeling/layers/relative_attention.py on lines 304..304
          official/nlp/modeling/layers/relative_attention.py on lines 450..450
          official/nlp/modeling/layers/relative_attention.py on lines 470..470
          research/object_detection/dataset_tools/seq_example_util_test.py on lines 358..358

          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 40.

          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 5 locations. Consider refactoring.
          Open

              content_attention_output = self.compute_attention(
          Severity: Major
          Found in official/nlp/modeling/layers/relative_attention.py and 4 other locations - About 1 hr to fix
          official/nlp/modeling/layers/relative_attention.py on lines 304..304
          official/nlp/modeling/layers/relative_attention.py on lines 470..470
          official/nlp/modeling/layers/relative_attention.py on lines 468..485
          research/object_detection/dataset_tools/seq_example_util_test.py on lines 358..358

          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 40.

          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 5 locations. Consider refactoring.
          Open

              attention_output = self.compute_attention(
          Severity: Major
          Found in official/nlp/modeling/layers/relative_attention.py and 4 other locations - About 1 hr to fix
          official/nlp/modeling/layers/relative_attention.py on lines 450..450
          official/nlp/modeling/layers/relative_attention.py on lines 470..470
          official/nlp/modeling/layers/relative_attention.py on lines 468..485
          research/object_detection/dataset_tools/seq_example_util_test.py on lines 358..358

          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 40.

          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 5 locations. Consider refactoring.
          Open

                  query_attention_output = self.compute_attention(
          Severity: Major
          Found in official/nlp/modeling/layers/relative_attention.py and 4 other locations - About 1 hr to fix
          official/nlp/modeling/layers/relative_attention.py on lines 304..304
          official/nlp/modeling/layers/relative_attention.py on lines 450..450
          official/nlp/modeling/layers/relative_attention.py on lines 468..485
          research/object_detection/dataset_tools/seq_example_util_test.py on lines 358..358

          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 40.

          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

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

          def _get_output_shape(output_rank, known_last_dims):
            return [None] * (output_rank - len(known_last_dims)) + list(known_last_dims)
          Severity: Major
          Found in official/nlp/modeling/layers/relative_attention.py and 1 other location - About 1 hr to fix
          official/nlp/modeling/layers/reuse_attention.py on lines 108..109

          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 38.

          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 5 locations. Consider refactoring.
          Open

              common_kwargs = dict(
                  kernel_initializer=self._kernel_initializer,
                  bias_initializer=self._bias_initializer,
                  kernel_regularizer=self._kernel_regularizer,
                  bias_regularizer=self._bias_regularizer,
          Severity: Major
          Found in official/nlp/modeling/layers/relative_attention.py and 4 other locations - About 50 mins to fix
          official/projects/yolo/modeling/layers/nn_blocks.py on lines 1911..1921
          official/projects/yolo/ops/mosaic.py on lines 236..244
          research/audioset/vggish/vggish_input.py on lines 61..69
          research/pcl_rl/trainer.py on lines 309..316

          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

          There are no issues that match your filters.

          Category
          Status