forked from Smorodov/Multitarget-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRuCLIP.h
More file actions
194 lines (168 loc) · 5.14 KB
/
Copy pathRuCLIP.h
File metadata and controls
194 lines (168 loc) · 5.14 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
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
#pragma once
#include "json.hpp"
#include <filesystem>
#include <fstream>
#include "TorchHeader.h"
///to handle fp16
class RCLayerNormImpl : public torch::nn::LayerNormImpl {
protected:
public:
RCLayerNormImpl(std::vector<int64_t> normalized_shape) : LayerNormImpl(normalized_shape) {}
virtual ~RCLayerNormImpl() {}
torch::Tensor forward(const torch::Tensor &x) ///!override
{
auto orig_type = x.dtype();
auto result = torch::nn::LayerNormImpl::forward(x.to(torch::kFloat32));
return result.to(orig_type);
}
};
TORCH_MODULE(RCLayerNorm);
class QuickGELUImpl : public torch::nn::Module {
protected:
public:
QuickGELUImpl() : torch::nn::Module() {}
virtual ~QuickGELUImpl() {}
torch::Tensor forward(const torch::Tensor &x)
{
return x * torch::sigmoid(1.702f * x);
}
};
TORCH_MODULE(QuickGELU);
class ResidualAttentionBlockImpl : public torch::nn::Module {
protected:
torch::nn::MultiheadAttention Attn{ nullptr };
RCLayerNorm Ln1{ nullptr };
torch::nn::Sequential Mlp{ nullptr };
RCLayerNorm Ln2{ nullptr };
torch::Tensor AttnMask;
public:
ResidualAttentionBlockImpl(const std::string& module_name, const int d_model, const int n_head, const torch::Tensor& attn_mask);
virtual ~ResidualAttentionBlockImpl() {}
torch::Tensor Attention(const torch::Tensor &x);
torch::Tensor forward(const torch::Tensor &x);
torch::nn::MultiheadAttention GetAttn() { return Attn; }
torch::nn::Sequential GetMlp() { return Mlp; }
};
TORCH_MODULE(ResidualAttentionBlock);
class TransformerImpl : public torch::nn::Module {
protected:
int Width,
Layers,
Heads;
//torch::Tensor AttnMask;
torch::nn::Sequential Resblocks;
public:
TransformerImpl(const std::string &module_name, const int width, const int layers, const int heads, const torch::Tensor &attn_mask = torch::Tensor());
virtual ~TransformerImpl() {}
torch::Tensor forward(const torch::Tensor &x);
void InitializeParameters();
};
TORCH_MODULE(Transformer);
class VisionTransformerImpl : public torch::nn::Module {
protected:
int InputResolution,
OutputDim;
torch::nn::Conv2d Conv1{ nullptr };
torch::Tensor ClassEmbedding,
PositionalEmbedding,
Proj;
RCLayerNorm LnPre{ nullptr },
LnPost{ nullptr };
Transformer VTTransformer{ nullptr };
public:
VisionTransformerImpl(
const std::string& module_name,
const int input_resolution,
const int patch_size,
const int width,
const int layers,
const int heads,
const int output_dim
);
virtual ~VisionTransformerImpl() {}
torch::Tensor forward(const torch::Tensor& x);
};
TORCH_MODULE(VisionTransformer);
class CLIPImpl : public torch::nn::Module {
protected:
int EosId,
//EmbedDim,
//ImageResolution,
//VisionLayers,
//VisionWidth,
//VisionPatchSize,
ContextLength,
VocabSize,
TransformerWidth,
/*TransformerHeads,*/
TransformerLayers;
VisionTransformer Visual{ nullptr };
Transformer NVTransformer{ nullptr };
torch::nn::Embedding TokenEmbedding{ nullptr };
torch::Tensor PositionalEmbedding;
RCLayerNorm LnFinal{ nullptr };
torch::Tensor TextProjection,
LogitScale;
public:
CLIPImpl(
const std::string &module_name,
const int embed_dim,
const int image_resolution,
const int vision_layers,
const int vision_width,
const int vision_patch_size,
const int context_length,
const int vocab_size,
const int transformer_width,
const int transformer_heads,
const int transformer_layers,
const int eos_id = 3
);
virtual ~CLIPImpl() {}
void InitializeParameters();
torch::Tensor BuildAttentionMask();
//auto dtype()
//{
// return Visual.conv1.weight.dtype();
//}
///pixel_values : torch::Tensor Processed images from RuCLIPProcessor class, out: image_latents : torch::Tensor Image embeddings
torch::Tensor EncodeImage(torch::Tensor pixel_values);
///input_ids : torch::Tensor Tokenized texts from RuCLIPProcessor class, out: text_latents : torch::Tensor Text embeddings
torch::Tensor EncodeText(torch::Tensor input_ids);
torch::Tensor forward(torch::Tensor input_ids, torch::Tensor pixel_values);
torch::Tensor GetLogitScale() { return LogitScale; }
};
TORCH_MODULE(CLIP);
inline CLIP FromPretrained(const std::filesystem::path &folder)
{
using json = nlohmann::json;
std::filesystem::path path = folder / "config.json";
std::cout << path << std::endl;
std::ifstream f(path);
json config = json::parse(f);
// Создание модели
auto clip = CLIP("ruclip",
int(config["embed_dim"]),
int(config["image_resolution"]),
int(config["vision_layers"]),
int(config["vision_width"]),
int(config["vision_patch_size"]),
int(config["context_length"]),
int(config["vocab_size"]),
int(config["transformer_width"]),
int(config["transformer_heads"]),
int(config["transformer_layers"]));
//for (auto &k : clip->named_parameters())
// std::cout << k.key() << std::endl;
//std::cout << "Model params count: " << Trainable::ParamsCount(clip) << std::endl;
// Загрузка состояния модели из файла
try
{
torch::load(clip, (folder / "jit_model.zip").string());
}
catch (std::exception& e)
{
std::cout << e.what() << std::endl;
}
return clip;
}