Skip to content

Commit 2f36c46

Browse files
committed
skip dumping variable types that always raise exceptions when dired"
1 parent 3fa5d2f commit 2f36c46

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

mldaikon/instrumentor/dumper.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535

3636
# this is a global variable to store the attributes that cannot be accessed due to errors, so that we don't try to access them again and waste time.
3737
skip_attrs_due_to_errs: dict[str, set[str | Hashable]] = {}
38+
skip_type_due_to_errs: dict[type, int] = {}
39+
TYPE_ERR_THRESHOLD = 3
3840

3941
logger = logging.getLogger(__name__)
4042

@@ -297,13 +299,21 @@ def convert_var_to_dict(var, include_tensor_data=True, dump_config=None) -> dict
297299
result: dict[str, object | str] = {}
298300
# currently only dump primitive types, tensors and nn.Module
299301
if dump_config is None:
302+
if (
303+
type(var) in skip_type_due_to_errs
304+
and skip_type_due_to_errs[type(var)] > TYPE_ERR_THRESHOLD
305+
):
306+
return result
300307
try:
301-
attr_names: list[str | Hashable] = [
308+
attr_names: list[str] = [
302309
name for name in dir(var) if not name.startswith("__")
303310
] # dir() won't get called on primitive vars (look at the logic below checking for primitive types) whose dump_config is always None, so no need to check for primitive types here.
304311
if issubclass(type(var), dict):
305-
attr_names += list(var.keys()) # hashable keys
312+
attr_names += [k for k in var.keys() if isinstance(k, str)]
306313
except Exception as e:
314+
if type(var) not in skip_type_due_to_errs:
315+
skip_type_due_to_errs[type(var)] = 0
316+
skip_type_due_to_errs[type(var)] += 1
307317
get_instrumentation_logger_for_process().debug(
308318
f"Failed to get attributes of object type {type(var)}, skipping it. Error: {e}."
309319
)
@@ -415,6 +425,6 @@ def var_to_serializable(obj, dump_config=None) -> dict[str, object]:
415425
obj
416426
) # HACK: using json instead of to check if obj is serializable as it always raises an exception if obj is not serializable, orjson may or may not raise an exception for unknown reasons.
417427
return {typename(obj): obj}
418-
except TypeError:
428+
except (TypeError, AttributeError):
419429
return obj_to_serializable(obj, dump_config=dump_config)
420430
# assert var_dict, f"Failed to convert object {obj} to dict."

0 commit comments

Comments
 (0)