TensorTonic
Study Plans
Study Plan
Compute Conditional Perplexity
Metrics & EvaluationLanguage Models
Medium

Compute conditional perplexity using only the response tokens selected by the mask. Logits and target IDs are already aligned at each batch and sequence position; no token shift is needed. Let iii range over those positions, xix_ixi​ be the target ID, mim_imi​ be its mask value (one for selected, zero otherwise), and pi(xi)p_i(x_i)pi​(xi​) be its probability under the vocabulary softmax. With M=∑imiM=\sum_i m_iM=∑i​mi​ selected tokens, the negative log likelihood is:

NLL=−1M∑imilog⁡pi(xi).\mathrm{NLL}=-\frac{1}{M}\sum_i m_i\log p_i(x_i).NLL=−M1​i∑​mi​logpi​(xi​).

Perplexity is:

PPL=exp⁡(NLL).\mathrm{PPL}=\exp(\mathrm{NLL}).PPL=exp(NLL).

Here, log is the natural logarithm and exp its inverse. Average across selected tokens, not across sequences. If none are selected, use NLL zero and perplexity one. Return a Python dictionary with negative_log_likelihood and perplexity as Python floats, and selected_token_count as a Python integer.

Loading visualization...

Examples

Input: logits = [[[0,0],[0,0],[0,0]]], target_ids = [[0,1,0]], response_mask = [[false,true,true]]

Output: {"negative_log_likelihood":0.693147,"perplexity":2,"selected_token_count":2}

Explanation: Each selected token has probability one half, so the average negative log likelihood is log 2 and perplexity is 2.

Input: logits = [[[4,0],[0,4],[9,-9],[-9,9]]], target_ids = [[0,1,1,0]], response_mask = [[false,true,false,false]]

Output: {"negative_log_likelihood":0.01815,"perplexity":1.018316,"selected_token_count":1}

Input: logits = [[[1,-1],[2,-2]]], target_ids = [[0,1]], response_mask = [[false,false]]

Output: {"negative_log_likelihood":0,"perplexity":1,"selected_token_count":0}

Hint 1

torch.log_softmax computes stable log probabilities.

Hint 2

Tensor.gather selects entries using an index tensor.

Hint 3

Boolean indexing selects only the positions marked true.

Requirements

  • Use PyTorch; evaluate token log probabilities in the logits dtype and their selected-token mean in float64.
  • Prompt and padding positions marked false must not affect either metric.
  • Do not modify input tensors.

Constraints

  • logits is a float32 or float64 PyTorch tensor of shape (batch, sequence, vocabulary), with at least one vocabulary entry.
  • target_ids and response_mask are PyTorch tensors of shape (batch, sequence), with int64 and bool dtypes respectively.
  • Target IDs are valid vocabulary indices, and the mask may select no tokens.
  • Inputs must produce a perplexity representable as a finite Python float.
  • Inputs share a device, contain finite values and may be noncontiguous.
Try Similar Problems
Score Multiple-Choice ContinuationsMediumFit Elo Ratings from Pairwise VotesHardRemove Response-Length Bias from Judge ScoresHardPerplexity ComputationEasyTrain a BPE Tokenizer From Scratch in PythonMediumCanonicalize Pairwise Preference DataMediumCompute Temperature-Based Data MixturesMediumApply Ranked BPE MergesMediumEvaluation Metrics (BLEU, ROUGE)MediumAUC-ROC from ScratchMediumImplement Prefix Cache Matching and ReuseMediumN-gram Language ModelMedium

Accepts: array

Accepts: array

Accepts: array

You must run your code first.

Compute Conditional Perplexity

Metrics & EvaluationLanguage Models
Medium

Compute conditional perplexity using only the response tokens selected by the mask. Logits and target IDs are already aligned at each batch and sequence position; no token shift is needed. Let iii range over those positions, xix_ixi​ be the target ID, mim_imi​ be its mask value (one for selected, zero otherwise), and pi(xi)p_i(x_i)pi​(xi​) be its probability under the vocabulary softmax. With M=∑imiM=\sum_i m_iM=∑i​mi​ selected tokens, the negative log likelihood is:

NLL=−1M∑imilog⁡pi(xi).\mathrm{NLL}=-\frac{1}{M}\sum_i m_i\log p_i(x_i).NLL=−M1​i∑​mi​logpi​(xi​).

Perplexity is:

PPL=exp⁡(NLL).\mathrm{PPL}=\exp(\mathrm{NLL}).PPL=exp(NLL).

Here, log is the natural logarithm and exp its inverse. Average across selected tokens, not across sequences. If none are selected, use NLL zero and perplexity one. Return a Python dictionary with negative_log_likelihood and perplexity as Python floats, and selected_token_count as a Python integer.

Loading visualization...

Examples

Input: logits = [[[0,0],[0,0],[0,0]]], target_ids = [[0,1,0]], response_mask = [[false,true,true]]

Output: {"negative_log_likelihood":0.693147,"perplexity":2,"selected_token_count":2}

Explanation: Each selected token has probability one half, so the average negative log likelihood is log 2 and perplexity is 2.

Input: logits = [[[4,0],[0,4],[9,-9],[-9,9]]], target_ids = [[0,1,1,0]], response_mask = [[false,true,false,false]]

Output: {"negative_log_likelihood":0.01815,"perplexity":1.018316,"selected_token_count":1}

Input: logits = [[[1,-1],[2,-2]]], target_ids = [[0,1]], response_mask = [[false,false]]

Output: {"negative_log_likelihood":0,"perplexity":1,"selected_token_count":0}

Hint 1

torch.log_softmax computes stable log probabilities.

Hint 2

Tensor.gather selects entries using an index tensor.

Hint 3

Boolean indexing selects only the positions marked true.

Requirements

  • Use PyTorch; evaluate token log probabilities in the logits dtype and their selected-token mean in float64.
  • Prompt and padding positions marked false must not affect either metric.
  • Do not modify input tensors.

Constraints

  • logits is a float32 or float64 PyTorch tensor of shape (batch, sequence, vocabulary), with at least one vocabulary entry.
  • target_ids and response_mask are PyTorch tensors of shape (batch, sequence), with int64 and bool dtypes respectively.
  • Target IDs are valid vocabulary indices, and the mask may select no tokens.
  • Inputs must produce a perplexity representable as a finite Python float.
  • Inputs share a device, contain finite values and may be noncontiguous.
Try Similar Problems
Score Multiple-Choice ContinuationsMediumFit Elo Ratings from Pairwise VotesHardRemove Response-Length Bias from Judge ScoresHardPerplexity ComputationEasyTrain a BPE Tokenizer From Scratch in PythonMediumCanonicalize Pairwise Preference DataMediumCompute Temperature-Based Data MixturesMediumApply Ranked BPE MergesMediumEvaluation Metrics (BLEU, ROUGE)MediumAUC-ROC from ScratchMediumImplement Prefix Cache Matching and ReuseMediumN-gram Language ModelMedium

Accepts: array

Accepts: array

Accepts: array

You must run your code first.