Skip to content

Add memory format support to typecasting shortcuts byte,char,double,bool,half,int,long,short,float,bfloat16#27228

Closed
VitalyFedyunin wants to merge 12 commits intogh/VitalyFedyunin/4/basefrom
gh/VitalyFedyunin/4/head
Closed

Add memory format support to typecasting shortcuts byte,char,double,bool,half,int,long,short,float,bfloat16#27228
VitalyFedyunin wants to merge 12 commits intogh/VitalyFedyunin/4/basefrom
gh/VitalyFedyunin/4/head

Conversation

@VitalyFedyunin
Copy link
Contributor

@VitalyFedyunin VitalyFedyunin commented Oct 2, 2019

Stack from ghstack:

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.

Differential Revision: D17980315

…ouble', 'bool', 'half', 'int', 'long', 'short','float','bfloat16'

[ghstack-poisoned]
@pytorchbot pytorchbot added module: internals Related to internal abstractions in c10 and ATen module: operators labels Oct 2, 2019
@pytorchbot pytorchbot added the module: pybind Related to our Python bindings / interactions with other Python libraries label Oct 2, 2019
… '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]
@VitalyFedyunin VitalyFedyunin changed the title Add memory format support to typecasting shortcuts 'byte', 'char', 'double', 'bool', 'half', 'int', 'long', 'short','float','bfloat16' Add memory format support to typecasting shortcuts byte,char,double,bool,half,int,long,short,float,bfloat16 Oct 2, 2019
…`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]
@VitalyFedyunin VitalyFedyunin mentioned this pull request Oct 14, 2019
…`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]
@VitalyFedyunin
Copy link
Contributor Author

@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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#28221 #28220 will do both later to unlock support for TorchScript and add tests.

…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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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...

Copy link
Contributor

@zou3519 zou3519 Oct 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we care about the performance, we can call native::to and specify the derivative for each of byte, char, ...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets address it separately #28223


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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the tensor really need to be this big in the test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really, will make it smaller.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in #27891

Copy link
Contributor

@ezyang ezyang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]
facebook-github-bot pushed a commit that referenced this pull request Oct 17, 2019
…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
@facebook-github-bot
Copy link
Contributor

@VitalyFedyunin merged this pull request in 15df371.

@facebook-github-bot facebook-github-bot deleted the gh/VitalyFedyunin/4/head branch October 28, 2019 22:07
xxtEchjovs44 pushed a commit to xxtEchjovs44/pytorch that referenced this pull request Jan 29, 2020
…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
thiagocrepaldi pushed a commit to thiagocrepaldi/pytorch that referenced this pull request Feb 4, 2020
…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
thiagocrepaldi pushed a commit to thiagocrepaldi/pytorch that referenced this pull request Feb 4, 2020
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Merged module: internals Related to internal abstractions in c10 and ATen module: pybind Related to our Python bindings / interactions with other Python libraries

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants