@@ -3343,8 +3343,7 @@ def check_reclayer_optimize_out(subnet_layer_dict, other_subnet_layers=None, sha
33433343 rec_layer_dict ["unit" ].update (other_subnet_layers )
33443344 config = Config ({
33453345 "debug_print_layer_output_template" : True ,
3346- "num_inputs" : n_in ,
3347- "num_outputs" : n_out
3346+ "extern_data" : {"data" : {"dim" : n_in }},
33483347 })
33493348 from returnn .tf .layers .rec import _SubnetworkRecCell
33503349 with make_scope () as session :
@@ -3421,6 +3420,38 @@ def test_reclayer_optimize_out_selfatt_left():
34213420 "class" : "self_attention" , "attention_left_only" : True , "num_heads" : 2 , "total_key_dim" : 6 , "n_out" : 18 })
34223421
34233422
3423+ def test_reclayer_optimize_out_cum_concat_gen_self_att ():
3424+ new_dim = DimensionTag (kind = DimensionTag .Types .Spatial , description = "cum_concat_new_dim" )
3425+ n_key = 5
3426+ n_value = 7
3427+ check_reclayer_optimize_out (
3428+ {"class" : "linear" , "from" : "att" , "activation" : None },
3429+ {
3430+ # This is very much the vanilla self attention,
3431+ # implemented via the new generic way.
3432+ # See https://github.com/rwth-i6/returnn/issues/391 for a long discussion.
3433+ # Commented shapes are always for the layers inside the loop (not optimized).
3434+ "qkv" : {"class" : "linear" , "from" : "data:source" , "activation" : None , "n_out" : n_key * 2 + n_value }, # [B,2*K+V]
3435+ "qkv_split" : {"class" : "split" , "from" : "qkv" , "size_splits" : [n_key , n_key , n_value ]},
3436+ "q" : {"class" : "copy" , "from" : "qkv_split/0" }, # [B,K]
3437+ "k" : {"class" : "copy" , "from" : "qkv_split/1" }, # [B,K]
3438+ "v" : {"class" : "copy" , "from" : "qkv_split/2" }, # [B,V]
3439+ # cum_concat here. Note that the optimized-out shape is not as you might expect [T,max(t),B,K],
3440+ # but instead using the optimized format, with extended dyn size on the special dim tag.
3441+ "k_accum" : {"class" : "cum_concat" , "new_dim" : new_dim , "from" : "k" }, # [t,B,K]
3442+ "v_accum" : {"class" : "cum_concat" , "new_dim" : new_dim , "from" : "v" }, # [t,B,V]
3443+ "energy" : {
3444+ "class" : "dot" , "from" : ["q" , "k_accum" ],
3445+ "red1" : "static:-1" , "red2" : "static:-1" ,
3446+ "var1" : None , "var2" : new_dim }, # [B,t]
3447+ "att_weights" : {"class" : "softmax_over_spatial" , "from" : "energy" , "axis" : new_dim }, # [B,t]
3448+ "att" : {
3449+ "class" : "dot" , "from" : ["att_weights" , "v_accum" ],
3450+ "red1" : new_dim , "red2" : new_dim ,
3451+ "var1" : None , "var2" : "static:-1" }, # [B,V]
3452+ })
3453+
3454+
34243455def test_reclayer_optimize_out_dot ():
34253456 # Used for multi-head dot-attention.
34263457 AttNumHeads = 4
0 commit comments