File size: 8,923 Bytes
36ec730
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import os
from typing import Optional, Tuple, Union
import torch
from torch import nn
from torch.nn import functional as F
from transformers import PreTrainedModel
from transformers.modeling_outputs import SequenceClassifierOutput
from .modeling_caduceus_moe import CaduceusPh, CaduceusPhPreTrainedModel
from .configuration_caduceus_ph import CaduceusPhConfig

class DilatedConvLayer(nn.Module):
    def __init__(self, in_channels, out_channels, padding, dilation, groups=1):
        super().__init__()
        self.dilated_conv = nn.Sequential(
            nn.Conv1d(
                in_channels=in_channels, 
                out_channels=out_channels, 
                kernel_size=3, 
                padding=padding, 
                dilation=dilation, 
                groups=groups,
            ),
            nn.SiLU(),
            nn.Dropout1d(p=0.25),
        )

    def forward(self, x: torch.Tensor):

        return self.dilated_conv(x)
    
class NormLayer(nn.Module):
    def __init__(self, norm_shape):
        super().__init__()
        self.layer_norm = nn.LayerNorm(normalized_shape=norm_shape)

    def forward(self, x: torch.Tensor):
        x = self.layer_norm(x.transpose(1,2))

        return x.transpose(1,2)
    
class DilatedConvBlock(nn.Module):
    def __init__(self, in_channels, out_channels, padding, dilation):
        super().__init__()
        self.dilated_layer = DilatedConvLayer(
            in_channels=in_channels,
            out_channels=out_channels,
            padding=padding,
            dilation=dilation,
            groups=1,
        )
        self.norm = NormLayer(out_channels)

    def forward(self, x: torch.Tensor):
        y = self.dilated_layer(x)
        y = self.norm(y)

        return y
    
class ShiftedConvBlock(nn.Module):
    def __init__(self, n_layer, in_channels, out_channels, padding, dilation, shift_steps=2, shift_stride=1):
        super().__init__()
        self.shift_steps = shift_steps
        self.hf_shift_steps = shift_steps // 2
        self.shift_stride = shift_stride
        self.pad = (shift_steps*shift_stride) // 2

        first_layer = [
            DilatedConvLayer(
                in_channels=in_channels,
                out_channels=out_channels,
                padding=padding,
                dilation=dilation,
                groups=shift_steps
            )
        ]
        next_layer = [
            DilatedConvLayer(
                in_channels=out_channels,
                out_channels=out_channels,
                padding=padding,
                dilation=dilation,
                groups=shift_steps,
            ) 
            for i in range(n_layer-1)
        ]

        self.shifted_layers = nn.ModuleList(first_layer+next_layer)
        self.norm = NormLayer(out_channels)

    def shift(self, x: torch.Tensor):
        bsz, d, L = x.shape
        xn = F.pad(x, (self.pad ,self.pad), "constant", 0)
        xs = torch.chunk(xn, self.shift_steps, 1)
        x_shift = [torch.roll(x_c, shift*self.shift_stride, 2) for x_c, shift in zip(xs, range(-self.hf_shift_steps, self.hf_shift_steps+1))]
        x_cat = torch.cat(x_shift, 1)
        x_cat = torch.narrow(x_cat, 2, self.pad, L)

        return x_cat

    def forward(self, x: torch.Tensor):
        y = self.shift(x)
        for layer in self.shifted_layers:
            y = layer(y)
        y = self.norm(y)

        return y
    
