We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Make torchdynamo not import third party package in `skipfiles.py`
@xuzhao9 in https://github.com/facebookresearch/torchdynamo/issues/107#issuecomment-1095681515 found that the following line makes alexnet 18% slower: 

https://github.com/jansel/torchdynamo/blob/bf90b8cdbacf35944fa8c12185b1823dc5cb90bb/torchdynamo/skipfiles.py#L123

It seems importing:  "networkx", "omegaconf", "onnx", "pandas",  and "sklearn" cause performance issues.

TorchDynamo is only importing these modules to find the filename, which is also a bit wasteful.  We should rewrite `skipfiles.py` to use [find_spec](https://docs.python.org/3/library/importlib.html#importlib.abc.PathEntryFinder.find_spec) instead, so we don't need to import unused packages.

Also, I think we can cut down the list of modules in skipfiles dramatically.  Most of those were added when TorchDynamo didn't automatically skip backends and supported much less of python, so likely many (most?) can be removed.


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `torchdynamo/skipfiles.py`
Content:
```
1 import abc
2 import collections
3 import contextlib
4 import copy
5 import copyreg
6 import dataclasses
7 import enum
8 import functools
9 import importlib
10 import inspect
11 import linecache
12 import logging
13 import multiprocessing
14 import operator
15 import os
16 import posixpath
17 import random
18 import re
19 import selectors
20 import signal
21 import tempfile
22 import threading
23 import tokenize
24 import traceback
25 import types
26 import typing
27 import unittest
28 import weakref
29 
30 import _collections_abc
31 import _weakrefset
32 import torch
33 
34 
35 def _module_dir(m: types.ModuleType):
36     return re.sub(r"__init__.py$", "", m.__file__)
37 
38 
39 SKIP_DIRS = [
40     # torch.*
41     _module_dir(torch),
42     # torchdynamo.*
43     os.path.dirname(__file__) + "/",
44     "<frozen importlib",
45     "<__array_function__ internals>",
46 ] + [
47     # skip some standard libs
48     _module_dir(m)
49     for m in (
50         abc,
51         collections,
52         contextlib,
53         copy,
54         copyreg,
55         dataclasses,
56         enum,
57         functools,
58         importlib,
59         inspect,
60         linecache,
61         logging,
62         multiprocessing,
63         operator,
64         os,
65         posixpath,
66         random,
67         re,
68         selectors,
69         signal,
70         tempfile,
71         threading,
72         tokenize,
73         traceback,
74         types,
75         typing,
76         unittest,
77         weakref,
78         _collections_abc,
79         _weakrefset,
80     )
81 ]
82 SKIP_DIRS_RE = None  # set in add() below
83 FILENAME_ALLOWLIST = {
84     torch.nn.Sequential.__init__.__code__.co_filename,
85 }
86 
87 
88 def add(module: types.ModuleType):
89     assert isinstance(module, types.ModuleType)
90     global SKIP_DIRS_RE
91     name = module.__file__
92     if name is None:
93         return
94     SKIP_DIRS.append(_module_dir(module))
95     SKIP_DIRS_RE = re.compile(f"^({'|'.join(map(re.escape, SKIP_DIRS))})")
96 
97 
98 def check(filename, allow_torch=False):
99     """Should skip this file?"""
100     if filename is None:
101         return True
102     if filename in FILENAME_ALLOWLIST:
103         return False
104     if allow_torch and is_torch(filename):
105         return False
106     return bool(SKIP_DIRS_RE.match(filename))
107 
108 
109 # skip common third party libs
110 for _name in (
111     "functorch",
112     "intel_extension_for_pytorch",
113     "networkx",
114     "numpy",
115     "omegaconf",
116     "onnx",
117     "onnxruntime",
118     "onnx_tf",
119     "pandas",
120     "sklearn",
121     "tabulate",
122     "tensorflow",
123     "tensorrt",
124     "torch2trt",
125     "tqdm",
126     "tree",
127     "tvm",
128     "fx2trt_oss",
129 ):
130     try:
131         add(importlib.import_module(_name))
132     except (ImportError, TypeError):
133         pass
134 
135 
136 def is_torch_inline_allowed(filename):
137     return filename.startswith(_module_dir(torch.nn)) or filename.startswith(
138         _module_dir(torch.distributions)
139     )
140 
141 
142 def is_torch(filename):
143     return filename.startswith(_module_dir(torch))
144
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/torchdynamo/skipfiles.py b/torchdynamo/skipfiles.py
--- a/torchdynamo/skipfiles.py
+++ b/torchdynamo/skipfiles.py
@@ -32,8 +32,12 @@
 import torch
 
 
+def _strip_init_py(s):
+    return re.sub(r"__init__.py$", "", s)
+
+
 def _module_dir(m: types.ModuleType):
-    return re.sub(r"__init__.py$", "", m.__file__)
+    return _strip_init_py(m.__file__)
 
 
 SKIP_DIRS = [
@@ -79,22 +83,32 @@
         _weakrefset,
     )
 ]
-SKIP_DIRS_RE = None  # set in add() below
 FILENAME_ALLOWLIST = {
     torch.nn.Sequential.__init__.__code__.co_filename,
 }
+SKIP_DIRS_RE = None
 
 
-def add(module: types.ModuleType):
-    assert isinstance(module, types.ModuleType)
+def _recompile_re():
     global SKIP_DIRS_RE
-    name = module.__file__
-    if name is None:
-        return
-    SKIP_DIRS.append(_module_dir(module))
     SKIP_DIRS_RE = re.compile(f"^({'|'.join(map(re.escape, SKIP_DIRS))})")
 
 
+def add(import_name: str):
+    if isinstance(import_name, types.ModuleType):
+        return add(import_name.__name__)
+    assert isinstance(import_name, str)
+    module_spec = importlib.util.find_spec(import_name)
+    if not module_spec:
+        return
+    origin = module_spec.origin
+    if origin is None:
+        return
+    global SKIP_DIRS_RE
+    SKIP_DIRS.append(_strip_init_py(origin))
+    _recompile_re()
+
+
 def check(filename, allow_torch=False):
     """Should skip this file?"""
     if filename is None:
@@ -127,10 +141,9 @@
     "tvm",
     "fx2trt_oss",
 ):
-    try:
-        add(importlib.import_module(_name))
-    except (ImportError, TypeError):
-        pass
+    add(_name)
+
+_recompile_re()
 
 
 def is_torch_inline_allowed(filename):

{"golden_diff": "diff --git a/torchdynamo/skipfiles.py b/torchdynamo/skipfiles.py\n--- a/torchdynamo/skipfiles.py\n+++ b/torchdynamo/skipfiles.py\n@@ -32,8 +32,12 @@\n import torch\n \n \n+def _strip_init_py(s):\n+    return re.sub(r\"__init__.py$\", \"\", s)\n+\n+\n def _module_dir(m: types.ModuleType):\n-    return re.sub(r\"__init__.py$\", \"\", m.__file__)\n+    return _strip_init_py(m.__file__)\n \n \n SKIP_DIRS = [\n@@ -79,22 +83,32 @@\n         _weakrefset,\n     )\n ]\n-SKIP_DIRS_RE = None  # set in add() below\n FILENAME_ALLOWLIST = {\n     torch.nn.Sequential.__init__.__code__.co_filename,\n }\n+SKIP_DIRS_RE = None\n \n \n-def add(module: types.ModuleType):\n-    assert isinstance(module, types.ModuleType)\n+def _recompile_re():\n     global SKIP_DIRS_RE\n-    name = module.__file__\n-    if name is None:\n-        return\n-    SKIP_DIRS.append(_module_dir(module))\n     SKIP_DIRS_RE = re.compile(f\"^({'|'.join(map(re.escape, SKIP_DIRS))})\")\n \n \n+def add(import_name: str):\n+    if isinstance(import_name, types.ModuleType):\n+        return add(import_name.__name__)\n+    assert isinstance(import_name, str)\n+    module_spec = importlib.util.find_spec(import_name)\n+    if not module_spec:\n+        return\n+    origin = module_spec.origin\n+    if origin is None:\n+        return\n+    global SKIP_DIRS_RE\n+    SKIP_DIRS.append(_strip_init_py(origin))\n+    _recompile_re()\n+\n+\n def check(filename, allow_torch=False):\n     \"\"\"Should skip this file?\"\"\"\n     if filename is None:\n@@ -127,10 +141,9 @@\n     \"tvm\",\n     \"fx2trt_oss\",\n ):\n-    try:\n-        add(importlib.import_module(_name))\n-    except (ImportError, TypeError):\n-        pass\n+    add(_name)\n+\n+_recompile_re()\n \n \n def is_torch_inline_allowed(filename):\n", "issue": "Make torchdynamo not import third party package in `skipfiles.py`\n@xuzhao9 in https://github.com/facebookresearch/torchdynamo/issues/107#issuecomment-1095681515 found that the following line makes alexnet 18% slower: \r\n\r\nhttps://github.com/jansel/torchdynamo/blob/bf90b8cdbacf35944fa8c12185b1823dc5cb90bb/torchdynamo/skipfiles.py#L123\r\n\r\nIt seems importing:  \"networkx\", \"omegaconf\", \"onnx\", \"pandas\",  and \"sklearn\" cause performance issues.\r\n\r\nTorchDynamo is only importing these modules to find the filename, which is also a bit wasteful.  We should rewrite `skipfiles.py` to use [find_spec](https://docs.python.org/3/library/importlib.html#importlib.abc.PathEntryFinder.find_spec) instead, so we don't need to import unused packages.\r\n\r\nAlso, I think we can cut down the list of modules in skipfiles dramatically.  Most of those were added when TorchDynamo didn't automatically skip backends and supported much less of python, so likely many (most?) can be removed.\r\n\n", "before_files": [{"content": "import abc\nimport collections\nimport contextlib\nimport copy\nimport copyreg\nimport dataclasses\nimport enum\nimport functools\nimport importlib\nimport inspect\nimport linecache\nimport logging\nimport multiprocessing\nimport operator\nimport os\nimport posixpath\nimport random\nimport re\nimport selectors\nimport signal\nimport tempfile\nimport threading\nimport tokenize\nimport traceback\nimport types\nimport typing\nimport unittest\nimport weakref\n\nimport _collections_abc\nimport _weakrefset\nimport torch\n\n\ndef _module_dir(m: types.ModuleType):\n    return re.sub(r\"__init__.py$\", \"\", m.__file__)\n\n\nSKIP_DIRS = [\n    # torch.*\n    _module_dir(torch),\n    # torchdynamo.*\n    os.path.dirname(__file__) + \"/\",\n    \"<frozen importlib\",\n    \"<__array_function__ internals>\",\n] + [\n    # skip some standard libs\n    _module_dir(m)\n    for m in (\n        abc,\n        collections,\n        contextlib,\n        copy,\n        copyreg,\n        dataclasses,\n        enum,\n        functools,\n        importlib,\n        inspect,\n        linecache,\n        logging,\n        multiprocessing,\n        operator,\n        os,\n        posixpath,\n        random,\n        re,\n        selectors,\n        signal,\n        tempfile,\n        threading,\n        tokenize,\n        traceback,\n        types,\n        typing,\n        unittest,\n        weakref,\n        _collections_abc,\n        _weakrefset,\n    )\n]\nSKIP_DIRS_RE = None  # set in add() below\nFILENAME_ALLOWLIST = {\n    torch.nn.Sequential.__init__.__code__.co_filename,\n}\n\n\ndef add(module: types.ModuleType):\n    assert isinstance(module, types.ModuleType)\n    global SKIP_DIRS_RE\n    name = module.__file__\n    if name is None:\n        return\n    SKIP_DIRS.append(_module_dir(module))\n    SKIP_DIRS_RE = re.compile(f\"^({'|'.join(map(re.escape, SKIP_DIRS))})\")\n\n\ndef check(filename, allow_torch=False):\n    \"\"\"Should skip this file?\"\"\"\n    if filename is None:\n        return True\n    if filename in FILENAME_ALLOWLIST:\n        return False\n    if allow_torch and is_torch(filename):\n        return False\n    return bool(SKIP_DIRS_RE.match(filename))\n\n\n# skip common third party libs\nfor _name in (\n    \"functorch\",\n    \"intel_extension_for_pytorch\",\n    \"networkx\",\n    \"numpy\",\n    \"omegaconf\",\n    \"onnx\",\n    \"onnxruntime\",\n    \"onnx_tf\",\n    \"pandas\",\n    \"sklearn\",\n    \"tabulate\",\n    \"tensorflow\",\n    \"tensorrt\",\n    \"torch2trt\",\n    \"tqdm\",\n    \"tree\",\n    \"tvm\",\n    \"fx2trt_oss\",\n):\n    try:\n        add(importlib.import_module(_name))\n    except (ImportError, TypeError):\n        pass\n\n\ndef is_torch_inline_allowed(filename):\n    return filename.startswith(_module_dir(torch.nn)) or filename.startswith(\n        _module_dir(torch.distributions)\n    )\n\n\ndef is_torch(filename):\n    return filename.startswith(_module_dir(torch))\n", "path": "torchdynamo/skipfiles.py"}], "after_files": [{"content": "import abc\nimport collections\nimport contextlib\nimport copy\nimport copyreg\nimport dataclasses\nimport enum\nimport functools\nimport importlib\nimport inspect\nimport linecache\nimport logging\nimport multiprocessing\nimport operator\nimport os\nimport posixpath\nimport random\nimport re\nimport selectors\nimport signal\nimport tempfile\nimport threading\nimport tokenize\nimport traceback\nimport types\nimport typing\nimport unittest\nimport weakref\n\nimport _collections_abc\nimport _weakrefset\nimport torch\n\n\ndef _strip_init_py(s):\n    return re.sub(r\"__init__.py$\", \"\", s)\n\n\ndef _module_dir(m: types.ModuleType):\n    return _strip_init_py(m.__file__)\n\n\nSKIP_DIRS = [\n    # torch.*\n    _module_dir(torch),\n    # torchdynamo.*\n    os.path.dirname(__file__) + \"/\",\n    \"<frozen importlib\",\n    \"<__array_function__ internals>\",\n] + [\n    # skip some standard libs\n    _module_dir(m)\n    for m in (\n        abc,\n        collections,\n        contextlib,\n        copy,\n        copyreg,\n        dataclasses,\n        enum,\n        functools,\n        importlib,\n        inspect,\n        linecache,\n        logging,\n        multiprocessing,\n        operator,\n        os,\n        posixpath,\n        random,\n        re,\n        selectors,\n        signal,\n        tempfile,\n        threading,\n        tokenize,\n        traceback,\n        types,\n        typing,\n        unittest,\n        weakref,\n        _collections_abc,\n        _weakrefset,\n    )\n]\nFILENAME_ALLOWLIST = {\n    torch.nn.Sequential.__init__.__code__.co_filename,\n}\nSKIP_DIRS_RE = None\n\n\ndef _recompile_re():\n    global SKIP_DIRS_RE\n    SKIP_DIRS_RE = re.compile(f\"^({'|'.join(map(re.escape, SKIP_DIRS))})\")\n\n\ndef add(import_name: str):\n    if isinstance(import_name, types.ModuleType):\n        return add(import_name.__name__)\n    assert isinstance(import_name, str)\n    module_spec = importlib.util.find_spec(import_name)\n    if not module_spec:\n        return\n    origin = module_spec.origin\n    if origin is None:\n        return\n    global SKIP_DIRS_RE\n    SKIP_DIRS.append(_strip_init_py(origin))\n    _recompile_re()\n\n\ndef check(filename, allow_torch=False):\n    \"\"\"Should skip this file?\"\"\"\n    if filename is None:\n        return True\n    if filename in FILENAME_ALLOWLIST:\n        return False\n    if allow_torch and is_torch(filename):\n        return False\n    return bool(SKIP_DIRS_RE.match(filename))\n\n\n# skip common third party libs\nfor _name in (\n    \"functorch\",\n    \"intel_extension_for_pytorch\",\n    \"networkx\",\n    \"numpy\",\n    \"omegaconf\",\n    \"onnx\",\n    \"onnxruntime\",\n    \"onnx_tf\",\n    \"pandas\",\n    \"sklearn\",\n    \"tabulate\",\n    \"tensorflow\",\n    \"tensorrt\",\n    \"torch2trt\",\n    \"tqdm\",\n    \"tree\",\n    \"tvm\",\n    \"fx2trt_oss\",\n):\n    add(_name)\n\n_recompile_re()\n\n\ndef is_torch_inline_allowed(filename):\n    return filename.startswith(_module_dir(torch.nn)) or filename.startswith(\n        _module_dir(torch.distributions)\n    )\n\n\ndef is_torch(filename):\n    return filename.startswith(_module_dir(torch))\n", "path": "torchdynamo/skipfiles.py"}]}