File size: 12,068 Bytes
3fd1a35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include "ling3/layer0.h"

#include "ling3/cpu_kernels.h"

#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <stdexcept>
#include <string>
#include <vector>

namespace ling3 {
namespace {

using Clock = std::chrono::steady_clock;
constexpr int kHidden = 1536;
constexpr int kHeads = 16;
constexpr int kHeadDimension = 128;
constexpr int kAttention = kHeads * kHeadDimension;
constexpr int kDense = 4608;
constexpr float kEpsilon = 1.0e-6F;

double Milliseconds(Clock::time_point begin, Clock::time_point end) {
    return std::chrono::duration<double, std::milli>(end - begin).count();
}

template <typename T>
std::span<const T> Typed(const TensorView & tensor, DataType type) {
    if (tensor.entry->dtype != static_cast<std::uint32_t>(type) ||
        tensor.entry->data_bytes % sizeof(T) != 0) {
        throw std::runtime_error(std::string(tensor.name) + " has an incompatible dtype");
    }
    return {
        reinterpret_cast<const T *>(tensor.data),
        static_cast<std::size_t>(tensor.entry->data_bytes / sizeof(T)),
    };
}

std::span<const std::byte> Blob(const TensorView & tensor, TensorRole role) {
    if (tensor.entry->role != static_cast<std::uint32_t>(role)) {
        throw std::runtime_error(std::string(tensor.name) + " has an incompatible role");
    }
    return {tensor.data, static_cast<std::size_t>(tensor.entry->data_bytes)};
}

std::vector<float> DecodeBf16(const TensorView & tensor, std::size_t expected) {
    const auto input = Typed<std::uint16_t>(tensor, DataType::kBFloat16);
    if (input.size() != expected) {
        throw std::runtime_error(std::string(tensor.name) + " has an incompatible shape");
    }
    std::vector<float> output(expected);
    for (std::size_t index = 0; index < expected; ++index) {
        output[index] = BFloat16ToFloat(input[index]);
    }
    return output;
}

std::unique_ptr<DynamicW4Linear> MakeLinear(
    const ModelPackage & package,
    const std::string & base) {
    const auto & weight = package.tensor(base + ".weight");
    const auto & scales = package.tensor(base + ".scales");
    const auto & correction = package.tensor(base + ".correction");
    if (weight.entry->rank != 2 ||
        weight.entry->dtype != static_cast<std::uint32_t>(DataType::kInt4Low) ||
        weight.entry->layout != static_cast<std::uint32_t>(TensorLayout::kPackedInt4Low)) {
        throw std::runtime_error(base + " is not a packed W4 linear");
    }
    const int k = static_cast<int>(weight.entry->dims[0]);
    const int n = static_cast<int>(weight.entry->dims[1]);
    return std::make_unique<DynamicW4Linear>(
        W4LinearConfig {k, n, static_cast<int>(weight.entry->flags), {0, 1, 2}},
        Typed<std::byte>(weight, DataType::kInt4Low),
        Typed<float>(scales, DataType::kFloat32),
        Typed<std::int32_t>(correction, DataType::kInt32));
}

void NormalizeHeads(std::span<float> values) {
    for (int head = 0; head < kHeads; ++head) {
        float sum = kEpsilon;
        const int begin = head * kHeadDimension;
        for (int index = 0; index < kHeadDimension; ++index) {
            const float value = values[begin + index];
            sum += value * value;
        }
        const float inverse = 1.0F / std::sqrt(sum);
        for (int index = 0; index < kHeadDimension; ++index) {
            values[begin + index] *= inverse;
        }
    }
}

void CausalConvSilu(
    std::span<const float> input,
    std::span<const float> weight,
    std::span<float> state,
    std::span<float> output) {
    if (input.size() != kAttention || output.size() != kAttention ||
        weight.size() != static_cast<std::size_t>(kAttention * 4) ||
        state.size() != static_cast<std::size_t>(kAttention * 3)) {
        throw std::invalid_argument("causal convolution tensor sizes are incompatible");
    }
    for (int channel = 0; channel < kAttention; ++channel) {
        auto * history = state.data() + static_cast<std::size_t>(channel) * 3;
        const auto * kernel = weight.data() + static_cast<std::size_t>(channel) * 4;
        const float value = history[0] * kernel[0] + history[1] * kernel[1] +
                            history[2] * kernel[2] + input[channel] * kernel[3];
        history[0] = history[1];
        history[1] = history[2];
        history[2] = input[channel];
        output[channel] = value / (1.0F + std::exp(-value));
    }
}

} // namespace

struct Layer0::Impl {
    const ModelPackage & package;
    std::span<const std::uint16_t> embeddings;
    std::vector<float> input_norm_weight;
    std::vector<float> post_norm_weight;
    std::vector<float> q_conv_weight;
    std::vector<float> k_conv_weight;
    std::vector<float> v_conv_weight;
    std::vector<float> output_norm_weight;
    std::span<const float> a_log;
    std::span<const float> dt_bias;
    std::unique_ptr<DynamicW4Linear> qkvfgb;
    std::unique_ptr<DynamicW4Linear> attention_output;
    std::unique_ptr<DynamicW4Linear> gate_up;
    std::unique_ptr<DynamicW4Linear> down;
    GdnStep gdn;
    std::array<std::vector<float>, 3> convolution_state;
    std::vector<float> hidden;
    std::vector<float> normalized;
    std::vector<float> projected;
    std::vector<float> q;
    std::vector<float> k;
    std::vector<float> v;
    std::vector<float> recurrence_output;
    std::vector<float> attention_vector;
    std::vector<float> attention_result;
    std::vector<float> ffn_projected;
    std::vector<float> ffn_hidden;
    std::vector<float> ffn_result;

