{"record":{"id":"e9c18efa47a653f9","repo":"Unity-Technologies/ml-agents","slug":"trying-to-export-an-attention-mechanism-that-doesn","errorCode":null,"errorMessage":"Trying to export an attention mechanism that doesn't have a set                     number of elements.","messagePattern":"Trying to export an attention mechanism that doesn't have a set                     number of elements\\.","errorType":"exception","errorClass":"UnityTrainerException","httpStatus":null,"severity":"error","filePath":"ml-agents/mlagents/trainers/torch_entities/attention.py","lineNumber":158,"sourceCode":"            kernel_init=Initialization.Normal,\n            kernel_gain=(0.125 / self.embedding_size) ** 0.5,\n        )\n\n    def add_self_embedding(self, size: int) -> None:\n        self.self_size = size\n        self.self_ent_encoder = LinearEncoder(\n            self.self_size + self.entity_size,\n            1,\n            self.embedding_size,\n            kernel_init=Initialization.Normal,\n            kernel_gain=(0.125 / self.embedding_size) ** 0.5,\n        )\n\n    def forward(self, x_self: torch.Tensor, entities: torch.Tensor) -> torch.Tensor:\n        num_entities = self.entity_num_max_elements\n        if num_entities < 0:\n            if exporting_to_onnx.is_exporting():\n                raise UnityTrainerException(\n                    \"Trying to export an attention mechanism that doesn't have a set max \\\n                    number of elements.\"\n                )\n            num_entities = entities.shape[1]\n\n        if self.self_size > 0:\n            expanded_self = x_self.reshape(-1, 1, self.self_size)\n            expanded_self = torch.cat([expanded_self] * num_entities, dim=1)\n            # Concatenate all observations with self\n            entities = torch.cat([expanded_self, entities], dim=2)\n        # Encode entities\n        encoded_entities = self.self_ent_encoder(entities)\n        return encoded_entities\n\n\nclass ResidualSelfAttention(torch.nn.Module):\n    \"\"\"\n    Residual self attentioninspired from https://arxiv.org/pdf/1909.07528.pdf. Can be used","sourceCodeStart":140,"sourceCodeEnd":176,"githubUrl":"https://github.com/Unity-Technologies/ml-agents/blob/3ecb446f75d1e7400eb404c562dc005d3164cffc/ml-agents/mlagents/trainers/torch_entities/attention.py#L140-L176","documentation":"A guard inside MultiHeadAttention's export path (forward/onnx export logic) that fires when entity_num_max_element was never set, so the attention module does not know the fixed number of entity elements required to build the ONNX graph. It is a sentinel error for an uninitialized setup step: the attention resolver was exported before the entity observations' sizes were recorded (self.entity_num_max_element is None), which makes static-shape export impossible.","triggerScenarios":"Thrown at ml-agents/mlagents/trainers/torch_entities/attention.py:158 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Ensure the model export (.onnx) is run after the network has processed at least one batch of real observations so entity sizes are known","Check that entity sensors are configured on the agent so the attention module receives entity observations","Upgrade ml-agents; export-shape handling for entity/attention networks has fixes in later releases"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"3ecb446f75d1e7400eb404c562dc005d3164cffc","analyzedAt":"2026-09-02T16:33:12.832Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T21:17:11.164Z"}