【TensorFlow源碼系列】【一】Session的建立

【一】代碼下載git

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

PS:本次源碼分析採用1.11版本設計模式

【二】Session簡介api

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

【三】session類圖分佈式

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

DirectSession  ::tensorflow\core\common_runtime\direct_session.h源碼分析

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

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

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也很容易,且不影響上層代碼。

相關文章
相關標籤/搜索