MCPcopy
hub / github.com/OpenPPL/ppq / MaxPool2d_forward

Function MaxPool2d_forward

ppq/executor/op/torch/default.py:489–538  ·  view source on GitHub ↗
(op: Operation, values: List[torch.Tensor], ctx: TorchBackendContext = None, **kwargs)

Source from the content-addressed store, hash-verified

487
488
489def MaxPool2d_forward(op: Operation, values: List[torch.Tensor], ctx: TorchBackendContext = None, **kwargs) -> torch.Tensor:
490 ASSERT_NUM_OF_INPUT(op=op, values=values, min_num_of_input=1, max_num_of_input=1)
491 process_attribute(op.attributes, values[0].shape[2:])
492
493 [x] = values
494 onnx_pads = GET_ATTRIBUTE_FROM_OPERATION(op=op, attribute='pads', default=0)
495 dilation = GET_ATTRIBUTE_FROM_OPERATION(op=op, attribute='dilations', default=1)
496 stride = GET_ATTRIBUTE_FROM_OPERATION(op=op, attribute='strides', default=None)
497 ceil_mode = bool(GET_ATTRIBUTE_FROM_OPERATION(op=op, attribute='ceil_mode', default=False))
498 if op.type == 'GlobalMaxPool': kernel_size = x.size()[2:]
499 else: kernel_size = GET_ATTRIBUTE_FROM_OPERATION(op=op, attribute='kernel_shape', compulsive=True)
500
501 ndim = x.ndim
502 # pool - 3d
503 if ndim == 5:
504 torch_pads = convert_onnx_pads_to_torch(onnx_pads=onnx_pads, mode='3d')
505 if isinstance(torch_pads, list) and len(torch_pads) != 3:
506 x = F.pad(x, torch_pads)
507 torch_pads = 0
508 output = F.max_pool3d(
509 x, kernel_size=kernel_size, padding=torch_pads,
510 dilation=dilation, stride=stride, ceil_mode=ceil_mode)
511
512 elif ndim == 4:
513 # onnx pads format[top, left, bottom, right] to torch pads format[left, right, top, bottom]
514 if isinstance(onnx_pads, list) and len(onnx_pads) == 4:
515 p_left, p_right, p_top, p_bottom = onnx_pads[1], onnx_pads[3], onnx_pads[0], onnx_pads[2]
516 # torch does not support padding contains 4 value, there is a fix of it.
517 if p_left == p_right and p_top == p_bottom:
518 onnx_pads = [p_top, p_left]
519 else:
520 x = F.pad(x, pad=convert_onnx_pads_to_torch(onnx_pads), value=float("-inf"))
521 onnx_pads = 0
522
523 output = F.max_pool2d(
524 x, kernel_size=kernel_size,
525 padding=onnx_pads, dilation=dilation,
526 stride=stride, ceil_mode=ceil_mode)
527
528 elif ndim in {2, 3}:
529 torch_pads = convert_onnx_pads_to_torch(onnx_pads=onnx_pads, mode='1d')
530 if isinstance(torch_pads, list) and len(torch_pads) != 1:
531 x = F.pad(x, torch_pads)
532 torch_pads = 0
533 output = F.max_pool1d(
534 x, kernel_size=kernel_size, padding=torch_pads,
535 dilation=dilation, stride=stride, ceil_mode=ceil_mode)
536 else:
537 raise ValueError(f'Operation {op.name} is invalid, {ndim}-d input is not supported.')
538 return output
539
540
541def BatchNormalization_forward(op: Operation, values: List[torch.Tensor], ctx: TorchBackendContext = None, **kwargs) -> torch.Tensor:

Callers

nothing calls this directly

Calls 4

process_attributeFunction · 0.90
ASSERT_NUM_OF_INPUTFunction · 0.85

Tested by

no test coverage detected