We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Importing scipy.misc after torch causes segfault
As shown in below. Is this something expected?
This seems most closely related to https://github.com/pytorch/pytorch/issues/595, but it is closed without any specific solutions.
```
(venv) $ pip freeze
appdirs==1.4.0
numpy==1.12.0
packaging==16.8
pyparsing==2.1.10
PyYAML==3.12
scipy==0.18.1
six==1.10.0
torch==0.1.9.post2

(venv) $ python -c 'import torch, scipy.misc'
Segmentation fault (core dumped)

(venv) $ python -c 'import scipy.misc, torch'

(venv) $ echo 'import torch, scipy.misc' > spam.py

(venv) $ python spam.py
Segmentation fault (core dumped)

(venv) $ gdb --args python spam.py
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from python...(no debugging symbols found)...done.
(gdb) run
Starting program: /tmp/venv/bin/python spam.py
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7fffc7088700 (LWP 5549)]
[New Thread 0x7fffc6887700 (LWP 5550)]
[New Thread 0x7fffc4086700 (LWP 5551)]
[New Thread 0x7fffc1885700 (LWP 5552)]
[New Thread 0x7fffbf084700 (LWP 5553)]
[New Thread 0x7fffbc883700 (LWP 5554)]
[New Thread 0x7fffba082700 (LWP 5555)]
[New Thread 0x7fffb7881700 (LWP 5556)]
[New Thread 0x7fffb5080700 (LWP 5557)]
[New Thread 0x7fffb287f700 (LWP 5558)]
[New Thread 0x7fffb007e700 (LWP 5559)]

Program received signal SIGSEGV, Segmentation fault.
0x00007fffc9d65fc0 in PyArray_API () from /tmp/venv/local/lib/python2.7/site-packages/numpy/core/multiarray.so
```

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `torch/__init__.py`
Content:
```
1 """
2 The torch package contains data structures for multi-dimensional
3 tensors and mathematical operations over these are defined.
4 Additionally, it provides many utilities for efficient serializing of
5 Tensors and arbitrary types, and other useful utilities.
6 
7 It has a CUDA counterpart, that enables you to run your tensor computations
8 on an NVIDIA GPU with compute capability >= 2.0.
9 """
10 
11 import sys
12 from ._utils import _import_dotted_name
13 from .version import __version__
14 
15 __all__ = [
16     'typename', 'is_tensor', 'is_storage', 'set_default_tensor_type',
17     'set_rng_state', 'get_rng_state', 'manual_seed', 'initial_seed',
18     'save', 'load', 'set_printoptions', 'chunk', 'split', 'stack',
19     'DoubleStorage', 'FloatStorage', 'LongStorage', 'IntStorage',
20     'ShortStorage', 'CharStorage', 'ByteStorage',
21     'DoubleTensor', 'FloatTensor', 'LongTensor', 'IntTensor',
22     'ShortTensor', 'CharTensor', 'ByteTensor',
23 ]
24 
25 ################################################################################
26 # Load the extension module
27 ################################################################################
28 
29 # Loading the extension with RTLD_GLOBAL option allows to not link extension
30 # modules against the _C shared object. Their missing THP symbols will be
31 # automatically filled by the dynamic loader.
32 import os as _dl_flags
33 
34 # first check if the os package has the required flags
35 if not hasattr(_dl_flags, 'RTLD_GLOBAL') or not hasattr(_dl_flags, 'RTLD_NOW'):
36     try:
37         # next try if DLFCN exists
38         import DLFCN as _dl_flags
39     except ImportError:
40         # as a last attempt, use compile-time constants
41         import torch._dl as _dl_flags
42 
43 old_flags = sys.getdlopenflags()
44 sys.setdlopenflags(_dl_flags.RTLD_GLOBAL | _dl_flags.RTLD_NOW)
45 
46 from torch._C import *
47 
48 __all__ += [name for name in dir(_C)
49             if name[0] != '_' and
50             not name.endswith('Base')]
51 
52 sys.setdlopenflags(old_flags)
53 del _dl_flags
54 del old_flags
55 
56 ################################################################################
57 # Define basic utilities
58 ################################################################################
59 
60 
61 def typename(o):
62     module = ''
63     class_name = ''
64     if hasattr(o, '__module__') and o.__module__ != 'builtins' \
65             and o.__module__ != '__builtin__' and o.__module__ is not None:
66         module = o.__module__ + '.'
67 
68     if hasattr(o, '__qualname__'):
69         class_name = o.__qualname__
70     elif hasattr(o, '__name__'):
71         class_name = o.__name__
72     else:
73         class_name = o.__class__.__name__
74 
75     return module + class_name
76 
77 
78 def is_tensor(obj):
79     r"""Returns True if `obj` is a pytorch tensor.
80 
81     Args:
82         obj (Object): Object to test
83     """
84     return obj.__class__ in _tensor_classes
85 
86 
87 def is_storage(obj):
88     r"""Returns True if `obj` is a pytorch storage object.
89 
90     Args:
91         obj (Object): Object to test
92     """
93     return obj.__class__ in _storage_classes
94 
95 
96 def set_default_tensor_type(t):
97     global Tensor
98     global Storage
99     Tensor = _import_dotted_name(t)
100     Storage = _import_dotted_name(t.replace('Tensor', 'Storage'))
101     _C._set_default_tensor_type(Tensor)
102 
103 
104 def set_rng_state(new_state):
105     r"""Sets the random number generator state.
106 
107     Args:
108         new_state (torch.ByteTensor): The desired state
109     """
110     default_generator.set_state(new_state)
111 
112 
113 def get_rng_state():
114     r"""Returns the random number generator state as a ByteTensor."""
115     return default_generator.get_state()
116 
117 
118 def manual_seed(seed):
119     r"""Sets the seed for generating random numbers. And returns a
120     `torch._C.Generator` object.
121 
122     Args:
123         seed (int or long): The desired seed.
124     """
125     return default_generator.manual_seed(seed)
126 
127 
128 def initial_seed():
129     r"""Returns the initial seed for generating random numbers as a
130     python `long`.
131     """
132     return default_generator.initial_seed()
133 
134 
135 from .serialization import save, load
136 from ._tensor_str import set_printoptions
137 
138 ################################################################################
139 # Define Storage and Tensor classes
140 ################################################################################
141 
142 from .storage import _StorageBase
143 from .tensor import _TensorBase
144 
145 
146 class DoubleStorage(_C.DoubleStorageBase, _StorageBase):
147     pass
148 
149 
150 class FloatStorage(_C.FloatStorageBase, _StorageBase):
151     pass
152 
153 
154 class HalfStorage(_C.HalfStorageBase, _StorageBase):
155     pass
156 
157 
158 class LongStorage(_C.LongStorageBase, _StorageBase):
159     pass
160 
161 
162 class IntStorage(_C.IntStorageBase, _StorageBase):
163     pass
164 
165 
166 class ShortStorage(_C.ShortStorageBase, _StorageBase):
167     pass
168 
169 
170 class CharStorage(_C.CharStorageBase, _StorageBase):
171     pass
172 
173 
174 class ByteStorage(_C.ByteStorageBase, _StorageBase):
175     pass
176 
177 
178 class DoubleTensor(_C.DoubleTensorBase, _TensorBase):
179 
180     def is_signed(self):
181         return True
182 
183     @classmethod
184     def storage_type(cls):
185         return DoubleStorage
186 
187 
188 class FloatTensor(_C.FloatTensorBase, _TensorBase):
189 
190     def is_signed(self):
191         return True
192 
193     @classmethod
194     def storage_type(cls):
195         return FloatStorage
196 
197 
198 class HalfTensor(_C.HalfTensorBase, _TensorBase):
199 
200     def is_signed(self):
201         return True
202 
203     @classmethod
204     def storage_type(cls):
205         return HalfStorage
206 
207 
208 class LongTensor(_C.LongTensorBase, _TensorBase):
209 
210     def is_signed(self):
211         return True
212 
213     @classmethod
214     def storage_type(cls):
215         return LongStorage
216 
217 
218 class IntTensor(_C.IntTensorBase, _TensorBase):
219 
220     def is_signed(self):
221         return True
222 
223     @classmethod
224     def storage_type(cls):
225         return IntStorage
226 
227 
228 class ShortTensor(_C.ShortTensorBase, _TensorBase):
229 
230     def is_signed(self):
231         return True
232 
233     @classmethod
234     def storage_type(cls):
235         return ShortStorage
236 
237 
238 class CharTensor(_C.CharTensorBase, _TensorBase):
239 
240     def is_signed(self):
241         # TODO
242         return False
243 
244     @classmethod
245     def storage_type(cls):
246         return CharStorage
247 
248 
249 class ByteTensor(_C.ByteTensorBase, _TensorBase):
250 
251     def is_signed(self):
252         return False
253 
254     @classmethod
255     def storage_type(cls):
256         return ByteStorage
257 
258 
259 _storage_classes = {
260     DoubleStorage, FloatStorage, LongStorage, IntStorage, ShortStorage,
261     CharStorage, ByteStorage,
262 }
263 
264 _tensor_classes = {
265     DoubleTensor, FloatTensor, LongTensor, IntTensor, ShortTensor,
266     CharTensor, ByteTensor,
267 }
268 
269 
270 set_default_tensor_type('torch.FloatTensor')
271 
272 ################################################################################
273 # Import interface functions defined in Python
274 ################################################################################
275 
276 from .functional import *
277 
278 ################################################################################
279 # Initialize extension
280 ################################################################################
281 
282 # Shared memory manager needs to know the exact location of manager executable
283 import os
284 manager_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'lib', 'torch_shm_manager')
285 if sys.version_info[0] >= 3:
286     manager_path = bytes(manager_path, 'ascii')
287 
288 _C._initExtension(manager_path)
289 
290 del os
291 del manager_path
292 
293 ################################################################################
294 # Remove unnecessary members
295 ################################################################################
296 
297 del DoubleStorageBase
298 del FloatStorageBase
299 del LongStorageBase
300 del IntStorageBase
301 del ShortStorageBase
302 del CharStorageBase
303 del ByteStorageBase
304 del DoubleTensorBase
305 del FloatTensorBase
306 del LongTensorBase
307 del IntTensorBase
308 del ShortTensorBase
309 del CharTensorBase
310 del ByteTensorBase
311 
312 del SparseDoubleTensorBase
313 del SparseFloatTensorBase
314 del SparseLongTensorBase
315 del SparseIntTensorBase
316 del SparseShortTensorBase
317 del SparseCharTensorBase
318 del SparseByteTensorBase
319 
320 ################################################################################
321 # Import most common subpackages
322 ################################################################################
323 
324 import torch.cuda
325 import torch.autograd
326 import torch.nn
327 import torch.optim
328 import torch.multiprocessing
329 
330 # attach docstrings to torch and tensor functions
331 from . import _torch_docs, _tensor_docs
332 del _torch_docs, _tensor_docs
333
```
--- 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/torch/__init__.py b/torch/__init__.py
--- a/torch/__init__.py
+++ b/torch/__init__.py
@@ -31,6 +31,13 @@
 # automatically filled by the dynamic loader.
 import os as _dl_flags
 
