mindspore.jit_class

View Source On Gitee
mindspore.jit_class(cls)[source]

Class decorator for user-defined classes.

This allows MindSpore to identify user-defined classes and thus obtain their attributes and methods.

Parameters

cls (Class) – User-defined class.

Returns

Class.

Raises
  • TypeError – If jit_class is used for non-class types or nn.Cell.

  • AttributeError – If the private attributes or magic methods of the class decorated with jit_class is called.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore.nn as nn
>>> from mindspore import jit_class
...
>>> @jit_class
... class UserDefinedNet:
...     def __init__(self):
...         self.value = 10
...
...     def func(self, x):
...         return 2 * x
...
>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.net = UserDefinedNet()
...
...     def construct(self, x):
...         out = self.net.value + self.net.func(x)
...         return out
...
>>> net = Net()
>>> out = net(5)
>>> print(out)
20