type
– Interface for types of variables#
Reference#
- class pytensor.graph.type.Type[source]#
Interface specification for variable type instances.
A Type instance is mainly responsible for two things:
creating
Variable
instances (conventionally,__call__
does this), andfiltering a value assigned to a
Variable
so that the value conforms to restrictions imposed by the type (also known as casting, this is done byfilter
).
- clone(*args, **kwargs) Type [source]#
Clone a copy of this type with the given arguments/keyword values, if any.
- constant_type[source]#
The
Type
that will be created by a call toType.make_constant
.alias of
Constant
- convert_variable(var: Variable) pytensor.graph.basic.Variable | None [source]#
Produce a
Variable
that’s compatible with bothself
andvar.type
, if possible.A compatible
Variable
is aVariable
with aType
that’s the “narrower” ofself
andvar.type
.If a compatible
Type
cannot be found, this method will returnNone
.
- abstract filter(data: Any, strict: bool = False, allow_downcast: Optional[bool] = None) D [source]#
Return data or an appropriately wrapped/converted data.
Subclass implementations should raise a TypeError exception if the data is not of an acceptable type.
- Parameters:
data (array-like) – The data to be filtered/converted.
strict (bool (optional)) – If
True
, the data returned must be the same as the data passed as an argument.allow_downcast (bool (optional)) – If
strict
isFalse
, andallow_downcast
isTrue
, the data may be cast to an appropriate type. Ifallow_downcast
isFalse
, it may only be up-cast and not lose precision. Ifallow_downcast
isNone
(default), the behaviour can be type-dependent, but for now it means only Python floats can be down-casted, and only to floatX scalars.
- filter_inplace(value: Any, storage: Any, strict: bool = False, allow_downcast: Optional[bool] = None)[source]#
Return data or an appropriately wrapped/converted data by converting it in-place.
This method allows one to reuse old allocated memory. If this method is implemented, it will be called instead of
Type.filter
.As of now, this method is not implemented and was previously used for transferring memory to and from GPU.
- Parameters:
value (array-like) –
storage (array-like) – The old value (e.g. the old NumPy array)
strict (bool) –
allow_downcast (bool (optional)) –
- Raises:
NotImplementedError –
- filter_variable(other: Union[Variable, D], allow_convert: bool = True) Variable [source]#
Convert a
other
into aVariable
with aType
that’s compatible withself
.If the involved
Type
s are not compatible, aTypeError
will be raised.
- in_same_class(otype: Type) bool | None [source]#
Determine if another
Type
represents a subset from the same “class” of types represented byself
.A “class” of types could be something like “float64 tensors with four dimensions”. One
Type
could represent a set containing only a type for “float64 tensors with shape (1, 2, 3, 4)” and another the set of “float64 tensors with shape (1, x, x, x)” for all suitable “x”.It’s up to each subclass of
Type
to determine to which “classes” of types this method applies.The default implementation assumes that all “classes” have only one unique element (i.e. it uses
self.__eq__
).
- is_super(otype: Type) bool | None [source]#
Determine if
self
is a supertype ofotype
.This method effectively implements the type relation
>
.In general,
t1.is_super(t2) == True
implies thatt1
can be replaced witht2
.See
Type.in_same_class
.- Return type:
None
if the type relation cannot be applied/determined.
- is_valid_value(data: D, strict: bool = True) bool [source]#
Return
True
for any python object that would be a legal value for aVariable
of thisType
.
- make_constant(value: D, name: Optional[str] = None) Constant [source]#
Return a new
Constant
instance of thisType
.- Parameters:
value (array-like) – The constant value.
name (None or str) – A pretty string for printing and debugging.
- make_variable(name: Optional[str] = None) Variable [source]#
Return a new
Variable
instance of thisType
.- Parameters:
name (None or str) – A pretty string for printing and debugging.
- classmethod values_eq(a: D, b: D) bool [source]#
Return
True
ifa
andb
can be considered exactly equal.a
andb
are assumed to be valid values of thisType
.
- classmethod values_eq_approx(a: D, b: D) bool [source]#
Return
True
ifa
andb
can be considered approximately equal.This function is used by PyTensor debugging tools to decide whether two values are equivalent, admitting a certain amount of numerical instability. For example, for floating-point numbers this function should be an approximate comparison.
By default, this does an exact comparison.
- variable_type[source]#
The
Type
that will be created by a call toType.make_variable
.alias of
Variable