model.h Source File

model.h Source File#

SDK qb Runtime Library: model.h Source File
SDK qb Runtime Library v0.28
MCS001-KR
model.h
Go to the documentation of this file.
1// Copyright ⓒ 2019- Mobilint Inc. All rights reserved.
5
6#ifndef MACCEL_MODEL_H_
7#define MACCEL_MODEL_H_
8
9#include <cstdint>
10#ifndef _MSC_VER
11#include <experimental/propagate_const>
12#endif
13#include <memory>
14#include <string>
15#include <vector>
16
17#include "maccel/export.h"
18#include "maccel/future.h"
19#include "maccel/ndarray.h"
20#include "maccel/status_code.h"
21#include "maccel/type.h"
22
23namespace mobilint {
24
29
30class Accelerator;
31class ModelImpl;
32
39class MACCEL_EXPORT Model {
40public:
55 static std::unique_ptr<Model> create(const std::string& mxq_path, StatusCode& sc);
56
72 static std::unique_ptr<Model> create(const std::string& mxq_path,
73 const ModelConfig& config, StatusCode& sc);
74
75 Model(const Model& other) = delete;
76 Model(Model&& other) noexcept;
77 Model& operator=(const Model& rhs) = delete;
78 Model& operator=(Model&& rhs) noexcept;
79 ~Model();
80
89 StatusCode launch(Accelerator& acc);
90
100
107
116 bool isTarget(CoreId core_id) const;
117
123 std::vector<CoreId> getTargetCores() const;
124
142
153 StatusCode infer(const std::vector<NDArray<float>>& input,
154 std::vector<NDArray<float>>& output);
155
167 std::vector<NDArray<float>> infer(const std::vector<NDArray<float>>& input,
168 StatusCode& sc);
169
181 StatusCode infer(const std::vector<float*>& input,
182 std::vector<std::vector<float>>& output);
183
198 std::vector<std::vector<float>> infer(const std::vector<float*>& input,
199 StatusCode& sc);
200
217 StatusCode infer(const std::vector<float*>& input,
218 std::vector<std::vector<float>>& output,
219 const std::vector<std::vector<int64_t>>& shape);
236 std::vector<std::vector<float>> infer(const std::vector<float*>& input,
237 const std::vector<std::vector<int64_t>>& shape,
238 StatusCode& sc);
239
253 StatusCode infer(const std::vector<NDArray<float>>& input,
254 std::vector<NDArray<float>>& output, uint32_t cache_size);
255
272 std::vector<NDArray<float>> infer(const std::vector<NDArray<float>>& input,
273 uint32_t cache_size, StatusCode& sc);
274
290 StatusCode infer(const std::vector<float*>& input,
291 std::vector<std::vector<float>>& output,
292 const std::vector<std::vector<int64_t>>& shape, uint32_t cache_size);
293
312 std::vector<std::vector<float>> infer(const std::vector<float*>& input,
313 const std::vector<std::vector<int64_t>>& shape,
314 uint32_t cache_size, StatusCode& sc);
315
317
342
353 StatusCode inferCHW(const std::vector<NDArray<float>>& input,
354 std::vector<NDArray<float>>& output);
355
367 std::vector<NDArray<float>> inferCHW(const std::vector<NDArray<float>>& input,
368 StatusCode& sc);
369
381 StatusCode inferCHW(const std::vector<float*>& input,
382 std::vector<std::vector<float>>& output);
383
398 std::vector<std::vector<float>> inferCHW(const std::vector<float*>& input,
399 StatusCode& sc);
400
417 StatusCode inferCHW(const std::vector<float*>& input,
418 std::vector<std::vector<float>>& output,
419 const std::vector<std::vector<int64_t>>& shape);
420
437 std::vector<std::vector<float>> inferCHW(
438 const std::vector<float*>& input, const std::vector<std::vector<int64_t>>& shape,
439 StatusCode& sc);
440
454 StatusCode inferCHW(const std::vector<NDArray<float>>& input,
455 std::vector<NDArray<float>>& output, uint32_t cache_size);
456
473 std::vector<NDArray<float>> inferCHW(const std::vector<NDArray<float>>& input,
474 uint32_t cache_size, StatusCode& sc);
475
491 StatusCode inferCHW(const std::vector<float*>& input,
492 std::vector<std::vector<float>>& output,
493 const std::vector<std::vector<int64_t>>& shape,
494 uint32_t cache_size);
495
514 std::vector<std::vector<float>> inferCHW(
515 const std::vector<float*>& input, const std::vector<std::vector<int64_t>>& shape,
516 uint32_t cache_size, StatusCode& sc);
517
519
532
533 StatusCode infer(const std::vector<NDArray<int8_t>>& input,
534 std::vector<NDArray<int8_t>>& output);
535 std::vector<NDArray<int8_t>> infer(const std::vector<NDArray<int8_t>>& input,
536 StatusCode& sc);
537 StatusCode infer(const std::vector<int8_t*>& input,
538 std::vector<std::vector<int8_t>>& output);
539 std::vector<std::vector<int8_t>> infer(const std::vector<int8_t*>& input,
540 StatusCode& sc);
541 StatusCode infer(const std::vector<int8_t*>& input,
542 std::vector<std::vector<int8_t>>& output,
543 const std::vector<std::vector<int64_t>>& shape);
544 std::vector<std::vector<int8_t>> infer(const std::vector<int8_t*>& input,
545 const std::vector<std::vector<int64_t>>& shape,
546 StatusCode& sc);
547
548 StatusCode infer(const std::vector<NDArray<int8_t>>& input,
549 std::vector<NDArray<int8_t>>& output, uint32_t cache_size);
550 std::vector<NDArray<int8_t>> infer(const std::vector<NDArray<int8_t>>& input,
551 uint32_t cache_size, StatusCode& sc);
552 StatusCode infer(const std::vector<int8_t*>& input,
553 std::vector<std::vector<int8_t>>& output,
554 const std::vector<std::vector<int64_t>>& shape, uint32_t cache_size);
555 std::vector<std::vector<int8_t>> infer(const std::vector<int8_t*>& input,
556 const std::vector<std::vector<int64_t>>& shape,
557 uint32_t cache_size, StatusCode& sc);
558
560
573 StatusCode inferCHW(const std::vector<NDArray<int8_t>>& input,
574 std::vector<NDArray<int8_t>>& output);
575 std::vector<NDArray<int8_t>> inferCHW(const std::vector<NDArray<int8_t>>& input,
576 StatusCode& sc);
577 StatusCode inferCHW(const std::vector<int8_t*>& input,
578 std::vector<std::vector<int8_t>>& output);
579 std::vector<std::vector<int8_t>> inferCHW(const std::vector<int8_t*>& input,
580 StatusCode& sc);
581 StatusCode inferCHW(const std::vector<int8_t*>& input,
582 std::vector<std::vector<int8_t>>& output,
583 const std::vector<std::vector<int64_t>>& shape);
584 std::vector<std::vector<int8_t>> inferCHW(
585 const std::vector<int8_t*>& input, const std::vector<std::vector<int64_t>>& shape,
586 StatusCode& sc);
587
588 StatusCode inferCHW(const std::vector<NDArray<int8_t>>& input,
589 std::vector<NDArray<int8_t>>& output, uint32_t cache_size);
590 std::vector<NDArray<int8_t>> inferCHW(const std::vector<NDArray<int8_t>>& input,
591 uint32_t cache_size, StatusCode& sc);
592 StatusCode inferCHW(const std::vector<int8_t*>& input,
593 std::vector<std::vector<int8_t>>& output,
594 const std::vector<std::vector<int64_t>>& shape,
595 uint32_t cache_size);
596 std::vector<std::vector<int8_t>> inferCHW(
597 const std::vector<int8_t*>& input, const std::vector<std::vector<int64_t>>& shape,
598 uint32_t cache_size, StatusCode& sc);
600
613 std::vector<NDArray<float>> inferToFloat(const std::vector<NDArray<int8_t>>& input,
614 StatusCode& sc);
615 std::vector<std::vector<float>> inferToFloat(const std::vector<int8_t*>& input,
616 StatusCode& sc);
617 std::vector<std::vector<float>> inferToFloat(
618 const std::vector<int8_t*>& input, const std::vector<std::vector<int64_t>>& shape,
619 StatusCode& sc);
620
621 std::vector<NDArray<float>> inferToFloat(const std::vector<NDArray<int8_t>>& input,
622 uint32_t cache_size, StatusCode& sc);
623 std::vector<std::vector<float>> inferToFloat(
624 const std::vector<int8_t*>& input, const std::vector<std::vector<int64_t>>& shape,
625 uint32_t cache_size, StatusCode& sc);
627
640 std::vector<NDArray<float>> inferCHWToFloat(const std::vector<NDArray<int8_t>>& input,
641 StatusCode& sc);
642 std::vector<std::vector<float>> inferCHWToFloat(const std::vector<int8_t*>& input,
643 StatusCode& sc);
644 std::vector<std::vector<float>> inferCHWToFloat(
645 const std::vector<int8_t*>& input, const std::vector<std::vector<int64_t>>& shape,
646 StatusCode& sc);
647
648 std::vector<NDArray<float>> inferCHWToFloat(const std::vector<NDArray<int8_t>>& input,
649 uint32_t cache_size, StatusCode& sc);
650 std::vector<std::vector<float>> inferCHWToFloat(
651 const std::vector<int8_t*>& input, const std::vector<std::vector<int64_t>>& shape,
652 uint32_t cache_size, StatusCode& sc);
654
672 StatusCode inferBuffer(const std::vector<Buffer>& input, std::vector<Buffer>& output,
673 const std::vector<std::vector<int64_t>>& shape = {},
674 uint32_t cache_size = 0);
675 StatusCode inferBuffer(const std::vector<std::vector<Buffer>>& input,
676 std::vector<std::vector<Buffer>>& output,
677 const std::vector<std::vector<int64_t>>& shape = {},
678 uint32_t cache_size = 0);
680
695 StatusCode inferBufferToFloat(const std::vector<Buffer>& input,
696 std::vector<NDArray<float>>& output,
697 const std::vector<std::vector<int64_t>>& shape = {},
698 uint32_t cache_size = 0);
699 StatusCode inferBufferToFloat(const std::vector<std::vector<Buffer>>& input,
700 std::vector<NDArray<float>>& output,
701 const std::vector<std::vector<int64_t>>& shape = {},
702 uint32_t cache_size = 0);
703 StatusCode inferBufferToFloat(const std::vector<Buffer>& input,
704 std::vector<std::vector<float>>& output,
705 const std::vector<std::vector<int64_t>>& shape = {},
706 uint32_t cache_size = 0);
707 StatusCode inferBufferToFloat(const std::vector<std::vector<Buffer>>& input,
708 std::vector<std::vector<float>>& output,
709 const std::vector<std::vector<int64_t>>& shape = {},
710 uint32_t cache_size = 0);
712
721
768
780 Future inferAsync(const std::vector<NDArray<float>>& input, StatusCode& sc);
781
793 Future inferAsyncCHW(const std::vector<NDArray<float>>& input, StatusCode& sc);
794
796
818
819 // Acquire buffer
820 std::vector<Buffer> acquireInputBuffer(
821 const std::vector<std::vector<int>>& seqlens = {}) const;
822 std::vector<Buffer> acquireOutputBuffer(
823 const std::vector<std::vector<int>>& seqlens = {}) const;
824 std::vector<std::vector<Buffer>> acquireInputBuffers(
825 const int batch_size, const std::vector<std::vector<int>>& seqlens = {}) const;
826 std::vector<std::vector<Buffer>> acquireOutputBuffers(
827 const int batch_size, const std::vector<std::vector<int>>& seqlens = {}) const;
828
829 // Deallocate acquired Input/Output buffer
830 StatusCode releaseBuffer(std::vector<Buffer>& buffer) const;
831 StatusCode releaseBuffers(std::vector<std::vector<Buffer>>& buffers) const;
832
833 // Reposition single batch
834 StatusCode repositionInputs(const std::vector<float*>& input,
835 std::vector<Buffer>& input_buf,
836 const std::vector<std::vector<int>>& seqlens = {}) const;
837 StatusCode repositionOutputs(const std::vector<Buffer>& output_buf,
838 std::vector<float*>& output,
839 const std::vector<std::vector<int>>& seqlens = {}) const;
840 StatusCode repositionOutputs(const std::vector<Buffer>& output_buf,
841 std::vector<std::vector<float>>& output,
842 const std::vector<std::vector<int>>& seqlens = {}) const;
843
844 // Reposition multiple batches
845 StatusCode repositionInputs(const std::vector<float*>& input,
846 std::vector<std::vector<Buffer>>& input_buf,
847 const std::vector<std::vector<int>>& seqlens = {}) const;
848 StatusCode repositionOutputs(const std::vector<std::vector<Buffer>>& output_buf,
849 std::vector<float*>& output,
850 const std::vector<std::vector<int>>& seqlens = {}) const;
851 StatusCode repositionOutputs(const std::vector<std::vector<Buffer>>& output_buf,
852 std::vector<std::vector<float>>& output,
853 const std::vector<std::vector<int>>& seqlens = {}) const;
854
856
862 const std::vector<std::vector<int64_t>>& getModelInputShape() const;
863
869 const std::vector<std::vector<int64_t>>& getModelOutputShape() const;
870
876 const std::vector<BufferInfo>& getInputBufferInfo() const;
877
883 const std::vector<BufferInfo>& getOutputBufferInfo() const;
884
890 std::vector<Scale> getInputScale() const;
891
897 std::vector<Scale> getOutputScale() const;
898
907 uint32_t getIdentifier() const;
908
914 std::string getModelPath() const;
915
922
929
939 StatusCode dumpCacheMemory(std::vector<int8_t>& buf);
940
950 std::vector<int8_t> dumpCacheMemory(StatusCode& sc);
951
961 StatusCode dumpCacheMemory(const std::string& cache_path);
962
972 StatusCode loadCacheMemory(const std::vector<int8_t>& buf);
973
983 StatusCode loadCacheMemory(const std::string& cache_path);
984
998 int filterCacheTail(int cache_size, int tail_size, const std::vector<bool>& mask,
999 StatusCode& sc);
1000
1014 int moveCacheTail(int num_head, int num_tail, int cache_size, StatusCode& sc);
1015
1017
1024
1028 StatusCode infer(const std::vector<float*>& input,
1029 std::vector<std::vector<float>>& output, int batch_size);
1030
1034 std::vector<std::vector<float>> infer(const std::vector<float*>& input,
1035 int batch_size, StatusCode& sc);
1036
1040 StatusCode inferHeightBatch(const std::vector<float*>& input,
1041 std::vector<std::vector<float>>& output,
1042 int height_batch_size);
1043
1048
1053
1058
1062 uint64_t getLatencyConsumed(const int npu_op_idx) const;
1063
1067 uint64_t getLatencyFinished(const int npu_op_idx) const;
1068
1072 std::shared_ptr<Statistics> getStatistics() const;
1073
1075
1076private:
1077 Model();
1078
1079#ifndef _MSC_VER
1080 std::experimental::propagate_const<std::unique_ptr<ModelImpl>> mImpl;
1081#else
1082 std::unique_ptr<ModelImpl> mImpl;
1083#endif
1084
1085 friend class Accelerator;
1086};
1087
1089
1090} // namespace mobilint
1091
1092#endif
Represents an accelerator, i.e., an NPU, used for executing models.
Definition acc.h:66
Represents a future for retrieving the result of asynchronous inference.
Definition future.h:41
Configures a core mode and core allocation of a model for NPU inference.
Definition type.h:257
std::string getModelPath() const
Returns the path to the MXQ model file associated with the Model.
uint64_t getLatencyConsumed(const int npu_op_idx) const
std::vector< std::vector< float > > infer(const std::vector< float * > &input, int batch_size, StatusCode &sc)
StatusCode inferHeightBatch(const std::vector< float * > &input, std::vector< std::vector< float > > &output, int height_batch_size)
bool isTarget(CoreId core_id) const
Checks if the NPU core specified by CoreId is the target of the model. In other words,...
StatusCode dumpCacheMemory(std::vector< int8_t > &buf)
Dumps the KV cache memory into a buffer.
static std::unique_ptr< Model > create(const std::string &mxq_path, StatusCode &sc)
Creates a Model object from the specified MXQ model file.
std::shared_ptr< Statistics > getStatistics() const
std::vector< NDArray< float > > infer(const std::vector< NDArray< float > > &input, StatusCode &sc)
This overload differs from the above function in that it directly returns the inference results inste...
StatusCode loadCacheMemory(const std::vector< int8_t > &buf)
Loads the KV cache memory from a buffer.
StatusCode dumpCacheMemory(const std::string &cache_path)
Dumps the KV cache memory to a file.
SchedulePolicy getSchedulePolicy() const
std::vector< Scale > getOutputScale() const
Returns the output scale of the model.
const std::vector< std::vector< int64_t > > & getModelOutputShape() const
Returns the output shape of the model.
StatusCode launch(Accelerator &acc)
Launches the model on the specified Accelerator, which represents the actual NPU.
LatencySetPolicy getLatencySetPolicy() const
std::vector< CoreId > getTargetCores() const
Returns the NPU cores the model is configured to use.
std::vector< std::vector< float > > inferCHW(const std::vector< float * > &input, StatusCode &sc)
This overload is provided for convenience but may result in additional data copies within the maccel ...
std::vector< std::vector< float > > inferCHW(const std::vector< float * > &input, const std::vector< std::vector< int64_t > > &shape, StatusCode &sc)
This overload is provided for convenience but may result in additional data copies within the maccel ...
const std::vector< BufferInfo > & getInputBufferInfo() const
Returns the input buffer information for the model.
int moveCacheTail(int num_head, int num_tail, int cache_size, StatusCode &sc)
Moves the tail of the KV cache memory to the end of the head.
StatusCode inferSpeedrun()
Development-only API for measuring pure NPU inference speed.
std::vector< std::vector< float > > infer(const std::vector< float * > &input, const std::vector< std::vector< int64_t > > &shape, uint32_t cache_size, StatusCode &sc)
This overload supports inference with KV cache.
StatusCode infer(const std::vector< NDArray< float > > &input, std::vector< NDArray< float > > &output)
Performs inference.
CoreMode getCoreMode() const
Retrieves the core mode of the model.
uint32_t getIdentifier() const
Returns the model's unique identifier.
std::vector< NDArray< float > > inferCHW(const std::vector< NDArray< float > > &input, StatusCode &sc)
This overload differs from the above function in that it directly returns the inference results inste...
Future inferAsync(const std::vector< NDArray< float > > &input, StatusCode &sc)
Initiates asynchronous inference with input in NHWC (batch N, height H, width W, channels C) or HWC f...
std::vector< NDArray< float > > infer(const std::vector< NDArray< float > > &input, uint32_t cache_size, StatusCode &sc)
This overload supports inference with KV cache.
StatusCode dispose()
Disposes of the model loaded onto the NPU.
StatusCode inferCHW(const std::vector< NDArray< float > > &input, std::vector< NDArray< float > > &output, uint32_t cache_size)
This overload supports inference with KV cache.
static std::unique_ptr< Model > create(const std::string &mxq_path, const ModelConfig &config, StatusCode &sc)
Creates a Model object from the specified MXQ model file and configuration.
int filterCacheTail(int cache_size, int tail_size, const std::vector< bool > &mask, StatusCode &sc)
Filter the tail of the KV cache memory.
const std::vector< std::vector< int64_t > > & getModelInputShape() const
Returns the input shape of the model.
void resetCacheMemory()
Resets the KV cache memory.
std::vector< NDArray< float > > inferCHW(const std::vector< NDArray< float > > &input, uint32_t cache_size, StatusCode &sc)
This overload supports inference with KV cache.
MaintenancePolicy getMaintenancePolicy() const
Future inferAsyncCHW(const std::vector< NDArray< float > > &input, StatusCode &sc)
Initiates asynchronous inference with input in NCHW (batch N, channels C, height H,...
StatusCode infer(const std::vector< float * > &input, std::vector< std::vector< float > > &output)
This overload is provided for convenience but may result in additional data copies within the maccel ...
const std::vector< BufferInfo > & getOutputBufferInfo() const
Returns the output buffer information of the model.
StatusCode infer(const std::vector< float * > &input, std::vector< std::vector< float > > &output, const std::vector< std::vector< int64_t > > &shape, uint32_t cache_size)
This overload supports inference with KV cache.
StatusCode infer(const std::vector< NDArray< float > > &input, std::vector< NDArray< float > > &output, uint32_t cache_size)
This overload supports inference with KV cache.
std::vector< std::vector< float > > infer(const std::vector< float * > &input, StatusCode &sc)
This overload is provided for convenience but may result in additional data copies within the maccel ...
std::vector< int8_t > dumpCacheMemory(StatusCode &sc)
Dumps the KV cache memory into a buffer.
std::vector< std::vector< float > > inferCHW(const std::vector< float * > &input, const std::vector< std::vector< int64_t > > &shape, uint32_t cache_size, StatusCode &sc)
This overload supports inference with KV cache.
std::vector< std::vector< float > > infer(const std::vector< float * > &input, const std::vector< std::vector< int64_t > > &shape, StatusCode &sc)
This overload is provided for convenience but may result in additional data copies within the maccel ...
StatusCode infer(const std::vector< float * > &input, std::vector< std::vector< float > > &output, int batch_size)
StatusCode loadCacheMemory(const std::string &cache_path)
Loads the KV cache memory from a file.
StatusCode inferCHW(const std::vector< float * > &input, std::vector< std::vector< float > > &output)
This overload is provided for convenience but may result in additional data copies within the maccel ...
std::vector< Scale > getInputScale() const
Returns the input scale of the model.
StatusCode inferCHW(const std::vector< NDArray< float > > &input, std::vector< NDArray< float > > &output)
Performs inference.
StatusCode infer(const std::vector< float * > &input, std::vector< std::vector< float > > &output, const std::vector< std::vector< int64_t > > &shape)
This overload is provided for convenience but may result in additional data copies within the maccel ...
StatusCode inferCHW(const std::vector< float * > &input, std::vector< std::vector< float > > &output, const std::vector< std::vector< int64_t > > &shape)
This overload is provided for convenience but may result in additional data copies within the maccel ...
uint64_t getLatencyFinished(const int npu_op_idx) const
StatusCode inferCHW(const std::vector< float * > &input, std::vector< std::vector< float > > &output, const std::vector< std::vector< int64_t > > &shape, uint32_t cache_size)
This overload supports inference with KV cache.
A class representing an N-dimensional array (NDArray).
Definition ndarray.h:77
MaintenancePolicy
Definition type.h:94
SchedulePolicy
Definition type.h:76
CoreMode
Defines the core mode for NPU execution.
Definition type.h:193
StatusCode
Enumerates status codes for the maccel runtime.
Definition status_code.h:24
LatencySetPolicy
Definition type.h:86
Represents a unique identifier for an NPU core.
Definition type.h:147