llvm / llvm/torch-mlir

AtenStackOp does not see tensors appended after list construction

Open
#3,044 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
1.9k
Forks
736
Avg merge
5d 22h
Merged PRs (30d)
15

Description

Found this issue while working on #2969

Using AtenStackOp on list constructed using ListConstruct with a ValueRange does NOT fail:

```mlir
module {
func.func @test_stack() -> !torch.vtensor<[3,2,3],f32> {
%int0 = torch.constant.int 0
%int2 = torch.constant.int 2
%int3 = torch.constant.int 3
%int6 = torch.constant.int 6
%float0 = torch.constant.float 0.0
%none = torch.constant.none
%shape = torch.prim.ListConstruct %int2, %int3 : (!torch.int, !torch.int) -> !torch.list

%1 = torch.aten.full %shape, %float0, %int6, %none, %none, %none : !torch.list, !torch.float, !torch.int, !torch.none, !torch.none, !torch.none -> !torch.vtensor<[2,3],f32>
%2 = torch.aten.full %shape, %float0, %int6, %none, %none, %none : !torch.list, !torch.float, !torch.int, !torch.none, !torch.none, !torch.none -> !torch.vtensor<[2,3],f32>
%3 = torch.aten.full %shape, %float0, %int6, %none, %none, %none : !torch.list, !torch.float, !torch.int, !torch.none, !torch.none, !torch.none -> !torch.vtensor<[2,3],f32>
%4 = torch.prim.ListConstruct %1, %2, %3 : (!torch.vtensor<[2,3],f32>, !torch.vtensor<[2,3],f32>, !torch.vtensor<[2,3],f32>) -> !torch.list>
%5 = torch.aten.stack %4, %int0 : !torch.list>, !torch.int -> !torch.vtensor<[3,2,3],f32>
return %5 : !torch.vtensor<[3,2,3],f32>
}
}

```

Minimal replicating example:
```mlir
module {
func.func @test_stack() -> !torch.vtensor<[3,2,3],f32> {
%int0 = torch.constant.int 0
%int2 = torch.constant.int 2
%int3 = torch.constant.int 3
%int6 = torch.constant.int 6
%float0 = torch.constant.float 0.0
%none = torch.constant.none
%shape = torch.prim.ListConstruct %int2, %int3 : (!torch.int, !torch.int) -> !torch.list

%1 = torch.aten.full %shape, %float0, %int6, %none, %none, %none : !torch.list, !torch.float, !torch.int, !torch.none, !torch.none, !torch.none -> !torch.vtensor<[2,3],f32>
%2 = torch.aten.full %shape, %float0, %int6, %none, %none, %none : !torch.list, !torch.float, !torch.int, !torch.none, !torch.none, !torch.none -> !torch.vtensor<[2,3],f32>
%3 = torch.aten.full %shape, %float0, %int6, %none, %none, %none : !torch.list, !torch.float, !torch.int, !torch.none, !torch.none, !torch.none -> !torch.vtensor<[2,3],f32>

%list_of_tensors = torch.prim.ListConstruct : () -> !torch.list>

torch.aten.append.t %list_of_tensors, %1 : !torch.list>, !torch.vtensor<[2,3],f32> -> !torch.list>
torch.aten.append.t %list_of_tensors, %2 : !torch.list>, !torch.vtensor<[2,3],f32> -> !torch.list>
torch.aten.append.t %list_of_tensors, %3 : !torch.list>, !torch.vtensor<[2,3],f32> -> !torch.list>

%5 = torch.aten.stack %list_of_tensors, %int0 : !torch.list>, !torch.int -> !torch.vtensor<[3,2,3],f32>
return %5 : !torch.vtensor<[3,2,3],f32>
}
}
```

Replicating example with loop:
```mlir
module {
func.func @test_stack() -> !torch.vtensor<[3,2,3],f32> {
%int0 = torch.constant.int 0
%int2 = torch.constant.int 2
%int3 = torch.constant.int 3
%int6 = torch.constant.int 6
%float0 = torch.constant.float 0.0
%none = torch.constant.none
%shape = torch.prim.ListConstruct %int2, %int3 : (!torch.int, !torch.int) -> !torch.list
%true = torch.constant.bool true

%list_of_tensors = torch.prim.ListConstruct : () -> !torch.list>

%loop_iters_max = torch.constant.int 3

torch.prim.Loop %loop_iters_max, %true, init() {
^bb0(%iter_index: !torch.int):
%tensor = torch.aten.full %shape, %float0, %int6, %none, %none, %none : !torch.list, !torch.float, !torch.int, !torch.none, !torch.none, !torch.none -> !torch.vtensor<[2,3],f32>
%discard_append_result = torch.aten.append.t %list_of_tensors, %tensor : !torch.list>, !torch.vtensor<[2,3],f32> -> !torch.list>
%continue = torch.constant.bool true
torch.prim.Loop.condition %true, iter()
} : (!torch.int, !torch.bool) -> ()

%5 = torch.aten.stack %list_of_tensors, %int0 : !torch.list>, !torch.int -> !torch.vtensor<[3,2,3],f32>
return %5 : !torch.vtensor<[3,2,3],f32>
}
}

```

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the minimal MLIR reproducer and the loop variant to confirm how AtenStackOp handles tensors appended after list construction. Trace the AtenStackOp implementation and its list-handling logic; the work is done when both examples correctly account for the appended tensors and produce the declared stacked result.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.