|
35 | 35 |
|
36 | 36 | # 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. |
37 | 37 | skip_attrs_due_to_errs: dict[str, set[str | Hashable]] = {} |
| 38 | +skip_type_due_to_errs: dict[type, int] = {} |
| 39 | +TYPE_ERR_THRESHOLD = 3 |
38 | 40 |
|
39 | 41 | logger = logging.getLogger(__name__) |
40 | 42 |
|
@@ -297,13 +299,21 @@ def convert_var_to_dict(var, include_tensor_data=True, dump_config=None) -> dict |
297 | 299 | result: dict[str, object | str] = {} |
298 | 300 | # currently only dump primitive types, tensors and nn.Module |
299 | 301 | 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 |
300 | 307 | try: |
301 | | - attr_names: list[str | Hashable] = [ |
| 308 | + attr_names: list[str] = [ |
302 | 309 | name for name in dir(var) if not name.startswith("__") |
303 | 310 | ] # 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. |
304 | 311 | 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)] |
306 | 313 | 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 |
307 | 317 | get_instrumentation_logger_for_process().debug( |
308 | 318 | f"Failed to get attributes of object type {type(var)}, skipping it. Error: {e}." |
309 | 319 | ) |
@@ -415,6 +425,6 @@ def var_to_serializable(obj, dump_config=None) -> dict[str, object]: |
415 | 425 | obj |
416 | 426 | ) # 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. |
417 | 427 | return {typename(obj): obj} |
418 | | - except TypeError: |
| 428 | + except (TypeError, AttributeError): |
419 | 429 | return obj_to_serializable(obj, dump_config=dump_config) |
420 | 430 | # assert var_dict, f"Failed to convert object {obj} to dict." |
0 commit comments