mindspore.ops.constexpr

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.

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

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

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

Examples

>>> a = (1, 2)
>>> # make an operator to calculate tuple len
>>> @constexpr
>>> def tuple_len(x):
...     return len(x)
>>> assert 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)
>>> assert tuple_len_class()(a) == 2