TypeError: Can't apply _send_to_device on object of type <class 'str'>, only of nested list/tuple/dicts of objects that satisfy _has_to_method

I am trying to do multi-task learning with transformers with my own custom training loop
and accelerate.

I have prepared my model,data and optimizer like this, but when I try to loop my batch I get this issue, also I have checked that batch’s type is still dict.

Is there any way to solve this?


accelerator = Accelerator()

device = accelerator.device

model_1,model_2, optimizer_1, optimizer_2, train_dl, dev_dl = accelerator.prepare(model_1,model_2, optimizer_1, optimizer_2, train_dl, dev_dl)


--> 1 for batch in train_dl:
      2   print(batch)

7 frames
/usr/local/lib/python3.7/dist-packages/accelerate/data_loader.py in __iter__(self)
    304             if state.distributed_type == DistributedType.TPU:
    305                 xm.mark_step()
--> 306             yield batch if self.device is None else send_to_device(batch, self.device)
    307 
    308 

/usr/local/lib/python3.7/dist-packages/accelerate/utils.py in send_to_device(tensor, device)
    202         return hasattr(t, "to")
    203 
--> 204     return recursively_apply(_send_to_device, tensor, device, test_type=_has_to_method, error_on_other_type=True)
    205 
    206 

/usr/local/lib/python3.7/dist-packages/accelerate/utils.py in recursively_apply(func, data, test_type, error_on_other_type, *args, **kwargs)
    169                     func, v, *args, test_type=test_type, error_on_other_type=error_on_other_type, **kwargs
    170                 )
--> 171                 for k, v in data.items()
    172             }
    173         )

/usr/local/lib/python3.7/dist-packages/accelerate/utils.py in <dictcomp>(.0)
    169                     func, v, *args, test_type=test_type, error_on_other_type=error_on_other_type, **kwargs
    170                 )
--> 171                 for k, v in data.items()
    172             }
    173         )

/usr/local/lib/python3.7/dist-packages/accelerate/utils.py in recursively_apply(func, data, test_type, error_on_other_type, *args, **kwargs)
    160                     func, o, *args, test_type=test_type, error_on_other_type=error_on_other_type, **kwargs
    161                 )
--> 162                 for o in data
    163             ),
    164         )

/usr/local/lib/python3.7/dist-packages/accelerate/utils.py in honor_type(obj, generator)
    119         # Can instantiate a namedtuple from a generator directly, contrary to a tuple/list.
    120         return type(obj)(*list(generator))
--> 121     return type(obj)(generator)
    122 
    123 

/usr/local/lib/python3.7/dist-packages/accelerate/utils.py in <genexpr>(.0)
    160                     func, o, *args, test_type=test_type, error_on_other_type=error_on_other_type, **kwargs
    161                 )
--> 162                 for o in data
    163             ),
    164         )

/usr/local/lib/python3.7/dist-packages/accelerate/utils.py in recursively_apply(func, data, test_type, error_on_other_type, *args, **kwargs)
    176     elif error_on_other_type:
    177         raise TypeError(
--> 178             f"Can't apply {func.__name__} on object of type {type(data)}, only of nested list/tuple/dicts of objects "
    179             f"that satisfy {test_type.__name__}."
    180         )

TypeError: Can't apply _send_to_device on object of type <class 'str'>, only of nested list/tuple/dicts of objects that satisfy _has_to_method.

This has been fixed on master, so you should just do a source install :slight_smile: