# What's the difference between \`next\_variable()\` and \`reconstruct()\` in \`IteratorVariable\`

**URL:** <https://dev-discuss.pytorch.org/t/whats-the-difference-between-next-variable-and-reconstruct-in-iteratorvariable/2548>\
**Category:** compiler\
**Created:** [October 24, 2024, 2:55am UTC](https://dev-discuss.pytorch.org/t/whats-the-difference-between-next-variable-and-reconstruct-in-iteratorvariable/2548 "2024-10-24T02:55:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shink](https://yyz2.discourse-cdn.com/flex036/user_avatar/dev-discuss.pytorch.org/shink/32/1970_2.png) [@shink](https://dev-discuss.pytorch.org/u/shink)\
**Post date:** [October 24, 2024, 2:55am UTC](https://dev-discuss.pytorch.org/t/whats-the-difference-between-next-variable-and-reconstruct-in-iteratorvariable/2548/1 "2024-10-24T02:55:18Z")

</div>

Hi team! I’m new to Dynamo and have a question.

I noticed that `next_variable()` and `reconstruct()` are not called in all cases,  
so what’s the difference between them and when are they called?

Case 1 will call `reconstruct()`:

```python
def fn1():
    @torch.compile(backend="eager", fullgraph=True)
    def fn(x):
        x = x + 1
        return zip(range(3), [0, 1, 2])

    inputs = torch.ones(1)
    res = fn(inputs)
    print(list(res)) # [(0, 0), (1, 1), (2, 2)]

```

Case 2 will call `next_variable()`:

```python
def g(x):
    return x.requires_grad

@torch.compile(backend="eager", fullgraph=True)
def fn(inputs):
    out = inputs[0]
    for inp in filter(g, inputs):
        out = out * inp
    return out

input1 = torch.arange(2, dtype=torch.bfloat16)
input2 = torch.arange(2, dtype=torch.bfloat16).requires_grad_(True)
inputs = [input1, input2]

res = fn(inputs)
print(res) # tensor([0., 1.], dtype=torch.bfloat16, grad_fn=<MulBackward0>)

```

---

<div class="post-metadata">

**Author:** ![jansel](https://yyz2.discourse-cdn.com/flex036/user_avatar/dev-discuss.pytorch.org/jansel/32/74_2.png) [@jansel](https://dev-discuss.pytorch.org/u/jansel)\
**Post date:** [October 25, 2024, 11:19pm UTC](https://dev-discuss.pytorch.org/t/whats-the-difference-between-next-variable-and-reconstruct-in-iteratorvariable/2548/2 "2024-10-25T23:19:16Z")

</div>

`next_variable` symbolically evaluates a call to `next()` on an iterator

`reconstruct` generates output bytecode to push a copy of the variable (in this case an iterator) onto the stack

The first one calls `reconstruct` because you are returning the iterator, so dynamo is forced to create an iterator at runtime. The second one can statically remove the iterator from your program since it is not visible outside the function.

---

<div class="post-metadata">

**Author:** ![shink](https://yyz2.discourse-cdn.com/flex036/user_avatar/dev-discuss.pytorch.org/shink/32/1970_2.png) [@shink](https://dev-discuss.pytorch.org/u/shink)\
**Post date:** [October 26, 2024, 1:46am UTC](https://dev-discuss.pytorch.org/t/whats-the-difference-between-next-variable-and-reconstruct-in-iteratorvariable/2548/3 "2024-10-26T01:46:47Z")

</div>

Wow thanks so much! Very helpful info!
