MCPcopy Index your code
hub / github.com/OpenDriveLab/ReSim / test_bert_inference

Function test_bert_inference

SwissArmyTransformer/tests/test_model_parallel.py:6–19  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

4import os
5
6def test_bert_inference():
7 with torch.no_grad():
8 model, args = BertModel.from_pretrained('bert-base-uncased')
9 model = model.to(int(os.getenv('LOCAL_RANK')))
10 model_device = next(model.parameters()).device
11 x = torch.ones((8, 256), device=model_device, dtype=torch.long)
12 inp = x.clone(); inp[:, -10:]*=30000 # split vocab
13 a = model(input_ids=inp, position_ids=x, attention_mask=None, token_type_ids=x*0)
14 mp_split_model(model, 2)
15
16 b = model(input_ids=inp, position_ids=x, attention_mask=None, token_type_ids=x*0)
17
18 print((a[0]-b[0]).abs().max())
19 # assert torch.allclose(a[0], b[0])
20
21if __name__ == '__main__':
22 # torchrun --standalone --nnodes=1 --nproc-per-node=2 tests/test_model_parallel.py

Callers 1

Calls 6

mp_split_modelFunction · 0.90
toMethod · 0.80
parametersMethod · 0.80
printFunction · 0.50
from_pretrainedMethod · 0.45
maxMethod · 0.45

Tested by

no test coverage detected