Skip to content

View/Reinterpret Tensor as a different type w/o copying #42571

@jakirkham

Description

@jakirkham

🚀 Feature

It would be useful to have a way to reinterpret the type of existing data as another type (without copying). For example, one can do this with NumPy (or CuPy) like so...

In [1]: import numpy                                                            

In [2]: b = b"\x00\x00\x00<\x00@"                                               

In [3]: a = numpy.asarray(memoryview(b))                                        

In [4]: a                                                                       
Out[4]: array([ 0,  0,  0, 60,  0, 64], dtype=uint8)

In [5]: a.view(numpy.float16)                                                   
Out[5]: array([0., 1., 2.], dtype=float16)

Motivation

In some cases users may feed data into PyTorch, which comes from more raw formats (like bytes and bytearray). So they don't really have the type factored in when provided. In these cases what's needed is a way to add the type after the fact.

Pitch

Ideally Tensor would get a new method, which allows users to reinterpret the type of data and returns a Tensor viewing the same data, but with a different type.

Alternatives

Suggestions welcome 🙂

Additional context

NA

Have searched the issue tracker and the discussion forum, but didn't come across anything that looked related. Though please let me know if I've merely overlooked something.

cc @mruberry @rgommers

Metadata

Metadata

Assignees

Labels

function requestA request for a new function or the addition of new arguments/modes to an existing function.module: numpyRelated to numpy support, and also numpy compatibility of our operatorstriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions