-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Add an api to trace an function and run it with XLA compilation. #23868
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -218,7 +218,8 @@ private class TraceContext { | |
|
||
/// Execute the trace graph function, and return the list of output tensors | ||
/// from the trace execution. These output tensors are owned by the caller. | ||
func execute(traceeInputs: [_AnyTensorHandle]) -> [CTensorHandle] { | ||
func execute( | ||
traceeInputs: [_AnyTensorHandle], useXla: Bool = false) -> [CTensorHandle] { | ||
// We must be in the `notTracing` enum mode. | ||
internalConsistencyCheck(_RuntimeConfig.traceState.context == nil) | ||
internalConsistencyCheck(traceGraphFn != nil) | ||
|
@@ -236,6 +237,11 @@ private class TraceContext { | |
checkOk(status) | ||
} | ||
|
||
if useXla { | ||
debugLog("Enabling XLA compilation") | ||
TFE_OpSetAttrBool(op, "_XlaCompile", 1) | ||
} | ||
|
||
debugLog("Adding \(traceeInputs.count) tracee input tensors.") | ||
internalConsistencyCheck(symbolicInputs.count == traceeInputs.count | ||
+ Int(additionalInputTensorCount)) | ||
|
@@ -993,6 +999,47 @@ public func _tffunc<State : _TensorArrayProtocolEnhanced, | |
} | ||
} | ||
|
||
// Trace the given function to generate a TF graph and return a closure | ||
// that can be used to launch the graph. | ||
public func _graph<In : TensorGroup, Out : TensorGroup>( | ||
_ fn: (In) -> Out, useXla: Bool = false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
) -> (In) -> Out { | ||
let traceContext: TraceContext = withoutActuallyEscaping(fn) { escapableFn in | ||
let wrappedFn = { | ||
(inputs: [CTensorHandle]) -> [CTensorHandle] in | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this could be moved to the previous line without going over the column limit. |
||
let buffer = UnsafeMutablePointer<CTensorHandle>.allocate( | ||
capacity: Int(inputs.count)) | ||
var ptr = buffer | ||
for input in inputs { | ||
ptr.initialize(to: input) | ||
ptr = ptr.advanced(by: 1) | ||
} | ||
let symbolicIn = In(_owning: buffer) | ||
let symbolicOut = escapableFn(symbolicIn) | ||
return symbolicOut.cTensorHandles | ||
} | ||
let dtypes = In._typeList.map { $0._cDataType } | ||
return _trace(with: dtypes, in: wrappedFn) | ||
} | ||
// The result is a closure that captures and executes the trace graph | ||
// function in the trace context. | ||
return { (input: In) -> (Out) in | ||
debugLog("Running trace function over input \(input).") | ||
|
||
debugLog("Getting input state tensor handles.") | ||
let inputStateTensorHandles = input.cTensorHandles | ||
let inputTensors = inputStateTensorHandles.map { | ||
_TFCCreateTensorHandleFromC($0) | ||
} | ||
debugLog("Executing trace graph function.") | ||
let returnValues = traceContext.execute( | ||
traceeInputs: inputTensors, useXla: useXla) | ||
|
||
debugLog("Creating output model instance.") | ||
return Out(_copying: returnValues) | ||
} | ||
} | ||
|
||
/// Trace the given function and return the name of the corresponding | ||
/// `TF_Function: In -> Out` that was created. | ||
public func _tffunc<In : TensorGroup, Out : TensorGroup>( | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Xla
->XLA