-
Notifications
You must be signed in to change notification settings - Fork 10.7k
Internal wrapper for direct eager dispatching. #23777
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 |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| //===-- ArrayOps.swift ----------------------------------------*- swift -*-===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors | ||
| // Licensed under Apache License v2.0 with Runtime Library Exception | ||
| // | ||
| // See https://swift.org/LICENSE.txt for license information | ||
| // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This file contains some Array ops that cannot be properly handled by #tfop. | ||
| // | ||
| // TODO: These should be deleted once we can properly generate raw ops for these. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| import CTensorFlow | ||
|
|
||
| public extension Raw { | ||
| /// Saves tensors in V2 checkpoint format. | ||
| /// | ||
| /// By default, saves the named tensors in full. If the caller wishes to save | ||
| /// specific slices of full tensors, "shape_and_slices" should be non-empty strings | ||
| /// and correspondingly well-formed. | ||
| /// | ||
| /// - Parameters: | ||
| /// - prefix: Must have a single element. The prefix of the V2 checkpoint to which we | ||
| /// write the tensors. | ||
| /// - tensor_names: shape {N}. The names of the tensors to be saved. | ||
| /// - shape_and_slices: shape {N}. The slice specs of the tensors to be saved. | ||
| /// Empty strings indicate that they are non-partitioned tensors. | ||
| /// - tensors: `N` tensors to save. | ||
| @inlinable @inline(__always) | ||
| static func saveV2( | ||
| prefix: StringTensor, | ||
| tensorNames: StringTensor, | ||
| shapeAndSlices: StringTensor, | ||
| tensors: [AnyTensor] | ||
| ) { | ||
| let s: CTFStatus = TF_NewStatus() | ||
| defer { TF_DeleteStatus(s) } | ||
| let op: CTFEOp = TFE_NewOp(_ExecutionContext.global.eagerContext, "SaveV2", s) | ||
| defer { TFE_DeleteOp(op) } | ||
| let _ = _TFCOpAddInputFromTensorGroup(op, prefix, s) | ||
| let _ = _TFCOpAddInputFromTensorGroup(op, tensorNames, s) | ||
| let _ = _TFCOpAddInputFromTensorGroup(op, shapeAndSlices, s) | ||
| let _ = _TFCOpAddInputFromAnyTensors(op, tensors, s) | ||
| let _ = _TFCOpSetAttrTypeArray(op, "dtypes", tensors.map { $0._tensorFlowDataType }) | ||
| return _TFCExecuteOp(op, s) | ||
| } | ||
|
|
||
| /// Restores tensors from a V2 checkpoint. | ||
| /// | ||
| /// For backward compatibility with the V1 format, this Op currently allows | ||
| /// restoring from a V1 checkpoint as well: | ||
| /// - This Op first attempts to find the V2 index file pointed to by "prefix", and | ||
| /// if found proceed to read it as a V2 checkpoint; | ||
| /// - Otherwise the V1 read path is invoked. | ||
| /// Relying on this behavior is not recommended, as the ability to fall back to read | ||
| /// V1 might be deprecated and eventually removed. | ||
| /// | ||
| /// By default, restores the named tensors in full. If the caller wishes to restore | ||
| /// specific slices of stored tensors, "shape_and_slices" should be non-empty | ||
| /// strings and correspondingly well-formed. | ||
| /// | ||
| /// Callers must ensure all the named tensors are indeed stored in the checkpoint. | ||
| /// | ||
| /// - Parameters: | ||
| /// - prefix: Must have a single element. The prefix of a V2 checkpoint. | ||
| /// - tensor_names: shape {N}. The names of the tensors to be restored. | ||
| /// - shape_and_slices: shape {N}. The slice specs of the tensors to be restored. | ||
| /// Empty strings indicate that they are non-partitioned tensors. | ||
| /// | ||
| /// - Attr dtypes: shape {N}. The list of expected dtype for the tensors. Must match | ||
| /// those stored in the checkpoint. | ||
| /// | ||
| /// - Output tensors: shape {N}. The restored tensors, whose shapes are read from the | ||
| /// checkpoint directly. | ||
| @inlinable @inline(__always) | ||
| static func restoreV2( | ||
| prefix: StringTensor, | ||
| tensorNames: StringTensor, | ||
| shapeAndSlices: StringTensor, | ||
| dtypes: [TensorDataType] | ||
| ) -> [AnyTensor] { | ||
| let s: CTFStatus = TF_NewStatus() | ||
| defer { TF_DeleteStatus(s) } | ||
| let op: CTFEOp = TFE_NewOp(_ExecutionContext.global.eagerContext, "RestoreV2", s) | ||
| defer { TFE_DeleteOp(op) } | ||
| let _ = _TFCOpAddInputFromTensorGroup(op, prefix, s) | ||
| let _ = _TFCOpAddInputFromTensorGroup(op, tensorNames, s) | ||
| let _ = _TFCOpAddInputFromTensorGroup(op, shapeAndSlices, s) | ||
| let _ = _TFCOpSetAttrTypeArray(op, "dtypes", dtypes) | ||
|
|
||
| var count: Int32 = Int32(dtypes.count) | ||
| let buffer: UnsafeMutablePointer<CTensorHandle> = | ||
| UnsafeMutablePointer.allocate(capacity: Int(count)) | ||
| defer { buffer.deallocate() } | ||
| _TFCEagerExecute(op, UnsafeMutablePointer<CTensorHandle?>(buffer), &count, s) | ||
| checkOk(s) | ||
|
|
||
| var out: [AnyTensor] = [] | ||
| var cursor = buffer | ||
| for type in dtypes { | ||
| out.append(makeTensor(dataType: type, owning: cursor.pointee)) | ||
| cursor = cursor.advanced(by: 1) | ||
| } | ||
| return out | ||
| } | ||
| } | ||
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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| //===-- ExecuteOp.swift.gyb -----------------------------------*- swift -*-===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors | ||
| // Licensed under Apache License v2.0 with Runtime Library Exception | ||
| // | ||
| // See https://swift.org/LICENSE.txt for license information | ||
| // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This file contains _TFCExecuteOp which allows dispatching an op and | ||
| // returning an arbitrary set of tensor-groups. | ||
| // | ||
| // TODO: A nice wrapper for TFEOp could possibly make this simpler to use. This | ||
| // may need to be extended in order to work with multiple tfops. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| @usableFromInline | ||
| func _TFCExecuteOp(_ op: CTFEOp, _ s: CTFStatus) { | ||
| var count: Int32 = 0 | ||
| var unused: CTensorHandle? | ||
| _TFCEagerExecute(op, &unused, &count, s) | ||
| checkOk(s) | ||
| } | ||
|
|
||
| %for n in range(1, 11): | ||
| // Calls _TFCEagerExecute under the hood and unpacks into TensorGroup conforming | ||
| // types. | ||
| @usableFromInline | ||
| func _TFCExecuteOp<${", ".join(["T" + str(i) + " : TensorGroup" for i in range(n)])}> | ||
pschuh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| (_ op: CTFEOp, _ s: CTFStatus) | ||
| -> (${", ".join(["T" + str(i) for i in range(n)])}) { | ||
|
|
||
| var count: Int32 = ${" + ".join(["T" + str(i) + "._tensorHandleCount" for i in range(n)])} | ||
| let buffer: UnsafeMutablePointer<CTensorHandle> = | ||
| UnsafeMutablePointer.allocate(capacity: Int(count)) | ||
| defer { buffer.deallocate() } | ||
| _TFCEagerExecute(op, UnsafeMutablePointer<CTensorHandle?>(buffer), &count, s) | ||
| checkOk(s) | ||
| %for i in range(n): | ||
| let off${i}: Int32 = ${"0" if i == 0 else "off" + str(i - 1) + " + T" + str(i - 1) + "._tensorHandleCount"} | ||
| %end | ||
| return (${", ".join(["T" + str(i) + ".init(_owning: buffer.advanced(by: Int(off" + str(i) + ")))" for i in range(n)])}) | ||
| } | ||
| %end | ||
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.
Uh oh!
There was an error while loading. Please reload this page.