Skip to content
This repository was archived by the owner on Jul 1, 2023. It is now read-only.

Trigger execution of operations with no outputs. #296

Merged
merged 3 commits into from
Jun 27, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
40 changes: 39 additions & 1 deletion Sources/TensorFlow/Core/LazyTensorOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,45 @@ extension LazyTensorOperation: TFTensorOperation {
fatalError("Unimplemented [TFFunction] attribute.")
}

func execute() {}
func execute() {
// If we want to stage this, we will need to add control dependencies.
// For the time-being, just build a TFE_Op and run it.
//
let op = TFE_Op(name, outputCount)
// TODO(https://bugs.swift.org/browse/TF-604):
// Materialize inputs en masse and not one-by-one.
for input in inputs {
switch input {
case .single(let v):
op.addInput(v._tfeTensorHandle)
case .list(let values):
for v in values {
op.addInput(v._tfeTensorHandle)
}
}
}
for (name, value) in attributes {
switch value {
case .boolValue(let v): op.updateAttribute(name, v)
case .intValue(let v): op.updateAttribute(name, v)
case .floatValue(let v): op.updateAttribute(name, v)
case .doubleValue(let v): op.updateAttribute(name, v)
case .stringValue(let v): op.updateAttribute(name, v)
case .boolArray(let v): op.updateAttribute(name, v)
case .intArray(let v): op.updateAttribute(name, v)
case .floatArray(let v): op.updateAttribute(name, v)
case .doubleArray(let v): op.updateAttribute(name, v)
case .stringArray(let v): op.updateAttribute(name, v)
case .constTensor(_): fatalError("Const Tensor cannot be eager attribute.")
case .tensorDataTypeValue(let v): op.updateAttribute(name, v)
case .tensorDataTypeArray(let v): op.updateAttribute(name, v)
case .optionalTensorShape(let v): op.updateAttribute(name, v)
case .optionalTensorShapeArray(let v): op.updateAttribute(name, v)
case .tensorFunctionPointer(_): fatalError("tensorFunctionPointer Unimplemented!")
}
}
op.execute()
}

func execute<T0: TensorArrayProtocol>(
_ count0: Int
Expand Down
39 changes: 38 additions & 1 deletion Tests/TensorFlowTests/LazyTensorEvaluationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,44 @@ final class LazyTensorEvaluationTests: XCTestCase {
XCTAssertTrue(isMaterialized(sum))
}

struct SimpleOutput: TensorGroup {
let a: TensorHandle<Int32>
let b: TensorHandle<Int32>
}

func testNoOutputOperations() {
let elements1: Tensor<Int32> = [0, 1, 2]
let elements2: Tensor<Int32> = [10, 11, 12]
let outputTypes = [Int32.tensorFlowDataType, Int32.tensorFlowDataType]
let outputShapes: [TensorShape?] = [nil, nil]
let dataset: VariantHandle = Raw.tensorSliceDataset(
components: [elements1, elements2],
outputShapes: outputShapes
)
let iterator: ResourceHandle = Raw.iteratorV2(sharedName: "blah",
container: "earth", outputTypes: outputTypes, outputShapes: outputShapes
)
// `dataset` and `iterator` should not be materialized yet.
XCTAssertFalse(isMaterialized(dataset.handle))
XCTAssertFalse(isMaterialized(iterator.handle))
Raw.makeIterator(dataset: dataset, iterator: iterator)

// `dataset` and `iterator` should be materialized now as
// makeIterator executes.
XCTAssertTrue(isMaterialized(dataset.handle))
XCTAssertTrue(isMaterialized(iterator.handle))
let next: SimpleOutput = Raw.iteratorGetNext(
iterator: iterator, outputShapes: outputShapes
)
XCTAssertEqual(Tensor(handle: next.a).scalarized(), 0)
XCTAssertEqual(Tensor(handle: next.b).scalarized(), 10)
}

private func isMaterialized<T: TensorFlowScalar>(_ input: Tensor<T>) -> Bool {
let tensor = input.handle.handle
return isMaterialized(input.handle.handle)
}

private func isMaterialized(_ tensor: _AnyTensorHandle) -> Bool {
guard let lazyTensor = tensor as? LazyTensor else { return true }
switch lazyTensor.handle {
case .symbolic(let op, _, _): return op.outputs != nil
Expand All @@ -100,6 +136,7 @@ final class LazyTensorEvaluationTests: XCTestCase {
("testMultipleMaterializations", testMultipleMaterializations),
("testSimpleControlFlow", testSimpleControlFlow),
("testSimpleLoop", testSimpleLoop),
("testNoOutputOperations", testNoOutputOperations)
]
}