|
| 1 | +#if canImport(_Differentiation) |
| 2 | + |
| 3 | +import OrderedCollectionsDifferentiable |
| 4 | +import Testing |
| 5 | + |
| 6 | +@Suite("Dictionary+Differentiation") |
| 7 | +struct DictionaryDifferentiationTests { |
| 8 | + @Test |
| 9 | + func testSubscriptGet() throws { |
| 10 | + let dictionary: OrderedDictionary<String, Double> = ["a": 3, "b": 7] |
| 11 | + |
| 12 | + let aMultiplier: Double = 13 |
| 13 | + let bMultiplier: Double = 17 |
| 14 | + |
| 15 | + func readFromDictionary(d: OrderedDictionary<String, Double>) -> Double { |
| 16 | + // note that we cannot use #require here as this function cannot throw (due to current compiler constraints wrt differentiation) |
| 17 | + // swift-format-ignore: NeverForceUnwrap |
| 18 | + let a = d["a"]! * aMultiplier |
| 19 | + let b = d["b"]! * bMultiplier |
| 20 | + return a + b |
| 21 | + } |
| 22 | + |
| 23 | + let vwg = valueWithGradient(at: dictionary, of: readFromDictionary) |
| 24 | + |
| 25 | + #expect(vwg.value == 3 * aMultiplier + 7 * bMultiplier) |
| 26 | + #expect(vwg.gradient == ["a": aMultiplier, "b": bMultiplier]) |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + func testOrderedDictionaryReadAndCombineValues() { |
| 31 | + @differentiable(reverse) |
| 32 | + func testFunction(newValues: OrderedDictionary<String, Double>) -> Double { |
| 33 | + // note that we cannot use #require here as this function cannot throw (due to current compiler constraints wrt differentiation) |
| 34 | + // swift-format-ignore: NeverForceUnwrap |
| 35 | + 1.0 * newValues["s1"]! + 2.0 * newValues["s2"]! + 3.0 * newValues["s3"]! |
| 36 | + } |
| 37 | + |
| 38 | + let vwg = valueWithGradient( |
| 39 | + at: ["s1": 10.0, "s2": 20.0, "s3": 30.0], |
| 40 | + of: testFunction |
| 41 | + ) |
| 42 | + |
| 43 | + #expect(vwg.value == 140.0) |
| 44 | + #expect(vwg.gradient == ["s1": 1.0, "s2": 2.0, "s3": 3.0]) |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | + @Test |
| 50 | + func testOrderedDictionaryInoutWriteMethod() { |
| 51 | + @differentiable(reverse) |
| 52 | + func combineByReplacingDictionaryValues(of mainDict: inout OrderedDictionary<String, Double>, with otherDict: OrderedDictionary<String, Double>) { |
| 53 | + for key in withoutDerivative(at: otherDict.keys) { |
| 54 | + // note that we cannot use #require here as this function cannot throw (due to current compiler constraints wrt differentiation) |
| 55 | + // swift-format-ignore: NeverForceUnwrap |
| 56 | + let otherValue = otherDict[key]! |
| 57 | + mainDict.update(at: key, with: otherValue) |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @differentiable(reverse) |
| 62 | + func inoutWrapper(dictionary: OrderedDictionary<String, Double>, otherDictionary: OrderedDictionary<String, Double>) -> OrderedDictionary<String, Double> { |
| 63 | + // we wrap the `combineByReplacingDictionaryValues` |
| 64 | + var mainCopy = dictionary |
| 65 | + combineByReplacingDictionaryValues(of: &mainCopy, with: otherDictionary) |
| 66 | + return mainCopy |
| 67 | + } |
| 68 | + |
| 69 | + let vwpb = valueWithPullback( |
| 70 | + at: ["s1": 10.0, "s2": 20.0, "s3": 30.0], |
| 71 | + ["s1": 2.0], //, "s2": nil, "s3": nil], |
| 72 | + of: inoutWrapper |
| 73 | + ) |
| 74 | + |
| 75 | + #expect(vwpb.value == ["s1": 2.0, "s2": 20.0, "s3": 30.0]) |
| 76 | + // we need to provide a full tangentvector to the pullback hence the keys with zero entries. |
| 77 | + #expect(vwpb.pullback(["s1": 1.0, "s2": 0.0, "s3": 0.0]) == (["s1": 0.0, "s2": 0.0, "s3": 0.0], ["s1": 1.0])) |
| 78 | + #expect(vwpb.pullback(["s1": 0.0, "s2": 1.0, "s3": 0.0]) == (["s1": 0.0, "s2": 1.0, "s3": 0.0], ["s1": 0.0])) |
| 79 | + #expect(vwpb.pullback(["s1": 0.0, "s2": 0.0, "s3": 1.0]) == (["s1": 0.0, "s2": 0.0, "s3": 1.0], ["s1": 0.0])) |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + func testInoutWriteAndSumValues() { |
| 84 | + @differentiable(reverse) |
| 85 | + func combineByReplacingDictionaryValues(of mainDict: inout OrderedDictionary<String, Double>, with otherDict: OrderedDictionary<String, Double>) { |
| 86 | + for key in withoutDerivative(at: otherDict.keys) { |
| 87 | + // note that we cannot use #require here as this function cannot throw (due to current compiler constraints wrt differentiation) |
| 88 | + // swift-format-ignore: NeverForceUnwrap |
| 89 | + let otherValue = otherDict[key]! |
| 90 | + mainDict.update(at: key, with: otherValue) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + @differentiable(reverse) |
| 95 | + func sumValues(of dictionary: OrderedDictionary<String, Double>) -> Double { |
| 96 | + var sum: Double = 0.0 |
| 97 | + for key in withoutDerivative(at: dictionary.keys) { |
| 98 | + // note that we cannot use #require here as this function cannot throw (due to current compiler constraints wrt differentiation) |
| 99 | + // swift-format-ignore: NeverForceUnwrap |
| 100 | + sum += dictionary[key]! |
| 101 | + } |
| 102 | + return sum |
| 103 | + } |
| 104 | + @differentiable(reverse,wrt: dictionary) |
| 105 | + |
| 106 | + func inoutWrapperAndSum(dictionary: OrderedDictionary<String, Double>, otherDictionary: OrderedDictionary<String, Double>) -> Double { |
| 107 | + var mainCopy = dictionary |
| 108 | + combineByReplacingDictionaryValues(of: &mainCopy, with: otherDictionary) |
| 109 | + return sumValues(of: mainCopy) |
| 110 | + } |
| 111 | + |
| 112 | + let vwg = valueWithGradient( |
| 113 | + at: ["s1": 10.0, "s2": 20.0, "s3": 30.0], |
| 114 | + ["s1": 2.0], //, "s2": nil, "s3": nil], |
| 115 | + of: inoutWrapperAndSum |
| 116 | + ) |
| 117 | + |
| 118 | + #expect(vwg.value == 52.0) |
| 119 | + #expect(vwg.gradient == (["s1": 0.0, "s2": 1.0, "s3": 1.0], ["s1": 1.0])) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +#endif |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | +#if canImport(_Differentiation) |
| 129 | + |
| 130 | +import _Differentiation |
| 131 | +import Testing |
| 132 | +import OrderedCollectionsDifferentiable |
| 133 | + |
| 134 | +//final class OrderedDictionaryDifferentiationTests: XCTestCase { |
| 135 | + |
| 136 | +// |
| 137 | +// func testDictionaryInout() { |
| 138 | +// func dictionaryOperationD(of newValues: OrderedDictionary<String, Double?>, on another: inout OrderedDictionary<String, Double?>) { |
| 139 | +// for key in withoutDerivative(at: another.keys) where newValues.keys.contains(key) { |
| 140 | +// let value = newValues[key]! |
| 141 | +// another.set(key, to: value) |
| 142 | +// } |
| 143 | +// } |
| 144 | +// @differentiable(reverse) |
| 145 | +// func testFunctionD(newValues: OrderedDictionary<String, Double?>, dict: OrderedDictionary<String, Double?>) -> Double { |
| 146 | +// var newDict = dict |
| 147 | +// dictionaryOperationD(of: newValues, on: &newDict) |
| 148 | +// return 1.0 * newDict["s1"]!! + 2.0 * newDict["s2"]!! + 3.0 * newDict["s3"]!! |
| 149 | +// } |
| 150 | +// |
| 151 | +// func dictionaryOperation<DataType>( |
| 152 | +// of newValues: OrderedDictionary<String, DataType?>, |
| 153 | +// on another: inout OrderedDictionary<String, DataType?> |
| 154 | +// ) |
| 155 | +// where DataType: Differentiable |
| 156 | +// { |
| 157 | +// for key in withoutDerivative(at: another.keys) where newValues.keys.contains(key) { |
| 158 | +// let value = newValues[key]! |
| 159 | +// another.set(key, to: value) |
| 160 | +// } |
| 161 | +// } |
| 162 | +// @differentiable(reverse) |
| 163 | +// func testFunction(newValues: OrderedDictionary<String, Double?>, dict: OrderedDictionary<String, Double?>) -> Double { |
| 164 | +// var newDict = dict |
| 165 | +// dictionaryOperation(of: newValues, on: &newDict) |
| 166 | +// return 1.0 * newDict["s1"]!! + 2.0 * newDict["s2"]!! + 3.0 * newDict["s3"]!! |
| 167 | +// } |
| 168 | +// let answerExpected = [1.0, 2.0, 3.0] |
| 169 | +// let answerConcreteType = gradient( |
| 170 | +// at: ["s1": 10.0, "s2": 20.0, "s3": 30.0], |
| 171 | +// ["s1": 0.0, "s2": nil, "s3": nil], |
| 172 | +// of: testFunctionD |
| 173 | +// ).0.sorted(by: { $0.key < $1.key }).compactMap(\.value.value) |
| 174 | +// let answerGenericType = gradient( |
| 175 | +// at: ["s1": 10.0, "s2": 20.0, "s3": 30.0], |
| 176 | +// ["s1": 0.0, "s2": nil, "s3": nil], |
| 177 | +// of: testFunction |
| 178 | +// ).0.sorted(by: { $0.key < $1.key }).compactMap(\.value.value) |
| 179 | +// |
| 180 | +// XCTAssertEqual(answerConcreteType, answerExpected) |
| 181 | +// XCTAssertEqual(answerGenericType, answerExpected) |
| 182 | +// } |
| 183 | +//} |
| 184 | +// |
| 185 | +#endif |
0 commit comments