Where does the layout transform of each op happen during alter_op_layout pass?

When you see the tensors changed from 4D to 5D, the corresponding conv2d op has already been changed from NCHW to NCHWc; otherwise the type won’t match. This is called “alter op layout”. Specifically, the function you pointed returns the altered NCHWc op:

Accordingly, your graph changed from 4D -> conv2d_NCHW to 4D -> layout_tranform -> 5D -> conv2d_NCHWc.