Do a 3x3 Kernel search on image/feature and get max value?

Lets say I have a tensor of [B, 1, H, W].

I want to do a 3x3 kernel search on the image, and return the max value found in that kernel so possibly [B, 1, H-1, W-1]

Similarly given [B, 3, H, W], perhaps to do the same 3x3 kernel search, but compute a special function across the 3 channels. For example, to calculate some distance functions across the 3 channels, and return the max value one.

Is there a smart tensorized way that one can achieve this?

The first operation sounds like a max pooling op.
The latter is quite undefined and I would guess you might be able to unfold the input and perform your operations via a matmul and additional operations, if needed.