aboutsummaryrefslogtreecommitdiff
path: root/tensorflow_lite_support/java/src/java/org/tensorflow/lite/support/tensorbuffer/TensorBuffer.java
blob: 446d0ea583afb27dd2635a0cbd39aaa8fb60ffb5 (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/* Copyright 2019 The TensorFlow Authors. 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.
==============================================================================*/

package org.tensorflow.lite.support.tensorbuffer;

import static org.tensorflow.lite.support.common.SupportPreconditions.checkArgument;
import static org.tensorflow.lite.support.common.SupportPreconditions.checkNotNull;
import static org.tensorflow.lite.support.common.SupportPreconditions.checkState;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.tensorflow.lite.DataType;

/** Represents the data buffer for either a model's input or its output. */
public abstract class TensorBuffer {
  /** Where the data is stored. */
  protected ByteBuffer buffer;

  /** Shape of the tensor stored in this buffer. */
  protected int[] shape;

  /** Number of elements in the buffer. It will be changed to a proper value in the constructor. */
  protected int flatSize = -1;

  /**
   * Indicator of whether this buffer is dynamic or fixed-size. Fixed-size buffers will have
   * pre-allocated memory and fixed size. While the size of dynamic buffers can be changed.
   */
  protected final boolean isDynamic;

  /**
   * Creates a {@link TensorBuffer} with specified {@code shape} and {@link DataType}. Here are some
   * examples:
   *
   * <pre>
   * Creating a float TensorBuffer with shape {2, 3}:
   * int[] shape = new int[] {2, 3};
   * TensorBuffer tensorBuffer = TensorBuffer.createFixedSize(shape, DataType.FLOAT32);
   * </pre>
   *
   * <pre>
   * Creating an uint8 TensorBuffer of a scalar:
   * int[] shape = new int[] {};
   * TensorBuffer tensorBuffer = TensorBuffer.createFixedSize(shape, DataType.UINT8);
   * </pre>
   *
   * <pre>
   * Creating an empty uint8 TensorBuffer:
   * int[] shape = new int[] {0};
   * TensorBuffer tensorBuffer = TensorBuffer.createFixedSize(shape, DataType.UINT8);
   * </pre>
   *
   * <p>The size of a fixed-size TensorBuffer cannot be changed once it is created.
   *
   * @param shape The shape of the {@link TensorBuffer} to be created.
   * @param dataType The dataType of the {@link TensorBuffer} to be created.
   * @throws NullPointerException if {@code shape} is null.
   * @throws IllegalArgumentException if {@code shape} has non-positive elements.
   */
  @NonNull
  public static TensorBuffer createFixedSize(@NonNull int[] shape, DataType dataType) {
    switch (dataType) {
      case FLOAT32:
        return new TensorBufferFloat(shape);
      case UINT8:
        return new TensorBufferUint8(shape);
      default:
        throw new AssertionError("TensorBuffer does not support data type: " + dataType);
    }
  }

  /**
   * Creates an empty dynamic {@link TensorBuffer} with specified {@link DataType}. The shape of the
   * created {@link TensorBuffer} is {0}.
   *
   * <p>Dynamic TensorBuffers will reallocate memory when loading arrays or data buffers of
   * different buffer sizes.
   *
   * @param dataType The dataType of the {@link TensorBuffer} to be created.
   */
  @NonNull
  public static TensorBuffer createDynamic(DataType dataType) {
    switch (dataType) {
      case FLOAT32:
        return new TensorBufferFloat();
      case UINT8:
        return new TensorBufferUint8();
      default:
        throw new AssertionError("TensorBuffer does not support data type: " + dataType);
    }
  }

  /**
   * Creates a {@link TensorBuffer} deep-copying data from another, with specified {@link DataType}.
   *
   * @param buffer the source {@link TensorBuffer} to copy from.
   * @param dataType the expected {@link DataType} of newly created {@link TensorBuffer}.
   * @throws NullPointerException if {@code buffer} is null.
   */
  @NonNull
  public static TensorBuffer createFrom(@NonNull TensorBuffer buffer, DataType dataType) {
    checkNotNull(buffer, "Cannot create a buffer from null");
    TensorBuffer result;
    if (buffer.isDynamic()) {
      result = createDynamic(dataType);
    } else {
      result = createFixedSize(buffer.shape, dataType);
    }
    // The only scenario we need float array is FLOAT32->FLOAT32, or we can always use INT as
    // intermediate container.
    // The assumption is not true when we support other data types.
    if (buffer.getDataType() == DataType.FLOAT32 && dataType == DataType.FLOAT32) {
      float[] data = buffer.getFloatArray();
      result.loadArray(data, buffer.shape);
    } else {
      int[] data = buffer.getIntArray();
      result.loadArray(data, buffer.shape);
    }
    return result;
  }

  /** Returns the data buffer. */
  @NonNull
  public ByteBuffer getBuffer() {
    return buffer;
  }

  /**
   * Gets the {@link TensorBuffer#flatSize} of the buffer.
   *
   * @throws IllegalStateException if the underlying data is corrupted
   */
  public int getFlatSize() {
    assertShapeIsCorect();
    return flatSize;
  }

  /**
   * Gets the current shape. (returning a copy here to avoid unexpected modification.)
   *
   * @throws IllegalStateException if the underlying data is corrupted
   */
  @NonNull
  public int[] getShape() {
    assertShapeIsCorect();
    return Arrays.copyOf(shape, shape.length);
  }

  /** Returns the data type of this buffer. */
  public abstract DataType getDataType();

  /**
   * Returns a float array of the values stored in this buffer. If the buffer is of different types
   * than float, the values will be converted into float. For example, values in {@link
   * TensorBufferUint8} will be converted from uint8 to float.
   */
  @NonNull
  public abstract float[] getFloatArray();

  /**
   * Returns a float value at a given index. If the buffer is of different types than float, the
   * value will be converted into float. For example, when reading a value from {@link
   * TensorBufferUint8}, the value will be first read out as uint8, and then will be converted from
   * uint8 to float.
   *
   * <pre>
   * For example, a TensorBuffer with shape {2, 3} that represents the following array,
   * [[0.0f, 1.0f, 2.0f], [3.0f, 4.0f, 5.0f]].
   *
   * The fourth element (whose value is 3.0f) in the TensorBuffer can be retrived by:
   * float v = tensorBuffer.getFloatValue(3);
   * </pre>
   *
   * @param absIndex The absolute index of the value to be read.
   */
  public abstract float getFloatValue(int absIndex);

  /**
   * Returns an int array of the values stored in this buffer. If the buffer is of different type
   * than int, the values will be converted into int, and loss of precision may apply. For example,
   * getting an int array from a {@link TensorBufferFloat} with values {400.32f, 23.04f}, the output
   * is {400, 23}.
   */
  @NonNull
  public abstract int[] getIntArray();

  /**
   * Returns an int value at a given index. If the buffer is of different types than int, the value
   * will be converted into int. For example, when reading a value from {@link TensorBufferFloat},
   * the value will be first read out as float, and then will be converted from float to int. Loss
   * of precision may apply.
   *
   * <pre>
   * For example, a TensorBuffer with shape {2, 3} that represents the following array,
   * [[0.0f, 1.0f, 2.0f], [3.0f, 4.0f, 5.0f]].
   *
   * The fourth element (whose value is 3.0f) in the TensorBuffer can be retrived by:
   * int v = tensorBuffer.getIntValue(3);
   * Note that v is converted from 3.0f to 3 as a result of type conversion.
   * </pre>
   *
   * @param absIndex The absolute index of the value to be read.
   */
  public abstract int getIntValue(int absIndex);

  /**
   * Returns the number of bytes of a single element in the array. For example, a float buffer will
   * return 4, and a byte buffer will return 1.
   */
  public abstract int getTypeSize();

  /** Returns if the {@link TensorBuffer} is dynamic sized (could resize arbitrarily). */
  public boolean isDynamic() {
    return isDynamic;
  }

  /**
   * Loads an int array into this buffer with specific shape. If the buffer is of different types
   * than int, the values will be converted into the buffer's type before being loaded into the
   * buffer, and loss of precision may apply. For example, loading an int array with values {400,
   * -23} into a {@link TensorBufferUint8} , the values will be clamped to [0, 255] and then be
   * casted to uint8 by {255, 0}.
   *
   * @param src The source array to be loaded.
   * @param shape Shape of the tensor that {@code src} represents.
   * @throws NullPointerException if {@code src} is null.
   * @throws NullPointerException if {@code shape} is null.
   * @throws IllegalArgumentException if the size of the array to be loaded does not match the
   *     specified shape.
   */
  public abstract void loadArray(@NonNull int[] src, @NonNull int[] shape);

  /**
   * Loads an int array into this buffer. If the buffer is of different types than int, the values
   * will be converted into the buffer's type before being loaded into the buffer, and loss of
   * precision may apply. For example, loading an int array with values {400, -23} into a {@link
   * TensorBufferUint8} , the values will be clamped to [0, 255] and then be casted to uint8 by
   * {255, 0}.
   *
   * <p>Size of {@code src} should always match the flat size of this {@link TensorBuffer}, for both
   * fixed-size and dynamic {@link TensorBuffer}.
   *
   * @param src The source array to be loaded.
   */
  public void loadArray(@NonNull int[] src) {
    loadArray(src, shape);
  }

  /**
   * Loads a float array into this buffer with specific shape. If the buffer is of different types
   * than float, the values will be converted into the buffer's type before being loaded into the
   * buffer, and loss of precision may apply. For example, loading a float array into a {@link
   * TensorBufferUint8} with values {400.32f, -23.04f}, the values will be clamped to [0, 255] and
   * then be casted to uint8 by {255, 0}.
   *
   * @param src The source array to be loaded.
   * @param shape Shape of the tensor that {@code src} represents.
   * @throws NullPointerException if {@code src} is null.
   * @throws NullPointerException if {@code shape} is null.
   * @throws IllegalArgumentException if the size of the array to be loaded does not match the
   *     specified shape.
   */
  public abstract void loadArray(@NonNull float[] src, @NonNull int[] shape);

  /**
   * Loads a float array into this buffer. If the buffer is of different types than float, the
   * values will be converted into the buffer's type before being loaded into the buffer, and loss
   * of precision may apply. For example, loading a float array into a {@link TensorBufferUint8}
   * with values {400.32f, -23.04f}, the values will be clamped to [0, 255] and then be casted to
   * uint8 by {255, 0}.
   *
   * <p>Size of {@code src} should always match the flat size of this {@link TensorBuffer}, for both
   * fixed-size and dynamic {@link TensorBuffer}.
   *
   * @param src The source array to be loaded.
   */
  public void loadArray(@NonNull float[] src) {
    loadArray(src, shape);
  }

  /**
   * Loads a byte buffer into this {@link TensorBuffer} with specific shape.
   *
   * <p>Important: The loaded buffer is a reference. DO NOT MODIFY. We don't create a copy here for
   * performance concern, but if modification is necessary, please make a copy.
   *
   * @param buffer The byte buffer to load.
   * @throws NullPointerException if {@code buffer} is null.
   * @throws IllegalArgumentException if the size of {@code buffer} and {@code typeSize} do not
   *     match or the size of {@code buffer} and {@code flatSize} do not match.
   */
  public void loadBuffer(@NonNull ByteBuffer buffer, @NonNull int[] shape) {
    checkNotNull(buffer, "Byte buffer cannot be null.");
    int flatSize = computeFlatSize(shape);
    checkArgument(
        (buffer.limit() == getTypeSize() * flatSize),
        "The size of byte buffer and the shape do not match.");

    resize(shape);
    buffer.rewind();
    this.buffer = buffer;
  }

  /**
   * Loads a byte buffer into this {@link TensorBuffer}. Buffer size must match the flat size of
   * this {@link TensorBuffer}.
   *
   * <p>Important: The loaded buffer is a reference. DO NOT MODIFY. We don't create a copy here for
   * performance concern, but if modification is necessary, please make a copy.
   *
   * @param buffer The byte buffer to load.
   */
  public void loadBuffer(@NonNull ByteBuffer buffer) {
    loadBuffer(buffer, shape);
  }

  /**
   * Constructs a fixed size {@link TensorBuffer} with specified {@code shape}.
   *
   * @throws NullPointerException if {@code shape} is null.
   * @throws IllegalArgumentException if {@code shape} has non-positive elements.
   */
  protected TensorBuffer(@NonNull int[] shape) {
    isDynamic = false;
    allocateMemory(shape);
  }

  /** Constructs a dynamic {@link TensorBuffer} which can be resized. */
  protected TensorBuffer() {
    isDynamic = true;
    // Initialize the dynamic TensorBuffer with an empty ByteBuffer.
    allocateMemory(new int[] {0});
  }

  /** Calculates number of elements in the buffer. */
  protected static int computeFlatSize(@NonNull int[] shape) {
    checkNotNull(shape, "Shape cannot be null.");
    int prod = 1;
    for (int s : shape) {
      prod = prod * s;
    }
    return prod;
  }

  /**
   * For dynamic buffer, resize the memory if needed. For fixed-size buffer, check if the {@code
   * shape} of src fits the buffer size.
   */
  protected void resize(@NonNull int[] shape) {
    if (isDynamic) {
      allocateMemory(shape);
    } else {
      // Make sure the new shape fits the buffer size when TensorBuffer has fixed size.
      checkArgument(Arrays.equals(shape, this.shape));
      this.shape = shape.clone();
    }
  }

  /**
   * Allocates buffer with corresponding size of the {@code shape}. If shape is an empty array, this
   * {@link TensorBuffer} will be created as a scalar and its flatSize will be 1.
   *
   * @throws NullPointerException if {@code shape} is null.
   * @throws IllegalArgumentException if {@code shape} has negative elements.
   */
  private void allocateMemory(@NonNull int[] shape) {
    checkNotNull(shape, "TensorBuffer shape cannot be null.");
    checkArgument(isShapeValid(shape), "Values in TensorBuffer shape should be non-negative.");

    // Check if the new shape is the same as current shape.
    int newFlatSize = computeFlatSize(shape);
    this.shape = shape.clone();
    if (flatSize == newFlatSize) {
      return;
    }

    // Update to the new shape.
    flatSize = newFlatSize;
    buffer = ByteBuffer.allocateDirect(flatSize * getTypeSize());
    buffer.order(ByteOrder.nativeOrder());
  }

  /**
   * Verifies if the shape of the {@link TensorBuffer} matched the size of the underlying {@link
   * ByteBuffer}.
   */
  private void assertShapeIsCorect() {
    int flatSize = computeFlatSize(shape);
    checkState(
        (buffer.limit() == getTypeSize() * flatSize),
        String.format(
            "The size of underlying ByteBuffer (%d) and the shape (%s) do not match. The"
                + " ByteBuffer may have been changed.",
            buffer.limit(), Arrays.toString(shape)));
  }

  /**
   * Checks if {@code shape} meets one of following two requirements: 1. Elements in {@code shape}
   * are all non-negative numbers. 2. {@code shape} is an empty array, which corresponds to scalar.
   */
  private static boolean isShapeValid(@NonNull int[] shape) {
    if (shape.length == 0) {
      // This shape refers to a scalar.
      return true;
    }

    // This shape refers to a multidimensional array.
    for (int s : shape) {
      // All elements in shape should be non-negative.
      if (s < 0) {
        return false;
      }
    }
    return true;
  }
}