|
| 1 | +// Copyright 2020 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 _Differentiation |
| 16 | + |
| 17 | +#if TENSORFLOW_USE_STANDARD_TOOLCHAIN |
| 18 | +@_spi(Reflection) import Swift |
| 19 | + |
| 20 | +infix operator .*: MultiplicationPrecedence |
| 21 | +infix operator .*=: AssignmentPrecedence |
| 22 | + |
| 23 | +/// Implementation detail of the reflection default implementation. |
| 24 | +/// |
| 25 | +/// Contains versions of functions in PointwiseMultiplicative that |
| 26 | +/// operate over key paths and modify a child of `Root` in-place. |
| 27 | +/// The key paths must all be WritableKeyPath<Root, Self>. This is a workaround |
| 28 | +/// to simulate having Self constraints. |
| 29 | +public protocol _PointwiseMultiplicative { |
| 30 | + /// lhs[keyPath: kp] .*= rhs[keyPath: kp] |
| 31 | + static func _pointwiseMult<Root>(_ lhs: inout Root, _ rhs: Root, _ kp: PartialKeyPath<Root>) |
| 32 | + /// out[keyPath: kp] = Self.one |
| 33 | + static func _setOne<Root>(_ out: inout Root, _ kp: PartialKeyPath<Root>) |
| 34 | + /// out[keyPath: kp] = out[keyPath: kp].reciprocal |
| 35 | + static func _setReciprocal<Root>(_ out: inout Root, _ kp: PartialKeyPath<Root>) |
| 36 | +} |
| 37 | + |
| 38 | +public protocol PointwiseMultiplicative: _PointwiseMultiplicative & AdditiveArithmetic { |
| 39 | + /// The one value. |
| 40 | + /// |
| 41 | + /// One is the identity element for multiplication. For any value, |
| 42 | + /// `x .* .one == x` and `.one .* x == x`. |
| 43 | + static var one: Self { get } |
| 44 | + |
| 45 | + /// The multiplicative inverse of self. |
| 46 | + /// |
| 47 | + /// For any value, `x .* x.reciprocal == .one` and |
| 48 | + /// `x.reciprocal .* x == .one`. |
| 49 | + var reciprocal: Self { get } |
| 50 | + |
| 51 | + /// Multiplies two values and produces their product. |
| 52 | + /// |
| 53 | + /// - Parameters: |
| 54 | + /// - lhs: The first value to multiply. |
| 55 | + /// - rhs: The second value to multiply. |
| 56 | + static func .* (lhs: Self, rhs: Self) -> Self |
| 57 | + |
| 58 | + /// Multiplies two values and produces their product. |
| 59 | + /// |
| 60 | + /// - Parameters: |
| 61 | + /// - lhs: The first value to multiply. |
| 62 | + /// - rhs: The second value to multiply. |
| 63 | + static func .*= (lhs: inout Self, rhs: Self) |
| 64 | +} |
| 65 | + |
| 66 | +extension PointwiseMultiplicative { |
| 67 | + public static func .*= (lhs: inout Self, rhs: Self) { |
| 68 | + lhs = lhs .* rhs |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +extension PointwiseMultiplicative |
| 73 | +where Self: ExpressibleByIntegerLiteral { |
| 74 | + public static var one: Self { |
| 75 | + return 1 |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +extension PointwiseMultiplicative { |
| 80 | + public static var one: Self { |
| 81 | + var out = self.zero |
| 82 | + visitChildren { kp, t in t._setOne(&out, kp) } |
| 83 | + return out |
| 84 | + } |
| 85 | + public var reciprocal: Self { |
| 86 | + var out = self |
| 87 | + Self.visitChildren { kp, t in t._setReciprocal(&out, kp) } |
| 88 | + return out |
| 89 | + } |
| 90 | + public static func .* (lhs: Self, rhs: Self) -> Self { |
| 91 | + var out = lhs |
| 92 | + visitChildren { kp, t in |
| 93 | + t._pointwiseMult(&out, rhs, kp) |
| 94 | + } |
| 95 | + return out |
| 96 | + } |
| 97 | + public static func _pointwiseMult<Root>( |
| 98 | + _ lhs: inout Root, _ rhs: Root, _ kp: PartialKeyPath<Root> |
| 99 | + ) { |
| 100 | + let kp = kp as! WritableKeyPath<Root, Self> |
| 101 | + lhs[keyPath: kp] .*= rhs[keyPath: kp] |
| 102 | + } |
| 103 | + public static func _setOne<Root>(_ out: inout Root, _ kp: PartialKeyPath<Root>) { |
| 104 | + let kp = kp as! WritableKeyPath<Root, Self> |
| 105 | + out[keyPath: kp] = Self.one |
| 106 | + } |
| 107 | + public static func _setReciprocal<Root>(_ out: inout Root, _ kp: PartialKeyPath<Root>) { |
| 108 | + let kp = kp as! WritableKeyPath<Root, Self> |
| 109 | + out[keyPath: kp] = out[keyPath: kp].reciprocal |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +extension PointwiseMultiplicative { |
| 114 | + internal static func visitChildren( |
| 115 | + _ body: (PartialKeyPath<Self>, _PointwiseMultiplicative.Type) -> Void |
| 116 | + ) { |
| 117 | + if !_forEachFieldWithKeyPath( |
| 118 | + of: Self.self, |
| 119 | + body: { name, kp in |
| 120 | + let valueType = type(of: kp).valueType |
| 121 | + guard let valueType = valueType as? _PointwiseMultiplicative.Type else { |
| 122 | + fatalError("not PointwiseMultiplicative: \(valueType)") |
| 123 | + } |
| 124 | + body(kp, valueType) |
| 125 | + return true |
| 126 | + }) |
| 127 | + { |
| 128 | + fatalError( |
| 129 | + "Unreflectable member of \(Self.self) while implementing PointwiseMultiplicative.") |
| 130 | + } |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +extension Array.DifferentiableView: _PointwiseMultiplicative |
| 135 | +where Element: Differentiable & PointwiseMultiplicative {} |
| 136 | +extension Tensor: _PointwiseMultiplicative where Scalar: Numeric {} |
| 137 | +#endif |
0 commit comments