-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathnet.h
More file actions
151 lines (114 loc) · 4.59 KB
/
net.h
File metadata and controls
151 lines (114 loc) · 4.59 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
#pragma once
#include <cstdint>
#include <cstdlib>
#include <memory>
#include <vector>
#include "glog/logging.h"
#include "infini_train/include/nn/modules/module.h"
#include "infini_train/include/nn/parallel/pp/pipeline_parallel.h"
#include "infini_train/include/nn/parallel/pp/pipeline_stage.h"
#include "infini_train/include/tensor.h"
struct GPT2Config {
int64_t block_size = 1024;
int64_t vocab_size = 50304;
int64_t original_vocab_size = 50257;
int64_t n_layer = 12;
int64_t n_head = 12;
int64_t n_embd = 768;
};
class NewGELU : public infini_train::nn::CloneableModule<NewGELU> {
public:
static constexpr char kType[] = "NewGELU";
NewGELU() : CloneableModule(kType) {}
std::vector<std::shared_ptr<infini_train::Tensor>>
Forward(const std::vector<std::shared_ptr<infini_train::Tensor>> &x) override;
};
class CausalSelfAttention : public infini_train::nn::CloneableModule<CausalSelfAttention> {
public:
static constexpr char kType[] = "CausalSelfAttention";
static constexpr char kCAttnLayerName[] = "c_attn";
static constexpr char kCProjLayerName[] = "c_proj";
static constexpr char kParamBiasName[] = "bias";
explicit CausalSelfAttention(const GPT2Config &config);
std::vector<std::shared_ptr<infini_train::Tensor>>
Forward(const std::vector<std::shared_ptr<infini_train::Tensor>> &x) override;
private:
GPT2Config config_;
int64_t n_head_ = 0;
int64_t n_embd_ = 0;
int64_t local_n_head_ = 0;
};
class MLP : public infini_train::nn::CloneableModule<MLP> {
public:
static constexpr char kType[] = "MLP";
static constexpr char kCFcLayerName[] = "c_fc";
static constexpr char kGeluLayerName[] = "gelu";
static constexpr char kCProjLayerName[] = "c_proj";
explicit MLP(const GPT2Config &config);
std::vector<std::shared_ptr<infini_train::Tensor>>
Forward(const std::vector<std::shared_ptr<infini_train::Tensor>> &x) override;
};
class Block : public infini_train::nn::CloneableModule<Block> {
public:
static constexpr char kType[] = "Block";
static constexpr char kLn1LayerName[] = "ln_1";
static constexpr char kAttnLayerName[] = "attn";
static constexpr char kLn2LayerName[] = "ln_2";
static constexpr char kMlpLayerName[] = "mlp";
explicit Block(const GPT2Config &config);
std::vector<std::shared_ptr<infini_train::Tensor>>
Forward(const std::vector<std::shared_ptr<infini_train::Tensor>> &x) override;
};
class GPT2FirstStage : public infini_train::nn::CloneableModule<GPT2FirstStage> {
public:
static constexpr char kType[] = "GPT2FirstStage";
static constexpr char kWTELayerName[] = "wte";
static constexpr char kWPELayerName[] = "wpe";
explicit GPT2FirstStage(const GPT2Config &config);
std::vector<std::shared_ptr<infini_train::Tensor>>
Forward(const std::vector<std::shared_ptr<infini_train::Tensor>> &x) override;
private:
const GPT2Config config_;
};
class GPT2Chunk : public infini_train::nn::CloneableModule<GPT2Chunk> {
public:
static constexpr char kType[] = "GPT2Chunk";
static constexpr char kHLayerName[] = "h";
GPT2Chunk(const GPT2Config &config, int start_layer, int end_layer);
std::vector<std::shared_ptr<infini_train::Tensor>>
Forward(const std::vector<std::shared_ptr<infini_train::Tensor>> &x) override;
private:
const GPT2Config config_;
};
class GPT2LastStage : public infini_train::nn::CloneableModule<GPT2LastStage> {
public:
static constexpr char kType[] = "GPT2LastStage";
static constexpr char kLnFLayerName[] = "ln_f";
static constexpr char kLMHeadLayerName[] = "lm_head";
explicit GPT2LastStage(const GPT2Config &config);
std::vector<std::shared_ptr<infini_train::Tensor>>
Forward(const std::vector<std::shared_ptr<infini_train::Tensor>> &x) override;
private:
const GPT2Config config_;
};
class GPT2 : public infini_train::nn::CloneableModule<GPT2> {
public:
static constexpr char kType[] = "GPT2";
static constexpr char kTransformerLayerName[] = "transformer";
enum class ModelType : int8_t {
kGPT2,
kGPT2Medium,
kGPT2Large,
kGPT2XL,
};
explicit GPT2(const GPT2Config &config);
std::vector<std::shared_ptr<infini_train::Tensor>>
Forward(const std::vector<std::shared_ptr<infini_train::Tensor>> &x) override;
static std::shared_ptr<GPT2> FromPretrained(ModelType model_type);
static std::shared_ptr<GPT2> FromLLMC(const std::string &filepath);
void SaveAsLLMC(const std::string &filepath) const;
int GetChunkSize() const;
private:
const GPT2Config config_;
const infini_train::nn::parallel::StageInfo stage_info_;
};