MCPcopy Create free account
hub / github.com/PaddlePaddle/Paddle / fill_constant

Function fill_constant

python/paddle/tensor/creation.py:1499–1613  ·  view source on GitHub ↗
(
    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,
)

Source from the content-addressed store, hash-verified

1497
1498
1499def 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)

Callers 13

_get_shape_tensorFunction · 0.90
_convert_to_tensor_listFunction · 0.90
cropFunction · 0.90
_get_SectionsTensorListFunction · 0.90
get_new_list_tensorFunction · 0.90
popFunction · 0.90
linspaceFunction · 0.70
logspaceFunction · 0.70
fullFunction · 0.70
arangeFunction · 0.70
rangeFunction · 0.70
mean_compositeFunction · 0.50

Calls 15

append_opMethod · 0.95
in_dynamic_or_pir_modeFunction · 0.85
_current_expected_placeFunction · 0.85
_get_paddle_placeFunction · 0.85
in_pir_modeFunction · 0.85
TypeErrorClass · 0.85
strFunction · 0.85
LayerHelperClass · 0.85
check_variable_and_dtypeFunction · 0.85
CPUPlaceMethod · 0.80
check_shapeMethod · 0.80
fullMethod · 0.80

Tested by

no test coverage detected