Skip to content
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

[torchlib] Fix _log_softmax #1789

Merged
merged 7 commits into from
Aug 8, 2024
Merged
Changes from all 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
12 changes: 8 additions & 4 deletions onnxscript/function_libs/torch_lib/ops/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,15 @@
) -> FLOAT:
"""_log_softmax(Tensor self, int dim, bool half_to_float) -> Tensor"""

# trace_only because we need to cast conditionally based on half_to_float
self_is_scalar = IsScalar(self)
if half_to_float:
self = op.Cast(self, to=FLOAT.dtype)

return aten__log_softmax(self, dim, half_to_float)
if self_is_scalar:
self = op.Unsqueeze(self, op.Constant(value_ints=[0]))

Check warning on line 89 in onnxscript/function_libs/torch_lib/ops/core.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/function_libs/torch_lib/ops/core.py#L89

Added line #L89 was not covered by tests
result = op.LogSoftmax(self, axis=dim)
if self_is_scalar:
result = op.Squeeze(result, op.Constant(value_ints=[0]))

Check warning on line 92 in onnxscript/function_libs/torch_lib/ops/core.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/function_libs/torch_lib/ops/core.py#L92

Added line #L92 was not covered by tests
return result


@torch_op("aten::_log_softmax", traceable=True)
Expand All @@ -101,7 +105,7 @@
if self_is_scalar:
self = op.Unsqueeze(self, op.Constant(value_ints=[0]))
result = op.LogSoftmax(self, axis=dim)
if self_is_scalar: # squeeze to scalar due to input is scalar
if self_is_scalar:
result = op.Squeeze(result)
return result

Expand Down
Loading