{"record":{"id":"50f0fe3500ba84d9","repo":"Unity-Technologies/ml-agents","slug":"when-using-memory-sequence-length-must-be-less-th","errorCode":null,"errorMessage":"When using memory, sequence length must be less than or equal to batch size. ","messagePattern":"When using memory, sequence length must be less than or equal to batch size\\. ","errorType":"validation","errorClass":"TrainerConfigError","httpStatus":null,"severity":"error","filePath":"ml-agents/mlagents/trainers/settings.py","lineNumber":655,"sourceCode":"    time_horizon: int = 64\n    summary_freq: int = 50000\n    threaded: bool = False\n    self_play: Optional[SelfPlaySettings] = None\n    behavioral_cloning: Optional[BehavioralCloningSettings] = None\n\n    cattr.register_structure_hook_func(\n        lambda t: t == Dict[RewardSignalType, RewardSignalSettings],\n        RewardSignalSettings.structure,\n    )\n\n    @network_settings.validator\n    def _check_batch_size_seq_length(self, attribute, value):\n        if self.network_settings.memory is not None:\n            if (\n                self.network_settings.memory.sequence_length\n                > self.hyperparameters.batch_size\n            ):\n                raise TrainerConfigError(\n                    \"When using memory, sequence length must be less than or equal to batch size. \"\n                )\n\n    @checkpoint_interval.validator\n    def _set_checkpoint_interval(self, attribute, value):\n        if self.even_checkpoints:\n            self.checkpoint_interval = int(self.max_steps / self.keep_checkpoints)\n\n    @staticmethod\n    def dict_to_trainerdict(d: Dict, t: type) -> \"TrainerSettings.DefaultTrainerDict\":\n        return TrainerSettings.DefaultTrainerDict(\n            cattr.structure(d, Dict[str, TrainerSettings])\n        )\n\n    @staticmethod\n    def structure(d: Mapping, t: type) -> Any:\n        \"\"\"\n        Helper method to structure a TrainerSettings class. Meant to be registered with","sourceCodeStart":637,"sourceCodeEnd":673,"githubUrl":"https://github.com/Unity-Technologies/ml-agents/blob/3ecb446f75d1e7400eb404c562dc005d3164cffc/ml-agents/mlagents/trainers/settings.py#L637-L673","documentation":"When NetworkSettings.memory is enabled (LSTM/memory), the sequence_length must not exceed hyperparameters.batch_size, otherwise batches cannot be formed correctly. attrs validators run at config load time and raise TrainerConfigError.","triggerScenarios":"Config sets network_settings.memory.sequence_length greater than hyperparameters.batch_size (e.g. batch_size: 64 with sequence_length: 128).","commonSituations":"Tuning memory settings for partially observable tasks and increasing sequence_length without touching batch_size; copying sequence_length values from another config with larger batch sizes.","solutions":["Increase hyperparameters.batch_size to be >= network_settings.memory.sequence_length.","Lower memory.sequence_length to be <= batch_size.","Remove the memory block if recurrent memory is not needed."],"exampleFix":"// before\nbatch_size: 64\nnetwork_settings:\n  memory:\n    sequence_length: 128\n// after\nbatch_size: 128\nnetwork_settings:\n  memory:\n    sequence_length: 128","handlingStrategy":"validation","validationCode":"if hp and net.get('memory'):\n    seq_len = net['memory']['sequence_length']\n    if seq_len > hp['batch_size']:\n        raise ValueError(f'sequence_length {seq_len} must be <= batch_size {hp[\"batch_size\"]}')","typeGuard":"def memory_ok(batch_size, memory):\n    return memory is None or memory.get('sequence_length', 0) <= batch_size","tryCatchPattern":"from mlagents.trainers.exception import TrainerConfigError\ntry:\n    TrainerSettings.structure(config)\nexcept TrainerConfigError as e:\n    if 'sequence length' in str(e).lower():\n        config['hyperparameters']['batch_size'] = config['network_settings']['memory']['sequence_length']","preventionTips":["When tuning sequence_length, raise batch_size to match","Keep memory.sequence_length <= batch_size in every config","Add a pre-training config check comparing the two fields"],"tags":["config","validation","ml-agents"],"backgroundTag":"invalid-config-threshold","analyzedSha":"3ecb446f75d1e7400eb404c562dc005d3164cffc","analyzedAt":"2026-09-02T16:33:12.832Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T21:17:11.164Z"}