/* Copyright (c) 2017 PaddlePaddle Authors. All Rights Reserve. 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 "paddle/fluid/operators/math/matrix_bit_code.h" #include namespace paddle { namespace operators { namespace math { template void MatrixBitCodeFunctor::Add(const framework::LoDTensor& vec, framework::LoDTensor* tmat) { size_t batch_size = tmat->dims()[0]; size_t width = tmat->dims()[1]; for (size_t i = 0; i < batch_size; ++i) { auto code = code_table->get_code(i); int code_length = code->get_length(); for (int j = 0; j < code_length; ++j) { size_t index = code->calc_index(j); tmat->data()[i * width + j] += vec.data()[index]; } } } template void MatrixBitCodeFunctor::AddGrad(const framework::LoDTensor& tmat, framework::LoDTensor* vec) { size_t batch_size = tmat.dims()[0]; size_t width = tmat.dims()[1]; for (size_t i = 0; i < batch_size; ++i) { auto code = code_table->get_code(i); int code_length = code->get_length(); for (int j = 0; j < code_length; ++j) { size_t index = code->calc_index(j); vec->data()[index] += tmat.data()[i * width + j]; } } } template void MatrixBitCodeFunctor::AddGrad(const framework::LoDTensor& tmat, framework::SelectedRows* vec) { size_t batch_size = tmat.dims()[0]; size_t width = tmat.dims()[1]; for (size_t i = 0; i < batch_size; ++i) { auto code = code_table->get_code(i); int code_length = code->get_length(); for (int j = 0; j < code_length; ++j) { size_t index = code->calc_index(j); int64_t row_index = vec->AutoGrownIndex(static_cast(index), false, true); vec->mutable_value()->data()[row_index] += tmat.data()[i * width + j]; } } } template void MatrixBitCodeFunctor::Sum(const framework::LoDTensor& tmat, framework::LoDTensor* sum, T scale_sum) { size_t num_samples = tmat.dims()[0]; size_t o_width = tmat.dims()[1]; for (size_t i = 0; i < num_samples; ++i) { T sm = static_cast(0.0); auto code = code_table->get_code(i); int code_length = code->get_length(); for (int j = 0; j < code_length; ++j) { if (code->calc_bit(j)) { // calc_bit starts from right most bit, while data in tmat[i] is in the // reverse order. sm += tmat.data()[i * o_width + j]; } } sum->data()[i] = scale_sum * sm; } } template void MatrixBitCodeFunctor::Mul(framework::LoDTensor* tmat, const framework::LoDTensor& weight, const framework::LoDTensor& input) { size_t num_samples = tmat->dims()[0]; size_t tmat_width = tmat->dims()[1]; size_t input_width = input.dims()[1]; size_t weight_width = weight.dims()[1]; auto tmat_value = tmat->data(); auto weight_value = weight.data(); auto input_value = input.data(); for (size_t i = 0; i < num_samples; ++i) { auto code = code_table->get_code(i); int code_length = code->get_length(); for (int j = 0; j < code_length; ++j) { size_t index = code->calc_index(j); T sum = static_cast(0.0); for (size_t k = 0; k < input_width; ++k) { sum += weight_value[weight_width * index + k] * input_value[input_width * i + k]; } tmat_value[i * tmat_width + j] += sum; } } } template void MatrixBitCodeFunctor::MulGradWeight(const framework::LoDTensor& tmat, framework::LoDTensor* weight, const framework::LoDTensor& input) { size_t num_samples = tmat.dims()[0]; size_t input_width = input.dims()[1]; size_t tmat_width = tmat.dims()[1]; size_t weight_width = weight->dims()[1]; auto tmat_value = tmat.data(); auto weight_value = weight->data(); auto input_value = input.data(); for (size_t i = 0; i < num_samples; ++i) { auto code = code_table->get_code(i); int code_length = code->get_length(); for (int j = 0; j < code_length; ++j) { size_t index = code->calc_index(j); for (size_t k = 0; k < input_width; ++k) { weight_value[weight_width * index + k] += tmat_value[i * tmat_width + j] * input_value[input_width * i + k]; } } } } template void MatrixBitCodeFunctor::MulGradWeight(const framework::LoDTensor& tmat, framework::SelectedRows* weight, const framework::LoDTensor& input) { size_t num_samples = tmat.dims()[0]; size_t input_width = input.dims()[1]; size_t tmat_width = tmat.dims()[1]; size_t weight_width = weight->value().dims()[1]; auto tmat_value = tmat.data(); auto weight_value = weight->mutable_value()->data(); auto input_value = input.data(); for (size_t i = 0; i < num_samples; ++i) { auto code = code_table->get_code(i); int code_length = code->get_length(); for (int j = 0; j < code_length; ++j) { size_t index = code->calc_index(j); for (size_t k = 0; k < input_width; ++k) { int64_t row_index = weight->AutoGrownIndex(static_cast(index), false, true); weight_value[row_index * weight_width + k] += tmat_value[i * tmat_width + j] * input_value[input_width * i + k]; } } } } template void MatrixBitCodeFunctor::MulGradError(const framework::LoDTensor& tmat, const framework::LoDTensor& weight, framework::LoDTensor* input) { size_t num_samples = tmat.dims()[0]; size_t tmat_width = tmat.dims()[1]; size_t input_width = input->dims()[1]; size_t weight_width = weight.dims()[1]; auto tmat_value = tmat.data(); auto weight_value = weight.data(); auto input_value = input->data(); for (size_t i = 0; i < num_samples; ++i) { auto code = code_table->get_code(i); int code_length = code->get_length(); for (int j = 0; j < code_length; ++j) { size_t index = code->calc_index(j); for (size_t k = 0; k < input_width; ++k) { input_value[input_width * i + k] += tmat_value[i * tmat_width + j] * weight_value[weight_width * index + k]; } } } } template void MatrixBitCodeFunctor::Sub(framework::LoDTensor* tmat) { size_t num_samples = tmat->dims()[0]; size_t o_width = tmat->dims()[1]; for (size_t i = 0; i < num_samples; ++i) { auto code = code_table->get_code(i); int code_length = code->get_length(); for (int j = 0; j < code_length; ++j) { if (code->calc_bit(j)) { tmat->data()[i * o_width + j] -= 1; } } } } template class MatrixBitCodeFunctor; template class MatrixBitCodeFunctor; } // namespace math } // namespace operators } // namespace paddle