Skip to content

Commit 3459bfd

Browse files
committed
feat: enabled splitting of group convolution for bias=False
1 parent 252f57c commit 3459bfd

1 file changed

Lines changed: 32 additions & 13 deletions

File tree

backends/nxp/aten_passes/split_group_convolution.py

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,27 @@ def _create_convolution_node(self, conv_target, args: tuple) -> Node:
111111
# Compute the output shapes for the `convolution`, and assign the `val` meta.
112112
with FakeTensorMode() as mode:
113113
input_shapes = [
114-
input_.meta["val"].shape if hasattr(input_, "meta") else input_.shape
114+
(
115+
input_.meta["val"].shape
116+
if hasattr(input_, "meta")
117+
else input_.shape if input_ is not None else None
118+
)
115119
for input_ in args[:3]
116120
]
117121
input_dtypes = [
118-
input_.meta["val"].dtype if hasattr(input_, "meta") else input_.dtype
122+
(
123+
input_.meta["val"].dtype
124+
if hasattr(input_, "meta")
125+
else input_.dtype if input_ is not None else None
126+
)
119127
for input_ in args[:3]
120128
]
121129
fake_inputs = [
122-
FakeTensor.from_tensor(torch.empty(shape, dtype=dtype), mode)
130+
(
131+
FakeTensor.from_tensor(torch.empty(shape, dtype=dtype), mode)
132+
if shape is not None and dtype is not None
133+
else None
134+
)
123135
for shape, dtype in zip(input_shapes, input_dtypes)
124136
]
125137
output = conv_target(*fake_inputs, *args[3:])
@@ -211,8 +223,11 @@ def _is_conv(node_: Node):
211223

212224
w_data = self._get_tensor_constant_from_node(w)
213225
b_data = self._get_tensor_constant_from_node(b)
214-
if w_data is None or b_data is None:
215-
continue # Only the standard case with static weights and bias is supported.
226+
227+
with_bias = b is not None
228+
# Only the standard case with static weights and static bias (or bias=False) is supported.
229+
if w_data is None or (b_data is None and with_bias):
230+
continue
216231

217232
# Create a `split` node to split the main input.
218233
# Split across dimension `1` (channels), `groups` slices of size `input_split_size`.
@@ -227,10 +242,9 @@ def _is_conv(node_: Node):
227242
for i in range(groups)
228243
]
229244

230-
# Split the weights and bias, across dimension `0`, slices of size `weight_split_size`.
245+
# Split the weights across dimension `0`, slices of size `weight_split_size`.
231246
weight_split_size = w.meta["val"].shape[0] // groups
232247
split_weights_data = torch.split(w_data, weight_split_size, 0)
233-
split_bias_data = torch.split(b_data, weight_split_size, 0)
234248

235249
# Turn the weights and biases into parameter nodes containing the data.
236250
# Use a different name for every parameter. The function internally ensures the name's uniqueness, but
@@ -241,12 +255,17 @@ def _is_conv(node_: Node):
241255
)
242256
for i, weight_data in enumerate(split_weights_data)
243257
]
244-
split_bias_nodes = [
245-
self._create_parameter_node_for_data(
246-
bias_data, b.name + f"_{i}_", split_node
247-
)
248-
for i, bias_data in enumerate(split_bias_data)
249-
]
258+
259+
if with_bias:
260+
split_bias_data = torch.split(b_data, weight_split_size, 0)
261+
split_bias_nodes = [
262+
self._create_parameter_node_for_data(
263+
bias_data, b.name + f"_{i}_", split_node
264+
)
265+
for i, bias_data in enumerate(split_bias_data)
266+
]
267+
else:
268+
split_bias_nodes = [None] * len(split_weight_nodes)
250269

251270
# Create the `conv` nodes.
252271
with self.module.graph.inserting_after(

0 commit comments

Comments
 (0)