class_docstr: Scaled Dot-Product Attention
class_docstr_tok: ["Scaled","Dot-Product","Attention"]
code: def forward(self, q, k, v, mask=None):
      k = paddle.transpose(k, perm=[0, 2, 1])
      attn = paddle.bmm(q, k)
      attn = attn / self.temperature
      if mask is not None:
          attn = attn.masked_fill(mask, -1e9)
          if mask.dim() == 3:
              mask = paddle.unsqueeze(mask, axis=1)
          elif mask.dim() == 2:
              mask = paddle.unsqueeze(mask, axis=1)
              mask = paddle.unsqueeze(mask, axis=1)
          repeat_times = [
              attn.shape[1] // mask.shape[1],
              attn.shape[2] // mask.shape[2],
          ]
          mask = paddle.tile(
              mask, [1, repeat_times[0], repeat_times[1], 1]
          )
          attn[mask == 0] = -1e9
      attn = self.softmax(attn)
      attn = self.dropout(attn)
      output = paddle.bmm(attn, v)
      return output
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import paddle
  from paddle import ParamAttr
  import paddle.nn
  import paddle.nn.functional
  from paddle.nn.initializer import Normal, XavierNormal
  import numpy
label_desc: Use image node_id 2 for calling the ScaledDotProductAttention obj's underlying member method code with example usage: obj.forward(q, k, v, mask) and returns: output