GLiNER icon indicating copy to clipboard operation
GLiNER copied to clipboard

Conversion to ONXX

Open jitesh0034 opened this issue 1 year ago • 9 comments

Using this snippet to save pre-trained model locally -

from gliner import GLiNER
model  = GLiNER.from_pretrained("urchade/gliner_medium")
model.save_pretrained("gliner_medium")

post this, running script for onxx conversion as per documentation - python GLiNER/convert_to_onnx.py --model_path gliner_medium/ --save_path gliner_onnx/ --quantize True

getting error on - OSError: gliner_medium does not appear to have a file named config.json. Checkout 'https://huggingface.co/gliner_medium/tree/None' for available files.

Tried with changing file name from 'gliner_config.json' to 'config.json' getting new error of - ValueError: Unrecognized model in gliner_medium. Should have a `model_type` key in its config.json

jitesh0034 avatar Aug 21 '24 17:08 jitesh0034

try the same onnx conversion in notebook version once, https://github.com/urchade/GLiNER/blob/main/examples/convert_to_onnx.ipynb

hari-ag00 avatar Aug 22 '24 07:08 hari-ag00

and try to use earlier gliner version, latest gliner version has some bugs in onnx conversion

hari-ag00 avatar Aug 22 '24 07:08 hari-ag00

thanks @hari-ag00
Also, can you suggest the version for this? Currently, i am using '0.2.2'

jitesh0034 avatar Aug 23 '24 07:08 jitesh0034

@jitesh0034 try using 0.2.3

hari-ag00 avatar Aug 23 '24 12:08 hari-ag00

Hi @hari-ag00, is the issue with conversion fixed with the latest gliner ?

manickavela29 avatar Nov 11 '24 10:11 manickavela29

@manickavela29, Not sure, which version are you using? facing any issue?

hari-ag00 avatar Dec 03 '24 12:12 hari-ag00

I am still having issues with attempts to convert to ONNX using the latest v0.2.16.

import os
import onnx
from gliner import GLiNER

chunk_size = 384 
gliner_model = GLiNER.from_pretrained("gliner-community/gliner_large-v2.5", max_length=chunk_size)
gliner_model.save_pretrained("gliner_large-v2.5")
onnx_save_path = os.path.join("/dirpath", "gliner-large-v25.onnx")

text = "ONNX is an open-source format designed to enable the interoperability of AI models across various frameworks and tools."
labels = ['format', 'model', 'tool', 'cat']

inputs, _ = gliner_model.prepare_model_inputs([text], labels)

if gliner_model.config.span_mode == 'token_level':
    all_inputs =  (inputs['input_ids'], inputs['attention_mask'], 
                    inputs['words_mask'], inputs['text_lengths'])
    input_names = ['input_ids', 'attention_mask', 'words_mask', 'text_lengths']
    dynamic_axes={
        "input_ids": {0: "batch_size", 1: "sequence_length"},
        "attention_mask": {0: "batch_size", 1: "sequence_length"},
        "words_mask": {0: "batch_size", 1: "sequence_length"},
        "text_lengths": {0: "batch_size", 1: "value"},
        "logits": {0: "position", 1: "batch_size", 2: "sequence_length", 3: "num_classes"},
    }
else:
    all_inputs =  (inputs['input_ids'], inputs['attention_mask'], 
                    inputs['words_mask'], inputs['text_lengths'],
                    inputs['span_idx'], inputs['span_mask'])
    input_names = ['input_ids', 'attention_mask', 'words_mask', 'text_lengths', 'span_idx', 'span_mask']
    dynamic_axes={
        "input_ids": {0: "batch_size", 1: "sequence_length"},
        "attention_mask": {0: "batch_size", 1: "sequence_length"},
        "words_mask": {0: "batch_size", 1: "sequence_length"},
        "text_lengths": {0: "batch_size", 1: "value"},
        "span_idx": {0: "batch_size", 1: "num_spans", 2: "idx"},
        "span_mask": {0: "batch_size", 1: "num_spans"},
        "logits": {0: "batch_size", 1: "sequence_length", 2: "num_spans", 3: "num_classes"},
    }
print('Converting the model...')
torch.onnx.export(
    gliner_model.model,
    all_inputs,
    f=onnx_save_path,
    input_names=input_names,
    output_names=["logits"],
    dynamic_axes=dynamic_axes,
    opset_version=14,
)

