class_docstr: Base implementation of a pattern over a sequence with multiple codebooks.
  
  The codebook pattern consists in a layout, defining for each sequence step
  the list of coordinates of each codebook timestep in the resulting interleaved sequence.
  The first item of the pattern is always an empty list in order to properly insert a special token
  to start with. For convenience, we also keep track of ``n_q`` the number of codebooks used for the pattern
  and ``timesteps`` the number of timesteps corresponding to the original sequence.
  
  The pattern provides convenient methods to build and revert interleaved sequences from it:
  ``build_pattern_sequence`` maps a given a dense input tensor of multi-codebook sequence from [B, K, T]
      to the interleaved sequence of shape [B, K, S] applying the pattern, with B being the batch size,
      K being the number of codebooks, T the number of original timesteps and S the number of sequence steps
      for the output sequence. The unfilled positions are replaced with a special token and the built sequence
      is returned along with a mask indicating valid tokens.
  ``revert_pattern_sequence`` maps back an interleaved sequence of shape [B, K, S] to the original alignment
      of codebooks across timesteps to an output tensor of shape [B, K, T], using again a special token and a mask
      to fill and specify invalid positions if needed.
  See the dedicated methods for more details.
class_docstr_tok: ["Base","implementation","of","a","pattern","over","a","sequence","with","multiple","codebooks",".","The","codebook","pattern","consists","in","a","layout",",","defining","for","each","sequence","step","the","list","of","coordinates","of","each","codebook","timestep","in","the","resulting","interleaved","sequence",".","The","first","item","of","the","pattern","is","always","an","empty","list","in","order","to","properly","insert","a","special","token","to","start","with",".","For","convenience",",","we","also","keep","track","of","``","n_q","``","the","number","of","codebooks","used","for","the","pattern","and","``","timesteps","``","the","number","of","timesteps","corresponding","to","the","original","sequence",".","The","pattern","provides","convenient","methods","to","build","and","revert","interleaved","sequences","from","it",":","``","build_pattern_sequence","``","maps","a","given","a","dense","input","tensor","of","multi-codebook","sequence","from","[","B",",","K",",","T","]","to","the","interleaved","sequence","of","shape","[","B",",","K",",","S","]","applying","the","pattern",",","with","B","being","the","batch","size",",","K","being","the","number","of","codebooks",",","T","the","number","of","original","timesteps","and","S","the","number","of","sequence","steps","for","the","output","sequence",".","The","unfilled","positions","are","replaced","with","a","special","token","and","the","built","sequence","is","returned","along","with","a","mask","indicating","valid","tokens",".","``","revert_pattern_sequence","``","maps","back","an","interleaved","sequence","of","shape","[","B",",","K",",","S","]","to","the","original","alignment","of","codebooks","across","timesteps","to","an","output","tensor","of","shape","[","B",",","K",",","T","]",",","using","again","a","special","token","and","a","mask","to","fill","and","specify","invalid","positions","if","needed",".","See","the","dedicated","methods","for","more","details","."]
code: def valid_layout(self):
      valid_step = len(self.layout) - self.max_delay
      return self.layout[:valid_step]
  
imports: from collections import namedtuple
  from dataclasses import dataclass
  from functools import lru_cache
  import logging
  import typing
  from abc import ABC, abstractmethod
  import torch
label_desc: Use image node_id 5 for calling the Pattern obj's underlying member method code with example usage: obj.valid_layout() and returns: self