Skip to content

torch.export.export_for_inference not available in current stable PyTorch #1514

@dongxiaolong

Description

@dongxiaolong

Hi @cpuhrsch,

I noticed that the code uses torch.export.export_for_inference which is not available in the current stable version of PyTorch (2.5.1). This might cause compatibility issues for users who haven't switched to nightly builds yet.
reproduce step

python server.py ../../../ large --port 6006 --furious --fast --save-fast "./"

Saving compiled models under directory ./

Saving at path=PosixPath('sam2_image_encoder.pt2')

Traceback (most recent call last):
  File "/root/ao/examples/sam2_amg_server/server.py", line 578, in <module>
    fire.Fire(main)
  File "/root/miniconda3/envs/sam2/lib/python3.10/site-packages/fire/core.py", line 135, in Fire
    component_trace = _Fire(component, args, parsed_flag_args, context, name)
  File "/root/miniconda3/envs/sam2/lib/python3.10/site-packages/fire/core.py", line 468, in _Fire
    component, remaining_args = _CallAndUpdateTrace(
  File "/root/miniconda3/envs/sam2/lib/python3.10/site-packages/fire/core.py", line 684, in _CallAndUpdateTrace
    component = fn(*varargs, **kwargs)
  File "/root/autodl-tmp/ao/examples/sam2_amg_server/server.py", line 437, in main
    export_model(mask_generator,
  File "/root/autodl-tmp/ao/examples/sam2_amg_server/compile_export_utils.py", line 153, in export_model
    aot_compile(model_directory,
  File "/root/ao/examples/sam2_amg_server/compile_export_utils.py", line 109, in aot_compile
    from torch.export import export_for_inference
ImportError: cannot import name 'export_for_inference' from 'torch.export' (/root/miniconda3/envs/sam2/lib/python3.10/site-packages/torch/export/__init__.py)

Current behavior:

  • The code assumes availability of torch.export.export_for_inference
  • This feature is only available in PyTorch nightly builds

Suggested solutions:

  1. Add version check and fallback behavior
  2. Document PyTorch version requirement
  3. Consider supporting both stable and nightly versions

Would appreciate your thoughts on how to best handle this compatibility issue.

Related PR: #1468

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions