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

Function pop

python/paddle/base/layers/math_op_patch.py:435–489  ·  view source on GitHub ↗

The type variable must be LoD Tensor Array. When self is DenseTensorArray, calling pop is similar to Python's pop on list. This interface is used to simplify dygraph to static graph operations. Args: self(Variable): The source variable, which must be DEN

(self, *args)

Source from the content-addressed store, hash-verified

433
434 @static_only
435 def pop(self, *args):
436 """
437 The type variable must be LoD Tensor Array.
438 When self is DenseTensorArray, calling pop is similar to Python's pop on list.
439 This interface is used to simplify dygraph to static graph operations.
440
441 Args:
442 self(Variable): The source variable, which must be DENSE_TENSOR_ARRAY
443 *args: optional, a int means index.
444 Returns:
445 Variable: self[index]
446 """
447 import paddle
448 from paddle.static.nn import while_loop
449 from paddle.tensor import fill_constant
450
451 if self.type != core.VarDesc.VarType.DENSE_TENSOR_ARRAY:
452 raise TypeError(
453 f"Only Variable with VarType.DENSE_TENSOR_ARRAY support `pop` method, but received type: {self.type}"
454 )
455 if len(args) == 0:
456 idx = -1
457 else:
458 idx = args[0]
459
460 assert isinstance(idx, int)
461
462 def cond(i, new_array):
463 return paddle.less_than(i, arr_len)
464
465 def body(i, new_array):
466 item = paddle.tensor.array_read(array=self, i=i)
467 paddle.tensor.array_write(
468 item, paddle.tensor.array_length(new_array), new_array
469 )
470
471 i = paddle.increment(i)
472 return i, new_array
473
474 arr_len = paddle.tensor.array_length(self)
475 if idx < 0:
476 idx = idx + arr_len
477 else:
478 idx = fill_constant(shape=[1], dtype="int64", value=idx)
479
480 pop_item = paddle.tensor.array_read(self, idx)
481
482 tmp = paddle.assign(self)
483 new_array = _slice_tensor_array(tmp, 0, idx)
484 i = idx + 1
485
486 _, new_array = while_loop(cond, body, [i, new_array])
487 paddle.assign(new_array, output=self)
488
489 return pop_item
490
491 def _scalar_op_(var, scale, bias):
492 block = current_block(var)

Callers

nothing calls this directly

Calls 5

fill_constantFunction · 0.90
while_loopFunction · 0.90
TypeErrorClass · 0.85
_slice_tensor_arrayFunction · 0.85
assignMethod · 0.45

Tested by

no test coverage detected