Imported Upstream version 1.8.0
[platform/core/ml/nnfw.git] / compiler / circle-tensordump / src / Dump.cpp
index dfa78f0..a8d3256 100644 (file)
@@ -136,6 +136,7 @@ void DumpTensors::run(std::ostream &os, const circle::Model *model, const std::s
         auto max = quant_param->max();
         auto scale = quant_param->scale();
         auto zero_point = quant_param->zero_point();
+        auto quantized_dimension = quant_param->quantized_dimension();
 
         os << " " + print_format2 + "   ├── min        : ";
         ::print_comma_sepearted(os, min);
@@ -146,9 +147,11 @@ void DumpTensors::run(std::ostream &os, const circle::Model *model, const std::s
         os << " " + print_format2 + "   ├── scale      : ";
         ::print_comma_sepearted(os, scale);
         os << std::endl;
-        os << " " + print_format2 + "   â\94\94── zero_point : ";
+        os << " " + print_format2 + "   â\94\9c── zero_point : ";
         ::print_comma_sepearted(os, zero_point);
         os << std::endl;
+        os << " " + print_format2 + "   └── quantized_dimension : " << quantized_dimension;
+        os << std::endl;
       }
 
       // buffer
@@ -229,7 +232,7 @@ std::vector<hsize_t> hdf5_dims_cast(const flatbuffers::Vector<T> *data,
 }
 
 /**
- *  This function writes data to given hdf5 file like below.
+ *  This function writes vector data to given hdf5 file like below.
  *
  *  GROUP "group_name"
  *   ㄴDATATYPE "type"
@@ -238,9 +241,9 @@ std::vector<hsize_t> hdf5_dims_cast(const flatbuffers::Vector<T> *data,
  *   ㄴDATA "data"
  */
 template <typename T>
-void write_data_to_hdf5(H5::H5File &file, std::string &group_name, std::string dataset_name,
-                        const H5::PredType &type, const flatbuffers::Vector<T> *data,
-                        std::vector<hsize_t> dims)
+void write_vector_data_to_hdf5(H5::H5File &file, std::string &group_name, std::string dataset_name,
+                               const H5::PredType &type, const flatbuffers::Vector<T> *data,
+                               std::vector<hsize_t> dims)
 {
   if (data == nullptr)
     return;
@@ -250,6 +253,17 @@ void write_data_to_hdf5(H5::H5File &file, std::string &group_name, std::string d
   dataset->write(data->data(), type);
 }
 
+/// @brief This function writes scalar data to given hdf5 file
+template <typename T>
+void write_scalar_data_to_hdf5(H5::H5File &file, std::string &group_name, std::string dataset_name,
+                               const H5::PredType &type, T data)
+{
+  auto dataspace = std::make_unique<H5::DataSpace>(H5S_SCALAR);
+  auto dataset = std::make_unique<H5::DataSet>(
+      file.createDataSet(group_name + "/" + dataset_name, type, *dataspace));
+  dataset->write(&data, type);
+}
+
 } // namespace
 
 namespace circletensordump
@@ -297,8 +311,9 @@ void DumpTensorsToHdf5::run(std::ostream &os, const circle::Model *model,
       auto buff_data_ptr = reader.buffers()->Get(buff_idx)->data();
       if (buff_data_ptr)
       {
-        ::write_data_to_hdf5(file, group_name, "weights", ::hdf5_dtype_cast(tensor->type()),
-                             buff_data_ptr, ::hdf5_dims_cast(buff_data_ptr, tensor->shape()));
+        ::write_vector_data_to_hdf5(file, group_name, "weights", ::hdf5_dtype_cast(tensor->type()),
+                                    buff_data_ptr,
+                                    ::hdf5_dims_cast(buff_data_ptr, tensor->shape()));
       }
 
       // write quantization parameters
@@ -306,17 +321,20 @@ void DumpTensorsToHdf5::run(std::ostream &os, const circle::Model *model,
       if (quant_param)
       {
         auto min = quant_param->min();
-        ::write_data_to_hdf5(file, group_name, "min", H5::PredType::NATIVE_FLOAT, min,
-                             ::hdf5_dims_cast(min));
+        ::write_vector_data_to_hdf5(file, group_name, "min", H5::PredType::NATIVE_FLOAT, min,
+                                    ::hdf5_dims_cast(min));
         auto max = quant_param->max();
-        ::write_data_to_hdf5(file, group_name, "max", H5::PredType::NATIVE_FLOAT, max,
-                             ::hdf5_dims_cast(max));
+        ::write_vector_data_to_hdf5(file, group_name, "max", H5::PredType::NATIVE_FLOAT, max,
+                                    ::hdf5_dims_cast(max));
         auto scale = quant_param->scale();
-        ::write_data_to_hdf5(file, group_name, "scale", H5::PredType::NATIVE_FLOAT, scale,
-                             ::hdf5_dims_cast(scale));
+        ::write_vector_data_to_hdf5(file, group_name, "scale", H5::PredType::NATIVE_FLOAT, scale,
+                                    ::hdf5_dims_cast(scale));
         auto zero_point = quant_param->zero_point();
-        ::write_data_to_hdf5(file, group_name, "zero_point", H5::PredType::NATIVE_INT64, zero_point,
-                             ::hdf5_dims_cast(zero_point));
+        ::write_vector_data_to_hdf5(file, group_name, "zero_point", H5::PredType::NATIVE_INT64,
+                                    zero_point, ::hdf5_dims_cast(zero_point));
+        auto quantized_dimension = quant_param->quantized_dimension();
+        ::write_scalar_data_to_hdf5(file, group_name, "quantized_dimension",
+                                    H5::PredType::NATIVE_INT32, quantized_dimension);
       }
     }
   }