Data Serialization

The API’s below demonstrate how to write and read tensors from disk when using the quadric C++ API.

Note

Reading from and writing tensors to disk is only supported within the HOST_MAIN(...) section of a quadric C++ kernel. Please see Creating a Simple Kernel for example usage.

readTensors

template<typename ...T>
void readTensorsFromJsonFile(const std::string &filename, T&... inputs)

Reads N tensors from a json file.

Parameters
  • [in] filename: The file name

  • [out] inputs: Series of output tensors

Template Parameters
  • T: Type of tensor inputs

template<typename ...T>
void readTensorsFromBinFile(const std::string &filename, T&... inputs)

Reads N tensors from a bin file.

Writes tensors to file (JSON)

Parameters
  • [in] filename: The file name

  • [out] inputs: Series of output tensors

Template Parameters
  • T: Types of tensor inputs

Warning

Please ensure the full path of the tensor is specified. When using the docker quadric sdk, paths to data files must be adjusted to match your workspace.

Example

The following code was run from a directory with tensor.bin inside of it. Since we launched the docker quadric sdk with -w /ws and `pwd`:/ws, we must reflect this in the path of the tensor as shown below.

readTensorsFromBinFile("/ws/tensor.bin", ddrInPtr);

writeTensors

template<typename ...T>
void writeTensorsToJsonFile(const std::string &filename, const T&... inputs)

Writes tensors to file (JSON)

Parameters
  • [in] filename: The file name

  • [in] inputs: Series of input tensors

Template Parameters
  • T: Type of tensor inputs

template<typename ...T>
void writeTensorsToBinFile(const std::string &filename, const T&... inputs)

Writes N tensors to a bin file.

Parameters
  • [in] filename: The file name

  • [in] inputs: Series of input tensors

Template Parameters
  • T: Types of tensor inputs

Example Usage

  typedef DdrTensor<int, 1, 1, 8, 8> tShape;
  tShape                             t1;
  tShape::allocate(t1);
  tShape t2;
  tShape::allocate(t2);
  tShape t3;
  tShape::allocate(t3);

  std::int32_t populatedConst = 1;
  populateTensorConst(t1, populatedConst);

  writeTensorsToJsonFile("test.json", t1);
  readTensorsFromJsonFile("test.json", t2);
  bool jsonResult = compareTensors(t1, t2);
  runtime_assert(jsonResult, "testTensorWriteRead: t1 does not match t2 (JSON)");

  writeTensorsToBinFile("test.bin", t1);
  readTensorsFromBinFile("test.bin", t3);

Writing binary tensors in Python (numpy) and reading them in a kernel

The following example creates a tensor to be ingested by the Creating a Simple Kernel example. Begin by writing a tensor in python in NCHW format to a binary file as shown below.

>>> import numpy as np
>>> data = np.random.randint(0, 255, size=(1, 1, 8, 8), dtype=np.int32)
>>> data
array([[[[ 70, 185,  93,  62,  14,   3,   4, 164],
         [119, 187, 102,   3, 136, 125, 198,   3],
         [104, 124,  64,  94, 195, 213, 212,  53],
         [134, 191, 160,  36,  41,  96, 170,  87],
         [202, 186,  11,   7, 193,  90,  38,   0],
         [191,  56, 128,   1,  24,  11, 215, 215],
         [250,  17,  73,  34, 189,  53, 204, 237],
         [ 83, 138,  23,  15,  39,  55, 188,  44]]]], dtype=int32)
>>> data.tofile('numpy_data.bin')

Rather than using the populateTensorSequential(ddrInPtr); call in the example, replace that line with

readTensorsFromBinFile("/ws/numpy_data.bin", ddrInPtr);

Parsing binary tensors created by a kernel using Python (numpy)

Given you’ve successfully executed a kernel and want to visualize or post-process tensors created by your kernel which are stored natively as binary files, use the numpy library library to view them as shown below. Note how the fromfile call below utilizes the same dtype as we specified in our kernel.

>>> import numpy as np
>>> np.fromfile('simple_flow_data_output/simple_flow_data_epu.s.tensor2_iss.bin', dtype=np.int32).reshape(8, 8)
array([[ 71, 186,  94,  63,  15,   4,   5, 165],
       [120, 188, 103,   4, 137, 126, 199,   4],
       [105, 125,  65,  95, 196, 214, 213,  54],
       [135, 192, 161,  37,  42,  97, 171,  88],
       [203, 187,  12,   8, 194,  91,  39,   1],
       [192,  57, 129,   2,  25,  12, 216, 216],
       [251,  18,  74,  35, 190,  54, 205, 238],
       [ 84, 139,  24,  16,  40,  56, 189,  45]], dtype=int32)

Parsing JSON serialized tensors in Python with the json_tricks library

Tensors can be imported into a Python script for further post-processing or visualization. We accomplish this by using the json_tricks library. To install this library, run the command below

$ pip3 install json_tricks

Given you’ve written a tensor using writeTensorsToJsonFile from within the HOST_MAIN of your kernel i.e

writeTensorsToJsonFile("/ws/tensor.json", ddrInPtr);

We can read the above tensor.json using the json_tricks library as shown below in Python.

>>> import json_tricks
>>> json_tricks.load('tensor.json')
[array([[[[ 70, 185,  93,  62,  14,   3,   4, 164],
         [119, 187, 102,   3, 136, 125, 198,   3],
         [104, 124,  64,  94, 195, 213, 212,  53],
         [134, 191, 160,  36,  41,  96, 170,  87],
         [202, 186,  11,   7, 193,  90,  38,   0],
         [191,  56, 128,   1,  24,  11, 215, 215],
         [250,  17,  73,  34, 189,  53, 204, 237],
         [ 83, 138,  23,  15,  39,  55, 188,  44]]]], dtype=int32)]