Update modeling_neollm.py
Browse files- modeling_neollm.py +35 -13
modeling_neollm.py
CHANGED
|
@@ -6095,14 +6095,26 @@ _EXTENDED_CCE_INSTALL = (
|
|
| 6095 |
"cut-cross-entropy @ "
|
| 6096 |
"git+https://github.com/Kitsunp/ml-cross-entropy.git@main"
|
| 6097 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6098 |
|
| 6099 |
|
| 6100 |
def _require_extended_cce_options(*option_names: str) -> None:
|
| 6101 |
"""Fail clearly only when an explicitly enabled CCE extension is unavailable."""
|
| 6102 |
if linear_cross_entropy is None:
|
| 6103 |
return
|
| 6104 |
-
|
| 6105 |
-
missing = [name for name in option_names if name not in parameters]
|
| 6106 |
if missing:
|
| 6107 |
raise ImportError(
|
| 6108 |
"The installed cut-cross-entropy package does not provide the enabled "
|
|
@@ -6112,7 +6124,6 @@ def _require_extended_cce_options(*option_names: str) -> None:
|
|
| 6112 |
)
|
| 6113 |
|
| 6114 |
|
| 6115 |
-
@torch.compiler.disable
|
| 6116 |
def compute_cce_loss(
|
| 6117 |
hidden_states,
|
| 6118 |
labels,
|
|
@@ -6126,7 +6137,13 @@ def compute_cce_loss(
|
|
| 6126 |
mu_loss_lambda=1e-4,
|
| 6127 |
return_loss_metrics=False,
|
| 6128 |
):
|
| 6129 |
-
"""CCE loss
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6130 |
if linear_cross_entropy is None:
|
| 6131 |
raise ImportError(
|
| 6132 |
"NeoLLM was configured with `ntp_loss_backend='cce'`, but "
|
|
@@ -7928,7 +7945,7 @@ class NeoLLMForCausalLM(NeoLLMPreTrainedModel, GenerationMixin):
|
|
| 7928 |
position_ids: Optional[torch.LongTensor] = None,
|
| 7929 |
inputs_embeds: Optional[torch.FloatTensor] = None,
|
| 7930 |
labels: Optional[torch.LongTensor] = None,
|
| 7931 |
-
meap_seed: Optional[int] = None,
|
| 7932 |
logits_to_keep: Union[int, torch.Tensor] = 0,
|
| 7933 |
output_hidden_states: Optional[bool] = None,
|
| 7934 |
return_dict: Optional[bool] = None,
|
|
@@ -7957,11 +7974,18 @@ class NeoLLMForCausalLM(NeoLLMPreTrainedModel, GenerationMixin):
|
|
| 7957 |
mask_token_id = getattr(self.config, "meap_mask_token_id", None)
|
| 7958 |
if mask_token_id is None:
|
| 7959 |
raise ValueError("`meap_mask_token_id` is required when MEAP is enabled.")
|
| 7960 |
-
|
| 7961 |
-
|
| 7962 |
-
|
| 7963 |
-
|
| 7964 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7965 |
eligible_mask = (
|
| 7966 |
attention_mask.to(device=input_ids.device, dtype=torch.bool)
|
| 7967 |
if attention_mask is not None
|
|
@@ -7989,9 +8013,7 @@ class NeoLLMForCausalLM(NeoLLMPreTrainedModel, GenerationMixin):
|
|
| 7989 |
masked_count.float() / eligible_count.clamp_min(1).float()
|
| 7990 |
).detach()
|
| 7991 |
self._last_meap_masked_tokens = masked_count.detach()
|
| 7992 |
-
self._last_meap_seed =
|
| 7993 |
-
effective_meap_seed, dtype=torch.long
|
| 7994 |
-
)
|
| 7995 |
|
| 7996 |
tweo_enabled = (
|
| 7997 |
bool(getattr(self.config, "use_tweo", False))
|
|
|
|
| 6095 |
"cut-cross-entropy @ "
|
| 6096 |
"git+https://github.com/Kitsunp/ml-cross-entropy.git@main"
|
| 6097 |
)
|
| 6098 |
+
_EXTENDED_CCE_PARAMETERS = (
|
| 6099 |
+
frozenset(inspect.signature(linear_cross_entropy).parameters)
|
| 6100 |
+
if linear_cross_entropy is not None
|
| 6101 |
+
else frozenset()
|
| 6102 |
+
)
|
| 6103 |
+
|
| 6104 |
+
# The compiler-safe CCE operator returns compact saved tensors whose first
|
| 6105 |
+
# dimension is the number of non-padding labels. Let Dynamo represent that
|
| 6106 |
+
# value as an unbacked SymInt; otherwise it splits the model immediately after
|
| 6107 |
+
# the otherwise opaque custom op. The data-dependent compaction itself remains
|
| 6108 |
+
# inside CCE and does not become an Inductor graph.
|
| 6109 |
+
if linear_cross_entropy is not None:
|
| 6110 |
+
torch._dynamo.config.capture_dynamic_output_shape_ops = True
|
| 6111 |
|
| 6112 |
|
| 6113 |
def _require_extended_cce_options(*option_names: str) -> None:
|
| 6114 |
"""Fail clearly only when an explicitly enabled CCE extension is unavailable."""
|
| 6115 |
if linear_cross_entropy is None:
|
| 6116 |
return
|
| 6117 |
+
missing = [name for name in option_names if name not in _EXTENDED_CCE_PARAMETERS]
|
|
|
|
| 6118 |
if missing:
|
| 6119 |
raise ImportError(
|
| 6120 |
"The installed cut-cross-entropy package does not provide the enabled "
|
|
|
|
| 6124 |
)
|
| 6125 |
|
| 6126 |
|
|
|
|
| 6127 |
def compute_cce_loss(
|
| 6128 |
hidden_states,
|
| 6129 |
labels,
|
|
|
|
| 6137 |
mu_loss_lambda=1e-4,
|
| 6138 |
return_loss_metrics=False,
|
| 6139 |
):
|
| 6140 |
+
"""CCE loss with a compiler-visible custom-op boundary in the extended backend.
|
| 6141 |
+
|
| 6142 |
+
The backend keeps data-dependent label compaction and Triton autotuning
|
| 6143 |
+
opaque while exposing one forward/backward operator to Dynamo. Older CCE
|
| 6144 |
+
installations retain their normal eager fallback and fail clearly only
|
| 6145 |
+
when an explicitly requested extension is unavailable.
|
| 6146 |
+
"""
|
| 6147 |
if linear_cross_entropy is None:
|
| 6148 |
raise ImportError(
|
| 6149 |
"NeoLLM was configured with `ntp_loss_backend='cce'`, but "
|
|
|
|
| 7945 |
position_ids: Optional[torch.LongTensor] = None,
|
| 7946 |
inputs_embeds: Optional[torch.FloatTensor] = None,
|
| 7947 |
labels: Optional[torch.LongTensor] = None,
|
| 7948 |
+
meap_seed: Optional[Union[int, torch.Tensor]] = None,
|
| 7949 |
logits_to_keep: Union[int, torch.Tensor] = 0,
|
| 7950 |
output_hidden_states: Optional[bool] = None,
|
| 7951 |
return_dict: Optional[bool] = None,
|
|
|
|
| 7974 |
mask_token_id = getattr(self.config, "meap_mask_token_id", None)
|
| 7975 |
if mask_token_id is None:
|
| 7976 |
raise ValueError("`meap_mask_token_id` is required when MEAP is enabled.")
|
| 7977 |
+
if meap_seed is None:
|
| 7978 |
+
effective_meap_seed = input_ids.new_tensor(
|
| 7979 |
+
int(getattr(self.config, "meap_seed", 0)), dtype=torch.long
|
| 7980 |
+
)
|
| 7981 |
+
elif isinstance(meap_seed, torch.Tensor):
|
| 7982 |
+
effective_meap_seed = meap_seed.to(
|
| 7983 |
+
device=input_ids.device, dtype=torch.long
|
| 7984 |
+
)
|
| 7985 |
+
else:
|
| 7986 |
+
effective_meap_seed = input_ids.new_tensor(
|
| 7987 |
+
int(meap_seed), dtype=torch.long
|
| 7988 |
+
)
|
| 7989 |
eligible_mask = (
|
| 7990 |
attention_mask.to(device=input_ids.device, dtype=torch.bool)
|
| 7991 |
if attention_mask is not None
|
|
|
|
| 8013 |
masked_count.float() / eligible_count.clamp_min(1).float()
|
| 8014 |
).detach()
|
| 8015 |
self._last_meap_masked_tokens = masked_count.detach()
|
| 8016 |
+
self._last_meap_seed = effective_meap_seed.detach()
|
|
|
|
|
|
|
| 8017 |
|
| 8018 |
tweo_enabled = (
|
| 8019 |
bool(getattr(self.config, "use_tweo", False))
|