Stonesjtu / pytorch_memlab

Profiling and inspecting memory in pytorch
MIT License
1.01k stars 37 forks source link

OSError: could not get source code when running LineProfiler #46

Open ewayuan opened 2 years ago

ewayuan commented 2 years ago

Hi, when I trying to use the LineProfiler example, the following error message pops up:

import torch

from pytorch_memlab import LineProfiler

def inner(): ... torch.nn.Linear(100, 100).cuda() ... def outer(): ... linear = torch.nn.Linear(100, 100).cuda() ... linear2 = torch.nn.Linear(100, 100).cuda() ... inner() ... with LineProfiler(outer, inner) as prof: ... outer() ... Traceback (most recent call last): File "", line 1, in File "/home/wyuancs/miniconda3/envs/response-selection/lib/python3.8/site-packages/pytorch_memlab/line_profiler/line_profiler.py", line 45, in init self.add_function(func) File "/home/wyuancs/miniconda3/envs/response-selection/lib/python3.8/site-packages/pytorch_memlab/line_profiler/line_profiler.py", line 59, in add_function first_line = inspect.getsourcelines(func)[1] File "/home/wyuancs/miniconda3/envs/response-selection/lib/python3.8/inspect.py", line 979, in getsourcelines lines, lnum = findsource(object) File "/home/wyuancs/miniconda3/envs/response-selection/lib/python3.8/inspect.py", line 798, in findsource raise OSError('could not get source code') OSError: could not get source code

How could I solve this problem? I tried to search online, but not find any solution

Stonesjtu commented 2 years ago

Can you try copy-paste these lines into a python source file and then runs it.


before edit

Do you execute these codes in python xxx.py, python terminal, or ipython terminal ?

ewayuan commented 2 years ago

Hi, Thanks for your reply. I'm using python xxx.py