Skip to content

Properties

Basic¤

shape property ¤

shape

ndim property ¤

ndim

dtype property ¤

dtype

T property ¤

T

Data Access¤

data property writable ¤

data

The backend array; reading it realizes the tensor.

realize ¤

realize()

Computes this tensor by running every recorded op it depends on once, elementwise chains as one fused call. Returns self.

Source code in stackgrad/tensor.py
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
def realize(self):
  """Computes this tensor by running every recorded op it depends on once, elementwise chains as one fused call. Returns self."""
  order, seen, stack = [], set(), [(self, False)]
  while stack:  # iterative, so a deep graph cannot hit the recursion limit
    node, inputs_done = stack.pop()
    if inputs_done:
      order.append(node)
    elif node._buf is None and node not in seen:
      seen.add(node)
      stack.append((node, True))
      stack.extend((x, False) for x in node._lazy[2] if isinstance(x, Tensor))
  runs, run_of = [], {}  # consecutive fusable nodes share a run
  for node in order:
    if not (runs and _fusable(node) and _fusable(runs[-1][-1])):
      runs.append([])
    runs[-1].append(node)
    run_of[node] = len(runs)
  # a run returns only what the caller or a later run reads; the rest keep _lazy, so reading one later recomputes it
  needed = {id(self)}
  for node in order:
    needed.update(id(x) for x in node._lazy[2] if isinstance(x, Tensor) and run_of.get(x) != run_of[node])
  del order, seen, run_of  # holding the graph here kept every intermediate buffer alive until realize returned
  for i, run in enumerate(runs):
    runs[i] = None  # a buffer goes as soon as its last reader has run and dropped its _lazy
    if len(run) > 1:
      _realize_fused(run, [node for node in run if id(node) in needed])
      continue
    node = run[0]
    ctx, kwargs, inputs = node._lazy
    args = [x._buf if isinstance(x, Tensor) else x for x in inputs]
    if _profiler is None:
      out = ctx.forward(*args, **kwargs)
    else:
      out = _profiler.time(type(ctx).__name__, node.device, ctx.forward, *args, **kwargs)
    _store(node, out)
  return self

numpy ¤

numpy()

The data as a NumPy array on the host (a copy).

Source code in stackgrad/tensor.py
391
392
393
def numpy(self):
  """The data as a NumPy array on the host (a copy)."""
  return backend.get(self.device).to_numpy(self.data)

item ¤

item()
Source code in stackgrad/tensor.py
395
396
def item(self):
  return self.data.item()

to ¤

to(device=None, dtype=None)

This tensor outside the graph, on device in dtype ("bfloat16", ...); None keeps the current one.

Source code in stackgrad/tensor.py
383
384
385
386
387
388
389
def to(self, device=None, dtype=None):
  """This tensor outside the graph, on `device` in `dtype` ("bfloat16", ...); None keeps the current one."""
  device = device or self.device
  data = self.data if device == self.device else backend.get(device).asarray(self.numpy())
  if dtype is not None:
    data = data.astype(backend.get(device).dtype(dtype))
  return Tensor(data, device=device, requires_grad=self.requires_grad)

detach ¤

detach()

Return a tensor sharing this data but cut out of the graph.

Source code in stackgrad/tensor.py
378
379
380
def detach(self):
  """Return a tensor sharing this data but cut out of the graph."""
  return Tensor(self.data, device=self.device)