8000 [caffe2] Remove OperatorBase::newstyle_outputs_ by peterbell10 · Pull Request #67093 · pytorch/pytorch · GitHub
[go: up one dir, main page]

Skip to content

[caffe2] Remove OperatorBase::newstyle_outputs_ #67093

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

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
de0edc0
[caffe2] Remove Operator::newstyle_outputs_
peterbell10 Oct 22, 2021
7bff0a1
Update on "[caffe2] Remove Operator::newstyle_outputs_"
peterbell10 Oct 22, 2021
52608de
Update on "[caffe2] Remove Operator::newstyle_outputs_"
peterbell10 Oct 22, 2021
a2bd24e
Update on "[caffe2] Remove Operator::newstyle_outputs_"
peterbell10 Oct 22, 2021
7d7f99f
Update on "[caffe2] Remove OperatorBase::newstyle_outputs_"
peterbell10 Oct 24, 2021
f3ad7c3
Update on "[caffe2] Remove OperatorBase::newstyle_outputs_"
peterbell10 Oct 28, 2021
ae55e45
Update on "[caffe2] Remove OperatorBase::newstyle_outputs_"
peterbell10 Nov 17, 2021
dd3ec09
Update on "[caffe2] Remove OperatorBase::newstyle_outputs_"
peterbell10 Dec 6, 2021
1d690ec
Update on "[caffe2] Remove OperatorBase::newstyle_outputs_"
peterbell10 Jan 3, 2022
fa6a3f6
Update on "[caffe2] Remove OperatorBase::newstyle_outputs_"
peterbell10 Jan 14, 2022
e65e6f3
Update on "[caffe2] Remove OperatorBase::newstyle_outputs_"
peterbell10 Jan 17, 2022
018f0a3
Update on "[caffe2] Remove OperatorBase::newstyle_outputs_"
peterbell10 Jan 18, 2022
8d90b65
Update on "[caffe2] Remove OperatorBase::newstyle_outputs_"
peterbell10 Jan 20, 2022
881db53
Update on "[caffe2] Remove OperatorBase::newstyle_outputs_"
peterbell10 Jan 21, 2022
f66e024
Rebase after master CI break on "[caffe2] Remove OperatorBase::newsty…
peterbell10 Jan 22, 2022
f830c30
Only build IValueInterface when necessary on "[caffe2] Remove Operato…
peterbell10 Feb 14, 2022
2e67734
Rebase and fix merge conflicts on "[caffe2] Remove OperatorBase::news…
peterbell10 Mar 29, 2022
f50c7ca
Rebase on "[caffe2] Remove OperatorBase::newstyle_outputs_"
peterbell10 Apr 8, 2022
b8ae3ac
Update on "[caffe2] Remove OperatorBase::newstyle_outputs_"
peterbell10 Jul 27, 2022
4723019
Rebase on "[caffe2] Remove OperatorBase::newstyle_outputs_"
peterbell10 Oct 10, 2022
3d950c5
Update on "[caffe2] Remove OperatorBase::newstyle_outputs_"
peterbell10 Nov 14, 2022
60eb729
Update on "[caffe2] Remove OperatorBase::newstyle_outputs_"
peterbell10 Jan 17, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion caffe2/contrib/aten/aten_op_template.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once
#include <unordered_map>
#include <string>
#include <ATen/ATen.h>
#include <ATen/Functions.h>
#include <c10/macros/Macros.h>
#include <c10/util/irange.h>
#include <caffe2/core/context.h>
Expand Down
36 changes: 23 additions & 13 deletions caffe2/core/export_caffe2_op_to_c10.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <c10/util/irange.h>
#include <torch/csrc/jit/frontend/function_schema_parser.h>
#include <torch/library.h>
#include <caffe2/core/tensor.h>
#include <vector>

