Implement a gated recurrent state scan. At position t, γt is a scalar decay gate, qt, kt, and vt are query, key, and value vectors, St is the state, and yt is the output. Superscript T denotes transpose.
St=γtSt−1+ktvtT. yt=qtTSt.Each batch state starts at zero, and the current output reads the updated state. Return a dictionary containing outputs with shape (B, S, Dv) and final_state with shape (B, Dk, Dv), both PyTorch tensors with the same dtype and device as q.
Input: q = [[[1,0],[0,1]]], k = [[[1,0],[0,1]]], v = [[[2,3],[4,5]]], gamma = [[0.5,1]]
Output: {"outputs":[[[2,3],[4,5]]],"final_state":[[[2,3],[4,5]]]}
Explanation: The first key writes the first state row. The second gate preserves that row while the orthogonal key writes the second row.
Input: q = [[[1,1],[2,0]]], k = [[[1,0],[0,1]]], v = [[[3],[5]]], gamma = [[0,0]]
Output: {"outputs":[[[3],[0]]],"final_state":[[[0],[5]]]}
Input: q = [[[1]]], k = [[[2]]], v = [[[3]]], gamma = [[0.25]]
Output: {"outputs":[[[6]]],"final_state":[[[6]]]}
torch.unsqueeze adds singleton dimensions for broadcasting a scalar gate.
torch.bmm handles the per-batch matrix products.
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Implement a gated recurrent state scan. At position t, γt is a scalar decay gate, qt, kt, and vt are query, key, and value vectors, St is the state, and yt is the output. Superscript T denotes transpose.
St=γtSt−1+ktvtT. yt=qtTSt.Each batch state starts at zero, and the current output reads the updated state. Return a dictionary containing outputs with shape (B, S, Dv) and final_state with shape (B, Dk, Dv), both PyTorch tensors with the same dtype and device as q.
Input: q = [[[1,0],[0,1]]], k = [[[1,0],[0,1]]], v = [[[2,3],[4,5]]], gamma = [[0.5,1]]
Output: {"outputs":[[[2,3],[4,5]]],"final_state":[[[2,3],[4,5]]]}
Explanation: The first key writes the first state row. The second gate preserves that row while the orthogonal key writes the second row.
Input: q = [[[1,1],[2,0]]], k = [[[1,0],[0,1]]], v = [[[3],[5]]], gamma = [[0,0]]
Output: {"outputs":[[[3],[0]]],"final_state":[[[0],[5]]]}
Input: q = [[[1]]], k = [[[2]]], v = [[[3]]], gamma = [[0.25]]
Output: {"outputs":[[[6]]],"final_state":[[[6]]]}
torch.unsqueeze adds singleton dimensions for broadcasting a scalar gate.
torch.bmm handles the per-batch matrix products.
Accepts: array
Accepts: array
Accepts: array
Accepts: array