Skip to content

feat: Add tensor type enforcement for converters #2324

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

Merged
merged 3 commits into from
Sep 29, 2023

Conversation

gs-olive
Copy link
Collaborator

Description

  • Add utilities to enforce tensor types for aten converters via direct reference
  • Add robust testing for utilities
  • Update decorator schemas throughout code

Fixes #2306

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 Sep 18, 2023
@github-actions github-actions bot added component: api [Python] Issues re: Python API component: conversion Issues re: Conversion stage component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths component: tests Issues re: Tests labels Sep 18, 2023
@github-actions github-actions bot requested a review from narendasan September 18, 2023 19:48
@gs-olive gs-olive force-pushed the tensor_type_enforcement branch from feea927 to 419348b Compare September 18, 2023 19:49
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to C++ style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to Python style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to Python style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to C++ style guidelines

@gs-olive gs-olive force-pushed the tensor_type_enforcement branch from 419348b to 568a531 Compare September 18, 2023 21:00
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to C++ style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to Python style guidelines

@gs-olive gs-olive force-pushed the tensor_type_enforcement branch from 568a531 to 38b970c Compare September 19, 2023 02:09
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to C++ style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to Python style guidelines

@gs-olive gs-olive force-pushed the tensor_type_enforcement branch from 38b970c to 25c337b Compare September 19, 2023 17:08
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to C++ style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to Python style guidelines

@gs-olive gs-olive force-pushed the tensor_type_enforcement branch from 25c337b to 7fd8187 Compare September 19, 2023 23:01
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to C++ style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to Python style guidelines

Comment on lines 255 to 317
# If the input is 64-bit, cast it to 32-bit for TRT freezing
if isinstance(input_val, torch.Tensor):
if input_val.dtype == torch.int64:
input_val = input_val.to(torch.int32)
elif input_val.dtype == torch.float64:
input_val = input_val.to(torch.float32)
elif isinstance(input_val, np.ndarray):
if input_val.dtype == np.int64:
input_val = input_val.astype(np.int32)
elif input_val.dtype == np.float64:
input_val = input_val.astype(np.float32)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Adds automatic type-casting for get_attr operators (cast to 32-bit). This function currently does not check the truncate_long_and_double argument since it applies to weights and additionally cannot easily access compiler-context variables without many code changes.

Additionally, this PR removes autocasting of boolean values to integers for constant freezing, since TRT documentation (link 1, link 2) indicates that boolean values are supported in this layer.

@gs-olive gs-olive marked this pull request as ready for review September 19, 2023 23:05
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to C++ style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to Python style guidelines

Comment on lines 255 to 309
# If the input is 64-bit, cast it to 32-bit for TRT freezing
if isinstance(input_val, torch.Tensor):
if input_val.dtype == torch.int64:
input_val = input_val.to(torch.int32)
elif input_val.dtype == torch.float64:
input_val = input_val.to(torch.float32)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Augment TRTNetwork to store user settings

  • Conversion Context class --> TRTNetwork, CompileSettings
  • INetwork + Context Class, Network inputs // CTX inputs
  • ctx.net replace network calls in converters

@gs-olive gs-olive force-pushed the tensor_type_enforcement branch from 8e5ea07 to d3c8d9c Compare September 27, 2023 21:42
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to Python style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to C++ style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to C++ style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to Python style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to C++ style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to Python style guidelines

@gs-olive gs-olive force-pushed the tensor_type_enforcement branch from 0b568d3 to db1b7af Compare September 29, 2023 00:23
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to C++ style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to Python style guidelines

Comment on lines 1370 to 1372
0: (TRTTensor,),
1: (TRTTensor, np.ndarray, torch.Tensor),
2: (TRTTensor, np.ndarray, torch.Tensor),
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Consider tuple ordering as priority for the converter

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

(np.ndarray, TRTTensor) implies np array preferred

@gs-olive gs-olive force-pushed the tensor_type_enforcement branch 2 times, most recently from 6091c6a to be84ba5 Compare September 29, 2023 19:32
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to C++ style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to Python style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to C++ style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to Python style guidelines

@gs-olive gs-olive force-pushed the tensor_type_enforcement branch from be84ba5 to b92be0d Compare September 29, 2023 20:08
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to C++ style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to Python style guidelines

- Add utilities to enforce tensor types for aten converters via direct
reference
- Add robust testing for utilities
- Update decorator schemas throughout code
@gs-olive gs-olive force-pushed the tensor_type_enforcement branch from b92be0d to 45e9184 Compare September 29, 2023 20:46
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to C++ style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to Python style guidelines

@gs-olive gs-olive merged commit 42e514b into main Sep 29, 2023
@gs-olive gs-olive deleted the tensor_type_enforcement branch September 29, 2023 21:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla signed component: api [Python] Issues re: Python API component: conversion Issues re: Conversion stage component: converters Issues re: Specific op converters component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths component: lowering Issues re: The lowering / preprocessing passes component: tests Issues re: Tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

✨[Feature] Add GetITensor constant management system to Dynamo
3 participants