constant.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from paddle import _C_ops
  15. from ...base import core, framework
  16. from ...base.framework import (
  17. _current_expected_place,
  18. in_dygraph_mode,
  19. in_dynamic_or_pir_mode,
  20. )
  21. # TODO: define the initializers of Constant in neural network
  22. from .initializer import Initializer
  23. __all__ = []
  24. class ConstantInitializer(Initializer):
  25. """Implements the constant initializer
  26. Args:
  27. value (float32, optional): constant value to initialize the variable. Default: 0.0.
  28. """
  29. def __init__(self, value=0.0, force_cpu=False):
  30. assert value is not None
  31. super().__init__()
  32. self._value = value
  33. self._force_cpu = force_cpu
  34. def forward(self, var, block=None):
  35. """Initialize the input tensor with constant.
  36. Args:
  37. var(Tensor): Tensor that needs to be initialized.
  38. block(Block, optional): The block in which initialization ops
  39. should be added. Used in static graph only, default None.
  40. Returns:
  41. The initialization op
  42. """
  43. import paddle
  44. block = self._check_block(block)
  45. assert isinstance(
  46. var,
  47. (
  48. framework.Variable,
  49. framework.EagerParamBase,
  50. paddle.pir.Value,
  51. paddle.pir.core.ParameterMeta,
  52. ),
  53. )
  54. assert isinstance(block, (framework.Block, paddle.pir.Block))
  55. if in_dynamic_or_pir_mode():
  56. place = _current_expected_place()
  57. if self._force_cpu:
  58. place = core.CPUPlace()
  59. if in_dygraph_mode():
  60. if isinstance(var, framework.EagerParamBase) and var.is_dist():
  61. out_var = _C_ops.full(
  62. var._local_shape, float(self._value), var.dtype, place
  63. )
  64. out_var = (
  65. paddle.distributed.auto_parallel.api.dtensor_from_local(
  66. out_var, var.process_mesh, var.placements
  67. )
  68. )
  69. out_var._share_underline_tensor_to(var)
  70. else:
  71. _C_ops.full_(
  72. var, var.shape, float(self._value), var.dtype, place
  73. )
  74. return None
  75. else:
  76. return _C_ops.full(
  77. var.shape, float(self._value), var.dtype, place
  78. )
  79. else:
  80. op = block.append_op(
  81. type="fill_constant",
  82. outputs={"Out": var},
  83. attrs={
  84. "shape": var.shape,
  85. "dtype": int(var.dtype),
  86. "value": float(self._value),
  87. 'str_value': str(float(self._value)),
  88. 'force_cpu': self._force_cpu,
  89. },
  90. stop_gradient=True,
  91. )
  92. var.op = op
  93. return op
  94. class Constant(ConstantInitializer):
  95. """Implement the constant initializer.
  96. Args:
  97. value (float32|float64, optional): constant value to initialize the parameter. Default: 0.0.
  98. Examples:
  99. .. code-block:: python
  100. >>> import paddle
  101. >>> import paddle.nn as nn
  102. >>> paddle.seed(2023)
  103. >>> data = paddle.rand([30, 10, 2], dtype='float32')
  104. >>> linear = nn.Linear(2,
  105. ... 4,
  106. ... weight_attr=nn.initializer.Constant(value=2.0))
  107. >>> res = linear(data)
  108. >>> print(linear.weight)
  109. Parameter containing:
  110. Tensor(shape=[2, 4], dtype=float32, place=Place(cpu), stop_gradient=False,
  111. [[2., 2., 2., 2.],
  112. [2., 2., 2., 2.]])
  113. """
  114. def __init__(self, value=0.0):
  115. if value is None:
  116. raise ValueError("value must not be none.")
  117. super().__init__(value=value, force_cpu=False)