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

Commit 8e2172d

Browse files
SarahByrneIntelSarahByrneIntel
andauthored
Sarah/feature/constant operation support (#62)
* Adding support for constant operator * Fix for constant operation * Adding fix for constant operator * adding support and test for constant operation * Adding tests for constant operation --------- Co-authored-by: SarahByrneIntel <[email protected]>
1 parent fa4c82a commit 8e2172d

File tree

5 files changed

+65
-5
lines changed

5 files changed

+65
-5
lines changed

include/intel_npu_acceleration_library/nn_factory.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,11 @@ class ModelFactory : public intel_npu_acceleration_library::OVInferenceModel {
5050
*
5151
* @param dtype element type of the tensor constant
5252
* @param shape shape of the tensor constant
53-
* @param values vector of literals for initializing the tensor constant
53+
* @param dst data pointer of the tensor constant
5454
* @return ov::op::Op*
5555
*/
56-
template <typename T>
57-
ov::op::Op* constant(ov::element::Type_t dtype, std::vector<size_t> shape, std::vector<T>& values) {
58-
auto constant = std::make_shared<ov::opset1::Constant>(dtype, ov::Shape(shape), values);
56+
ov::op::Op* constant(ov::element::Type_t dtype, std::vector<size_t> shape, const void* dst) {
57+
auto constant = std::make_shared<ov::opset1::Constant>(dtype, ov::Shape(shape), dst);
5958
operations.push_back(constant);
6059
return constant.get();
6160
}

intel_npu_acceleration_library/backend/bindings.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,15 @@ def init_network_factory(lib: ctypes.CDLL):
106106
lib.parameter.argtypes = [handler, ctypes.c_int, c_u32_array, ctypes.c_char_p]
107107
lib.parameter.restype = handler
108108

109+
lib.constant.argtypes = [
110+
handler,
111+
ctypes.c_int,
112+
c_u32_array,
113+
ctypes.c_char_p,
114+
ctypes.c_void_p,
115+
]
116+
lib.constant.restype = handler
117+
109118
lib.compile.argtypes = [handler, handler]
110119
lib.compile.restype = handler
111120

intel_npu_acceleration_library/backend/factory.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,25 @@ def parameter(
9292
self._mm, shape_ptr.size, shape_ptr, self.get_backend_dtype(dtype)
9393
)
9494

95+
def constant(
96+
self,
97+
data: np.array,
98+
) -> ctypes._Pointer:
99+
"""Generate a model input constant.
100+
101+
Args:
102+
data (np.array): Input numpy data array
103+
104+
Returns:
105+
ctypes._Pointer: an instance to a constant object
106+
107+
"""
108+
dst = data.ctypes.data_as(ctypes.c_void_p)
109+
shape_ptr = np.array(data.shape, dtype=np.uint32)
110+
return backend_lib.constant(
111+
self._mm, shape_ptr.size, shape_ptr, self.get_backend_dtype(data.dtype), dst
112+
)
113+
95114
def convolution(
96115
self,
97116
input_node: ctypes._Pointer,

src/bindings.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,13 @@ intel_npu_acceleration_library_DLL_API ov::op::Op* parameter(intel_npu_accelerat
126126
return factory->parameter(shape, ov_dtype);
127127
}
128128

129+
intel_npu_acceleration_library_DLL_API ov::op::Op* constant(intel_npu_acceleration_library::ModelFactory* factory,
130+
size_t size, unsigned int* data, char* dtype, void* dst) {
131+
ov::element::Type_t ov_dtype = intel_npu_acceleration_library::dtype_from_string(std::string(dtype));
132+
std::vector<size_t> shape(data, data + size);
133+
return factory->constant(ov_dtype, shape, dst);
134+
}
135+
129136
intel_npu_acceleration_library_DLL_API ov::op::Op* matmul(intel_npu_acceleration_library::ModelFactory* factory,
130137
ov::op::Op* in0, ov::op::Op* in1) {
131138
return factory->matmul(in0, in1);

test/python/test_layers.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
# SPDX-License-Identifier: Apache 2.0
44
#
55

6-
from intel_npu_acceleration_library.backend import MLP, NNFactory
6+
from intel_npu_acceleration_library.backend import MLP, NNFactory, MatMul
77
from sklearn.metrics import r2_score
88
import numpy as np
99
import pytest
1010
import torch
11+
import itertools
1112

1213

1314
class MLP_PT(torch.nn.Module):
@@ -240,3 +241,28 @@ def test_activation(batch, hidden_dim, activation):
240241
assert np.isfinite(out).all(), "NPU output contains NaN or Inf"
241242

242243
assert 1 - r2_score(reference, out) < 0.001
244+
245+
246+
@pytest.mark.parametrize("batch", [16, 128])
247+
@pytest.mark.parametrize("hidden_dim", [256, 512])
248+
def test_constant(batch, hidden_dim):
249+
250+
data = np.random.rand(batch, hidden_dim).astype(np.float16)
251+
X = torch.rand((batch, hidden_dim)).to(torch.float16) - 0.5
252+
253+
model = NNFactory()
254+
cc = model.constant(data=data)
255+
input = model.parameter(X.shape)
256+
output = model.eltwise_add(cc, input)
257+
model.compile(output)
258+
out = model.run(X.numpy())
259+
260+
reference = data + X.numpy()
261+
print(out)
262+
print(reference)
263+
264+
assert out.shape == reference.shape, "Output shape mismatch"
265+
assert np.isfinite(reference).all(), "Pytorch Reference contains NaN or Inf"
266+
assert np.isfinite(out).all(), "NPU output contains NaN or Inf"
267+
268+
assert 1 - r2_score(reference, out) < 0.001

0 commit comments

Comments
 (0)