tzh
2024-08-22 c7d0944258c7d0943aa7b2211498fd612971ce27
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2018 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
 
import Foundation
 
/// TensorFlow Lite interpreter errors.
public enum InterpreterError: Error {
  case invalidTensorIndex(index: Int, maxIndex: Int)
  case invalidTensorDataCount(provided: Int, required: Int)
  case invalidTensorDataType
  case failedToLoadModel
  case failedToCreateInterpreter
  case failedToResizeInputTensor(index: Int)
  case failedToCopyDataToInputTensor
  case failedToAllocateTensors
  case allocateTensorsRequired
  case invokeInterpreterRequired
  case tensorFlowLiteError(String)
}
 
// MARK: - Extensions
 
extension InterpreterError: LocalizedError {
  /// Localized description of the interpreter error.
  public var errorDescription: String? {
    switch self {
    case .invalidTensorIndex(let index, let maxIndex):
      return "Invalid tensor index \(index), max index is \(maxIndex)."
    case .invalidTensorDataCount(let providedCount, let requiredCount):
      return "Provided data count \(providedCount) must match the required count \(requiredCount)."
    case .invalidTensorDataType:
      return "Tensor data type is unsupported or could not be determined because of a model error."
    case .failedToLoadModel:
      return "Failed to load the given model."
    case .failedToCreateInterpreter:
      return "Failed to create the interpreter."
    case .failedToResizeInputTensor(let index):
      return "Failed to resize input tesnor at index \(index)."
    case .failedToCopyDataToInputTensor:
      return "Failed to copy data to input tensor."
    case .failedToAllocateTensors:
      return "Failed to allocate memory for input tensors."
    case .allocateTensorsRequired:
      return "Must call allocateTensors()."
    case .invokeInterpreterRequired:
      return "Must call invoke()."
    case .tensorFlowLiteError(let message):
      return "TensorFlow Lite Error: \(message)"
    }
  }
}
 
extension InterpreterError: CustomStringConvertible {
  /// Textual representation of the TensorFlow Lite interpreter error.
  public var description: String {
    return errorDescription ?? "Unknown error."
  }
}
 
#if swift(>=4.2)
extension InterpreterError: Equatable {}
#else
extension InterpreterError: Equatable {
  public static func == (lhs: InterpreterError, rhs: InterpreterError) -> Bool {
    switch (lhs, rhs) {
    case (.invalidTensorDataType, .invalidTensorDataType),
         (.failedToLoadModel, .failedToLoadModel),
         (.failedToCreateInterpreter, .failedToCreateInterpreter),
         (.failedToAllocateTensors, .failedToAllocateTensors),
         (.allocateTensorsRequired, .allocateTensorsRequired),
         (.invokeInterpreterRequired, .invokeInterpreterRequired):
      return true
    case (.invalidTensorIndex(let lhsIndex, let lhsMaxIndex),
          .invalidTensorIndex(let rhsIndex, let rhsMaxIndex)):
      return lhsIndex == rhsIndex && lhsMaxIndex == rhsMaxIndex
    case (.invalidTensorDataCount(let lhsProvidedCount, let lhsRequiredCount),
          .invalidTensorDataCount(let rhsProvidedCount, let rhsRequiredCount)):
      return lhsProvidedCount == rhsProvidedCount && lhsRequiredCount == rhsRequiredCount
    case (.failedToResizeInputTensor(let lhsIndex), .failedToResizeInputTensor(let rhsIndex)):
      return lhsIndex == rhsIndex
    case (.tensorFlowLiteError(let lhsMessage), .tensorFlowLiteError(let rhsMessage)):
      return lhsMessage == rhsMessage
    default:
      return false
    }
  }
}
#endif  // swift(>=4.2)