Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Adapt ones_like dtype for torch 2.8.0 #2598

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

Closed
M-Quadra wants to merge 1 commit into apple:main from M-Quadra:pr/ones_like
Closed
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
26 changes: 16 additions & 10 deletions coremltools/converters/mil/frontend/torch/ops.py
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -5505,7 +5505,7 @@ def _parse_keyword_args(context, node, dtype) -> Var:

@register_torch_op
def ones_like(context, node):
def _parse_positional_args(context, node) -> Tuple[Var, Optional[Var]]:
def _parse_positional_args(context, node) -> Tuple[Var, Optional[str]]:
inputs = _get_inputs(
context,
node,
Expand All @@ -5516,22 +5516,28 @@ def _parse_positional_args(context, node) -> Tuple[Var, Optional[Var]]:
dtype = None
if len(inputs) > 1 and inputs[1] is not None:
dtype = inputs[1]
if dtype is None:
dtype = _get_kwinputs(context, node, "dtype", default=[dtype])[0]
if dtype is None and node.meta is not None:
dtype = TORCH_DTYPE_TO_NUM[node.meta['tensor_meta'].dtype]
if isinstance(dtype, Var): dtype = dtype.val.item()
if isinstance(dtype, int): dtype = NUM_TO_DTYPE_STRING[dtype]
if dtype is None: dtype = types.builtin_to_string(x.dtype)
return x, dtype

def _parse_keyword_args(context, node, dtype) -> Var:
dtype = _get_kwinputs(context, node, "dtype", default=[dtype])[0]
return dtype

x, dtype = _parse_positional_args(context, node)
dtype = _parse_keyword_args(context, node, dtype)

if is_current_opset_version_compatible_with(target.iOS16):
res = mb.fill_like(ref_tensor=x, value=1.0)
v = {
"fp16": np.float16(1.0),
"fp32": np.float32(1.0),
"int32": np.int32(1),
"bool": np.bool_(True),
}.get(dtype, 1.0)
res = mb.fill_like(ref_tensor=x, value=v)
else:
res = mb.fill(shape=mb.shape(x=x), value=1.0)
# By default use input x's dtype.
dtype_str = NUM_TO_DTYPE_STRING[dtype.val] if dtype is not None else types.builtin_to_string(x.dtype)
res = _cast_to(res, dtype_str, node.name)
res = _cast_to(res, dtype, node.name)
context.add(res, node.name)


Expand Down

AltStyle によって変換されたページ (->オリジナル) /