Skip to content

Commit

Permalink
Don't recompute numel and contiguous in detach (#112689)
Browse files Browse the repository at this point in the history
When symolic shapes are involved, `refresh_numel` and `refresh_contiguous` are
fairly expensive since they dispatch to python for SymInt handling. However, in
the case of detach we can just copy the existing values instead.
Pull Request resolved: #112689
Approved by: https://github.com/lezcano, https://github.com/ezyang
  • Loading branch information
peterbell10 authored and pytorchmergebot committed Nov 7, 2023
1 parent 52e2b87 commit c6f435b
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 5 deletions.
4 changes: 1 addition & 3 deletions c10/core/TensorImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -819,9 +819,6 @@ c10::intrusive_ptr<TensorImpl> TensorImpl::shallow_copy_and_detach_core(
/*dest_impl=*/impl.get(),
/*version_counter=*/std::forward<VariableVersion>(version_counter),
/*allow_tensor_metadata_change=*/allow_tensor_metadata_change);

impl->refresh_numel();
impl->refresh_contiguous();
return impl;
}

Expand Down Expand Up @@ -871,6 +868,7 @@ void TensorImpl::copy_generic_tensor_metadata(
src_impl->is_non_overlapping_and_dense_;
dest_impl->is_wrapped_number_ = src_impl->is_wrapped_number_;
dest_impl->reserved_ = src_impl->reserved_;
dest_impl->numel_ = src_impl->numel_;
if (src_impl->extra_meta_ != nullptr) {
dest_impl->extra_meta_ = src_impl->extra_meta_->clone();
}
Expand Down
2 changes: 0 additions & 2 deletions c10/core/TensorImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2071,8 +2071,6 @@ struct C10_API TensorImpl : public c10::intrusive_ptr_target {
/*dest_impl=*/this,
/*version_counter=*/version_counter(),
/*allow_tensor_metadata_change=*/allow_tensor_metadata_change());
refresh_numel();
refresh_contiguous();
}

// Inference tensor doesn't have version counter,
Expand Down

0 comments on commit c6f435b

Please sign in to comment.