class ShiftedUNetHead(nn.Module):
    def __init__(

            self,

            embd_dim=[512,1024,1536,2560,4096],

            dilation=[1,4,8,16],

            shift_steps=[2,4,4]

        ):
        super().__init__()
        self.down_conv1 = DilatedConvBlock(embd_dim[0], embd_dim[1], padding=dilation[0], dilation=dilation[0])

        self.down_conv2 = ShiftedConvBlock(
            n_layer=2, in_channels=embd_dim[1], out_channels=embd_dim[2], 
            padding=dilation[1], dilation=dilation[1], shift_steps=shift_steps[0]
        )
        self.down_conv3 = ShiftedConvBlock(
            n_layer=2, in_channels=embd_dim[2], out_channels=embd_dim[3], 
            padding=dilation[2], dilation=dilation[2], shift_steps=shift_steps[1]
        )
        self.down_conv4 = ShiftedConvBlock(
            n_layer=3, in_channels=embd_dim[3], out_channels=embd_dim[4], 
            padding=dilation[3], dilation=dilation[3], shift_steps=shift_steps[2]
        )

        self.up_trans1 = nn.ConvTranspose1d(embd_dim[4], embd_dim[3], kernel_size=2, stride=2, groups=128)
        self.up_trans2 = nn.ConvTranspose1d(embd_dim[3], embd_dim[2], kernel_size=2, stride=2, groups=128)
        self.up_trans3 = nn.ConvTranspose1d(embd_dim[2], embd_dim[1], kernel_size=2, stride=2, groups=128)
        
        self.up_conv1 = ShiftedConvBlock(
            n_layer=2, in_channels=embd_dim[3], out_channels=embd_dim[3],
            padding=dilation[2], dilation=dilation[2], shift_steps=shift_steps[1]
        )
        self.up_conv2 = ShiftedConvBlock(
            n_layer=2, in_channels=embd_dim[2], out_channels=embd_dim[2],
            padding=dilation[1], dilation=dilation[1], shift_steps=shift_steps[0]
        )
        self.up_conv3 = DilatedConvBlock(embd_dim[1], embd_dim[1], padding=dilation[0], dilation=dilation[0])

        self.norm_f = NormLayer(embd_dim[1])

    def forward(self, x: torch.Tensor): 
        x = self.down_conv1(x) 
        t1 = x

        x = F.avg_pool1d(x, kernel_size=2, stride=2) 
        x = self.down_conv2(x) 
        t3 = x

        x = F.avg_pool1d(x, kernel_size=2, stride=2) 
        x = self.down_conv3(x) 
        t5 = x

        x = F.avg_pool1d(x, kernel_size=2, stride=2)
        x = self.down_conv4(x)

        x = self.up_trans1(x) 
        x = torch.add(x, t5) 
        x = self.up_conv1(x) 

        x = self.up_trans2(x) 
        x = torch.add(x, t3) 
        x = self.up_conv2(x) 

        x = self.up_trans3(x) 
        x = torch.add(x, t1) 
        x = self.up_conv3(x) 
        
        return self.norm_f(x)

class SegmentCaduceus(CaduceusPhPreTrainedModel):
    """SegmentCaduceusModel for sequence segmentation"""

    def __init__(self, config: CaduceusPhConfig, device=None, dtype=None, **kwargs):
        super().__init__(config, **kwargs)
        self.config = config
        self.num_features = config.num_features
        self.training_features = None
        factory_kwargs = {"device": device, "dtype": dtype}
        self.caduceus_ph = CaduceusPh(config, **factory_kwargs, **kwargs)
        
        self.shift_unet_head = ShiftedUNetHead()
        self.final_head = nn.Sequential(
            nn.Conv1d(2 * config.d_model, config.d_model, kernel_size=1, padding=0),
            nn.SiLU(),
            nn.Conv1d(config.d_model, self.num_features, kernel_size=1, padding=0),
        )

        self.post_init()
        
    def get_feature_logits(self, feature: str, strand: str, logits: torch.FloatTensor):
        if feature not in {"gene", "CDS", "exon"}:
            raise ValueError("Input features must be in 'gene', 'CDS' or 'exon'.")
        if strand not in {"+", "-"}:
            raise ValueError("Input strand must be '+' or '-'.")
            
        feature_dict = {
            "gene+": 0, 
            "gene-":1,
            "exon+": 6, 
            "exon-":7,
            "CDS+": 8, 
            "CDS-":9,
        }
        
        feature_index = feature_dict[feature+strand]
        
        return logits[...,feature_index]

    def forward(

        self,

        input_ids: torch.LongTensor = None,

        inputs_embeds: Optional[torch.FloatTensor] = None,

        labels: Optional[torch.LongTensor] = None,

        output_hidden_states: Optional[bool] = None,

        return_dict: Optional[bool] = None,

        # **kwargs

    ):
        if not ((input_ids.shape[1] - 2) % 8 == 0):
            raise ValueError("Input sequence length must be divisible by the 8.")
        
        output_hidden_states = (
            output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
        )
        return_dict = return_dict if return_dict is not None else self.config.use_return_dict

        outputs = self.caduceus_ph(
            input_ids=input_ids,
            inputs_embeds=inputs_embeds,
            output_hidden_states=output_hidden_states,
            return_dict=return_dict,
        )

        hidden_states = self.shift_unet_head(outputs[0][:,1:-1,:].transpose(1,2))
        logits = self.final_head(hidden_states)
        logits = torch.sigmoid(torch.transpose(logits, 1, 2))

        return SequenceClassifierOutput(
            loss=None,
            logits=logits,
            hidden_states=torch.transpose(hidden_states, 1, 2),
        )