Add an operation to calculate inclusive cumulative sum of elements of a tensor in a given dimension. Given an input tensor, that function creates an operation that calculates inclusive cumulative sum of elements in the dimension 'dim' to create a new tensor. The output tensor h
(input: Tensor, dim: int, prefer_plugin: bool = True)
| 2409 | |
| 2410 | |
| 2411 | def cumsum(input: Tensor, dim: int, prefer_plugin: bool = True) -> Tensor: |
| 2412 | ''' |
| 2413 | Add an operation to calculate inclusive cumulative sum of elements of |
| 2414 | a tensor in a given dimension. |
| 2415 | |
| 2416 | Given an input tensor, that function creates an operation that calculates |
| 2417 | inclusive cumulative sum of elements in the dimension 'dim' to create |
| 2418 | a new tensor. The output tensor has the same shape as the input tensor. |
| 2419 | |
| 2420 | The input tensor must have rank >= 1. The 'dim' must be valid, and negative |
| 2421 | value is supported. |
| 2422 | |
| 2423 | For example, on input=[[4, 2, 5], [2, 1, 2], [4, 7, 1]], which has a shape |
| 2424 | [3, 3], |
| 2425 | |
| 2426 | cumsum(input, 0) |
| 2427 | |
| 2428 | will produce [[4, 2, 5], [6, 3, 7], [10, 10, 8]]. |
| 2429 | |
| 2430 | cumsum(input, 1) |
| 2431 | |
| 2432 | will produce [[4, 6, 11], [2, 3, 5], [4, 11, 12]]. |
| 2433 | |
| 2434 | That operation is implemented by TensorRT ILoopLayer. |
| 2435 | |
| 2436 | Parameters: |
| 2437 | input : Tensor |
| 2438 | The input tensor to calculate the inclusive cumulative sum. |
| 2439 | |
| 2440 | dim : int |
| 2441 | The dimension to calculate the inclusive cumulative sum. Negative |
| 2442 | value is supported. |
| 2443 | |
| 2444 | prefer_plugin : bool |
| 2445 | Whether to use the cumsumLastDim plugin if dim is last dim. |
| 2446 | |
| 2447 | Returns: |
| 2448 | The tensor containing the inclusive cumulative sum of input. |
| 2449 | ''' |
| 2450 | assert input.rank() >= 1, "input should have rank >= 1" |
| 2451 | assert dim < input.rank() and dim >= -input.rank( |
| 2452 | ), f"dim should be in [{-input.rank()}, {input.rank()}) when input have rank {input.rank()}" |
| 2453 | |
| 2454 | dim = dim_resolve_negative(dim, input.ndim())[0] |
| 2455 | |
| 2456 | if dim == input.ndim() - 1: |
| 2457 | if prefer_plugin: |
| 2458 | last_dim = input.size(-1) |
| 2459 | if last_dim == -1: # dynamic? |
| 2460 | last_dim = shape(input, -1) |
| 2461 | old_shape = shape(input) |
| 2462 | if input.ndim() == 1: |
| 2463 | input_2d = unsqueeze( |
| 2464 | input, 0) # special handling of rank-1 dynamic tensor |
| 2465 | elif input.ndim() != 2: |
| 2466 | input_2d = input.view(concat([-1, last_dim]), |
| 2467 | zero_is_placeholder=False) |
| 2468 | else: |
no test coverage detected