Skip to content

Commit

Permalink
don't pickle table index (#2264)
Browse files Browse the repository at this point in the history
  • Loading branch information
lhoestq authored Apr 26, 2021
1 parent 3db67f5 commit 22c5928
Showing 1 changed file with 18 additions and 23 deletions.
41 changes: 18 additions & 23 deletions src/datasets/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,31 +145,30 @@ def __deepcopy__(self, memo: dict):
return _deepcopy(self, memo)

def __getstate__(self):
state = self.__dict__.copy()
# We can't pickle objects that are bigger than 4GiB, or it causes OverflowError
# So we write the table on disk instead
if self.table.nbytes >= config.MAX_TABLE_NBYTES_FOR_PICKLING:
table = state.pop("table")
table = self.table
with tempfile.NamedTemporaryFile("wb", delete=False, suffix=".arrow") as tmp_file:
filename = tmp_file.name
logger.debug(
f"Attempting to pickle a table bigger than 4GiB. Writing it on the disk instead at {filename}"
)
_write_table_to_file(table=table, filename=filename)
state["path"] = filename
return state
return {"path": filename}
else:
return state
return {"table": self.table}

def __setstate__(self, state):
state = state.copy()
if "path" in state:
filename = state.pop("path")
filename = state["path"]
logger.debug(f"Unpickling a big table from the disk at {filename}")
state["table"] = _in_memory_arrow_table_from_file(filename)
table = _in_memory_arrow_table_from_file(filename)
logger.debug(f"Removing temporary table file at {filename}")
os.remove(filename)
self.__dict__ = state
else:
table = state["table"]
Table.__init__(self, table)

@inject_arrow_table_documentation(pa.Table.validate)
def validate(self, *args, **kwargs):
Expand Down Expand Up @@ -434,16 +433,14 @@ def from_file(cls, filename: str, replays=None):
return cls(table, filename, replays)

def __getstate__(self):
state = self.__dict__.copy()
state.pop("table")
return state
return {"path": self.path, "replays": self.replays}

def __setstate__(self, state):
state = state.copy()
table = _memory_mapped_arrow_table_from_file(state["path"])
table = self._apply_replays(table, state["replays"])
state["table"] = table
self.__dict__ = state
path = state["path"]
replays = state["replays"]
table = _memory_mapped_arrow_table_from_file(path)
table = self._apply_replays(table, replays)
MemoryMappedTable.__init__(self, table, path=path, replays=replays)

@staticmethod
def _apply_replays(table: pa.Table, replays: Optional[List[Replay]] = None) -> pa.Table:
Expand Down Expand Up @@ -569,14 +566,12 @@ def __init__(self, table: pa.Table, blocks: List[List[TableBlock]]):
)

def __getstate__(self):
state = self.__dict__.copy()
state.pop("table")
return state
return {"blocks": self.blocks}

def __setstate__(self, state):
state = state.copy()
state["table"] = self._concat_blocks_horizontally_and_vertically(state["blocks"])
self.__dict__ = state
blocks = state["blocks"]
table = self._concat_blocks_horizontally_and_vertically(blocks)
ConcatenationTable.__init__(self, table, blocks=blocks)

@staticmethod
def _concat_blocks(blocks: List[Union[TableBlock, pa.Table]], axis: int = 0) -> pa.Table:
Expand Down

1 comment on commit 22c5928

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Show benchmarks

PyArrow==1.0.0

Show updated benchmarks!

Benchmark: benchmark_array_xd.json

metric read_batch_formatted_as_numpy after write_array2d read_batch_formatted_as_numpy after write_flattened_sequence read_batch_formatted_as_numpy after write_nested_sequence read_batch_unformated after write_array2d read_batch_unformated after write_flattened_sequence read_batch_unformated after write_nested_sequence read_col_formatted_as_numpy after write_array2d read_col_formatted_as_numpy after write_flattened_sequence read_col_formatted_as_numpy after write_nested_sequence read_col_unformated after write_array2d read_col_unformated after write_flattened_sequence read_col_unformated after write_nested_sequence read_formatted_as_numpy after write_array2d read_formatted_as_numpy after write_flattened_sequence read_formatted_as_numpy after write_nested_sequence read_unformated after write_array2d read_unformated after write_flattened_sequence read_unformated after write_nested_sequence write_array2d write_flattened_sequence write_nested_sequence
new / old (diff) 0.021976 / 0.011353 (0.010623) 0.015245 / 0.011008 (0.004237) 0.051859 / 0.038508 (0.013350) 0.042318 / 0.023109 (0.019208) 0.363137 / 0.275898 (0.087239) 0.387825 / 0.323480 (0.064345) 0.012179 / 0.007986 (0.004193) 0.004848 / 0.004328 (0.000519) 0.012831 / 0.004250 (0.008580) 0.055233 / 0.037052 (0.018181) 0.360258 / 0.258489 (0.101769) 0.415825 / 0.293841 (0.121984) 0.174870 / 0.128546 (0.046323) 0.122913 / 0.075646 (0.047267) 0.480362 / 0.419271 (0.061091) 0.473649 / 0.043533 (0.430116) 0.358654 / 0.255139 (0.103515) 0.405395 / 0.283200 (0.122196) 1.718122 / 0.141683 (1.576439) 1.965494 / 1.452155 (0.513339) 1.950600 / 1.492716 (0.457883)

