{"record":{"id":"9fbd80250db090d1","repo":"hankcs/HanLP","slug":"attn-output-should-be-of-size-bsz-self-num-he","errorCode":null,"errorMessage":"`attn_output` should be of size {(bsz, self.num_heads, tgt_len, self.head_dim)}, but is {attn_output.size()}","messagePattern":"`attn_output` should be of size (.+?), but is (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"hanlp/components/amr/amrbart/model_interface/modeling_bart.py","lineNumber":270,"sourceCode":"            attn_weights = layer_head_mask.view(1, -1, 1, 1) * attn_weights.view(bsz, self.num_heads, tgt_len, src_len)\n            attn_weights = attn_weights.view(bsz * self.num_heads, tgt_len, src_len)\n\n        if output_attentions:\n            # this operation is a bit awkward, but it's required to\n            # make sure that attn_weights keeps its gradient.\n            # In order to do so, attn_weights have to be reshaped\n            # twice and have to be reused in the following\n            attn_weights_reshaped = attn_weights.view(bsz, self.num_heads, tgt_len, src_len)\n            attn_weights = attn_weights_reshaped.view(bsz * self.num_heads, tgt_len, src_len)\n        else:\n            attn_weights_reshaped = None\n\n        attn_probs = nn.functional.dropout(attn_weights, p=self.dropout, training=self.training)\n\n        attn_output = torch.bmm(attn_probs, value_states)\n\n        if attn_output.size() != (bsz * self.num_heads, tgt_len, self.head_dim):\n            raise ValueError(\n                f\"`attn_output` should be of size {(bsz, self.num_heads, tgt_len, self.head_dim)}, but is\"\n                f\" {attn_output.size()}\"\n            )\n\n        attn_output = attn_output.view(bsz, self.num_heads, tgt_len, self.head_dim)\n        attn_output = attn_output.transpose(1, 2)\n\n        # Use the `embed_dim` from the config (stored in the class) rather than `hidden_state` because `attn_output` can be\n        # partitioned aross GPUs when using tensor-parallelism.\n        attn_output = attn_output.reshape(bsz, tgt_len, self.embed_dim)\n\n        attn_output = self.out_proj(attn_output)\n\n        return attn_output, attn_weights_reshaped, past_key_value\n\n\nclass BartEncoderLayer(nn.Module):\n    def __init__(self, config: BartConfig):","sourceCodeStart":252,"sourceCodeEnd":288,"githubUrl":"https://github.com/hankcs/HanLP/blob/ddb1299bddff079e447af52ec12549c50636bfa8/hanlp/components/amr/amrbart/model_interface/modeling_bart.py#L252-L288","documentation":"WeightNormalization (Keras-style) needs to reparameterize the layer's weight matrix, which it locates via the kernel attribute (or layer.cell.kernel for RNNs). Wrapping a layer type that stores weights under a different attribute (e.g. Embedding or custom layers) raises this ValueError in build().","triggerScenarios":"Wrapping a Keras layer without a kernel attribute (many custom layers, Embedding in some versions) with WeightNormalization and calling build/forward on it.","commonSituations":"Porting weight-norm configs between layer types; wrapping Lambda/custom layers; Keras version changes where layer internals moved off .kernel; wrapping RNNs where cell detection (is_rnn) fails so it looks for kernel on the wrong object.","solutions":["Wrap only kernel-based layers (Dense, Conv1D/2D, RNN cells)","For custom layers, expose the weight matrix as self.kernel so the wrapper can find it","Use torch's built-in torch.nn.utils.weight_norm for PyTorch modules instead of this Keras-style wrapper"],"exampleFix":"# before\nwn = WeightNormalization(MyCustomLayer(...))\n# after\nclass MyCustomLayer(nn.Module):\n    def __init__(...):\n        self.kernel = nn.Parameter(...)  # expose kernel\nwn = WeightNormalization(MyCustomLayer())","handlingStrategy":"type-guard","validationCode":"assert hasattr(layer.cell if is_rnn else layer, 'kernel'), 'layer must expose .kernel for WeightNormalization'","typeGuard":"def wrappable(layer, is_rnn=False) -> bool:\n    return hasattr(layer.cell if is_rnn else layer, 'kernel')","tryCatchPattern":"try:\n    wn = WeightNormalization(layer); wn.build(input_shape)\nexcept ValueError as e:\n    raise ValueError(f'cannot weight-normalize {type(layer).__name__}: {e}') from e","preventionTips":["Only wrap kernel-based layers (Dense/Conv/RNN cells)","Expose weights as self.kernel in custom layers","Consider torch.nn.utils.weight_norm for native modules"],"tags":["hanlp","weight-normalization","keras","layer-wrapper"],"backgroundTag":"unsupported-layer-wrapping","analyzedSha":"ddb1299bddff079e447af52ec12549c50636bfa8","analyzedAt":"2026-08-27T03:36:54.287Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}