|
5 | 5 |
|
6 | 6 | import numpy as np
|
7 | 7 | import torch
|
| 8 | +import torch_tensorrt.dynamo.conversion.impl as impl |
8 | 9 | from torch import SymBool, SymFloat, SymInt
|
9 | 10 | from torch.fx.node import Argument, Target
|
10 | 11 | from torch_tensorrt import _enums
|
@@ -530,3 +531,111 @@ def flatten_dims(
|
530 | 531 | new_shape = tuple(shape[:start_dim]) + (num_elements,) + tuple(shape[end_dim + 1 :])
|
531 | 532 |
|
532 | 533 | return new_shape
|
| 534 | + |
| 535 | + |
| 536 | +def append( |
| 537 | + ctx: ConversionContext, |
| 538 | + target: Target, |
| 539 | + source_ir: Optional[SourceIR], |
| 540 | + name: str, |
| 541 | + original_tensor: TRTTensor, |
| 542 | + new_value: Union[TRTTensor, int, float, torch.Tensor, np.ndarray], |
| 543 | + dim: int = 0, |
| 544 | +) -> TRTTensor: |
| 545 | + """ |
| 546 | + Append a new value to the last of the original tensor along the specified dimension (default 0). |
| 547 | + For example, if the original tensor is [1, 2, 3], the new value is 4, and the dim is 0, |
| 548 | + the new tensor will be [1, 2, 3, 4]. |
| 549 | +
|
| 550 | + Args: |
| 551 | + ctx (ConversionContext): A ConversionContext containing the TensorRT network |
| 552 | + target (Target): Target of calling node |
| 553 | + source_ir (Optional[SourceIR]): SourceIR of calling converter |
| 554 | + name (str): Name of the calling layer |
| 555 | + original_tensor (TRTTensor): A TRTTensor to append the new value to |
| 556 | + new_value (Union[TRTTensor, int, float, torch.Tensor, np.ndarray]): A new value to append |
| 557 | + dim (int, optional): Dimention to append the new value. Defaults to 0. |
| 558 | +
|
| 559 | + Returns: |
| 560 | + TRTTensor: A new TRTTensor that is the result of appending the new value to the original tensor |
| 561 | + """ |
| 562 | + if isinstance(new_value, (int, float)): |
| 563 | + new_value = np.array([new_value]) |
| 564 | + new_value = get_trt_tensor(ctx, new_value, name, original_tensor.dtype) |
| 565 | + |
| 566 | + return impl.cat.cat( |
| 567 | + ctx, |
| 568 | + target, |
| 569 | + source_ir, |
| 570 | + f"{name}_concat", |
| 571 | + [original_tensor, new_value], |
| 572 | + get_positive_dim(dim, len(original_tensor.shape)), |
| 573 | + ) |
| 574 | + |
| 575 | + |
| 576 | +def set_item( |
| 577 | + ctx: ConversionContext, |
| 578 | + target: Target, |
| 579 | + source_ir: Optional[SourceIR], |
| 580 | + name: str, |
| 581 | + original_tensor: TRTTensor, |
| 582 | + index: int, |
| 583 | + new_value: Union[TRTTensor, int, float, torch.Tensor, np.ndarray], |
| 584 | +) -> TRTTensor: |
| 585 | + """ |
| 586 | + Set a new value to the original tensor at the specified index. For example, |
| 587 | + if the original tensor is [1, 2, 3], the new value is 4, and the index is 1, |
| 588 | + the new tensor will be [1, 4, 3]. |
| 589 | + If the index is out of bound, the new value will be appended to the end. |
| 590 | +
|
| 591 | + Args: |
| 592 | + ctx (ConversionContext): A ConversionContext containing the TensorRT network |
| 593 | + target (Target): Target of calling node |
| 594 | + source_ir (Optional[SourceIR]): SourceIR of calling converter |
| 595 | + name (str): Name of the calling layer |
| 596 | + original_tensor (TRTTensor): A TRTTensor to set the new value to |
| 597 | + index (int): The index to set the new value |
| 598 | + new_value (Union[TRTTensor, int, float, torch.Tensor, np.ndarray]): A new value to set |
| 599 | +
|
| 600 | + Returns: |
| 601 | + TRTTensor: A new TRTTensor that is the result of setting the new value to the original tensor |
| 602 | + """ |
| 603 | + if isinstance(new_value, (int, float)): |
| 604 | + new_value = np.array([new_value]) |
| 605 | + new_value = get_trt_tensor(ctx, new_value, name, original_tensor.dtype) |
| 606 | + |
| 607 | + len_original_tensor = original_tensor.shape[0] |
| 608 | + index = get_positive_dim(index, len_original_tensor) |
| 609 | + |
| 610 | + front_tensor = impl.slice.slice_op( |
| 611 | + ctx, |
| 612 | + target, |
| 613 | + source_ir, |
| 614 | + f"{name}_slice_front", |
| 615 | + original_tensor, |
| 616 | + dim=0, |
| 617 | + start=0, |
| 618 | + stop=index, |
| 619 | + step=1, |
| 620 | + ) |
| 621 | + rear_tensor = impl.slice.slice_op( |
| 622 | + ctx, |
| 623 | + target, |
| 624 | + source_ir, |
| 625 | + f"{name}_slice_rear", |
| 626 | + original_tensor, |
| 627 | + dim=0, |
| 628 | + start=index + 1, |
| 629 | + stop=len_original_tensor, |
| 630 | + step=1, |
| 631 | + ) |
| 632 | + |
| 633 | + ans = impl.cat.cat( |
| 634 | + ctx, |
| 635 | + target, |
| 636 | + source_ir, |
| 637 | + f"{name}_concat", |
| 638 | + [front_tensor, new_value, rear_tensor], |
| 639 | + 0, |
| 640 | + ) |
| 641 | + return ans |
0 commit comments