Download nlu/DeBERTa/training/_utils.py from nvan13/HRA: direct link, hf CLI and curl.
- Browser
- Download file 501 Bytes
-
https://huggingface.co/nvan13/HRA/resolve/main/nlu/DeBERTa/training/_utils.py
- Command line
-
hf download hf://nvan13/HRA/nlu/DeBERTa/training/_utils.py
-
curl -L -o _utils.py https://huggingface.co/nvan13/HRA/resolve/main/nlu/DeBERTa/training/_utils.py
501 Bytes
| import torch | |
| from collections.abc import Sequence, Mapping | |
| def batch_apply(batch, fn): | |
| if isinstance(batch, torch.Tensor): | |
| return fn(batch) | |
| elif isinstance(batch, Sequence): | |
| return [batch_apply(x, fn) for x in batch] | |
| elif isinstance(batch, Mapping): | |
| return {x:batch_apply(batch[x], fn) for x in batch} | |
| else: | |
| raise NotImplementedError(f'Type of {type(batch)} are not supported in batch_apply') | |
| def batch_to(batch, device): | |
| return batch_apply(batch, lambda x: x.to(device)) | |