+# if we have numpy, it *must* be imported before the call to setdlopenflags()
+# or there is risk that later c modules will segfault when importing numpy
+try:
+    import numpy as np
+except:
+    pass
+
 # first check if the os package has the required flags
 if not hasattr(_dl_flags, 'RTLD_GLOBAL') or not hasattr(_dl_flags, 'RTLD_NOW'):
     try:

{"golden_diff": "diff --git a/torch/__init__.py b/torch/__init__.py\n--- a/torch/__init__.py\n+++ b/torch/__init__.py\n@@ -31,6 +31,13 @@\n # automatically filled by the dynamic loader.\n import os as _dl_flags\n \n+# if we have numpy, it *must* be imported before the call to setdlopenflags()\n+# or there is risk that later c modules will segfault when importing numpy\n+try:\n+    import numpy as np\n+except:\n+    pass\n+\n # first check if the os package has the required flags\n if not hasattr(_dl_flags, 'RTLD_GLOBAL') or not hasattr(_dl_flags, 'RTLD_NOW'):\n     try:\n", "issue": "Importing scipy.misc after torch causes segfault\nAs shown in below. Is this something expected?\r\nThis seems most closely related to https://github.com/pytorch/pytorch/issues/595, but it is closed without any specific solutions.\r\n```\r\n(venv) $ pip freeze\r\nappdirs==1.4.0\r\nnumpy==1.12.0\r\npackaging==16.8\r\npyparsing==2.1.10\r\nPyYAML==3.12\r\nscipy==0.18.1\r\nsix==1.10.0\r\ntorch==0.1.9.post2\r\n\r\n(venv) $ python -c 'import torch, scipy.misc'\r\nSegmentation fault (core dumped)\r\n\r\n(venv) $ python -c 'import scipy.misc, torch'\r\n\r\n(venv) $ echo 'import torch, scipy.misc' > spam.py\r\n\r\n(venv) $ python spam.py\r\nSegmentation fault (core dumped)\r\n\r\n(venv) $ gdb --args python spam.py\r\nGNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1\r\nCopyright (C) 2014 Free Software Foundation, Inc.\r\nLicense GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\r\nThis is free software: you are free to change and redistribute it.\r\nThere is NO WARRANTY, to the extent permitted by law.  Type \"show copying\"\r\nand \"show warranty\" for details.\r\nThis GDB was configured as \"x86_64-linux-gnu\".\r\nType \"show configuration\" for configuration details.\r\nFor bug reporting instructions, please see:\r\n<http://www.gnu.org/software/gdb/bugs/>.\r\nFind the GDB manual and other documentation resources online at:\r\n<http://www.gnu.org/software/gdb/documentation/>.\r\nFor help, type \"help\".\r\nType \"apropos word\" to search for commands related to \"word\"...\r\nReading symbols from python...(no debugging symbols found)...done.\r\n(gdb) run\r\nStarting program: /tmp/venv/bin/python spam.py\r\n[Thread debugging using libthread_db enabled]\r\nUsing host libthread_db library \"/lib/x86_64-linux-gnu/libthread_db.so.1\".\r\n[New Thread 0x7fffc7088700 (LWP 5549)]\r\n[New Thread 0x7fffc6887700 (LWP 5550)]\r\n[New Thread 0x7fffc4086700 (LWP 5551)]\r\n[New Thread 0x7fffc1885700 (LWP 5552)]\r\n[New Thread 0x7fffbf084700 (LWP 5553)]\r\n[New Thread 0x7fffbc883700 (LWP 5554)]\r\n[New Thread 0x7fffba082700 (LWP 5555)]\r\n[New Thread 0x7fffb7881700 (LWP 5556)]\r\n[New Thread 0x7fffb5080700 (LWP 5557)]\r\n[New Thread 0x7fffb287f700 (LWP 5558)]\r\n[New Thread 0x7fffb007e700 (LWP 5559)]\r\n\r\nProgram received signal SIGSEGV, Segmentation fault.\r\n0x00007fffc9d65fc0 in PyArray_API () from /tmp/venv/local/lib/python2.7/site-packages/numpy/core/multiarray.so\r\n```\n", "before_files": [{"content": "\"\"\"\nThe torch package contains data structures for multi-dimensional\ntensors and mathematical operations over these are defined.\nAdditionally, it provides many utilities for efficient serializing of\nTensors and arbitrary types, and other useful utilities.\n\nIt has a CUDA counterpart, that enables you to run your tensor computations\non an NVIDIA GPU with compute capability >= 2.0.\n\"\"\"\n\nimport sys\nfrom ._utils import _import_dotted_name\nfrom .version import __version__\n\n__all__ = [\n    'typename', 'is_tensor', 'is_storage', 'set_default_tensor_type',\n    'set_rng_state', 'get_rng_state', 'manual_seed', 'initial_seed',\n    'save', 'load', 'set_printoptions', 'chunk', 'split', 'stack',\n    'DoubleStorage', 'FloatStorage', 'LongStorage', 'IntStorage',\n    'ShortStorage', 'CharStorage', 'ByteStorage',\n    'DoubleTensor', 'FloatTensor', 'LongTensor', 'IntTensor',\n    'ShortTensor', 'CharTensor', 'ByteTensor',\n]\n\n################################################################################\n# Load the extension module\n################################################################################\n\n# Loading the extension with RTLD_GLOBAL option allows to not link extension\n# modules against the _C shared object. Their missing THP symbols will be\n# automatically filled by the dynamic loader.\nimport os as _dl_flags\n\n# first check if the os package has the required flags\nif not hasattr(_dl_flags, 'RTLD_GLOBAL') or not hasattr(_dl_flags, 'RTLD_NOW'):\n    try:\n        # next try if DLFCN exists\n        import DLFCN as _dl_flags\n    except ImportError:\n        # as a last attempt, use compile-time constants\n        import torch._dl as _dl_flags\n\nold_flags = sys.getdlopenflags()\nsys.setdlopenflags(_dl_flags.RTLD_GLOBAL | _dl_flags.RTLD_NOW)\n\nfrom torch._C import *\n\n__all__ += [name for name in dir(_C)\n            if name[0] != '_' and\n            not name.endswith('Base')]\n\nsys.setdlopenflags(old_flags)\ndel _dl_flags\ndel old_flags\n\n################################################################################\n# Define basic utilities\n################################################################################\n\n\ndef typename(o):\n    module = ''\n    class_name = ''\n    if hasattr(o, '__module__') and o.__module__ != 'builtins' \\\n            and o.__module__ != '__builtin__' and o.__module__ is not None:\n        module = o.__module__ + '.'\n\n    if hasattr(o, '__qualname__'):\n        class_name = o.__qualname__\n    elif hasattr(o, '__name__'):\n        class_name = o.__name__\n    else:\n        class_name = o.__class__.__name__\n\n    return module + class_name\n\n\ndef is_tensor(obj):\n    r\"\"\"Returns True if `obj` is a pytorch tensor.\n\n    Args:\n        obj (Object): Object to test\n    \"\"\"\n    return obj.__class__ in _tensor_classes\n\n\ndef is_storage(obj):\n    r\"\"\"Returns True if `obj` is a pytorch storage object.\n\n    Args:\n        obj (Object): Object to test\n    \"\"\"\n    return obj.__class__ in _storage_classes\n\n\ndef set_default_tensor_type(t):\n    global Tensor\n    global Storage\n    Tensor = _import_dotted_name(t)\n    Storage = _import_dotted_name(t.replace('Tensor', 'Storage'))\n    _C._set_default_tensor_type(Tensor)\n\n\ndef set_rng_state(new_state):\n    r\"\"\"Sets the random number generator state.\n\n    Args:\n        new_state (torch.ByteTensor): The desired state\n    \"\"\"\n    default_generator.set_state(new_state)\n\n\ndef get_rng_state():\n    r\"\"\"Returns the random number generator state as a ByteTensor.\"\"\"\n    return default_generator.get_state()\n\n\ndef manual_seed(seed):\n    r\"\"\"Sets the seed for generating random numbers. And returns a\n    `torch._C.Generator` object.\n\n    Args:\n        seed (int or long): The desired seed.\n    \"\"\"\n    return default_generator.manual_seed(seed)\n\n\ndef initial_seed():\n    r\"\"\"Returns the initial seed for generating random numbers as a\n    python `long`.\n    \"\"\"\n    return default_generator.initial_seed()\n\n\nfrom .serialization import save, load\nfrom ._tensor_str import set_printoptions\n\n################################################################################\n# Define Storage and Tensor classes\n################################################################################\n\nfrom .storage import _StorageBase\nfrom .tensor import _TensorBase\n\n\nclass DoubleStorage(_C.DoubleStorageBase, _StorageBase):\n    pass\n\n\nclass FloatStorage(_C.FloatStorageBase, _StorageBase):\n    pass\n\n\nclass HalfStorage(_C.HalfStorageBase, _StorageBase):\n    pass\n\n\nclass LongStorage(_C.LongStorageBase, _StorageBase):\n    pass\n\n\nclass IntStorage(_C.IntStorageBase, _StorageBase):\n    pass\n\n\nclass ShortStorage(_C.ShortStorageBase, _StorageBase):\n    pass\n\n\nclass CharStorage(_C.CharStorageBase, _StorageBase):\n    pass\n\n\nclass ByteStorage(_C.ByteStorageBase, _StorageBase):\n    pass\n\n\nclass DoubleTensor(_C.DoubleTensorBase, _TensorBase):\n\n    def is_signed(self):\n        return True\n\n    @classmethod\n    def storage_type(cls):\n        return DoubleStorage\n\n\nclass FloatTensor(_C.FloatTensorBase, _TensorBase):\n\n    def is_signed(self):\n        return True\n\n    @classmethod\n    def storage_type(cls):\n        return FloatStorage\n\n\nclass HalfTensor(_C.HalfTensorBase, _TensorBase):\n\n    def is_signed(self):\n        return True\n\n    @classmethod\n    def storage_type(cls):\n        return HalfStorage\n\n\nclass LongTensor(_C.LongTensorBase, _TensorBase):\n\n    def is_signed(self):\n        return True\n\n    @classmethod\n    def storage_type(cls):\n        return LongStorage\n\n\nclass IntTensor(_C.IntTensorBase, _TensorBase):\n\n    def is_signed(self):\n        return True\n\n    @classmethod\n    def storage_type(cls):\n        return IntStorage\n\n\nclass ShortTensor(_C.ShortTensorBase, _TensorBase):\n\n    def is_signed(self):\n        return True\n\n    @classmethod\n    def storage_type(cls):\n        return ShortStorage\n\n\nclass CharTensor(_C.CharTensorBase, _TensorBase):\n\n    def is_signed(self):\n        # TODO\n        return False\n\n    @classmethod\n    def storage_type(cls):\n        return CharStorage\n\n\nclass ByteTensor(_C.ByteTensorBase, _TensorBase):\n\n    def is_signed(self):\n        return False\n\n    @classmethod\n    def storage_type(cls):\n        return ByteStorage\n\n\n_storage_classes = {\n    DoubleStorage, FloatStorage, LongStorage, IntStorage, ShortStorage,\n    CharStorage, ByteStorage,\n}\n\n_tensor_classes = {\n    DoubleTensor, FloatTensor, LongTensor, IntTensor, ShortTensor,\n    CharTensor, ByteTensor,\n}\n\n\nset_default_tensor_type('torch.FloatTensor')\n\n################################################################################\n# Import interface functions defined in Python\n################################################################################\n\nfrom .functional import *\n\n################################################################################\n# Initialize extension\n################################################################################\n\n# Shared memory manager needs to know the exact location of manager executable\nimport os\nmanager_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'lib', 'torch_shm_manager')\nif sys.version_info[0] >= 3:\n    manager_path = bytes(manager_path, 'ascii')\n\n_C._initExtension(manager_path)\n\ndel os\ndel manager_path\n\n################################################################################\n# Remove unnecessary members\n################################################################################\n\ndel DoubleStorageBase\ndel FloatStorageBase\ndel LongStorageBase\ndel IntStorageBase\ndel ShortStorageBase\ndel CharStorageBase\ndel ByteStorageBase\ndel DoubleTensorBase\ndel FloatTensorBase\ndel LongTensorBase\ndel IntTensorBase\ndel ShortTensorBase\ndel CharTensorBase\ndel ByteTensorBase\n\ndel SparseDoubleTensorBase\ndel SparseFloatTensorBase\ndel SparseLongTensorBase\ndel SparseIntTensorBase\ndel SparseShortTensorBase\ndel SparseCharTensorBase\ndel SparseByteTensorBase\n\n################################################################################\n# Import most common subpackages\n################################################################################\n\nimport torch.cuda\nimport torch.autograd\nimport torch.nn\nimport torch.optim\nimport torch.multiprocessing\n\n# attach docstrings to torch and tensor functions\nfrom . import _torch_docs, _tensor_docs\ndel _torch_docs, _tensor_docs\n", "path": "torch/__init__.py"}], "after_files": [{"content": "\"\"\"\nThe torch package contains data structures for multi-dimensional\ntensors and mathematical operations over these are defined.\nAdditionally, it provides many utilities for efficient serializing of\nTensors and arbitrary types, and other useful utilities.\n\nIt has a CUDA counterpart, that enables you to run your tensor computations\non an NVIDIA GPU with compute capability >= 2.0.\n\"\"\"\n\nimport sys\nfrom ._utils import _import_dotted_name\nfrom .version import __version__\n\n__all__ = [\n    'typename', 'is_tensor', 'is_storage', 'set_default_tensor_type',\n    'set_rng_state', 'get_rng_state', 'manual_seed', 'initial_seed',\n    'save', 'load', 'set_printoptions', 'chunk', 'split', 'stack',\n    'DoubleStorage', 'FloatStorage', 'LongStorage', 'IntStorage',\n    'ShortStorage', 'CharStorage', 'ByteStorage',\n    'DoubleTensor', 'FloatTensor', 'LongTensor', 'IntTensor',\n    'ShortTensor', 'CharTensor', 'ByteTensor',\n]\n\n################################################################################\n# Load the extension module\n################################################################################\n\n# Loading the extension with RTLD_GLOBAL option allows to not link extension\n# modules against the _C shared object. Their missing THP symbols will be\n# automatically filled by the dynamic loader.\nimport os as _dl_flags\n\n# if we have numpy, it *must* be imported before the call to setdlopenflags()\n# or there is risk that later c modules will segfault when importing numpy\ntry:\n    import numpy as np\nexcept:\n    pass\n\n# first check if the os package has the required flags\nif not hasattr(_dl_flags, 'RTLD_GLOBAL') or not hasattr(_dl_flags, 'RTLD_NOW'):\n    try:\n        # next try if DLFCN exists\n        import DLFCN as _dl_flags\n    except ImportError:\n        # as a last attempt, use compile-time constants\n        import torch._dl as _dl_flags\n\nold_flags = sys.getdlopenflags()\nsys.setdlopenflags(_dl_flags.RTLD_GLOBAL | _dl_flags.RTLD_NOW)\n\nfrom torch._C import *\n\n__all__ += [name for name in dir(_C)\n            if name[0] != '_' and\n            not name.endswith('Base')]\n\nsys.setdlopenflags(old_flags)\ndel _dl_flags\ndel old_flags\n\n################################################################################\n# Define basic utilities\n################################################################################\n\n\ndef typename(o):\n    module = ''\n    class_name = ''\n    if hasattr(o, '__module__') and o.__module__ != 'builtins' \\\n            and o.__module__ != '__builtin__' and o.__module__ is not None:\n        module = o.__module__ + '.'\n\n    if hasattr(o, '__qualname__'):\n        class_name = o.__qualname__\n    elif hasattr(o, '__name__'):\n        class_name = o.__name__\n    else:\n        class_name = o.__class__.__name__\n\n    return module + class_name\n\n\ndef is_tensor(obj):\n    r\"\"\"Returns True if `obj` is a pytorch tensor.\n\n    Args:\n        obj (Object): Object to test\n    \"\"\"\n    return obj.__class__ in _tensor_classes\n\n\ndef is_storage(obj):\n    r\"\"\"Returns True if `obj` is a pytorch storage object.\n\n    Args:\n        obj (Object): Object to test\n    \"\"\"\n    return obj.__class__ in _storage_classes\n\n\ndef set_default_tensor_type(t):\n    global Tensor\n    global Storage\n    Tensor = _import_dotted_name(t)\n    Storage = _import_dotted_name(t.replace('Tensor', 'Storage'))\n    _C._set_default_tensor_type(Tensor)\n\n\ndef set_rng_state(new_state):\n    r\"\"\"Sets the random number generator state.\n\n    Args:\n        new_state (torch.ByteTensor): The desired state\n    \"\"\"\n    default_generator.set_state(new_state)\n\n\ndef get_rng_state():\n    r\"\"\"Returns the random number generator state as a ByteTensor.\"\"\"\n    return default_generator.get_state()\n\n\ndef manual_seed(seed):\n    r\"\"\"Sets the seed for generating random numbers. And returns a\n    `torch._C.Generator` object.\n\n    Args:\n        seed (int or long): The desired seed.\n    \"\"\"\n    return default_generator.manual_seed(seed)\n\n\ndef initial_seed():\n    r\"\"\"Returns the initial seed for generating random numbers as a\n    python `long`.\n    \"\"\"\n    return default_generator.initial_seed()\n\n\nfrom .serialization import save, load\nfrom ._tensor_str import set_printoptions\n\n################################################################################\n# Define Storage and Tensor classes\n################################################################################\n\nfrom .storage import _StorageBase\nfrom .tensor import _TensorBase\n\n\nclass DoubleStorage(_C.DoubleStorageBase, _StorageBase):\n    pass\n\n\nclass FloatStorage(_C.FloatStorageBase, _StorageBase):\n    pass\n\n\nclass HalfStorage(_C.HalfStorageBase, _StorageBase):\n    pass\n\n\nclass LongStorage(_C.LongStorageBase, _StorageBase):\n    pass\n\n\nclass IntStorage(_C.IntStorageBase, _StorageBase):\n    pass\n\n\nclass ShortStorage(_C.ShortStorageBase, _StorageBase):\n    pass\n\n\nclass CharStorage(_C.CharStorageBase, _StorageBase):\n    pass\n\n\nclass ByteStorage(_C.ByteStorageBase, _StorageBase):\n    pass\n\n\nclass DoubleTensor(_C.DoubleTensorBase, _TensorBase):\n\n    def is_signed(self):\n        return True\n\n    @classmethod\n    def storage_type(cls):\n        return DoubleStorage\n\n\nclass FloatTensor(_C.FloatTensorBase, _TensorBase):\n\n    def is_signed(self):\n        return True\n\n    @classmethod\n    def storage_type(cls):\n        return FloatStorage\n\n\nclass HalfTensor(_C.HalfTensorBase, _TensorBase):\n\n    def is_signed(self):\n        return True\n\n    @classmethod\n    def storage_type(cls):\n        return HalfStorage\n\n\nclass LongTensor(_C.LongTensorBase, _TensorBase):\n\n    def is_signed(self):\n        return True\n\n    @classmethod\n    def storage_type(cls):\n        return LongStorage\n\n\nclass IntTensor(_C.IntTensorBase, _TensorBase):\n\n    def is_signed(self):\n        return True\n\n    @classmethod\n    def storage_type(cls):\n        return IntStorage\n\n\nclass ShortTensor(_C.ShortTensorBase, _TensorBase):\n\n    def is_signed(self):\n        return True\n\n    @classmethod\n    def storage_type(cls):\n        return ShortStorage\n\n\nclass CharTensor(_C.CharTensorBase, _TensorBase):\n\n    def is_signed(self):\n        # TODO\n        return False\n\n    @classmethod\n    def storage_type(cls):\n        return CharStorage\n\n\nclass ByteTensor(_C.ByteTensorBase, _TensorBase):\n\n    def is_signed(self):\n        return False\n\n    @classmethod\n    def storage_type(cls):\n        return ByteStorage\n\n\n_storage_classes = {\n    DoubleStorage, FloatStorage, LongStorage, IntStorage, ShortStorage,\n    CharStorage, ByteStorage,\n}\n\n_tensor_classes = {\n    DoubleTensor, FloatTensor, LongTensor, IntTensor, ShortTensor,\n    CharTensor, ByteTensor,\n}\n\n\nset_default_tensor_type('torch.FloatTensor')\n\n################################################################################\n# Import interface functions defined in Python\n################################################################################\n\nfrom .functional import *\n\n################################################################################\n# Initialize extension\n################################################################################\n\n# Shared memory manager needs to know the exact location of manager executable\nimport os\nmanager_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'lib', 'torch_shm_manager')\nif sys.version_info[0] >= 3:\n    manager_path = bytes(manager_path, 'ascii')\n\n_C._initExtension(manager_path)\n\ndel os\ndel manager_path\n\n################################################################################\n# Remove unnecessary members\n################################################################################\n\ndel DoubleStorageBase\ndel FloatStorageBase\ndel LongStorageBase\ndel IntStorageBase\ndel ShortStorageBase\ndel CharStorageBase\ndel ByteStorageBase\ndel DoubleTensorBase\ndel FloatTensorBase\ndel LongTensorBase\ndel IntTensorBase\ndel ShortTensorBase\ndel CharTensorBase\ndel ByteTensorBase\n\ndel SparseDoubleTensorBase\ndel SparseFloatTensorBase\ndel SparseLongTensorBase\ndel SparseIntTensorBase\ndel SparseShortTensorBase\ndel SparseCharTensorBase\ndel SparseByteTensorBase\n\n################################################################################\n# Import most common subpackages\n################################################################################\n\nimport torch.cuda\nimport torch.autograd\nimport torch.nn\nimport torch.optim\nimport torch.multiprocessing\n\n# attach docstrings to torch and tensor functions\nfrom . import _torch_docs, _tensor_docs\ndel _torch_docs, _tensor_docs\n", "path": "torch/__init__.py"}]}