This repository was archived by the owner on Jul 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathLazyTensorTraceCacheTests.swift
131 lines (120 loc) · 3.85 KB
/
LazyTensorTraceCacheTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2019 The TensorFlow Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import CTensorFlow
import XCTest
@testable import TensorFlow
final class LazyTensorTraceCacheTests: LazyTensorTestCase {
override class func setUp() {
super.setUp()
LazyTensorContext.local.shouldPromoteConstants = true
}
override class func tearDown() {
super.tearDown()
LazyTensorTraceCache.clearCache()
}
func testConstPromotion() {
LazyTensorTraceCache.clearCache()
let a = Tensor<Float>(1.0)
let b = Tensor<Float>(2.0)
let c = Tensor<Float>(3.0)
let d = Tensor<Float>(4.0)
let w = a * b
let x = c * d
// Trigger materialization for `w` so that a trace with constants and mul is added to cache.
XCTAssertEqual(
lazyTrace(w).description,
"""
lazyTrace_3() -> (%2) {
%0 = Const[dtype: float, value: 1.0]()
%1 = Const[dtype: float, value: 2.0]()
%2 = Mul[T: float](%0, %1)
}
""")
XCTAssertEqual(w.scalars, [2.0])
// The trace for `x` should have the inputs to Mul as arguments instead of constants.
XCTAssertEqual(
lazyTrace(x).description,
"""
lazyTrace_3(%0: float, %1: float) -> (%2) {
%2 = Mul[T: float](%0, %1)
}
""")
XCTAssertEqual(x.scalarized(), 12.0)
let e = Tensor<Float>(shape: [1, 3], scalars: [1, 2, 3])
let f = Tensor<Float>(5.0)
let y = e * f
// We won't promote constants in 'y' as shape of constants is different.
XCTAssertEqual(
lazyTrace(y).description,
"""
lazyTrace_3() -> (%2) {
%0 = Const[dtype: float, value: [[1.0, 2.0, 3.0]]]()
%1 = Const[dtype: float, value: 5.0]()
%2 = Mul[T: float](%0, %1)
}
""")
XCTAssertEqual(y.scalars, [5.0, 10.0, 15.0])
}
func testDoNotPromoteEqualConstants() {
LazyTensorTraceCache.clearCache()
let a = Tensor<Float>(1.0)
let b = Tensor<Float>(2.0)
let c = Tensor<Float>(3.0)
let w = a * b
let x = a * c
XCTAssertEqual(
lazyTrace(w).description,
"""
lazyTrace_3() -> (%2) {
%0 = Const[dtype: float, value: 1.0]()
%1 = Const[dtype: float, value: 2.0]()
%2 = Mul[T: float](%0, %1)
}
""")
XCTAssertEqual(w.scalars, [2.0])
// Const 1.0 is not promoted.
XCTAssertEqual(
lazyTrace(x).description,
"""
lazyTrace_3(%1: float) -> (%2) {
%0 = Const[dtype: float, value: 1.0]()
%2 = Mul[T: float](%0, %1)
}
""")
}
private func lazyTensorOperation<T: TensorFlowScalar>(
_ input: Tensor<T>
) -> LazyTensorOperation? {
let tensor = input.handle.handle
guard let lazyTensor = tensor as? LazyTensorHandle else {
XCTFail("Trying to get lazy trace for a non-lazy tensor.")
return nil
}
guard case let .symbolic(lazyOp, _, _) = lazyTensor.handle else {
XCTFail("Cannot get lazy trace for a concrete tensor.")
return nil
}
return lazyOp
}
private func lazyTrace<T: TensorFlowScalar>(
_ input: Tensor<T>
) -> LazyTensorTrace {
let lazyOperation = lazyTensorOperation(input)!
return LazyTensorTraceBuilder.materializationTraceInfo(lazyOperation).trace
}
static var allTests = [
("testConstPromotion", testConstPromotion),
("testDoNotPromoteEqualConstants", testDoNotPromoteEqualConstants),
]
}