NSData(TIOData)

@interface NSData (TIOData)

An NSData object may be an input to a tensor or an output from a tensor.

The underlying bytes will be supplied directly to or accepted directly from a tensor. NSData already implements both:

@code

  • (instancetype)initWithBytes:(const void *)bytes length:(NSUInteger)length
  • (void)getBytes:(void *)buffer length:(NSUInteger)length`. @endcode

So we just pass initialization to those methods without making any assumptions about the type of the data (float_t or uint8_t).

  • Initializes an NSData object with bytes from a tensor.

    Bytes are copied according to the following rules, with information about quantization taken from the description:

    • If the layer is unquantized, the tensor’s bytes are copied directly into a data object (the bytes are implicitly interpreted as float_t values).

    • If the layer is quantized and no dequantizer block is provided, the tensor’s bytes are copied directly into a data object (the bytes are implicitly interpreted as uint8_t values).

    • If the layer is quantized and a dequantizer block is provided, the tensor’s bytes are interpreted as uint8_t values, passed to the dequantizer block, and the resulting float_t bytes are copied into a data object.

    Declaration

    Objective-C

    - (nullable instancetype)initWithBytes:(nonnull const void *)bytes
                                    length:(NSUInteger)length
                               description:
                                   (nonnull id<TIOLayerDescription>)description;

    Swift

    init?(bytes: UnsafeRawPointer, length: UInt, description: TIOLayerDescription)

    Parameters

    bytes

    The output buffer to read from.

    length

    The length of the buffer.

    description

    A description of the data this buffer produces.

    Return Value

    instancetype An instance of NSData.

  • Request to fill a tensor with bytes.

    Bytes are copied according to the following rules, with information about quantization taken from the description:

    • If the layer is unquantized, the data’s bytes are copied directly to the buffer (and implicitly interpreted as float_t values).

    • If the layer is quantized and no quantizer block is provided, the data’s bytes are copied directly to the buffer (and implicitly interpreted as uint8_t values).

    • If the layer is quantized and a quantizer block is provided, the data’s bytes are interpreted as float_t values, passed to the quantizer block, and the uint8_t values returned from it are copied to the buffer.

    Declaration

    Objective-C

    - (void)getBytes:(nonnull void *)buffer
              length:(NSUInteger)length
         description:(nonnull id<TIOLayerDescription>)description;

    Swift

    func getBytes(_ buffer: UnsafeMutableRawPointer, length: UInt, description: TIOLayerDescription)

    Parameters

    buffer

    The input buffer to copy bytes to.

    length

    The length of the input buffer.

    description

    A description of the data this buffer expects.