xref: /aosp_15_r20/external/libopus/dnn/torch/osce/models/lpcnet_feature_net.py (revision a58d3d2adb790c104798cd88c8a3aff4fa8b82cc)
1"""
2/* Copyright (c) 2023 Amazon
3   Written by Jan Buethe */
4/*
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   - Redistributions of source code must retain the above copyright
10   notice, this list of conditions and the following disclaimer.
11
12   - Redistributions in binary form must reproduce the above copyright
13   notice, this list of conditions and the following disclaimer in the
14   documentation and/or other materials provided with the distribution.
15
16   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
20   OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
28"""
29
30import torch
31from torch import nn
32import torch.nn.functional as F
33
34from utils.complexity import _conv1d_flop_count
35
36class LPCNetFeatureNet(nn.Module):
37
38    def __init__(self,
39                 feature_dim=84,
40                 num_channels=256,
41                 upsamp_factor=2,
42                 lookahead=True):
43
44        super().__init__()
45
46        self.feature_dim = feature_dim
47        self.num_channels = num_channels
48        self.upsamp_factor = upsamp_factor
49        self.lookahead = lookahead
50
51        self.conv1 = nn.Conv1d(feature_dim, num_channels, 3)
52        self.conv2 = nn.Conv1d(num_channels, num_channels, 3)
53
54        self.gru = nn.GRU(num_channels, num_channels, batch_first=True)
55
56        self.tconv = nn.ConvTranspose1d(num_channels, num_channels, upsamp_factor, upsamp_factor)
57
58    def flop_count(self, rate=100):
59        count = 0
60        for conv in self.conv1, self.conv2, self.tconv:
61            count += _conv1d_flop_count(conv, rate)
62
63        count += 2 * (3 * self.gru.input_size * self.gru.hidden_size + 3 * self.gru.hidden_size * self.gru.hidden_size) * rate
64
65        return count
66
67
68    def forward(self, features, state=None):
69        """ features shape: (batch_size, num_frames, feature_dim) """
70
71        batch_size = features.size(0)
72
73        if state is None:
74            state = torch.zeros((1, batch_size, self.num_channels), device=features.device)
75
76
77        features = features.permute(0, 2, 1)
78        if self.lookahead:
79            c = torch.tanh(self.conv1(F.pad(features, [1, 1])))
80            c = torch.tanh(self.conv2(F.pad(c, [2, 0])))
81        else:
82            c = torch.tanh(self.conv1(F.pad(features, [2, 0])))
83            c = torch.tanh(self.conv2(F.pad(c, [2, 0])))
84
85        c = torch.tanh(self.tconv(c))
86
87        c = c.permute(0, 2, 1)
88
89        c, _ = self.gru(c, state)
90
91        return c