mindspore.ops

Operators can be used in the construct function of Cell.

Examples

>>> 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 the pre-defined combination of operators.

  • The functional operators are the pre-instantiated Primitive operators, which can be used directly as a function.

  • For functional operators usage, please refer to https://gitee.com/mindspore/mindspore/blob/master/mindspore/ops/functional.py

class mindspore.ops.AiCPURegOp(op_name)[source]

Class for AiCPU op info register

attr(name=None, value_type=None, value=None, **kwargs)[source]

Register AiCPU op attribute information.

Parameters
  • name (str) – Name of the attribute. Default: None.

  • value_type (str) – Value type of the attribute. Default: None.

  • value (str) – Value of the attribute. Default: None.

  • kwargs (dict) – Other information of the attribute.

input(index=None, name=None, param_type=None, **kwargs)[source]

Register AiCPU op input information.

Parameters
  • index (int) – Order of the input. Default: None.

  • name (str) – Name of the input. Default: None.

  • param_type (str) – Param type of the input. Default: None.

  • kwargs (dict) – Other information of the input.

output(index=None, name=None, param_type=None, **kwargs)[source]

Register AiCPU op output information.

Parameters
  • index (int) – Order of the output. Default: None.

  • name (str) – Name of the output. Default: None.

  • param_type (str) – Param type of the output. Default: None.

  • kwargs (dict) – Other information of the output.

class mindspore.ops.DataType[source]

Various combinations of dtype and format.

The current list below may be incomplete. Please add it if necessary.

class mindspore.ops.Primitive(name)[source]

Primitive is the base class of primitives in python.

Parameters

name (str) – Name for the current Primitive.

Examples

>>> 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)
add_prim_attr(name, value)[source]

Adds primitive attribute.

Parameters
  • name (str) – Attribute Name.

  • value (Any) – Attribute value.

check_elim(*args)[source]

Check if certain inputs should go to the backend. Subclass in need should override this method.

Parameters

*args (Primitive args) – Same as arguments of current Primitive.

Returns

A tuple consisting of two elements. The first element indicates whether we should filter out current arguments; the seconde element is the output if we need to filter out the arguments.

init_prim_io_names(inputs, outputs)[source]

Initializes the name of inputs and outpus of Tensor or attributes.

Parameters
  • inputs (list[str]) – list of inputs names.

  • outputs (list[str]) – list of outputs names.

set_prim_instance_name(instance_name)[source]

Set instance name to primitive operator.

Note

It will be called by default when user defines primitive operator.

Parameters

instance_name (str) – Instance name of primitive operator set by user.

set_strategy(strategy)[source]

Add strategies to primitive attribute.

Note

It is valid only in semi auto parallel or auto parallel mode. In other parallel modes, strategies set here will be ignored.

Parameters

strategy (tuple) – Strategy describes the distributed parallel mode of the current primitive.

property update_parameter

Whether the primitive will update the value of parameter.

class mindspore.ops.PrimitiveWithInfer(name)[source]

PrimitiveWithInfer is the base class of primitives in python defines functions for tracking inference in python.

There are four method can be overide to define the infer logic of the primitive: __infer__(), infer_shape(), infer_dtype(), and infer_value(). If __infer__() is defined in primitive, the __infer__() has highest priority to be called. If __infer__() is not defined, infer_shape() and infer_dtype() can be defined to describe the infer logic of the shape and type. The infer_value() is used for constant propagation.

Parameters

name (str) – Name of the current Primitive.

Examples

>>> # 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()
infer_dtype(*args)[source]

Infer output dtype based on input dtype.

Parameters

args (mindspore.dtype) – data type of inputs.

Returns

mindspore.dtype, data type of outputs.

infer_shape(*args)[source]

Infer output shape based on input shape.

Note

The shape of scalar is an empty tuple.

Parameters

args (tuple(int)) – shapes of input tensors.

Returns

tuple(int), shapes of output tensors.

infer_value(*args)[source]

Infer output value based on input value at compile time.

Parameters

args (Any) – value of inputs.

Returns

Value of outputs. Return None, the value can not be inferred at compile time in this case.

class mindspore.ops.TBERegOp(op_name)[source]

Class for TBE op info register.

async_flag(async_flag)[source]

Define the calculation efficiency of the operator, whether the asynchronous calculation is supported.

Parameters

async_flag (bool) – Value of async flag. Default: false.

attr(name=None, param_type=None, value_type=None, value=None, default_value=None, **kwargs)[source]

Register TBE op attribute information.

Parameters
  • name (str) – Name of the attribute. Default: None.

  • param_type (str) – Param type of the attribute. Default: None.

  • value_type (str) – Type of the attribute. Default: None.

  • value (str) – Value of the attribute. Default: None.

  • default_value (str) – Default value of attribute. Default: None.

  • kwargs (dict) – Other information of the attribute.

binfile_name(binfile_name)[source]

Set the binary file name of the operator, it is optional.

Parameters

binfile_name (str) – The binary file name of the operator.

compute_cost(compute_cost)[source]

Define the calculation efficiency of operator, which refers to the value of the cost model in the tiling module.

Parameters

compute_cost (int) – Value of compute cost. Default: 10.

dynamic_format(dynamic_format)[source]

Whether the operator supports dynamic selection of format and dtype or not.

Parameters

dynamic_format (bool) – Value of dynamic format. Default: false.

input(index=None, name=None, need_compile=None, param_type=None, shape=None, **kwargs)[source]

Register TBE op input information.

Parameters
  • index (int) – Order of the input. Default: None.

  • name (str) – Name of the input. Default: None.

  • need_compile (bool) – Whether the input needs to be compiled or not. Default: None.

  • param_type (str) – Type of the input. Default: None.

  • shape (str) – Shape of the input. Default: None.

  • kwargs (dict) – Other information of the input.

kernel_name(kernel_name)[source]

The name of operator kernel.

Parameters

kernel_name (str) – Name of operator kernel.

op_pattern(pattern=None)[source]

The behavior type of opeator, such as broadcast, reduce and so on.

Parameters

pattern (str) – Value of op pattern.

output(index=None, name=None, need_compile=None, param_type=None, shape=None, **kwargs)[source]

Register TBE op output information.

Parameters
  • index (int) – Order of the output. Default: None.

  • name (str) – Name of the output. Default: None.

  • need_compile (bool) – Whether the output needs to be compiled or not. Default: None.

  • param_type (str) – Type of the output. Default: None.

  • shape (str) – Shape of the output. Default: None.

  • kwargs (dict) – Other information of the output.

partial_flag(partial_flag)[source]

Define the calculation efficiency of operator, whether the partial calculation is supported.

Parameters

partial_flag (bool) – Value of partial flag. Default: true.

reshape_type(reshape_type)[source]

Reshape type of operator.

Parameters

reshape_type (str) – Value of reshape type.

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

Make 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 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.get_vm_impl_fn(prim)[source]

Get the virtual implementation function by a primitive object or primitive name.

Parameters

prim (Union[Primitive, str]) – primitive object or name for operator register.

Returns

function, vm function

mindspore.ops.op_info_register(op_info)[source]

A decorator which is used to register an operator.

Note

‘op_info’ should represent the operator information by string with json format. The ‘op_info’ will be added into oplib.

Parameters

op_info (str or dict) – operator information in json format.

Returns

Function, returns a decorator for op info register.

mindspore.ops.prim_attr_register(fn)[source]

Primitive attributes register.

Register the decorator of the built-in operator primitive ‘__init__’. The function will add all the parameters of ‘__init__’ as operator attributes.

Parameters

fn (function) – __init__ function of primitive.

Returns

function, original function.