    explicit Impl(const ModelPackage & model)
        : package(model),
          embeddings(Typed<std::uint16_t>(
              package.tensor("model.word_embeddings.weight"), DataType::kBFloat16)),
          input_norm_weight(DecodeBf16(
              package.tensor("model.layers.0.input_layernorm.weight"), kHidden)),
          post_norm_weight(DecodeBf16(
              package.tensor("model.layers.0.post_attention_layernorm.weight"), kHidden)),
          q_conv_weight(DecodeBf16(
              package.tensor("model.layers.0.attention.q_conv1d.weight"), kAttention * 4)),
          k_conv_weight(DecodeBf16(
              package.tensor("model.layers.0.attention.k_conv1d.weight"), kAttention * 4)),
          v_conv_weight(DecodeBf16(
              package.tensor("model.layers.0.attention.v_conv1d.weight"), kAttention * 4)),
          output_norm_weight(DecodeBf16(
              package.tensor("model.layers.0.attention.o_norm.weight"), kHeadDimension)),
          a_log(Typed<float>(package.tensor("model.layers.0.attention.A_log"), DataType::kFloat32)),
          dt_bias(Typed<float>(package.tensor("model.layers.0.attention.dt_bias"), DataType::kFloat32)),
          qkvfgb(MakeLinear(package, "model.layers.0.attention.qkvfgb")),
          attention_output(MakeLinear(package, "model.layers.0.attention.o_proj")),
          gate_up(MakeLinear(package, "model.layers.0.mlp.gate_up")),
          down(MakeLinear(package, "model.layers.0.mlp.down_proj")),
          gdn(
              Blob(package.tensor("rknn.gdn.heads6"), TensorRole::kRknnIsland),
              Blob(package.tensor("rknn.gdn.heads5"), TensorRole::kRknnIsland)),
          hidden(kHidden),
          normalized(kHidden),
          projected(10304),
          q(kAttention),
          k(kAttention),
          v(kAttention),
          recurrence_output(kAttention),
          attention_vector(kAttention),
          attention_result(kHidden),
          ffn_projected(2 * kDense),
          ffn_hidden(kDense),
          ffn_result(kHidden) {
        if (embeddings.size() != static_cast<std::size_t>(157184 * kHidden) ||
            a_log.size() != kHeads || dt_bias.size() != kAttention) {
            throw std::runtime_error("layer 0 raw tensor shapes are incompatible");
        }
        for (auto & state : convolution_state) state.assign(kAttention * 3, 0.0F);
    }

