Skip to content

Commit

Permalink
fix patching module that doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
lhoestq committed Jun 15, 2022
1 parent dc41a88 commit b2bf67c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/datasets/utils/patching.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ def __enter__(self):
# in this case we need to patch "os" and "os.path"

for i in range(len(submodules)):
submodule = import_module(".".join(submodules[: i + 1]))
try:
submodule = import_module(".".join(submodules[: i + 1]))
except ModuleNotFoundError:
continue
# We iterate over all the globals in self.obj in case we find "os" or "os.path"
for attr in self.obj.__dir__():
obj_attr = getattr(self.obj, attr)
Expand All @@ -79,7 +82,10 @@ def __enter__(self):
# itself if it was imported as "from os.path import join".

if submodules: # if it's an attribute of a submodule like "os.path.join"
attr_value = getattr(import_module(".".join(submodules)), target_attr)
try:
attr_value = getattr(import_module(".".join(submodules)), target_attr)
except (AttributeError, ModuleNotFoundError):
return
# We iterate over all the globals in self.obj in case we find "os.path.join"
for attr in self.obj.__dir__():
# We don't check for the name of the global, but rather if its value *is* "os.path.join".
Expand Down
8 changes: 8 additions & 0 deletions tests/test_patching.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,11 @@ def test_patch_submodule_successive():
assert _test_patching.os.path.join is original_join
assert _test_patching.os.path.dirname is original_dirname
assert _test_patching.os.rename is original_rename


def test_patch_submodule_doesnt_exist():
mock = "__test_patch_submodule_doesnt_exist_mock__"
with patch_submodule(_test_patching, "__module_that_doesn_exist__.__attribute_that_doesn_exist__", mock):
pass
with patch_submodule(_test_patching, "os.__attribute_that_doesn_exist__", mock):
pass

1 comment on commit b2bf67c

@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==6.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.010666 / 0.011353 (-0.000687) 0.004844 / 0.011008 (-0.006164) 0.037566 / 0.038508 (-0.000942) 0.037855 / 0.023109 (0.014746) 0.368766 / 0.275898 (0.092868) 0.394830 / 0.323480 (0.071351) 0.007393 / 0.007986 (-0.000593) 0.003701 / 0.004328 (-0.000627) 0.008656 / 0.004250 (0.004405) 0.038500 / 0.037052 (0.001448) 0.333969 / 0.258489 (0.075480) 0.407066 / 0.293841 (0.113225) 0.049551 / 0.128546 (-0.078996) 0.014951 / 0.075646 (-0.060695) 0.326472 / 0.419271 (-0.092799) 0.066605 / 0.043533 (0.023073) 0.356390 / 0.255139 (0.101251) 0.388461 / 0.283200 (0.105262) 0.108977 / 0.141683 (-0.032706) 2.192263 / 1.452155 (0.740108) 2.373607 / 1.492716 (0.880891)

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.275838 / 0.018006 (0.257832) 0.528494 / 0.000490 (0.528004) 0.019698 / 0.000200 (0.019498) 0.000407 / 0.000054 (0.000353)

Benchmark: benchmark_indices_mapping.json

