class_docstr: Returns a layer that computes per-head attention (via scaled dot-product).
  
  This layer computes the core of the attention mechanism. Given per-head
  queries (Q), keys (K), values (V), and mask, it:
  
    - computes the scaled dot product of each Q-K pair;
    - applies mask to screen out positions that come from padding tokens
      (indicated by 0 value);
    - [if created in ``'train'`` mode] applies dropout to Q-K dot products;
    - computes Q-K attention strengths using a per-query softmax of the Q-K dot
      products; and
    - for each query position, combines V vectors according to the Q-K
      attention strengths.
class_docstr_tok: ["Returns","a","layer","that","computes","per-head","attention","(","via","scaled","dot-product",")",".","This","layer","computes","the","core","of","the","attention","mechanism",".","Given","per-head","queries","(","Q",")",",","keys","(","K",")",",","values","(","V",")",",","and","mask",",","it",":","-","computes","the","scaled","dot","product","of","each","Q-K","pair",";","-","applies","mask","to","screen","out","positions","that","come","from","padding","tokens","(","indicated","by","0","value",")",";","-","[","if","created","in","``","'train","'","``","mode","]","applies","dropout","to","Q-K","dot","products",";","-","computes","Q-K","attention","strengths","using","a","per-query","softmax","of","the","Q-K","dot","products",";","and","-","for","each","query","position",",","combines","V","vectors","according","to","the","Q-K","attention","strengths","."]
docstr: Creates a :py:class:`DotProductAttention` instance in a specific mode.
  
  Args:
    dropout: Probababilistic rate for attention dropout, which overrides
        (sets to zero) some attention strengths derived from query-key
        matching. As a result, on a given forward pass, some value vectors
        don't contribute to the output, analogous to how regular dropout can
        cause some node activations to be ignored. Applies only if layer is
        created in ``'train'`` mode.
    mode: One of ``'train'``, ``'eval'``, ``'predict'`` or ``'viz'``.
code: def __init__(self, dropout=0.0, mode="train"):
      """Creates a :py:class:`DotProductAttention` instance in a specific mode.
  
      Args:
        dropout: Probababilistic rate for attention dropout, which overrides
            (sets to zero) some attention strengths derived from query-key
            matching. As a result, on a given forward pass, some value vectors
            don't contribute to the output, analogous to how regular dropout can
            cause some node activations to be ignored. Applies only if layer is
            created in ``'train'`` mode.
        mode: One of ``'train'``, ``'eval'``, ``'predict'`` or ``'viz'``.
      """
      super().__init__(n_in=4, n_out=1)
      self._dropout = dropout
      self._mode = mode
  
imports: import numpy
  from trax import fastmath
  from trax.fastmath import numpy
  from trax.layers import base
  from trax.layers import combinators
  from trax.layers import convolution
  from trax.layers import core
  from trax.layers import initializers
  from trax.layers.assert_shape import assert_shape
  from trax.layers.base import Fn
  from trax.layers.research import sparsity
label_desc: Use image node_id 1 to create a new DotProductAttention object from inherited base classes: base with example: obj = DotProductAttention(dropout, mode)