1 // Copyright 2022 Google Inc. 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 <Foundation/Foundation.h> 16 17 NS_ASSUME_NONNULL_BEGIN 18 19 @class TFLTensor; 20 21 /** Protocol for providing and mutating data on a `TFLTensor`. */ 22 @protocol TFLTensorDataAccessor <NSObject> 23 24 /** 25 * Copies the given data into the input tensor. This is allowed only before the interpreter or 26 * signature runner is invoked. 27 * 28 * @param data The data to set. The byte size of the data must match what's required by the input 29 * tensor. 30 * @param inputTensor The input tensor to copy data to. 31 * @param error An optional error parameter populated when there is an error in setting the data. 32 * 33 * @return Whether the data was copied into the input tensor at the given index successfully. 34 * Returns NO if an error occurred. 35 */ 36 - (BOOL)copyData:(NSData *)data toInputTensor:(TFLTensor *)inputTensor error:(NSError **)error; 37 38 /** 39 * Retrieves a copy of the data from the given tensor. For an output tensor, the interpreter or 40 * signature invocation has to complete before the data can be retrieved. 41 * 42 * @param tensor A tensor. 43 * @param error An optional error parameter populated when there is an error in getting the data. 44 * 45 * @return The data of the given tensor. `nil` if there is an error or data is not available. 46 */ 47 - (nullable NSData *)dataFromTensor:(TFLTensor *)tensor error:(NSError **)error; 48 49 /** 50 * Retrieves the shape of the given tensor, an array of positive unsigned integer(s) containing the 51 * size of each dimension. For example: shape of [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]] is 52 * [2, 2, 3]. 53 * 54 * @param tensor An input or output tensor. 55 * @param error An optional error parameter populated when there is an error in retrieving the 56 * shape. 57 * 58 * @return The shape of the tensor. `nil` if there is an error in retrieving the shape. 59 */ 60 - (nullable NSArray<NSNumber *> *)shapeOfTensor:(TFLTensor *)tensor error:(NSError **)error; 61 62 @end 63 64 NS_ASSUME_NONNULL_END 65