Before generating new tokens with an autoregressive language model, you must make sure the prompt plus the tokens you plan to generate fits inside the model's fixed context window. If the prompt is too long, the standard fix is to drop tokens from the LEFT (the oldest tokens), keeping the most recent tokens that are most relevant for continuation.
Implement a function trim_to_context(input_ids, context_len, max_new_tokens) that takes:
input_ids: a 2D array-like of shape (batch_size, seq_len) containing integer token IDs.context_len: int, the model's maximum supported context length (prompt + generated tokens combined).max_new_tokens: int, the number of tokens that will be generated after the prompt.The function must return a 2D numpy array (converted via .tolist() for printing in tests) such that the returned sequence length L satisfies L + max_new_tokens <= context_len. If the original input already satisfies this, return it unchanged. Otherwise, keep only the rightmost allowed = context_len - max_new_tokens columns of input_ids.
Edge cases:
max_new_tokens >= context_len, there is no room for any prompt tokens, so return an array of shape (batch_size, 0) as a list of empty lists.max_new_tokens < 0 or context_len <= 0, raise a ValueError.Return a Python list (the .tolist() of the trimmed numpy array).