@@ -90,6 +90,21 @@ def process(self, element):
9090 yield element
9191
9292
93+ def _ensure_transformers_config_compat (config : DistilBertConfig ) -> DistilBertConfig :
94+ """Adds missing config attributes for cross-version transformers compatibility.
95+
96+ The benchmark can run with container images whose transformers version differs
97+ from the launcher environment. Some versions assume these attributes exist.
98+ """
99+ if not hasattr (config , 'pruned_heads' ):
100+ config .pruned_heads = {}
101+ if not hasattr (config , 'torchscript' ):
102+ config .torchscript = False
103+ if not hasattr (config , 'return_dict' ):
104+ config .return_dict = True
105+ return config
106+
107+
93108def parse_known_args (argv ):
94109 """Parses command-line arguments for pipeline execution."""
95110 parser = argparse .ArgumentParser ()
@@ -241,11 +256,8 @@ def run(
241256 method = beam .io .WriteToBigQuery .Method .STREAMING_INSERTS
242257 pipeline_options .view_as (StandardOptions ).streaming = True
243258
244- model_config = DistilBertConfig .from_pretrained (
245- known_args .model_path , num_labels = 2 )
246- # Some transformers versions may not initialize this field on config objects.
247- if not hasattr (model_config , 'pruned_heads' ):
248- model_config .pruned_heads = {}
259+ model_config = _ensure_transformers_config_compat (
260+ DistilBertConfig .from_pretrained (known_args .model_path , num_labels = 2 ))
249261
250262 model_handler = PytorchModelHandlerKeyedTensor (
251263 model_class = DistilBertForSequenceClassification ,
0 commit comments