Skip to content

Commit d525b2c

Browse files
Small fixes
1 parent 82ba3f0 commit d525b2c

3 files changed

Lines changed: 144 additions & 37 deletions

File tree

dpbench/benchmarks/deformable_convolution/__init__.py

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,5 @@
22
#
33
# SPDX-License-Identifier: Apache 2.0
44

5-
from .deformable_convolution_initialize import initialize
6-
from .deformable_convolution_numba_npr import (
7-
deformable_convolution as deformable_convolution_numba_npr,
8-
)
9-
from .deformable_convolution_numpy import (
10-
deformable_convolution as deformable_convolution_numpy,
11-
)
12-
from .deformable_convolution_sycl_native_ext import deformable_convolution_sycl
13-
14-
__all__ = [
15-
"initialize",
16-
"deformable_convolution_numba_npr",
17-
"deformable_convolution_numba_numpy",
18-
"deformable_convolution_sycl",
19-
]
20-
21-
"""l2-norm calculation of n vectors
22-
23-
Input
24-
---------
25-
npoints: int
26-
number of vectors
27-
dims: int
28-
dimension of single vector
29-
seed: int
30-
random seed to generate random number
31-
32-
Output
33-
-------
34-
d: array
35-
l2 norm of each vector
36-
37-
Method
38-
------
39-
||Vj||2=sqrt(sum(Xj[i]*Xj[i]))
40-
here i is 0->dims, j is 0->npoints
5+
"""Deformable convolution
416
"""
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# Copyright 2022 Intel Corp.
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
import math
6+
7+
import numpy as np
8+
from numba import prange
9+
from numba_mlir import njit
10+
11+
12+
@njit(parallel=True, inline="always", fastmath=True)
13+
def bilinear(input, offset_y, offset_x):
14+
height, width = input.shape
15+
start_x = int(math.floor(offset_x))
16+
start_x_weight = 1 - (offset_x - start_x)
17+
start_y = int(math.floor(offset_y))
18+
start_y_weight = 1 - (offset_y - start_y)
19+
20+
output = 0
21+
if (
22+
offset_x >= width
23+
or offset_y >= height
24+
or offset_x <= -1
25+
or offset_y <= -1
26+
):
27+
return output
28+
29+
if start_x >= 0 and start_y >= 0:
30+
w = start_x_weight * start_y_weight
31+
output += w * input[start_y, start_x]
32+
33+
if start_x + 1 < width and start_y >= 0:
34+
w = (1 - start_x_weight) * start_y_weight
35+
output += w * input[start_y, start_x + 1]
36+
37+
if start_x >= 0 and start_y + 1 < height:
38+
w = start_x_weight * (1 - start_y_weight)
39+
output += w * input[start_y + 1, start_x]
40+
41+
if start_x + 1 < width and start_y + 1 < height:
42+
w = (1 - start_x_weight) * (1 - start_y_weight)
43+
output += w * input[start_y + 1, start_x + 1]
44+
45+
return output
46+
47+
48+
@njit(parallel=True, fastmath=True)
49+
def deform(
50+
input, offset, output, stride, pad, dilation, groups, deformable_groups
51+
):
52+
k_height, k_width, _, out_height, out_width = offset.shape
53+
channels, _, _ = input.shape
54+
55+
k_h_m = (k_height - 1) // 2
56+
k_w_m = (k_width - 1) // 2
57+
58+
for ckhkw in prange(channels * k_height * k_width):
59+
for h in prange(out_height):
60+
for w in prange(out_width):
61+
c = ckhkw // (k_height * k_width)
62+
khkw = ckhkw % (k_height * k_width)
63+
kh = khkw // k_width
64+
kw = khkw % k_width
65+
66+
offset_y = (
67+
offset[kh, kw, 1, h, w]
68+
+ h * stride[0]
69+
+ (kh - k_h_m) * dilation[0]
70+
- (pad[0] - k_h_m)
71+
)
72+
offset_x = (
73+
offset[kh, kw, 0, h, w]
74+
+ w * stride[1]
75+
+ (kw - k_w_m) * dilation[1]
76+
- (pad[1] - k_w_m)
77+
)
78+
79+
output[c, kh, kw, h, w] = bilinear(input[c], offset_y, offset_x)
80+
81+
82+
@njit(parallel=True, fastmath=True)
83+
def deformable_convolution_b1(
84+
input,
85+
output,
86+
offset,
87+
weights,
88+
bias,
89+
tmp,
90+
stride,
91+
pad,
92+
dilation,
93+
groups,
94+
deformable_groups,
95+
):
96+
out_channels, height, width = output.shape
97+
_, in_channels, k_height, k_width = weights.shape
98+
99+
deform(input, offset, tmp, stride, pad, dilation, groups, deformable_groups)
100+
101+
tmp = tmp.reshape((in_channels * k_height * k_width, height * width))
102+
103+
_weights = weights.reshape((out_channels, in_channels * k_height * k_width))
104+
_output = output.reshape((out_channels, height * width))
105+
np.dot(_weights, tmp, _output)
106+
107+
_bias = bias.reshape((out_channels, 1))
108+
_output[:] = _output + _bias
109+
110+
111+
@njit(parallel=True)
112+
def deformable_convolution(
113+
input,
114+
output,
115+
offset,
116+
weights,
117+
bias,
118+
tmp,
119+
stride_y,
120+
stride_x,
121+
pad_y,
122+
pad_x,
123+
dilation_y,
124+
dilation_x,
125+
groups,
126+
deformable_groups,
127+
):
128+
batch, _, _, _ = input.shape
129+
for b in range(batch):
130+
deformable_convolution_b1(
131+
input[b],
132+
output[b],
133+
offset,
134+
weights,
135+
bias,
136+
tmp,
137+
(stride_y, stride_x),
138+
(pad_y, pad_x),
139+
(dilation_y, dilation_x),
140+
groups,
141+
deformable_groups,
142+
)

dpbench/config/reader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from .implementation_postfix import Implementation
2020
from .module import Module
2121

22-
_REFERENCE_IMPLEMENTATIONS = {"numpy", "python"}
22+
_REFERENCE_IMPLEMENTATIONS = {"numpy", "python", "sycl"}
2323

2424

2525
def read_configs( # noqa: C901: TODO: move modules into config

0 commit comments

Comments
 (0)