Depthwise-Separable Convolution Cost
Implement conv_macs(in_channels, out_channels, kernel, height, width) returning a length-2 integer array [standard, separable].
Examples
One channel in, one out, 3x3, one pixel: 9 versus 9 + 1
- Input
- conv_macs(1, 1, 3, 1, 1)
- Output
- [9, 10]
The MobileNet-style layer from the prompt
- Input
- conv_macs(32, 64, 3, 8, 8)
- Output
- [1179648, 149504]
At k = 1 the separable version is the more expensive one
- Input
- conv_macs(16, 16, 1, 4, 4)
- Output
- [4096, 4352]
Hints
Hint 1
Convert the input with before doing elementwise work.
Hint 2
Watch for this: multiplied depthwise cost by out channels.
Requirements
in_channels: C_inout_channels: C_out: k (square kernel)
height: H of the output mapwidth: W of the output mapReturn array [standard_macs, separable_macs] as integers
Constraints
Allowed library: NumPy only
Time limit: 200 ms, Memory: 64 MB
Where this shows up
8 employers weight this skill
4 autonomy companies, 1 defense company, 1 health and bio company, 1 enterprise vendor, 1 AI product company. Top match scores 81.
import numpy as np
def conv_macs(in_channels, out_channels, kernel, height, width):
"""
Multiply-accumulate counts for a standard and a depthwise-separable conv.
Args:
in_channels: C_in
out_channels: C_out
kernel: k (square kernel)
height: H of the output map
width: W of the output map
Returns:
array [standard_macs, separable_macs] as integers
"""
# YOUR CODE HERE
pass