2020
2121from nv_ingest_api .internal .primitives .nim import ModelInterface
2222import tritonclient .grpc as grpcclient
23+ from nv_ingest_api .internal .primitives .nim .model_interface .decorators import global_cache
24+ from nv_ingest_api .internal .primitives .nim .model_interface .decorators import lock
2325from nv_ingest_api .internal .primitives .nim .model_interface .decorators import multiprocessing_cache
2426from nv_ingest_api .internal .primitives .nim .model_interface .helpers import get_model_name
2527from nv_ingest_api .util .image_processing import scale_image_to_encoding_size
@@ -135,10 +137,36 @@ def __init__(
135137 self .class_labels = class_labels
136138
137139 if endpoints :
138- self .model_name = get_yolox_model_name (endpoints [0 ], default_model_name = "yolox_ensemble" )
139- self ._grpc_uses_bls = self .model_name == "pipeline"
140+ self ._yolox_grpc_endpoint = endpoints [0 ]
141+ self ._model_name = None
142+ self ._grpc_uses_bls_value = None # Resolved on first use
140143 else :
141- self ._grpc_uses_bls = False
144+ self ._yolox_grpc_endpoint = None
145+ self ._model_name = None
146+ self ._grpc_uses_bls_value = False
147+
148+ def _resolve_yolox_model_name_if_needed (self ) -> None :
149+ """Resolve model name and BLS flag from the gRPC endpoint on first use. Cached on the instance."""
150+ if self ._yolox_grpc_endpoint is None :
151+ return
152+ if self ._model_name is not None :
153+ return
154+ self ._model_name = get_yolox_model_name (self ._yolox_grpc_endpoint , default_model_name = "yolox_ensemble" )
155+ self ._grpc_uses_bls_value = self ._model_name == "pipeline"
156+
157+ @property
158+ def model_name (self ) -> Optional [str ]:
159+ self ._resolve_yolox_model_name_if_needed ()
160+ return self ._model_name
161+
162+ @model_name .setter
163+ def model_name (self , value : Optional [str ]) -> None :
164+ self ._model_name = value
165+
166+ @property
167+ def _grpc_uses_bls (self ) -> bool :
168+ self ._resolve_yolox_model_name_if_needed ()
169+ return bool (self ._grpc_uses_bls_value )
142170
143171 def prepare_data_for_inference (self , data : Dict [str , Any ]) -> Dict [str , Any ]:
144172 """
@@ -2117,7 +2145,6 @@ def postprocess_included_texts(boxes, confs, labels, classes):
21172145 return boxes , labels , confs
21182146
21192147
2120- @multiprocessing_cache (max_calls = 100 ) # Cache results first to avoid redundant retries from backoff
21212148@backoff .on_predicate (backoff .expo , max_time = 30 )
21222149def get_yolox_model_name (yolox_grpc_endpoint , default_model_name = "yolox" ):
21232150 # If a gRPC endpoint isn't provided (common when using HTTP-only NIM endpoints),
@@ -2131,6 +2158,15 @@ def get_yolox_model_name(yolox_grpc_endpoint, default_model_name="yolox"):
21312158 ):
21322159 return default_model_name
21332160
2161+ key = (
2162+ "get_yolox_model_name" ,
2163+ (yolox_grpc_endpoint ,),
2164+ frozenset ({"default_model_name" : default_model_name }.items ()),
2165+ )
2166+ with lock :
2167+ if key in global_cache :
2168+ return global_cache [key ]
2169+
21342170 try :
21352171 client = grpcclient .InferenceServerClient (yolox_grpc_endpoint )
21362172 model_index = client .get_model_repository_index (as_json = True )
@@ -2148,14 +2184,23 @@ def get_yolox_model_name(yolox_grpc_endpoint, default_model_name="yolox"):
21482184 "nemoretriever-page-elements-v2" ,
21492185 ):
21502186 if preferred in model_names :
2151- return preferred
2187+ result = preferred
2188+ with lock :
2189+ global_cache [key ] = result
2190+ return result
21522191
21532192 # Otherwise pick a best-effort match for newer model names.
21542193 candidates = [m for m in model_names if isinstance (m , str ) and ("yolox" in m or "page-elements" in m )]
21552194 if candidates :
2156- return sorted (candidates )[0 ]
2157-
2158- return default_model_name
2195+ result = sorted (candidates )[0 ]
2196+ with lock :
2197+ global_cache [key ] = result
2198+ return result
2199+
2200+ result = default_model_name
2201+ with lock :
2202+ global_cache [key ] = result
2203+ return result
21592204 except Exception as e :
21602205 logger .warning (
21612206 "Failed to inspect YOLOX model repository at '%s' (%s). Falling back to '%s'." ,
0 commit comments