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

Commit f04d499

Browse files
Add prelu and normalize ops (#107)
1 parent 0ebb47b commit f04d499

File tree

4 files changed

+73
-2
lines changed

4 files changed

+73
-2
lines changed

include/intel_npu_acceleration_library/nn_factory.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,19 @@ class ModelFactory : public intel_npu_acceleration_library::OVInferenceModel {
914914
return power.get();
915915
}
916916

917+
/**
918+
* @brief Create a new prelu operation
919+
*
920+
* @param x1 operation's input node
921+
* @param slope operation's slope
922+
* @return ov::op::Op*
923+
*/
924+
ov::op::Op* prelu(ov::op::Op* x1, ov::op::Op* slope) {
925+
auto power = std::make_shared<ov::op::v0::PRelu>(x1->output(0), slope->output(0));
926+
operations.push_back(power);
927+
return power.get();
928+
}
929+
917930
/**
918931
* @brief Create a new log softmax operation
919932
*

intel_npu_acceleration_library/backend/ops.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def get_supported_ops() -> List[SupportedOp]:
5757
SupportedOp(name="log_act", inputs=1),
5858
SupportedOp(name="negative", inputs=1),
5959
SupportedOp(name="relu", inputs=1),
60+
SupportedOp(name="prelu", inputs=2),
6061
SupportedOp(name="sigmoid", inputs=1),
6162
SupportedOp(name="sign", inputs=1),
6263
SupportedOp(name="sin_act", inputs=1),

intel_npu_acceleration_library/nn/functional.py

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,15 +443,45 @@ def layer_norm(
443443
"""
444444
axis = input.shape.index(normalized_shape[0])
445445
ln = generate_op([input], "normL2", axis, eps)
446-
if weight:
446+
if weight is not None:
447447
ln = ln * weight
448448

449-
if bias:
449+
if bias is not None:
450450
ln = ln + bias
451451

452452
return ln
453453

454454

455+
@implements(torch.nn.functional.normalize)
456+
def normalize(
457+
input: Tensor,
458+
p: float = 2.0,
459+
dim: int = 1,
460+
eps: float = 1e-12,
461+
out: Optional[Tensor] = None,
462+
) -> Tensor:
463+
"""Return the normalized tensor.
464+
465+
Args:
466+
input (Tensor): The input tensor.
467+
p (float): The power value. Defaults to 2.0.
468+
dim (int): The dim to normalize. Defaults to 1.
469+
eps (float): The epsilon value. Defaults to 1e-12.
470+
out (Optional[Tensor], optional): Output tensor. Defaults to None.
471+
472+
Raises:
473+
NotImplementedError: p != 2 is not supported yet
474+
475+
Returns:
476+
Tensor: Output tensor.
477+
"""
478+
if p != 2:
479+
raise NotImplementedError("p != 2 is not supported yet")
480+
481+
out = generate_op([input], "normL2", dim, eps)
482+
return out
483+
484+
455485
@implements(torch.ceil)
456486
def ceil(x: Tensor, out: Optional[Tensor] = None) -> Tensor:
457487
"""Return the ceil of a tensor element-wise.
@@ -814,6 +844,20 @@ def relu(x: Tensor, inplace=False) -> Tensor:
814844
return out
815845

816846

847+
@implements(torch.nn.functional.prelu)
848+
def prelu(x: Tensor, weight: Tensor) -> Tensor:
849+
"""Return the parametric relu of a tensor element-wise.
850+
851+
Args:
852+
x (Tensor): The input tensor.
853+
weight (Tensor): The weights tensor.
854+
855+
Returns:
856+
Tensor: Output tensor.
857+
"""
858+
return generate_op([x, weight], "prelu")
859+
860+
817861
@implements(torch.nn.functional.sigmoid)
818862
def sigmoid(x: Tensor) -> Tensor:
819863
"""Return the sigmoid of a tensor element-wise.
@@ -955,6 +999,10 @@ def adaptive_avg_pool2d(input: Tensor, output_size: Sequence[int]):
955999
Returns:
9561000
Tensor: Output tensor.
9571001
"""
1002+
if output_size == 1:
1003+
return generate_op(
1004+
[input], "reduce_mean", reduction_axes=[-2, -1], keep_dims=True
1005+
)
9581006
return generate_op([input, output_size], "adaptive_avg_pool")
9591007

9601008

@@ -977,6 +1025,10 @@ def adaptive_max_pool2d(
9771025
"""
9781026
if return_indices:
9791027
raise NotImplementedError("return_indices is not supported yet")
1028+
if output_size == 1:
1029+
return generate_op(
1030+
[input], "reduce_max", reduction_axes=[-2, -1], keep_dims=True
1031+
)
9801032
return generate_op([input, output_size], "adaptive_max_pool")
9811033

9821034

src/bindings.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,11 @@ intel_npu_acceleration_library_DLL_API ov::op::Op* relu(intel_npu_acceleration_l
262262
return factory->relu(in0);
263263
}
264264

265+
intel_npu_acceleration_library_DLL_API ov::op::Op* prelu(intel_npu_acceleration_library::ModelFactory* factory,
266+
ov::op::Op* in0, ov::op::Op* in1) {
267+
return factory->prelu(in0, in1);
268+
}
269+
265270
intel_npu_acceleration_library_DLL_API ov::op::Op* sigmoid(intel_npu_acceleration_library::ModelFactory* factory,
266271
ov::op::Op* in0) {
267272
return factory->sigmoid(in0);

0 commit comments

Comments
 (0)