Benchmark: benchmark_getitem_100B.json

metric get_batch_of_1024_random_rows get_batch_of_1024_rows get_first_row get_last_row
new / old (diff) 0.017505 / 0.018006 (-0.000502) 0.000476 / 0.000490 (-0.000014) 0.000193 / 0.000200 (-0.000007) 0.000051 / 0.000054 (-0.000004)

Benchmark: benchmark_indices_mapping.json

metric select shard shuffle sort train_test_split
new / old (diff) 0.047070 / 0.037411 (0.009659) 0.022661 / 0.014526 (0.008135) 0.030387 / 0.176557 (-0.146169) 0.064870 / 0.737135 (-0.672266) 0.030007 / 0.296338 (-0.266331)

Benchmark: benchmark_iterating.json

metric read 5000 read 50000 read_batch 50000 10 read_batch 50000 100 read_batch 50000 1000 read_formatted numpy 5000 read_formatted pandas 5000 read_formatted tensorflow 5000 read_formatted torch 5000 read_formatted_batch numpy 5000 10 read_formatted_batch numpy 5000 1000 shuffled read 5000 shuffled read 50000 shuffled read_batch 50000 10 shuffled read_batch 50000 100 shuffled read_batch 50000 1000 shuffled read_formatted numpy 5000 shuffled read_formatted_batch numpy 5000 10 shuffled read_formatted_batch numpy 5000 1000
new / old (diff) 0.408569 / 0.215209 (0.193360) 4.165705 / 2.077655 (2.088050) 2.149507 / 1.504120 (0.645387) 1.929638 / 1.541195 (0.388443) 1.969953 / 1.468490 (0.501462) 6.698927 / 4.584777 (2.114150) 6.057253 / 3.745712 (2.311541) 8.572314 / 5.269862 (3.302452) 7.402587 / 4.565676 (2.836910) 0.650394 / 0.424275 (0.226118) 0.010379 / 0.007607 (0.002772) 0.526479 / 0.226044 (0.300435) 5.210233 / 2.268929 (2.941305) 3.087974 / 55.444624 (-52.356651) 2.771327 / 6.876477 (-4.105150) 2.796243 / 2.142072 (0.654171) 7.011894 / 4.805227 (2.206667) 4.968260 / 6.500664 (-1.532404) 6.783388 / 0.075469 (6.707919)

Benchmark: benchmark_map_filter.json

metric filter map fast-tokenizer batched map identity map identity batched map no-op batched map no-op batched numpy map no-op batched pandas map no-op batched pytorch map no-op batched tensorflow
new / old (diff) 10.448530 / 1.841788 (8.606742) 14.193357 / 8.074308 (6.119049) 30.723333 / 10.191392 (20.531941) 0.834136 / 0.680424 (0.153712) 0.658410 / 0.534201 (0.124209) 0.837323 / 0.579283 (0.258040) 0.635172 / 0.434364 (0.200808) 0.690120 / 0.540337 (0.149783) 1.509284 / 1.386936 (0.122348)
PyArrow==latest
Show updated benchmarks!

Benchmark: benchmark_array_xd.json

