-
Notifications
You must be signed in to change notification settings - Fork 364
Closed the perf gap of resnet and enabled refit #3629
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
Open
cehongwang
wants to merge
2
commits into
main
Choose a base branch
from
perf-gap
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+167
−82
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
get_trt_tensor, | ||
has_dynamic_shape, | ||
set_layer_name, | ||
to_trt_weights, | ||
) | ||
from torch_tensorrt.dynamo.conversion.impl.cat import cat | ||
from torch_tensorrt.dynamo.conversion.impl.elementwise.ops import ge | ||
|
@@ -47,90 +48,163 @@ def batch_norm( | |
|
||
# Save the original output shape for later use | ||
output_shape = input.shape | ||
# We perform constant folding for batch norm when the weight, bias, running_mean, and running_var are all tensors. | ||
# Batch norm operation can be fused into a single layer, which is more efficient than the original implementation. | ||
# In this way, the batch norm layer will be fused with the Convolution layer and get a performance boost. | ||
if all( | ||
[ | ||
isinstance(weight, torch.Tensor), | ||
isinstance(bias, torch.Tensor), | ||
isinstance(running_mean, torch.Tensor), | ||
isinstance(running_var, torch.Tensor), | ||
] | ||
): | ||
if weight is None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this check is redundant. If |
||
weight = 1.0 | ||
|
||
if bias is None: | ||
bias = 0.0 | ||
|
||
if running_mean is None: | ||
cehongwang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
running_mean = 0.0 | ||
|
||
if running_var is None: | ||
running_var = 1.0 | ||
adjusted_scale = weight / torch.sqrt(running_var + eps) | ||
adjusted_bias = bias - running_mean * adjusted_scale | ||
power = torch.ones_like(adjusted_scale) | ||
adjusted_scale = to_trt_weights( | ||
ctx, | ||
adjusted_scale, | ||
name, | ||
layer_type_name="SCALE", | ||
weight_type_name="SCALE", | ||
target=target, | ||
source_ir=source_ir, | ||
) | ||
adjusted_bias = to_trt_weights( | ||
ctx, | ||
adjusted_bias, | ||
name, | ||
layer_type_name="SCALE", | ||
weight_type_name="SHIFT", | ||
target=target, | ||
source_ir=source_ir, | ||
) | ||
|
||
# We name the weight here according to the state_dict name | ||
weight = ( | ||
get_trt_tensor(ctx, 1.0, f"{name}_weight") | ||
if weight is None | ||
else get_trt_tensor(ctx, weight, f"{name}_weight") | ||
) | ||
bias = ( | ||
get_trt_tensor(ctx, 0.0, f"{name}_bias") | ||
if bias is None | ||
else get_trt_tensor(ctx, bias, f"{name}_bias") | ||
) | ||
running_mean = ( | ||
get_trt_tensor(ctx, 0.0, f"{name}_running_mean") | ||
if running_mean is None | ||
else get_trt_tensor(ctx, running_mean, f"{name}_running_mean") | ||
) | ||
running_var = ( | ||
get_trt_tensor(ctx, 1.0, f"{name}_running_var") | ||
if running_var is None | ||
else get_trt_tensor(ctx, running_var, f"{name}_running_var") | ||
) | ||
power = to_trt_weights( | ||
ctx, | ||
power, | ||
name, | ||
layer_type_name="SCALE", | ||
weight_type_name="POWER", | ||
target=target, | ||
source_ir=source_ir, | ||
) | ||
|
||
# eps_tensor for numerical stability | ||
eps_tensor = get_trt_tensor(ctx, eps, f"{name}_eps") | ||
output_shape = input.shape | ||
if len(input.shape) < 4: | ||
|
||
# adjusted_var = running_var + eps | ||
adjusted_var = impl.elementwise.add( | ||
ctx, target, source_ir, f"{name}_adjusted_var", running_var, eps_tensor | ||
) | ||
new_shape = ( | ||
(input.shape[0], input.shape[1], 1, 1) | ||
if len(input.shape) == 2 | ||
else (input.shape[0], input.shape[1], input.shape[2], 1) | ||
) | ||
input = impl.shuffle.reshape( | ||
ctx, target, source_ir, f"{name}_reshape_2d", input, new_shape | ||
) | ||
|
||
# sqrt_adjusted_var = sqrt(adjusted_var) | ||
sqrt_adjusted_var = impl.unary.sqrt( | ||
ctx, target, source_ir, f"{name}_sqrt", adjusted_var | ||
) | ||
layer = ctx.net.add_scale_nd( | ||
input, trt.ScaleMode.CHANNEL, adjusted_bias, adjusted_scale, power, 1 | ||
) | ||
set_layer_name(layer, target, name, source_ir) | ||
output = layer.get_output(0) | ||
|
||
# scale = weight / sqrt_adjusted_var | ||
scale = impl.elementwise.div( | ||
ctx, target, source_ir, f"{name}_scale", weight, sqrt_adjusted_var | ||
) | ||
else: | ||
|
||
# scaled_running_mean = running_mean * scale | ||
scaled_running_mean = impl.elementwise.mul( | ||
ctx, target, source_ir, f"{name}_scaled_running_mean", running_mean, scale | ||
) | ||
# We name the weight here according to the state_dict name | ||
weight = ( | ||
get_trt_tensor(ctx, 1.0, f"{name}_weight") | ||
if weight is None | ||
else get_trt_tensor(ctx, weight, f"{name}_weight") | ||
) | ||
bias = ( | ||
get_trt_tensor(ctx, 0.0, f"{name}_bias") | ||
if bias is None | ||
else get_trt_tensor(ctx, bias, f"{name}_bias") | ||
) | ||
running_mean = ( | ||
get_trt_tensor(ctx, 0.0, f"{name}_running_mean") | ||
if running_mean is None | ||
else get_trt_tensor(ctx, running_mean, f"{name}_running_mean") | ||
) | ||
running_var = ( | ||
get_trt_tensor(ctx, 1.0, f"{name}_running_var") | ||
if running_var is None | ||
else get_trt_tensor(ctx, running_var, f"{name}_running_var") | ||
) | ||
|
||
# bias_adjusted = bias - scaled_running_mean | ||
bias_adjusted = impl.elementwise.sub( | ||
ctx, target, source_ir, f"{name}_bias_adjusted", bias, scaled_running_mean | ||
) | ||
# eps_tensor for numerical stability | ||
eps_tensor = get_trt_tensor(ctx, eps, f"{name}_eps") | ||
|
||
# Reshape scale and bias_adjusted to match input shape for broadcasting | ||
expanded_shape = [1] * len(output_shape) | ||
expanded_shape[1] = output_shape[1] # Set channel dimension | ||
# adjusted_var = running_var + eps | ||
adjusted_var = impl.elementwise.add( | ||
ctx, target, source_ir, f"{name}_adjusted_var", running_var, eps_tensor | ||
) | ||
|
||
scale_reshape = impl.shuffle.reshape( | ||
ctx, | ||
target, | ||
source_ir, | ||
f"{name}_reshape_scale", | ||
scale, | ||
tuple(expanded_shape), | ||
) | ||
bias_adjusted_reshape = impl.shuffle.reshape( | ||
ctx, | ||
target, | ||
source_ir, | ||
f"{name}_reshape_bias", | ||
bias_adjusted, | ||
tuple(expanded_shape), | ||
) | ||
# sqrt_adjusted_var = sqrt(adjusted_var) | ||
sqrt_adjusted_var = impl.unary.sqrt( | ||
ctx, target, source_ir, f"{name}_sqrt", adjusted_var | ||
) | ||
|
||
# Apply the scale and bias to the input | ||
scaled_input = impl.elementwise.mul( | ||
ctx, target, source_ir, f"{name}_scaled_input", input, scale_reshape | ||
) | ||
output = impl.elementwise.add( | ||
ctx, | ||
target, | ||
source_ir, | ||
f"{name}_output", | ||
scaled_input, | ||
bias_adjusted_reshape, | ||
) | ||
# scale = weight / sqrt_adjusted_var | ||
scale = impl.elementwise.div( | ||
ctx, target, source_ir, f"{name}_scale", weight, sqrt_adjusted_var | ||
) | ||
|
||
# scaled_running_mean = running_mean * scale | ||
scaled_running_mean = impl.elementwise.mul( | ||
ctx, target, source_ir, f"{name}_scaled_running_mean", running_mean, scale | ||
) | ||
|
||
# bias_adjusted = bias - scaled_running_mean | ||
bias_adjusted = impl.elementwise.sub( | ||
ctx, target, source_ir, f"{name}_bias_adjusted", bias, scaled_running_mean | ||
) | ||
|
||
# Reshape scale and bias_adjusted to match input shape for broadcasting | ||
expanded_shape = [1] * len(output_shape) | ||
expanded_shape[1] = output_shape[1] # Set channel dimension | ||
|
||
scale_reshape = impl.shuffle.reshape( | ||
ctx, | ||
target, | ||
source_ir, | ||
f"{name}_reshape_scale", | ||
scale, | ||
tuple(expanded_shape), | ||
) | ||
bias_adjusted_reshape = impl.shuffle.reshape( | ||
ctx, | ||
target, | ||
source_ir, | ||
f"{name}_reshape_bias", | ||
bias_adjusted, | ||
tuple(expanded_shape), | ||
) | ||
|
||
# Apply the scale and bias to the input | ||
scaled_input = impl.elementwise.mul( | ||
ctx, target, source_ir, f"{name}_scaled_input", input, scale_reshape | ||
) | ||
output = impl.elementwise.add( | ||
ctx, | ||
target, | ||
source_ir, | ||
f"{name}_output", | ||
scaled_input, | ||
bias_adjusted_reshape, | ||
) | ||
|
||
# For BatchNorm1d, reshape output back to original shape if necessary | ||
if len(output_shape) < 4: | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should abstract this imo. Like if there are any weight types that require constant folding in converter this should be associated with the converter. Then the refit system will just iterate through all these constant fold operations. Ideally the converter can use the same implementation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently BN is the only one. Do you think we should have a constant_fold function and have
refit
andconversion
call that function?