Protein sequence and inverse folding
Iterative masked-token reveal with fixed prompts
Free account with email, Google or GitHub. Your code runs in an isolated PyTorch sandbox.
Your task
Choose which editable masked positions to reveal under a per-example budget. Commit supplied proposals using the selected entropy or priority ordering and return the updated tokens and masks.
Function signature
iterative_masked_token_update(
tokens: torch.Tensor,
proposals: torch.Tensor,
probabilities: torch.Tensor,
priorities: torch.Tensor,
lengths: torch.Tensor,
position_mask: torch.Tensor,
editable_mask: torch.Tensor,
target_remaining: torch.Tensor,
strategy: str = 'entropy',
mask_id: int = -1,
) -> dict[str, torch.Tensor]Worked example
Reveal low entropy positions until one mask remains
Candidates are positions 1, 2, and 4. Their confidence distributions are [1, 0, 0, 0], [1/4, 1/4, 1/4, 1/4], and [1/2, 1/2, 0, 0], with entropies 0, \log4, and \log2. With target_remaining 1, reveal 3-1=2 candidates: positions 1 and 4. Copy their proposals 3 and 1, giving tokens [90, 3, -1, 2, 1, 91]. Position 2 remains masked; known token 2 at position 3 and boundary tokens 90/91 are unchanged. Proposal 3 at position 1 is copied even though its confidence distribution concentrates on ID 0.
Parameters
| Parameter | Shape and meaning |
|---|---|
tokenstorch.Tensor | [B, L, D]Integer [B,L,D], current token slots. |
proposalstorch.Tensor | [B, L, D]Supplied nonmask proposals. |
probabilitiestorch.Tensor | [B, L, D, V]Nonnegative floating [B,L,D,V], per-slot distributions; zero terms have the specified safe branch. |
prioritiestorch.Tensor | [B, L]Supplied finite floating [B,L], hard random ordering keys. |
lengthstorch.Tensor | [B]Integer [B], includes BOS/EOS. |
position_masktorch.Tensor | [B, L]Bool [B,L], present positions. |
editable_masktorch.Tensor | [B, L]Bool [B,L], allowed edits. |
target_remainingtorch.Tensor | [B]Nonnegative integer [B], schedule's remaining-mask target. |
strategystrDefault 'entropy' | Choose "entropy" to reveal lowest-entropy positions or "random" to use the supplied priority ordering. Both obey the per-example reveal budget and stable tie rule. |
mask_idintDefault -1 | Token ID marking a currently masked position. |
Return
dict[str, torch.Tensor]
Return exactly the named fields applicable to this operation.
Required behavior
tokensandproposalsare integer[B,L,D], withB,D\ge 1,L\ge 2;D=1represents a sequence/structure track andD=8can represent function-code slots.probabilities[B,L,D,V]hasV\ge 2, finite NONNEGATIVE supplied categoricalprobabilities, each slot summing to1.- This independently supplied confidence distribution alone determines
entropy; it need not be the categorical law that producedproposals. No universal upstream before/after-top-pdistribution is assumed. Zeroprobabilitiesmay represent excluded vocabulary IDs. All floats share dtype/device. priorities[B,L]is supplied finite random priority, not drawn internally.lengths[B]integers lie in[2,L]and include BOS/EOS: position0and position\mathrm{lengths}[b]-1are special, and all later positions are padding.position_mask[B,L]andeditable_mask[B,L]are bool withTruemeans present/eligible-for-editing respectively.target_remaining[B]is a nonnegative integer count supplied by the schedule. Configmask_idis an integer (default-1); strategy is 'entropy' (default) or 'random'.- A candidate position must satisfy
0<i<\mathrm{length}-1,position_maskandeditable_maskTrue, and ALLDcurrent slots equalmask_id. Thus partially known function slots are not overwritten. - For every valid nonspecial position return
\mathrm{entropy}_i=-\sum_{\mathrm{slot},v}p_{i,\mathrm{slot},v}\log p_{i,\mathrm{slot},v}, defining ap=0term as0with a chosen zero derivative on that branch; its singular ambient derivative at zero is not claimed. Invalid/special/paddedentropyis exactly0. - For strategy
entropy, sort candidate positions by increasing totalentropy; for random, by increasing suppliedpriorities. Equal scores are ordered by smaller position index. The number to commit is\operatorname{max}(\mathrm{number\_of\_candidates}-\mathrm{target\_remaining},0), so an oversized remaining target safely commits none. - Replace ALL
Dslots at committed positions with their suppliedproposals, preserving every other integer exactly. Proposal IDs are in[0,V)and must differ frommask_idat committed positions, ensuring a monotonic reveal. - Return
tokens,commit_mask[B,L],remaining_mask[B,L]for eligible positions still masked,committed_count[B],remaining_count[B], andentropy[B,L]. No neural-model call, categorical draw, remasking of knowntokens, or schedule evaluation is hidden in this step. - Batch rows have independent
lengthsand budgets; no-mask and all-fixed cases are allowed.
Why this matters
The model’s uncertainty ranks eligible positions; the commit budget decides how many complete token slots become visible in this step.
Starter code
You write the body of this function in the BioTorch workspace. Your code runs in an isolated PyTorch sandbox; Run checks the public examples and Submit grades the full test suite, including gradients.
import torch
def iterative_masked_token_update(
tokens: torch.Tensor, # (B, L, D)
proposals: torch.Tensor, # (B, L, D)
probabilities: torch.Tensor, # (B, L, D, V)
priorities: torch.Tensor, # (B, L)
lengths: torch.Tensor, # (B,)
position_mask: torch.Tensor, # (B, L)
editable_mask: torch.Tensor, # (B, L)
target_remaining: torch.Tensor, # (B,)
strategy: str = 'entropy',
mask_id: int = -1,
) -> dict[str, torch.Tensor]:
"""Return dict[str, torch.Tensor]:
tokens: (B, L, D).
commit_mask: (B, L).
remaining_mask: (B, L).
committed_count: (B,).
remaining_count: (B,).
entropy: (B, L)."""
# Implement the operation. Inputs and test setup are supplied by BioTorch.
raise NotImplementedError("Implement iterative_masked_token_update")
Ready to implement it?
3 progressive hints, a solution walkthrough, and grading that checks values, shapes and gradients are waiting in the workspace.
Free account with email, Google or GitHub. Your code runs in an isolated PyTorch sandbox.
Sources and further reading
A primitive exercise, not an end-to-end reproduction of the named models. No affiliation or endorsement is implied.