# Nested checkpoint directories

**URL:** <https://discuss.ray.io/t/nested-checkpoint-directories/11021>\
**Category:** Uncategorized\
**Created:** [June 13, 2023, 5:30pm UTC](https://discuss.ray.io/t/nested-checkpoint-directories/11021 "2023-06-13T17:30:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![0piero](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/0piero/32/3109_2.png) [@0piero](https://discuss.ray.io/u/0piero)\
**Post date:** [June 13, 2023, 5:30pm UTC](https://discuss.ray.io/t/nested-checkpoint-directories/11021/1 "2023-06-13T17:30:44Z")

</div>

The problem I have is that when generating session reports and saving checkpoints, the directories are being nested. For example: exp/checkpoint\_01/checkpoint\_02/… This nested structure becomes problematic as the number of saves increases, resulting in excessively long directory names. What am I doing wrong?

code for saving:

```auto
dir = session.get_trial_dir()
checkpoint = Checkpoint(local_path=dir)
session.report(metrics, checkpoint=checkpoint)

```

---

<div class="post-metadata">

**Author:** ![justinvyu](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/justinvyu/32/4309_2.png) [@justinvyu](https://discuss.ray.io/u/justinvyu)\
**Post date:** [June 13, 2023, 6:01pm UTC](https://discuss.ray.io/t/nested-checkpoint-directories/11021/2 "2023-06-13T18:01:43Z")

</div>

Hi @0piero,

A few things:

1. You should not checkpoint the trial directory. The trial directory is already available to you – so you should only checkpoint stuff training-related state that you want to output from within your training loop. Checkpoints are generated within the trial directory, which is what’s causing this nesting problem.

2. You should use one of the official constructors of `Checkpoint`. Directly creating a checkpoint is a private API – you should instead use `Checkpoint.from_directory` in this case. See the docs below:

[https://docs.ray.io/en/latest/ray-air/api/doc/ray.air.checkpoint.Checkpoint.from\_directory.html](https://docs.ray.io/en/latest/ray-air/api/doc/ray.air.checkpoint.Checkpoint.from_directory.html)

Here’s an example:

```python
import os
import tempfile

from ray import tune

def train_fn(config):
    # ...
    tmpdir = tempfile.mkdtemp()
    with open(os.path.join(tmpdir, "model.pt"), "w") as f:
        torch.save(..., f) # Save some file to this directory
    checkpoint = Checkpoint.from_directory(tmpdir)
    session.report(..., checkpoint=checkpoint)

tuner = tune.Tuner(train_fn)
tuner.fit()

```
