# Truncating part of a computational graph

**URL:** <https://discuss.pytorch.org/t/truncating-part-of-a-computational-graph/224977>\
**Category:** autograd\
**Created:** [May 19, 2026, 11:12pm UTC](https://discuss.pytorch.org/t/truncating-part-of-a-computational-graph/224977 "2026-05-19T23:12:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pikantrop](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/p/b77776/32.png) [@pikantrop](https://discuss.pytorch.org/u/pikantrop)\
**Post date:** [May 19, 2026, 11:12pm UTC](https://discuss.pytorch.org/t/truncating-part-of-a-computational-graph/224977/1 "2026-05-19T23:12:36Z")

</div>

Hi,

My goal is to keep a computational graph depth over a recursive sequence constant.

Let’s assume I have a training data D\_1, D\_2, … and a model M, then my model behaves the following

M…(M(M(D\_1) || D\_2) || D\_3)…

On the step that trains on D\_3, I would like to have computational graph of M(M(M\_1 || D\_2) || D\_3), where M\_1 is the output of M(D\_1) but with no graph attached to it. My first attempt was (simplified code)

```auto
while True:
    inputs, target = batch
    history, prediction = model(torch.cat((self.saved_history, inputs), dim=0))
    loss = F.mse_loss(prediction, target)

    del self.saved_history
    self.saved_history = history

```

but that keeps full computational history (I guess torch.cat already captured a reference to it, so deleting the saved\_history here is really a no-op). What I really need to say is, cut the computational graph of history tensor where it points to saved\_history tensor.

Detaching saved\_history in model(torch.cat((self.saved\_history.detach(), inputs) also does not achieve my goal, as the graph then only encompasses the very last iteration (not last 2 iterations which his my goal in this sample).

Is there a way to express it in pytorch?

Piotr

---

<div class="post-metadata">

**Author:** ![JuanFMontesinos](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/juanfmontesinos/32/76115_2.png) [@JuanFMontesinos](https://discuss.pytorch.org/u/JuanFMontesinos)\
**Post date:** [June 16, 2026, 1:08am UTC](https://discuss.pytorch.org/t/truncating-part-of-a-computational-graph/224977/2 "2026-06-16T01:08:54Z")

</div>

What do you mean by

> I would like to have computational graph […] but with no graph attached to it