Returns

Asking to truncate to max_length but no maximum length is provided and the model has no predefined maximum length. Default to no truncation.
Converting the model...
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[2], line 34
     24     dynamic_axes={
     25         "input_ids": {0: "batch_size", 1: "sequence_length"},
     26         "attention_mask": {0: "batch_size", 1: "sequence_length"},
   (...)
     31         "logits": {0: "batch_size", 1: "sequence_length", 2: "num_spans", 3: "num_classes"},
     32     }
     33 print('Converting the model...')
---> 34 torch.onnx.export(
     35     gliner_model.model,
     36     all_inputs,
     37     f=onnx_save_path,
     38     input_names=input_names,
     39     output_names=["logits"],
     40     dynamic_axes=dynamic_axes,
     41     opset_version=14,
     42 )

File .../python3.11/site-packages/torch/onnx/__init__.py:375, in export(model, args, f, kwargs, export_params, verbose, input_names, output_names, opset_version, dynamic_axes, keep_initializers_as_inputs, dynamo, external_data, dynamic_shapes, report, verify, profile, dump_exported_program, artifacts_dir, fallback, training, operator_export_type, do_constant_folding, custom_opsets, export_modules_as_functions, autograd_inlining, **_)
    369 if dynamic_shapes:
    370     raise ValueError(
    371         "The exporter only supports dynamic shapes "
    372         "through parameter dynamic_axes when dynamo=False."
    373     )
--> 375 export(
    376     model,
    377     args,
    378     f,  # type: ignore[arg-type]
    379     kwargs=kwargs,
    380     export_params=export_params,
    381     verbose=verbose is True,
    382     input_names=input_names,
    383     output_names=output_names,
    384     opset_version=opset_version,
    385     dynamic_axes=dynamic_axes,
    386     keep_initializers_as_inputs=keep_initializers_as_inputs,
    387     training=training,
    388     operator_export_type=operator_export_type,
    389     do_constant_folding=do_constant_folding,
    390     custom_opsets=custom_opsets,
    391     export_modules_as_functions=export_modules_as_functions,
    392     autograd_inlining=autograd_inlining,
    393 )
    394 return None

File .../python3.11/site-packages/torch/onnx/utils.py:502, in export(model, args, f, kwargs, export_params, verbose, training, input_names, output_names, operator_export_type, opset_version, do_constant_folding, dynamic_axes, keep_initializers_as_inputs, custom_opsets, export_modules_as_functions, autograd_inlining)
    499 if kwargs is not None:
    500     args = args + (kwargs,)
--> 502 _export(
    503     model,
    504     args,
    505     f,
    506     export_params,
    507     verbose,
    508     training,
    509     input_names,
    510     output_names,
    511     operator_export_type=operator_export_type,
    512     opset_version=opset_version,
    513     do_constant_folding=do_constant_folding,
    514     dynamic_axes=dynamic_axes,
    515     keep_initializers_as_inputs=keep_initializers_as_inputs,
    516     custom_opsets=custom_opsets,
    517     export_modules_as_functions=export_modules_as_functions,
    518     autograd_inlining=autograd_inlining,
    519 )
    521 return None

File .../lib/python3.11/site-packages/torch/onnx/utils.py:1564, in _export(model, args, f, export_params, verbose, training, input_names, output_names, operator_export_type, export_type, opset_version, do_constant_folding, dynamic_axes, keep_initializers_as_inputs, fixed_batch_size, custom_opsets, add_node_names, onnx_shape_inference, export_modules_as_functions, autograd_inlining)
   1561     dynamic_axes = {}
   1562 _validate_dynamic_axes(dynamic_axes, model, input_names, output_names)
-> 1564 graph, params_dict, torch_out = _model_to_graph(
   1565     model,
   1566     args,
   1567     verbose,
   1568     input_names,
   1569     output_names,
   1570     operator_export_type,
   1571     val_do_constant_folding,
   1572     fixed_batch_size=fixed_batch_size,
   1573     training=training,
   1574     dynamic_axes=dynamic_axes,
   1575 )
   1577 # TODO: Don't allocate a in-memory string for the protobuf
   1578 defer_weight_export = (
   1579     export_type is not _exporter_states.ExportTypes.PROTOBUF_FILE
   1580 )

