Decorator to make any fx with this use the lazy property :param fn: :return:
(fn)
| 45 | |
| 46 | |
| 47 | def data_loader(fn): |
| 48 | """ |
| 49 | Decorator to make any fx with this use the lazy property |
| 50 | :param fn: |
| 51 | :return: |
| 52 | """ |
| 53 | |
| 54 | wraps(fn) |
| 55 | attr_name = '_lazy_' + fn.__name__ |
| 56 | |
| 57 | def _get_data_loader(self): |
| 58 | try: |
| 59 | value = getattr(self, attr_name) |
| 60 | except AttributeError: |
| 61 | try: |
| 62 | value = fn(self) # Lazy evaluation, done only once. |
| 63 | if ( |
| 64 | value is not None and |
| 65 | not isinstance(value, list) and |
| 66 | fn.__name__ in ['test_dataloader', 'val_dataloader'] |
| 67 | ): |
| 68 | value = [value] |
| 69 | except AttributeError as e: |
| 70 | # Guard against AttributeError suppression. (Issue #142) |
| 71 | traceback.print_exc() |
| 72 | error = f'{fn.__name__}: An AttributeError was encountered: ' + str(e) |
| 73 | raise RuntimeError(error) from e |
| 74 | setattr(self, attr_name, value) # Memoize evaluation. |
| 75 | return value |
| 76 | |
| 77 | return _get_data_loader |
| 78 | |
| 79 | |
| 80 | def parallel_apply(modules, inputs, kwargs_tup=None, devices=None): # pragma: no cover |
nothing calls this directly
no outgoing calls
no test coverage detected