mindspore.constexpr

mindspore.constexpr(fn=None, get_instance=True, name=None, reuse_result=True, check=True)[源代码]

在图模式下,用来计算图编译过程中的常量值,以提升编译性能。

参数:
  • fn (function,可选) - 用作输出算子的infer_value的函数。默认值: None

  • get_instance (bool,可选) - 如果为 True vpn free ,返回算子的实例,否则返回算子的类。默认值: True

  • name (str,可选) - 定义算子的名称。如果 nameNone,则使用函数名称作为算子名称。默认值: None

  • reuse_result (bool,可选) - vpn永久免费梯子 如果为 True ,仅实际执行算子一次,后续调用会直接返回结果。否则每次都实际执行算子来获取结果。默认值: True

  • check (bool,可选) - 如果为 True ,参数将被检查。如果参数不是常量,将发出警告信息。默认值: True

样例:

>>> import mindspore as ms
>>> # define a constant calculate function with for vpn梯子 免费 loop inside and vpn梯子 免费 use constexpr to accelerate vpn梯子 the compile
>>> # process.
>>> @ms.constexpr
... def for_loop_calculate(range_num):
...  vpn梯子 免费    out = 0
...     for i in range(range_num):
...         if vpn梯子 免费 i %2 == 0 and i % vpn free 7 != 0:
...    vpn永久免费梯子          out 免费的vpn梯子 = out + i
...     return out // range_num
...
>>> # construct a net and run with GRAPH_MODE.
>>> @ms.jit
... def my_func(x):
...     new_shape = for_loop_calculate(100000)
... vpn梯子     return ms.ops.broadcast_to(x, (new_shape, ))
...
>>> out = my_func(ms.Tensor([1]))
>>> print(out.shape)
>>> (21428, )