transformers - 💡(How to fix) Fix Bring Loss classes back to modeling file and leverage modular [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
huggingface/transformers#44404Fetched 2026-04-08 00:28:42
View on GitHub
Comments
1
Participants
2
Timeline
8
Reactions
0
Author
Timeline (top)
subscribed ×4mentioned ×3commented ×1

Root Cause

As discussed while implementing #36895 , I think losses that are currently present in the loss folder should be back to the modeling file like it is the case for many existing models (MaskFormer for example). I think it makes sense because the loss, when custom like the LwDetr or RfDetr one are part of the model definition. They are released by authors as part of it. And per the tenets, users should not have to browse across different files to know how the loss got computed for their training. Additionally, we have modular now so it could be a great opportunity to refactor them and leverage that. I'd like to volunteer for that.

RAW_BUFFERClick to expand / collapse

As discussed while implementing #36895 , I think losses that are currently present in the loss folder should be back to the modeling file like it is the case for many existing models (MaskFormer for example). I think it makes sense because the loss, when custom like the LwDetr or RfDetr one are part of the model definition. They are released by authors as part of it. And per the tenets, users should not have to browse across different files to know how the loss got computed for their training. Additionally, we have modular now so it could be a great opportunity to refactor them and leverage that. I'd like to volunteer for that.

EDIT : at least for the custom one that are defined in the LOSS_MAPPING : https://github.com/huggingface/transformers/blob/28d02a31386a4dfd85e2a91f86b7dfcebd44596d/src/transformers/loss/loss_utils.py#L157-L167

cc @yonigozlan @molbap @ArthurZucker

extent analysis

Problem Summary

Move custom losses from the loss folder to the modeling file.

Root Cause Analysis

The issue is that custom losses are scattered across different files, making it difficult to understand how they are computed.

Fix Plan

Step 1: Identify Custom Losses

  • Check the LOSS_MAPPING dictionary in loss_utils.py to identify custom losses.
  • For example:
LOSS_MAPPING = {
    "LwDetr": "transformers.loss.lw_detr_loss.LwDetrLoss",
    "RfDetr": "transformers.loss.rf_detr_loss.RfDetrLoss",
}

Step 2: Move Custom Losses to Modeling File

  • Create a new file for each custom loss in the modeling file (e.g., model.py).
  • For example, create a new file lw_detr_loss.py with the following content:
class LwDetrLoss:
    def __init__(self, args):
        # Initialize loss components
        self.loss_components = {}

    def forward(self, inputs):
        # Compute loss
        loss = 0
        for component in self.loss_components.values():
            loss += component(inputs)
        return loss

Step 3: Update LOSS_MAPPING Dictionary

  • Update the LOSS_MAPPING dictionary to point to the new custom loss files.
  • For example:
LOSS_MAPPING = {
    "LwDetr": "model.lw_detr_loss.LwDetrLoss",
    "RfDetr": "model.rf_detr_loss.RfDetrLoss",
}

Step 4: Refactor Modular Code

  • Refactor the modular code to leverage the new custom loss files.
  • For example:
class Model:
    def __init__(self, args):
        # Initialize custom losses
        self.losses = {
            "L

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

transformers - 💡(How to fix) Fix Bring Loss classes back to modeling file and leverage modular [1 comments, 2 participants]