File .../lib/python3.11/site-packages/torch/onnx/utils.py:1113, in _model_to_graph(model, args, verbose, input_names, output_names, operator_export_type, do_constant_folding, _disable_torch_constant_prop, fixed_batch_size, training, dynamic_axes)
   1110     args = (args,)
   1112 model = _pre_trace_quant_model(model, args)
-> 1113 graph, params, torch_out, module = _create_jit_graph(model, args)
   1114 params_dict = _get_named_param_dict(graph, params)
   1116 try:

File .../lib/python3.11/site-packages/torch/onnx/utils.py:997, in _create_jit_graph(model, args)
    992     graph = _C._propagate_and_assign_input_shapes(
    993         graph, flattened_args, param_count_list, False, False
    994     )
    995     return graph, params, torch_out, None
--> 997 graph, torch_out = _trace_and_get_graph_from_model(model, args)
    998 _C._jit_pass_onnx_lint(graph)
    999 state_dict = torch.jit._unique_state_dict(model)

File .../lib/python3.11/site-packages/torch/onnx/utils.py:904, in _trace_and_get_graph_from_model(model, args)
    902 prev_autocast_cache_enabled = torch.is_autocast_cache_enabled()
    903 torch.set_autocast_cache_enabled(False)
--> 904 trace_graph, torch_out, inputs_states = torch.jit._get_trace_graph(
    905     model,
    906     args,
    907     strict=False,
    908     _force_outplace=False,
    909     _return_inputs_states=True,
    910 )
    911 torch.set_autocast_cache_enabled(prev_autocast_cache_enabled)
    913 warn_on_static_input_change(inputs_states)

File .../lib/python3.11/site-packages/torch/jit/_trace.py:1500, in _get_trace_graph(f, args, kwargs, strict, _force_outplace, return_inputs, _return_inputs_states)
   1498 if not isinstance(args, tuple):
   1499     args = (args,)
-> 1500 outs = ONNXTracedModule(
   1501     f, strict, _force_outplace, return_inputs, _return_inputs_states
   1502 )(*args, **kwargs)
   1503 return outs

File .../lib/python3.11/site-packages/torch/nn/modules/module.py:1736, in Module._wrapped_call_impl(self, *args, **kwargs)
   1734     return self._compiled_call_impl(*args, **kwargs)  # type: ignore[misc]
   1735 else:
-> 1736     return self._call_impl(*args, **kwargs)

File .../lib/python3.11/site-packages/torch/nn/modules/module.py:1747, in Module._call_impl(self, *args, **kwargs)
   1742 # If we don't have any hooks, we want to skip the rest of the logic in
   1743 # this function, and just call forward.
   1744 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks
   1745         or _global_backward_pre_hooks or _global_backward_hooks
   1746         or _global_forward_hooks or _global_forward_pre_hooks):
-> 1747     return forward_call(*args, **kwargs)
   1749 result = None
   1750 called_always_called_hooks = set()

File .../lib/python3.11/site-packages/torch/jit/_trace.py:139, in ONNXTracedModule.forward(self, *args)
    136     else:
    137         return tuple(out_vars)
--> 139 graph, out = torch._C._create_graph_by_tracing(
    140     wrapper,
    141     in_vars + module_state,
    142     _create_interpreter_name_lookup_fn(),
    143     self.strict,
    144     self._force_outplace,
    145 )
    147 if self._return_inputs:
    148     return graph, outs[0], ret_inputs[0]

File .../lib/python3.11/site-packages/torch/jit/_trace.py:130, in ONNXTracedModule.forward.<locals>.wrapper(*args)
    128 if self._return_inputs_states:
    129     inputs_states.append(_unflatten(in_args, in_desc))
--> 130 outs.append(self.inner(*trace_inputs))
    131 if self._return_inputs_states:
    132     inputs_states[0] = (inputs_states[0], trace_inputs)

File .../lib/python3.11/site-packages/torch/nn/modules/module.py:1736, in Module._wrapped_call_impl(self, *args, **kwargs)
   1734     return self._compiled_call_impl(*args, **kwargs)  # type: ignore[misc]
   1735 else:
-> 1736     return self._call_impl(*args, **kwargs)

