Add memory format support to typecasting shortcuts byte,char,double,bool,half,int,long,short,float,bfloat16#27228
Conversation
…ouble', 'bool', 'half', 'int', 'long', 'short','float','bfloat16' [ghstack-poisoned]
… 'char', 'double', 'bool', 'half', 'int', 'long', 'short','float','bfloat16'" [ghstack-poisoned]
…`char`,`double`,`bool`,`half`,`int`,`long`,`short`,`float`,`bfloat16`" Adds memory_format keyword argument (positional for cpp). 'Preserve' behavior now follows next rules: 1) If tensor is non-overlapping and dense - output tensor will have the same strides as input tensor. 2) If not (1) and tensor is stored in the channels last format, output tensor going to have channels last format. 3) Output tensor is going to be contiguous in all other cases. --- Dense tensor is the tensor that store values in a contiguous block of memory. Non-overlapping tensor is the tensor in which elements occupy individual non-repetitive memory. [ghstack-poisoned]
byte,char,double,bool,half,int,long,short,float,bfloat16
…`char`,`double`,`bool`,`half`,`int`,`long`,`short`,`float`,`bfloat16`" Adds memory_format keyword argument (positional for cpp). 'Preserve' behavior now follows next rules: 1) If tensor is non-overlapping and dense - output tensor will have the same strides as input tensor. 2) If not (1) and tensor is stored in the channels last format, output tensor going to have channels last format. 3) Output tensor is going to be contiguous in all other cases. --- Dense tensor is the tensor that store values in a contiguous block of memory. Non-overlapping tensor is the tensor in which elements occupy individual non-repetitive memory. [ghstack-poisoned]
…`char`,`double`,`bool`,`half`,`int`,`long`,`short`,`float`,`bfloat16`" Adds memory_format keyword argument (positional for cpp). 'Preserve' behavior now follows next rules: 1) If tensor is non-overlapping and dense - output tensor will have the same strides as input tensor. 2) If not (1) and tensor is stored in the channels last format, output tensor going to have channels last format. 3) Output tensor is going to be contiguous in all other cases. --- Dense tensor is the tensor that store values in a contiguous block of memory. Non-overlapping tensor is the tensor in which elements occupy individual non-repetitive memory. [ghstack-poisoned]
…`char`,`double`,`bool`,`half`,`int`,`long`,`short`,`float`,`bfloat16`" Adds memory_format keyword argument (positional for cpp). 'Preserve' behavior now follows next rules: 1) If tensor is non-overlapping and dense - output tensor will have the same strides as input tensor. 2) If not (1) and tensor is stored in the channels last format, output tensor going to have channels last format. 3) Output tensor is going to be contiguous in all other cases. --- Dense tensor is the tensor that store values in a contiguous block of memory. Non-overlapping tensor is the tensor in which elements occupy individual non-repetitive memory. [ghstack-poisoned]
…`char`,`double`,`bool`,`half`,`int`,`long`,`short`,`float`,`bfloat16`" Adds memory_format keyword argument (positional for cpp). 'Preserve' behavior now follows next rules: 1) If tensor is non-overlapping and dense - output tensor will have the same strides as input tensor. 2) If not (1) and tensor is stored in the channels last format, output tensor going to have channels last format. 3) Output tensor is going to be contiguous in all other cases. --- Dense tensor is the tensor that store values in a contiguous block of memory. Non-overlapping tensor is the tensor in which elements occupy individual non-repetitive memory. [ghstack-poisoned]
…cuts `byte`,`char`,`double`,`bool`,`half`,`int`,`long`,`short`,`float`,`bfloat16`" Adds memory_format keyword argument (positional for cpp). 'Preserve' behavior now follows next rules: 1) If tensor is non-overlapping and dense - output tensor will have the same strides as input tensor. 2) If not (1) and tensor is stored in the channels last format, output tensor going to have channels last format. 3) Output tensor is going to be contiguous in all other cases. --- Dense tensor is the tensor that store values in a contiguous block of memory. Non-overlapping tensor is the tensor in which elements occupy individual non-repetitive memory. [ghstack-poisoned]
…hortcuts `byte`,`char`,`double`,`bool`,`half`,`int`,`long`,`short`,`float`,`bfloat16`" Adds memory_format keyword argument (positional for cpp). 'Preserve' behavior now follows next rules: 1) If tensor is non-overlapping and dense - output tensor will have the same strides as input tensor. 2) If not (1) and tensor is stored in the channels last format, output tensor going to have channels last format. 3) Output tensor is going to be contiguous in all other cases. --- Dense tensor is the tensor that store values in a contiguous block of memory. Non-overlapping tensor is the tensor in which elements occupy individual non-repetitive memory. [ghstack-poisoned]
|
@ailzhang breaks XLA |
|
|
||
| static PyObject * THPVariable_char(PyObject* self, PyObject* args) { | ||
| return THPVariable_to_type(self, ScalarType::Char); | ||
| static PyObject * THPVariable_to_type_char(PyObject* self, PyObject* args, PyObject* kwargs) { |
There was a problem hiding this comment.
We should keep the name as THPVariable_char. Most, if not all autogenerated functions follow a naming format of THPVariable_{api_name}; I also rely on this to search through the code base (but I'm not sure if anyone else does this).
| static PyObject * THPVariable_to_type_float(PyObject* self, PyObject* args, PyObject* kwargs) { | ||
| HANDLE_TH_ERRORS | ||
| static PythonArgParser parser({ | ||
| "float(*, MemoryFormat? memory_format=None)" |
There was a problem hiding this comment.
Are there jit tests that check that this work without MemoryFormat? We should also add the following:
- If the JIT supports
float(MemoryFormat), then add a test to check this - If the JIT does not support
float(MemoryFormat), then add a test and file an issue
…shortcuts `byte`,`char`,`double`,`bool`,`half`,`int`,`long`,`short`,`float`,`bfloat16`" Adds memory_format keyword argument (positional for cpp). 'Preserve' behavior now follows next rules: 1) If tensor is non-overlapping and dense - output tensor will have the same strides as input tensor. 2) If not (1) and tensor is stored in the channels last format, output tensor going to have channels last format. 3) Output tensor is going to be contiguous in all other cases. --- Dense tensor is the tensor that store values in a contiguous block of memory. Non-overlapping tensor is the tensor in which elements occupy individual non-repetitive memory. [ghstack-poisoned]
| return THPVariable_Wrap(dispatch_to(self_, scalarType, false, false, optional_memory_format)); | ||
| END_HANDLE_TH_ERRORS | ||
| } | ||
| static PyObject * THPVariable_byte(PyObject* self, PyObject* args) { |
There was a problem hiding this comment.
I am a bit skeptical that these should be manually bound. They seem like fairly simple functions that the code generator should be able to understand...
There was a problem hiding this comment.
Eh, if we made byte, char, ..., into native functions, then they'd call at::to. This would do two dispatches; once for byte and once for to, which could be annoying. I don't know how much times dispatches take nowadays or if it is important that we micro-manage performance on these functions
There was a problem hiding this comment.
If we care about the performance, we can call native::to and specify the derivative for each of byte, char, ...
There was a problem hiding this comment.
I don't think the dispatching really matters, casting isn't super common and these functions don't even support non_blocking, so you are going to pay the copy immediately anyway.
|
|
||
| def test_memory_format_type_shortcuts(self, device): | ||
| def input_generator_fn(device): | ||
| return torch.randn((10, 3, 32, 32), device=device, dtype=torch.float32).clamp(0, 1).round().contiguous(memory_format=torch.channels_last) |
There was a problem hiding this comment.
Does the tensor really need to be this big in the test?
There was a problem hiding this comment.
Not really, will make it smaller.
ezyang
left a comment
There was a problem hiding this comment.
I concur with Richard, implications on JIT must be checked as you have manually edited the Python bindings. However, I do think that this can come in a later PR.
…ting shortcuts `byte`,`char`,`double`,`bool`,`half`,`int`,`long`,`short`,`float`,`bfloat16`" Adds memory_format keyword argument (positional for cpp). 'Preserve' behavior now follows next rules: 1) If tensor is non-overlapping and dense - output tensor will have the same strides as input tensor. 2) If not (1) and tensor is stored in the channels last format, output tensor going to have channels last format. 3) Output tensor is going to be contiguous in all other cases. --- Dense tensor is the tensor that store values in a contiguous block of memory. Non-overlapping tensor is the tensor in which elements occupy individual non-repetitive memory. [ghstack-poisoned]
…cuts `byte`,`char`,`double`,`bool`,`half`,`int`,`long`,`short`,`float`,`bfloat16`" Adds memory_format keyword argument (positional for cpp). 'Preserve' behavior now follows next rules: 1) If tensor is non-overlapping and dense - output tensor will have the same strides as input tensor. 2) If not (1) and tensor is stored in the channels last format, output tensor going to have channels last format. 3) Output tensor is going to be contiguous in all other cases. --- Dense tensor is the tensor that store values in a contiguous block of memory. Non-overlapping tensor is the tensor in which elements occupy individual non-repetitive memory. [ghstack-poisoned]
…ble`,`bool`,`half`,`int`,`long`,`short`,`float`,`bfloat16` (#27228) Summary: Pull Request resolved: #27228 Adds memory_format keyword argument (positional for cpp). 'Preserve' behavior now follows next rules: 1) If tensor is non-overlapping and dense - output tensor will have the same strides as input tensor. 2) If not (1) and tensor is stored in the channels last format, output tensor going to have channels last format. 3) Output tensor is going to be contiguous in all other cases. --- Dense tensor is the tensor that store values in a contiguous block of memory. Non-overlapping tensor is the tensor in which elements occupy individual non-repetitive memory. Test Plan: Imported from OSS Differential Revision: D17980315 Pulled By: VitalyFedyunin fbshipit-source-id: fd5615621bc4968aa4ef2a26430c492c552ed671
|
@VitalyFedyunin merged this pull request in 15df371. |
…ble`,`bool`,`half`,`int`,`long`,`short`,`float`,`bfloat16` Adds memory_format keyword argument (positional for cpp). 'Preserve' behavior now follows next rules: 1) If tensor is non-overlapping and dense - output tensor will have the same strides as input tensor. 2) If not (1) and tensor is stored in the channels last format, output tensor going to have channels last format. 3) Output tensor is going to be contiguous in all other cases. --- Dense tensor is the tensor that store values in a contiguous block of memory. Non-overlapping tensor is the tensor in which elements occupy individual non-repetitive memory. ghstack-source-id: 3364785 Pull Request resolved: pytorch/pytorch#27228
…ble`,`bool`,`half`,`int`,`long`,`short`,`float`,`bfloat16` (pytorch#27228) Summary: Pull Request resolved: pytorch#27228 Adds memory_format keyword argument (positional for cpp). 'Preserve' behavior now follows next rules: 1) If tensor is non-overlapping and dense - output tensor will have the same strides as input tensor. 2) If not (1) and tensor is stored in the channels last format, output tensor going to have channels last format. 3) Output tensor is going to be contiguous in all other cases. --- Dense tensor is the tensor that store values in a contiguous block of memory. Non-overlapping tensor is the tensor in which elements occupy individual non-repetitive memory. Test Plan: Imported from OSS Differential Revision: D17980128 Pulled By: VitalyFedyunin fbshipit-source-id: b2646bab72c4475b7a82bb271d204a9d96d28bd4
…ble`,`bool`,`half`,`int`,`long`,`short`,`float`,`bfloat16` (pytorch#27228) Summary: Pull Request resolved: pytorch#27228 Adds memory_format keyword argument (positional for cpp). 'Preserve' behavior now follows next rules: 1) If tensor is non-overlapping and dense - output tensor will have the same strides as input tensor. 2) If not (1) and tensor is stored in the channels last format, output tensor going to have channels last format. 3) Output tensor is going to be contiguous in all other cases. --- Dense tensor is the tensor that store values in a contiguous block of memory. Non-overlapping tensor is the tensor in which elements occupy individual non-repetitive memory. Test Plan: Imported from OSS Differential Revision: D17980315 Pulled By: VitalyFedyunin fbshipit-source-id: fd5615621bc4968aa4ef2a26430c492c552ed671
Stack from ghstack:
operator==of TensorOptions as confusing one #28076 Killoperator==of TensorOptions as confusing oneresize_as_operator #27979 Add memory format support toresize_as_operatorrandn_likeoperator #27890 Add memory format support torandn_likeoperatorrandint_likeoperator #27889 Add memory format support torandint_likeoperatorzeros_likeoperator #27562 Add memory format support tozeros_likeoperatorrand_likeoperator #27561 Add memory format support torand_likeoperatorones_likeoperator #27270 Add memory format support toones_likeoperatorfull_likeoperator #27262 Add memory format support tofull_likeoperatorempty_likeoperator #27244 Add memory format support toempty_likeoperatorbyte,char,double,bool,half,int,long,short,float,bfloat16#27228 Add memory format support to typecasting shortcutsbyte,char,double,bool,half,int,long,short,float,bfloat16cpuandcudaoperators #27223 Add memory format support tocpuandcudaoperatorsAdds memory_format keyword argument (positional for cpp).
'Preserve' behavior now follows next rules:
Dense tensor is the tensor that store values in a contiguous block of memory.
Non-overlapping tensor is the tensor in which elements occupy individual non-repetitive memory.
Differential Revision: D17980315