Skip to content

Commit c7dcfdb

Browse files
committed
refactor(data): simplify packing fingerprint
Signed-off-by: yaoyu-33 <yaoyu.094@gmail.com>
1 parent 3125bad commit c7dcfdb

1 file changed

Lines changed: 26 additions & 51 deletions

File tree

‎src/megatron/bridge/data/builders/gpt_sft.py‎

Lines changed: 26 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def _default_gpt_sft_preprocessing() -> PromptCompletionSFTPreprocessingConfig:
8080
)
8181

8282

83-
def _huggingface_snapshot_identity(tokenizer_path: str) -> tuple[str, str] | None:
83+
def _hf_snapshot_identity(tokenizer_path: str) -> tuple[str, str] | None:
8484
"""Extract the repository cache name and revision from a Hugging Face snapshot path."""
8585
match = re.search(
8686
r"(?:^|[/\\])models--(?P<repository>[^/\\]+)[/\\]snapshots[/\\](?P<revision>[^/\\]+)",
@@ -91,67 +91,42 @@ def _huggingface_snapshot_identity(tokenizer_path: str) -> tuple[str, str] | Non
9191
return match.group("repository"), match.group("revision")
9292

9393

94-
def _tokenizer_packing_identity(tokenizer: MegatronTokenizer) -> dict[str, Any]:
95-
"""Return stable tokenizer properties that can change packed token IDs."""
96-
identifiers = getattr(tokenizer, "unique_identifiers", None)
97-
if isinstance(identifiers, dict):
98-
normalized_identifiers = dict(identifiers)
99-
tokenizer_path = normalized_identifiers.get("tokenizer_path")
100-
if isinstance(tokenizer_path, str):
101-
snapshot_identity = _huggingface_snapshot_identity(tokenizer_path)
102-
if snapshot_identity is not None:
103-
repository, revision = snapshot_identity
104-
normalized_identifiers["tokenizer_path"] = f"hf-cache://{repository}@{revision}"
105-
template_identifier = normalized_identifiers.pop("chat_template", None)
106-
if isinstance(template_identifier, str):
107-
normalized_identifiers["chat_template_sha256"] = hashlib.sha256(
108-
template_identifier.encode("utf-8")
109-
).hexdigest()
110-
identity: dict[str, Any] = {"unique_identifiers": normalized_identifiers}
111-
else:
112-
identity = {"class": f"{type(tokenizer).__module__}.{type(tokenizer).__qualname__}"}
113-
114-
chat_template = getattr(tokenizer, "chat_template", None)
115-
if not isinstance(chat_template, str):
116-
tokenizer_instance = getattr(tokenizer, "_tokenizer", None)
117-
chat_template = getattr(tokenizer_instance, "chat_template", None)
118-
if isinstance(chat_template, str):
119-
identity["chat_template_sha256"] = hashlib.sha256(chat_template.encode("utf-8")).hexdigest()
120-
121-
for attribute in ("bos_id", "eos_id", "pad_id"):
122-
try:
123-
token_id = getattr(tokenizer, attribute, None)
124-
except (AttributeError, NotImplementedError):
125-
continue
126-
if isinstance(token_id, int) and not isinstance(token_id, bool):
127-
identity[attribute] = token_id
128-
129-
return identity
130-
131-
13294
def _packing_fingerprint(
13395
config: "GPTSFTDatasetConfig",
13496
dataset_kwargs: dict[str, Any] | None,
13597
tokenizer: MegatronTokenizer,
13698
) -> str:
13799
"""Fingerprint every setting that can change builder-managed packed rows."""
138100
preprocessing = resolve_gpt_sft_preprocessing(config)
101+
tokenizer_identity = getattr(tokenizer, "unique_identifiers", None)
102+
if not isinstance(tokenizer_identity, dict):
103+
tokenizer_identity = {"class": f"{type(tokenizer).__module__}.{type(tokenizer).__qualname__}"}
104+
else:
105+
tokenizer_identity = dict(tokenizer_identity)
106+
tokenizer_path = tokenizer_identity.get("tokenizer_path")
107+
if isinstance(tokenizer_path, str) and (snapshot := _hf_snapshot_identity(tokenizer_path)) is not None:
108+
tokenizer_identity["tokenizer_path"] = f"hf-cache://{snapshot[0]}@{snapshot[1]}"
109+
110+
chat_template = getattr(tokenizer, "chat_template", None)
111+
if not isinstance(chat_template, str):
112+
chat_template = getattr(getattr(tokenizer, "_tokenizer", None), "chat_template", None)
113+
if not isinstance(chat_template, str):
114+
chat_template = None
115+
139116
packing_identity = {
140117
"seq_length": config.seq_length,
141118
"seed": config.seed,
142-
"preprocessing_explicit": config.preprocessing is not None,
143-
"preprocessing_type": type(preprocessing).__name__,
144-
"preprocessing": asdict(preprocessing),
119+
"preprocessing": (type(preprocessing).__name__, asdict(preprocessing)),
145120
"dataset_kwargs": dataset_kwargs,
146-
"tokenizer": _tokenizer_packing_identity(tokenizer),
121+
"tokenizer": tokenizer_identity,
122+
"chat_template": chat_template,
123+
"chat_eos_policy": "append_if_missing" if isinstance(preprocessing, ChatSFTPreprocessingConfig) else None,
124+
"max_single_sequence_length": (
125+
config.offline_packing_specs.max_single_sequence_length
126+
if config.offline_packing_specs is not None
127+
else None
128+
),
147129
}
148-
if isinstance(preprocessing, ChatSFTPreprocessingConfig):
149-
packing_identity["chat_eos_policy"] = "append_if_missing_v1"
150-
if (
151-
config.offline_packing_specs is not None
152-
and config.offline_packing_specs.max_single_sequence_length is not None
153-
):
154-
packing_identity["max_single_sequence_length"] = config.offline_packing_specs.max_single_sequence_length
155130
return hashlib.sha256(
156131
json.dumps(packing_identity, sort_keys=True, separators=(",", ":"), default=str).encode("utf-8")
157132
).hexdigest()[:12]
@@ -960,7 +935,7 @@ def _extract_tokenizer_model_name(self) -> str:
960935
return self.offline_packing_specs.tokenizer_model_name
961936
elif isinstance(tokenizer_instance, tokenizer_cls):
962937
name = str(self.tokenizer.path)
963-
snapshot_identity = _huggingface_snapshot_identity(name)
938+
snapshot_identity = _hf_snapshot_identity(name)
964939
if snapshot_identity is not None:
965940
tokenizer_model_name, _ = snapshot_identity
966941
return tokenizer_model_name

0 commit comments

Comments
 (0)