赞
踩
https://gitee.com/mindspore/docs/blob/master/tutorials/source_zh_cn/beginner/accelerate_with_static_graph.ipynb
基本介绍 || 快速入门 || 张量 Tensor || 数据集 Dataset || 数据变换 Transforms || 网络构建 || 函数式自动微分 || 模型训练 || 保存与加载 || 使用静态图加速
AI编译框架分为两种运行模式,分别是动态图模式以及静态图模式。MindSpore默认情况下是以动态图模式运行,但也支持手工切换为静态图模式。两种运行模式的详细介绍如下:
动态图的特点是计算图的构建和计算同时发生(Define by run),其符合Python的解释执行方式,在计算图中定义一个Tensor时,其值就已经被计算且确定,因此在调试模型时较为方便,能够实时得到中间结果的值,但由于所有节点都需要被保存,导致难以对整个计算图进行优化。
在MindSpore中,动态图模式又被称为PyNative模式。由于动态图的解释执行特性,在脚本开发和网络流程调试过程中,推荐使用动态图模式进行调试。
如需要手动控制框架采用PyNative模式,可以通过以下代码进行网络构建:
import numpy as np import mindspore as ms from mindspore import nn, Tensor ms.set_context(mode=ms.PYNATIVE_MODE) # 使用set_context进行动态图模式的配置 class Network(nn.Cell): def __init__(self): super().__init__() self.flatten = nn.Flatten() self.dense_relu_sequential = nn.SequentialCell( nn.Dense(28*28, 512), nn.ReLU(), nn.Dense(512, 512), nn.ReLU(), nn.Dense(512, 10) ) def construct(self, x): x = self.flatten(x) logits = self.dense_relu_sequential(x) return logits model = Network() input = Tensor(np.ones([64, 1, 28, 28]).astype(np.float32)) output = model(input) print(output)
[WARNING] ME(4706:281473506197520,MainProcess):2024-08-06-03:55:44.901.390 [mindspore/hal/device.py:155] Backend Ascend is not created yet. [WARNING] ME(4706,ffffa859c010,python):2024-08-06-03:55:44.901.152 [mindspore/ccsrc/runtime/hardware/device_context_manager.cc:466] GetDeviceContext] Device context of device Ascend is not created yet. [[-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037] [-0.02150986 0.14259806 -0.09672696 0.06892041 -0.07039133 0.05796466 0.08224779 -0.0833988 -0.09188233 -0.03166037]]
相较于动态图而言,静态图的特点是将计算图的构建和实际计算分开(Define and run)。有关静态图模式的运行原理,可以参考静态图语法支持。
在MindSpore中,静态图模式又被称为Graph模式,在Graph模式下,基于图优化、计算图整图下沉等技术,编译器可以针对图进行全局的优化,获得较好的性能,因此比较适合网络固定且需要高性能的场景。
如需要手动控制框架采用静态图模式,可以通过以下代码进行网络构建:
import numpy as np import mindspore as ms from mindspore import nn, Tensor ms.set_context(mode=ms.GRAPH_MODE) # 使用set_context进行运行静态图模式的配置 class Network(nn.Cell): def __init__(self): super().__init__() self.flatten = nn.Flatten() self.dense_relu_sequential = nn.SequentialCell( nn.Dense(28*28, 512), nn.ReLU(), nn.Dense(512, 512), nn.ReLU(), nn.Dense(512, 10) ) def construct(self, x): x = self.flatten(x) logits = self.dense_relu_sequential(x) return logits model = Network() input = Tensor(np.ones([64, 1, 28, 28]).astype(np.float32)) output = model(input) print(output)
[WARNING] ME(4706:281473506197520,MainProcess):2024-08-06-03:55:54.732.739 [mindspore/context.py:1104] For 'context.set_context' in Ascend backend, the backend is already initialized, please set it before the definition of any Tensor and Parameter, and the instantiation and execution of any operation and net, otherwise the settings may not take effect. [ERROR] CORE(4706,ffffa859c010,python):2024-08-06-03:55:55.090.857 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_4706/4016835682.py] [[-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938] [-0.04193115 -0.09265137 -0.12658691 -0.0970459 -0.12939453 0.21777344 0.12585449 -0.03903198 0.06396484 0.03710938]]
MindSpore编译器重点面向Tensor数据的计算以及其微分处理。因此使用MindSpore API以及基于Tensor对象的操作更适合使用静态图编译优化。其他操作虽然可以部分入图编译,但实际优化作用有限。另外,静态图模式先编译后执行的模式导致其存在编译耗时。因此,如果函数无需反复执行,那么使用静态图加速也可能没有价值。
有关使用静态图来进行网络编译的示例,请参考网络构建。
通常情况下,由于动态图的灵活性,我们会选择使用PyNative模式来进行自由的神经网络构建,以实现模型的创新和优化。但是当需要进行性能加速时,我们需要对神经网络部分或整体进行加速。MindSpore提供了两种切换为图模式的方式,分别是基于装饰器的开启方式以及基于全局context的开启方式。
MindSpore提供了jit装饰器,可以通过修饰Python函数或者Python类的成员函数使其被编译成计算图,通过图优化等技术提高运行速度。此时我们可以简单的对想要进行性能优化的模块进行图编译加速,而模型其他部分,仍旧使用解释执行方式,不丢失动态图的灵活性。无论全局context是设置成静态图模式还是动态图模式,被jit修饰的部分始终会以静态图模式进行运行。
在需要对Tensor的某些运算进行编译加速时,可以在其定义的函数上使用jit修饰器,在调用该函数时,该模块自动被编译为静态图。需要注意的是,jit装饰器只能用来修饰函数,无法对类进行修饰。jit的使用示例如下:
import numpy as np import mindspore as ms from mindspore import nn, Tensor class Network(nn.Cell): def __init__(self): super().__init__() self.flatten = nn.Flatten() self.dense_relu_sequential = nn.SequentialCell( nn.Dense(28*28, 512), nn.ReLU(), nn.Dense(512, 512), nn.ReLU(), nn.Dense(512, 10) ) def construct(self, x): x = self.flatten(x) logits = self.dense_relu_sequential(x) return logits input = Tensor(np.ones([64, 1, 28, 28]).astype(np.float32)) @ms.jit # 使用ms.jit装饰器,使被装饰的函数以静态图模式运行 def run(x): model = Network() return model(x) output = run(input) print(output)
[ERROR] CORE(4706,ffffa859c010,python):2024-08-06-03:55:59.637.602 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_4706/4227391393.py] [[ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828] [ 0.38671875 0.17675781 -0.1050415 -0.0670166 -0.22253418 0.15893555 -0.11834717 -0.07342529 -0.13439941 0.25048828]]
除使用修饰器外,也可使用函数变换方式调用jit方法,示例如下:
import numpy as np import mindspore as ms from mindspore import nn, Tensor class Network(nn.Cell): def __init__(self): super().__init__() self.flatten = nn.Flatten() self.dense_relu_sequential = nn.SequentialCell( nn.Dense(28*28, 512), nn.ReLU(), nn.Dense(512, 512), nn.ReLU(), nn.Dense(512, 10) ) def construct(self, x): x = self.flatten(x) logits = self.dense_relu_sequential(x) return logits input = Tensor(np.ones([64, 1, 28, 28]).astype(np.float32)) def run(x): model = Network() return model(x) run_with_jit = ms.jit(run) # 通过调用jit将函数转换为以静态图方式执行 output = run_with_jit(input) print(output)
[ERROR] CORE(4706,ffffa859c010,python):2024-08-06-03:56:00.218.220 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_4706/1035776523.py] [[ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432] [ 0.00085497 0.11248779 -0.00818634 -0.20715332 0.05758667 -0.14367676 0.16821289 0.29223633 0.03942871 0.07867432]]
当我们需要对神经网络的某部分进行加速时,可以直接在construct方法上使用jit修饰器,在调用实例化对象时,该模块自动被编译为静态图。示例如下:
import numpy as np import mindspore as ms from mindspore import nn, Tensor class Network(nn.Cell): def __init__(self): super().__init__() self.flatten = nn.Flatten() self.dense_relu_sequential = nn.SequentialCell( nn.Dense(28*28, 512), nn.ReLU(), nn.Dense(512, 512), nn.ReLU(), nn.Dense(512, 10) ) @ms.jit # 使用ms.jit装饰器,使被装饰的函数以静态图模式运行 def construct(self, x): x = self.flatten(x) logits = self.dense_relu_sequential(x) return logits input = Tensor(np.ones([64, 1, 28, 28]).astype(np.float32)) model = Network() output = model(input) print(output)
[ERROR] CORE(4706,ffffa859c010,python):2024-08-06-03:56:01.117.912 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_4706/2029473088.py] [[-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ] [-0.15197754 0.12036133 -0.14147949 -0.08544922 -0.06933594 -0.05297852 -0.0256958 -0.05389404 0.00824738 0.0171051 ]]
context模式是一种全局的设置模式。代码示例如下:
import numpy as np import mindspore as ms from mindspore import nn, Tensor ms.set_context(mode=ms.GRAPH_MODE) # 使用set_context进行运行静态图模式的配置 class Network(nn.Cell): def __init__(self): super().__init__() self.flatten = nn.Flatten() self.dense_relu_sequential = nn.SequentialCell( nn.Dense(28*28, 512), nn.ReLU(), nn.Dense(512, 512), nn.ReLU(), nn.Dense(512, 10) ) def construct(self, x): x = self.flatten(x) logits = self.dense_relu_sequential(x) return logits model = Network() input = Tensor(np.ones([64, 1, 28, 28]).astype(np.float32)) output = model(input) print(output)
[WARNING] ME(4706:281473506197520,MainProcess):2024-08-06-03:56:01.512.676 [mindspore/context.py:1104] For 'context.set_context' in Ascend backend, the backend is already initialized, please set it before the definition of any Tensor and Parameter, and the instantiation and execution of any operation and net, otherwise the settings may not take effect. [ERROR] CORE(4706,ffffa859c010,python):2024-08-06-03:56:01.755.075 [mindspore/core/utils/file_utils.cc:253] GetRealPath] Get realpath failed, path[/tmp/ipykernel_4706/4016835682.py] [[ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ] [ 0.08453369 0.04693604 -0.13366699 0.09106445 0.15905762 -0.04476929 0.13061523 -0.06280518 -0.02482605 0.0479126 ]]
在Graph模式下,Python代码并不是由Python解释器去执行,而是将代码编译成静态计算图,然后执行静态计算图。因此,编译器无法支持全量的Python语法。MindSpore的静态图编译器维护了Python常用语法子集,以支持神经网络的构建及训练。详情可参考静态图语法支持。
使用静态图高级编程技巧可以有效地提高编译效率以及执行效率,并可以使程序运行的更加稳定。详情可参考静态图高级编程技巧。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。