mindspore.ops
Operators can be used in the construct function of Cell.
Examples
- Copy>>> from mindspore.ops import operations as P
- >>> from mindspore.ops import composite as C
- >>> from mindspore.ops import functional as F
Note
The Primitive operators in operations need to be used after instantiation.
The composite operators are pre-defined combination of operator.
The functional operators are the pre-instantiated Primitive operators, which can be used directly like a function.
mindspore.ops.
getvm_impl_fn
(_prim)[source]Gets vm function by primitive obj or primitive name for c++
mindspore.ops.
opinfo_register
(_op_info)[source]- A decorator used as register of operator implementation.
Note
‘op_info’ must be a str of json format represent the op info, the op info will be added into oplib.
- Parameters
op_info (str) – op info of json format.
Returns
- Function, returns a decorator for op info register.
mindspore.ops.
constexpr
(fn=None, get_instance=True, name=None)[source]Makes a PrimitiveWithInfer operator, which infer the value while compiling.
Examples
- Copy>>> a = (1, 2)
- >>> # make a operator to calculate tuple len
- >>> @constexpr
- >>> def tuple_len(x):
- >>> return len(x)
- >>> assert tuple_len(a) == 2
- >>>
- >>> # make a 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
mindspore.ops.
primattr_register
(_fn)[source]- Primitive attributes register.
Registering the decorator of the built-in operator primitive initfunction will add all the parameters of init as operator attributes.
- Parameters
fn (function) – init function of primitive.
Returns
- function, original function.
- class
mindspore.ops.
Primitive
(name)[source] Primitive is base class for primitives in python.
- Parameters
- name (str) – Name for current Primitive.
Examples
- Copy>>> add = Primitive('add')
- >>>
- >>> # or work with prim_attr_register:
- >>> # init a Primitive class with attr1 and attr2
- >>> class Add(Primitive):
- >>> @prim_attr_register
- >>> def __init__(self, attr1, attr2):
- >>> # check attr1 and attr2 or do some initializations
- >>> # init a Primitive obj with attr1=1 and attr2=2
- >>> add = Add(attr1=1, attr2=2)
addprim_attr
(_name, value)[source]Adds primitive attribute.
- Parameters
name (str) – Attribute Name.
value (Any) – Attribute value.
initprim_io_names
(_inputs, outputs)[source]Initializes inputs and outpus name of Tensor or attributes.
setprim_instance_name
(_instance_name)[source]- Sets instance name to primitive operator.
Note
Will be called by default when user defines primitive operator.
- Parameters
-
instance_name (str) – Instance name of primitive operator set by user.
setstrategy
(_strategy)[source]- Adds strategy to primitive attribute.
Note
Valid only in semi auto parallel or auto parallel mode.
- Parameters
-
strategy (tuple) – Strategy describes the distributed parallel mode of the current primitive.
- class
mindspore.ops.
PrimitiveWithInfer
(name)[source] - PrimitiveWithInfer is base class for primitives in python and defines functions for infer of tracks in python.
There are four method can be overide to define the infer logic of the primitive: infer(), infershape(),inferdtype(), and infervalue(). If infer() is defined in primitive, the _infer() has highest priorityto be called. If __infer() is not defined, infer_shape() and infer_dtype() can be defined to describle shapeand type infer logic. The infer_value() is used for constant propogation.
- Parameters
- name (str) – Name for current Primitive.
Examples
- Copy>>> # init a Primitive class with infer
- >>> class Add(PrimitiveWithInfer):
- >>> @prim_attr_register
- >>> def __init__(self):
- >>> pass
- >>>
- >>> def infer_shape(self, x, y):
- >>> return x # output shape same as first input 'x'
- >>>
- >>> def infer_dtype(self, x, y):
- >>> return x # output type same as first input 'x'
- >>>
- >>> # init a Primitive obj
- >>> add = Add()
inferdtype
(*args_)[source]Infer output dtype based on input dtype.
- Parameters
inputs (mstype) – data type of inputs.
outputs (mstype) – data type of outputs.
infershape
(*args_)[source]Infer output shape based on input shape.
Note
The shape of scalar is an empty tuple.
infervalue
(*args_)[source]Infer output value based on input value at compile time.
- Parameters
inputs (any) – value of inputs.
outputs (any) – value of outputs.