metric select shard shuffle sort train_test_split
new / old (diff) 0.030157 / 0.037411 (-0.007255) 0.111947 / 0.014526 (0.097421) 0.122571 / 0.176557 (-0.053986) 0.175511 / 0.737135 (-0.561624) 0.121299 / 0.296338 (-0.175039)

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.601637 / 0.215209 (0.386428) 6.035217 / 2.077655 (3.957562) 2.257730 / 1.504120 (0.753610) 1.938507 / 1.541195 (0.397313) 1.939756 / 1.468490 (0.471265) 0.790545 / 4.584777 (-3.794232) 6.574570 / 3.745712 (2.828857) 3.310455 / 5.269862 (-1.959406) 1.642865 / 4.565676 (-2.922811) 0.090280 / 0.424275 (-0.333995) 0.013739 / 0.007607 (0.006132) 0.810891 / 0.226044 (0.584846) 8.297185 / 2.268929 (6.028257) 3.135003 / 55.444624 (-52.309621) 2.360580 / 6.876477 (-4.515897) 2.413280 / 2.142072 (0.271207) 1.004979 / 4.805227 (-3.800248) 0.192601 / 6.500664 (-6.308063) 0.075911 / 0.075469 (0.000442)

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) 2.063770 / 1.841788 (0.221982) 16.536486 / 8.074308 (8.462178) 43.896271 / 10.191392 (33.704879) 1.032686 / 0.680424 (0.352262) 0.663273 / 0.534201 (0.129072) 0.609106 / 0.579283 (0.029823) 0.719432 / 0.434364 (0.285068) 0.409991 / 0.540337 (-0.130346) 0.425369 / 1.386936 (-0.961567)
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.010255 / 0.011353 (-0.001098) 0.004717 / 0.011008 (-0.006291) 0.038785 / 0.038508 (0.000277) 0.036766 / 0.023109 (0.013657) 0.396418 / 0.275898 (0.120520) 0.421192 / 0.323480 (0.097712) 0.007437 / 0.007986 (-0.000548) 0.004127 / 0.004328 (-0.000201) 0.008096 / 0.004250 (0.003845) 0.038172 / 0.037052 (0.001120) 0.367723 / 0.258489 (0.109234) 0.426822 / 0.293841 (0.132981) 0.048374 / 0.128546 (-0.080173) 0.016474 / 0.075646 (-0.059172) 0.312127 / 0.419271 (-0.107144) 0.064994 / 0.043533 (0.021461) 0.360425 / 0.255139 (0.105286) 0.381416 / 0.283200 (0.098216) 0.097017 / 0.141683 (-0.044666) 2.225304 / 1.452155 (0.773150) 2.283786 / 1.492716 (0.791070)

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.303054 / 0.018006 (0.285048) 0.501990 / 0.000490 (0.501500) 0.022171 / 0.000200 (0.021971) 0.000789 / 0.000054 (0.000734)

Benchmark: benchmark_indices_mapping.json

metric select shard shuffle sort train_test_split
new / old (diff) 0.025896 / 0.037411 (-0.011515) 0.111979 / 0.014526 (0.097453) 0.110825 / 0.176557 (-0.065731) 0.149695 / 0.737135 (-0.587440) 0.112639 / 0.296338 (-0.183699)

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.582771 / 0.215209 (0.367562) 6.051238 / 2.077655 (3.973583) 2.270099 / 1.504120 (0.765979) 1.862478 / 1.541195 (0.321283) 1.894054 / 1.468490 (0.425564) 0.743793 / 4.584777 (-3.840984) 6.489152 / 3.745712 (2.743440) 4.743426 / 5.269862 (-0.526436) 1.524761 / 4.565676 (-3.040916) 0.085090 / 0.424275 (-0.339185) 0.013803 / 0.007607 (0.006196) 0.756116 / 0.226044 (0.530071) 7.852173 / 2.268929 (5.583245) 2.943392 / 55.444624 (-52.501233) 2.195017 / 6.876477 (-4.681460) 2.364445 / 2.142072 (0.222372) 1.023915 / 4.805227 (-3.781312) 0.198658 / 6.500664 (-6.302006) 0.077513 / 0.075469 (0.002044)

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) 1.838649 / 1.841788 (-0.003138) 15.320239 / 8.074308 (7.245931) 42.808891 / 10.191392 (32.617499) 1.021413 / 0.680424 (0.340989) 0.598874 / 0.534201 (0.064673) 0.561495 / 0.579283 (-0.017788) 0.653821 / 0.434364 (0.219457) 0.389795 / 0.540337 (-0.150542) 0.400849 / 1.386936 (-0.986087)

CML watermark

Please sign in to comment.