File .../nlp/lib/python3.11/site-packages/torch/nn/modules/module.py:1747, in Module._call_impl(self, *args, **kwargs)
   1742 # If we don't have any hooks, we want to skip the rest of the logic in
   1743 # this function, and just call forward.
   1744 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks
   1745         or _global_backward_pre_hooks or _global_backward_hooks
   1746         or _global_forward_hooks or _global_forward_pre_hooks):
-> 1747     return forward_call(*args, **kwargs)
   1749 result = None
   1750 called_always_called_hooks = set()

File .../python3.11/site-packages/torch/nn/modules/module.py:1726, in Module._slow_forward(self, *input, **kwargs)
   1724         recording_scopes = False
   1725 try:
-> 1726     result = self.forward(*input, **kwargs)
   1727 finally:
   1728     if recording_scopes:

File .../lib/python3.11/site-packages/gliner/modeling/base.py:233, in SpanModel.forward(self, input_ids, attention_mask, labels_embeddings, labels_input_ids, labels_attention_mask, words_embedding, mask, prompts_embedding, prompts_embedding_mask, words_mask, text_lengths, span_idx, span_mask, labels, **kwargs)
    215 def forward(self,        
    216             input_ids: Optional[torch.FloatTensor] = None,
    217             attention_mask: Optional[torch.LongTensor] = None,
   (...)
    230             **kwargs
    231             ):
--> 233     prompts_embedding, prompts_embedding_mask, words_embedding, mask = self.get_representations(input_ids, attention_mask, 
    234                                                                             labels_embeddings, labels_input_ids, labels_attention_mask, 
    235                                                                                                                 text_lengths, words_mask)
    236     span_idx = span_idx*span_mask.unsqueeze(-1)
    238     span_rep = self.span_rep_layer(words_embedding, span_idx)

File .../lib/python3.11/site-packages/gliner/modeling/base.py:182, in BaseModel.get_representations(self, input_ids, attention_mask, labels_embeddings, labels_input_ids, labels_attention_mask, text_lengths, words_mask, **kwargs)
    177     prompts_embedding, prompts_embedding_mask, words_embedding, mask = self.get_bi_representations(
    178             input_ids, attention_mask, labels_embeddings, labels_input_ids, labels_attention_mask, 
    179                                                                 text_lengths, words_mask, **kwargs
    180     )
    181 else:
--> 182     prompts_embedding, prompts_embedding_mask, words_embedding, mask = self.get_uni_representations(
    183                             input_ids, attention_mask, text_lengths, words_mask, **kwargs
    184     )
    185 return prompts_embedding, prompts_embedding_mask, words_embedding, mask

File .../lib/python3.11/site-packages/gliner/modeling/base.py:128, in BaseModel.get_uni_representations(self, input_ids, attention_mask, text_lengths, words_mask, **kwargs)
    119 def get_uni_representations(self, 
    120             input_ids: Optional[torch.FloatTensor] = None,
    121             attention_mask: Optional[torch.LongTensor] = None,
    122             text_lengths: Optional[torch.Tensor] = None,
    123             words_mask: Optional[torch.LongTensor] = None,
    124             **kwargs):
    126     token_embeds = self.token_rep_layer(input_ids, attention_mask, **kwargs)
--> 128     prompts_embedding, prompts_embedding_mask, words_embedding, mask = self._extract_prompt_features_and_word_embeddings(token_embeds, input_ids, attention_mask, 
    129                                                                                                     text_lengths, words_mask)
    131     if self.config.has_rnn:
    132         words_embedding = self.rnn(words_embedding, mask)

File .../lib/python3.11/site-packages/gliner/modeling/base.py:110, in BaseModel._extract_prompt_features_and_word_embeddings(self, token_embeds, input_ids, attention_mask, text_lengths, words_mask)
    108 def _extract_prompt_features_and_word_embeddings(self, token_embeds, input_ids, attention_mask, 
    109                                                                 text_lengths, words_mask):
--> 110     prompts_embedding, prompts_embedding_mask, words_embedding, mask = extract_prompt_features_and_word_embeddings(self.config, 
    111                                                                                                                    token_embeds, 
    112                                                                                                                    input_ids, 
    113                                                                                                                    attention_mask, 
    114                                                                                                                    text_lengths, 
    115                                                                                                                    words_mask,
    116                                                                                                                    self.config.embed_ent_token)
    117     return prompts_embedding, prompts_embedding_mask, words_embedding, mask

