|
| 1 | +import logging |
| 2 | +from typing import Dict |
| 3 | + |
| 4 | +import torch |
| 5 | + |
| 6 | +logger = logging.getLogger(__name__) |
| 7 | + |
| 8 | + |
| 9 | +class WeightChecker: |
| 10 | + def __init__(self, model_runner): |
| 11 | + self._model_runner = model_runner |
| 12 | + self._snapshot_tensors = None |
| 13 | + |
| 14 | + def handle(self, action: str): |
| 15 | + logger.info(f"[WeightChecker] handle action={action}") |
| 16 | + if action == "snapshot": |
| 17 | + self._snapshot() |
| 18 | + elif action == "reset_tensors": |
| 19 | + self._reset_tensors() |
| 20 | + elif action == "compare": |
| 21 | + self._compare() |
| 22 | + else: |
| 23 | + raise Exception(f"Unsupported {action=}") |
| 24 | + |
| 25 | + def _snapshot(self): |
| 26 | + named_tensors = [ |
| 27 | + (name, param.data.detach().cpu()) for name, param in self._model_state() |
| 28 | + ] |
| 29 | + self._snapshot_tensors = dict(named_tensors) |
| 30 | + assert len(self._snapshot_tensors) == len( |
| 31 | + named_tensors |
| 32 | + ), f"should not have duplicated tensor name" |
| 33 | + |
| 34 | + def _reset_tensors(self): |
| 35 | + for name, param in self._model_state(): |
| 36 | + param.copy_(_random_like(param)) |
| 37 | + |
| 38 | + def _compare(self): |
| 39 | + assert self._snapshot_tensors is not None |
| 40 | + |
| 41 | + _check_tensors( |
| 42 | + expect_tensors=self._snapshot_tensors, |
| 43 | + actual_tensors=dict(self._model_state()), |
| 44 | + ) |
| 45 | + |
| 46 | + def _model_state(self): |
| 47 | + # TODO: support EAGLE etc (e.g. yield from both main model and draft model) |
| 48 | + yield from self._model_runner.model.named_parameters() |
| 49 | + yield from self._model_runner.model.named_buffers() |
| 50 | + |
| 51 | + |
| 52 | +def _check_tensors( |
| 53 | + expect_tensors: Dict[str, torch.Tensor], actual_tensors: Dict[str, torch.Tensor] |
| 54 | +): |
| 55 | + from sglang.srt.debug_utils.dumper import get_tensor_info |
| 56 | + |
| 57 | + assert len(expect_tensors) == len(actual_tensors) |
| 58 | + |
| 59 | + good_names = [] |
| 60 | + error_messages = [] |
| 61 | + |
| 62 | + for name in expect_tensors: |
| 63 | + expect = expect_tensors[name].cuda() |
| 64 | + actual = actual_tensors[name].cuda() |
| 65 | + |
| 66 | + if torch.all(expect == actual): |
| 67 | + good_names.append(name) |
| 68 | + else: |
| 69 | + abs_diff = (actual.float() - expect.float()).abs() |
| 70 | + error_messages.append( |
| 71 | + f"name={name} " |
| 72 | + f"max_abs_err={abs_diff.max()} " |
| 73 | + f"mean_abs_err={abs_diff.mean()} " |
| 74 | + f"{get_tensor_info(expect)=} " |
| 75 | + f"{get_tensor_info(actual)=} " |
| 76 | + ) |
| 77 | + |
| 78 | + logger.info(f"[check_tensors] passed: {good_names}") |
| 79 | + if len(error_messages) > 0: |
| 80 | + raise Exception(f"check tensor equality failed:\n" + "\n".join(error_messages)) |
| 81 | + |
| 82 | + |
| 83 | +def _random_like(t: torch.Tensor): |
| 84 | + device = t.device |
| 85 | + shape = t.shape |
| 86 | + dtype = t.dtype |
| 87 | + |
| 88 | + if dtype.is_floating_point: |
| 89 | + return torch.rand(shape, device=device, dtype=torch.float32).to(dtype) |
| 90 | + |
| 91 | + if dtype == torch.bool: |
| 92 | + return torch.rand(shape, device=device) > 0.5 |
| 93 | + |
| 94 | + info = torch.iinfo(dtype) |
| 95 | + return torch.randint( |
| 96 | + low=int(info.min), high=int(info.max), size=shape, device=device, dtype=dtype |
| 97 | + ) |
0 commit comments