Caffe2 - Python API
A deep learning, cross platform ML framework
storage.py
1 import io
2 
3 import torch
4 from ._utils import _type, _cuda
5 
6 
7 class _StorageBase(object):
8  is_cuda = False
9  is_sparse = False
10 
11  def __str__(self):
12  content = ' ' + '\n '.join(str(self[i]) for i in range(len(self)))
13  return content + '\n[{} of size {}]'.format(torch.typename(self), len(self))
14 
15  def __repr__(self):
16  return str(self)
17 
18  def __iter__(self):
19  return iter(map(lambda i: self[i], range(self.size())))
20 
21  def __copy__(self):
22  return self.clone()
23 
24  def __deepcopy__(self, memo):
25  memo = memo.setdefault('torch', {})
26  if self._cdata in memo:
27  return memo[self._cdata]
28  new_storage = self.clone()
29  memo[self._cdata] = new_storage
30  return new_storage
31 
32  def __reduce__(self):
33  b = io.BytesIO()
34  torch.save(self, b)
35  return (_load_from_bytes, (b.getvalue(),))
36 
37  def __sizeof__(self):
38  return super(_StorageBase, self).__sizeof__() + self.element_size() * self.size()
39 
40  def clone(self):
41  """Returns a copy of this storage"""
42  device = self.get_device() if self.is_cuda else -1
43  with torch.cuda.device(device):
44  return type(self)(self.size()).copy_(self)
45 
46  def tolist(self):
47  """Returns a list containing the elements of this storage"""
48  return [v for v in self]
49 
50  def cpu(self):
51  """Returns a CPU copy of this storage if it's not already on the CPU"""
52  return self.type(getattr(torch, self.__class__.__name__))
53 
54  def double(self):
55  """Casts this storage to double type"""
56  return self.type(type(self).__module__ + '.DoubleStorage')
57 
58  def float(self):
59  """Casts this storage to float type"""
60  return self.type(type(self).__module__ + '.FloatStorage')
61 
62  def half(self):
63  """Casts this storage to half type"""
64  return self.type(type(self).__module__ + '.HalfStorage')
65 
66  def long(self):
67  """Casts this storage to long type"""
68  return self.type(type(self).__module__ + '.LongStorage')
69 
70  def int(self):
71  """Casts this storage to int type"""
72  return self.type(type(self).__module__ + '.IntStorage')
73 
74  def short(self):
75  """Casts this storage to short type"""
76  return self.type(type(self).__module__ + '.ShortStorage')
77 
78  def char(self):
79  """Casts this storage to char type"""
80  return self.type(type(self).__module__ + '.CharStorage')
81 
82  def byte(self):
83  """Casts this storage to byte type"""
84  return self.type(type(self).__module__ + '.ByteStorage')
85 
86  def bool(self):
87  """Casts this storage to bool type"""
88  return self.type(type(self).__module__ + '.BoolStorage')
89 
90  def pin_memory(self):
91  """Copies the storage to pinned memory, if it's not already pinned."""
92  if self.is_cuda:
93  raise TypeError("cannot pin '{0}' only CPU memory can be pinned"
94  .format(self.type()))
95  import torch.cuda
96  allocator = torch.cuda._host_allocator()
97  return type(self)(self.size(), allocator=allocator).copy_(self)
98 
99  def share_memory_(self):
100  """Moves the storage to shared memory.
101 
102  This is a no-op for storages already in shared memory and for CUDA
103  storages, which do not need to be moved for sharing across processes.
104  Storages in shared memory cannot be resized.
105 
106  Returns: self
107  """
108  from torch.multiprocessing import get_sharing_strategy
109  if self.is_cuda:
110  pass # CUDA doesn't use POSIX shared memory
111  elif get_sharing_strategy() == 'file_system':
112  self._share_filename_()
113  else:
114  self._share_fd_()
115  return self
116 
117  @classmethod
118  def _new_shared(cls, size):
119  """Creates a new storage in shared memory with the same data type"""
120  from torch.multiprocessing import get_sharing_strategy
121  if cls.is_cuda:
122  return cls(size)
123  elif get_sharing_strategy() == 'file_system':
124  return cls._new_using_filename(size)
125  else:
126  return cls._new_using_fd(size)
127 
128 
129 def _load_from_bytes(b):
130  return torch.load(io.BytesIO(b))
131 
132 
133 _StorageBase.type = _type
134 _StorageBase.cuda = _cuda
def _host_allocator()
Definition: __init__.py:529
def typename(o)
Define basic utilities.
Definition: __init__.py:94