# Constrained decoding based on position

**URL:** <https://discuss.huggingface.co/t/constrained-decoding-based-on-position/110056>\
**Category:** 🤗Transformers\
**Created:** [October 4, 2024, 12:41am UTC](https://discuss.huggingface.co/t/constrained-decoding-based-on-position/110056 "2024-10-04T00:41:58Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jielei](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@jielei](https://discuss.huggingface.co/u/jielei)\
**Post date:** [October 4, 2024, 12:41am UTC](https://discuss.huggingface.co/t/constrained-decoding-based-on-position/110056/1 "2024-10-04T00:41:58Z")

</div>

Hello team!

Is there a way to constrain the LM to only decode certain tokens at certain positions? For example, assume I need model to decode 6 tokens, indexed by 0 to 5. The tokens at positions (0, 2, 4) should be from vocabulary\_1, these at positions (1, 3, 5) should be from vocabulary\_2.

vocabulary\_1 and vocabulary\_2 are disjoint set of tokens, both are subsets of the training vocabulary.

Any suggestion is welcome!

* * *

Update:

This can be simplified a bit for my case. vocabulary\_1 is actually the first half of the training vocabulary, vocabulary\_2 is the 2nd half. So ideally at positions (0, 2, 4), I can mask logits of shape (1, |V|) with logits[:, -|V|/2:] = float(“-inf”), and similarly for positions (1, 3, 5) to mask another half. Wanna ask how this can be easily done with transformers generate function.
