mirror of
https://github.com/pytorch/pytorch.git
synced 2025-11-06 17:24:59 +08:00
* Remove dilations for pooling in onnx export and other small fixes (#3698) * fix optimization pass issues * remove pool dilations * Fix export for recent changes in ONNX (#3708) * Fix symbolic for Embedding and Upsampling and improve error messages * Record stack traces during JIT tracing (#3607) * Update comments and size logic * Record stack traces during JIT tracing * Use string helper functions and AutoGIL * Use SourceLocation object instead of storing in debugName * Address zdevito comments * Address comments * Allow 1->N broadcasts at the beginning and end to be fused (#3616) * Allow 1->N broadcasts at the beginning and end to be fused * Update comments and size logic * Implement bmm symbolic (#3681) * Buildfix. Signed-off-by: Edward Z. Yang <ezyang@fb.com> * Now actually fix padding (the tests are added in onnx-pytorch) (#3893) * Now actually fix padding (the tests are added in onnx-pytorch) * fix test * Fix exporting HalfTensor * Fix padding according to https://github.com/onnx/onnx/issues/261 * Update ONNX IR we emit to version 0.0.2 (attribute discriminators) / fix Permute export (#3484) * Regenerate ONNX nanopb from latest version. But don't bump the IR version, we don't handle discriminators yet. Signed-off-by: Edward Z. Yang <ezyang@fb.com> * Add discriminator to AttributeProto. Signed-off-by: Edward Z. Yang <ezyang@fb.com> * Add back ONNX definition for permute Signed-off-by: Edward Z. Yang <ezyang@fb.com> * PyTorch now uses operator versioning. Also move some of the exporter info out of the ModelProto constructor. Signed-off-by: Edward Z. Yang <ezyang@fb.com>
104 lines
3.4 KiB
Python
104 lines
3.4 KiB
Python
import torch
|
|
from functools import reduce
|
|
from operator import mul
|
|
|
|
|
|
def maybe_view(variable, size, check_same_size=True):
|
|
if check_same_size and variable.size() == size:
|
|
return variable
|
|
return variable.contiguous().view(size)
|
|
|
|
|
|
def maybe_unexpand(variable, old_size, check_same_size=True):
|
|
if check_same_size and variable.size() == old_size:
|
|
return variable
|
|
num_unsqueezed = variable.dim() - len(old_size)
|
|
expanded_dims = [dim for dim, (expanded, original)
|
|
in enumerate(zip(variable.size()[num_unsqueezed:], old_size))
|
|
if expanded != original]
|
|
|
|
for _ in range(num_unsqueezed):
|
|
variable = variable.sum(0, keepdim=False)
|
|
for dim in expanded_dims:
|
|
variable = variable.sum(dim, keepdim=True)
|
|
return variable
|
|
|
|
|
|
_SAME_SIZE = 2
|
|
_EXPANDABLE = 1
|
|
_NOT_EXPANDABLE = 0
|
|
|
|
|
|
def variable_expandable(variable, old_size):
|
|
if variable.size() == old_size:
|
|
return _SAME_SIZE
|
|
try:
|
|
torch._C._infer_size(variable.size(), old_size)
|
|
except RuntimeError:
|
|
return _NOT_EXPANDABLE
|
|
return _EXPANDABLE
|
|
|
|
|
|
def maybe_unexpand_or_view(variable, old_size):
|
|
var_expanded = variable_expandable(variable, old_size)
|
|
|
|
if var_expanded == _SAME_SIZE:
|
|
return variable
|
|
elif var_expanded == _EXPANDABLE:
|
|
return maybe_unexpand(variable, old_size, False)
|
|
else:
|
|
return maybe_view(variable, old_size, False)
|
|
|
|
|
|
# Generate paddings in ONNX order based on pad in pytorch.
|
|
# Arguments:
|
|
# dim: the dimension of the tensor.
|
|
# pad: the paddings in pytorch.
|
|
# The order is dim_n_begin, dim_n_end, dim_n-1_begin, dim_n-1_end, ...
|
|
def prepare_onnx_paddings(dim, pad):
|
|
assert isinstance(dim, int)
|
|
# The desired order of paddings is
|
|
# dim_0_begin, dim_1_begin, ... , dim_0_end, ..., dim_n_end.
|
|
# n is the dimension of input.
|
|
assert len(pad) <= dim * 2
|
|
# assume zero-dimensions in the beginning
|
|
paddings = list(pad[:]) + [0] * (dim * 2 - len(pad))
|
|
# reverse order and collate first beginnings and then ends
|
|
paddings = paddings[-2::-2] + paddings[-1::-2]
|
|
assert len(paddings) == dim * 2
|
|
return paddings
|
|
|
|
|
|
# Check whether the op enable broadcasting, and whether it is supported by ONNX.
|
|
# If dims1 and dims2 are different, then broadcast is True.
|
|
# We always assume the combination of dims1 and dims2 is broadcastable.
|
|
# The following types of broadcasting are supported in ONNX:
|
|
# 1) Only one element in dims2, such as dims2 = [1, 1]
|
|
# 2) dims2 is suffix of dims1, such as dims1 = [2, 3, 4], and dims2 = [3, 4]
|
|
# Details can be found here: https://github.com/onnx/onnx/blob/master/docs/Operators.md#Gemm
|
|
def check_onnx_broadcast(dims1, dims2):
|
|
broadcast = False
|
|
supported = True
|
|
len1 = len(dims1)
|
|
len2 = len(dims2)
|
|
numel1 = reduce(lambda x, y: x * y, dims1)
|
|
numel2 = reduce(lambda x, y: x * y, dims2)
|
|
if len1 < len2:
|
|
broadcast = True
|
|
if numel2 != 1:
|
|
supported = False
|
|
elif len1 > len2:
|
|
broadcast = True
|
|
if numel2 != 1 and dims1[len1 - len2:] != dims2:
|
|
supported = False
|
|
else:
|
|
if dims1 != dims2:
|
|
broadcast = True
|
|
if numel2 != 1:
|
|
supported = False
|
|
|
|
if not supported:
|
|
raise ValueError("Numpy style broadcasting is not supported in ONNX. "
|
|
"Input dims are: {}, {}".format(dims1, dims2))
|
|
return broadcast
|