Skip to content
This repository was archived by the owner on Jul 1, 2023. It is now read-only.
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions Sources/TensorFlow/Core/Runtime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import Darwin
import Glibc
#endif
import CTensorFlow
import Foundation

/// TraceContext contains the state needed to build a trace graph function (TF_Function). As eager
/// ops are executed in tracing mode, their corresponding nodes are added to the trace graph (via
Expand Down Expand Up @@ -569,6 +570,15 @@ public final class _ExecutionContext {
// Initialize the TF runtime exactly once. Only affects local execution
// (when _RuntimeConfig.tensorFlowServer is set to "").
if !_RuntimeConfig.tensorFlowRuntimeInitialized {
// Install a signal handler to ensure we exit when interrupted.
signal(SIGINT, SIG_IGN)
let sigintSrc = DispatchSource.makeSignalSource(signal: SIGINT, queue: .main)
sigintSrc.setEventHandler {
print("Caught interrupt signal, exiting...")
exit(1)
}
sigintSrc.resume()

var args = ["dummyProgramName"]
if _RuntimeConfig.printsDebugLog {
args.append("--alsologtostderr")
Expand All @@ -588,24 +598,19 @@ public final class _ExecutionContext {

// Calculate the addresses of all the strings within our single buffer, and then call
// TF_InitMain.
flattenedStringBytes.withUnsafeMutableBufferPointer { flattenedStringBytesBuffer in
flattenedStringBytes.withUnsafeMutableBufferPointer { buffer in
var stringAddrs: [UnsafeMutablePointer<Int8>?] = []
var currentStringAddr = flattenedStringBytesBuffer.baseAddress
var currentStringAddr = buffer.baseAddress
.map(UnsafeMutablePointer.init)
for length in lengths {
stringAddrs.append(currentStringAddr)
currentStringAddr = currentStringAddr?.advanced(by: length)
}

stringAddrs.withUnsafeMutableBufferPointer { stringAddrsBuffer in
var cArgs = [stringAddrsBuffer.baseAddress.map(UnsafeMutablePointer.init)]
var cArgsCount = [Int32(args.count)]

cArgs.withUnsafeMutableBufferPointer { cArgsBuffer in
cArgsCount.withUnsafeMutableBufferPointer { cArgsCountBuffer in
TF_InitMain(nil, cArgsCountBuffer.baseAddress, cArgsBuffer.baseAddress)
}
}
var cArgsCount = Int32(args.count)
var cArgs = stringAddrsBuffer.baseAddress.map(UnsafeMutablePointer.init)
TF_InitMain(nil, &cArgsCount, &cArgs)
}
}
_RuntimeConfig.tensorFlowRuntimeInitialized = true
Expand Down