MCPcopy Create free account
hub / github.com/Oneflow-Inc/oneflow / InplaceAsStrided

Function InplaceAsStrided

oneflow/core/framework/tensor_methods.cpp:708–733  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

706}
707
708Maybe<void> InplaceAsStrided(const std::shared_ptr<one::Tensor>& input,
709 const std::vector<int64_t>& sizes, const std::vector<int64_t>& strides,
710 const int64_t storage_offset) {
711 DimVector dim_vec;
712 dim_vec.insert(dim_vec.end(), sizes.begin(), sizes.end());
713 Shape target_shape(dim_vec);
714 Stride stride(strides.begin(), strides.end());
715 JUST(view::InplaceView(input, target_shape, stride, storage_offset));
716 if (autograd::GradMode::is_enabled() && input->requires_grad()) {
717 auto backward_fn = std::make_shared<BackwardFunction>();
718 backward_fn->body = [=](const TensorTuple& out_grads, TensorTuple* in_grads,
719 bool create_graph) -> Maybe<void> {
720 autograd::AutoGradMode mode(create_graph);
721 CHECK_EQ_OR_RETURN(out_grads.size(), 1)
722 << "out grad size should be 1, but got " << out_grads.size();
723 in_grads->resize(1);
724 (*in_grads)[0] = JUST(AsStridedGrad(out_grads[0], input, sizes, strides, storage_offset));
725 return Maybe<void>::Ok();
726 };
727 backward_fn->status = []() { return true; };
728 TensorTuple outputs{input};
729 JUST(GetThreadLocalAutogradEngine()->AddNode("view::inplace_as_strided_backward", backward_fn,
730 {input}, &outputs));
731 }
732 return Maybe<void>::Ok();
733}
734
735Maybe<Tensor> Transpose(const std::shared_ptr<Tensor>& input, const std::vector<int32_t>& permute) {
736 const auto& shape = input->shape();

Callers 1

operator()Method · 0.85

Calls 11

InplaceViewFunction · 0.85
AsStridedGradFunction · 0.85
insertMethod · 0.80
AddNodeMethod · 0.80
is_enabledFunction · 0.70
endMethod · 0.45
beginMethod · 0.45
requires_gradMethod · 0.45
sizeMethod · 0.45
resizeMethod · 0.45

Tested by

no test coverage detected