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

Function create_array

python/paddle/tensor/array.py:309–379  ·  view source on GitHub ↗

This OP creates an array. It is used as the input of :ref:`api_paddle_tensor_array_array_read` and :ref:`api_paddle_tensor_array_array_write`. Args: dtype (str): The data type of the elements in the array. Support data type: float32, float64, int32, int64 and bool. init

(
    dtype: _typing.DTypeLike,
    initialized_list: Sequence[paddle.Tensor] | None = None,
)

Source from the content-addressed store, hash-verified

307
308
309def create_array(
310 dtype: _typing.DTypeLike,
311 initialized_list: Sequence[paddle.Tensor] | None = None,
312) -> paddle.Tensor | list[paddle.Tensor]:
313 """
314 This OP creates an array. It is used as the input of :ref:`api_paddle_tensor_array_array_read` and
315 :ref:`api_paddle_tensor_array_array_write`.
316
317 Args:
318 dtype (str): The data type of the elements in the array. Support data type: float32, float64, int32, int64 and bool.
319 initialized_list(list): Used to initialize as default value for created array.
320 All values in initialized list should be a Tensor.
321
322 Returns:
323 list|Tensor, An empty array. In dynamic mode, ``array`` is a Python list. But in static graph mode, array is a Tensor
324 whose ``VarType`` is ``DENSE_TENSOR_ARRAY``.
325
326 Examples:
327 .. code-block:: pycon
328
329 >>> import paddle
330
331 >>> arr = paddle.tensor.create_array(dtype="float32")
332 >>> x = paddle.full(shape=[1, 3], fill_value=5, dtype="float32")
333 >>> i = paddle.zeros(shape=[1], dtype="int32")
334
335 >>> arr = paddle.tensor.array_write(x, i, array=arr)
336
337 >>> item = paddle.tensor.array_read(arr, i)
338 >>> print(item.numpy())
339 [[5. 5. 5.]]
340
341 """
342 array = []
343 if initialized_list is not None:
344 if not isinstance(initialized_list, (list, tuple)):
345 raise TypeError(
346 f"Require type(initialized_list) should be list/tuple, but received {type(initialized_list)}"
347 )
348 array = list(initialized_list)
349
350 # NOTE: Only support plain list like [x, y,...], not support nested list in static graph mode.
351 for val in array:
352 if not isinstance(val, (Variable, paddle.pir.Value)):
353 raise TypeError(
354 f"All values in `initialized_list` should be Variable or pir.Value, but received {type(val)}."
355 )
356
357 if in_dynamic_mode():
358 return array
359 elif in_pir_mode():
360 if not isinstance(dtype, (core.VarDesc.VarType, core.DataType)):
361 dtype = paddle.base.framework.convert_np_dtype_to_dtype_(dtype)
362 out = paddle._pir_ops.create_array(dtype)
363 for val in array:
364 if dtype != paddle.base.libpaddle.DataType.UNDEFINED:
365 val = paddle.cast(val, dtype)
366 paddle._pir_ops.array_write_(out, val, array_length(out))

Callers 2

true_fnFunction · 0.90
array_writeFunction · 0.70

Calls 9

TypeErrorClass · 0.85
listFunction · 0.85
in_pir_modeFunction · 0.85
LayerHelperClass · 0.85
array_writeFunction · 0.85
array_lengthFunction · 0.70
typeFunction · 0.50
castMethod · 0.45
create_variableMethod · 0.45

Tested by

no test coverage detected