|
| 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 | +import CTensorFlow |
| 15 | + |
| 16 | +extension LazyTensorOperation { |
| 17 | + /// Returns a newly created TFE_Op with only the attributes set. NOTE: the |
| 18 | + /// caller should explicitly call `TFE_DeleteOp(tfeOp.op)` and |
| 19 | + /// `TFE_DeleteStatus(tfeOp.status)` to free the resources allocated in the |
| 20 | + /// newly created TFE_Op. |
| 21 | + private var tfeOp: TFE_Op { |
| 22 | + let op = TFE_Op(name, outputCount) |
| 23 | + for (name, value) in attributes { |
| 24 | + switch value { |
| 25 | + case .boolValue(let v): op.updateAttribute(name, v) |
| 26 | + case .intValue(let v): op.updateAttribute(name, v) |
| 27 | + case .floatValue(let v): op.updateAttribute(name, v) |
| 28 | + case .doubleValue(let v): op.updateAttribute(name, v) |
| 29 | + case .stringValue(let v): op.updateAttribute(name, v) |
| 30 | + case .boolArray(let v): op.updateAttribute(name, v) |
| 31 | + case .intArray(let v): op.updateAttribute(name, v) |
| 32 | + case .floatArray(let v): op.updateAttribute(name, v) |
| 33 | + case .doubleArray(let v): op.updateAttribute(name, v) |
| 34 | + case .stringArray(let v): op.updateAttribute(name, v) |
| 35 | + case .constTensor(_): fatalError("Const Tensor cannot be eager attribute.") |
| 36 | + case .tensorDataTypeValue(let v): op.updateAttribute(name, v) |
| 37 | + case .tensorDataTypeArray(let v): op.updateAttribute(name, v) |
| 38 | + case .optionalTensorShape(let v): op.updateAttribute(name, v) |
| 39 | + case .optionalTensorShapeArray(let v): op.updateAttribute(name, v) |
| 40 | + case .tensorFunctionPointer(_): fatalError("tensorFunctionPointer Unimplemented!") |
| 41 | + } |
| 42 | + } |
| 43 | + return op |
| 44 | + } |
| 45 | + |
| 46 | + func updateOutputShapes() { |
| 47 | + let status = TF_NewStatus() |
| 48 | + defer { TF_DeleteStatus(status) } |
| 49 | + |
| 50 | + let inputShapes: [TensorShape] = inputs.lazy.flatMap { (input: Input) -> [TensorShape] in |
| 51 | + switch input { |
| 52 | + case .single(let handle): return [handle.shape] |
| 53 | + case .list(let values): return values.lazy.map { $0.shape } |
| 54 | + } |
| 55 | + } |
| 56 | + let inputShapeList = TF_NewShapeAndTypeList(/*num_shapes*/ Int32(inputShapes.count)) |
| 57 | + defer { TF_DeleteShapeAndTypeList(inputShapeList) } |
| 58 | + for (i, shape) in inputShapes.enumerated() { |
| 59 | + let int64_dimensions = shape.dimensions.map { Int64($0) } |
| 60 | + int64_dimensions.withUnsafeBufferPointer { buffer in |
| 61 | + TF_ShapeAndTypeListSetShape( |
| 62 | + inputShapeList, |
| 63 | + /*index*/ Int32(i), |
| 64 | + buffer.baseAddress, |
| 65 | + Int32(int64_dimensions.count)) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // This will be filled in by `TFE_InferShapes` and should be freed later. |
| 70 | + var outputShapeListPtr = UnsafeMutablePointer<TF_ShapeAndTypeList>(nil) |
| 71 | + defer { TF_DeleteShapeAndTypeList(outputShapeListPtr) } |
| 72 | + |
| 73 | + let tfeOp = self.tfeOp |
| 74 | + defer { |
| 75 | + TFE_DeleteOp(tfeOp.op) |
| 76 | + TF_DeleteStatus(tfeOp.status) |
| 77 | + } |
| 78 | + |
| 79 | + TFE_InferShapes( |
| 80 | + tfeOp.op, |
| 81 | + /*input_shapes*/ inputShapeList, |
| 82 | + /*input_tensors*/ nil, |
| 83 | + /*num_input_tensors*/ 0, |
| 84 | + /*input_tensors_as_shapes*/ nil, |
| 85 | + /*input_resource_shapes_and_types*/ nil, |
| 86 | + /*output_shapes*/ &outputShapeListPtr, |
| 87 | + /*output_resource_shapes_and_types*/ nil, |
| 88 | + status) |
| 89 | + |
| 90 | + checkOk(status) |
| 91 | + |
| 92 | + precondition(outputShapeListPtr != nil, "TFE_InferShapes returned nil for output shapes") |
| 93 | + let outputShapeList = outputShapeListPtr!.pointee |
| 94 | + outputShapes = (0..<outputShapeList.num_items).lazy.map { index -> TensorShape? in |
| 95 | + let outputShape = outputShapeList.items![Int(index)] |
| 96 | + if outputShape.num_dims == -1 { return nil } |
| 97 | + let dims = (0..<outputShape.num_dims).lazy.map { Int(outputShape.dims![Int($0)]) } |
| 98 | + let hasUnknownDims = dims.contains { $0 == -1 } |
| 99 | + return hasUnknownDims ? nil : TensorShape(dims) |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments