|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +import copy |
| 8 | + |
| 9 | +import pytest |
| 10 | +import torch |
| 11 | +from executorch.examples.models import MODEL_NAME_TO_MODEL |
| 12 | +from executorch.examples.models.mlperf_tiny.streaming_wakeword import StreamingWakeWord |
| 13 | +from executorch.examples.models.model_factory import EagerModelFactory |
| 14 | +from torch import nn |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture |
| 18 | +def model(): |
| 19 | + torch.manual_seed(42) |
| 20 | + model = StreamingWakeWord().eval() |
| 21 | + with torch.no_grad(): |
| 22 | + for block in model.blocks: |
| 23 | + block.bn.bias.uniform_(-0.1, 0.1) |
| 24 | + block.bn.running_mean.uniform_(-0.2, 0.2) |
| 25 | + block.bn.running_var.uniform_(0.5, 1.5) |
| 26 | + return model |
| 27 | + |
| 28 | + |
| 29 | +@pytest.mark.parametrize("signal", ["random", "zero", "impulse", "alternating"]) |
| 30 | +@torch.no_grad() |
| 31 | +def test_matches_full_windows(model, signal): |
| 32 | + layers = [] |
| 33 | + for block in model.blocks: |
| 34 | + layers.extend( |
| 35 | + copy.deepcopy(layer) |
| 36 | + for layer in (block.depthwise, block.pointwise, block.bn, block.relu) |
| 37 | + ) |
| 38 | + reference = nn.Sequential(*layers, nn.Flatten(1), copy.deepcopy(model.classifier)) |
| 39 | + frames = torch.randn(1, 40, 70, 1) |
| 40 | + if signal == "zero": |
| 41 | + frames.zero_() |
| 42 | + elif signal == "impulse": |
| 43 | + frames.zero_() |
| 44 | + frames[:, :, 30, :] = 1 |
| 45 | + elif signal == "alternating": |
| 46 | + frames[:, :, ::2, :] = 1 |
| 47 | + frames[:, :, 1::2, :] = -1 |
| 48 | + |
| 49 | + for index in range(frames.shape[2]): |
| 50 | + actual = model(frames[:, :, index : index + 1, :]) |
| 51 | + if index >= model.RECEPTIVE_FIELD - 1: |
| 52 | + window = frames[:, :, index - model.RECEPTIVE_FIELD + 1 : index + 1, :] |
| 53 | + torch.testing.assert_close(actual, reference(window), atol=1e-6, rtol=1e-5) |
| 54 | + |
| 55 | + |
| 56 | +@torch.no_grad() |
| 57 | +def test_reset_replay_and_independent_streams(model): |
| 58 | + inputs = [torch.randn(model.FRAME_SHAPE) for _ in range(100)] |
| 59 | + independent = StreamingWakeWord().eval() |
| 60 | + independent.load_state_dict(model.state_dict()) |
| 61 | + expected = [model(frame).clone() for frame in inputs] |
| 62 | + |
| 63 | + for frame, result in zip(inputs[:35], expected[:35]): |
| 64 | + torch.testing.assert_close(independent(frame), result) |
| 65 | + |
| 66 | + model.reset() |
| 67 | + assert all(torch.count_nonzero(block.history) == 0 for block in model.blocks) |
| 68 | + for frame, result in zip(inputs[35:], expected[35:]): |
| 69 | + torch.testing.assert_close(independent(frame), result) |
| 70 | + |
| 71 | + for frame, result in zip(inputs, expected): |
| 72 | + torch.testing.assert_close(model(frame), result) |
| 73 | + |
| 74 | + |
| 75 | +@torch.no_grad() |
| 76 | +def test_factory_returns_probabilities(): |
| 77 | + example, inputs, _, _ = EagerModelFactory.create_model( |
| 78 | + *MODEL_NAME_TO_MODEL["streaming_wakeword"] |
| 79 | + ) |
| 80 | + assert not example.training |
| 81 | + output = example(*inputs) |
| 82 | + assert output.shape == (1, 3) |
| 83 | + torch.testing.assert_close(output.sum(dim=-1), torch.ones(1)) |
| 84 | + |
| 85 | + |
| 86 | +def test_rejects_multi_frame_input(model): |
| 87 | + with pytest.raises(ValueError, match="LFBE frame"): |
| 88 | + model(torch.zeros(1, 40, 2, 1)) |
0 commit comments