File .../python3.11/site-packages/gliner/modeling/base.py:55, in extract_prompt_features_and_word_embeddings(config, token_embeds, input_ids, attention_mask, text_lengths, words_mask, embed_ent_token, **kwargs)
     52 num_class_tokens = torch.sum(class_token_mask, dim=-1, keepdim=True)
     54 max_embed_dim = num_class_tokens.max()
---> 55 max_text_length = text_lengths.max()
     56 aranged_class_idx = torch.arange(max_embed_dim, 
     57                                     dtype=attention_mask.dtype, 
     58                                     device=token_embeds.device).expand(batch_size, -1)
     60 batch_indices, target_class_idx = torch.where(aranged_class_idx<num_class_tokens)

AttributeError: 'NoneType' object has no attribute 'max'

nadolsw avatar Jan 29 '25 17:01 nadolsw

I am still having issues with attempts to convert to ONNX using the latest v0.2.16.

import os
import onnx
from gliner import GLiNER

chunk_size = 384 
gliner_model = GLiNER.from_pretrained("gliner-community/gliner_large-v2.5", max_length=chunk_size)
gliner_model.save_pretrained("gliner_large-v2.5")
onnx_save_path = os.path.join("/dirpath", "gliner-large-v25.onnx")

text = "ONNX is an open-source format designed to enable the interoperability of AI models across various frameworks and tools."
labels = ['format', 'model', 'tool', 'cat']

inputs, _ = gliner_model.prepare_model_inputs([text], labels)

if gliner_model.config.span_mode == 'token_level':
    all_inputs =  (inputs['input_ids'], inputs['attention_mask'], 
                    inputs['words_mask'], inputs['text_lengths'])
    input_names = ['input_ids', 'attention_mask', 'words_mask', 'text_lengths']
    dynamic_axes={
        "input_ids": {0: "batch_size", 1: "sequence_length"},
        "attention_mask": {0: "batch_size", 1: "sequence_length"},
        "words_mask": {0: "batch_size", 1: "sequence_length"},
        "text_lengths": {0: "batch_size", 1: "value"},
        "logits": {0: "position", 1: "batch_size", 2: "sequence_length", 3: "num_classes"},
    }
else:
    all_inputs =  (inputs['input_ids'], inputs['attention_mask'], 
                    inputs['words_mask'], inputs['text_lengths'],
                    inputs['span_idx'], inputs['span_mask'])
    input_names = ['input_ids', 'attention_mask', 'words_mask', 'text_lengths', 'span_idx', 'span_mask']
    dynamic_axes={
        "input_ids": {0: "batch_size", 1: "sequence_length"},
        "attention_mask": {0: "batch_size", 1: "sequence_length"},
        "words_mask": {0: "batch_size", 1: "sequence_length"},
        "text_lengths": {0: "batch_size", 1: "value"},
        "span_idx": {0: "batch_size", 1: "num_spans", 2: "idx"},
        "span_mask": {0: "batch_size", 1: "num_spans"},
        "logits": {0: "batch_size", 1: "sequence_length", 2: "num_spans", 3: "num_classes"},
    }
print('Converting the model...')
torch.onnx.export(
    gliner_model.model,
    all_inputs,
    f=onnx_save_path,
    input_names=input_names,
    output_names=["logits"],
    dynamic_axes=dynamic_axes,
    opset_version=14,
)

Returns

Asking to truncate to max_length but no maximum length is provided and the model has no predefined maximum length. Default to no truncation.
Converting the model...
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[2], line 34
     24     dynamic_axes={
     25         "input_ids": {0: "batch_size", 1: "sequence_length"},
     26         "attention_mask": {0: "batch_size", 1: "sequence_length"},
   (...)
     31         "logits": {0: "batch_size", 1: "sequence_length", 2: "num_spans", 3: "num_classes"},
     32     }
     33 print('Converting the model...')
---> 34 torch.onnx.export(
     35     gliner_model.model,
     36     all_inputs,
     37     f=onnx_save_path,
     38     input_names=input_names,
     39     output_names=["logits"],
     40     dynamic_axes=dynamic_axes,
     41     opset_version=14,
     42 )

File .../python3.11/site-packages/torch/onnx/__init__.py:375, in export(model, args, f, kwargs, export_params, verbose, input_names, output_names, opset_version, dynamic_axes, keep_initializers_as_inputs, dynamo, external_data, dynamic_shapes, report, verify, profile, dump_exported_program, artifacts_dir, fallback, training, operator_export_type, do_constant_folding, custom_opsets, export_modules_as_functions, autograd_inlining, **_)
    369 if dynamic_shapes:
    370     raise ValueError(
    371         "The exporter only supports dynamic shapes "
    372         "through parameter dynamic_axes when dynamo=False."
    373     )
--> 375 export(
    376     model,
    377     args,
    378     f,  # type: ignore[arg-type]
    379     kwargs=kwargs,
    380     export_params=export_params,
    381     verbose=verbose is True,
    382     input_names=input_names,
    383     output_names=output_names,
    384     opset_version=opset_version,
    385     dynamic_axes=dynamic_axes,
    386     keep_initializers_as_inputs=keep_initializers_as_inputs,
    387     training=training,
    388     operator_export_type=operator_export_type,
    389     do_constant_folding=do_constant_folding,
    390     custom_opsets=custom_opsets,
    391     export_modules_as_functions=export_modules_as_functions,
    392     autograd_inlining=autograd_inlining,
    393 )
    394 return None

File .../python3.11/site-packages/torch/onnx/utils.py:502, in export(model, args, f, kwargs, export_params, verbose, training, input_names, output_names, operator_export_type, opset_version, do_constant_folding, dynamic_axes, keep_initializers_as_inputs, custom_opsets, export_modules_as_functions, autograd_inlining)
    499 if kwargs is not None:
    500     args = args + (kwargs,)
--> 502 _export(
    503     model,
    504     args,
    505     f,
    506     export_params,
    507     verbose,
    508     training,
    509     input_names,
    510     output_names,
    511     operator_export_type=operator_export_type,
    512     opset_version=opset_version,
    513     do_constant_folding=do_constant_folding,
    514     dynamic_axes=dynamic_axes,
    515     keep_initializers_as_inputs=keep_initializers_as_inputs,
    516     custom_opsets=custom_opsets,
    517     export_modules_as_functions=export_modules_as_functions,
    518     autograd_inlining=autograd_inlining,
    519 )
    521 return None

File .../lib/python3.11/site-packages/torch/onnx/utils.py:1564, in _export(model, args, f, export_params, verbose, training, input_names, output_names, operator_export_type, export_type, opset_version, do_constant_folding, dynamic_axes, keep_initializers_as_inputs, fixed_batch_size, custom_opsets, add_node_names, onnx_shape_inference, export_modules_as_functions, autograd_inlining)
   1561     dynamic_axes = {}
   1562 _validate_dynamic_axes(dynamic_axes, model, input_names, output_names)
-> 1564 graph, params_dict, torch_out = _model_to_graph(
   1565     model,
   1566     args,
   1567     verbose,
   1568     input_names,
   1569     output_names,
   1570     operator_export_type,
   1571     val_do_constant_folding,
   1572     fixed_batch_size=fixed_batch_size,
   1573     training=training,
   1574     dynamic_axes=dynamic_axes,
   1575 )
   1577 # TODO: Don't allocate a in-memory string for the protobuf
   1578 defer_weight_export = (
   1579     export_type is not _exporter_states.ExportTypes.PROTOBUF_FILE
   1580 )

File .../lib/python3.11/site-packages/torch/onnx/utils.py:1113, in _model_to_graph(model, args, verbose, input_names, output_names, operator_export_type, do_constant_folding, _disable_torch_constant_prop, fixed_batch_size, training, dynamic_axes)
   1110     args = (args,)
   1112 model = _pre_trace_quant_model(model, args)
-> 1113 graph, params, torch_out, module = _create_jit_graph(model, args)
   1114 params_dict = _get_named_param_dict(graph, params)
   1116 try:

File .../lib/python3.11/site-packages/torch/onnx/utils.py:997, in _create_jit_graph(model, args)
    992     graph = _C._propagate_and_assign_input_shapes(
    993         graph, flattened_args, param_count_list, False, False
    994     )
    995     return graph, params, torch_out, None
--> 997 graph, torch_out = _trace_and_get_graph_from_model(model, args)
    998 _C._jit_pass_onnx_lint(graph)
    999 state_dict = torch.jit._unique_state_dict(model)

