// Copyright 2018 Google Inc. 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. // ============================================================================= // Compatibility support for Eigen. #ifndef DRAGNN_RUNTIME_MATH_EIGEN_H_ #define DRAGNN_RUNTIME_MATH_EIGEN_H_ #include "dragnn/runtime/alignment.h" #include "dragnn/runtime/math/types.h" #include "third_party/eigen3/Eigen/Core" namespace syntaxnet { namespace dragnn { namespace runtime { namespace internal { // Returns a combination of bit-options for Eigen matrices. constexpr int GetEigenMatrixOptions() { return Eigen::AutoAlign | Eigen::RowMajor; } // Returns a combination of bit-options for Eigen maps of runtime types. constexpr int GetEigenMapOptions() { static_assert(kAlignmentBytes >= EIGEN_MAX_ALIGN_BYTES, "Runtime alignment is not compatible with Eigen alignment."); return Eigen::Aligned; } // Eigen matrix and (row) vector types. Don't use these directly; instead use // the public Map types and functions below to wrap runtime types. template using EigenVector = Eigen::Matrix; template using EigenMatrix = Eigen::Matrix; // Eigen stride for matrix types. using EigenMatrixStride = Eigen::Stride; // Returns the Eigen stride associated with the |matrix|. template EigenMatrixStride GetEigenMatrixStride(MatrixImpl matrix) { return EigenMatrixStride(matrix.row_stride(), 1); } } // namespace internal // Eigen wrappers around a runtime-allocated matrix or (row) vector. template using EigenVectorMap = Eigen::Map, internal::GetEigenMapOptions()>; template using MutableEigenVectorMap = Eigen::Map, internal::GetEigenMapOptions()>; template using EigenMatrixMap = Eigen::Map, internal::GetEigenMapOptions(), internal::EigenMatrixStride>; template using MutableEigenMatrixMap = Eigen::Map, internal::GetEigenMapOptions(), internal::EigenMatrixStride>; // Returns an Eigen wrapper around the |vector| or |matrix|. template EigenVectorMap AsEigenMap(Vector vector) { return EigenVectorMap(vector.data(), vector.size()); } template MutableEigenVectorMap AsEigenMap(MutableVector vector) { return MutableEigenVectorMap(vector.data(), vector.size()); } template EigenMatrixMap AsEigenMap(Matrix matrix) { return EigenMatrixMap(matrix.data(), matrix.num_rows(), matrix.num_columns(), internal::GetEigenMatrixStride(matrix)); } template MutableEigenMatrixMap AsEigenMap(MutableMatrix matrix) { return MutableEigenMatrixMap(matrix.data(), matrix.num_rows(), matrix.num_columns(), internal::GetEigenMatrixStride(matrix)); } } // namespace runtime } // namespace dragnn } // namespace syntaxnet #endif // DRAGNN_RUNTIME_MATH_EIGEN_H_