Tensorflow框架重点
Tensorflow使用了自动化构建工具bazel、脚本语言调用c或cpp的包裹工具swig、使用EIGEN作为矩阵处理工具、Nvidia-cuBLAS GPU加速计算库、结构化数据存储格式protobuf
Swig | 1. Simplified Wrapper and Interface Generator (SWIG) ,基本思想就是向脚本语言接口公开 C/C++ 代码。SWIG 允许您向广泛的脚本语言公开 C/C++ 代码,包括 Ruby、Perl、Tcl 和 Python。 参考:1. 使用SWIG实现Python调用C/C++代码 | |
---|---|---|
Bazel | 1. bazel假定每个目录为[package]单元,目录里面包含了源文件和一个描述文件BUILD,描述文件中指定了如何将源文件转换成构建的输出。![]() ![]() ![]() ![]() ![]() ![]() |
|
EIGEN | 1. Eigen is a C++ template library for linear algebra: matrices, vectors, numerical solvers, and related algorithms. http://eigen.tuxfamily.org/ 支持整数、浮点数、复数,使用模板编程,可以为特殊的数据结构提供矩阵操作。比如在用ceres-solver进行做优化问题(比如bundle adjustment)的时候,有时候需要用模板编程写一个目标函数,ceres可以将模板自动替换为内部的一个可以自动求微分的特殊的double类型。而如果要在这个模板函数中进行矩阵计算,使用Eigen就会非常方便。支持逐元素、分块、和整体的矩阵操作。内含大量矩阵分解算法包括LU,LDLt,QR、SVD等等。支持使用Intel MKL加速部分功能支持多线程稀疏矩阵支持良好,到今年新出的Eigen3.2,已经自带了SparseLU、SparseQR、共轭梯度(ConjugateGradient solver)、bi conjugate gradient stabilized solver等解稀疏矩阵的功能。同时提供SPQR、UmfPack等外部稀疏矩阵库的接口。支持常用几何运算,包括旋转矩阵、四元数、矩阵变换、AngleAxis(欧拉角与Rodrigues变换)等等。更新活跃,用户众多(Google、WilliowGarage也在用),使用Eigen的比较著名的开源项目有ROS(机器人操作系统)、PCL(点云处理库)、Google Ceres(优化算法)。OpenCV自带到Eigen的接口。2. 常用的矩阵计算工具有blas, cublas(caffe)、atlas、openblas(mxnet)、eigen,还有lapack、mkl(intel)、Armadillo(matlab)![]() ![]() |
|
protobuf | 1. Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,或者说序列化。它很适合做数据存储或 RPC 数据交换格式。可用于通讯协议、数据存储等领域的语言无关、平台无关、可扩展的序列化结构数据格式。2. 使用 > 编写.proto文件,编译后生成 .pb.h / .pb.cc文件 > Writer: SerializeToOstream(), Reader: ParseFromIstream() > required:一个格式良好的消息一定要含有1个这种字段; > optional:消息格式中该字段可以有0个或1个值(不超过1个)。 > repeated:在一个格式良好的消息中,这种字段可以重复任意多次(包括0次)。相当于java中的List。3. ![]() ![]() |
|
StreamExecutor | > google stream executor team: work on parallel programming models for CPUs, GPUs and other platforms. > StreamExecutor is a unified wrapper around the CUDA and OpenCL host-side programming models (runtimes). >StreamExecutor and libomptarget are libraries that are both meant to solve the problem of providing runtime support for offloading computational work to an accelerator device. The libomptarget library is already hosted within the OpenMP LLVM subproject, and there is currently a proposal to create another LLVM subproject containing StreamExecutor. > StreamExecutor is currently used as the runtime for the vast majority of Google’s internal GPGPU applications, and a snapshot of it is included in the open-source TensorFlow project, where it serves as the GPGPU runtime.https://github.com/henline/streamexecutordochttps://github.com/henline/streamexecutordoc/blob/master/se_and_openmp.rst | |
TF C++ | 1. TF源码安装: following the instructions here2. example: tensorflow/cc/tutorials/example_trainer.cc3. 自定义的op Kernel: tutorial for adding a new op in C++.4. TF c++ 调试: debugging Tensorflow’s C++ code behind the SWIG interface > The simplest interface between Python and C++ is the pure-C API in tensor_c_api.h |
Tensorflow源码分析
Tensor的组成:
Tensor的组成可以分成3部分:DataType、TensorShape、TensorBuf. 即数据类型、张量的形状和存储数据的内存.
DataType:Tensor数据的类型,float、double、int等
TensorShape: Tensor的形状. 这里要表示维度需存储两种元素,第一种是维数、第二种是每个维度的大小. 例如:一个矩阵的大小为3×4,那么其维数为2,第一个维度大小是3,第二个是4.
TensorBuf:就是存储数据的内存地址,比如一个Tensor是3×4的矩阵,并且类型时float,那么其TensorBuf就float*类型,并且长度是12,在实现上是基于模板实现的.
Tensor的运算:
Tensor的运算主要由kernels完成,每个kernel都完成不同的运算。其中CPU版本的kernel大部分都使用Eigen库来实现,GPU版本的kernel少部分使用Eigen,大部分使用cuda编程实现。
Eigen unsupported模块提供了同样叫Tensor的类,主要就是能完成各种复杂的数学计算,并且是并行实现的(主要使用线程池或cuda),效率很高. 其API文档可参考 https://github.com/PX4/eigen/blob/master/unsupported/Eigen/CXX11/src/Tensor/README.md. TensorFlow使用Eigen进行计算时,首先要将其自己实现的Tensor类的对象转换成Eigen支持的Tensor对象. 可以再core/framework/tensor_types.h下面看到Eigen支持的各种数据类型,主要是TensorMap类. 并进行了重命名,其源码如下所示:
1 | int storage[128]; |