tensorflow/models

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

Summary

Maintainability
F
6 days
Test Coverage

File reuse_attention.py has 506 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/modeling/layers/reuse_attention.py - About 1 day to fix

    Function _build_from_signature has a Cognitive Complexity of 20 (exceeds 5 allowed). Consider refactoring.
    Open

      def _build_from_signature(self, query, value, key=None):
        """Builds layers and variables.
    
        Once the method is called, self._built_from_signature will be set to True.
    
    
    Severity: Minor
    Found in official/nlp/modeling/layers/reuse_attention.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 __init__ has 18 arguments (exceeds 4 allowed). Consider refactoring.
    Open

      def __init__(self,
    Severity: Major
    Found in official/nlp/modeling/layers/reuse_attention.py - About 2 hrs to fix

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

        def __init__(self,
                     num_heads,
                     key_dim,
                     value_dim=None,
                     dropout=0.0,
      Severity: Minor
      Found in official/nlp/modeling/layers/reuse_attention.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 _compute_attention has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
      Open

        def _compute_attention(self,
                               query,
                               key,
                               value,
                               reuse_scores=None,
      Severity: Minor
      Found in official/nlp/modeling/layers/reuse_attention.py - About 55 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

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

        def call(self,
                 query,
                 value,
                 key=None,
                 attention_mask=None,
      Severity: Minor
      Found in official/nlp/modeling/layers/reuse_attention.py - About 55 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

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

        def call(self,
      Severity: Major
      Found in official/nlp/modeling/layers/reuse_attention.py - About 50 mins to fix

        Function _compute_attention has 6 arguments (exceeds 4 allowed). Consider refactoring.
        Open

          def _compute_attention(self,
        Severity: Minor
        Found in official/nlp/modeling/layers/reuse_attention.py - About 45 mins to fix

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

            def _make_output_dense(self, free_dims, common_kwargs, name=None,
                                   use_bias=True):
              """Builds the output projection matrix.
          
              Args:
          Severity: Minor
          Found in official/nlp/modeling/layers/reuse_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/reuse_attention.py and 1 other location - About 1 day to fix
          official/nlp/modeling/layers/relative_attention.py on lines 23..49

          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

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

                  self._query_dense = tf_keras.layers.EinsumDense(
                      einsum_equation,
                      output_shape=_get_output_shape(
                          output_rank - 1,
                          [self._num_heads - self._reuse_heads, self._key_dim]),
          Severity: Major
          Found in official/nlp/modeling/layers/reuse_attention.py and 1 other location - About 4 hrs to fix
          official/nlp/modeling/layers/reuse_attention.py on lines 378..387

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

          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

                  self._key_dense = tf_keras.layers.EinsumDense(
                      einsum_equation,
                      output_shape=_get_output_shape(
                          output_rank - 1,
                          [self._num_heads - self._reuse_heads, self._key_dim]),
          Severity: Major
          Found in official/nlp/modeling/layers/reuse_attention.py and 1 other location - About 4 hrs to fix
          official/nlp/modeling/layers/reuse_attention.py on lines 365..374

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

          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

                for _ in range(len(attention_scores.shape) - len(attention_mask.shape)):
                  attention_mask = tf.expand_dims(
          Severity: Major
          Found in official/nlp/modeling/layers/reuse_attention.py and 1 other location - About 1 hr to fix
          official/projects/qat/nlp/modeling/layers/multi_head_attention.py on lines 70..71

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

          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/reuse_attention.py and 1 other location - About 1 hr to fix
          official/nlp/modeling/layers/relative_attention.py on lines 52..53

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

              if hasattr(value, "shape"):
                self._value_shape = tf.TensorShape(value.shape)
              else:
                self._value_shape = tf.TensorShape(value)
          Severity: Major
          Found in official/nlp/modeling/layers/reuse_attention.py and 2 other locations - About 1 hr to fix
          official/nlp/modeling/layers/reuse_attention.py on lines 336..339
          official/nlp/modeling/layers/reuse_attention.py on lines 344..349

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

              if key is None:
                self._key_shape = self._value_shape
              elif hasattr(key, "shape"):
                self._key_shape = tf.TensorShape(key.shape)
              else:
          Severity: Major
          Found in official/nlp/modeling/layers/reuse_attention.py and 2 other locations - About 1 hr to fix
          official/nlp/modeling/layers/reuse_attention.py on lines 336..339
          official/nlp/modeling/layers/reuse_attention.py on lines 340..343

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

              if hasattr(query, "shape"):
                self._query_shape = tf.TensorShape(query.shape)
              else:
                self._query_shape = tf.TensorShape(query)
          Severity: Major
          Found in official/nlp/modeling/layers/reuse_attention.py and 2 other locations - About 1 hr to fix
          official/nlp/modeling/layers/reuse_attention.py on lines 340..343
          official/nlp/modeling/layers/reuse_attention.py on lines 344..349

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

              if self._reuse_heads < self._num_heads:
                attention_output.append(
                    tf.einsum(self._combine_equation, self._dropout_layer(
                        new_scores, training=training), value[-1]))
          Severity: Minor
          Found in official/nlp/modeling/layers/reuse_attention.py and 1 other location - About 45 mins to fix
          official/nlp/modeling/layers/reuse_attention.py on lines 557..560

          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

              if self._reuse_heads > 0:
                attention_output.append(
                    tf.einsum(self._combine_equation, self._dropout_layer(
                        reuse_scores, training=training), value[0]))
          Severity: Minor
          Found in official/nlp/modeling/layers/reuse_attention.py and 1 other location - About 45 mins to fix
          official/nlp/modeling/layers/reuse_attention.py on lines 562..565

          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

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

              common_kwargs = dict(
                  kernel_regularizer=self._kernel_regularizer,
                  bias_regularizer=self._bias_regularizer,
                  activity_regularizer=self._activity_regularizer,
                  kernel_constraint=self._kernel_constraint,
          Severity: Major
          Found in official/nlp/modeling/layers/reuse_attention.py and 6 other locations - About 35 mins to fix
          official/nlp/modeling/layers/gated_feedforward.py on lines 103..108
          official/nlp/modeling/layers/multi_channel_attention.py on lines 63..68
          official/nlp/modeling/layers/reuse_transformer.py on lines 161..166
          official/nlp/modeling/layers/tn_transformer_expand_condense.py on lines 132..137
          official/nlp/modeling/layers/transformer.py on lines 265..270
          official/nlp/modeling/layers/transformer_scaffold.py on lines 148..153

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

              attention_output = [od(attention_output[i]) for i, od in enumerate(
                  self._output_dense)]
          Severity: Minor
          Found in official/nlp/modeling/layers/reuse_attention.py and 2 other locations - About 30 mins to fix
          research/seq_flow_lite/layers/qrnn_layers.py on lines 322..322
          research/seq_flow_lite/layers/qrnn_layers.py on lines 323..324

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

          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