-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathwhisper_encoder.py
More file actions
69 lines (57 loc) · 2.22 KB
/
Copy pathwhisper_encoder.py
File metadata and controls
69 lines (57 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import logging
import depthai as dai
import numpy as np
from utils.constants import Config
logger = logging.getLogger()
class WhisperEncoder(dai.node.HostNode):
"""Processes the encoder outputs for the Whisper model.
Encoder outputs are used to initialize the decoder with necessary tensors.
These tensors include:
- k_cache_self: Key cache for self-attention
- v_cache_self: Value cache for self-attention
- x: Input token, initialized with the start of text token (SOT)
- index: Index tensor, initialized to zero
"""
def __init__(self) -> None:
super().__init__()
self.decoder_initialization = self.createOutput()
self.passthrough = self.createOutput(
name="passthrough",
)
def build(
self,
input_enc: dai.Node.Input,
decoder_tokens: dai.Node.Input,
) -> "WhisperEncoder":
self.link_args(input_enc, decoder_tokens)
return self
def process(self, encoder_out: dai.Buffer, decoder_tokens: dai.Buffer) -> None:
k_cache_cross = encoder_out.getTensor("k_cache_cross")
v_cache_cross = encoder_out.getTensor("v_cache_cross")
self.passthrough.send(encoder_out)
initialized_encoder_out = dai.NNData()
initialized_encoder_out.addTensor(
"k_cache_cross", k_cache_cross, dataType=dai.TensorInfo.DataType.FP16
)
initialized_encoder_out.addTensor(
"v_cache_cross", v_cache_cross, dataType=dai.TensorInfo.DataType.FP16
)
initialized_encoder_out.addTensor(
"k_cache_self",
np.zeros((4, 6, 64, Config.MEAN_DECODE_LEN), dtype=np.float16),
)
initialized_encoder_out.addTensor(
"v_cache_self",
np.zeros((4, 6, Config.MEAN_DECODE_LEN, 64), dtype=np.float16),
)
initialized_encoder_out.addTensor(
"x",
np.array([[Config.TOKENS.TOKEN_SOT]], dtype=np.int32),
dataType=dai.TensorInfo.DataType.INT,
)
initialized_encoder_out.addTensor(
"index",
np.zeros([1, 1], dtype=np.int32),
dataType=dai.TensorInfo.DataType.INT,
)
self.decoder_initialization.send(initialized_encoder_out)