MCPcopy Create free account
hub / github.com/AnswerDotAI/ModernBERT / FALanguageCrossEntropy

Class FALanguageCrossEntropy

src/flex_bert.py:54–79  ·  view source on GitHub ↗

Torchmetric that computes cross entropy on language modeling outputs using flash_attn's Cross Entropy. Adds metric state variables: sum_loss (float): The sum of the per-example loss in the batch. total_items (float): The number of batches to average across. Args: di

Source from the content-addressed store, hash-verified

52
53@rename_class("LanguageCrossEntropy")
54class FALanguageCrossEntropy(LanguageCrossEntropy):
55 """Torchmetric that computes cross entropy on language modeling outputs using flash_attn's Cross Entropy.
56
57 Adds metric state variables:
58 sum_loss (float): The sum of the per-example loss in the batch.
59 total_items (float): The number of batches to average across.
60
61 Args:
62 dist_sync_on_step (bool, optional): Synchronize metric state across processes at
63 each forward() before returning the value at the step. Default: ``False``.
64 ignore_index (int, optional): The class index to ignore. Default: ``-100``.
65 """
66
67 # Make torchmetrics call update only once
68 full_state_update = False
69
70 def __init__(self, dist_sync_on_step: bool = False, ignore_index: int = -100):
71 super().__init__(dist_sync_on_step=dist_sync_on_step)
72
73 if CrossEntropyLoss is None:
74 raise ImportError("flash_attn is not installed. Please install flash_attn to use FALanguageCrossEntropy.")
75
76 self.ignore_index = ignore_index
77 self.loss_fn = CrossEntropyLoss(ignore_index=ignore_index, reduction="sum")
78 self.add_state("sum_loss", default=torch.tensor(0.0), dist_reduce_fx="sum")
79 self.add_state("total_items", default=torch.tensor(0), dist_reduce_fx="sum")
80
81
82@rename_class("LanguageCrossEntropy")

Callers 1

create_flex_bert_mlmFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected