(
shape: ShapeLike,
dtype: DTypeLike,
value: bool | float | paddle.Tensor,
force_cpu: bool = False,
out: paddle.Tensor | None = None,
place: PlaceLike | None = None,
name: str | None = None,
)
| 1497 | |
| 1498 | |
| 1499 | def fill_constant( |
| 1500 | shape: ShapeLike, |
| 1501 | dtype: DTypeLike, |
| 1502 | value: bool | float | paddle.Tensor, |
| 1503 | force_cpu: bool = False, |
| 1504 | out: paddle.Tensor | None = None, |
| 1505 | place: PlaceLike | None = None, |
| 1506 | name: str | None = None, |
| 1507 | ) -> paddle.Tensor: |
| 1508 | shape = [shape] if isinstance(shape, int) else shape |
| 1509 | if in_dynamic_or_pir_mode(): |
| 1510 | if place is None: |
| 1511 | place = _current_expected_place() |
| 1512 | else: |
| 1513 | place = _get_paddle_place(place) |
| 1514 | |
| 1515 | if force_cpu: |
| 1516 | place = core.CPUPlace() |
| 1517 | |
| 1518 | if not isinstance(dtype, (core.VarDesc.VarType, core.DataType)): |
| 1519 | dtype = convert_np_dtype_to_dtype_(dtype) |
| 1520 | |
| 1521 | if in_pir_mode() and isinstance(dtype, core.VarDesc.VarType): |
| 1522 | dtype = paddle.pir.core.vartype_to_datatype[dtype] |
| 1523 | |
| 1524 | if in_dynamic_mode(): |
| 1525 | if isinstance(shape, (list, tuple)): |
| 1526 | shape = paddle.utils.convert_shape_to_list(shape) |
| 1527 | else: |
| 1528 | paddle.utils.check_shape(shape) |
| 1529 | if isinstance(shape, (list, tuple)): |
| 1530 | if paddle.utils._contain_var(shape): |
| 1531 | shape = paddle.utils.get_int_tensor_list(shape) |
| 1532 | elif isinstance(shape, paddle.pir.Value): |
| 1533 | pass |
| 1534 | else: |
| 1535 | raise TypeError("Shape only supports Value, or list, or tuple.") |
| 1536 | |
| 1537 | if out is None: |
| 1538 | out = _C_ops.full(shape, value, dtype, place) |
| 1539 | out.stop_gradient = True |
| 1540 | return out |
| 1541 | |
| 1542 | if out.dtype != dtype: |
| 1543 | raise TypeError( |
| 1544 | "Required out.dtype == dtype if specifying out, but received f{out.dtype} != f{dtype}" |
| 1545 | ) |
| 1546 | out = _C_ops.full_(out, shape, value, dtype, place) |
| 1547 | out.stop_gradient = True |
| 1548 | return out |
| 1549 | |
| 1550 | else: |
| 1551 | attrs = {'force_cpu': force_cpu} |
| 1552 | dtype = convert_dtype(dtype) |
| 1553 | if not isinstance(value, Variable): |
| 1554 | if dtype in ['int8', 'uint8', 'int16', 'int32', 'int64']: |
| 1555 | attrs['str_value'] = str(int(value)) |
| 1556 | attrs['value'] = int(value) |
no test coverage detected