24 #include <hailo/hailort.h>
25 #include <hailo/hailort.hpp>
31 #define HAILO_CHECK(obj, msg) do { if (!obj) LFATAL(msg << ": " << obj.status() << " [" << \
32 hailo_get_status_message(obj.status()) << ']'); } while (0)
57 {
return itsInAttrs; }
61 {
return itsOutAttrs; }
74 dataroot::freeze(doit);
83 if (itsDevice)
LFATAL(
"Network already loaded... restart the module to load a new one.");
86 auto dev = hailort::Device::create_pcie();
87 if (!dev)
LFATAL(
"Failed to create PCIe device:" << dev.status());
88 itsDevice = dev.release();
109 LINFO(
"Loading HEF file " << m <<
" ...");
110 auto hef = hailort::Hef::create(m);
113 std::vector<std::string> ngn = hef->get_network_groups_names();
114 for (std::string
const & n : ngn)
LINFO(
"Network Group: " << n);
115 if (ngn.size() != 1)
LERROR(
"More than one network groups in HEF -- USING FIRST ONE");
117 auto configure_params = hef->create_configure_params(HAILO_STREAM_INTERFACE_PCIE);
118 HAILO_CHECK(configure_params,
"Could not configure params from HEF file " << m);
120 auto network_groups = itsDevice->configure(hef.value(), configure_params.value());
121 HAILO_CHECK(network_groups,
"Could not configure device");
122 if (network_groups->empty())
LFATAL(
"HEF file " << m <<
" does not contain any network groups");
123 itsNetGroup = std::move(network_groups->at(0));
126 constexpr
bool QUANTIZED =
true;
127 constexpr hailo_format_type_t FORMAT_TYPE = HAILO_FORMAT_TYPE_AUTO;
129 auto vstreams = hailort::VStreamsBuilder::create_vstreams(*itsNetGroup, QUANTIZED, FORMAT_TYPE);
130 HAILO_CHECK(vstreams,
"Failed to create vstreams");
132 itsInStreams = std::move(vstreams->first);
133 itsOutStreams = std::move(vstreams->second);
136 auto activated_network_group = itsNetGroup->activate();
137 HAILO_CHECK(activated_network_group,
"Failed activating network group");
138 itsActiveNetGroup = activated_network_group.release();
141 for (
auto const & vs : itsInStreams)
144 auto const & attr = itsInAttrs.back();
148 for (
auto const & vs : itsOutStreams)
151 auto const & attr = itsOutAttrs.back();
158 itsDevice->set_throttling_state(!
turbo::get());
164 if (itsDevice) itsDevice->set_throttling_state(! newval);
169 std::vector<std::string> & info)
171 if (blobs.size() != itsInStreams.size())
172 LFATAL(
"Received " << blobs.size() <<
" blobs, but network has " << itsInStreams.size() <<
" inputs");
175 for (
size_t i = 0; i < blobs.size(); ++i)
179 if (err.empty() ==
false)
LFATAL(err);
182 std::vector<std::future<std::string>> fvec(itsInStreams.size() + itsOutStreams.size());
185 for (uint32_t i = 0; i < itsOutStreams.size(); ++i)
186 fvec[i + itsInStreams.size()] =
jevois::async([
this](uint32_t i,
bool dq) -> std::string
188 auto const & attr = itsOutAttrs[i];
189 uint8_t * tensor_data = (uint8_t *)itsRawOutMats[i].data;
190 size_t const sz = itsRawOutMats[i].total() * itsRawOutMats[i].elemSize();
192 auto status = itsOutStreams[i].read(hailort::MemoryView(tensor_data, sz));
193 if (status != HAILO_SUCCESS)
LFATAL(
"Failed to collect output " << i <<
" from device: " << status);
202 itsOutMats[i] = itsRawOutMats[i];
209 for (
size_t b = 0; b < blobs.size(); ++b)
210 fvec[b] =
jevois::async([
this, &blobs](
size_t b) -> std::string
212 cv::Mat
const & blob = blobs[b];
213 size_t const sz = blob.total() * blob.elemSize();
216 auto status = itsInStreams[b].write(hailort::MemoryView(blob.data, sz));
217 if (status != HAILO_SUCCESS)
LFATAL(
"Failed to write input " << b <<
" data to device: " << status);
223 static std::string devstr =
"- Hailo8: ---W, ---C";
224 if ((jevois::frameNum() % 30) == 0)
226 bool throttle = itsDevice->get_throttling_state().value();
227 hailo_chip_temperature_info_t
const temp = itsDevice->get_chip_temperature().value();
228 float pwr = itsDevice->power_measurement(HAILO_DVM_OPTIONS_AUTO, HAILO_POWER_MEASUREMENT_TYPES__POWER).value();
230 devstr =
jevois::sformat(
"- Hailo8: %.1fW, %.0fC%s", pwr, temp.ts0_temperature, throttle ?
"" :
" (turbo)");
235 info.insert(info.end(), std::make_move_iterator(retvec.begin()), std::make_move_iterator(retvec.end()));
236 info.emplace_back(devstr);