dataset-distillation icon indicating copy to clipboard operation
dataset-distillation copied to clipboard

Code needs to be updated to make sure it runs without errors

Open KoustubhPhalak opened this issue 11 months ago • 7 comments

The function accumulate_grad() inside Trainer() class in train_distilled_image.py needs to be updated as follows:

def accumulate_grad(self, grad_infos):
        bwd_out = []
        bwd_grad = []
        for datas, gdatas, lrs, glrs in grad_infos:
            bwd_out.extend(lrs)
            bwd_grad.extend(glrs)
            for d, g in zip(datas, gdatas):
                if d.grad is not None:
                    d.grad.add_(g)
                else:
                    d.grad = g
        if bwd_out:
            torch.autograd.backward(bwd_out, grad_tensors=bwd_grad)

With the current code, I get the following error:

Traceback (most recent call last):
  File "./dataset_distillation/dataset-distillation/main.py", line 402, in <module>
    main(options.get_state())
  File "./dataset_distillation/dataset-distillation/main.py", line 131, in main
    steps = train_distilled_image.distill(state, state.models)
  File "./dataset_distillation/dataset-distillation/train_distilled_image.py", line 290, in distill
    return Trainer(state, models).train()
  File "./dataset_distillation/dataset-distillation/train_distilled_image.py", line 248, in train
    self.accumulate_grad(grad_infos)
  File "./dataset_distillation/dataset-distillation/train_distilled_image.py", line 176, in accumulate_grad
    d.grad.add_(g)
AttributeError: 'NoneType' object has no attribute 'add_'

I request the code authors to kindly have a look at this once and rectify it as per my suggestion if they think this modification looks OK. Thanks.

PS: I would like to mention @jundeli who helped me in figuring out the issue. Thanks to him for that!

KoustubhPhalak avatar Jan 25 '25 22:01 KoustubhPhalak

@KoustubhPhalak Have you figured out the way to rectify it?

karan-kumawat17 avatar Mar 20 '25 14:03 karan-kumawat17

@KoustubhPhalak Have you figured out the way to rectify it?

Hi @karan-kumawat17 , if you replace the code for accumulate_grad() function as I have shown above, it should work. For reference I will attach it here again:

def accumulate_grad(self, grad_infos):
        bwd_out = []
        bwd_grad = []
        for datas, gdatas, lrs, glrs in grad_infos:
            bwd_out.extend(lrs)
            bwd_grad.extend(glrs)
            for d, g in zip(datas, gdatas):
                if d.grad is not None:
                    d.grad.add_(g)
                else:
                    d.grad = g
        if bwd_out:
            torch.autograd.backward(bwd_out, grad_tensors=bwd_grad)

KoustubhPhalak avatar Mar 20 '25 14:03 KoustubhPhalak

FYI, I did not update the code, instead, I installed an older version of dependencies, you can check my fork here: https://github.com/kowyo/dataset-distillation/tree/fixed-dep

kowyo avatar Mar 29 '25 14:03 kowyo

FYI, I did not update the code, instead, I installed an older version of dependencies, you can check my fork here: https://github.com/kowyo/dataset-distillation/tree/fixed-dep

That makes sense. I would imagine that the code was made on older version of dependencies, and something might have been updated in the dependencies after which the code started throwing error. Shifting back to older versions of these dependencies should also do the trick, rather than modifying code like I have done.

Thanks for sharing @kowyo !

KoustubhPhalak avatar Mar 29 '25 15:03 KoustubhPhalak

FYI, I did not update the code, instead, I installed an older version of dependencies, you can check my fork here: https://github.com/kowyo/dataset-distillation/tree/fixed-dep

this link doesn't work

yanyycc avatar Apr 18 '25 03:04 yanyycc

I opened a PR https://github.com/ssnl/dataset-distillation/pull/64 and this has been merged into main branch

kowyo avatar Apr 18 '25 06:04 kowyo

I opened a PR #64 and this has been merged into main branch

Thanks for this!

KoustubhPhalak avatar Jun 02 '25 15:06 KoustubhPhalak