{"record":{"id":"822432989f748c40","repo":"WZMIAOMIAO/deep-learning-for-image-processing","slug":"illegal-stride-value-822432","errorCode":null,"errorMessage":"illegal stride value.","messagePattern":"illegal stride value\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pytorch_classification/model_complexity/model.py","lineNumber":146,"sourceCode":"        cx[\"h\"], cx[\"w\"] = h, w\n\n        return cx\n\n\nclass MBConv(nn.Module):\n    def __init__(self,\n                 kernel_size: int,\n                 input_c: int,\n                 out_c: int,\n                 expand_ratio: int,\n                 stride: int,\n                 se_ratio: float,\n                 drop_rate: float,\n                 norm_layer: Callable[..., nn.Module]):\n        super(MBConv, self).__init__()\n\n        if stride not in [1, 2]:\n            raise ValueError(\"illegal stride value.\")\n\n        self.has_shortcut = (stride == 1 and input_c == out_c)\n\n        activation_layer = nn.SiLU  # alias Swish\n        expanded_c = input_c * expand_ratio\n\n        # 在EfficientNetV2中，MBConv中不存在expansion=1的情况所以conv_pw肯定存在\n        assert expand_ratio != 1\n        # Point-wise expansion\n        self.expand_conv = ConvBNAct(input_c,\n                                     expanded_c,\n                                     kernel_size=1,\n                                     norm_layer=norm_layer,\n                                     activation_layer=activation_layer)\n\n        # Depth-wise convolution\n        self.dwconv = ConvBNAct(expanded_c,\n                                expanded_c,","sourceCodeStart":128,"sourceCodeEnd":164,"githubUrl":"https://github.com/WZMIAOMIAO/deep-learning-for-image-processing/blob/1ec3fe6f374fc9969973a61f819de25658595afa/pytorch_classification/model_complexity/model.py#L128-L164","documentation":"EfficientNet's MBConv block supports only stride 1 or 2 (identity or downsampling); the constructor validates the stride parameter and raises ValueError for anything else. The stride determines whether a shortcut connection and pooling/strided conv are built, so other values are unsupported.","triggerScenarios":"Constructing MBConv (or an EfficientNet variant whose block config contains a stride) with a stride value outside [1, 2], e.g. 0, 3, or a float from a malformed config.","commonSituations":"Hand-editing the stage/block config list, importing stride settings from another architecture, or a typo when defining a custom EfficientNet variant.","solutions":["Set every block's stride to 1 or 2 in the config.","Stride 1 requires input_c == out_c for a shortcut; use stride 2 when changing channels/downsampling.","Copy the canonical B0-B7 configs (e.g. [[1,16,1,1],[6,24,2,2],[6,40,2,2],...]) rather than editing stride fields ad hoc."],"exampleFix":"// before\nparams = [[1, 16, 1, 3], [6, 24, 2, 3], ...]  # stride 3 in first block\n// after\nparams = [[1, 16, 1, 3], [6, 24, 2, 3], ...]  # strides must be 1 or 2 only; fix [k_c, out_c, s, n]: stride s in {1,2}","handlingStrategy":"validation","validationCode":"strides = [p[2] for p in block_params]\nassert all(s in (1, 2) for s in strides), f\"block strides must be 1 or 2, got {strides}\"","typeGuard":"def is_legal_stride(s) -> bool:\n    return s in (1, 2)","tryCatchPattern":"try:\n    block = MBConv(input_c=input_c, out_c=out_c, stride=stride, expand_ratio=e, se_ratio=se, drop_rate=dr, norm_layer=nn.BatchNorm2d)\nexcept ValueError as e:\n    logging.error(\"MBConv config error: %s\", e)\n    raise","preventionTips":["Only use 1 (keep channels, optional shortcut) or 2 (downsample) for block strides","Copy canonical EfficientNet B0-B7 config tables","Type-check configs so stride is an int, not a float/string","Remember stride-1 shortcut requires input_c == out_c"],"tags":["python","pytorch","valueerror","model-config","efficientnet"],"backgroundTag":"invalid-model-config","analyzedSha":"1ec3fe6f374fc9969973a61f819de25658595afa","analyzedAt":"2026-08-30T09:19:11.901Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}