NVIDIA-Merlin / NVIDIA-Merlin/Transformers4Rec

[BUG] `Head._task_weights = defaultdict()` silently behaves like a plain dict

Open Beginner friendly
#813 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.3k
Forks
165
Avg merge
1m
Merged PRs (30d)
2

Description

Bug description

PR #802 (commit ab7207cf) changed self._task_weights = defaultdict(lambda: 1.0) to self._task_weights = defaultdict() in transformers4rec/torch/model/base.py:

https://github.com/NVIDIA-Merlin/Transformers4Rec/blob/8bf122f5dcb39feecfc6dabde734d79c2d1c4380/transformers4rec/torch/model/base.py#L272

self._task_weights = defaultdict()

defaultdict with no factory argument has the same behavior as a plain dict — missing keys raise KeyError. The previous defaultdict(lambda: 1.0) returned the documented default 1.0 for tasks without an explicit weight.

Only one usage was patched to .get(name, 1.0). Direct indexing sites (e.g. future code, or external subclasses that read head._task_weights[task_name]) will now raise KeyError where they previously returned 1.0.

Steps/Code to reproduce bug
from collections import defaultdict

old = defaultdict(lambda: 1.0)
new = defaultdict()

old["anything"]   # -> 1.0
new["anything"]   # KeyError: 'anything'
Expected behavior

Either restore the lambda: 1.0 factory, so defaultdict conveys its documented meaning, or switch to plain dict() with consistent .get(name, 1.0) access at every call site. Using defaultdict() is misleading because the name promises a default factory it no longer has.

Environment details
  • Transformers4Rec: main @ 8bf122f5 (regression from PR #802 / commit ab7207cf)
Additional context

Preferred fix — restore the factory (closest to original behavior):

self._task_weights = defaultdict(lambda: 1.0)

Happy to send a one-line PR.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Open transformers4rec/torch/model/base.py and inspect the _task_weights initialization against the prior factory behavior described in the issue. Verify the reproduction and relevant existing tests; done means missing task names return 1.0 while explicit weights continue to work.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
machine-learning
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
74/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.