File .../lib/python3.11/site-packages/torch/onnx/utils.py:904, in _trace_and_get_graph_from_model(model, args)
    902 prev_autocast_cache_enabled = torch.is_autocast_cache_enabled()
    903 torch.set_autocast_cache_enabled(False)
--> 904 trace_graph, torch_out, inputs_states = torch.jit._get_trace_graph(
    905     model,
    906     args,
    907     strict=False,
    908     _force_outplace=False,
    909     _return_inputs_states=True,
    910 )
    911 torch.set_autocast_cache_enabled(prev_autocast_cache_enabled)
    913 warn_on_static_input_change(inputs_states)

File .../lib/python3.11/site-packages/torch/jit/_trace.py:1500, in _get_trace_graph(f, args, kwargs, strict, _force_outplace, return_inputs, _return_inputs_states)
   1498 if not isinstance(args, tuple):
   1499     args = (args,)
-> 1500 outs = ONNXTracedModule(
   1501     f, strict, _force_outplace, return_inputs, _return_inputs_states
   1502 )(*args, **kwargs)
   1503 return outs

File .../lib/python3.11/site-packages/torch/nn/modules/module.py:1736, in Module._wrapped_call_impl(self, *args, **kwargs)
   1734     return self._compiled_call_impl(*args, **kwargs)  # type: ignore[misc]
   1735 else:
-> 1736     return self._call_impl(*args, **kwargs)

File .../lib/python3.11/site-packages/torch/nn/modules/module.py:1747, in Module._call_impl(self, *args, **kwargs)
   1742 # If we don't have any hooks, we want to skip the rest of the logic in
   1743 # this function, and just call forward.
   1744 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks
   1745         or _global_backward_pre_hooks or _global_backward_hooks
   1746         or _global_forward_hooks or _global_forward_pre_hooks):
-> 1747     return forward_call(*args, **kwargs)
   1749 result = None
   1750 called_always_called_hooks = set()

File .../lib/python3.11/site-packages/torch/jit/_trace.py:139, in ONNXTracedModule.forward(self, *args)
    136     else:
    137         return tuple(out_vars)
--> 139 graph, out = torch._C._create_graph_by_tracing(
    140     wrapper,
    141     in_vars + module_state,
    142     _create_interpreter_name_lookup_fn(),
    143     self.strict,
    144     self._force_outplace,
    145 )
    147 if self._return_inputs:
    148     return graph, outs[0], ret_inputs[0]

File .../lib/python3.11/site-packages/torch/jit/_trace.py:130, in ONNXTracedModule.forward.<locals>.wrapper(*args)
    128 if self._return_inputs_states:
    129     inputs_states.append(_unflatten(in_args, in_desc))
--> 130 outs.append(self.inner(*trace_inputs))
    131 if self._return_inputs_states:
    132     inputs_states[0] = (inputs_states[0], trace_inputs)

File .../lib/python3.11/site-packages/torch/nn/modules/module.py:1736, in Module._wrapped_call_impl(self, *args, **kwargs)
   1734     return self._compiled_call_impl(*args, **kwargs)  # type: ignore[misc]
   1735 else:
-> 1736     return self._call_impl(*args, **kwargs)

File .../nlp/lib/python3.11/site-packages/torch/nn/modules/module.py:1747, in Module._call_impl(self, *args, **kwargs)
   1742 # If we don't have any hooks, we want to skip the rest of the logic in
   1743 # this function, and just call forward.
   1744 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks
   1745         or _global_backward_pre_hooks or _global_backward_hooks
   1746         or _global_forward_hooks or _global_forward_pre_hooks):
-> 1747     return forward_call(*args, **kwargs)
   1749 result = None
   1750 called_always_called_hooks = set()

File .../python3.11/site-packages/torch/nn/modules/module.py:1726, in Module._slow_forward(self, *input, **kwargs)
   1724         recording_scopes = False
   1725 try:
-> 1726     result = self.forward(*input, **kwargs)
   1727 finally:
   1728     if recording_scopes:

File .../lib/python3.11/site-packages/gliner/modeling/base.py:233, in SpanModel.forward(self, input_ids, attention_mask, labels_embeddings, labels_input_ids, labels_attention_mask, words_embedding, mask, prompts_embedding, prompts_embedding_mask, words_mask, text_lengths, span_idx, span_mask, labels, **kwargs)
    215 def forward(self,        
    216             input_ids: Optional[torch.FloatTensor] = None,
    217             attention_mask: Optional[torch.LongTensor] = None,
   (...)
    230             **kwargs
    231             ):
