in neuron-explainer/neuron_explainer/explanations/simulator.py [0:0]
def was_token_split(current_token: str, response_tokens: Sequence[str], start_index: int) -> bool:
"""
Return whether current_token (a token from the subject model) was split into multiple tokens by
the simulator model (as represented by the tokens in response_tokens). start_index is the index
in response_tokens at which to begin looking backward to form a complete token. It is usually
the first token *before* the delimiter that separates the token from the normalized activation,
barring some unusual cases.
This mainly happens if the subject model uses a different tokenizer than the simulator model.
But it can also happen in cases where Unicode characters are split. This function handles both
cases.
"""
merged_response_tokens = ""
merged_response_index = start_index
while len(merged_response_tokens) < len(current_token):
response_token = response_tokens[merged_response_index]
response_token, merged_response_index = handle_byte_encoding(
response_tokens, merged_response_index
)
merged_response_tokens = response_token + merged_response_tokens
merged_response_index -= 1
# It's possible that merged_response_tokens is longer than current_token at this point,
# since the between-lines delimiter may have been merged into the original token. But it
# should always be the case that merged_response_tokens ends with current_token.
assert merged_response_tokens.endswith(current_token)
num_merged_tokens = start_index - merged_response_index
token_was_split = num_merged_tokens > 1
if token_was_split:
logger.debug(
"Warning: token from the subject model was split into 2+ tokens by the simulator model."
)
return token_was_split