|
| 1 | +//===-- ArrayOps.swift ----------------------------------------*- swift -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +// |
| 13 | +// This file contains some Array ops that cannot be properly handled by #tfop. |
| 14 | +// |
| 15 | +// TODO: These should be deleted once we can properly generate raw ops for these. |
| 16 | +// |
| 17 | +//===----------------------------------------------------------------------===// |
| 18 | + |
| 19 | +import CTensorFlow |
| 20 | + |
| 21 | +public extension Raw { |
| 22 | + /// Saves tensors in V2 checkpoint format. |
| 23 | + /// |
| 24 | + /// By default, saves the named tensors in full. If the caller wishes to save |
| 25 | + /// specific slices of full tensors, "shape_and_slices" should be non-empty strings |
| 26 | + /// and correspondingly well-formed. |
| 27 | + /// |
| 28 | + /// - Parameters: |
| 29 | + /// - prefix: Must have a single element. The prefix of the V2 checkpoint to which we |
| 30 | + /// write the tensors. |
| 31 | + /// - tensor_names: shape {N}. The names of the tensors to be saved. |
| 32 | + /// - shape_and_slices: shape {N}. The slice specs of the tensors to be saved. |
| 33 | + /// Empty strings indicate that they are non-partitioned tensors. |
| 34 | + /// - tensors: `N` tensors to save. |
| 35 | + @inlinable @inline(__always) |
| 36 | + static func saveV2( |
| 37 | + prefix: StringTensor, |
| 38 | + tensorNames: StringTensor, |
| 39 | + shapeAndSlices: StringTensor, |
| 40 | + tensors: [AnyTensor] |
| 41 | + ) { |
| 42 | + let s: CTFStatus = TF_NewStatus() |
| 43 | + defer { TF_DeleteStatus(s) } |
| 44 | + let op: CTFEOp = TFE_NewOp(_ExecutionContext.global.eagerContext, "SaveV2", s) |
| 45 | + defer { TFE_DeleteOp(op) } |
| 46 | + let _ = _TFCOpAddInputFromTensorGroup(op, prefix, s) |
| 47 | + let _ = _TFCOpAddInputFromTensorGroup(op, tensorNames, s) |
| 48 | + let _ = _TFCOpAddInputFromTensorGroup(op, shapeAndSlices, s) |
| 49 | + let _ = _TFCOpAddInputFromAnyTensors(op, tensors, s) |
| 50 | + let _ = _TFCOpSetAttrTypeArray(op, "dtypes", tensors.map { $0._tensorFlowDataType }) |
| 51 | + return _TFCExecuteOp(op, s) |
| 52 | + } |
| 53 | + |
| 54 | + /// Restores tensors from a V2 checkpoint. |
| 55 | + /// |
| 56 | + /// For backward compatibility with the V1 format, this Op currently allows |
| 57 | + /// restoring from a V1 checkpoint as well: |
| 58 | + /// - This Op first attempts to find the V2 index file pointed to by "prefix", and |
| 59 | + /// if found proceed to read it as a V2 checkpoint; |
| 60 | + /// - Otherwise the V1 read path is invoked. |
| 61 | + /// Relying on this behavior is not recommended, as the ability to fall back to read |
| 62 | + /// V1 might be deprecated and eventually removed. |
| 63 | + /// |
| 64 | + /// By default, restores the named tensors in full. If the caller wishes to restore |
| 65 | + /// specific slices of stored tensors, "shape_and_slices" should be non-empty |
| 66 | + /// strings and correspondingly well-formed. |
| 67 | + /// |
| 68 | + /// Callers must ensure all the named tensors are indeed stored in the checkpoint. |
| 69 | + /// |
| 70 | + /// - Parameters: |
| 71 | + /// - prefix: Must have a single element. The prefix of a V2 checkpoint. |
| 72 | + /// - tensor_names: shape {N}. The names of the tensors to be restored. |
| 73 | + /// - shape_and_slices: shape {N}. The slice specs of the tensors to be restored. |
| 74 | + /// Empty strings indicate that they are non-partitioned tensors. |
| 75 | + /// |
| 76 | + /// - Attr dtypes: shape {N}. The list of expected dtype for the tensors. Must match |
| 77 | + /// those stored in the checkpoint. |
| 78 | + /// |
| 79 | + /// - Output tensors: shape {N}. The restored tensors, whose shapes are read from the |
| 80 | + /// checkpoint directly. |
| 81 | + @inlinable @inline(__always) |
| 82 | + static func restoreV2( |
| 83 | + prefix: StringTensor, |
| 84 | + tensorNames: StringTensor, |
| 85 | + shapeAndSlices: StringTensor, |
| 86 | + dtypes: [TensorDataType] |
| 87 | + ) -> [AnyTensor] { |
| 88 | + let s: CTFStatus = TF_NewStatus() |
| 89 | + defer { TF_DeleteStatus(s) } |
| 90 | + let op: CTFEOp = TFE_NewOp(_ExecutionContext.global.eagerContext, "RestoreV2", s) |
| 91 | + defer { TFE_DeleteOp(op) } |
| 92 | + let _ = _TFCOpAddInputFromTensorGroup(op, prefix, s) |
| 93 | + let _ = _TFCOpAddInputFromTensorGroup(op, tensorNames, s) |
| 94 | + let _ = _TFCOpAddInputFromTensorGroup(op, shapeAndSlices, s) |
| 95 | + let _ = _TFCOpSetAttrTypeArray(op, "dtypes", dtypes) |
| 96 | + |
| 97 | + var count: Int32 = Int32(dtypes.count) |
| 98 | + let buffer: UnsafeMutablePointer<CTensorHandle> = |
| 99 | + UnsafeMutablePointer.allocate(capacity: Int(count)) |
| 100 | + defer { buffer.deallocate() } |
| 101 | + _TFCEagerExecute(op, UnsafeMutablePointer<CTensorHandle?>(buffer), &count, s) |
| 102 | + checkOk(s) |
| 103 | + |
| 104 | + var out: [AnyTensor] = [] |
| 105 | + var cursor = buffer |
| 106 | + for type in dtypes { |
| 107 | + out.append(makeTensor(dataType: type, owning: cursor.pointee)) |
| 108 | + cursor = cursor.advanced(by: 1) |
| 109 | + } |
| 110 | + return out |
| 111 | + } |
| 112 | +} |
0 commit comments