Tensor and numerical foundations · Medium

Project and Split Packed QKV

Problem 7 of 50 · Python and NumPy

Problem

Project token rows through a packed QKV matrix. x is T × I and weight is I × (3D); its columns contain all Q features, then all K, then all V. Return {"q": Q, "k": K, "v": V}, each of shape heads × T × (D/heads). Within each projection, consecutive features belong to consecutive heads.

def packed_qkv(x, weight, heads):

Constraints

  • 1 ≤ T,I,D ≤ 12; heads is a positive divisor of D.
  • Inputs are finite; no bias is applied.
  • Inputs satisfy these constraints; invalid-input handling is not graded. Use only Python standard library and NumPy.

Examples

Example 1

Arguments: [
  [
    [
      1
    ]
  ],
  [
    [
      1,
      2,
      3
    ]
  ],
  1
]
Expected result: {
  "q": [
    [
      [
        1
      ]
    ]
  ],
  "k": [
    [
      [
        2
      ]
    ]
  ],
  "v": [
    [
      [
        3
      ]
    ]
  ]
}

The packed result [1,2,3] becomes one scalar each for Q, K, and V.

Example 2

Arguments: [
  [
    [
      1,
      2
    ]
  ],
  [
    [
      1,
      0,
      0,
      0,
      0,
      0
    ],
    [
      0,
      1,
      0,
      0,
      0,
      0
    ]
  ],
  2
]
Expected result: {
  "q": [
    [
      [
        1
      ]
    ],
    [
      [
        2
      ]
    ]
  ],
  "k": [
    [
      [
        0
      ]
    ],
    [
      [
        0
      ]
    ]
  ],
  "v": [
    [
      [
        0
      ]
    ],
    [
      [
        0
      ]
    ]
  ]
}

The two Q features [1,2] are placed in separate heads; K and V are zero.

Sign in to solve this problem

Browse all coding problems · Learn the foundations