# Sharded checkpoints

**URL:** <https://discuss.huggingface.co/t/sharded-checkpoints/22321>\
**Category:** 🤗Accelerate\
**Created:** [August 30, 2022, 10:07pm UTC](https://discuss.huggingface.co/t/sharded-checkpoints/22321 "2022-08-30T22:07:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Samuel-Fipps](https://avatars.discourse-cdn.com/v4/letter/s/e56c9b/32.png) [@Samuel-Fipps](https://discuss.huggingface.co/u/Samuel-Fipps)\
**Post date:** [August 30, 2022, 10:07pm UTC](https://discuss.huggingface.co/t/sharded-checkpoints/22321/1 "2022-08-30T22:07:27Z")

</div>

> **[Handling big models](https://huggingface.co/docs/accelerate/usage_guides/big_modeling)**
>
> We’re on a journey to advance and democratize artificial intelligence through open source and open science.

This link show how to can set memory limits using device\_map.  
But before you can do that you need a sharded checkpoint already for the below function. How do you get sharded checkpoints if the model can’t fit on your gpu’s to start off with?

The whole reason i’m doing this is because when i use the shard option i get cuda out of memory errors.

```auto
  model = load_checkpoint_and_dispatch(
      model, "sharded-gpt-j-6B", device_map="auto", no_split_module_classes=["GPTJBlock"]
  )

```

---

<div class="post-metadata">

**Author:** ![Samuel-Fipps](https://avatars.discourse-cdn.com/v4/letter/s/e56c9b/32.png) [@Samuel-Fipps](https://discuss.huggingface.co/u/Samuel-Fipps)\
**Post date:** [August 30, 2022, 11:29pm UTC](https://discuss.huggingface.co/t/sharded-checkpoints/22321/2 "2022-08-30T23:29:48Z")

</div>

I figured it out, but the max memory mapping didn’t work anyway

I just edited my config for a t5 and loaded it up and saved it. This gave the sharded checkpoints.

```auto
  my_config = T5Config.from_pretrained("/media/New Volume/models/need to make/xl-nl32/")
  model = T5Model(my_config)
  model.save_pretrained("/media/New Volume/models/need to make/xl-nl32made", from_pt=True)

```

Then with accelerate

```auto
checkpoint = "/media/New Volume/models/need to make/xl-nl32made"
config = AutoConfig.from_pretrained(checkpoint)
with init_empty_weights():
    model = T5Model(config)

my_device_map = infer_auto_device_map(model, max_memory={0: "10GiB", 1: "10GiB", "cpu": "120GiB"})
model = load_checkpoint_and_dispatch(
    model, "/media/New Volume/models/need to make/xl-nl32made", device_map=my_device_map)

accelerator = Accelerator()
 
model.train()
inputs = torch.load('cnn_dailymail_article_train_20k.pt')
label = torch.load('cnn_dailymail_highlights_train_20k.pt')
count = 0
steps = 10000
printSteps = 50
totalLoss = 0

device = accelerator.device

inputs, label, model = accelerator.prepare(inputs, label, model)
  
optimizer = Adafactor(model.parameters(), scale_parameter=False, relative_step=False, warmup_init=False, lr=0.00001)
optimizer = accelerator.prepare(optimizer)

with accelerator.accumulate(model):
    for ihjfdls in inputs:
        start_time = time.time()

        i = inputs[count]
        j = label[count]

        i = torch.from_numpy(i)
        j = torch.from_numpy(j)
        i = i.type(torch.long)
        j = j.type(torch.long)
        i = i.reshape( 1, -1)
        j = j.reshape( 1, -1)
        
        loss = model( input_ids=i, labels=j, use_cache=False).loss
        count += 1
        
        optimizer.zero_grad()
        accelerator.backward(loss)
        optimizer.step()

        totalLoss = totalLoss + loss.item()

        if count % printSteps == 0: 
            print("epoch:", count, "loss:", totalLoss/printSteps)
            if (totalLoss/printSteps) < 1.0: break
            totalLoss = 0

        if count == steps: break

        trainttime = time.time()

```

But this memory mapping didn’t work, I tried it by launching Accelerator with different configs and just by running the file alone.

---

<div class="post-metadata">

**Author:** ![muellerzr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/muellerzr/32/21583_2.png) [@muellerzr](https://discuss.huggingface.co/u/muellerzr)\
**Post date:** [August 30, 2022, 11:45pm UTC](https://discuss.huggingface.co/t/sharded-checkpoints/22321/3 "2022-08-30T23:45:40Z")

</div>

> [@Samuel-Fipps](#):
>
> but the max memory mapping didn’t work anyway

Can you provide more towards what you did and how you tried setting your max memory mapping?

---

<div class="post-metadata">

**Author:** ![Samuel-Fipps](https://avatars.discourse-cdn.com/v4/letter/s/e56c9b/32.png) [@Samuel-Fipps](https://discuss.huggingface.co/u/Samuel-Fipps)\
**Post date:** [August 31, 2022, 1:33am UTC](https://discuss.huggingface.co/t/sharded-checkpoints/22321/4 "2022-08-31T01:33:06Z")

</div>

I updated my response
