google-ai-edge / ai-edge-torch

Supporting PyTorch models with the Google AI Edge TFLite runtime.
Apache License 2.0
287 stars 38 forks source link

Conversion fails on model loaded via torch.load or torch.jit.load #221

Open saseptim opened 6 days ago

saseptim commented 6 days ago

Description of the bug:

I have a pytorch model which was saved with torch.jit.save(). I tried both with a traced model and scripted model. The error is:

_File /orcam/ear/scratch/usr/avis/VENV_AI_EDGE/lib/python3.10/site-packages/torch/export/_trace.py:1449, in _export(mod, args, kwargs, dynamic_shapes, strict, preserve_module_call_signature, pre_dispatch, _allow_complex_guards_as_runtime_asserts, _disable_forced_specializations, _is_torch_jit_trace) 1447 original_state_dict = mod.state_dict(keep_vars=True) 1448 if not _is_torch_jit_trace: -> 1449 forward_arg_names = _get_forward_arg_names(mod, args, kwargs) 1450 else: 1451 forward_arg_names = None

File /orcam/ear/scratch/usr/avis/VENV_AI_EDGE/lib/python3.10/site-packages/torch/export/_trace.py:753, in _get_forward_arg_names(mod, args, kwargs) 739 def _get_forward_arg_names( 740 mod: torch.nn.Module, 741 args: Tuple[Any, ...], 742 kwargs: Optional[Dict[str, Any]] = None, 743 ) -> List[str]: 744 """ 745 Gets the argument names to forward that are used, for restoring the 746 original signature when unlifting the exported program module. (...) 751 export lifted modules. 752 """ --> 753 sig = inspect.signature(mod.forward) 754 _args = sig.bind_partial(*args).arguments 756 names: List[str] = []

File /usr/lib/python3.10/inspect.py:3254, in signature(obj, follow_wrapped, globals, locals, eval_str) 3252 def signature(obj, *, follow_wrapped=True, globals=None, locals=None, eval_str=False): 3253 """Get a signature object for the passed callable.""" -> 3254 return Signature.from_callable(obj, follow_wrapped=follow_wrapped, 3255 globals=globals, locals=locals, eval_str=eval_str)

File /usr/lib/python3.10/inspect.py:3002, in Signature.from_callable(cls, obj, follow_wrapped, globals, locals, eval_str) 2998 @classmethod 2999 def from_callable(cls, obj, *, 3000 follow_wrapped=True, globals=None, locals=None, eval_str=False): 3001 """Constructs Signature for the given callable object.""" -> 3002 return _signature_from_callable(obj, sigcls=cls, 3003 follow_wrapper_chains=follow_wrapped, 3004 globals=globals, locals=locals, eval_str=eval_str)

File /usr/lib/python3.10/inspect.py:2550, in _signature_from_callable(obj, follow_wrapper_chains, skip_bound_arg, globals, locals, eval_str, sigcls) 2548 except ValueError as ex: 2549 msg = 'no signature found for {!r}'.format(obj) -> 2550 raise ValueError(msg) from ex 2552 if sig is not None: 2553 # For classes and objects we skip the first parameter of their 2554 # call, new, or init methods 2555 if skip_bound_arg:

ValueError: no signature found for <torch.ScriptMethod object at 0x7f942662ffb0>_

Actual vs expected behavior:

No response

Any other information you'd like to share?

No response

pkgoogle commented 3 days ago

Hi @saseptim, I don't believe ai-edge-torch can handle that file format..., for this repo the PyTorch model needs to be torch.export compliant... you can find more details here: https://github.com/google-ai-edge/ai-edge-torch/blob/main/docs/pytorch_converter/README.md#conversion

Do you have an example script showing what you are doing? Thanks.