@@ -3451,6 +3451,43 @@ def test_reclayer_optimize_out_dot():
34513451 rtol = 1e-3 )
34523452
34533453
3454+ def test_reclayer_optimize_out_dot_consistent_axes ():
3455+ # https://github.com/rwth-i6/returnn/issues/569
3456+ # Used for multi-head dot-attention.
3457+ n_heads = 4
3458+ n_key = 5
3459+ n_value = 7
3460+ n_key_total = n_heads * n_key
3461+ n_value_total = n_heads * n_value
3462+ check_reclayer_optimize_out (
3463+ {"class" : "linear" , "activation" : None , "from" : "att" },
3464+ other_subnet_layers = {
3465+ "s" : {"class" : "linear" , "activation" : None , "with_bias" : False , "from" : "data:source" ,
3466+ "n_out" : n_key_total }, # (B, D) -- Q (query). D should be same as enc_ctx
3467+ "att_query" : {"class" : "split_dims" , "axis" : "F" , "dims" : (n_heads , n_key ), "from" : "s" }, # (B, H, D/H)
3468+ # Here is the main test, the dot-layer:
3469+ "energy" : {"class" : "dot" , "red1" : - 1 , "red2" : - 1 , "var1" : "T" , "var2" : "T" ,
3470+ "from" : ["base:enc_ctx" , "att_query" ]},
3471+ # energy inside the loop will be (B, H, enc-T, 1).
3472+ # energy outside the loop will be (B, H, enc-T, dec-T). I.e. enc-T is still the first time axis.
3473+ "att_weights" : {"class" : "softmax_over_spatial" , "from" : "energy" }, # (B, enc-T, H, 1)
3474+ "att0" : {"class" : "generic_attention" , "weights" : "att_weights" , "base" : "base:enc_value" }, # (B, H, V)
3475+ "att" : {"class" : "merge_dims" , "axes" : "static" , "from" : "att0" }, # (B, H*V); Use "static" here.
3476+ },
3477+ shared_base_net = {
3478+ "encoder" : {"class" : "copy" , "from" : "data" },
3479+ "enc_ctx0" : {"class" : "linear" , "activation" : None , "with_bias" : False , "from" : "encoder" ,
3480+ "n_out" : n_key_total }, # (B, enc-T, D)
3481+ "enc_ctx" : {"class" : "split_dims" , "axis" : "F" , "dims" : (n_heads , n_key ),
3482+ "from" : "enc_ctx0" , "is_output_layer" : True }, # (B, enc-T, H, D/H)
3483+ "enc_value0" : {"class" : "linear" , "activation" : None , "with_bias" : False , "from" : "encoder" ,
3484+ "n_out" : n_value_total },
3485+ "enc_value" : {"class" : "split_dims" , "axis" : "F" , "dims" : (n_heads , n_value ),
3486+ "from" : "enc_value0" , "is_output_layer" : True }, # (B, enc-T, H, D/H)
3487+ },
3488+ rtol = 1e-3 )
3489+
3490+
34543491def test_reclayer_optimize_out_dot_kv_in_rec ():
34553492 # Same as test_reclayer_optimize_out_dot, but with the att key/value layers declared INSIDE the rec layer.
34563493 AttNumHeads = 4
0 commit comments