rohitg00/ai-engineering-from-scratch · error · ValueError
max_len must be >= 2 to fit INST and RESP
Error message
max_len must be >= 2 to fit INST and RESP
What it means
Error "max_len must be >= 2 to fit INST and RESP" thrown in rohitg00/ai-engineering-from-scratch.
Source
Thrown at phases/19-capstone-projects/39-instruction-tuning-sft/code/main.py:66
needed but always keeps the RESP marker plus at least one response
token so SFT collation never produces fully-masked labels."""
if max_len < 3:
raise ValueError("max_len must be >= 3 to fit INST, RESP, and one response token")
inst_bytes = list(instruction.encode("utf-8", errors="ignore"))
resp_bytes = list(response.encode("utf-8", errors="ignore"))
# Reserve 2 control tokens + at least 1 response byte.
max_inst = max_len - 3
inst_bytes = inst_bytes[:max_inst]
ids = [self.INST_ID] + inst_bytes + [self.RESP_ID]
resp_start = len(ids)
ids.extend(resp_bytes[: max_len - len(ids)])
return ids, resp_start
def encode_prefix(self, instruction: str, max_len: int) -> List[int]:
"""Encode just the instruction prefix for generation. Always keeps the
RESP marker so the model sees the same boundary as during training."""
if max_len < 2:
raise ValueError("max_len must be >= 2 to fit INST and RESP")
inst_bytes = list(instruction.encode("utf-8", errors="ignore"))[: max_len - 2]
ids = [self.INST_ID] + inst_bytes + [self.RESP_ID]
return ids
def decode_response(self, ids: Sequence[int]) -> str:
"""Decode a generated response, dropping specials."""
chunk = bytes(i for i in ids if i < 256)
return chunk.decode("utf-8", errors="replace")
# ---------------------------------------------------------------------------
# Tiny GPT
# ---------------------------------------------------------------------------
class CausalSelfAttention(nn.Module):
def __init__(self, hidden: int, heads: int, max_len: int):
super().__init__()View on GitHub (pinned to 39ea8a1c6d)
When it happens
Trigger: Thrown at phases/19-capstone-projects/39-instruction-tuning-sft/code/main.py:66 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of rohitg00/ai-engineering-from-scratch@39ea8a1c6d (2026-08-26).
Data as JSON: /api/errors/e6b4a3f84f9644b3.
Report an issue: GitHub.