--> 233     prompts_embedding, prompts_embedding_mask, words_embedding, mask = self.get_representations(input_ids, attention_mask, 
    234                                                                             labels_embeddings, labels_input_ids, labels_attention_mask, 
    235                                                                                                                 text_lengths, words_mask)
    236     span_idx = span_idx*span_mask.unsqueeze(-1)
    238     span_rep = self.span_rep_layer(words_embedding, span_idx)

File .../lib/python3.11/site-packages/gliner/modeling/base.py:182, in BaseModel.get_representations(self, input_ids, attention_mask, labels_embeddings, labels_input_ids, labels_attention_mask, text_lengths, words_mask, **kwargs)
    177     prompts_embedding, prompts_embedding_mask, words_embedding, mask = self.get_bi_representations(
    178             input_ids, attention_mask, labels_embeddings, labels_input_ids, labels_attention_mask, 
    179                                                                 text_lengths, words_mask, **kwargs
    180     )
    181 else:
--> 182     prompts_embedding, prompts_embedding_mask, words_embedding, mask = self.get_uni_representations(
    183                             input_ids, attention_mask, text_lengths, words_mask, **kwargs
    184     )
    185 return prompts_embedding, prompts_embedding_mask, words_embedding, mask

File .../lib/python3.11/site-packages/gliner/modeling/base.py:128, in BaseModel.get_uni_representations(self, input_ids, attention_mask, text_lengths, words_mask, **kwargs)
    119 def get_uni_representations(self, 
    120             input_ids: Optional[torch.FloatTensor] = None,
    121             attention_mask: Optional[torch.LongTensor] = None,
    122             text_lengths: Optional[torch.Tensor] = None,
    123             words_mask: Optional[torch.LongTensor] = None,
    124             **kwargs):
    126     token_embeds = self.token_rep_layer(input_ids, attention_mask, **kwargs)
--> 128     prompts_embedding, prompts_embedding_mask, words_embedding, mask = self._extract_prompt_features_and_word_embeddings(token_embeds, input_ids, attention_mask, 
    129                                                                                                     text_lengths, words_mask)
    131     if self.config.has_rnn:
    132         words_embedding = self.rnn(words_embedding, mask)

File .../lib/python3.11/site-packages/gliner/modeling/base.py:110, in BaseModel._extract_prompt_features_and_word_embeddings(self, token_embeds, input_ids, attention_mask, text_lengths, words_mask)
    108 def _extract_prompt_features_and_word_embeddings(self, token_embeds, input_ids, attention_mask, 
    109                                                                 text_lengths, words_mask):
--> 110     prompts_embedding, prompts_embedding_mask, words_embedding, mask = extract_prompt_features_and_word_embeddings(self.config, 
    111                                                                                                                    token_embeds, 
    112                                                                                                                    input_ids, 
    113                                                                                                                    attention_mask, 
    114                                                                                                                    text_lengths, 
    115                                                                                                                    words_mask,
    116                                                                                                                    self.config.embed_ent_token)
    117     return prompts_embedding, prompts_embedding_mask, words_embedding, mask

File .../python3.11/site-packages/gliner/modeling/base.py:55, in extract_prompt_features_and_word_embeddings(config, token_embeds, input_ids, attention_mask, text_lengths, words_mask, embed_ent_token, **kwargs)
     52 num_class_tokens = torch.sum(class_token_mask, dim=-1, keepdim=True)
     54 max_embed_dim = num_class_tokens.max()
---> 55 max_text_length = text_lengths.max()
     56 aranged_class_idx = torch.arange(max_embed_dim, 
     57                                     dtype=attention_mask.dtype, 
     58                                     device=token_embeds.device).expand(batch_size, -1)
     60 batch_indices, target_class_idx = torch.where(aranged_class_idx<num_class_tokens)

AttributeError: 'NoneType' object has no attribute 'max'

hari-ag00 avatar Feb 28 '25 07:02 hari-ag00

Check my sample notebook, I have used my fine-tuned Gliner model and tried converting ONNX, it's working https://colab.research.google.com/drive/1lwGSOAOo_hVs2K8cMkRks2MEJho7H18f?usp=sharing

hari-ag00 avatar Feb 28 '25 07:02 hari-ag00