Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

3.6 KB
Raw Blame
// Copyright (C) 2018-2026 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include <openvino/runtime/core.hpp>
#include <openvino/runtime/intel_npu/level_zero/level_zero.hpp>
#include <openvino/runtime/intel_npu/properties.hpp>
#include <openvino/runtime/intel_npu/remote_properties.hpp>

int main() {
    ov::Core core;
    auto model = core.read_model("model.xml");
    auto input = model->get_parameters().at(0);

    auto compiled_model = core.compile_model(model, "NPU");
    auto npu_context = compiled_model.get_context().as<ov::intel_npu::level_zero::ZeroContext>();

    auto in_element_type = input->get_element_type();
    auto in_shape = input->get_shape();

    {
        //! [default_context_from_core]
        auto npu_context = core.get_default_context("NPU").as<ov::intel_npu::level_zero::ZeroContext>();
        // Extract raw level zero context handle from RemoteContext
        void* context_handle = npu_context.get();
        //! [default_context_from_core]
    }

    {
        //! [default_context_from_model]
        auto npu_context = compiled_model.get_context().as<ov::intel_npu::level_zero::ZeroContext>();
        // Extract raw level zero context handle from RemoteContext
        void* context_handle = npu_context.get();
        //! [default_context_from_model]
    }

    {
        //! [file_mapping]
        ov::intel_npu::FileDescriptor file_descriptor{"file_name"};
        auto remote_tensor = npu_context.create_tensor(in_element_type, in_shape, file_descriptor);
        //! [file_mapping]
    }

    {
        //! [wrap_nt_handle]
        void* shared_buffer = nullptr;
        ov::intel_npu::MemType memory_type = ov::intel_npu::MemType::SHARED_BUF;
        auto remote_tensor = npu_context.create_tensor(in_element_type, in_shape, shared_buffer, memory_type);
        //! [wrap_nt_handle]
    }

    {
        //! [import_cpu_va]
        void* standard_allocation = nullptr;
        ov::intel_npu::MemType memory_type = ov::intel_npu::MemType::CPU_VA;
        auto remote_tensor = npu_context.create_tensor(in_element_type, in_shape, standard_allocation, memory_type);
        //! [import_cpu_va]
    }

    {
        //! [import_cpu_va_import_size]
        void* standard_allocation = nullptr;
        // Size in bytes of the whole allocation, page-aligned and not smaller than the tensor size
        size_t allocation_size = 4096;
        ov::AnyMap params = {{ov::intel_npu::mem_type.name(), ov::intel_npu::MemType::CPU_VA},
                             {ov::intel_npu::mem_handle.name(), standard_allocation},
                             {ov::intel_npu::import_size.name(), allocation_size}};
        auto remote_tensor = npu_context.create_tensor(in_element_type, in_shape, params);
        //! [import_cpu_va_import_size]
    }

    {
        //! [wrap_dmabuf_fd]
        int dma_buf_fd = 0;
        ov::intel_npu::MemType memory_type = ov::intel_npu::MemType::SHARED_BUF;
        auto remote_tensor = npu_context.create_tensor(in_element_type, in_shape, dma_buf_fd, memory_type);
        //! [wrap_dmabuf_fd]
    }

    {
        //! [allocate_remote_level_zero_host]
        auto remote_tensor = npu_context.create_l0_host_tensor(in_element_type, in_shape);
        // Extract raw level zero pointer from remote tensor
        void* level_zero_ptr = remote_tensor.get();
        //! [allocate_remote_level_zero_host]
    }

    {
        //! [allocate_level_zero_host]
        auto tensor = npu_context.create_host_tensor(in_element_type, in_shape);
        // Extract raw level zero pointer from remote tensor
        void* level_zero_ptr = tensor.data();
        //! [allocate_level_zero_host]
    }

    return 0;
}