forked from Tensor-Array/Tensor-Array
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransformer.cc
More file actions
95 lines (88 loc) · 3.66 KB
/
transformer.cc
File metadata and controls
95 lines (88 loc) · 3.66 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
/*
Copyright 2024 TensorArray-Creators
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "transformer.hh"
#include "layer_utility.hh"
namespace tensor_array
{
namespace layers
{
TransformerEncoderImpl::TransformerEncoderImpl(unsigned int d_model, unsigned int n_head, unsigned int ff_size) :
multihead_attn(d_model, n_head),
feed_forward
{
Linear(ff_size),
Activation(&ReLU),
Linear(d_model)
}
{
this->map_layer.insert(std::make_pair("MultiheadAttn", multihead_attn.get_shared()));
this->map_layer.insert(std::make_pair("FeedForward", feed_forward.get_shared()));
this->map_layer.insert(std::make_pair("LayerNorm1", layer_norm_1.get_shared()));
this->map_layer.insert(std::make_pair("LayerNorm2", layer_norm_2.get_shared()));
}
value::Tensor TransformerEncoderImpl::calculate(const value::Tensor& input)
{
auto attn_output = this->multihead_attn(input, input, input);
attn_output = this->layer_norm_1(input + attn_output);
auto ff_output = this->feed_forward(attn_output);
return this->layer_norm_2(attn_output + ff_output);
}
TransformerDecoderImpl::TransformerDecoderImpl(unsigned int d_model, unsigned int n_head, unsigned int ff_size) :
masked_multihead_attn(d_model, n_head),
multihead_attn(d_model, n_head),
feed_forward
{
Linear(ff_size),
Activation(&ReLU),
Linear(d_model)
}
{
this->map_layer.insert(std::make_pair("MaskedMultiheadAttn", masked_multihead_attn.get_shared()));
this->map_layer.insert(std::make_pair("MultiheadAttn", multihead_attn.get_shared()));
this->map_layer.insert(std::make_pair("FeedForward", feed_forward.get_shared()));
this->map_layer.insert(std::make_pair("LayerNorm1", layer_norm_1.get_shared()));
this->map_layer.insert(std::make_pair("LayerNorm2", layer_norm_2.get_shared()));
this->map_layer.insert(std::make_pair("LayerNorm3", layer_norm_3.get_shared()));
}
value::Tensor TransformerDecoderImpl::calculate(const value::Tensor& tgt, const value::Tensor& memory_encode, const value::Tensor& tgt_mask)
{
auto attn_output = this->masked_multihead_attn(tgt, tgt, tgt, tgt_mask);
attn_output = this->layer_norm_1(tgt + attn_output);
auto attn_output_2 = this->multihead_attn(attn_output, memory_encode, memory_encode);
attn_output = this->layer_norm_2(attn_output + attn_output_2);
auto ff_output = this->feed_forward(attn_output_2);
ff_output = this->layer_norm_3(attn_output_2 + ff_output);
return ff_output;
}
TransformerImpl::TransformerImpl(unsigned int d_model, unsigned int n_head, unsigned int ff_size, unsigned int num_layers):
fc(ff_size),
encoder_blocks()
{
for (size_t i = 0; i < num_layers; i++)
{
encoder_blocks->insert(TransformerEncoder(d_model, n_head, ff_size));
TransformerDecoder temp_decoder(d_model, n_head, ff_size);
decoder_blocks.push_back(std::move(temp_decoder));
}
}
value::Tensor TransformerImpl::calculate(const value::Tensor& src, const value::Tensor& tgt, const value::Tensor& tgt_mask)
{
value::Tensor src_e = src;
value::Tensor tgt_e = tgt;
src_e = this->encoder_blocks(src_e);
for (auto& it : this->decoder_blocks)
tgt_e = it(tgt_e, src_e, tgt_mask);
return SoftMax(this->fc(tgt_e), 0);
}
}
}