1. 程式人生 > >[圖解tensorflow源碼] 入門準備工作附常用的矩陣計算工具[轉]

[圖解tensorflow源碼] 入門準備工作附常用的矩陣計算工具[轉]

cto org system open 協議 ring 矩陣 orf per

[圖解tensorflow源碼] 入門準備工作

附常用的矩陣計算工具[轉]

Link: https://www.cnblogs.com/yao62995/p/5773142.html

?tensorflow使用了自動化構建工具bazel、腳本語言調用ccpp的包裹工具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,描述文件中指定了如何將源文件轉換成構建的輸出。

技術分享圖片技術分享圖片技術分享圖片

? ?


技術分享圖片技術分享圖片技術分享圖片技術分享圖片

1. name屬性來命名規則,deps屬性來描述規則之間的依賴關系
2. 使用冒號來分隔包名和規則名;如果某條規則所依賴的規則在其他目錄下,就用"//"開頭,如果在同一目錄下,可以忽略包名而用冒號開頭。
依賴關系:
技術分享圖片技術分享圖片技術分享圖片

??

3. bazel命令:

? ? ?>?bazel build -c opt?//tensorflow/tools/pip_package:build_pip_package

4. 調試模式:

? ? ?> ?bazel build -c dbg

參考:

1.?安裝bazel

? ?>?yum?install?java-1.8.0-openjdk

? ?>?yum?install?java-1.8.0-openjdk-devel

? ?>?https://github.com/google/bazel/ 下載最新版本bazel_xxx.sh安裝

2.?使用bazel構建系統

? ?

3.?bazel 命令手冊

??

? ?

??

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等解稀疏矩陣的功能。同時提供SPQRUmfPack等外部稀疏矩陣庫的接口。
  • 支持常用幾何運算,包括旋轉矩陣、四元數、矩陣變換、AngleAxis(歐拉角與Rodrigues變換)等等。
  • 更新活躍,用戶眾多(Google、WilliowGarage也在用),使用Eigen的比較著名的開源項目有ROS(機器人操作系統)、PCL(點雲處理庫)、Google?Ceres(優化算法)。OpenCV自帶到Eigen的接口。

2. 常用的矩陣計算工具有blas,?cublas(caffe)、atlas、openblas(mxnet)、eigen,還有lapack、mkl(intel)、Armadillo(matlab)

技術分享圖片

? ?

技術分享圖片技術分享圖片

3. Eigen庫包含 Eigen模塊和unsupported模塊,其中Eigen模塊為official module,unsupported模塊為開源貢獻者開發的,沒有official support。

1.?矩陣運算庫blas, cblas, openblas, atlas, lapack, mkl
2. eigen 參考文檔

https://eigen.tuxfamily.org/dox/

? ?

C++矩陣運算庫推薦

?

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.?

技術分享圖片

? ?

技術分享圖片技術分享圖片

4. grpc需要理解4個方面(service,stub,channel,observer)

  • service,
  • stub:客戶端調用 stub 對象,所謂 stub 對象就是具有聲明好的方法的 fake object。stub 對象將請求用 protobuf 方式序列化成字節流,用於線上傳輸,到 server 端後調用真正的實現對象處理。
  • channel,指定連接的服務器地址和端口,用於stub連接到service
  • observer,服務端,觀察處理返回和關閉通道

?Protobuf 語法指南

gRPC 入門及源碼分析

??

Stream

Executor?

> 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/streamexecutordoc

https://github.com/henline/streamexecutordoc/blob/master/se_and_openmp.rst

? ?

??

TF C++

1. TF源碼安裝:?following the instructions here

2. example: ?tensorflow/cc/tutorials/example_trainer.cc

3. 自定義的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源碼] 入門準備工作附常用的矩陣計算工具[轉]