Skip to content

Commit f3ecb5b

Browse files
sizhit2The tunix Authors
authored andcommitted
Inject unified Tunix and vLLM environment fingerprinting metadata
PiperOrigin-RevId: 915141032
1 parent e67d0ef commit f3ecb5b

2 files changed

Lines changed: 74 additions & 1 deletion

File tree

tests/sft/metrics_logger_test.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,27 @@ def test_backend_kwargs_are_passed_to_backends(self):
226226
resume="must",
227227
id="12345",
228228
settings=mock_wandb.Settings.return_value,
229+
config={
230+
"tunix_version": mock.ANY,
231+
"tunix_commit": mock.ANY,
232+
"vllm_version": mock.ANY,
233+
"vllm_commit": mock.ANY,
234+
},
229235
)
230236

237+
def test_get_module_info(self):
238+
"""Tests environment fingerprinting module info extraction."""
239+
ver, commit = metrics_logger._get_module_info(None)
240+
self.assertEqual(ver, "not_installed")
241+
self.assertEqual(commit, "not_installed")
242+
243+
dummy_module = mock.Mock()
244+
dummy_module.__version__ = "0.4.0+dev.deadbeef"
245+
del dummy_module.__file__
246+
ver, commit = metrics_logger._get_module_info(dummy_module)
247+
self.assertEqual(ver, "0.4.0+dev.deadbeef")
248+
self.assertEqual(commit, "deadbeef")
249+
231250

232251
if __name__ == "__main__":
233252
absltest.main()

tunix/sft/metrics_logger.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import collections
44
import dataclasses
55
import enum
6+
import importlib.metadata
7+
import os
8+
import subprocess
69
from typing import Any, Callable
710

811
from absl import logging
@@ -11,6 +14,41 @@
1114
import numpy as np
1215
from tunix.utils import env_utils
1316

17+
try:
18+
import vllm # pylint: disable=g-import-not-at-top
19+
except ImportError:
20+
vllm = None
21+
22+
23+
def _get_module_info(
24+
module: Any, path_for_git: str = "", package_name: str = ""
25+
) -> tuple[str, str]:
26+
"""Resolves (version, commit) concisely for environment fingerprinting."""
27+
if module is None and not package_name and not path_for_git:
28+
return "not_installed", "not_installed"
29+
30+
ver = getattr(module, "__version__", "")
31+
if package_name:
32+
try:
33+
ver = importlib.metadata.version(package_name)
34+
except Exception:
35+
pass
36+
ver = ver or "unknown"
37+
38+
commit = ""
39+
path = path_for_git or getattr(module, "__file__", "")
40+
if path and os.path.exists(path):
41+
try:
42+
d = os.path.dirname(path) if os.path.isfile(path) else path
43+
commit = subprocess.check_output(
44+
["git", "rev-parse", "HEAD"], cwd=d, stderr=subprocess.DEVNULL, text=True
45+
).strip()
46+
except Exception:
47+
pass
48+
49+
commit = commit or (ver.split("+")[-1] if "+" in ver else "unknown")
50+
return ver, commit
51+
1452
LoggingBackend = metrax_logging.LoggingBackend
1553
TensorboardBackend = metrax_logging.TensorboardBackend
1654
WandbBackend = metrax_logging.WandbBackend
@@ -55,6 +93,16 @@ def create_backends(self) -> list[LoggingBackend]:
5593
if jax.process_index() != 0:
5694
return []
5795

96+
tunix_version, tunix_commit = _get_module_info(
97+
None, path_for_git=__file__, package_name="google-tunix"
98+
)
99+
vllm_version, vllm_commit = _get_module_info(vllm)
100+
101+
logging.info("=== Tunix Environment Fingerprint ===")
102+
logging.info("Tunix: version=%s, commit=%s", tunix_version, tunix_commit)
103+
logging.info("vLLM: version=%s, commit=%s", vllm_version, vllm_commit)
104+
logging.info("=====================================")
105+
58106
# Case 1: Override. Use user-provided factories.
59107
if (
60108
"custom_backend" in self.backend_kwargs
@@ -83,7 +131,13 @@ def create_backends(self) -> list[LoggingBackend]:
83131
)
84132
)
85133
try:
86-
wandb_kwargs = kwargs_dict.get("wandb", {})
134+
wandb_kwargs = dict(kwargs_dict.get("wandb", {}))
135+
wandb_config = dict(wandb_kwargs.get("config", {}))
136+
wandb_config["tunix_version"] = tunix_version
137+
wandb_config["tunix_commit"] = tunix_commit
138+
wandb_config["vllm_version"] = vllm_version
139+
wandb_config["vllm_commit"] = vllm_commit
140+
wandb_kwargs["config"] = wandb_config
87141
active_backends.append(
88142
WandbBackend(
89143
project=self.project_name,

0 commit comments

Comments
 (0)