File size: 7,283 Bytes
2f5ce58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import torch
import torch.nn as nn
import torch.nn.functional as F
from typing import Optional
from transformers import PreTrainedModel, PretrainedConfig
from .modeling_plantgfm import PlantGFMForCausalLM

class SegmentGFMConfig(PretrainedConfig):
    model_type = "segmentglm"
    def __init__(
        self,
        pre_trained_path = None, 
        unet_embd_dim = [1024,1536,2560,4096], 
        unet_kernel_size = 3, 
        unet_dilation = [6,12,24], 
        unet_padding = [6,12,24], 
        unet_layer_dropout = 0.25, 
        out_embd_dim = 256, 
        out_k = 1, 
        **kwargs,
    ):
        self.pre_trained_path = pre_trained_path
        self.unet_embd_dim = unet_embd_dim
        self.unet_kernel_size = unet_kernel_size
        self.unet_dilation = unet_dilation
        self.unet_padding = unet_padding
        self.unet_layer_dropout = unet_layer_dropout
        self.out_embd_dim = out_embd_dim
        self.out_k = out_k

        super().__init__(**kwargs)

    @classmethod
    def from_original_config(cls, config_path, **kwargs):
        with open(config_path, "r") as f:
            config = json.load(f)

        pre_trained_path = config["pre_trained_path"]
        unet_embd_dim = config["unet_embd_dim"]
        unet_kernel_size = config["unet_kernel_size"]
        unet_dilation = config["unet_dilation"]
        unet_padding = config["unet_padding"]
        unet_layer_dropout = config["unet_layer_dropout"]
        out_embd_dim = config["out_embd_dim"]
        out_k = config["out_k"]

        return cls(
            pre_trained_path = pre_trained_path,
            unet_embd_dim = unet_embd_dim,
            unet_kernel_size = unet_kernel_size,
            unet_dilation = unet_dilation,
            unet_padding = unet_padding,
            unet_layer_dropout = unet_layer_dropout,
            out_embd_dim = out_embd_dim,
            out_k = out_k,
            **kwargs
        )

class PlantGFMEmbd(nn.Module):
    def __init__(self, config):
        super().__init__()
        self.config = config
        glm_model = PlantGFMForCausalLM.from_pretrained(self.config.pre_trained_path)
        self.glm_decoder = glm_model.get_decoder()

    def forward(self, input_ids):
        embd = self.glm_decoder(input_ids, return_dict=True)["last_hidden_state"]
        embd = embd[:,1:-1,:].transpose(1,2)
        return embd
    
class DilatedConvLayer(nn.Module):
    def __init__(self, in_channels, out_channels, kernel_size, padding, dilation, dropout_rate):
        super().__init__()
        self.dilated_conv = nn.Sequential(
            nn.Conv1d(
                in_channels=in_channels, 
                out_channels=out_channels, 
                kernel_size=kernel_size, 
                padding=padding, 
                dilation=dilation, 
            ),
            nn.Conv1d(
                in_channels=out_channels, 
                out_channels=out_channels, 
                kernel_size=kernel_size, 
                padding=padding, 
                dilation=dilation, 
            ),
            nn.SiLU(),
            nn.Dropout1d(p=dropout_rate),
        )

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

        return self.dilated_conv(x)

    
class DilatedUNetHead(nn.Module):
    def __init__(
            self, 
            embd_dim,
            kernel_size,
            padding,
            dilation,
            layer_dropout=0.25,
            out_embd_dim=256,
            out_k=1,
        ):
        super().__init__()
        self.out_k = out_k
        self.down_conv1 = DilatedConvLayer(embd_dim[0], embd_dim[1], kernel_size, padding[0], dilation[0], layer_dropout)
        self.down_conv2 = DilatedConvLayer(embd_dim[1], embd_dim[2], kernel_size, padding[1], dilation[1], layer_dropout)
        self.down_conv3 = DilatedConvLayer(embd_dim[2], embd_dim[3], kernel_size, padding[2], dilation[2], layer_dropout)

        self.up_trans1 = nn.ConvTranspose1d(embd_dim[3], embd_dim[2], kernel_size=2, stride=2, groups=64)
        self.up_trans2 = nn.ConvTranspose1d(embd_dim[2], embd_dim[1], kernel_size=2, stride=2, groups=64)

        self.up_conv1 = DilatedConvLayer(2*embd_dim[2], embd_dim[2], kernel_size, padding[1], dilation[1], layer_dropout)
        self.up_conv2 = DilatedConvLayer(2*embd_dim[1], out_embd_dim, kernel_size, padding[0], dilation[0], layer_dropout)

        self.output = nn.Conv1d(in_channels=out_embd_dim, out_channels=out_k, kernel_size=1, padding=0)

    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) 


        x = self.up_trans1(x) 
        x = torch.cat([x, t3], 1)
        x = self.up_conv1(x) 

        x = self.up_trans2(x) 
        x = torch.cat([x, t1], 1)
        x = self.up_conv2(x)

        x = self.output(x)

        if self.out_k == 1:
            return x.squeeze(1) # when out_k==1 return target (bsz, L)
        
        return x # return target (bsz, out_k, L)

class IoULoss(nn.Module):
    def __init__(self, smooth=1e-6):

        super(IoULoss, self).__init__()
        self.smooth = smooth

    def forward(self, inputs, targets):
        inputs = torch.sigmoid(inputs)
        inputs = inputs.view(inputs.size(0), -1)  # (batch_size, *)
        targets = targets.view(targets.size(0), -1)  # (batch_size, *)

        intersection = (inputs * targets).sum(dim=1)  
        total = (inputs + targets).sum(dim=1)
        union = total - intersection

        iou = (intersection + self.smooth) / (union + self.smooth)

        return 1 - iou.mean()

class CombinedLoss(nn.Module):
    def __init__(self, smooth=1e-6, bce_weight=0.5, iou_weight=0.5):

        super(CombinedLoss, self).__init__()
        self.bce_loss = nn.BCEWithLogitsLoss()
        self.iou_loss = IoULoss(smooth=smooth)
        self.bce_weight = bce_weight
        self.iou_weight = iou_weight

    def forward(self, inputs, targets):

        bce = self.bce_loss(inputs, targets)
        iou = self.iou_loss(inputs, targets)
        combined_loss = self.bce_weight * bce + self.iou_weight * iou
        return combined_loss

class SegmentGFMModel(PreTrainedModel):
    config_class = SegmentGFMConfig
    _no_split_modules = ["DilatedUNetHead"]
    supports_gradient_checkpointing = True

    def __init__(self, config):
        super().__init__(config)

        self.config = config
        self.glm_embd = PlantGFMEmbd(config=config)
        self.unet_head = DilatedUNetHead(
            self.config.unet_embd_dim,
            self.config.unet_kernel_size,
            self.config.unet_padding,
            self.config.unet_dilation,
            self.config.unet_layer_dropout,
            self.config.out_embd_dim,
            self.config.out_k
        )

        self.loss_funct = CombinedLoss(bce_weight=0.5, iou_weight=0.5)

    def forward(self, input_ids: torch.LongTensor = None, labels: Optional[torch.FloatTensor] = None):
        x = self.glm_embd(input_ids)
        x = self.unet_head(x)
        if labels is None:
            return x
        
        return {
            "loss": self.loss_funct(x, labels),
            "predictions": x
        }