Program Listing for File OpenVINO.hpp

Return to documentation for file (include/depthai/openvino/OpenVINO.hpp)

#pragma once

#include <algorithm>
#include <exception>
#include <filesystem>
#include <map>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#include "depthai/common/TensorInfo.hpp"

namespace dai {

class OpenVINO {
   public:
    enum Version { VERSION_2020_3, VERSION_2020_4, VERSION_2021_1, VERSION_2021_2, VERSION_2021_3, VERSION_2021_4, VERSION_2022_1, VERSION_UNIVERSAL };

    // Device for which the blob is compiled
    enum class Device { VPU, VPUX };
    struct Blob {
        Blob(std::vector<uint8_t> data);
        Blob(const std::filesystem::path& path);

        Version version;
        Device device;
        std::unordered_map<std::string, TensorInfo> networkInputs;
        std::unordered_map<std::string, TensorInfo> networkOutputs;
        uint32_t stageCount = 0;
        uint32_t numShaves = 0;
        uint32_t numSlices = 0;
        std::vector<uint8_t> data;
    };

    class SuperBlob {
       public:
        static constexpr size_t NUMBER_OF_PATCHES = 16;

        SuperBlob(std::vector<uint8_t> data);

        SuperBlob(const std::filesystem::path& pathToSuperBlobFile);

        dai::OpenVINO::Blob getBlobWithNumShaves(int numShaves);

       private:
        // A header in the superblob containing metadata about the blob and patches
        struct SuperBlobHeader {
            static constexpr size_t HEADER_SIZE = 1 * sizeof(uint64_t) + NUMBER_OF_PATCHES * sizeof(uint64_t);

            static SuperBlobHeader fromData(const std::vector<uint8_t>& data);

            int64_t blobSize;
            std::vector<int64_t> patchSizes;
        };

        // Read the SuperBlob file into memory
        std::vector<uint8_t> readSuperBlobFile(const std::filesystem::path& path);

        // Get a pointer to the first byte of the blob data
        const uint8_t* getBlobDataPointer();

        // Get the size in bytes of the blob data
        int64_t getBlobDataSize();

        // Get a pointer to the first byte of the patch data for a specific number of shaves
        const uint8_t* getPatchDataPointer(int numShaves);

        // Get the size in bytes of the patch data for a specific number of shaves
        int64_t getPatchDataSize(int numShaves);

        // Load header - throw if an error occurs
        void loadAndCheckHeader();

        // Validate superblob - throw if an error occurs
        void validateSuperblob();

        SuperBlobHeader header;
        std::vector<uint8_t> data;
    };

    constexpr static const Version DEFAULT_VERSION = VERSION_2022_1;

    static std::vector<Version> getVersions();

    static std::string getVersionName(Version version);

    static Version parseVersionName(const std::string& versionString);

    static std::vector<Version> getBlobSupportedVersions(std::uint32_t majorVersion, std::uint32_t minorVersion);

    static Version getBlobLatestSupportedVersion(std::uint32_t majorVersion, std::uint32_t minorVersion);

    static Version getBlobVersion(std::uint32_t majorVersion, std::uint32_t minorVersion);

    static bool areVersionsBlobCompatible(Version v1, Version v2);

   private:
    static const std::map<std::pair<std::uint32_t, std::uint32_t>, Version> blobVersionToOpenvinoGuessMapping;
    static const std::map<std::pair<std::uint32_t, std::uint32_t>, std::vector<Version>> blobVersionToOpenvinoMapping;
};

}  // namespace dai