Decoding and sampling · Medium

Finite-State Constrained Decoding

Problem 28 of 50 · Python and NumPy

Problem

Perform one greedy decoding step constrained by a finite-state machine. transitions maps string state IDs to dictionaries mapping string token IDs to integer next states. Only transitions from the current state are legal.

Choose the legal token with greatest logit, breaking ties by smaller numeric token ID. Return {"token": chosen ID, "state": next state}. If no legal token exists, return token −1 and preserve the current state. Missing state entries act like empty transition maps.

def constrained_step(logits, state, transitions):

Constraints

  • logits has 1..32 finite entries; all transition token IDs are valid vocabulary IDs.
  • state and next states are nonnegative integers; JSON object keys are their decimal strings.
  • Inputs satisfy these constraints; invalid-input handling is not graded. Use only Python standard library and NumPy.

Examples

Example 1

Arguments: [
  [
    9,
    2,
    3
  ],
  0,
  {
    "0": {
      "1": 1,
      "2": 2
    }
  }
]
Expected result: {
  "token": 2,
  "state": 2
}

Token 0 has the highest overall logit but is illegal. Legal token 2 beats token 1 and moves to state 2.

Example 2

Arguments: [
  [
    1,
    2
  ],
  3,
  {}
]
Expected result: {
  "token": -1,
  "state": 3
}

State 3 has no transitions, so return token -1 and leave state 3 unchanged.

Sign in to solve this problem

Browse all coding problems · Learn the foundations