Depthwise-Separable Convolution Cost

~12 mincode completion

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_in

  • out_channels: C_out

  • : k (square kernel)

  • height: H of the output map

  • width: W of the output map

  • Return array [standard_macs, separable_macs] as integers

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~12 min

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.

Python
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
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.