{"record":{"id":"c6ca544dab9e201c","repo":"google-research/timesfm","slug":"incompatible-input-dimension-got-input-in-featur","errorCode":null,"errorMessage":"Incompatible input dimension, got {input_in_features} but module expects {self.in_features}.","messagePattern":"Incompatible input dimension, got (.+?) but module expects (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/timesfm/flax/transformer.py","lineNumber":219,"sourceCode":"\n    if use_per_dim_scale:\n      self.per_dim_scale = PerDimScale(num_dims=self.head_dim, rngs=rngs)\n    else:\n      self.per_dim_scale = None\n\n  def __call__(\n    self,\n    inputs_q: Array,\n    *,\n    decode_cache: DecodeCache | None = None,\n    patch_mask: Array | None = None,\n    deterministic: bool | None = None,\n    sow_weights: bool = False,\n  ) -> tuple[Float[Array, \"b ... o\"], DecodeCache | None]:\n    \"\"\"Applies multi-head dot product attention on the input data.\"\"\"\n    _, n_patches, input_in_features = inputs_q.shape\n    if input_in_features != self.in_features:\n      raise ValueError(\n        f\"Incompatible input dimension, got {input_in_features} \"\n        f\"but module expects {self.in_features}.\"\n      )\n    if patch_mask is None:\n      patch_mask = jnp.zeros_like(inputs_q.shape[:-1], dtype=jnp.bool)\n\n    # For query: rope -> ln -> per_dim_scale\n    query = self.query(inputs_q)\n    key = self.key(inputs_q)\n    value = self.value(inputs_q)\n\n    if decode_cache is None:\n      num_masked = jnp.sum(patch_mask.astype(jnp.int32), axis=-1, keepdims=False)\n      next_index = jnp.zeros_like(num_masked, dtype=jnp.int32)\n    else:\n      num_masked = (\n        jnp.sum(patch_mask.astype(jnp.int32), axis=-1, keepdims=False)\n        + decode_cache.num_masked","sourceCodeStart":201,"sourceCodeEnd":237,"githubUrl":"https://github.com/google-research/timesfm/blob/331c6d33cb1ac2611de3056d0ac7164aab6301eb/src/timesfm/flax/transformer.py#L201-L237","documentation":"MultiHeadAttention.__call__ unpacks inputs_q.shape and verifies the last (feature) axis equals the in_features the projection layers were built with. A mismatch means the input tensor's hidden size differs from what the attention module was initialized for.","triggerScenarios":"Feeding a tensor whose last dimension differs from self.in_features into attention __call__ — e.g. passing patch embeddings of the wrong width, changing model_dims after layers were constructed, or wiring an intermediate tensor into the wrong attention layer.","commonSituations":"Pipeline wiring mistakes (feeding the wrong tensor), checkpoint/config mismatch where weights expect a different hidden size, or reshaping errors upstream that alter the feature axis.","solutions":["Make the input's last dimension match in_features (check the tensor just before the attention call).","Rebuild the attention module (and config) with in_features equal to the actual input width if the new size is intended.","Verify the checkpoint and config model_dims agree when loading pretrained weights."],"exampleFix":"// before\nattn = MultiHeadAttention(in_features=128, ...)\ny = attn(x)  # x.shape[-1] == 64 -> ValueError\n// after\nx = nnx.Linear(64, 128)(x)  # project to 128 first\ny = attn(x)","handlingStrategy":"validation","validationCode":"assert inputs.shape[-1] == attn.in_features, (\n    f\"input feature dim {inputs.shape[-1]} != attention in_features {attn.in_features}\")","typeGuard":"def fits_attention(attn, inputs) -> bool:\n    return inputs.ndim >= 3 and inputs.shape[-1] == attn.in_features","tryCatchPattern":"try:\n    out = attn(x)\nexcept ValueError as e:\n    if \"Incompatible input dimension\" in str(e):\n        x = project_to(x, attn.in_features)  # add/fix projection layer\n        out = attn(x)\n    else:\n        raise","preventionTips":["Derive every layer's in_features from one config field (model_dims).","Check tensor shapes at each pipeline hop when wiring modules manually.","Verify checkpoint architecture params match the constructed config before loading weights."],"tags":["shape","attention","validation","dimension-mismatch"],"backgroundTag":"dimension-mismatch","analyzedSha":"331c6d33cb1ac2611de3056d0ac7164aab6301eb","analyzedAt":"2026-08-29T01:04:23.138Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}