Skip to content
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

feat: Add option to specify int64 as an Input dtype #1551

Merged
merged 2 commits into from
Jan 9, 2023

Conversation

gs-olive
Copy link
Collaborator

@gs-olive gs-olive commented Dec 15, 2022

Description

  • Rework Input paradigm to be based on at::ScalarType as opposed to the previous nvinfer1::DataType, allowing a larger representation space of data types

  • When paired with truncate_long_and_double, insert casts to ensure Torch engines using Int64 tensors receive the correct types, and TensorRT engines operating on those tensors receive downcasted Int32 versions thereof

  • Add Torch block at the beginning of model graph to prepare types of input tensors for forthcoming engines in sequence

  • Automatically follow internal tensor types to abstract away the different internal engines used (Torch/TensorRT) from the user

  • Provide a framework for streamlined addition of other data types, including torch.double as valid input types

  • Improve error checking to ensure model compilation and behavior is as documented. For example, disallow specification of Long type input if the engine is required to be converted entirely to TRT

  • Modify compiler to extract inferred data types for each input

  • Add Python API testing to ensure casts are inserted correctly and run in Torch

  • Known Limitations:

    • Specifying dtype=torch.long on an Input in an input_signature is not supported currently and will throw an error before model compilation when used with the Python API
    • While Torch may output Int64 tensors from the overall model, Torch-TRT currently can only output Int32 tensors for models using TRT, as there is not a mechanism in place for differentiating intermediate blocks from final/beginning blocks in the graph
    • Torch-TRT will almost definitely alter the data type of the input tensor, in-place, if dtype=torch.long is specified, and the returned result will be of type torch.int32

Note: The scope of this feature is currently limited to partitioning-enabled models (require_full_compilation=False) having truncate_long_and_double=True, since the feature prepends a Torch-executed block to the graph which performs the necessary casts, and so it requires both partitioning and truncation.

Fixes #1546
Addresses most of #1543

Type of change

  • New feature (non-breaking change which adds functionality)

Checklist:

  • [ x ] My code follows the style guidelines of this project (You can use the linters)
  • [ x ] I have performed a self-review of my own code
  • [ x ] I have commented my code, particularly in hard-to-understand areas and hacks
  • [ x ] I have made corresponding changes to the documentation
  • [ x ] I have added tests to verify my fix or my feature
  • [ x ] New and existing unit tests pass locally with my changes
  • [ x ] I have added the relevant labels to my PR in so that relevant reviewers are notified

@gs-olive gs-olive self-assigned this Dec 15, 2022
@github-actions github-actions bot added component: core Issues re: The core compiler component: lowering Issues re: The lowering / preprocessing passes component: tests Issues re: Tests labels Dec 15, 2022
core/lowering/lowering.cpp Outdated Show resolved Hide resolved
core/compiler.cpp Outdated Show resolved Hide resolved
@narendasan
Copy link
Collaborator

@peri044 is the reviewer for this feature

@gs-olive gs-olive marked this pull request as draft December 15, 2022 19:54
@github-actions github-actions bot added component: api [Python] Issues re: Python API component: api [C++] Issues re: C++ API component: conversion Issues re: Conversion stage labels Dec 21, 2022
@gs-olive gs-olive force-pushed the autocast branch 2 times, most recently from 14ed6dd to bed7f39 Compare December 21, 2022 04:42
@gs-olive gs-olive changed the title feat: Automatically cast user inputs to inferred data type feat: Add option to specify int64 as an Input dtype Dec 21, 2022
@gs-olive gs-olive marked this pull request as ready for review December 21, 2022 04:48
- Rework `Input` paradigm to be based on `at::ScalarType` as opposed to
the previous `nvinfer1::DataType`, allowing a larger representation
space of data types
- When paired with `truncate_long_and_double`, insert casts to ensure
Torch engines using Int64 tensors receive the correct types, and
TensorRT engines operating on those tensors receive downcasted Int32
versions thereof
- Add Torch block at the beginning of model graph to prepare types
of input tensors for forthcoming engines in sequence
- Automatically follow internal tensor types to abstract away the
different internal engines used (Torch/TensorRT) from the user
- Provide a framework for streamlined addition of other data types,
including `torch.double` as valid input types
- Improve error checking to ensure model compilation and behavior is as
documented. For example, disallow specification of Long type input if
the engine is required to be converted entirely to TRT

- Known Limitations:
- Specifying `dtype=torch.long` on an `Input` in an `input_signature` is
not supported currently and will throw an error before model compilation
when used with the Python API
- While Torch may output Int64 tensors from the overall model, Torch-TRT
currently can only output Int32 tensors for models using TRT, as there
is not a mechanism in place for differentiating intermediate blocks from
final/beginning blocks in the graph
- Torch-TRT will almost definitely alter the data type of the input
tensor, in-place, if `dtype=torch.long` is specified, and the returned
result will be of type `torch.int32`
core/lowering/lowering.cpp Show resolved Hide resolved
core/lowering/lowering.cpp Outdated Show resolved Hide resolved
cpp/src/types.cpp Outdated Show resolved Hide resolved
py/torch_tensorrt/csrc/tensorrt_classes.cpp Outdated Show resolved Hide resolved
py/torch_tensorrt/_Input.py Outdated Show resolved Hide resolved
@peri044
Copy link
Collaborator

peri044 commented Dec 21, 2022

@mfeliz-cruise Please check the description for more details on the extent of int64 dtype support. This PR should resolve the usecases that we discussed.

core/lowering/lowering.cpp Outdated Show resolved Hide resolved
- Address review comments
- Add cpp API testing and support
- Improve length and efficiency of autocast graph
- Improve messages displayed to user
core/lowering/lowering.cpp Show resolved Hide resolved
core/lowering/lowering.cpp Show resolved Hide resolved
@gs-olive gs-olive requested a review from peri044 January 9, 2023 17:20
Copy link
Collaborator

@peri044 peri044 left a comment

Choose a reason for hiding this comment

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

LGTM.

core/lowering/lowering.cpp Show resolved Hide resolved
@peri044 peri044 merged commit 835abf0 into pytorch:main Jan 9, 2023
@gs-olive gs-olive deleted the autocast branch January 10, 2023 00:34
@gs-olive
Copy link
Collaborator Author

Fixes #1543

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla signed component: api [C++] Issues re: C++ API component: api [Python] Issues re: Python API component: conversion Issues re: Conversion stage component: core Issues re: The core compiler component: lowering Issues re: The lowering / preprocessing passes component: partitioning component: tests Issues re: Tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

✨[Feature] Support INT64 inputs at the graph input level
4 participants