【TensorFlow源碼系列】【一】Session的創建 原

【一】代碼下載

https://github.com/tensorflow/tensorflow/releases/

PS:本次源碼分析採用1.11版本

【二】Session簡介

在TensorFlow中,session是溝通tf的橋樑,模型的訓練、推理,都需要通過session,session持有graph的引用。tf支持單機與分佈式,因此session也分爲單機版與分佈式版。

【三】session類圖

Session  ::tensorflow\core\public\session.h

DirectSession  ::tensorflow\core\common_runtime\direct_session.h

GrpcSession  ::tensorflow\core\distributed_runtime\rpc\grpc_session.h

class DirectSession : public Session    --->單機版session

class GrpcSession : public Session      ---> 分佈式版session

【四】session創建

tf對外提供了一套C API,負責給client語言使用。在tensorflow\c\c_api.h中,session的創建流程我們從這裏開始。

TF_NewSession

    NewSession (tensorflow\core\public\session.h)

1. NewSession實現 tensorflow\core\common_runtime\session.cc

Status NewSession(const SessionOptions& options, Session** out_session) {
  SessionFactory* factory;
  Status s = SessionFactory::GetFactory(options, &factory);   // 通過SessionOptions來選擇不同的factory
  s = factory->NewSession(options, out_session);   // 調用對應的factory來創建對應的session,也就是創建DirectSession 還GrpcSession
}

2. 關於SessionOptions

這裏看一下該定義,省略掉其他成員,僅僅看看target,註釋中說,如果target爲空,則創建DirectSession,如果target以 'grpc’開頭,則創建GrpcSession

/// Configuration information for a Session.
struct SessionOptions {

  /// \brief The TensorFlow runtime to connect to.
  ///
  /// If 'target' is empty or unspecified, the local TensorFlow runtime
  /// implementation will be used.  Otherwise, the TensorFlow engine
  /// defined by 'target' will be used to perform all computations.
  ///
  /// "target" can be either a single entry or a comma separated list
  /// of entries. Each entry is a resolvable address of the
  /// following format:
  ///   local
  ///   ip:port
  ///   host:port
  ///   ... other system-specific formats to identify tasks and jobs ...
  ///
  /// NOTE: at the moment 'local' maps to an in-process service-based
  /// runtime.
  ///
  /// Upon creation, a single session affines itself to one of the
  /// remote processes, with possible load balancing choices when the
  /// "target" resolves to a list of possible processes.
  ///
  /// If the session disconnects from the remote process during its
  /// lifetime, session calls may fail immediately.
  string target;
};

3. SessionFactory::GetFactory

這裏的關鍵在於:session_factory.second->AcceptsOptions(options)

依據選項來,這裏實際上是調用對應的factory方法

DirectSessionFactory

bool AcceptsOptions(const SessionOptions& options) override {
    return options.target.empty();  // target爲空則爲true
  }

GrpcSessionFactory

bool AcceptsOptions(const SessionOptions& options) override {
    return str_util::StartsWith(options.target, kSchemePrefix); // const char* const kSchemePrefix = "grpc://"; target爲grpc開頭
  }

 

class GrpcSessionFactory : public SessionFactory

class DirectSessionFactory : public SessionFactory

【五】總結

session的創建通過option(可以理解爲配置)來選擇性創建,而session的真正創建由sessionfactory來完成,這裏採用了工廠設計模式,將各個factory通過註冊的方式註冊到sessionfactory中,這樣在系統啓動後,這些factory就可以用了,新增一個factory也很容易,且不影響上層代碼。

 

 

    

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章