50 lines
1.9 KiB
C++
50 lines
1.9 KiB
C++
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
|
|
//
|
|
// 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.
|
|
|
|
#pragma once
|
|
|
|
#include <vector>
|
|
|
|
namespace phi {
|
|
|
|
template <typename TensorType, typename T>
|
|
void ResetParameterVector(const std::vector<TensorType>& raw_params_vec,
|
|
const int& num_layers,
|
|
const bool& is_bidirec,
|
|
std::vector<std::vector<T*>>* params_vec) {
|
|
// the parameter raw sequence is [FWhi, FWhh, BWhi, BWhh] * num_layers
|
|
// + [FBhi, FBhh, BBhi, BBhh] * num_layers, we will reset the parameter to
|
|
// ([FWhi, FWhh, FBhi, FBhh] + [BWhi, BWhh, BBhi, BBhh]) * num_layers
|
|
const int& direction_num = is_bidirec ? 2 : 1;
|
|
const int& layer_weight_size = 4 * direction_num;
|
|
const int& all_weight_size = num_layers * layer_weight_size;
|
|
const int& bias_start_idx = all_weight_size / 2;
|
|
for (int i = 0; i < num_layers; i++) {
|
|
params_vec->at(i).resize(layer_weight_size);
|
|
for (int j = 0; j < layer_weight_size; j++) {
|
|
int k = j % 4;
|
|
const int& section = j / 4;
|
|
int tensor_idx = i * 2 * direction_num + section * 2 + k % 2;
|
|
if (k >= 2) {
|
|
tensor_idx += bias_start_idx;
|
|
}
|
|
using remove_cv_t = typename std::remove_cv<T>::type;
|
|
params_vec->at(i)[j] =
|
|
raw_params_vec[tensor_idx]->template data<remove_cv_t>();
|
|
}
|
|
}
|
|
}
|
|
|
|
} // namespace phi
|