@@ -3961,34 +3961,57 @@ def aten_ops_linear(
39613961 )
39623962
39633963
3964- def scaled_dot_product_attention_validator (
3965- node : Node , settings : Optional [CompilationSettings ] = None
3966- ) -> bool :
3967- if node .kwargs .get ("enable_gqa" , False ):
3968- _LOGGER .debug (
3969- "enable_gqa is not yet supported by the converter. Please try setting decompose_attention=True in the compilation settings."
3970- )
3971- return False
3972-
3964+ def _attention_qkv_shapes_supported (node : Node ) -> bool :
39733965 query_shape , key_shape , value_shape = None , None , None
39743966 if "val" in node .args [0 ].meta :
39753967 query_shape = node .args [0 ].meta ["val" ].size ()
39763968 if "val" in node .args [1 ].meta :
39773969 key_shape = node .args [1 ].meta ["val" ].size ()
39783970 if "val" in node .args [2 ].meta :
39793971 value_shape = node .args [2 ].meta ["val" ].size ()
3980- if (
3981- query_shape != key_shape
3982- or query_shape != value_shape
3983- or key_shape != value_shape
3984- ):
3972+
3973+ # If shape metadata is unavailable, defer to runtime/converter checks.
3974+ if query_shape is None or key_shape is None or value_shape is None :
3975+ return True
3976+
3977+ if len (query_shape ) != len (key_shape ) or len (query_shape ) != len (value_shape ):
39853978 _LOGGER .debug (
3986- "query, key, and value have different shapes . Please try setting decompose_attention=True in the compilation settings."
3979+ "query, key, and value must have the same rank . Please try setting decompose_attention=True in the compilation settings."
39873980 )
39883981 return False
3982+
3983+ # TensorRT IAttention layer supports different sequence lengths for query and key/value
3984+ # ([B, Nq, Sq, H] vs [B, Nkv, Skv, H]), but K and V must still agree on all dims.
3985+ seq_dim = len (query_shape ) - 2
3986+ for dim , (query_dim , key_dim , value_dim ) in enumerate (
3987+ zip (query_shape , key_shape , value_shape )
3988+ ):
3989+ if dim == seq_dim :
3990+ if key_dim != value_dim :
3991+ _LOGGER .debug (
3992+ "key and value must have the same sequence length. Please try setting decompose_attention=True in the compilation settings."
3993+ )
3994+ return False
3995+ else :
3996+ if query_dim != key_dim or query_dim != value_dim or key_dim != value_dim :
3997+ _LOGGER .debug (
3998+ "query, key, and value differ on a non-sequence dimension. Please try setting decompose_attention=True in the compilation settings."
3999+ )
4000+ return False
39894001 return True
39904002
39914003
4004+ def scaled_dot_product_attention_validator (
4005+ node : Node , settings : Optional [CompilationSettings ] = None
4006+ ) -> bool :
4007+ if node .kwargs .get ("enable_gqa" , False ):
4008+ _LOGGER .debug (
4009+ "enable_gqa is not yet supported by the converter. Please try setting decompose_attention=True in the compilation settings."
4010+ )
4011+ return False
4012+ return _attention_qkv_shapes_supported (node )
4013+
4014+
39924015@dynamo_tensorrt_converter (
39934016 torch .ops .aten .scaled_dot_product_attention .default ,
39944017 supports_dynamic_shapes = True ,
@@ -4024,24 +4047,7 @@ def scaled_dot_product_flash_attention_validator(
40244047 if args_bounds_check (node .args , 5 , False ):
40254048 _LOGGER .debug ("return_debug_mask is not yet supported." )
40264049 return False
4027-
4028- query_shape , key_shape , value_shape = None , None , None
4029- if "val" in node .args [0 ].meta :
4030- query_shape = node .args [0 ].meta ["val" ].size ()
4031- if "val" in node .args [1 ].meta :
4032- key_shape = node .args [1 ].meta ["val" ].size ()
4033- if "val" in node .args [2 ].meta :
4034- value_shape = node .args [2 ].meta ["val" ].size ()
4035- if (
4036- query_shape != key_shape
4037- or query_shape != value_shape
4038- or key_shape != value_shape
4039- ):
4040- _LOGGER .debug (
4041- "query, key, and value have different shapes. Please try setting decompose_attention=True in the compilation settings."
4042- )
4043- return False
4044- return True
4050+ return _attention_qkv_shapes_supported (node )
40454051
40464052
40474053@dynamo_tensorrt_converter (
@@ -4078,24 +4084,7 @@ def scaled_dot_product_efficient_attention_validator(
40784084 if args_bounds_check (node .args , 4 , False ):
40794085 _LOGGER .debug ("compute_log_sumexp is not yet supported." )
40804086 return False
4081-
4082- query_shape , key_shape , value_shape = None , None , None
4083- if "val" in node .args [0 ].meta :
4084- query_shape = node .args [0 ].meta ["val" ].size ()
4085- if "val" in node .args [1 ].meta :
4086- key_shape = node .args [1 ].meta ["val" ].size ()
4087- if "val" in node .args [2 ].meta :
4088- value_shape = node .args [2 ].meta ["val" ].size ()
4089- if (
4090- query_shape != key_shape
4091- or query_shape != value_shape
4092- or key_shape != value_shape
4093- ):
4094- _LOGGER .debug (
4095- "query, key, and value have different shapes. Please try setting decompose_attention=True in the compilation settings."
4096- )
4097- return False
4098- return True
4087+ return _attention_qkv_shapes_supported (node )
40994088
41004089
41014090@dynamo_tensorrt_converter (
0 commit comments