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

Process TFRecord reader binding classes only when it is enabled #5360

Merged
merged 2 commits into from
Mar 8, 2024
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
117 changes: 59 additions & 58 deletions dali/python/nvidia/dali/ops/_operators/tfrecord.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,61 +29,62 @@ def tfrecord_enabled():
return False


def _get_impl(name, schema_name, internal_schema_name):

class _TFRecordReaderImpl(
ops.python_op_factory(name, schema_name, internal_schema_name, generated=False)
):
"""custom wrappers around ops"""

def __init__(self, path, index_path, features, **kwargs):
if isinstance(path, list):
self._path = path
else:
self._path = [path]
if isinstance(index_path, list):
self._index_path = index_path
else:
self._index_path = [index_path]

kwargs.update({"path": self._path, "index_path": self._index_path})
self._features = features

super().__init__(**kwargs)

def __call__(self, *inputs, **kwargs):
feature_names = []
features = []
for feature_name, feature in self._features.items():
feature_names.append(feature_name)
features.append(feature)
if not isinstance(feature, _b.tfrecord.Feature):
raise TypeError(
"Expected `nvidia.dali.tfrecord.Feature` for the "
f'"{feature_name}", but got {type(feature)}. '
"Use `nvidia.dali.tfrecord.FixedLenFeature` or "
"`nvidia.dali.tfrecord.VarLenFeature` to define the features to extract."
)

kwargs.update({"feature_names": feature_names, "features": features})

# We won't have MIS as this op doesn't have any inputs (Reader)
linear_outputs = super().__call__(*inputs, **kwargs)
# We may have single, flattened output
if not isinstance(linear_outputs, list):
linear_outputs = [linear_outputs]
outputs = {}
for feature_name, output in zip(feature_names, linear_outputs):
outputs[feature_name] = output

return outputs

return _TFRecordReaderImpl


class TFRecordReader(_get_impl("_TFRecordReader", "TFRecordReader", "_TFRecordReader")):
pass


class TFRecord(_get_impl("_TFRecord", "readers__TFRecord", "readers___TFRecord")):
pass
if tfrecord_enabled():

def _get_impl(name, schema_name, internal_schema_name):

class _TFRecordReaderImpl(
ops.python_op_factory(name, schema_name, internal_schema_name, generated=False)
):
"""custom wrappers around ops"""

def __init__(self, path, index_path, features, **kwargs):
if isinstance(path, list):
self._path = path
else:
self._path = [path]
if isinstance(index_path, list):
self._index_path = index_path
else:
self._index_path = [index_path]

kwargs.update({"path": self._path, "index_path": self._index_path})
self._features = features

super().__init__(**kwargs)

def __call__(self, *inputs, **kwargs):
feature_names = []
features = []
for feature_name, feature in self._features.items():
feature_names.append(feature_name)
features.append(feature)
if not isinstance(feature, _b.tfrecord.Feature):
raise TypeError(
"Expected `nvidia.dali.tfrecord.Feature` for the "
f'"{feature_name}", but got {type(feature)}. '
"Use `nvidia.dali.tfrecord.FixedLenFeature` or "
"`nvidia.dali.tfrecord.VarLenFeature` "
"to define the features to extract."
)

kwargs.update({"feature_names": feature_names, "features": features})

# We won't have MIS as this op doesn't have any inputs (Reader)
linear_outputs = super().__call__(*inputs, **kwargs)
# We may have single, flattened output
if not isinstance(linear_outputs, list):
linear_outputs = [linear_outputs]
outputs = {}
for feature_name, output in zip(feature_names, linear_outputs):
outputs[feature_name] = output

return outputs

return _TFRecordReaderImpl

class TFRecordReader(_get_impl("_TFRecordReader", "TFRecordReader", "_TFRecordReader")):
pass

class TFRecord(_get_impl("_TFRecord", "readers__TFRecord", "readers___TFRecord")):
pass
Loading