GitHub issue #5822: Audio Dataset with_format torch problem
State: closed | Created: 2023-05-04T20:07:51Z

### Describe the bug

Common Voice v10 Delta (German) Dataset from here https://commonvoice.mozilla.org/de/datasets

```
audio_dataset = \
   (Dataset
    .from_dict({"audio": ('/tmp/cv-corpus-10.0-delta-2022-07-04/de/clips/' + df.path).to_list()})
    .cast_column("audio", Audio(sampling_rate=16_000))
    .with_format('numpy'))
audio_dataset[0]["audio"]
```

works, but

```
audio_dataset = \
   (Dataset
    .from_dict({"audio": ('/tmp/cv-corpus-10.0-delta-2022-07-04/de/clips/' + df.path).to_list()})
    .cast_column("audio", Audio(sampling_rate=16_000))
    .with_format('torch'))
audio_dataset[0]["audio"]
```

does not instead I get

```
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[54], line 1
----> 1 audio_dataset[0]["audio"]

File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/datasets/arrow_dataset.py:2154, in Dataset.__getitem__(self, key)
   2152 def __getitem__(self, key):  # noqa: F811
   2153     """Can be used to index columns (by string names) or rows (by integer index or iterable of indices or bools)."""
-> 2154     return self._getitem(
   2155         key,
   2156     )

File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/datasets/arrow_dataset.py:2139, in Dataset._getitem(self, key, decoded, **kwargs)
   2137 formatter = get_formatter(format_type, features=self.features, decoded=decoded, **format_kwargs)
   2138 pa_subtable = query_table(self._data, key, indices=self._indices if self._indices is not None else None)
-> 2139 formatted_output = format_table(
   2140     pa_subtable, key, formatter=formatter, format_columns=format_columns, output_all_columns=output_all_columns
   2141 )
   2142 return formatted_output

File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/datasets/formatting/formatting.py:532, in format_table(table, key, formatter, format_columns, output_all_columns)
    530 python_formatter = PythonFormatter(features=None)
    531 if format_columns is None:
--> 532     return formatter(pa_table, query_type=query_type)
    533 elif query_type == "column":
    534     if key in format_columns:

File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/datasets/formatting/formatting.py:281, in Formatter.__call__(self, pa_table, query_type)
    279 def __call__(self, pa_table: pa.Table, query_type: str) -> Union[RowFormat, ColumnFormat, BatchFormat]:
    280     if query_type == "row":
--> 281         return self.format_row(pa_table)
    282     elif query_type == "column":
    283         return self.format_column(pa_table)

File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/datasets/formatting/torch_formatter.py:58, in TorchFormatter.format_row(self, pa_table)
     56 def format_row(self, pa_table: pa.Table) -> dict:
     57     row = self.numpy_arrow_extractor().extract_row(pa_table)
---> 58     return self.recursive_tensorize(row)

File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/datasets/formatting/torch_formatter.py:54, in TorchFormatter.recursive_tensorize(self, data_struct)
     53 def recursive_tensorize(self, data_struct: dict):
---> 54     return map_nested(self._recursive_tensorize, data_struct, map_list=False)

File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/datasets/utils/py_utils.py:356, in map_nested(function, data_struct, dict_only, map_list, map_tuple, map_numpy, num_proc, types, disable_tqdm, desc)
    354     num_proc = 1
    355 if num_proc <= 1 or len(iterable) <= num_proc:
--> 356     mapped = [
    357         _single_map_nested((function, obj, types, None, True, None))
    358         for obj in logging.tqdm(iterable, disable=disable_tqdm, desc=desc)
    359     ]
    360 else:
    361     split_kwds = []  # We organize the splits ourselve (contiguous splits)

File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/datasets/utils/py_utils.py:357, in <listcomp>(.0)
    354     num_proc = 1
    355 if num_proc <= 1 or len(iterable) <= num_proc:
    356     mapped = [
--> 357         _single_map_nested((function, obj, types, None, True, None))
    358         for obj in logging.tqdm(iterable, disable=disable_tqdm, desc=desc)
    359     ]
    360 else:
    361     split_kwds = []  # We organize the splits ourselve (contiguous splits)

File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/datasets/utils/py_utils.py:309, in _single_map_nested(args)
    306 pbar = logging.tqdm(pbar_iterable, disable=disable_tqdm, position=rank, unit="obj", desc=pbar_desc)
    308 if isinstance(data_struct, dict):
--> 309     return {k: _single_map_nested((function, v, types, None, True, None)) for k, v in pbar}
    310 else:
    311     mapped = [_single_map_nested((function, v, types, None, True, None)) for v in pbar]

File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/datasets/utils/py_utils.py:309, in <dictcomp>(.0)
    306 pbar = logging.tqdm(pbar_iterable, disable=disable_tqdm, position=rank, unit="obj", desc=pbar_desc)
    308 if isinstance(data_struct, dict):
--> 309     return {k: _single_map_nested((function, v, types, None, True, None)) for k, v in pbar}
    310 else:
    311     mapped = [_single_map_nested((function, v, types, None, True, None)) for v in pbar]

File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/datasets/utils/py_utils.py:293, in _single_map_nested(args)
    291 # Singleton first to spare some computation
    292 if not isinstance(data_struct, dict) and not isinstance(data_struct, types):
--> 293     return function(data_struct)
    295 # Reduce logging to keep things readable in multiprocessing with tqdm
    296 if rank is not None and logging.get_verbosity() < logging.WARNING:

File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/datasets/formatting/torch_formatter.py:51, in TorchFormatter._recursive_tensorize(self, data_struct)
     49     if data_struct.dtype == np.object:  # pytorch tensors cannot be instantied from an array of objects
     50         return [self.recursive_tensorize(substruct) for substruct in data_struct]
---> 51 return self._tensorize(data_struct)

File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/datasets/formatting/torch_formatter.py:38, in TorchFormatter._tensorize(self, value)
     35 import torch
     37 default_dtype = {}
---> 38 if np.issubdtype(value.dtype, np.integer):
     39     default_dtype = {"dtype": torch.int64}
     40 elif np.issubdtype(value.dtype, np.floating):

AttributeError: 'NoneType' object has no attribute 'dtype'
```

### Steps to reproduce the bug

1. Download some audio dataset in this case I used Common Voice v10 Delta (German) Dataset from here https://commonvoice.mozilla.org/de/datasets
2. Try the Code from above

### Expected behavior

It should work for torch

### Environment info

pytorch: 2.0.0
datasets: 2.3.2
numpy: 1.21.6

Python: 3.8
Linux