Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pytorch OSError: could not get source code #5729

Closed
pythondever opened this issue Apr 13, 2021 · 5 comments
Closed

pytorch OSError: could not get source code #5729

pythondever opened this issue Apr 13, 2021 · 5 comments

Comments

@pythondever
Copy link

I'm using pyinstaller package pytorch is successful but when i run binary file get error

Traceback (most recent call last):
File "torch/_utils_internal.py", line 49, in get_source_lines_and_file
sourcelines, file_lineno = inspect.getsourcelines(obj)
File "inspect.py", line 967, in getsourcelines
File "inspect.py", line 798, in findsource
OSError: could not get source code

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "detector/main.py", line 138, in
File "detector/main.py", line 31, in init
File "importlib/init.py", line 127, in import_module
File "", line 1014, in _gcd_import
File "", line 991, in _find_and_load
File "", line 975, in _find_and_load_unlocked
File "", line 671, in _load_unlocked
File "PyInstaller/loader/pyimod03_importers.py", line 531, in exec_module
File "detector/detect_0.py", line 6, in
File "", line 991, in _find_and_load
File "", line 975, in _find_and_load_unlocked
File "", line 671, in _load_unlocked
File "PyInstaller/loader/pyimod03_importers.py", line 531, in exec_module
File "detectron2/engine/init.py", line 11, in
File "", line 991, in _find_and_load
File "", line 975, in _find_and_load_unlocked
File "", line 671, in _load_unlocked
File "PyInstaller/loader/pyimod03_importers.py", line 531, in exec_module
File "detectron2/engine/hooks.py", line 14, in
File "", line 991, in _find_and_load
File "", line 975, in _find_and_load_unlocked
File "", line 671, in _load_unlocked
File "PyInstaller/loader/pyimod03_importers.py", line 531, in exec_module
File "fvcore/nn/init.py", line 4, in
File "", line 991, in _find_and_load
File "", line 975, in _find_and_load_unlocked
File "", line 671, in _load_unlocked
File "PyInstaller/loader/pyimod03_importers.py", line 531, in exec_module
File "fvcore/nn/focal_loss.py", line 50, in
File "torch/jit/_script.py", line 936, in script
ast = get_jit_def(obj, obj.name)
File "torch/jit/frontend.py", line 197, in get_jit_def
sourcelines, file_lineno, filename = get_source_lines_and_file(fn, torch._C.ErrorReport.call_stack())
File "torch/_utils_internal.py", line 56, in get_source_lines_and_file
raise OSError(msg) from e
OSError: Can't get source for <function sigmoid_focal_loss at 0x7f931dd6e160>. TorchScript requires source access in order to carry out compilation, make sure original .py files are available.
[7269] Failed to execute script main
########################################################################
development environment information:
python 3.8.5
pyinstaller 4.2
platform Ubuntu 18.04.5 LTS
torch 1.7.1
torchvision 0.8.2
#########################################################################
main.spec

-- mode: python ; coding: utf-8 --

import torch
import inspect
block_cipher = None

excluded_modules = ['torch.distributions']

def collect_source_files(modules):
datas = []
for module in modules:
source = inspect.getsourcefile(module)
dest = f"src.{module.name}" # use "src." prefix
datas.append((source, dest))
return datas

source_files = collect_source_files([torch]) # return same structure as collect_data_files()
source_files_toc = TOC((name, path, 'DATA') for path, name in source_files)

a = Analysis(['../detector/main.py'],
pathex=['/data1/kazhen'],
binaries=[],
datas=[],
hiddenimports=['detector.detect_0', 'detector.detect_1', 'detector.detect_2', 'detector.detect_3',
'detector.detect_4', 'detector.detect_5', 'detector.detectron2', 'torch'],
hookspath=[],
runtime_hooks=[],
excludes=None,
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)

pyz = PYZ(a.pure, a.zipped_data, source_files_toc,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
[],
exclude_binaries=True,
name='detect_main',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=True )
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
upx_exclude=[],
name='detect_main')

i tried issue https://github.com/pyinstaller/pyinstaller/issues/4764#issuecomment-633777790 but it doesn't work.
how can i fix this error ?

@bwoodsend
Copy link
Member

Duplicate of #5673.

@rokm
Copy link
Member

rokm commented Apr 13, 2021

The comment #4764 (comment) should work in combination with develop branch of PyInstaller, which has #5697 merged.

So can you try installing and using that: pip install https://github.com/pyinstaller/pyinstaller/archive/refs/heads/develop.zip

@pythondever
Copy link
Author

The comment #4764 (comment) should work in combination with develop branch of PyInstaller, which has #5697 merged.

So can you try installing and using that: pip install https://github.com/pyinstaller/pyinstaller/archive/refs/heads/develop.zip

yes, i'm tried install but still get same error ;( is my spec file wrong ?

@rokm
Copy link
Member

rokm commented Apr 14, 2021

Well, for either approach to work (#5697 that loads collected py files directly, or the collect_source_files approach (for which you'll need to explicitly patch the loader as per #4764 (comment))) you need to ensure that not only torch .py files are collected, but also (at least some of) your detector's .py files, as those are the ones that will get JIT'ed.

From your traceback, I can see (local) packages such as fvcore.nn, detectron2, detector...

@pythondever
Copy link
Author

Well, for either approach to work (#5697 that loads collected py files directly, or the collect_source_files approach (for which you'll need to explicitly patch the loader as per #4764 (comment))) you need to ensure that not only torch .py files are collected, but also (at least some of) your detector's .py files, as those are the ones that will get JIT'ed.

From your traceback, I can see (local) packages such as fvcore.nn, detectron2, detector...

yes great!, it work! i'm collect fvcore and detectron2 package using collect_source_files
thanks bro!

this my spec file

import torch
import inspect
block_cipher = None
from PyInstaller.utils.hooks import collect_data_files

a = Analysis(['../detector/main.py'],
pathex=['/data1/kazhen'],
binaries=[],
datas=[*collect_data_files("torch", include_py_files=True), *collect_data_files("fvcore", include_py_files=True), *collect_data_files("detectron2", include_py_files=True)],
hiddenimports=['detector.detect_0', 'detector.detect_1', 'detector.detect_2', 'detector.detect_3',
'detector.detect_4', 'detector.detect_5', ],

         hookspath=[],
         runtime_hooks=[],
         excludes=None,
         win_no_prefer_redirects=False,
         win_private_assemblies=False,
         cipher=block_cipher,
         noarchive=False)

pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
[],
exclude_binaries=True,
name='detect_main',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=True )
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
upx_exclude=[],
name='detect_main')

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants