mindspore.utils

mindspore.utils.stress_detect(detect_type='aic')[source]

This api will be deprecated and removed in future versions, please use the api mindspore.tools.stress_detect() instead.

Parameters

detect_type (str, optional) – The type of stress test to perform. There are two options available: 'aic' and 'hccs', which perform AiCore and HCCS link stress tests on the device, respectively. Default: "aic".

Returns

int, the return value represents the error type. 0 indicates normal. 1 indicates failure to start some or all test cases. 2 indicates a hardware failure, and it is recommended to replace the device.

Supported Platforms:

Ascend

Examples

>>> from mindspore.utils import stress_detect
>>> ret = stress_detect()
>>> print(ret)
0
mindspore.utils.dlpack.from_dlpack(dlpack)[source]

Converts a DLPack object to a MindSpore Tensor.

This function allows for the sharing of tensor data from other deep learning frameworks that support DLPack. The data is not copied and the returned MindSpore Tensor shares the memory with the source tensor.

Warning

This is an experimental API that is subject to change or deletion.

Parameters

dlpack (PyCapsule) – The DLPack object to be converted, which is a capsule containing a pointer to a DLManagedTensor.

Returns

Tensor, the MindSpore Tensor that shares memory with the DLPack object.

Supported Platforms:

Ascend

Examples

>>> import mindspore as ms
>>> from mindspore.utils.dlpack import to_dlpack, from_dlpack
>>> import numpy as np
>>> # Create a MindSpore Tensor and convert it to DLPack
>>> x = ms.Tensor(np.random.rand(2, 3), ms.float32)
>>> dlpack_obj = to_dlpack(x)
>>>
>>> # Convert the DLPack object back to a MindSpore Tensor
>>> y = from_dlpack(dlpack_obj)
>>> print(x.shape == y.shape)
True
mindspore.utils.dlpack.to_dlpack(tensor)[source]

Converts a MindSpore Tensor to a DLPack object.

The DLPack format is a standard for sharing tensor data between different deep learning frameworks. The returned DLPack object is a Python capsule that can be consumed by other libraries that support DLPack. The capsule contains a pointer to a DLManagedTensor structure. The consumer of the DLPack object is responsible for releasing the memory.

Warning

This is an experimental API that is subject to change or deletion.

Parameters

tensor (Tensor) – The MindSpore Tensor to be converted.

Returns

PyCapsule, a DLPack object that can be consumed by other libraries.

Supported Platforms:

Ascend

Examples

>>> import mindspore as ms
>>> from mindspore.utils.dlpack import to_dlpack, from_dlpack
>>> import numpy as np
>>> # Convert a MindSpore Tensor to DLPack
>>> x = ms.Tensor(np.random.rand(2, 3), ms.float32)
>>> dlpack_obj = to_dlpack(x)
>>>
>>> # At this point, dlpack_obj can be used by other frameworks that support DLPack.
>>> # For demonstration, we convert it back to a MindSpore Tensor.
>>> y = from_dlpack(dlpack_obj)
>>> print(x.shape == y.shape)
True
mindspore.utils.dryrun.set_simulation()[source]

This interface is used to enable the dryrun function. The dryrun function is mainly used to simulate the actual operation of the large model. After it is enabled, the memory usage, compilation information, etc. can be simulated without occupying device card. In the PyNative mode, once it is enabled, if values are fetched from the device to the host, the Python call stack log will be printed to inform users that these values are inaccurate.

Supported Platforms:

Ascend

Examples

>>> import mindspore as ms
>>> from mindspore.utils import dryrun
>>> import numpy as np
>>> dryrun.set_simulation()
>>> print(os.environ.get('MS_SIMULATION_LEVEL'))
1
mindspore.utils.dryrun.mock(mock_val, *args)[source]

In the network, if some if branch need to use the actual execution values and the virtual execution cannot obtain them, this interface can be used to return simulated values. During actual execution, the correct results can be obtained and the execution values can be returned.

Parameters
  • mock_val (Union[Value, Tensor]) – The value you want to return.

  • args (Union[Value, function]) – The content you want to mock, it can be values, function and so on.

Returns

If dryrun is enabled, mock_val will be returned; otherwise, the actual execution value of args will be returned.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore as ms
>>> from mindspore.utils import dryrun
>>> import numpy as np
>>> dryrun.set_simulation()
>>> a = ms.Tensor(np.random.rand(3, 3).astype(np.float32))
>>> if dryrun.mock(True, a[0, 0] > 0.5):
...     print("return mock_val: True.")
return mock_val: True
>>>
>>> import mindspore as ms
>>> from mindspore.utils import dryrun
>>> import numpy as np
>>> a = ms.Tensor(np.ones((3, 3)).astype(np.float32))
>>> if dryrun.mock(False, a[0, 0] > 0.5):
...     print("return real execution: True.")
return real execution: True.
>>>
>>> import mindspore as ms
>>> from mindspore.utils import dryrun
>>> import numpy as np
>>> a = ms.Tensor(np.ones((3, 3)).astype(np.float32))
>>> if dryrun.mock(False, (a > 0.5).any):
...     print("return real execution: True.")
return real execution: True.
mindspore.utils.sdc_detect_start()[source]

This api will be deprecated and removed in future versions, please use the api mindspore.tools.sdc_detect_start() instead.

Supported Platforms:

Ascend

Examples

>>> from mindspore.utils import sdc_detect_start
>>> sdc_detect_start()
mindspore.utils.sdc_detect_stop()[source]

This api will be deprecated and removed in future versions, please use the api mindspore.tools.sdc_detect_stop() instead.

Supported Platforms:

Ascend

Examples

>>> from mindspore.utils import sdc_detect_stop
>>> sdc_detect_stop()
mindspore.utils.get_sdc_detect_result()[source]

This api will be deprecated and removed in future versions, please use the api mindspore.tools.get_sdc_detect_result() instead.

Returns

bool, indicating whether silent data corruption has occurred after detection start.

Supported Platforms:

Ascend

Examples

>>> from mindspore.utils import get_sdc_detect_result
>>> result = get_sdc_detect_result()
>>> print(result)
False