mindspore.ops.constexpr

mindspore.ops.constexpr(fn=None, get_instance=True, name=None, reuse_result=True, check=True)[source]

Creates a PrimitiveWithInfer operator that can infer the value at compile time. We can use it to define a function to compute constant value using the constants in the constructor.

Parameters
  • fn (function) – A fn use as the infer_value of the output operator. Default: None.

  • get_instance (bool) – If true, return the instance of operator, otherwise return the operator class. Default: True.

  • name (str) – Defines the operator name. If name is None, use the function name as op name. Default: None.

  • reuse_result (bool) – If true, the operator will be executed once and reuse the result next time, otherwise the operator will always be executed. Default: True.

  • check (bool) – If ture, the parameters will be checked and the warning message will raised if the parameter is not const value. Default: True.

Examples

>>> from mindspore.ops import constexpr
>>> a = (1, 2)
>>> # make an operator to calculate tuple len
>>> @constexpr
... def tuple_len(x):
...     return len(x)
...
>>> print(tuple_len(a))
2
>>> # make an operator class to calculate tuple len
>>> @constexpr(get_instance=False, name="TupleLen")
... def tuple_len_class(x):
...     return len(x)
...
>>> print(tuple_len_class()(a))
2