unilm icon indicating copy to clipboard operation
unilm copied to clipboard

mutable variables should not be the default parameter

Open jsrdcht opened this issue 2 years ago • 2 comments

Describe the bug The problem arises from the code below: @BACKBONES.register_module() class BEiT(nn.Module): """ Vision Transformer with support for patch or hybrid CNN input stage """ def init(self, img_size=224, patch_size=16, in_chans=3, num_classes=80, embed_dim=768, depth=12, num_heads=12, mlp_ratio=4., qkv_bias=False, qk_scale=None, drop_rate=0., attn_drop_rate=0., drop_path_rate=0., hybrid_backbone=None, norm_layer=None, init_values=None, use_checkpoint=False, use_abs_pos_emb=True, use_rel_pos_bias=False, use_shared_rel_pos_bias=False, out_indices=[3, 5, 7, 11]): super().init()

Expected behavior mutable variables should not be the default parameter

jsrdcht avatar Feb 01 '23 12:02 jsrdcht

Hi @jsrdcht , could you please provide additional information, such as the specific commands being run?

addf400 avatar Feb 02 '23 06:02 addf400

if somewhere the out_indices list (a mutable object) is modified (e.g. out_indices.append(13) ), in subsequent calls to the function the default value for out_indices will be e.g. [3, 5, 7, 11, 13] instead of [3, 5, 7, 11] as originally intended.

relevant snippet from the python docs:

Default parameter values are evaluated from left to right when the function definition is executed. This means that the expression is evaluated once, when the function is defined, and that the same “pre-computed” value is used for each call. This is especially important to understand when a default parameter value is a mutable object, such as a list or a dictionary: if the function modifies the object (e.g. by appending an item to a list), the default parameter value is in effect modified. This is generally not what was intended.

https://docs.python.org/3/reference/compound_stmts.html#function-definitions

stjaco avatar Feb 02 '23 09:02 stjaco