Download tests/test_streaming_inference_freeze.py from Tinghong-Ye22/code-HFD: direct link, hf CLI and curl.
- Browser
- Download file 990 Bytes
-
https://huggingface.co/Tinghong-Ye22/code-HFD/resolve/main/tests/test_streaming_inference_freeze.py
- Command line
-
hf download hf://Tinghong-Ye22/code-HFD/tests/test_streaming_inference_freeze.py
-
curl -L -o test_streaming_inference_freeze.py https://huggingface.co/Tinghong-Ye22/code-HFD/resolve/main/tests/test_streaming_inference_freeze.py
990 Bytes
| import ast | |
| from pathlib import Path | |
| import torch | |
| def test_inference_loader_finalization_freezes_without_changing_weights(): | |
| path=Path(__file__).resolve().parents[1]/'src/psi/models/full_duplex_psi0.py' | |
| tree=ast.parse(path.read_text()) | |
| loader=next(n for n in ast.walk(tree) | |
| if isinstance(n,ast.FunctionDef) and n.name=='from_streaming_checkpoint') | |
| # Execute the actual finalization expression, without loading a 3B model. | |
| expression=loader.body[-2] | |
| assert isinstance(expression,ast.Expr) | |
| model=torch.nn.Sequential(torch.nn.Linear(2,2),torch.nn.Dropout(.5)) | |
| model.device=torch.device('cpu') | |
| before={k:v.clone() for k,v in model.state_dict().items()} | |
| exec(compile(ast.Module(body=[expression],type_ignores=[]),str(path),'exec'),{'model':model}) | |
| assert not model.training | |
| assert not any(p.requires_grad for p in model.parameters()) | |
| for k,v in model.state_dict().items():torch.testing.assert_close(v,before[k],rtol=0,atol=0) | |