@@ -194,26 +194,37 @@ def topk(
194194 sorted : Optional [bool ],
195195 return_indices : bool = True ,
196196) -> Union [TRTTensor , Tuple [TRTTensor , TRTTensor ]]:
197- if largest :
198- topk_layer = ctx .net .add_topk (
199- input ,
200- trt .TopKOperation .MAX ,
201- k ,
202- get_axes_for_reduce_op (get_positive_dim (dim , len (input .shape ))),
203- )
204- else :
205- topk_layer = ctx .net .add_topk (
206- input ,
207- trt .TopKOperation .MIN ,
208- k ,
209- get_axes_for_reduce_op (get_positive_dim (dim , len (input .shape ))),
197+ # ITopKLayer requires rank >= 2 (same broadcast as argmax_argmin above).
198+ is_rank_1 = len (input .shape ) == 1
199+ if is_rank_1 :
200+ input = impl .shuffle .reshape (
201+ ctx , target , source_ir , f"{ name } _broadcast" , input , (* input .shape , 1 )
210202 )
203+ dim = 0
204+
205+ topk_layer = ctx .net .add_topk (
206+ input ,
207+ trt .TopKOperation .MAX if largest else trt .TopKOperation .MIN ,
208+ k ,
209+ get_axes_for_reduce_op (get_positive_dim (dim , len (input .shape ))),
210+ )
211211
212212 # TensorRT ITopKLayer does not have a sorted flag, it is always returning the sorted topk elements
213213 # so here no matter sorted is True or False the returned the topk Tensor object is always sorted
214214 set_layer_name (topk_layer , target , f"{ name } _topk" , source_ir )
215215
216+ values = topk_layer .get_output (0 )
217+ indices = topk_layer .get_output (1 )
218+
219+ if is_rank_1 :
220+ values = impl .shuffle .reshape (
221+ ctx , target , source_ir , f"{ name } _values_1d" , values , (- 1 ,)
222+ )
223+ indices = impl .shuffle .reshape (
224+ ctx , target , source_ir , f"{ name } _indices_1d" , indices , (- 1 ,)
225+ )
226+
216227 if return_indices :
217- return topk_layer . get_output ( 0 ), topk_layer . get_output ( 1 )
228+ return values , indices
218229 else :
219- return topk_layer . get_output ( 0 )
230+ return values
0 commit comments