-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Open
Labels
Description
bug描述 Describe the Bug
paddle.DataParallel调用父类的astype方法时, 其参数层的类型不会改变
import paddle
import paddle.nn as nn
weight_attr = paddle.ParamAttr(name="weight",initializer=paddle.nn.initializer.Constant(value=1.5))
bias_attr = paddle.ParamAttr(name="bias",initializer=paddle.nn.initializer.Constant(value=2.5))
linear = paddle.nn.Linear(2, 2, weight_attr=weight_attr, bias_attr=bias_attr).to(device="cpu",dtype="float32")
linear=linear.astype("int8")
print(linear)
# output:
# Linear(in_features=2, out_features=2, dtype=paddle.int8)
print(linear.parameters())
# excepted output
[Parameter containing:
Tensor(shape=[2, 2], dtype=int8, place=Place(cpu), stop_gradient=False,
[[1, 1],
[1, 1]]), Parameter containing:
Tensor(shape=[2], dtype=int8, place=Place(cpu), stop_gradient=False,
[2, 2])]
# actual output:
[Parameter containing:
Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=False,
[[1.50000000, 1.50000000],
[1.50000000, 1.50000000]]), Parameter containing:
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=False,
[2.50000000, 2.50000000])]其他补充信息 Additional Supplementary Information
查看源码, 应该是因为
# paddle/nn/layer/layers.py row1208
for _, param in self.named_parameters(include_sublayers=True):
param._to(None, dtype)
# paddle\base\dygraph\tensor_patch_methods.py row590
def _to(
self: Tensor,
device: PlaceLike | None = None,
dtype: DTypeLike | None = None,
blocking: bool | None = None,
copy_tensor: bool | None = None,
) -> Tensor:
# 该_to办法只返回一个符合dtype的新的参数层, 但是并没有直接改变param的参数类型