|
| 1 | +#if !COMPILING_TENSORFLOW_MODULE |
| 2 | +import TensorFlow |
| 3 | +#endif |
| 4 | + |
| 5 | +//===-- Dataset.swift -----------------------------------------*- swift -*-===// |
| 6 | +// |
| 7 | +// This source file is part of the Swift.org open source project |
| 8 | +// |
| 9 | +// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors |
| 10 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 11 | +// |
| 12 | +// See https://swift.org/LICENSE.txt for license information |
| 13 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 14 | +// |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | +// |
| 17 | +// The dataset API. |
| 18 | +// |
| 19 | +//===----------------------------------------------------------------------===// |
| 20 | + |
| 21 | +/// The default graph seed. |
| 22 | +/// |
| 23 | +/// - Note: See TensorFlow's `python.framework.random_seed.DEFAULT_GRAPH_SEED`. |
| 24 | +@usableFromInline let _defaultGraphSeed: Int64 = 87654321 |
| 25 | + |
| 26 | +/// Returns the local seeds an operation should use given an op-specific seed. |
| 27 | +/// |
| 28 | +/// Given operation-specific seed, `seed`, this helper function returns two |
| 29 | +/// seeds derived from graph-level and op-level seeds. Many random operations |
| 30 | +/// internally use the two seeds to allow user to change the seed globally for a |
| 31 | +/// graph, or for only specific operations. |
| 32 | +/// |
| 33 | +/// - Note: See TensorFlow's `python.framework.random_seed.get_seed`. |
| 34 | +/// |
| 35 | +// TODO: There's no support for TF's "global seed" yet, so we always use the |
| 36 | +// default graph seed as the first seed. Need to investigate the best way to |
| 37 | +// model TF's "global seed". |
| 38 | +@usableFromInline @inline(__always) |
| 39 | +func _tensorSeeds(_ seed: Tensor<Int64>) -> (Tensor<Int64>, Tensor<Int64>) { |
| 40 | + return (Tensor(_defaultGraphSeed), seed) |
| 41 | +} |
| 42 | + |
| 43 | +//===----------------------------------------------------------------------===// |
| 44 | +// Single value dataset |
| 45 | +//===----------------------------------------------------------------------===// |
| 46 | + |
| 47 | +/// Represents a potentially large set of elements. |
| 48 | +/// |
| 49 | +/// A `Dataset` can be used to represent an input pipeline as a collection of |
| 50 | +/// element tensors. |
| 51 | +@_fixed_layout |
| 52 | +public struct Dataset<Element : TensorGroup> { |
| 53 | + public let _handle: VariantHandle |
| 54 | + |
| 55 | + @inlinable |
| 56 | + public init(_handle: VariantHandle) { |
| 57 | + self._handle = _handle |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +public extension Dataset { |
| 62 | + @inlinable |
| 63 | + init(randomSeed: Int64) { |
| 64 | + let (seed1, seed2) = _tensorSeeds(Tensor(randomSeed)) |
| 65 | + self.init(_handle: Raw.experimentalRandomDataset( |
| 66 | + seed: seed1, |
| 67 | + seed2: seed2, |
| 68 | + outputTypes: Element._typeList, |
| 69 | + outputShapes: Element._unknownShapeList)) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +public extension Dataset { |
| 74 | + /// Creates a dataset from a batch of elements as a tensor. |
| 75 | + @inlinable |
| 76 | + init(elements: Element) { |
| 77 | + self.init(_handle: Raw.tensorSliceDataset( |
| 78 | + components: [elements], |
| 79 | + outputShapes: Element._unknownShapeList)) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +extension Dataset : Sequence { |
| 84 | + public typealias Iterator = DatasetIterator<Element> |
| 85 | + |
| 86 | + /// Returns an iterator over the elements of this dataset. |
| 87 | + @inlinable |
| 88 | + public func makeIterator() -> DatasetIterator<Element> { |
| 89 | + let resource = Raw.anonymousIterator( |
| 90 | + outputTypes: Element._typeList, |
| 91 | + outputShapes: Element._unknownShapeList) |
| 92 | + Raw.makeIterator(dataset: _handle, iterator: resource) |
| 93 | + return DatasetIterator(_handle: resource) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +public extension Dataset { |
| 98 | + // Note that this Dataset API implementation uses an experimental tracing |
| 99 | + // feature, which is not robust and does not have great diagnostics yet. |
| 100 | + @inlinable |
| 101 | + func map<ResultElement : TensorGroup>( |
| 102 | + _ transform: (Element) -> ResultElement |
| 103 | + ) -> Dataset<ResultElement> { |
| 104 | + return Dataset<ResultElement>(_handle: Raw.mapDataset( |
| 105 | + inputDataset: _handle, |
| 106 | + otherArguments: Tensor<Int32>(0), |
| 107 | + f: transform, |
| 108 | + outputTypes: ResultElement._typeList, |
| 109 | + outputShapes: ResultElement._unknownShapeList, |
| 110 | + useInterOpParallelism: true, |
| 111 | + preserveCardinality: false)) |
| 112 | + } |
| 113 | + |
| 114 | + @inlinable |
| 115 | + func map<ResultElement : TensorGroup>( |
| 116 | + parallelCallCount: Int, |
| 117 | + _ transform: (Element) -> ResultElement |
| 118 | + ) -> Dataset<ResultElement> { |
| 119 | + return Dataset<ResultElement>(_handle: Raw.parallelMapDataset( |
| 120 | + inputDataset: _handle, |
| 121 | + otherArguments: Tensor<Int32>(0), |
| 122 | + numParallelCalls: Tensor<Int32>(Int32(parallelCallCount)), |
| 123 | + f: transform, |
| 124 | + outputTypes: ResultElement._typeList, |
| 125 | + outputShapes: ResultElement._unknownShapeList, |
| 126 | + useInterOpParallelism: true, |
| 127 | + sloppy: false, |
| 128 | + preserveCardinality: false)) |
| 129 | + } |
| 130 | + |
| 131 | + @inlinable |
| 132 | + func filter( |
| 133 | + _ isIncluded: (Element) -> Tensor<Bool> |
| 134 | + ) -> Dataset { |
| 135 | + return Dataset(_handle: Raw.filterDataset( |
| 136 | + inputDataset: _handle, |
| 137 | + otherArguments: Tensor<Int32>(0), |
| 138 | + predicate: isIncluded, |
| 139 | + outputTypes: Element._typeList, |
| 140 | + outputShapes: Element._unknownShapeList)) |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +public extension Dataset { |
| 145 | + @inlinable |
| 146 | + func shuffled( |
| 147 | + sampleCount: Int, randomSeed: Int64 |
| 148 | + ) -> Dataset { |
| 149 | + let (seed1, seed2) = _tensorSeeds(Tensor(randomSeed)) |
| 150 | + return Dataset(_handle: Raw.shuffleDataset( |
| 151 | + inputDataset: _handle, |
| 152 | + bufferSize: Tensor(Int64(sampleCount)), |
| 153 | + seed: seed1, |
| 154 | + seed2: seed2, |
| 155 | + outputTypes: Element._typeList, |
| 156 | + outputShapes: Element._unknownShapeList)) |
| 157 | + } |
| 158 | + |
| 159 | + @inlinable |
| 160 | + func batched(_ batchSize: Int) -> Dataset { |
| 161 | + return Dataset(_handle: Raw.batchDataset( |
| 162 | + inputDataset: _handle, |
| 163 | + batchSize: Tensor(Int64(batchSize)), |
| 164 | + outputTypes: Element._typeList, |
| 165 | + outputShapes: Element._unknownShapeList)) |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +/// The type that allows iteration over a dataset's elements. |
| 170 | +@_fixed_layout |
| 171 | +public struct DatasetIterator<Element : TensorGroup> { |
| 172 | + @usableFromInline let _handle: ResourceHandle |
| 173 | + |
| 174 | + @usableFromInline |
| 175 | + internal init(_handle: ResourceHandle) { |
| 176 | + self._handle = _handle |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +extension DatasetIterator : IteratorProtocol { |
| 181 | + /// Advances to the next element and returns it, or `nil` if no next element |
| 182 | + /// exists. |
| 183 | + @inlinable |
| 184 | + public mutating func next() -> Element? { |
| 185 | + let optional = Raw.iteratorGetNextAsOptional( |
| 186 | + iterator: _handle, |
| 187 | + outputTypes: Element._typeList, |
| 188 | + outputShapes: Element._unknownShapeList) |
| 189 | + guard Raw.optionalHasValue(optional: optional).scalarized() else { |
| 190 | + return nil |
| 191 | + } |
| 192 | + return Raw.optionalGetValue( |
| 193 | + optional: optional, |
| 194 | + outputShapes: Element._unknownShapeList) |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +/// A 2-tuple-like struct that conforms to TensorGroup that represents a tuple |
| 199 | +/// of 2 types conforming to TensorGroup. |
| 200 | +@_fixed_layout |
| 201 | +public struct Zip2TensorGroup<T : TensorGroup, U : TensorGroup> : TensorGroup { |
| 202 | + public var first: T |
| 203 | + public var second: U |
| 204 | + |
| 205 | + public init(_ first: T, _ second: U) { |
| 206 | + self.first = first |
| 207 | + self.second = second |
| 208 | + } |
| 209 | +} |
| 210 | + |
| 211 | +@inlinable |
| 212 | +public func zip<T : TensorGroup, U : TensorGroup>( |
| 213 | + _ dataset1: Dataset<T>, _ dataset2: Dataset<U> |
| 214 | +) -> Dataset<Zip2TensorGroup<T, U>> { |
| 215 | + let handle = Raw.zipDataset( |
| 216 | + inputDatasets: [dataset1._handle, dataset2._handle], |
| 217 | + outputTypes: Zip2TensorGroup<T, U>._typeList, |
| 218 | + outputShapes: Zip2TensorGroup<T, U>._unknownShapeList) |
| 219 | + return Dataset(_handle: handle) |
| 220 | +} |
0 commit comments