MCPcopy
hub / github.com/tinygrad/tinygrad / allocate

Method allocate

tinygrad/device.py:142–161  ·  view source on GitHub ↗
(self, opaque=None, external_ptr=None)

Source from the content-addressed store, hash-verified

140 return self._bufs[device]
141 def ensure_allocated(self) -> Buffer: return self.allocate() if not self.is_initialized() else self
142 def allocate(self, opaque=None, external_ptr=None) -> Buffer:
143 assert not self.is_initialized(), "can't allocate already allocated buffer"
144 if DEBUG >= 7: print(f"buffer: allocate {self.nbytes} bytes on {self.device}")
145 if not self.device.startswith("NULL") and self.size > MAX_BUFFER_SIZE > 0 and (self.options is None or self.options.external_ptr is None):
146 raise RuntimeError(f"buffer of size {self.size/1e6:.2f}M is too large")
147 self.allocator:Allocator = Device[self.device].allocator
148 if external_ptr is not None:
149 self.options = replace(self.options, external_ptr=external_ptr) if self.options else BufferSpec(external_ptr=external_ptr)
150 if self._base is not None:
151 self._base.ensure_allocated()
152 self._base.allocated_views += 1
153 assert hasattr(self.allocator, "_offset"), "offset function required for view"
154 self._bufs[self.device] = self.allocator._offset(self.base._buf, self.nbytes, self.offset)
155 else:
156 self._bufs[self.device] = opaque if opaque is not None else self.allocator.alloc(self.nbytes, self.options)
157 if not self.device.startswith("DISK") and (self.options is None or self.options.external_ptr is None):
158 GlobalCounters.mem_used += self.nbytes
159 GlobalCounters.mem_used_per_device[self.device] += self.nbytes
160 if PROFILE: Buffer.profile_events.append(ProfilePointEvent(self.device, "alloc", self.trace_num, {"dtype":self.dtype, "sz":self.size}))
161 return self
162 def deallocate(self):
163 assert self.device in self._bufs, "buffer must be allocated to deallocate"
164 if DEBUG is not None and DEBUG >= 7: print(f"buffer: deallocate {self.nbytes} bytes on {self.device}")

Callers 15

__init__Method · 0.95
ensure_allocatedMethod · 0.95
_fromnpFunction · 0.80
_frompyFunction · 0.80
from_blobMethod · 0.80
optimize_local_sizeFunction · 0.80
intel_xmx.pyFile · 0.80
test_pm4.pyFile · 0.80
test_gcMethod · 0.80
test_gc_uop_in_argMethod · 0.80
_allocFunction · 0.80
_test_uop_resultFunction · 0.80

Calls 7

is_initializedMethod · 0.95
ProfilePointEventClass · 0.90
BufferSpecClass · 0.85
ensure_allocatedMethod · 0.80
appendMethod · 0.80
_offsetMethod · 0.45
allocMethod · 0.45

Tested by 15

test_gcMethod · 0.64
test_gc_uop_in_argMethod · 0.64
_allocFunction · 0.64
_test_uop_resultFunction · 0.64
test_subbuffer_allocMethod · 0.64
setUpClassMethod · 0.64
_test_single_valueFunction · 0.64
_test_single_value_constFunction · 0.64
_test_uops_resultFunction · 0.64
helper_realized_astFunction · 0.64
helper_linearizer_astFunction · 0.64