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

Sarah/feature/constant operation support #62

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions include/intel_npu_acceleration_library/nn_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,11 @@ class ModelFactory : public intel_npu_acceleration_library::OVInferenceModel {
*
* @param dtype element type of the tensor constant
* @param shape shape of the tensor constant
* @param values vector of literals for initializing the tensor constant
* @param dst data pointer of the tensor constant
* @return ov::op::Op*
*/
template <typename T>
ov::op::Op* constant(ov::element::Type_t dtype, std::vector<size_t> shape, std::vector<T>& values) {
auto constant = std::make_shared<ov::opset1::Constant>(dtype, ov::Shape(shape), values);
ov::op::Op* constant(ov::element::Type_t dtype, std::vector<size_t> shape, const void* dst) {
auto constant = std::make_shared<ov::opset1::Constant>(dtype, ov::Shape(shape), dst);
operations.push_back(constant);
return constant.get();
}
Expand Down
9 changes: 9 additions & 0 deletions intel_npu_acceleration_library/backend/bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ def init_network_factory(lib: ctypes.CDLL):
lib.parameter.argtypes = [handler, ctypes.c_int, c_u32_array, ctypes.c_char_p]
lib.parameter.restype = handler

lib.constant.argtypes = [
handler,
ctypes.c_int,
c_u32_array,
ctypes.c_char_p,
ctypes.c_void_p,
]
lib.constant.restype = handler

lib.compile.argtypes = [handler, handler]
lib.compile.restype = handler

Expand Down
19 changes: 19 additions & 0 deletions intel_npu_acceleration_library/backend/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,25 @@ def parameter(
self._mm, shape_ptr.size, shape_ptr, self.get_backend_dtype(dtype)
)

def constant(
self,
data: np.array,
) -> ctypes._Pointer:
"""Generate a model input constant.

Args:
data (np.array): Input numpy data array

Returns:
ctypes._Pointer: an instance to a constant object

"""
dst = data.ctypes.data_as(ctypes.c_void_p)
shape_ptr = np.array(data.shape, dtype=np.uint32)
return backend_lib.constant(
self._mm, shape_ptr.size, shape_ptr, self.get_backend_dtype(data.dtype), dst
)

def convolution(
self,
input_node: ctypes._Pointer,
Expand Down
7 changes: 7 additions & 0 deletions src/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ intel_npu_acceleration_library_DLL_API ov::op::Op* parameter(intel_npu_accelerat
return factory->parameter(shape, ov_dtype);
}

intel_npu_acceleration_library_DLL_API ov::op::Op* constant(intel_npu_acceleration_library::ModelFactory* factory,
size_t size, unsigned int* data, char* dtype, void* dst) {
ov::element::Type_t ov_dtype = intel_npu_acceleration_library::dtype_from_string(std::string(dtype));
std::vector<size_t> shape(data, data + size);
return factory->constant(ov_dtype, shape, dst);
}

intel_npu_acceleration_library_DLL_API ov::op::Op* matmul(intel_npu_acceleration_library::ModelFactory* factory,
ov::op::Op* in0, ov::op::Op* in1) {
return factory->matmul(in0, in1);
Expand Down
28 changes: 27 additions & 1 deletion test/python/test_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
# SPDX-License-Identifier: Apache 2.0
#

from intel_npu_acceleration_library.backend import MLP, NNFactory
from intel_npu_acceleration_library.backend import MLP, NNFactory, MatMul
from sklearn.metrics import r2_score
import numpy as np
import pytest
import torch
import itertools


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

assert 1 - r2_score(reference, out) < 0.001


@pytest.mark.parametrize("batch", [16, 128])
@pytest.mark.parametrize("hidden_dim", [256, 512])
def test_constant(batch, hidden_dim):

data = np.random.rand(batch, hidden_dim).astype(np.float16)
X = torch.rand((batch, hidden_dim)).to(torch.float16) - 0.5

model = NNFactory()
cc = model.constant(data=data)
input = model.parameter(X.shape)
output = model.eltwise_add(cc, input)
model.compile(output)
out = model.run(X.numpy())

reference = data + X.numpy()
print(out)
print(reference)

assert out.shape == reference.shape, "Output shape mismatch"
assert np.isfinite(reference).all(), "Pytorch Reference contains NaN or Inf"
assert np.isfinite(out).all(), "NPU output contains NaN or Inf"

assert 1 - r2_score(reference, out) < 0.001
Loading