xref: /aosp_15_r20/external/pytorch/benchmarks/fastrnns/test_bench.py (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1*da0073e9SAndroid Build Coastguard Workerimport pytest
2*da0073e9SAndroid Build Coastguard Worker
3*da0073e9SAndroid Build Coastguard Workerimport torch
4*da0073e9SAndroid Build Coastguard Worker
5*da0073e9SAndroid Build Coastguard Workerfrom .fuser import set_fuser
6*da0073e9SAndroid Build Coastguard Workerfrom .runner import get_nn_runners
7*da0073e9SAndroid Build Coastguard Worker
8*da0073e9SAndroid Build Coastguard Worker
9*da0073e9SAndroid Build Coastguard Worker@pytest.fixture(scope="class")
10*da0073e9SAndroid Build Coastguard Workerdef modeldef(request, net_name, executor, fuser):
11*da0073e9SAndroid Build Coastguard Worker    set_fuser(fuser, executor)
12*da0073e9SAndroid Build Coastguard Worker
13*da0073e9SAndroid Build Coastguard Worker    # Given a 'net_name' provided by generate_tests, build the thing
14*da0073e9SAndroid Build Coastguard Worker    name, rnn_creator, context = get_nn_runners(net_name)[0]
15*da0073e9SAndroid Build Coastguard Worker    creator_args = creator_args = {
16*da0073e9SAndroid Build Coastguard Worker        "seqLength": 100,
17*da0073e9SAndroid Build Coastguard Worker        "numLayers": 1,
18*da0073e9SAndroid Build Coastguard Worker        "inputSize": 512,
19*da0073e9SAndroid Build Coastguard Worker        "hiddenSize": 512,
20*da0073e9SAndroid Build Coastguard Worker        "miniBatch": 64,
21*da0073e9SAndroid Build Coastguard Worker        "device": "cuda",
22*da0073e9SAndroid Build Coastguard Worker        "seed": None,
23*da0073e9SAndroid Build Coastguard Worker    }
24*da0073e9SAndroid Build Coastguard Worker    return rnn_creator(**creator_args)
25*da0073e9SAndroid Build Coastguard Worker
26*da0073e9SAndroid Build Coastguard Worker
27*da0073e9SAndroid Build Coastguard Workerdef cuda_sync(func, *args, **kwargs):
28*da0073e9SAndroid Build Coastguard Worker    out = func(*args, **kwargs)
29*da0073e9SAndroid Build Coastguard Worker    torch.cuda.synchronize()
30*da0073e9SAndroid Build Coastguard Worker    return out
31*da0073e9SAndroid Build Coastguard Worker
32*da0073e9SAndroid Build Coastguard Worker
33*da0073e9SAndroid Build Coastguard Worker@pytest.mark.benchmark(
34*da0073e9SAndroid Build Coastguard Worker    warmup=True,
35*da0073e9SAndroid Build Coastguard Worker    warmup_iterations=3,
36*da0073e9SAndroid Build Coastguard Worker    disable_gc=True,
37*da0073e9SAndroid Build Coastguard Worker    max_time=0.1,
38*da0073e9SAndroid Build Coastguard Worker    group="fastrnns",
39*da0073e9SAndroid Build Coastguard Worker)
40*da0073e9SAndroid Build Coastguard Workerclass TestBenchNetwork:
41*da0073e9SAndroid Build Coastguard Worker    # See 'modeldef' fixture, which provides the things to benchmark
42*da0073e9SAndroid Build Coastguard Worker    def test_forward(self, modeldef, benchmark):
43*da0073e9SAndroid Build Coastguard Worker        forward_output = benchmark(cuda_sync, modeldef.forward, *modeldef.inputs)
44*da0073e9SAndroid Build Coastguard Worker
45*da0073e9SAndroid Build Coastguard Worker    def test_backward(self, modeldef, benchmark):
46*da0073e9SAndroid Build Coastguard Worker        backward_input = modeldef.forward(*modeldef.inputs)
47*da0073e9SAndroid Build Coastguard Worker        if modeldef.backward_setup is not None:
48*da0073e9SAndroid Build Coastguard Worker            backward_input = modeldef.backward_setup(backward_input)
49*da0073e9SAndroid Build Coastguard Worker
50*da0073e9SAndroid Build Coastguard Worker        if modeldef.backward is not None:
51*da0073e9SAndroid Build Coastguard Worker            benchmark(cuda_sync, modeldef.backward, *backward_input, retain_graph=True)
52*da0073e9SAndroid Build Coastguard Worker
53*da0073e9SAndroid Build Coastguard Worker            with torch.no_grad():
54*da0073e9SAndroid Build Coastguard Worker                for param in modeldef.params:
55*da0073e9SAndroid Build Coastguard Worker                    assert param.grad is not None
56*da0073e9SAndroid Build Coastguard Worker                    param.grad.zero_()
57