Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support conv3d on cpu for TF #19641

Merged
merged 1 commit into from
Apr 30, 2024
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
6 changes: 6 additions & 0 deletions keras/src/backend/tensorflow/nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,12 @@ def _conv_xla():
# If kernel's in_channel does not match input's channels, it indicates
# convolution is broken down into groups.
return _conv_xla()
if data_format == "channels_first" and len(inputs.shape) == 5:
inputs = convert_to_tensor(inputs)
if inputs.device.split(":")[-2] == "CPU":
inputs = tf.transpose(inputs, perm=(0, 2, 3, 4, 1))
data_format = "channels_last"
return tf.transpose(_conv(), perm=(0, 4, 1, 2, 3))
return _conv()


Expand Down
16 changes: 11 additions & 5 deletions keras/src/ops/nn_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1445,23 +1445,29 @@ def test_conv_2d_group_2(self, strides, dilation_rate):
)
self.assertAllClose(outputs, expected)

@parameterized.product(strides=(1, (1, 1, 1), 2), padding=("valid", "same"))
def test_conv_3d(self, strides, padding):
if backend.config.image_data_format() == "channels_last":
@parameterized.product(
strides=(1, (1, 1, 1), 2),
padding=("valid", "same"),
data_format=("channels_first", "channels_last"),
)
def test_conv_3d(self, strides, padding, data_format):
if data_format == "channels_last":
input_shape = (2, 8, 8, 8, 3)
else:
input_shape = (2, 3, 8, 8, 8)
inputs_3d = np.arange(3072, dtype=float).reshape(input_shape)
kernel = np.arange(162, dtype=float).reshape([3, 3, 3, 3, 2])

outputs = knn.conv(inputs_3d, kernel, strides, padding=padding)
outputs = knn.conv(
inputs_3d, kernel, strides, padding=padding, data_format=data_format
)
expected = np_conv3d(
inputs_3d,
kernel,
bias_weights=np.zeros((2,)),
strides=strides,
padding=padding,
data_format=backend.config.image_data_format(),
data_format=data_format,
dilation_rate=1,
groups=1,
)
Expand Down