quadrants.lang.field#
Classes#
Quadrants field class. |
|
Quadrants scalar field with SNode implementation. |
|
Quadrants bitpacked fields, where fields with quantized types are packed together. |
Module Contents#
- class quadrants.lang.field.Field(_vars)[source]#
Quadrants field class.
A field is constructed by a list of field members. For example, a scalar field has 1 field member, while a 3x3 matrix field has 9 field members. A field member is a Python Expr wrapping a C++ FieldExpression.
- Parameters:
vars (List[Expr]) – Field members.
- property snode[source]#
Gets representative SNode for info purposes.
- Returns:
Representative SNode (SNode of first field member).
- Return type:
- property layout[source]#
Canonical-axis-permutation tuple, or
Nonefor identity.Mirrors
Ndarray.layout: returns the same value the caller passed toqd.tensor(..., layout=...)(orNoneif that kwarg was omitted / was the identity permutation). Lets downstream code introspect the physical layout without having to know which backend produced the tensor.Fields constructed directly via
qd.field(..., order=...)also report their layout here — theorder=axis-string is translated into an integer permutation and stashed on the same_qd_layoutattribute that layout-tagged ndarrays use.
- has_grad() bool[source]#
Whether this field’s adjoint (reverse-mode gradient) SNode is allocated.
self.gradis non-Nonefor every real-dtype field (the wrapper is allocated up-front soqd.root.lazy_grad()can populate it later); the actual placed-or-not signal is whether the underlying SNode has been placed vianeeds_grad=True,qd.root.lazy_grad(), or an explicitqd.root.dense(...).place(field.grad). Mirrorsself.snode.ptr.has_adjoint().
- has_dual() bool[source]#
Whether this field’s dual (forward-mode gradient) SNode is allocated.
Same semantics as
has_grad()for the dual companion. Mirrorsself.snode.ptr.has_dual().
- abstract to_numpy(dtype: quadrants._lib.core.quadrants_python.DataTypeCxx | None = None, *, copy=True)[source]#
Converts self to a numpy array.
- Parameters:
copy –
True(default) returns an independent copy,Falserequires zero-copy or raises,Noneuses zero-copy when available and falls back to a copy otherwise.- Returns:
The result numpy array.
- Return type:
numpy.ndarray
- abstract to_torch(device=None, *, copy=True)[source]#
Converts self to a torch tensor.
- Parameters:
device (torch.device, optional) – The desired device of returned tensor.
copy –
True(default) returns an independent copy,Falserequires zero-copy or raises,Noneuses zero-copy when available and falls back to a copy otherwise.
- Returns:
The result torch tensor.
- Return type:
torch.tensor
- abstract from_numpy(arr)[source]#
Loads all elements from a numpy array.
The shape of the numpy array needs to be the same as self.
- Parameters:
arr (numpy.ndarray) – The source numpy array.
- from_torch(arr)[source]#
Loads all elements from a torch tensor.
The shape of the torch tensor needs to be the same as self.
- Parameters:
arr (torch.tensor) – The source torch tensor.
- class quadrants.lang.field.ScalarField(var)[source]#
Bases:
FieldQuadrants scalar field with SNode implementation.
- Parameters:
var (Expr) – Field member.
- to_dlpack(versioned=False)[source]#
Export this field as a DLPack capsule.
- Parameters:
versioned – If True, emit a DLPack v1
DLManagedTensorVersionedcapsule ("dltensor_versioned",flags=0). NumPy >= 2.1 can consume v1 capsules and returns writable arrays; NumPy 2.0 marks v0 capsules read-only; NumPy < 2.1 cannot consume v1 capsules at all. If False (default), emit a v0DLManagedTensor("dltensor"), required bytorch.utils.dlpack.from_dlpack.
Note: caller is responsible for calling qd.sync() between modifying the field and reading it.
- to_numpy(dtype=None, *, copy=True)[source]#
Converts this field to a
numpy.ndarray.- Parameters:
dtype – Optional target numpy dtype.
copy –
True(default) returns an independent copy,Falserequires zero-copy or raises,Noneuses zero-copy when available and falls back to a copy otherwise.
- to_torch(device=None, *, copy=True)[source]#
Converts this field to a
torch.Tensor.- Parameters:
device – Optional torch device for the returned tensor.
copy –
True(default) returns an independent copy,Falserequires zero-copy or raises,Noneuses zero-copy when available and falls back to a copy otherwise.