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

Function AsStrided

oneflow/core/framework/tensor_methods.cpp:681–706  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

679}
680
681Maybe<Tensor> AsStrided(const std::shared_ptr<one::Tensor>& input,
682 const std::vector<int64_t>& sizes, const std::vector<int64_t>& strides,
683 const int64_t storage_offset) {
684 DimVector dim_vec;
685 dim_vec.insert(dim_vec.end(), sizes.begin(), sizes.end());
686 Shape target_shape(dim_vec);
687 Stride stride(strides.begin(), strides.end());
688 auto output = JUST(view::BasicView(input, target_shape, stride, storage_offset));
689 if (autograd::GradMode::is_enabled() && input->requires_grad()) {
690 auto backward_fn = std::make_shared<BackwardFunction>();
691 backward_fn->body = [=](const TensorTuple& out_grads, TensorTuple* in_grads,
692 bool create_graph) -> Maybe<void> {
693 autograd::AutoGradMode mode(create_graph);
694 CHECK_EQ_OR_RETURN(out_grads.size(), 1)
695 << "out grad size should be 1, but got " << out_grads.size();
696 in_grads->resize(1);
697 (*in_grads)[0] = JUST(AsStridedGrad(out_grads[0], input, sizes, strides, storage_offset));
698 return Maybe<void>::Ok();
699 };
700 backward_fn->status = []() { return true; };
701 TensorTuple outputs{output};
702 JUST(GetThreadLocalAutogradEngine()->AddNode("view::as_strided_backward", backward_fn, {input},
703 &outputs));
704 }
705 return output;
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,

Callers 9

AsStridedGradFunction · 0.70
ApplySelectIndexingFunction · 0.50
operator()Method · 0.50
operator()Method · 0.50
operator()Method · 0.50
operator()Method · 0.50
operator()Method · 0.50
operator()Method · 0.50
operator()Method · 0.50

Calls 11

BasicViewFunction · 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