1 - Tensorflow源碼目錄結構
基於2018年5月28日github的tensorflow源碼,即1.8版本
第一層:
tensorflow: 核心代碼目錄。
third_party:第三方庫,包括:eigen3,fft2d,hadoop,mkl,probuf ,kafka,mpi,tensorRT,nccl,grpc等等。
tools:只有兩個文件 bazel.rc 和 tf_env_collect.sh。
util/python:存放用到的 python 工具,不過當前版本其中只有一個BUILD文件。
tensorflow:
c:
cc:
compiler:
contrib:存放有其他項目貢獻者添加的相關貢獻代碼,非核心官方代碼
core:這是 tensorflow 的核心代碼模塊.
docs_str:以md呈現的幫助文檔
examples:以jupyter notebook呈現的例子文檔
g3doc:為空,都遷移到docs_str
go,java,python:主要的第三方 API,其中python是最全的接口
stream_executor:tensorflow 流圖的並行計算執行,核心代碼,主要是關於cuda的封裝。
tools:
user_ops:
tensorflow/core:Core 目錄是代碼最核心的部分,包含 框架、圖、會話、runtime 最核心的部分
api_def:
common_runtime:公共運行庫,包含 會話(session)、線程(thread),內存管理(memory), 設備調度(device)等基本運行庫。
debug:
distributed_runtime:分布式運行庫,與上面類似,作為分布式情況下的運行庫,提供運行支撐。
example:
framework:框架基礎模塊定義,主要是通用組件的結構格式定義;
graph:計算流圖相關基礎操作(類結構),包括 拆分、合並、執行 等操作,被外面的 executor 調用;
kernels:核心操作定義,像常用的運算 matmul,sigmoid 等操作;
lib:基礎庫用於內部調用,包括 hash、io、jpeg、math 等;
ops:對 kernel 下的op進行注冊和對外聲明;
platform: 針對不同平台的依賴
profiler:
protobuf:Google 的傳輸交換模塊,用於傳輸時的數據序列化;
public:
user_ops:
util:
2 - Tensorflow運行目錄結構
上述是基於tensorflow的源碼進行目錄結構展示,而我們通常使用的時候,是基於編譯后的,即所謂運行的目錄。基於tensorflow_gpu 1.7,采用命令
tree -L 1 -F -C tensorflow
對centos系統中"Anaconda3/lib/python3.6/site-packages"下面的tensorflow進行目錄展示
圖2.1 tensorflow一級目錄結構
其中,contrib較為特殊,其中涉及的部分較多
圖2.2 tensorflow/contrib一級目錄結構
圖2.3 tensorflow中其他子目錄的一級目錄結構
當我們運行
import tensorflow as tf
會自動執行"tensorflow/_init_.py"該文件,而該文件中有如圖所示的一行代碼,會自動將tensorflow/python下的py文件都導入進來
然而tensorflow/python同時會自動運行tensorflow/python/_init_.py
從而在頂層空間中會有keras等變量,可以直接通過tf.keras進行訪問,而其對應的就是tensorflow/python/keras文件夾。當然該初始化文件中還有部分是導入tensorflow/core的
所以,我們通常直接訪問tf.xxx都是在訪問tensorflow/python和tensorflow/core等下面的部分;當然因為tensorflow/_init_.py下面有如
del python
del core
等代碼,從而,我們在不顯式導入的基礎上,只是訪問
tensorflow/python/{keras,layers,...}
tensorflow/contrib
tensorflow/tools
這三個部分的文件夾路徑
參考文獻:
- .【深度長文5篇系列】Tensorflow代碼解析,知乎
- .【源碼目錄淺析】淺入淺出TensorFlow 9 - 代碼框架解析
- .【利炳根_TensorFlow】學習筆記TF050:TensorFlow源代碼解析](https://www.jianshu.com/p/b859b1e6ff5c);tensorflow系列
- .【tensorflow教程】如何高效的學習 TensorFlow 代碼?
- .【圖解tensorflow源碼】tensorflow源碼分析
- .TensorFlow 源碼分析之內存管理BFC算法
- .Tensorflow-slim 學習筆記
- .【tensorflow框架結構】TensorFlow 技術框架解析 | 圖文理解深度學習技術實現
- .【4篇源碼解析】tensorflow源碼分析
- .【distributed Tensorflow’s workflow】Tensorflow architecture analysis
- .【article】look-inside-tensorflow-googles-open-source-deep-learning-framework
- .【video】Tensorflow源碼級技術分享
- .【】Tensorflow C++ 編譯和調用圖模型
- .【】如何閱讀TensorFlow源碼
- .【】TensorFlow架構與設計:會話生命周期