summaryrefslogtreecommitdiff
path: root/compiler/nnc/tests/acl_soft_backend/artifact_cmake/main.cpp
blob: c326b390b59d881b91e3ca5b79b801b60a4a0f72 (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
/*
 * Copyright (c) 2018 Samsung Electronics Co., Ltd. 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.
 */

#include "AclArtifact.h"
#include <iostream>
#include <memory>
#include <H5Cpp.h>

using namespace std;
using namespace arm_compute;

static unique_ptr<char[]> getTensorData(CLTensor &tensor)
{
  auto buf = unique_ptr<char[]>(new char[tensor.info()->total_size()]);
  tensor.map();
  Window window;
  window.use_tensor_dimensions(tensor.info()->tensor_shape());
  Iterator i(&tensor, window);
  char *ptr = &buf[0];

  execute_window_loop(window,
                      [&i, &ptr](const Coordinates &) {
                        memcpy(ptr, i.ptr(), sizeof(float));
                        ptr += sizeof(float);
                      },
                      i);

  tensor.unmap();
  return buf;
}

static void readTensor(CLTensor &tensor, H5::DataSet &dataset)
{
  auto buf = unique_ptr<char[]>(new char[tensor.info()->total_size()]);
  dataset.read(&buf[0], H5::PredType::NATIVE_FLOAT);
  tensor.map();
  Window window;
  window.use_tensor_dimensions(tensor.info()->tensor_shape());
  Iterator i(&tensor, window);
  char *ptr = &buf[0];

  execute_window_loop(window,
                      [&i, &ptr](const Coordinates &) {
                        memcpy(i.ptr(), ptr, sizeof(float));
                        ptr += sizeof(float);
                      },
                      i);

  tensor.unmap();
}

static bool readTensorFromHDF5File(CLTensor &tensor, const string &file_name)
{
  // Read from the .hdf5 file
  try
  {
    H5::H5File h5File(file_name, H5F_ACC_RDONLY);
    auto tensor_name = h5File.getObjnameByIdx(0);
    auto dataset = h5File.openDataSet(tensor_name);
    auto dataspace = dataset.getSpace();
    auto rank = dataspace.getSimpleExtentNdims();

    if (rank < 2)
      return false;

    hsize_t dims[rank];

    if (dataspace.getSimpleExtentDims(dims) != rank)
      return false;

    TensorShape shape;
    shape.set_num_dimensions(rank - 1);

    for (int i = 1; i < rank; ++i)
      shape[rank - i - 1] = dims[i];

    readTensor(tensor, dataset);
  }
  catch (H5::FileIException &)
  {
    return false;
  }

  return true;
}

static void writeTensorToHDF5File(CLTensor &tensor, const string &tensor_name,
                                  const string &file_name)
{
  const TensorShape &orig_shape = tensor.info()->tensor_shape();
  const TensorShape &transposed_shape = orig_shape;
  int rank = transposed_shape.num_dimensions();
  hsize_t dims[rank + 1];
  dims[0] = 1;

  for (int i = 0; i < rank; ++i)
    dims[rank - i] = transposed_shape[i];

  // Write to the .hdf5 file
  H5::H5File h5File(file_name, H5F_ACC_TRUNC);
  H5::DataSpace dataspace(rank + 1, dims);
  auto dataset = h5File.createDataSet(tensor_name, H5::PredType::IEEE_F32BE, dataspace);
  dataset.write(&getTensorData(tensor)[0], H5::PredType::NATIVE_FLOAT);
}

int main(int argc, char *argv[])
{
  CLScheduler::get().default_init();

  if (!CLScheduler::get().is_initialised())
  {
    cout << "Failed to initialise the ACL scheduler" << endl;
    return 1;
  }

  AclArtifact artifact;
  CLTensor &artifact_in = artifact.getInput();
  readTensorFromHDF5File(artifact_in, "in.hdf5");

  artifact.Inference();

  CLTensor &artifact_out = artifact.getOutput();
  writeTensorToHDF5File(artifact_out, "out", "out.hdf5");

  return 0;
}