Skip to content

Add Op(multinomial) | torchlib(feat) #1032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions onnxscript/function_libs/torch_lib/ops/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4820,15 +4820,24 @@ def aten_mul_bool(self: BOOL, other: BOOL) -> BOOL:
return op.And(self, other)


@torch_op("aten::multinomial")
def aten_multinomial(
self: TensorType,
self: TFloat,
num_samples: int,
replacement: bool = False,
generator: Optional[str] = None,
) -> TensorType:
replacement: bool = False, # pylint: disable=unused-argument
) -> TInt:
"""multinomial(Tensor self, int num_samples, bool replacement=False, *, Generator? generator=None) -> Tensor"""

raise NotImplementedError()
# ONNX Multinomial doesn't support 1D input
if op.Size(op.Shape(self)) == 1:
unsqueezed_input = op.Unsqueeze(self, axes=0)
else:
unsqueezed_input = self
# ONNX multinomial expects log probability
log_input = op.Log(unsqueezed_input)
result = op.Multinomial(log_input, dtype=INT64.dtype, sample_size=num_samples)
if op.Size(op.Shape(self)) == 1:
result = op.Squeeze(result)
return result


def aten_multiply(self: TensorType, other: TensorType) -> TensorType:
Expand Down
5 changes: 5 additions & 0 deletions onnxscript/tests/function_libs/torch_lib/ops_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -1545,6 +1545,11 @@ def _where_input_wrangler(
matcher=lambda sample: len(sample.args) > 0,
reason="this ATen overload only supports one tensor as input by design",
),
TorchLibOpInfo(
"multinomial",
core_ops.aten_multinomial,
nondeterministic=True,
),
TorchLibOpInfo(
# Custom from extra_opinfo
"ops.aten.max_pool1d",
Expand Down