namespace caffe2 {
Expand All @@ -20,19 +21,19 @@ namespace detail {
constexpr const char* PREALLOCATED_OUTPUT_ARGNAME =
"_caffe2_preallocated_outputs";

using _CallCaffe2OpFunc = c10::List<at::Tensor>(
using _CallCaffe2OpFunc = std::vector<caffe2::Tensor>(
const c10::FunctionSchema& schema, 10000
std::vector<c10::IValue>&& inputs,
c10::List<at::Tensor>&& outputs);
std::vector<c10::IValue> &&inputs,
std::vector<caffe2::Tensor> &&outputs);

template <class Caffe2Operator>
inline c10::List<at::Tensor> _call_caffe2_op(
inline std::vector<caffe2::Tensor> _call_caffe2_op(
const c10::FunctionSchema& schema,
std::vector<c10::IValue>&& inputs,
c10::List<at::Tensor>&& outputs) {
std::vector<c10::IValue> &&inputs,
std::vector<caffe2::Tensor> &&outputs) {
Caffe2Operator op(schema, std::move(inputs), std::move(outputs), -1);
op.Run(-1);
return std::move(op).move_newstyle_outputs();
return std::move(op).move_output_tensors();
}

// This function is inline in the hope that compilers optimizing for speed will
Expand Down Expand Up @@ -62,7 +63,6 @@ inline void _call_caffe2_op_from_c10(
*OptionalType::create(ListType::ofTensors())));
IValue preallocated_outputs = torch::jit::pop(*stack);

const size_t num_outputs = schema.returns().size();
const size_t num_inputs = schema.arguments().size() -
1; // -1 because the last argument is the list of preallocated tensors

Expand All @@ -71,7 +71,7 @@ inline void _call_caffe2_op_from_c10(
// either the schema doesn't support preallocated outputs or it does but
// they haven't been passed in. Pass a list of uninitialized tensors to
// the caffe2 operator as preallocated outputs.
outputs.resize(num_outputs);
outputs.resize(schema.returns().size());
} else {
AT_ASSERT(preallocated_outputs.isTensorList());
outputs = std::move(preallocated_outputs).toTensorList();
Expand All @@ -81,7 +81,15 @@ inline void _call_caffe2_op_from_c10(
// instances in the cache.
std::vector<IValue> inputs = torch::jit::pop(*stack, num_inputs);

outputs = (*call_op)(schema, std::move(inputs), std::move(outputs));
// Convert outputs to caffe2::Tensor
const size_t num_outputs = outputs.size();
std::vector<caffe2::Tensor> outputs_c2(num_outputs);
for (auto i : c10::irange(num_outputs)) {
outputs_c2[i] = caffe2::Tensor(outputs.extract(i));
}

outputs_c2 = (*call_op)(schema, std::move(inputs), std::move(outputs_c2));
TORCH_INTERNAL_ASSERT(num_outputs == outputs_c2.size());

bool return_tensor_list = false;
if (schema.returns().size() == 1) {
Expand All @@ -93,11 +101,13 @@ inline void _call_caffe2_op_from_c10(
}
}
if (return_tensor_list) {
// We should not unwrap the list if we expect tensor list in the schema.
for (const auto i : c10::irange(num_outputs)) {
outputs.set(i, at::Tensor(std::move(outputs_c2[i])));
}
torch::jit::push(*stack, outputs);
} else {
for (const auto i : c10::irange(outputs.size())) {
torch::jit::push(*stack, outputs.extract(i));
for (const auto i : c10::irange(num_outputs)) {
torch::jit::push(*stack, at::Tensor(std::move(outputs_c2[i])));
}
}

Expand Down
9 changes: 2 additions & 7 deletions caffe2/core/operator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ OperatorBase::OperatorBase(const OperatorDef& operator_def, Workspace* ws)
device_option_(
operator_def.has_device_option() ? operator_def.device_option()
: DeviceOption()),
#if defined(EXPOSE_C2_OPS) || \
!defined(CAFFE2_IS_XPLAT_BUILD) && !defined(C10_MOBILE)
newstyle_outputs_(),
#endif
input_size_(operator_def.input_size()),
event_(std::make_unique<Event>(device_option_)) {
static GlobalInitIsCalledGuard guard;
Expand Down Expand Up @@ -124,14 +120,13 @@ compute_input_size_(const std::vector<c10::IValue>& inputs) {
OperatorBase::OperatorBase(
const c10::FunctionSchema& fn_schema,
std::vector<c10::IValue> inputs,
c10::List<at::Tensor> outputs)
std::vector<caffe2::Tensor> outputs)
// NOLINTNEXTLINE(performance-move-const-arg)
: fn_schema_(make_unique<c10::FunctionSchema>(std::move(fn_schema))),
newstyle_inputs_(std::move(inputs)),
newstyle_outputs_(std::move(outputs)),
output_tensors_(std::move(outputs)),
input_size_(compute_input_size_(newstyle_inputs_)) {
input_tensors_.resize(input_size_);
output_tensors_.resize(newstyle_outputs_.size());
}
#endif

Expand Down
37 changes: 13 additions & 24 deletions caffe2/core/operator.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class TORCH_API OperatorBase : public Observable<OperatorBase> {
explicit OperatorBase(
const c10::FunctionSchema& schema,
std::vector<c10::IValue> inputs,
c10::List<at::Tensor> outputs);
std::vector<caffe2::Tensor> outputs);
#endif

virtual ~OperatorBase() noexcept;
Expand Down Expand Up @@ -250,15 +250,12 @@ class TORCH_API OperatorBase : public Observable<OperatorBase> {
}
#if defined(EXPOSE_C2_OPS) || \
!defined(CAFFE2_IS_XPLAT_BUILD) && !defined(C10_MOBILE)
at::Tensor output = newstyle_outputs_[idx];
if (!output.defined() || caffe2::Tensor(output).GetDeviceType() != type) {
auto &output = output_tensors_[idx];
if (!output.defined() || output.GetDeviceType() != type) {
// Fix tensor type
Tensor tensor = Tensor(type);
output = at::Tensor(std::move(tensor.getIntrusivePtr()));
output = Tensor(type);
}
output_tensors_[idx] = caffe2::Tensor(output);
newstyle_outputs_[idx] = std::move(output);
return &output_tensors_[idx];
return &output;
#else
CAFFE_THROW("Non-legacy operators are not legal in xplat/caffe2");
#endif
Expand All @@ -280,9 +277,6 @@ class TORCH_API OperatorBase : public Observable<OperatorBase> {
if (!isLegacyOperator()) {
#if defined(EXPOSE_C2_OPS) || \
!defined(CAFFE2_IS_XPLAT_BUILD) && !defined(C10_MOBILE)
newstyle_outputs_[idx] = at::Tensor(tensor);

// also update the tensor in the hack
output_tensors_[idx] = std::move(tensor);
#else
CAFFE_THROW("Non-legacy operators are not legal in xplat/caffe2");
Expand Down Expand Up @@ -310,16 +304,12 @@ class TORCH_API OperatorBase : public Observable<OperatorBase> {
}
#if defined(EXPOSE_C2_OPS) || \
!defined(CAFFE2_IS_XPLAT_BUILD) && !defined(C10_MOBILE)
at::Tensor output = newstyle_outputs_[idx];
Tensor tensor = output.defined()
? GetSizedTensorWithOptions(caffe2::Tensor(output), dims, options)
auto &output = output_tensors_[idx];
output = output.defined()
? GetSizedTensorWithOptions(std::move(output), dims, options)
: caffe2::empty(dims, options);
// assign it back in case it changed
output = at::Tensor(std::move(tensor.getIntrusivePtr()));

output_tensors_[idx] = caffe2::Tensor(output);
newstyle_outputs_[idx] = std::move(output);
return &output_tensors_[idx];
return &output;
#else
CAFFE_THROW("Non-legacy operators are not legal in xplat/caffe2");
#endif
Expand Down Expand Up @@ -434,7 +424,7 @@ class TORCH_API OperatorBase : public Observable<OperatorBase> {
}
#if defined(EXPOSE_C2_OPS) || \
!defined(CAFFE2_IS_XPLAT_BUILD) && !defined(C10_MOBILE)
return newstyle_outputs_.size();
return output_tensors_.size();
#else
CAFFE_THROW("Non-legacy operators are not legal in xplat/caffe2");
#endif
Expand Down Expand Up @@ -599,8 +589,8 @@ class TORCH_API OperatorBase : public Observable<OperatorBase> {

#if defined(EXPOSE_C2_OPS) || \
!defined(CAFFE2_IS_XPLAT_BUILD) && !defined(C10_MOBILE)
c10::List<at::Tensor> move_newstyle_outputs() && {
return std::move(newstyle_outputs_);
std::vector<caffe2::Tensor> move_output_tensors() && {
return std::move(output_tensors_);
}
#endif

Expand All @@ -620,7 +610,6 @@ class TORCH_API OperatorBase : public Observable<OperatorBase> {
!defined(CAFFE2_IS_XPLAT_BUILD) && !defined(C10_MOBILE)
std::unique_ptr<const c10::FunctionSchema> fn_schema_;
vector<c10::IValue> newstyle_inputs_;
c10::List<at::Tensor> newstyle_outputs_;
#endif
// HACK
// We preserve the fact that Output() returns Tensor*
Expand Down Expand Up @@ -819,7 +808,7 @@ class Operator : public OperatorBase {
explicit Operator(
const c10::FunctionSchema& fn_schema,
std::vector<c10::IValue> inputs,
c10::List<at::Tensor> outputs,
std::vector<caffe2::Tensor> outputs,
StreamId stream = 0)
: OperatorBase(fn_schema, std::move(inputs), std::move(outputs)) {
// In the constructor, we switch to the device so that the child class
Expand Down
0