Skip to content

logging: stop warning_once from crashing on unhashable kwargs like extra={...}#4047

Open
c-tonneslan wants to merge 1 commit into
huggingface:mainfrom
c-tonneslan:warning-once-unhashable-kwargs
Open

logging: stop warning_once from crashing on unhashable kwargs like extra={...}#4047
c-tonneslan wants to merge 1 commit into
huggingface:mainfrom
c-tonneslan:warning-once-unhashable-kwargs

Conversation

@c-tonneslan
Copy link
Copy Markdown

`MultiProcessAdapter.warning_once` is decorated with `@functools.lru_cache(None)`, which hashes every positional and keyword argument. The standard `logging` API accepts an `extra={...}` kwarg, but dicts aren't hashable, so a normal call like

```python
logger = get_logger(name)
logger.warning_once("only once", extra={"id": 1})
```

raises `TypeError: unhashable type: 'dict'`. Same story for any other unhashable kwarg (`stack_info=False` is fine, but `extra` is the common one).

Cache by the message text on a per-adapter set instead. That matches the docstring ("the same message only once"), accepts whatever kwargs the underlying `warning()` accepts, and as a bonus drops the implicit `self` retention that an `lru_cache(None)` on an instance method causes.

Added a regression test in `tests/test_logging.py` that calls `warning_once` with an `extra={...}` kwarg, twice with the same message and once with a different one, and asserts each unique message is emitted exactly once. The two existing logging tests still pass.

Sample crash before:

```pycon

from accelerate import Accelerator
from accelerate.logging import get_logger
Accelerator()
logger = get_logger("test")
logger.warning_once("hi", extra={"a": 1})
Traceback (most recent call last):
...
TypeError: unhashable type: 'dict'
```

`MultiProcessAdapter.warning_once` was decorated with
`@functools.lru_cache(None)`, which hashes every positional and
keyword argument. The standard `logging` API accepts an `extra={...}`
kwarg, and a dict isn't hashable, so a perfectly normal call like

    logger.warning_once("only once", extra={"id": 1})

raised `TypeError: unhashable type: 'dict'`.

Cache by the message text on a per-adapter set instead. That matches
the docstring (`"the same message only once"`), accepts any kwargs the
underlying `warning()` accepts, and also drops the implicit `self`
retention that `lru_cache` on a method caused.

Added a regression test in `tests/test_logging.py` that calls
`warning_once` with an `extra={...}` kwarg, twice with the same
message and once with a different message, and asserts each unique
message is emitted exactly once.

Signed-off-by: Charlie Tonneslan <cst0520@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant