type.h Source File

type.h Source File#

SDK qb Runtime Library: type.h Source File
SDK qb Runtime Library v1.3
MCS001-EN
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
45QBRUNTIME_EXPORT std::string getQbRuntimeVendor();
46
55QBRUNTIME_EXPORT std::string getQbRuntimeProduct();
56
64enum class Cluster : int32_t {
65 Cluster0 = 1 << 16,
66 Cluster1 = 2 << 16,
67 Error = 0x7FFF'0000,
68};
69
77enum class Core : int32_t {
78 Core0 = 1,
79 Core1 = 2,
80 Core2 = 3,
81 Core3 = 4,
82 All = 0x0000'FFFC,
83 GlobalCore = 0x0000'FFFE,
84 Error = 0x0000'FFFF,
85};
86
94
98struct Scale {
99 std::vector<float> scale_list;
100 float scale = 0.0F;
101 bool is_uniform = false;
102 std::vector<int8_t>
104 int8_t zero_point = 0;
105 bool is_asymmetric = false;
106
113 float operator[](int i) const {
114 if (is_uniform) {
115 return scale;
116 }
117 return scale_list[i];
118 }
119};
120
127struct CoreId {
130
137 bool operator==(const CoreId& rhs) const {
138 return std::tie(cluster, core) == std::tie(rhs.cluster, rhs.core);
139 }
140
147 bool operator<(const CoreId& rhs) const {
148 return std::tie(cluster, core) < std::tie(rhs.cluster, rhs.core);
149 }
150};
151
157struct Buffer {
158 int8_t* data = nullptr;
159 uint64_t size = 0;
160};
161
174enum class CoreMode : uint8_t {
175 Single = 0,
176 Multi = 1,
177 Global = 2,
180 Auto = 0xE,
181 Error = 0xF,
182};
183
188 // clang-format off
189 uint32_t original_height = 0;
190 uint32_t original_width = 0;
191 uint32_t original_channel = 0;
192 uint32_t reshaped_height = 0;
193 uint32_t reshaped_width = 0;
194 uint32_t reshaped_channel = 0;
195 uint32_t height = 0;
196 uint32_t width = 0;
197 uint32_t channel = 0;
198 uint32_t max_height = 0;
199 uint32_t max_width = 0;
200 uint32_t max_channel = 0;
201 uint32_t max_cache_size = 0;
202 // clang-format on
203
208 uint32_t original_size() const {
210 }
211
216 uint32_t reshaped_size() const {
218 }
219
224 uint32_t size() const { return height * width * channel; }
225};
226
239class QBRUNTIME_EXPORT ModelConfig {
240public:
246
260
274 bool setSingleCoreMode(int num_cores);
275
287 bool setSingleCoreMode(std::vector<CoreId> core_ids);
288
301 bool setMultiCoreMode(std::vector<Cluster> clusters = {Cluster::Cluster0,
303
317 bool setGlobal4CoreMode(std::vector<Cluster> clusters = {Cluster::Cluster0,
319
330
338 CoreMode getCoreMode() const { return mCoreMode; }
339
348 CoreAllocationPolicy getCoreAllocationPolicy() const { return mCoreAllocationPolicy; }
349
358 int getNumCores() const { return mNumCores; }
359
375 bool forceSingleNPUBundle(int npu_bundle_index);
376
386 int getForcedNPUBundleIndex() const { return mForcedNPUBundleIndex; }
387
398 const std::vector<CoreId>& getCoreIds() const { return mCoreIds; }
399
400 const std::vector<Cluster>& getClusters() const { return mClusters; }
401
417 void setAsyncPipelineEnabled(bool enable);
418
424 bool getAsyncPipelineEnabled() const { return mAsyncPipelineEnabled; }
425
446 void setActivationSlots(int count);
447
455 int getActivationSlots() const { return mActivationSlots; }
456
457 explicit ModelConfig(int num_cores);
458
459 bool setGlobalCoreMode(std::vector<Cluster> clusters);
460
464 std::vector<uint64_t> early_latencies;
468 std::vector<uint64_t> finish_latencies;
469
470private:
471 CoreMode mCoreMode = CoreMode::Auto;
473 std::vector<Cluster> mClusters;
474 std::vector<CoreId> mCoreIds;
475 int mNumCores;
476 int mForcedNPUBundleIndex = -1; // -1 means single npu bundle usage is not forced.
477 bool mAsyncPipelineEnabled = false;
478 int mActivationSlots = 1;
479};
480
484enum class LogLevel : char {
485 Debug = 1,
486 Info = 2,
487 Warn = 3,
488 Err = 4,
489 Fatal = 5,
490 Off = 6,
491};
492
496enum class CacheType : uint8_t { Default = 0, Batch, Error = 0x0F };
497
501struct CacheInfo {
502 CacheType cache_type = CacheType::Error;
503 std::string name;
504 std::string layer_hash;
505 uint64_t size = 0;
506 size_t num_batches = 0;
507};
508
512enum class DataType : uint8_t { Float32 = 0, Float16, Int8, Uint8, Error = 0x0F };
513
518 int sequence_length;
519 int cache_size;
520 int cache_id;
521};
522
523QBRUNTIME_EXPORT void setLogLevel(LogLevel level);
524
537QBRUNTIME_EXPORT bool startTracingEvents(const char* path);
538
545QBRUNTIME_EXPORT void stopTracingEvents();
546
564QBRUNTIME_EXPORT std::string getModelSummary(const std::string& mxq_path);
565
571QBRUNTIME_EXPORT std::vector<int> getAvailableDeviceNumbers();
572
574
575} // namespace mobilint
576
577#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:338
bool getAsyncPipelineEnabled() const
Returns whether the asynchronous pipeline is enabled in this configuration.
Definition type.h:424
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:455
const std::vector< CoreId > & getCoreIds() const
Returns the list of NPU CoreIds to be used for model inference.
Definition type.h:398
std::vector< uint64_t > early_latencies
Definition type.h:464
std::vector< uint64_t > finish_latencies
Definition type.h:468
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:348
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:358
int getForcedNPUBundleIndex() const
Retrieves the index of the forced NPU bundle.
Definition type.h:386
void setActivationSlots(int count)
Sets activation buffer slots for multi-activation supported model.
DataType
DataType.
Definition type.h:512
QBRUNTIME_EXPORT std::string getQbRuntimeProduct()
Retrieves product information of the qbruntime.
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:64
QBRUNTIME_EXPORT void stopTracingEvents()
Stops event tracing and writes the recorded trace log.
QBRUNTIME_EXPORT std::string getQbRuntimeVendor()
Retrieves the vendor name of the qbruntime.
CacheType
CacheType.
Definition type.h:496
LogLevel
LogLevel.
Definition type.h:484
CoreMode
Defines the core mode for NPU execution.
Definition type.h:174
QBRUNTIME_EXPORT std::string getQbRuntimeGitVersion()
Retrieves the Git commit hash of the qbruntime.
QBRUNTIME_EXPORT std::string getQbRuntimeVersion()
Retrieves the version of the qbruntime.
CoreAllocationPolicy
Core allocation policy.
Definition type.h:90
Core
Enumerates cores within a cluster in the ARIES NPU.
Definition type.h:77
Struct containing BatchLLM parameters.
Definition type.h:517
Struct representing input/output buffer information.
Definition type.h:187
uint32_t original_height
Definition type.h:189
uint32_t max_cache_size
Definition type.h:201
uint32_t width
Definition type.h:196
uint32_t reshaped_channel
Definition type.h:194
uint32_t height
Definition type.h:195
uint32_t reshaped_height
Definition type.h:192
uint32_t original_size() const
Returns the total size of the original input/output.
Definition type.h:208
uint32_t max_channel
Definition type.h:200
uint32_t original_channel
Definition type.h:191
uint32_t channel
Definition type.h:197
uint32_t reshaped_size() const
Returns the total size of the reshaped input/output.
Definition type.h:216
uint32_t max_width
Definition type.h:199
uint32_t size() const
Returns the total size of the NPU input/output.
Definition type.h:224
uint32_t original_width
Definition type.h:190
uint32_t max_height
Definition type.h:198
uint32_t reshaped_width
Definition type.h:193
A simple byte-sized buffer.
Definition type.h:157
uint64_t size
Definition type.h:159
int8_t * data
Definition type.h:158
Struct representing KV-cache information.
Definition type.h:501
Represents a unique identifier for an NPU core.
Definition type.h:127
bool operator<(const CoreId &rhs) const
Compares two CoreId objects for ordering.
Definition type.h:147
bool operator==(const CoreId &rhs) const
Checks if two CoreId objects are equal.
Definition type.h:137
Cluster cluster
Definition type.h:128
Struct for scale values.
Definition type.h:98
float operator[](int i) const
Returns the scale value at the specified index.
Definition type.h:113
int8_t zero_point
Definition type.h:104
float scale
Definition type.h:100
std::vector< int8_t > zero_points
Definition type.h:103
bool is_uniform
Definition type.h:101
bool is_asymmetric
Definition type.h:105
std::vector< float > scale_list
Definition type.h:99