{
  "scope": "Three hand-set token vectors; self-attention with 2 heads; NumPy equations compared to torch.nn.MultiheadAttention. No training, corpus, positional encoding, mask, task metric, speed test, or benchmark.",
  "environment": {
    "python": "3.10.12",
    "numpy": "1.26.3",
    "torch": "2.3.1+cu118",
    "device": "cpu",
    "dtype": "float64",
    "batch_first": true,
    "dropout": 0.0,
    "module_eval": true,
    "need_weights": true,
    "average_attn_weights": false
  },
  "dimensions": {
    "batch": 1,
    "sequence_length": 3,
    "d_model": 4,
    "num_heads": 2,
    "head_dim": 2,
    "shapes": {
      "X": [
        3,
        4
      ],
      "Q": [
        3,
        4
      ],
      "K": [
        3,
        4
      ],
      "V": [
        3,
        4
      ],
      "Q_heads_K_heads_V_heads": [
        2,
        3,
        2
      ],
      "scores_per_head": [
        2,
        3,
        3
      ],
      "weights_per_head": [
        2,
        3,
        3
      ],
      "contexts_per_head": [
        2,
        3,
        2
      ],
      "concat_heads": [
        3,
        4
      ],
      "output_after_WO": [
        3,
        4
      ],
      "torch_output": [
        1,
        3,
        4
      ],
      "torch_weights_per_head": [
        1,
        2,
        3,
        3
      ]
    },
    "meaning": "The first dimension of projected tensors is token position; the head axis holds 2 separate 2D subspaces. Each score/weight matrix is [head, query-token, key-token]."
  },
  "parameters_row_vectors_use_X_times_W_transpose": {
    "X": [
      [
        0.2,
        -0.4,
        1.0,
        0.5
      ],
      [
        1.1,
        0.3,
        -0.2,
        0.8
      ],
      [
        -0.7,
        1.2,
        0.4,
        -0.1
      ]
    ],
    "W_Q_out_by_in": [
      [
        0.5,
        -0.2,
        0.3,
        0.1
      ],
      [
        -0.4,
        0.6,
        0.2,
        -0.3
      ],
      [
        0.1,
        0.2,
        -0.5,
        0.4
      ],
      [
        0.3,
        -0.1,
        0.6,
        0.2
      ]
    ],
    "b_Q": [
      0.1,
      -0.05,
      0.2,
      0.15
    ],
    "W_K_out_by_in": [
      [
        0.2,
        0.4,
        -0.3,
        0.1
      ],
      [
        -0.1,
        0.5,
        0.2,
        -0.2
      ],
      [
        0.6,
        -0.3,
        0.1,
        0.4
      ],
      [
        -0.2,
        0.1,
        0.5,
        0.3
      ]
    ],
    "b_K": [
      -0.1,
      0.07,
      0.03,
      0.12
    ],
    "W_V_out_by_in": [
      [
        0.4,
        0.2,
        -0.1,
        0.3
      ],
      [
        0.1,
        -0.5,
        0.6,
        0.2
      ],
      [
        -0.2,
        0.3,
        0.4,
        -0.1
      ],
      [
        0.5,
        0.1,
        0.2,
        -0.4
      ]
    ],
    "b_V": [
      0.02,
      0.1,
      -0.08,
      0.05
    ],
    "W_O_out_by_in": [
      [
        0.7,
        -0.2,
        0.1,
        0.3
      ],
      [
        -0.3,
        0.4,
        0.5,
        -0.1
      ],
      [
        0.2,
        0.6,
        -0.4,
        0.15
      ],
      [
        0.1,
        -0.35,
        0.2,
        0.55
      ]
    ],
    "b_O": [
      0.05,
      -0.02,
      0.07,
      -0.04
    ],
    "note": "All Q/K/V and output matrices are dense, explicitly overwritten, and not identity or diagonal matrices. These are illustrative fixed parameters, not learned weights."
  },
  "projected_Q_K_V": {
    "Q": [
      [
        0.63,
        -0.32,
        -0.16,
        0.95
      ],
      [
        0.61,
        -0.59,
        0.79,
        0.49
      ],
      [
        -0.38,
        1.06,
        0.13,
        0.04
      ]
    ],
    "K": [
      [
        -0.47,
        -0.05,
        0.57,
        0.69
      ],
      [
        0.38,
        -0.09,
        0.9,
        0.07
      ],
      [
        0.11,
        0.84,
        -0.75,
        0.55
      ]
    ],
    "V": [
      [
        0.07,
        1.02,
        0.11,
        0.11
      ],
      [
        0.78,
        0.1,
        -0.37,
        0.27
      ],
      [
        -0.09,
        -0.35,
        0.59,
        -0.06
      ]
    ]
  },
  "per_head": [
    {
      "head": 0,
      "Q": [
        [
          0.63,
          -0.32
        ],
        [
          0.61,
          -0.59
        ],
        [
          -0.38,
          1.06
        ]
      ],
      "K": [
        [
          -0.47,
          -0.05
        ],
        [
          0.38,
          -0.09
        ],
        [
          0.11,
          0.84
        ]
      ],
      "V": [
        [
          0.07,
          1.02
        ],
        [
          0.78,
          0.1
        ],
        [
          -0.09,
          -0.35
        ]
      ],
      "raw_QK_transpose_scores": [
        [
          -0.2801,
          0.2682,
          -0.1995
        ],
        [
          -0.2572,
          0.2849,
          -0.4285
        ],
        [
          0.1256,
          -0.2398,
          0.8486
        ]
      ],
      "scaled_scores_divide_sqrt_head_dim": [
        [
          -0.19806060941,
          0.189646038714,
          -0.141067802847
        ],
        [
          -0.181867864121,
          0.20145472196,
          -0.302995255738
        ],
        [
          0.088812611717,
          -0.169564206129,
          0.600050814515
        ]
      ],
      "softmax_over_key_token_axis": [
        [
          0.283106019173,
          0.417184304622,
          0.299709676205
        ],
        [
          0.298233948345,
          0.437554306023,
          0.264211745632
        ],
        [
          0.290726527375,
          0.224529306305,
          0.48474416632
        ]
      ],
      "weight_row_sums": [
        1.0,
        1.0,
        1.0
      ],
      "weighted_value_context": [
        [
          0.318247308089,
          0.225588183347
        ],
        [
          0.338389677975,
          0.255479946943
        ],
        [
          0.151856740865,
          0.149333530341
        ]
      ]
    },
    {
      "head": 1,
      "Q": [
        [
          -0.16,
          0.95
        ],
        [
          0.79,
          0.49
        ],
        [
          0.13,
          0.04
        ]
      ],
      "K": [
        [
          0.57,
          0.69
        ],
        [
          0.9,
          0.07
        ],
        [
          -0.75,
          0.55
        ]
      ],
      "V": [
        [
          0.11,
          0.11
        ],
        [
          -0.37,
          0.27
        ],
        [
          0.59,
          -0.06
        ]
      ],
      "raw_QK_transpose_scores": [
        [
          0.5643,
          -0.0775,
          0.6425
        ],
        [
          0.7884,
          0.7453,
          -0.323
        ],
        [
          0.1017,
          0.1198,
          -0.0755
        ]
      ],
      "scaled_scores_divide_sqrt_head_dim": [
        [
          0.399020356624,
          -0.054800775542,
          0.454316106912
        ],
        [
          0.557482986287,
          0.527006684018,
          -0.228395490323
        ],
        [
          0.071912759647,
          0.084711392386,
          -0.05338656198
        ]
      ],
      "softmax_over_key_token_axis": [
        [
          0.371464198659,
          0.235952699161,
          0.392583102179
        ],
        [
          0.412251690605,
          0.399877304012,
          0.187871005382
        ],
        [
          0.345409580211,
          0.349858761558,
          0.304731658231
        ]
      ],
      "weight_row_sums": [
        1.0,
        1.0,
        1.0
      ],
      "weighted_value_context": [
        [
          0.185182593449,
          0.081013304495
        ],
        [
          0.008236976657,
          0.142042297727
        ],
        [
          0.088338990403,
          0.11417301995
        ]
      ]
    }
  ],
  "head_concat_and_output_projection": {
    "concatenated_heads_before_WO": [
      [
        0.318247308089,
        0.225588183347,
        0.185182593449,
        0.081013304495
      ],
      [
        0.338389677975,
        0.255479946943,
        0.008236976657,
        0.142042297727
      ],
      [
        0.151856740865,
        0.149333530341,
        0.088338990403,
        0.11417301995
      ]
    ],
    "W_O_times_concat_plus_b_O": [
      [
        0.270477729686,
        0.059251047187,
        0.207081329921,
        -0.0055372972
      ],
      [
        0.279213172178,
        -0.029410666059,
        0.308977457757,
        -0.015808354551
      ],
      [
        0.169518817563,
        0.026928583083,
        0.171761823209,
        0.00338189752
      ]
    ],
    "torch_mha_output": [
      [
        0.270477729686,
        0.059251047187,
        0.207081329921,
        -0.0055372972
      ],
      [
        0.279213172178,
        -0.029410666059,
        0.308977457757,
        -0.015808354551
      ],
      [
        0.169518817563,
        0.026928583083,
        0.171761823209,
        0.00338189752
      ]
    ],
    "torch_per_head_weights": [
      [
        [
          0.283106019173,
          0.417184304622,
          0.299709676205
        ],
        [
          0.298233948345,
          0.437554306023,
          0.264211745632
        ],
        [
          0.290726527375,
          0.224529306305,
          0.48474416632
        ]
      ],
      [
        [
          0.371464198659,
          0.235952699161,
          0.392583102179
        ],
        [
          0.412251690605,
          0.399877304012,
          0.187871005382
        ],
        [
          0.345409580211,
          0.349858761558,
          0.304731658231
        ]
      ]
    ],
    "torch_manual_scores_from_same_packed_QK": [
      [
        [
          -0.19806060941,
          0.189646038714,
          -0.141067802847
        ],
        [
          -0.181867864121,
          0.20145472196,
          -0.302995255738
        ],
        [
          0.088812611717,
          -0.169564206129,
          0.600050814515
        ]
      ],
      [
        [
          0.399020356624,
          -0.054800775542,
          0.454316106912
        ],
        [
          0.557482986287,
          0.527006684018,
          -0.228395490323
        ],
        [
          0.071912759647,
          0.084711392386,
          -0.05338656198
        ]
      ]
    ]
  },
  "max_abs_difference_numpy_vs_torch": {
    "Q": 2.220446049250313e-16,
    "K": 1.1102230246251565e-16,
    "V": 1.1102230246251565e-16,
    "scaled_scores": 1.1102230246251565e-16,
    "per_head_weights": 5.551115123125783e-17,
    "final_output_after_WO": 5.551115123125783e-17,
    "assertion_tolerance": 1e-12
  },
  "checks": "PASS: dense hand-set Q/K/V and W_O projections, per-head scores/weights/contexts, concatenation and output projection match PyTorch MultiheadAttention in the single specified CPU/float64 case."
}
