type.h Source File

type.h Source File#

SDK qb Runtime Library: type.h Source File
SDK qb Runtime Library v1.4
MCS001-KR
type.h
Go to the documentation of this file.
1// Copyright ⓒ 2019- Mobilint Inc. All rights reserved.
5
6#ifndef QBRUNTIME_TYPE_H_
7#define QBRUNTIME_TYPE_H_
8
9#include <cstdint>
10#include <string>
11#include <tuple>
12#include <vector>
13
14#include "qbruntime/export.h"
15
16namespace mobilint {
23
29QBRUNTIME_EXPORT std::string getQbRuntimeVersion();
30
36QBRUNTIME_EXPORT std::string getQbRuntimeGitVersion();
37
41QBRUNTIME_EXPORT std::string getQbRuntimeVendor();
42
46QBRUNTIME_EXPORT std::string getQbRuntimeProduct();
47
55enum class Cluster : int32_t {
56 Cluster0 = 1 << 16,
57 Cluster1 = 2 << 16,
58 Error = 0x7FFF'0000,
59};
60
68enum class Core : int32_t {
69 Core0 = 1,
70 Core1 = 2,
71 Core2 = 3,
72 Core3 = 4,
73 All = 0x0000'FFFC,
74 GlobalCore = 0x0000'FFFE,
75 Error = 0x0000'FFFF,
76};
77
85
89struct Scale {
90 std::vector<float> scale_list;
91 float scale = 0.0F;
92 bool is_uniform = false;
93 std::vector<int8_t>
95 int8_t zero_point = 0;
96 bool is_asymmetric = false;
97
104 float operator[](int i) const {
105 if (is_uniform) {
106 return scale;
107 }
108 return scale_list[i];
109 }
110};
111
118struct CoreId {
121
128 bool operator==(const CoreId& rhs) const {
129 return std::tie(cluster, core) == std::tie(rhs.cluster, rhs.core);
130 }
131
138 bool operator<(const CoreId& rhs) const {
139 return std::tie(cluster, core) < std::tie(rhs.cluster, rhs.core);
140 }
141};
142
148struct Buffer {
149 int8_t* data = nullptr;
150 uint64_t size = 0;
151};
152
165enum class CoreMode : uint8_t {
166 Single = 0,
167 Multi = 1,
168 Global = 2,
171 Auto = 0xE,
172 Error = 0xF,
173};
174
179 // clang-format off
180 uint32_t original_height = 0;
181 uint32_t original_width = 0;
182 uint32_t original_channel = 0;
183 uint32_t reshaped_height = 0;
184 uint32_t reshaped_width = 0;
185 uint32_t reshaped_channel = 0;
186 uint32_t height = 0;
187 uint32_t width = 0;
188 uint32_t channel = 0;
189 uint32_t max_height = 0;
190 uint32_t max_width = 0;
191 uint32_t max_channel = 0;
192 uint32_t max_cache_size = 0;
193 // clang-format on
194
199 uint32_t original_size() const {
201 }
202
207 uint32_t reshaped_size() const {
209 }
210
215 uint32_t size() const { return height * width * channel; }
216};
217
230class QBRUNTIME_EXPORT ModelConfig {
231public:
237
253
267 bool setSingleCoreMode(int num_cores);
268
280 bool setSingleCoreMode(std::vector<CoreId> core_ids);
281
294 bool setMultiCoreMode(std::vector<Cluster> clusters = {Cluster::Cluster0,
296
310 bool setGlobal4CoreMode(std::vector<Cluster> clusters = {Cluster::Cluster0,
312
323
331 CoreMode getCoreMode() const { return mCoreMode; }
332
341 CoreAllocationPolicy getCoreAllocationPolicy() const { return mCoreAllocationPolicy; }
342
351 int getNumCores() const { return mNumCores; }
352
368 bool forceSingleNPUBundle(int npu_bundle_index);
369
379 int getForcedNPUBundleIndex() const { return mForcedNPUBundleIndex; }
380
394 const std::vector<CoreId>& getCoreIds() const { return mCoreIds; }
395
407 const std::vector<Cluster>& getClusters() const { return mClusters; }
408
424 void setAsyncPipelineEnabled(bool enable);
425
431 bool getAsyncPipelineEnabled() const { return mAsyncPipelineEnabled; }
432
453 void setActivationSlots(int count);
454
462 int getActivationSlots() const { return mActivationSlots; }
463
464 explicit ModelConfig(int num_cores);
465
466 bool setGlobalCoreMode(std::vector<Cluster> clusters);
467
468private:
469 CoreMode mCoreMode = CoreMode::Auto;
471 std::vector<Cluster> mClusters;
472 std::vector<CoreId> mCoreIds;
473 int mNumCores;
474 int mForcedNPUBundleIndex = -1; // -1 means single npu bundle usage is not forced.
475 bool mAsyncPipelineEnabled = false;
476 int mActivationSlots = 1;
477};
478
482enum class LogLevel : char {
483 Debug = 1,
484 Info = 2,
485 Warn = 3,
486 Err = 4,
487 Fatal = 5,
488 Off = 6,
489};
490
494enum class CacheType : uint8_t { Default = 0, Batch, Error = 0x0F };
495
499struct CacheInfo {
500 CacheType cache_type = CacheType::Error;
501 std::string name;
502 std::string layer_hash;
503 uint64_t size = 0;
504 size_t num_batches = 0;
505};
506
510enum class DataType : uint8_t { Float32 = 0, Float16, Int8, Uint8, BInt16, Error = 0x0F };
511
516 int sequence_length;
517 int cache_size;
518 int cache_id;
519};
520
521QBRUNTIME_EXPORT void setLogLevel(LogLevel level);
522
535QBRUNTIME_EXPORT bool startTracingEvents(const char* path);
536
543QBRUNTIME_EXPORT void stopTracingEvents();
544
562QBRUNTIME_EXPORT std::string getModelSummary(const std::string& mxq_path);
563
570QBRUNTIME_EXPORT std::vector<std::string> getAvailableDevices();
571
579QBRUNTIME_EXPORT std::vector<int> getAvailableDeviceNumbers();
580
588QBRUNTIME_EXPORT std::vector<int> getAvailableDeviceNumbers(
589 const std::string& device_name);
590
592
593} // namespace mobilint
594
595#endif
bool setGlobal8CoreMode()
Sets the model to use global8-core mode for inference.
bool setGlobalCoreMode(std::vector< Cluster > clusters)
CoreMode getCoreMode() const
Gets the core mode to be applied to the model.
Definition type.h:331
bool getAsyncPipelineEnabled() const
Returns whether the asynchronous pipeline is enabled in this configuration.
Definition type.h:431
bool setGlobal4CoreMode(std::vector< Cluster > clusters={Cluster::Cluster0, Cluster::Cluster1})
Sets the model to use global4-core mode for inference with a specified set of NPU clusters.
bool setAutoCoreMode()
Sets the model to detect CoreMode automatically.
void setAsyncPipelineEnabled(bool enable)
Enables or disables the asynchronous pipeline required for asynchronous inference.
ModelConfig(int num_cores)
bool setSingleCoreMode(int num_cores)
Sets the model to use single-core mode for inference with a specified number of local cores.
ModelConfig()
Default constructor. This default-constructed object is initially set to auto-core mode.
bool setMultiCoreMode(std::vector< Cluster > clusters={Cluster::Cluster0, Cluster::Cluster1})
Sets the model to use multi-core mode for batch inference.
int getActivationSlots() const
Returns activation buffer slot count.
Definition type.h:462
const std::vector< CoreId > & getCoreIds() const
Returns the list of NPU CoreIds to be used for model inference.
Definition type.h:394
bool setSingleCoreMode(std::vector< CoreId > core_ids)
Sets the model to use single-core mode for inference with a specific set of NPU local cores.
CoreAllocationPolicy getCoreAllocationPolicy() const
Gets the core allocation policy to be applied to the model.
Definition type.h:341
bool forceSingleNPUBundle(int npu_bundle_index)
Forces the use of a specific NPU bundle.
int getNumCores() const
Gets the number of cores to be allocated for the model.
Definition type.h:351
int getForcedNPUBundleIndex() const
Retrieves the index of the forced NPU bundle.
Definition type.h:379
const std::vector< Cluster > & getClusters() const
Returns the list of clusters to be used for model inference.
Definition type.h:407
void setActivationSlots(int count)
Sets activation buffer slots for multi-activation supported model.
DataType
DataType.
Definition type.h:510
QBRUNTIME_EXPORT std::string getQbRuntimeProduct()
QBRUNTIME_EXPORT bool startTracingEvents(const char *path)
Starts event tracing and prepares to save the trace log to a specified file.
QBRUNTIME_EXPORT std::vector< int > getAvailableDeviceNumbers()
Get the number of available NPU devices.
QBRUNTIME_EXPORT std::string getModelSummary(const std::string &mxq_path)
Generates a structured summary of the specified MXQ model.
Cluster
Enumerates clusters in the ARIES NPU.
Definition type.h:55
QBRUNTIME_EXPORT void stopTracingEvents()
Stops event tracing and writes the recorded trace log.
QBRUNTIME_EXPORT std::string getQbRuntimeVendor()
CacheType
CacheType.
Definition type.h:494
LogLevel
LogLevel.
Definition type.h:482
CoreMode
Defines the core mode for NPU execution.
Definition type.h:165
QBRUNTIME_EXPORT std::string getQbRuntimeGitVersion()
Retrieves the Git commit hash of the qbruntime.
QBRUNTIME_EXPORT std::string getQbRuntimeVersion()
Retrieves the version of the qbruntime.
QBRUNTIME_EXPORT std::vector< std::string > getAvailableDevices()
Get the device names of the NPU devices detected on the system.
CoreAllocationPolicy
Core allocation policy.
Definition type.h:81
Core
Enumerates cores within a cluster in the ARIES NPU.
Definition type.h:68
Struct containing BatchLLM parameters.
Definition type.h:515
Struct representing input/output buffer information.
Definition type.h:178
uint32_t original_height
Definition type.h:180
uint32_t max_cache_size
Definition type.h:192
uint32_t width
Definition type.h:187
uint32_t reshaped_channel
Definition type.h:185
uint32_t height
Definition type.h:186
uint32_t reshaped_height
Definition type.h:183
uint32_t original_size() const
Returns the total size of the original input/output.
Definition type.h:199
uint32_t max_channel
Definition type.h:191
uint32_t original_channel
Definition type.h:182
uint32_t channel
Definition type.h:188
uint32_t reshaped_size() const
Returns the total size of the reshaped input/output.
Definition type.h:207
uint32_t max_width
Definition type.h:190
uint32_t size() const
Returns the total size of the NPU input/output.
Definition type.h:215
uint32_t original_width
Definition type.h:181
uint32_t max_height
Definition type.h:189
uint32_t reshaped_width
Definition type.h:184
A simple byte-sized buffer.
Definition type.h:148
uint64_t size
Definition type.h:150
int8_t * data
Definition type.h:149
Struct representing KV-cache information.
Definition type.h:499
Represents a unique identifier for an NPU core.
Definition type.h:118
bool operator<(const CoreId &rhs) const
Compares two CoreId objects for ordering.
Definition type.h:138
bool operator==(const CoreId &rhs) const
Checks if two CoreId objects are equal.
Definition type.h:128
Cluster cluster
Definition type.h:119
Struct for scale values.
Definition type.h:89
float operator[](int i) const
Returns the scale value at the specified index.
Definition type.h:104
int8_t zero_point
Definition type.h:95
float scale
Definition type.h:91
std::vector< int8_t > zero_points
Definition type.h:94
bool is_uniform
Definition type.h:92
bool is_asymmetric
Definition type.h:96
std::vector< float > scale_list
Definition type.h:90