mindspore.ops.CustomOpBuilder

View Source On Gitee
class mindspore.ops.CustomOpBuilder(name, sources, backend=None, include_paths=None, cflags=None, ldflags=None, **kwargs)[source]

CustomOpBuilder is used to initialize and configure custom operators for MindSpore. Users can define and load custom operator modules through this class and apply them to the network.

In most cases, users only need to provide the source files and additional compilation options in the constructor and call the load method to complete the compilation and loading of the operator. If users have specific customization requirements, they can inherit this class and override certain methods. It is important to note that if methods are overridden, some parameters passed to the constructor may be ignored.

Warning

This is an experimental API that is subject to change.

Parameters
  • name (str) – The unique name of the custom operator module, used to identify the operator.

  • sources (Union[str, list[str]]) – The source file(s) of the custom operator. It can be a single file path or a list of file paths.

  • backend (str, optional) – The target backend for the operator, such as "CPU" or "Ascend". Default: None.

  • include_paths (list[str], optional) – Additionally included paths needed during compilation. Default: None.

  • cflags (str, optional) – Extra C++ compiler flags to be used during compilation. Default: None.

  • ldflags (str, optional) – Extra linker flags to be used during linking. Default: None.

  • kwargs (dict, optional) – Additional keyword arguments for future extensions or specific custom requirements.

Note

  • If the backend argument is provided, additional default flags will be automatically added to the compilation and linking steps to support the operator's target backend. The default options can be referenced in the implementation of the get_cflags and get_ldflags methods in the CustomOpBuilder.

  • The sources argument must point to valid source files for the custom operator.

Supported Platforms:

Ascend CPU

Examples

>>> from mindspore import ops
>>> builder = ops.CustomOpBuilder(
...     name="custom_op_cpu",
...     sources="custom_ops_impl/pybind_op_cpu.cpp",
...     backend="CPU"
... )
>>> my_ops = builder.load()
build()[source]

Build the custom operator module.

This method generates a dynamic library file for the custom operator based on the provided source files, include paths, compilation flags, and link flags.

Returns

str, The path to the compiled module.

get_cflags()[source]

Get the C++ compiler flags for building the custom operator.

Returns

list[str], A list of C++ compiler flags.

get_include_paths()[source]

Get the include paths required for compiling the custom operator.

Returns

list[str], A list of include paths.

get_ldflags()[source]

Get the linker flags for building the custom operator.

Returns

list[str], A list of linker flags.

get_sources()[source]

Get the source files for the custom operator.

Returns

str or list[str], The source file(s) for the operator.

load()[source]

Build and load the custom operator module.

Returns

Module, The loaded custom operator module.