aboutsummaryrefslogtreecommitdiff
path: root/tensorflow_lite_support/java/src/java/org/tensorflow/lite/support/tensorbuffer/TensorBufferFloat.java
blob: 65bbd7d0e35a68d6a1e0c760dbeb2e7ad2c9d84a (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
/* 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 java.nio.FloatBuffer;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.tensorflow.lite.DataType;
import org.tensorflow.lite.support.common.SupportPreconditions;

/** Represents data buffer with float values. */
public final class TensorBufferFloat extends TensorBuffer {
  private static final DataType DATA_TYPE = DataType.FLOAT32;

  /**
   * Creates a {@link TensorBufferFloat} with specified {@code shape}.
   *
   * @throws NullPointerException if {@code shape} is null.
   * @throws IllegalArgumentException if {@code shape} has non-positive elements.
   */
  TensorBufferFloat(@NonNull int[] shape) {
    super(shape);
  }

  TensorBufferFloat() {
    super();
  }

  @Override
  public DataType getDataType() {
    return DATA_TYPE;
  }

  @Override
  @NonNull
  public float[] getFloatArray() {
    buffer.rewind();
    float[] arr = new float[flatSize];

    FloatBuffer floatBuffer = buffer.asFloatBuffer();
    floatBuffer.get(arr);
    return arr;
  }

  @Override
  public float getFloatValue(int absIndex) {
    return buffer.getFloat(absIndex << 2);
  }

  @Override
  @NonNull
  public int[] getIntArray() {
    buffer.rewind();
    float[] floatArr = new float[flatSize];
    buffer.asFloatBuffer().get(floatArr);

    int[] intArr = new int[flatSize];
    for (int i = 0; i < flatSize; i++) {
      intArr[i] = (int) floatArr[i];
    }
    return intArr;
  }

  @Override
  public int getIntValue(int absIndex) {
    return (int) buffer.getFloat(absIndex << 2);
  }

  @Override
  public int getTypeSize() {
    return DATA_TYPE.byteSize();
  }

  @Override
  public void loadArray(@NonNull float[] src, @NonNull int[] shape) {
    SupportPreconditions.checkNotNull(src, "The array to be loaded cannot be null.");
    SupportPreconditions.checkArgument(
        src.length == computeFlatSize(shape),
        "The size of the array to be loaded does not match the specified shape.");
    resize(shape);
    buffer.rewind();

    FloatBuffer floatBuffer = buffer.asFloatBuffer();
    floatBuffer.put(src);
  }

  @Override
  public void loadArray(@NonNull int[] src, @NonNull int[] shape) {
    SupportPreconditions.checkNotNull(src, "The array to be loaded cannot be null.");
    SupportPreconditions.checkArgument(
        src.length == computeFlatSize(shape),
        "The size of the array to be loaded does not match the specified shape.");
    resize(shape);
    buffer.rewind();

    float[] floatArray = new float[src.length];
    int cnt = 0;
    for (int a : src) {
      floatArray[cnt++] = (float) a;
    }
    buffer.asFloatBuffer().put(floatArray);
  }
}