Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cache form remapping to avoid per-chunk workload #998

Merged
merged 2 commits into from
Oct 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 58 additions & 31 deletions src/uproot/_dask.py
Original file line number Diff line number Diff line change
Expand Up @@ -885,8 +885,9 @@ def __call__(self, form: Form) -> tuple[Form, TrivialFormMappingInfo]:


class UprootReadMixin:
form_mapping: ImplementsFormMapping
base_form: Form
expected_form: Form
form_mapping_info: ImplementsFormMappingInfo
common_keys: frozenset[str]
interp_options: dict[str, Any]

Expand All @@ -898,24 +899,24 @@ def read_tree(self, tree: HasBranches, start: int, stop: int) -> AwkArray:
awkward = uproot.extras.awkward()
nplike = Numpy.instance()

form, form_info = self.form_mapping(self.base_form)

# The remap implementation should correctly populate the generated
# buffer mapping in __call__, such that the high-level form can be
# used in `from_buffers`
mapping = form_info.load_buffers(
mapping = self.form_mapping_info.load_buffers(
tree, self.common_keys, start, stop, self.interp_options
)

# Populate container with placeholders if keys aren't required
# Otherwise, read from disk
container = {}
for buffer_key, dtype in form.expected_from_buffers(
buffer_key=form_info.buffer_key
for buffer_key, dtype in self.expected_form.expected_from_buffers(
buffer_key=self.form_mapping_info.buffer_key
).items():
# Which key(s) does this buffer require. This code permits the caller
# to require multiple keys to compute a single buffer.
keys_for_buffer = form_info.keys_for_buffer_keys(frozenset({buffer_key}))
keys_for_buffer = self.form_mapping_info.keys_for_buffer_keys(
frozenset({buffer_key})
)
# If reading this buffer loads a permitted key, read from the tree
# We might not have _all_ keys if e.g. buffer A requires one
# but not two of the keys required for buffer B
Expand All @@ -930,20 +931,19 @@ def read_tree(self, tree: HasBranches, start: int, stop: int) -> AwkArray:
)

return awkward.from_buffers(
form,
self.expected_form,
stop - start,
container,
behavior=form_info.behavior,
buffer_key=form_info.buffer_key,
behavior=self.form_mapping_info.behavior,
buffer_key=self.form_mapping_info.buffer_key,
)

def mock(self) -> AwkArray:
awkward = uproot.extras.awkward()
high_level_form, form_info = self.form_mapping(self.base_form)
return awkward.typetracer.typetracer_from_form(
high_level_form,
self.expected_form,
highlevel=True,
behavior=form_info.behavior,
behavior=self.form_mapping_info.behavior,
)

def prepare_for_projection(self) -> tuple[AwkArray, TypeTracerReport, dict]:
Expand All @@ -952,25 +952,24 @@ def prepare_for_projection(self) -> tuple[AwkArray, TypeTracerReport, dict]:

# A form mapping will (may) remap the base form into a new form
# The remapped form can be queried for structural information
high_level_form, form_info = self.form_mapping(self.base_form)

# Build typetracer and associated report object
meta, report = awkward.typetracer.typetracer_with_report(
high_level_form,
self.expected_form,
highlevel=True,
behavior=form_info.behavior,
buffer_key=form_info.buffer_key,
behavior=self.form_mapping_info.behavior,
buffer_key=self.form_mapping_info.buffer_key,
)

return (
meta,
report,
{
"trace": dask_awkward.lib.utils.trace_form_structure(
high_level_form,
buffer_key=form_info.buffer_key,
self.expected_form,
buffer_key=self.form_mapping_info.buffer_key,
),
"form_info": form_info,
"form_info": self.form_mapping_info,
},
)

Expand Down Expand Up @@ -1016,20 +1015,27 @@ class _UprootRead(UprootReadMixin):
def __init__(
self,
ttrees,
common_keys,
interp_options,
form_mapping: ImplementsFormMapping,
base_form,
common_keys: frozenset[str],
interp_options: dict[str, Any],
base_form: Form,
expected_form: Form,
form_mapping_info: ImplementsFormMappingInfo,
) -> None:
self.ttrees = ttrees
self.common_keys = frozenset(common_keys)
self.interp_options = interp_options
self.form_mapping = form_mapping
self.base_form = base_form
self.expected_form = expected_form
self.form_mapping_info = form_mapping_info

def project_keys(self: T, keys: frozenset[str]) -> T:
return _UprootRead(
self.ttrees, keys, self.interp_options, self.form_mapping, self.base_form
self.ttrees,
keys,
self.interp_options,
self.base_form,
self.expected_form,
self.form_mapping_info,
)

def __call__(self, i_start_stop) -> AwkArray:
Expand All @@ -1046,16 +1052,18 @@ def __init__(
real_options,
common_keys,
interp_options,
form_mapping: ImplementsFormMapping,
base_form: Form,
expected_form: Form,
form_mapping_info: ImplementsFormMappingInfo,
) -> None:
self.custom_classes = custom_classes
self.allow_missing = allow_missing
self.real_options = real_options
self.common_keys = frozenset(common_keys)
self.interp_options = interp_options
self.form_mapping = form_mapping
self.base_form = base_form
self.expected_form = expected_form
self.form_mapping_info = form_mapping_info

def __call__(self, blockwise_args) -> AwkArray:
(
Expand Down Expand Up @@ -1104,8 +1112,9 @@ def project_keys(self: T, keys: frozenset[str]) -> T:
self.real_options,
keys,
self.interp_options,
self.form_mapping,
self.base_form,
self.expected_form,
self.form_mapping_info,
)


Expand Down Expand Up @@ -1289,13 +1298,22 @@ def real_filter_branch(branch):
divisions.append(0)
partition_args.append((0, 0, 0))

if form_mapping is None:
expected_form = dask_awkward.lib.utils.form_with_unique_keys(
base_form, "<root>"
)
form_mapping_info = TrivialFormMappingInfo(expected_form)
else:
expected_form, form_mapping_info = form_mapping(base_form)

return dask_awkward.from_map(
_UprootRead(
ttrees,
common_keys,
interp_options,
form_mapping=TrivialFormMapping() if form_mapping is None else form_mapping,
base_form=base_form,
expected_form=expected_form,
form_mapping_info=form_mapping_info,
),
partition_args,
divisions=tuple(divisions),
Expand Down Expand Up @@ -1370,15 +1388,24 @@ def _get_dak_array_delay_open(
)
)

if form_mapping is None:
expected_form = dask_awkward.lib.utils.form_with_unique_keys(
base_form, "<root>"
)
form_mapping_info = TrivialFormMappingInfo(expected_form)
else:
expected_form, form_mapping_info = form_mapping(base_form)

return dask_awkward.from_map(
_UprootOpenAndRead(
custom_classes,
allow_missing,
real_options,
common_keys,
interp_options,
form_mapping=TrivialFormMapping() if form_mapping is None else form_mapping,
base_form=base_form,
expected_form=expected_form,
form_mapping_info=form_mapping_info,
),
partition_args,
divisions=None if divisions is None else tuple(divisions),
Expand Down
Loading