Skip to content

isinstance(value, Dict[str, torch.Tensor]) does not work #46122

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
ailzhang opened this issue Oct 9, 2020 · 3 comments
Closed

isinstance(value, Dict[str, torch.Tensor]) does not work #46122

ailzhang opened this issue Oct 9, 2020 · 3 comments
Labels
oncall: jit Add this issue/PR to JIT oncall triage queue

Comments

@ailzhang
Copy link
Contributor

ailzhang commented Oct 9, 2020

import torch
from typing import Dict, Any
@torch.jit.ignore
def dbg(x: Any) -> None:
  print(type(x))
  print(x)
  print(isinstance(x, dict))
  # this doesn't work in python actually: Parameterized generics cannot be used with class or instance checks
  # print(isinstance(x, Dict[str, torch.Tensor]))

@torch.jit.script
def foo(x : Any):
  # dbg(x)
  assert isinstance(x, Dict[str, torch.Tensor])
  return x["a"]

foo({'a': torch.rand(5)})

cc @gmagogsfm

@ailzhang ailzhang added the oncall: jit Add this issue/PR to JIT oncall triage queue label Oct 9, 2020
@gmagogsfm
Copy link
Contributor

This is being worked on in #37516.

@eellison
Copy link
Contributor

eellison commented Oct 9, 2020

@gmagogsfm I think this is a different issue. Here we're correctly generating the right IR something is just borking in the runtime

@eellison eellison reopened this Oct 9, 2020
facebook-github-bot pushed a commit that referenced this issue Nov 14, 2020
Summary:
Fix for #46122

For `Any`, we infer the type of the ivalue to set the ivalue's type tag. When we saw a Tensor, we would use a specialized Tensor type, so when `Dict[str, Tensor]` was passed in as any `Any` arg it would be inferred as `Dict[str, Float(2, 2, 2, 2)]` which breaks runtime `isinstance` checking.

Pull Request resolved: #46130

Reviewed By: glaringlee

Differential Revision: D24261447

Pulled By: eellison

fbshipit-source-id: 8a2bb26ce5b6c56c8dcd8db79e420f4b5ed83ed5
@Lilyjjo
Copy link
Contributor

Lilyjjo commented Feb 8, 2021

This code snippet runs now, I'm going to close the issue.

@Lilyjjo Lilyjjo closed this as completed Feb 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
oncall: jit Add this issue/PR to JIT oncall triage queue
Projects
None yet
Development

No branches or pull requests

4 participants