mindspore.ops.constexpr(fn=None, get_instance=True, name=None)[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.

  • 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.


>>> 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))
>>> # 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))