|
| 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 | + private func runTrace(trace: LazyTensorTrace, input: TensorGroup) -> [TFETensorHandle] { |
| 70 | + let tffunc = TFFunction(trace: trace) |
| 71 | + let inputHandles = input._tensorHandles.map { $0._tfeTensorHandle } |
| 72 | + let outputHandles = tffunc.execute(inputHandles) |
| 73 | + return outputHandles |
| 74 | + } |
| 75 | + |
| 76 | + static var allTests = [ |
| 77 | + ("testSingleInput", testSingleInput), |
| 78 | + ("testTensorGroupInputOutputs", testTensorGroupInputOutputs) |
| 79 | + ] |
| 80 | +} |
0 commit comments