Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit 74e2360

Browse files
Add c++ examples (#86)
1 parent 0ac709b commit 74e2360

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

docs/source/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ Site map
9191

9292
.. toctree::
9393
developer.md
94-
adding_operation.md
94+
adding_operations.md
9595
:maxdepth: 1
9696
:caption: Developements guide:
9797

examples/cpp/main.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ int main() {
1717
// create parameter
1818
auto input = factory->parameter({batch, inC}, ov::element::f16);
1919
auto weights = factory->parameter({outC, inC}, ov::element::f16);
20+
auto bias = factory->parameter({1, outC}, ov::element::f16);
2021

2122
// create matmul
2223
auto matmul = factory->matmul(input, weights);
24+
auto matmul_bias = factory->eltwise_add(matmul, bias);
25+
factory->result(matmul_bias);
2326

2427
// Compile the model
25-
factory->compile(matmul);
28+
factory->compile();
2629

2730
// Save OV model
2831
std::cout << "Saving model to matmul.xml" << std::endl;
@@ -31,14 +34,17 @@ int main() {
3134
// Here you can create float16 buffers and run inference by using
3235
half_ptr input_buffer = new uint16_t[batch * inC];
3336
half_ptr weights_buffer = new uint16_t[outC * inC];
37+
half_ptr bias_buffer = new uint16_t[outC];
3438
half_ptr output_buffer = new uint16_t[batch * outC];
3539

36-
memset(input_buffer, 0, 128 * 256 * sizeof(uint16_t));
37-
memset(weights_buffer, 0, 128 * 256 * sizeof(uint16_t));
38-
memset(output_buffer, 0, 128 * 512 * sizeof(uint16_t));
40+
memset(input_buffer, 0, batch * inC * sizeof(uint16_t));
41+
memset(weights_buffer, 0, outC * inC * sizeof(uint16_t));
42+
memset(output_buffer, 0, batch * outC * sizeof(uint16_t));
43+
memset(bias_buffer, 0, outC * sizeof(uint16_t));
3944

4045
factory->setInputTensor(input_buffer, 0);
4146
factory->setInputTensor(weights_buffer, 1);
47+
factory->setInputTensor(bias_buffer, 2);
4248
factory->setOutputTensor(output_buffer, 0);
4349

4450
// Run inference
@@ -49,6 +55,7 @@ int main() {
4955

5056
delete[] input_buffer;
5157
delete[] weights_buffer;
58+
delete[] bias_buffer;
5259
delete[] output_buffer;
5360
return 0;
5461
}

include/intel_npu_acceleration_library/nn_factory.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,22 @@ class ModelFactory : public intel_npu_acceleration_library::OVInferenceModel {
9090
return matmul.get();
9191
}
9292

93+
/**
94+
* @brief Create a new linear operation
95+
*
96+
* @param input matmul lhs input
97+
* @param weights matmul rhs input, a.k.a. weights
98+
* @param bias matmul bias input
99+
* @return ov::op::Op*
100+
*/
101+
ov::op::Op* linear(ov::op::Op* input, ov::op::Op* weights, ov::op::Op* bias) {
102+
auto mm_op = matmul(input, weights);
103+
if (bias != nullptr) {
104+
return eltwise_add(mm_op, bias);
105+
}
106+
return mm_op;
107+
}
108+
93109
/**
94110
* @brief Create a new convolution operation
95111
*

0 commit comments

Comments
 (0)