def truncate_seq_pair(tokens_a, max_num_tokens, rng):
    """Truncates a pair of sequences to a maximum sequence length."""
    while True:
        total_length = len(tokens_a)
        if total_length <= max_num_tokens:
            return tokens_a
        trunc_tokens = tokens_a
        assert len(trunc_tokens) >= 1
        if rng.random() < 0.5:
            tokens_a = trunc_tokens[1:]
        else:
            tokens_a = trunc_tokens[:-1]