    void Reset() {
        for (auto & state : convolution_state) std::fill(state.begin(), state.end(), 0.0F);
        gdn.Reset();
    }

    Layer0Timings DecodeToken(std::uint32_t token, std::span<float> output) {
        if (token >= 157184 || output.size() != kHidden) {
            throw std::invalid_argument("layer 0 token or output is out of range");
        }
        const auto begin = Clock::now();
        const auto embedding = embeddings.subspan(static_cast<std::size_t>(token) * kHidden, kHidden);
        for (int index = 0; index < kHidden; ++index) hidden[index] = BFloat16ToFloat(embedding[index]);
        RmsNorm(hidden.data(), input_norm_weight.data(), normalized.data(), kHidden, kEpsilon);
        qkvfgb->Run(normalized, projected);

        CausalConvSilu(
            std::span<const float>(projected).subspan(0, kAttention),
            q_conv_weight,
            convolution_state[0],
            q);
        CausalConvSilu(
            std::span<const float>(projected).subspan(kAttention, kAttention),
            k_conv_weight,
            convolution_state[1],
            k);
        CausalConvSilu(
            std::span<const float>(projected).subspan(2 * kAttention, kAttention),
            v_conv_weight,
            convolution_state[2],
            v);
        NormalizeHeads(q);
        NormalizeHeads(k);

        const auto f = std::span<const float>(projected).subspan(3 * kAttention, kAttention);
        const auto gate = std::span<const float>(projected).subspan(4 * kAttention, kAttention);
        const auto beta_logits = std::span<const float>(projected).subspan(5 * kAttention, kHeads);
        std::vector<float> decay(kAttention);
        std::vector<float> beta(kHeads);
        for (int head = 0; head < kHeads; ++head) {
            const float a = std::exp(a_log[head]);
            beta[head] = 1.0F / (1.0F + std::exp(-beta_logits[head]));
            for (int index = 0; index < kHeadDimension; ++index) {
                const int offset = head * kHeadDimension + index;
                const float argument = a * (f[offset] + dt_bias[offset]);
                decay[offset] = -5.0F / (1.0F + std::exp(-argument));
            }
        }
        gdn.Run(q, k, v, decay, beta, recurrence_output);

        for (int head = 0; head < kHeads; ++head) {
            const int base = head * kHeadDimension;
            float sum = 0.0F;
            for (int index = 0; index < kHeadDimension; ++index) {
                const float value = recurrence_output[base + index];
                sum += value * value;
            }
            const float inverse = 1.0F /
                std::sqrt(sum / static_cast<float>(kHeadDimension) + kEpsilon);
            for (int index = 0; index < kHeadDimension; ++index) {
                const int offset = base + index;
                const float sigmoid_gate = 1.0F / (1.0F + std::exp(-gate[offset]));
                attention_vector[offset] = recurrence_output[offset] * inverse *
                    output_norm_weight[index] * sigmoid_gate;
            }
        }
        attention_output->Run(attention_vector, attention_result);
        for (int index = 0; index < kHidden; ++index) hidden[index] += attention_result[index];
        const auto attention_end = Clock::now();

        RmsNorm(hidden.data(), post_norm_weight.data(), normalized.data(), kHidden, kEpsilon);
        gate_up->Run(normalized, ffn_projected);
        SiluMultiply(ffn_projected.data(), ffn_projected.data() + kDense, ffn_hidden.data(), kDense);
        down->Run(ffn_hidden, ffn_result);
        for (int index = 0; index < kHidden; ++index) output[index] = hidden[index] + ffn_result[index];
        const auto end = Clock::now();
        return {
            Milliseconds(begin, attention_end),
            Milliseconds(attention_end, end),
            Milliseconds(begin, end),
        };
    }
};

Layer0::Layer0(const ModelPackage & package) : impl_(std::make_unique<Impl>(package)) {}
Layer0::~Layer0() = default;
void Layer0::Reset() { impl_->Reset(); }
Layer0Timings Layer0::DecodeToken(std::uint32_t token, std::span<float> output) {
    return impl_->DecodeToken(token, output);
}

} // namespace ling3