MCPcopy
hub / github.com/NVIDIA/TensorRT-LLM / where

Function where

tensorrt_llm/functional.py:3043–3099  ·  view source on GitHub ↗

Add a where (aka select or if-then-else) operation. Assuming the three input parameters have the same shape, that function creates the operation to compute a tensor of the same shape such that: for ii in range(mul(condition.shape)): output[ii] = left[ii] if conditi

(condition: Union[Tensor, bool], left: Union[Tensor, int, float],
          right: Union[Tensor, int, float])

Source from the content-addressed store, hash-verified

3041
3042
3043def where(condition: Union[Tensor, bool], left: Union[Tensor, int, float],
3044 right: Union[Tensor, int, float]) -> Tensor:
3045 '''
3046 Add a where (aka select or if-then-else) operation.
3047
3048 Assuming the three input parameters have the same shape, that function creates
3049 the operation to compute a tensor of the same shape such that:
3050
3051 for ii in range(mul(condition.shape)):
3052 output[ii] = left[ii] if condition[ii] else right[ii]
3053
3054 For each input, that function first creates a constant tensor if the
3055 condition is boolean or the left/right input is an integer or a float.
3056 Then, if needed, it expands the smaller tensor to make sure its
3057 rank is the same as the larger one. Then, it performs the selection.
3058
3059 It is implemented using the ISelectLayer from TensorRT.
3060
3061 Parameters:
3062 condition : Union[Tensor, bool]
3063 The condition. If that input is a boolean, the function
3064 creates a constant tensor.
3065
3066 left : Union[Tensor, int, float]
3067 The first input. If that input is an integer or a float, the
3068 function creates a constant tensor.
3069
3070 right : Union[Tensor, int, float]
3071 The second input. If that input is an integer or a float, the
3072 function creates a constant tensor.
3073
3074 Returns:
3075 The tensor produced by this where operation.
3076 '''
3077 # Convert to tensors.
3078 condition = constant_to_tensor_(condition)
3079 left, right = constants_to_tensors_(left, right)
3080
3081 # Find the tensor with the largest rank of the three.
3082 largest = condition
3083 if largest.rank() < left.rank():
3084 largest = left
3085 if largest.rank() < right.rank():
3086 largest = right
3087
3088 # Expand the tensors to match the largest one.
3089 if condition is not largest:
3090 condition = expand_dims_like(condition, largest)
3091 if left is not largest:
3092 left = expand_dims_like(left, largest)
3093 if right is not largest:
3094 right = expand_dims_like(right, largest)
3095
3096 # Insert the operation.
3097 layer = default_trtnet().add_select(condition.trt_tensor, left.trt_tensor,
3098 right.trt_tensor)
3099 return _create_tensor(layer.get_output(0), layer)
3100

Callers 15

warp_logitsFunction · 0.90
_prepare_drafter_inputFunction · 0.90
embeddingFunction · 0.85
softplusFunction · 0.85
expand_maskFunction · 0.85
forwardMethod · 0.85
forwardMethod · 0.85
mask_and_softmaxMethod · 0.85
make_causal_maskFunction · 0.85
forwardMethod · 0.85
forwardMethod · 0.85

Calls 7

constant_to_tensor_Function · 0.85
constants_to_tensors_Function · 0.85
expand_dims_likeFunction · 0.85
default_trtnetFunction · 0.85
_create_tensorFunction · 0.85
rankMethod · 0.45
get_outputMethod · 0.45

Tested by

no test coverage detected