question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

App: Can not pass a PyTorch Tensor to Work's run method

See original GitHub issue

🐛 Bug

I get this error while trying to pass a tensor to a work’s run method: (tried locally so far)

INFO: Your app has started. View it in your browser: http://127.0.0.1:7501/view
INFO: Your Lightning App is being stopped. This won't take long.
INFO: Your Lightning App has been stopped successfully!
Traceback (most recent call last):
  File "/home/krshrimali/anaconda3/envs/pl-17/lib/python3.9/site-packages/deepdiff/deephash.py", line 430, in _prep_tuple
    obj._asdict
AttributeError: 'tuple' object has no attribute '_asdict'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/krshrimali/anaconda3/envs/pl-17/bin/lightning", line 8, in <module>
    sys.exit(main())
  File "/home/krshrimali/anaconda3/envs/pl-17/lib/python3.9/site-packages/lightning_app/cli/lightning_cli.py", line 70, in main
    _main()
  File "/home/krshrimali/anaconda3/envs/pl-17/lib/python3.9/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "/home/krshrimali/anaconda3/envs/pl-17/lib/python3.9/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "/home/krshrimali/anaconda3/envs/pl-17/lib/python3.9/site-packages/click/core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/krshrimali/anaconda3/envs/pl-17/lib/python3.9/site-packages/click/core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/krshrimali/anaconda3/envs/pl-17/lib/python3.9/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/krshrimali/anaconda3/envs/pl-17/lib/python3.9/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/home/krshrimali/anaconda3/envs/pl-17/lib/python3.9/site-packages/lightning_app/cli/lightning_cli.py", line 362, in run_app
    _run_app(file, cloud, cluster_id, without_server, no_cache, name, blocking, open_ui, env)
  File "/home/krshrimali/anaconda3/envs/pl-17/lib/python3.9/site-packages/lightning_app/cli/lightning_cli.py", line 314, in _run_app
    dispatch(
  File "/home/krshrimali/anaconda3/envs/pl-17/lib/python3.9/site-packages/lightning_app/runners/runtime.py", line 65, in dispatch
    return runtime.dispatch(on_before_run=on_before_run, name=name, no_cache=no_cache, cluster_id=cluster_id)
  File "/home/krshrimali/anaconda3/envs/pl-17/lib/python3.9/site-packages/lightning_app/runners/multiprocess.py", line 103, in dispatch
    self.app._run()
  File "/home/krshrimali/anaconda3/envs/pl-17/lib/python3.9/site-packages/lightning_app/core/app.py", line 427, in _run
    done = self.run_once()
  File "/home/krshrimali/anaconda3/envs/pl-17/lib/python3.9/site-packages/lightning_app/core/app.py", line 376, in run_once
    self.root.run()
  File "check.py", line 20, in run
    self.w.run(self._output)
  File "/home/krshrimali/anaconda3/envs/pl-17/lib/python3.9/site-packages/lightning_app/runners/backends/backend.py", line 73, in _dynamic_run_wrapper
    return work.run(*args, **kwargs)
  File "/home/krshrimali/anaconda3/envs/pl-17/lib/python3.9/site-packages/lightning_app/utilities/proxies.py", line 101, in __call__
    call_hash = self.work._call_hash(self.work_run, args, kwargs)
  File "/home/krshrimali/anaconda3/envs/pl-17/lib/python3.9/site-packages/lightning_app/core/work.py", line 428, in _call_hash
    return str(DeepHash(call_obj)[call_obj])[:7]
  File "/home/krshrimali/anaconda3/envs/pl-17/lib/python3.9/site-packages/deepdiff/deephash.py", line 190, in __init__
    self._hash(obj, parent=parent, parents_ids=frozenset({get_id(obj)}))
  File "/home/krshrimali/anaconda3/envs/pl-17/lib/python3.9/site-packages/deepdiff/deephash.py", line 477, in _hash
    result, counts = self._prep_dict(obj=obj, parent=parent, parents_ids=parents_ids)
  File "/home/krshrimali/anaconda3/envs/pl-17/lib/python3.9/site-packages/deepdiff/deephash.py", line 360, in _prep_dict
    hashed, count = self._hash(item, parent=key_in_report, parents_ids=parents_ids_added)
  File "/home/krshrimali/anaconda3/envs/pl-17/lib/python3.9/site-packages/deepdiff/deephash.py", line 480, in _hash
    result, counts = self._prep_tuple(obj=obj, parent=parent, parents_ids=parents_ids)
  File "/home/krshrimali/anaconda3/envs/pl-17/lib/python3.9/site-packages/deepdiff/deephash.py", line 433, in _prep_tuple
    result, counts = self._prep_iterable(obj=obj, parent=parent, parents_ids=parents_ids)
  File "/home/krshrimali/anaconda3/envs/pl-17/lib/python3.9/site-packages/deepdiff/deephash.py", line 393, in _prep_iterable
    hashed, count = self._hash(item, parent=new_parent, parents_ids=parents_ids_added)
  File "/home/krshrimali/anaconda3/envs/pl-17/lib/python3.9/site-packages/deepdiff/deephash.py", line 488, in _hash
    result, counts = self._prep_obj(obj=obj, parent=parent, parents_ids=parents_ids)
  File "/home/krshrimali/anaconda3/envs/pl-17/lib/python3.9/site-packages/deepdiff/deephash.py", line 321, in _prep_obj
    result, counts = self._prep_dict(obj, parent=parent, parents_ids=parents_ids,
  File "/home/krshrimali/anaconda3/envs/pl-17/lib/python3.9/site-packages/deepdiff/deephash.py", line 360, in _prep_dict
    hashed, count = self._hash(item, parent=key_in_report, parents_ids=parents_ids_added)
  File "/home/krshrimali/anaconda3/envs/pl-17/lib/python3.9/site-packages/deepdiff/deephash.py", line 483, in _hash
    result, counts = self._prep_iterable(obj=obj, parent=parent, parents_ids=parents_ids)
  File "/home/krshrimali/anaconda3/envs/pl-17/lib/python3.9/site-packages/deepdiff/deephash.py", line 393, in _prep_iterable
    hashed, count = self._hash(item, parent=new_parent, parents_ids=parents_ids_added)
  File "/home/krshrimali/anaconda3/envs/pl-17/lib/python3.9/site-packages/deepdiff/deephash.py", line 483, in _hash
    result, counts = self._prep_iterable(obj=obj, parent=parent, parents_ids=parents_ids)
  File "/home/krshrimali/anaconda3/envs/pl-17/lib/python3.9/site-packages/deepdiff/deephash.py", line 393, in _prep_iterable
    hashed, count = self._hash(item, parent=new_parent, parents_ids=parents_ids_added)
  File "/home/krshrimali/anaconda3/envs/pl-17/lib/python3.9/site-packages/deepdiff/deephash.py", line 483, in _hash
    result, counts = self._prep_iterable(obj=obj, parent=parent, parents_ids=parents_ids)
  File "/home/krshrimali/anaconda3/envs/pl-17/lib/python3.9/site-packages/deepdiff/deephash.py", line 383, in _prep_iterable
    for i, item in enumerate(obj):
  File "/home/krshrimali/anaconda3/envs/pl-17/lib/python3.9/site-packages/torch/_tensor.py", line 723, in __iter__
    raise TypeError('iteration over a 0-d tensor')
TypeError: iteration over a 0-d tensor

To Reproduce

import lightning as L
import torch

class Work(L.LightningWork):
    def __init__(self):
        super().__init__()

    def run(self, tens):
        print(tens)

class Flow(L.LightningFlow):
    def __init__(self):
        super().__init__()
        self.w = Work()

    def run(self):
        # Also tried using Payload(torch.tensor(1)) -> same error
        # Error: TypeError: iteration over a 0-d tensor
        self.w.run(torch.tensor(1))


app = L.LightningApp(Flow())

Expected behavior

It should not raise the error above?

Environment

  • Lightning Component (e.g. Trainer, LightningModule, LightningApp, LightningWork, LightningFlow):
  • PyTorch Lightning Version (e.g., 1.5.0):
  • Lightning App Version (e.g., 0.5.2): 0.6.0
  • PyTorch Version (e.g., 1.10): 1.12.1+cu116
  • Python version (e.g., 3.9): 3.9.12
  • OS (e.g., Linux):
  • CUDA/cuDNN version:
  • GPU models and configuration:
  • How you installed PyTorch (conda, pip, source):
  • If compiling from source, the output of torch.__config__.show():
  • Running environment of LightningApp (e.g. local, cloud):
  • Any other relevant information:

Additional context

cc: @rohitgr7 @tchaton @ethanwharris

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
tchatoncommented, Sep 12, 2022

@krshrimali The payload is meant to be created within work.run method and pass to another work, not in the flow.

0reactions
stale[bot]commented, Oct 16, 2022

This issue has been automatically marked as stale because it hasn’t had any recent activity. This issue will be closed in 7 days if no further activity occurs. Thank you for your contributions, PyTorch Lightning Team!

Read more comments on GitHub >

github_iconTop Results From Across the Web

torch.Tensor — PyTorch 1.13 documentation
A tensor of specific data type can be constructed by passing a torch.dtype ... torch.device and/or torch.dtype , consider using to() method on...
Read more >
Extending PyTorch — PyTorch 1.13 documentation
Tensor arguments that track history (i.e., with requires_grad=True ) will be converted to ones that don't track history before the call, and their...
Read more >
Tensors — PyTorch Tutorials 1.13.0+cu117 documentation
This implementation uses PyTorch tensors to manually compute the forward pass, loss, and backward pass. A PyTorch Tensor is basically the same as...
Read more >
Introduction to PyTorch Tensors
Tensors are the central data abstraction in PyTorch. This interactive notebook provides an in-depth introduction to the torch.Tensor class.
Read more >
Learning PyTorch with Examples
An n-dimensional Tensor, similar to numpy but can run on GPUs ... order polynomial to sine function by manually implementing the forward and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found