metric read_batch_formatted_as_numpy after write_array2d read_batch_formatted_as_numpy after write_flattened_sequence read_batch_formatted_as_numpy after write_nested_sequence read_batch_unformated after write_array2d read_batch_unformated after write_flattened_sequence read_batch_unformated after write_nested_sequence read_col_formatted_as_numpy after write_array2d read_col_formatted_as_numpy after write_flattened_sequence read_col_formatted_as_numpy after write_nested_sequence read_col_unformated after write_array2d read_col_unformated after write_flattened_sequence read_col_unformated after write_nested_sequence read_formatted_as_numpy after write_array2d read_formatted_as_numpy after write_flattened_sequence read_formatted_as_numpy after write_nested_sequence read_unformated after write_array2d read_unformated after write_flattened_sequence read_unformated after write_nested_sequence write_array2d write_flattened_sequence write_nested_sequence
new / old (diff) 0.023170 / 0.011353 (0.011817) 0.015545 / 0.011008 (0.004537) 0.054314 / 0.038508 (0.015806) 0.045311 / 0.023109 (0.022202) 0.356304 / 0.275898 (0.080406) 0.388218 / 0.323480 (0.064738) 0.011522 / 0.007986 (0.003537) 0.004583 / 0.004328 (0.000255) 0.011341 / 0.004250 (0.007090) 0.064321 / 0.037052 (0.027269) 0.369290 / 0.258489 (0.110801) 0.443918 / 0.293841 (0.150077) 0.155951 / 0.128546 (0.027404) 0.126657 / 0.075646 (0.051011) 0.492374 / 0.419271 (0.073103) 0.453804 / 0.043533 (0.410271) 0.354268 / 0.255139 (0.099129) 0.469179 / 0.283200 (0.185980) 2.051445 / 0.141683 (1.909762) 1.869129 / 1.452155 (0.416975) 2.302526 / 1.492716 (0.809810)

Benchmark: benchmark_getitem_100B.json

metric get_batch_of_1024_random_rows get_batch_of_1024_rows get_first_row get_last_row
new / old (diff) 0.017363 / 0.018006 (-0.000643) 0.000460 / 0.000490 (-0.000030) 0.000179 / 0.000200 (-0.000021) 0.000044 / 0.000054 (-0.000010)

Benchmark: benchmark_indices_mapping.json

metric select shard shuffle sort train_test_split
new / old (diff) 0.043950 / 0.037411 (0.006538) 0.028870 / 0.014526 (0.014344) 0.030215 / 0.176557 (-0.146342) 0.049977 / 0.737135 (-0.687158) 0.031497 / 0.296338 (-0.264841)

Benchmark: benchmark_iterating.json

metric read 5000 read 50000 read_batch 50000 10 read_batch 50000 100 read_batch 50000 1000 read_formatted numpy 5000 read_formatted pandas 5000 read_formatted tensorflow 5000 read_formatted torch 5000 read_formatted_batch numpy 5000 10 read_formatted_batch numpy 5000 1000 shuffled read 5000 shuffled read 50000 shuffled read_batch 50000 10 shuffled read_batch 50000 100 shuffled read_batch 50000 1000 shuffled read_formatted numpy 5000 shuffled read_formatted_batch numpy 5000 10 shuffled read_formatted_batch numpy 5000 1000
new / old (diff) 0.405726 / 0.215209 (0.190517) 4.034754 / 2.077655 (1.957099) 2.049010 / 1.504120 (0.544890) 1.807890 / 1.541195 (0.266695) 1.824433 / 1.468490 (0.355942) 6.610548 / 4.584777 (2.025771) 5.886448 / 3.745712 (2.140736) 8.350467 / 5.269862 (3.080605) 7.035727 / 4.565676 (2.470050) 0.701563 / 0.424275 (0.277287) 0.012444 / 0.007607 (0.004837) 0.641957 / 0.226044 (0.415913) 5.443047 / 2.268929 (3.174118) 3.020290 / 55.444624 (-52.424334) 2.754576 / 6.876477 (-4.121901) 2.794981 / 2.142072 (0.652909) 7.405368 / 4.805227 (2.600141) 4.576250 / 6.500664 (-1.924414) 8.263618 / 0.075469 (8.188149)

Benchmark: benchmark_map_filter.json

metric filter map fast-tokenizer batched map identity map identity batched map no-op batched map no-op batched numpy map no-op batched pandas map no-op batched pytorch map no-op batched tensorflow
new / old (diff) 11.085600 / 1.841788 (9.243813) 13.943158 / 8.074308 (5.868850) 29.800468 / 10.191392 (19.609076) 0.954594 / 0.680424 (0.274171) 0.645966 / 0.534201 (0.111766) 0.772599 / 0.579283 (0.193316) 0.583637 / 0.434364 (0.149273) 0.711807 / 0.540337 (0.171469) 1.553971 / 1.386936 (0.167035)

CML watermark

Please sign in to comment.