|
| 1 | +// Copyright 2019 The TensorFlow Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import XCTest |
| 16 | + |
| 17 | +@testable import TensorFlow |
| 18 | +import CTensorFlow |
| 19 | + |
| 20 | +final class LazyTensorExplicitTraceTests: XCTestCase { |
| 21 | + override class func setUp() { |
| 22 | + super.setUp() |
| 23 | + _RuntimeConfig.useLazyTensor = true |
| 24 | + } |
| 25 | + |
| 26 | + override class func tearDown() { |
| 27 | + super.tearDown() |
| 28 | + _RuntimeConfig.useLazyTensor = false |
| 29 | + } |
| 30 | + |
| 31 | + func testSingleInput() { |
| 32 | + func fn(x: Tensor<Float>) -> Tensor<Float> { return x + x } |
| 33 | + let trace = LazyTensorTraceBuilder.trace(fn) |
| 34 | + XCTAssertEqual(trace.description, |
| 35 | + """ |
| 36 | + lazyTrace_2(%0: float) -> (%1) { |
| 37 | + %1 = Add[T: float](%0, %0) |
| 38 | + } |
| 39 | + """) |
| 40 | + let outputs = runTrace(trace: trace, input: Tensor<Float>(10.0)) |
| 41 | + XCTAssertEqual(outputs.count, 1) |
| 42 | + XCTAssertEqual(outputs[0].valueDescription, "20.0") |
| 43 | + } |
| 44 | + |
| 45 | + func testTensorGroupInputOutputs() { |
| 46 | + typealias TensorFloatInt32Pair = Zip2TensorGroup<Tensor<Float>, Tensor<Int32>> |
| 47 | + typealias TensorInt32FloatPair = Zip2TensorGroup<Tensor<Int32>, Tensor<Float>> |
| 48 | + func fn(input: TensorFloatInt32Pair) -> TensorInt32FloatPair { |
| 49 | + return TensorInt32FloatPair(input.second * 4, input.first + 3.0) |
| 50 | + } |
| 51 | + let trace = LazyTensorTraceBuilder.trace(fn) |
| 52 | + XCTAssertEqual(trace.description, |
| 53 | + """ |
| 54 | + lazyTrace_6(%0: float, %1: int32) -> (%3, %5) { |
| 55 | + %2 = Const[dtype: int32, value: 4]() |
| 56 | + %3 = Mul[T: int32](%1, %2) |
| 57 | + %4 = Const[dtype: float, value: 3.0]() |
| 58 | + %5 = Add[T: float](%0, %4) |
| 59 | + } |
| 60 | + """) |
| 61 | + let outputs = runTrace( |
| 62 | + trace: trace, |
| 63 | + input: TensorFloatInt32Pair(Tensor<Float>(10.0), Tensor<Int32>(5))) |
| 64 | + XCTAssertEqual(outputs.count, 2) |
| 65 | + XCTAssertEqual(outputs[0].valueDescription, "20") |
| 66 | + XCTAssertEqual(outputs[1].valueDescription, "13.0") |
| 67 | + } |
| 68 | + |
| 69 | + func testClosureCapturesOfTensors() { |
| 70 | + let x = Tensor<Float>(10.0) |
| 71 | + let y = x + x |
| 72 | + func fn(input: Tensor<Float>) -> Tensor<Float> { |
| 73 | + return input * y |
| 74 | + } |
| 75 | + let trace = LazyTensorTraceBuilder.trace(fn) |
| 76 | + /// Note that the computation x + x is encoded in the trace. |
| 77 | + XCTAssertEqual(trace.description, |
| 78 | + """ |
| 79 | + lazyTrace_4(%0: float) -> (%3) { |
| 80 | + %1 = Const[dtype: float, value: 10.0]() |
| 81 | + %2 = Add[T: float](%1, %1) |
| 82 | + %3 = Mul[T: float](%0, %2) |
| 83 | + } |
| 84 | + """) |
| 85 | + let outputs = runTrace( |
| 86 | + trace: trace, |
| 87 | + input: Tensor<Float>(5.0)) |
| 88 | + XCTAssertEqual(outputs.count, 1) |
| 89 | + XCTAssertEqual(outputs[0].valueDescription, "100.0") |
| 90 | + } |
| 91 | + |
| 92 | + func testClosureCapturesOfNonTensors() { |
| 93 | + let x: Float = 5.0 |
| 94 | + func fn(input: Tensor<Float>) -> Tensor<Float> { |
| 95 | + return input * Tensor<Float>(x) |
| 96 | + } |
| 97 | + let trace = LazyTensorTraceBuilder.trace(fn) |
| 98 | + /// Note that the computation x + x is encoded in the trace. |
| 99 | + XCTAssertEqual(trace.description, |
| 100 | + """ |
| 101 | + lazyTrace_3(%0: float) -> (%2) { |
| 102 | + %1 = Const[dtype: float, value: 5.0]() |
| 103 | + %2 = Mul[T: float](%0, %1) |
| 104 | + } |
| 105 | + """) |
| 106 | + let outputs = runTrace(trace: trace, input: Tensor<Float>(23.0)) |
| 107 | + XCTAssertEqual(outputs.count, 1) |
| 108 | + XCTAssertEqual(outputs[0].valueDescription, "115.0") |
| 109 | + } |
| 110 | + |
| 111 | + func testNestedTracing() { |
| 112 | + func square(input: Tensor<Float>) -> Tensor<Float> { |
| 113 | + return input * input |
| 114 | + } |
| 115 | + |
| 116 | + func nestedTrace(input: Tensor<Float>) -> Tensor<Float> { |
| 117 | + let trace = LazyTensorTraceBuilder.trace(square) |
| 118 | + let outputs = runTrace(trace: trace, input: Tensor<Float>(3.0)) |
| 119 | + XCTAssertEqual(outputs.count, 1) |
| 120 | + let handle = TensorHandle<Float>(handle: outputs[0]) |
| 121 | + let y = Tensor<Float>(handle: handle) |
| 122 | + return y + input |
| 123 | + } |
| 124 | + |
| 125 | + let trace = LazyTensorTraceBuilder.trace(nestedTrace) |
| 126 | + XCTAssertEqual(trace.description, |
| 127 | + """ |
| 128 | + lazyTrace_3(%0: float) -> (%2) { |
| 129 | + %1 = Const[dtype: float, value: 9.0]() |
| 130 | + %2 = Add[T: float](%1, %0) |
| 131 | + } |
| 132 | + """) |
| 133 | + let outputs = runTrace(trace: trace, input: Tensor<Float>(4.0)) |
| 134 | + XCTAssertEqual(outputs.count, 1) |
| 135 | + XCTAssertEqual(outputs[0].valueDescription, "13.0") |
| 136 | + } |
| 137 | + |
| 138 | + private func runTrace(trace: LazyTensorTrace, input: TensorGroup) -> [TFETensorHandle] { |
| 139 | + let tffunc = TFFunction(trace: trace) |
| 140 | + let inputHandles = input._tensorHandles.map { $0._tfeTensorHandle } |
| 141 | + let outputHandles = tffunc.execute(inputHandles) |
| 142 | + return outputHandles |
| 143 | + } |
| 144 | + |
| 145 | + static var allTests = [ |
| 146 | + ("testSingleInput", testSingleInput), |
| 147 | + ("testTensorGroupInputOutputs", testTensorGroupInputOutputs), |
| 148 | + ("testClosureCapturesOfTensors", testClosureCapturesOfTensors), |
| 149 | + ("testClosureCapturesOfNonTensors", testClosureCapturesOfNonTensors), |
| 150 | + ("testNestedTracing", testNestedTracing) |
| 151 | + ] |
| 152 | +} |
0 commit comments