#include <c10/core/Tensor.h>
#include <c10/util/Array.h>
-namespace at {
-class BaseContext;
-}
-
namespace c10 {
namespace core {
namespace opschema {
USE_OPERATOR_CONTEXT_FUNCTIONS;
- static constexpr bool op_has_context_argument = std::is_same<
- BaseContext*,
- c10::guts::typelist::last_t<
- typename Schema::signature::parameter_types>>::value;
static constexpr bool op_has_state_argument =
!std::is_same<void, State>::value;
static constexpr size_t num_inputs() {
return Schema::signature::num_args - num_outputs() - num_parameters() -
- (op_has_context_argument ? 1 : 0) - (op_has_state_argument ? 1 : 0);
+ (op_has_state_argument ? 1 : 0);
}
static constexpr size_t num_parameters() {
size_t... OutputIndex,
size_t... ParameterIndex>
c10::guts::enable_if_t<
- details::true_t<InputIndex...>::value && op_has_context_argument &&
- op_has_state_argument && !use_array_input,
- void>
- RunOnDevice_(
- c10::guts::index_sequence<InputIndex...>,
- c10::guts::index_sequence<OutputIndex...>,
- c10::guts::index_sequence<ParameterIndex...>) {
- c10::Dispatcher<OpSchemaDef>::call(
- C10Tensor(Input(InputIndex))...,
- C10Tensor(*Output(OutputIndex))...,
- std::get<ParameterIndex>(parameters_)...,
- state_.get(),
- static_cast<BaseContext*>(&context_));
- }
-
- template <
- size_t... InputIndex,
- size_t... OutputIndex,
- size_t... ParameterIndex>
- c10::guts::enable_if_t<
- details::true_t<InputIndex...>::value && op_has_context_argument &&
- !op_has_state_argument && !use_array_input,
- void>
- RunOnDevice_(
- c10::guts::index_sequence<InputIndex...>,
- c10::guts::index_sequence<OutputIndex...>,
- c10::guts::index_sequence<ParameterIndex...>) {
- c10::Dispatcher<OpSchemaDef>::call(
- C10Tensor(Input(InputIndex))...,
- C10Tensor(*Output(OutputIndex))...,
- std::get<ParameterIndex>(parameters_)...,
- static_cast<BaseContext*>(&context_));
- }
-
- template <
- size_t... InputIndex,
- size_t... OutputIndex,
- size_t... ParameterIndex>
- c10::guts::enable_if_t<
- details::true_t<InputIndex...>::value && !op_has_context_argument &&
+ details::true_t<InputIndex...>::value &&
op_has_state_argument && !use_array_input,
void>
RunOnDevice_(
size_t... OutputIndex,
size_t... ParameterIndex>
c10::guts::enable_if_t<
- details::true_t<InputIndex...>::value && !op_has_context_argument &&
+ details::true_t<InputIndex...>::value &&
!op_has_state_argument && !use_array_input,
void>
RunOnDevice_(
size_t... OutputIndex,
size_t... ParameterIndex>
c10::guts::enable_if_t<
- details::true_t<InputIndex...>::value && op_has_context_argument &&
- op_has_state_argument && use_array_input,
- void>
- RunOnDevice_(
- c10::guts::index_sequence<InputIndex...>,
- c10::guts::index_sequence<OutputIndex...>,
- c10::guts::index_sequence<ParameterIndex...>) {
- c10::Dispatcher<OpSchemaDef>::call(
- at::ArrayRef<C10Tensor>(array_inputs_()),
- C10Tensor(*Output(OutputIndex))...,
- std::get<ParameterIndex>(parameters_)...,
- state_.get(),
- static_cast<BaseContext*>(&context_));
- }
-
- template <
- size_t... InputIndex,
- size_t... OutputIndex,
- size_t... ParameterIndex>
- c10::guts::enable_if_t<
- details::true_t<InputIndex...>::value && op_has_context_argument &&
- !op_has_state_argument && use_array_input,
- void>
- RunOnDevice_(
- c10::guts::index_sequence<InputIndex...>,
- c10::guts::index_sequence<OutputIndex...>,
- c10::guts::index_sequence<ParameterIndex...>) {
- c10::Dispatcher<OpSchemaDef>::call(
- at::ArrayRef<C10Tensor>(array_inputs_()),
- C10Tensor(*Output(OutputIndex))...,
- std::get<ParameterIndex>(parameters_)...,
- static_cast<BaseContext*>(&context_));
- }
-
- template <
- size_t... InputIndex,
- size_t... OutputIndex,
- size_t... ParameterIndex>
- c10::guts::enable_if_t<
- details::true_t<InputIndex...>::value && !op_has_context_argument &&
+ details::true_t<InputIndex...>::value &&
op_has_state_argument && use_array_input,
void>
RunOnDevice_(
size_t... OutputIndex,
size_t... ParameterIndex>
c10::guts::enable_if_t<
- details::true_t<InputIndex...>::value && !op_has_context_argument &&
+ details::true_t<InputIndex...>::value &&
!op_has_state_argument && use_array_input,
void>
RunOnDevice_(
const C10Tensor& B_,
const C10Tensor& C_,
bool legacy_broadcast,
- int axis,
- BaseContext* context) {
+ int axis) {
Tensor A(A_);
Tensor B(B_);
Tensor C(C_);
+ CPUContext context;
const DataType* A_data = A.template data<DataType>();
const DataType* B_data = B.template data<DataType>();
std::vector<int> A_dims;
A.data<DataType>(),
B.data<DataType>(),
C.mutable_data<DataType>(),
- static_cast<CPUContext*>(context));
+ static_cast<CPUContext*>(&context));
}
} // namespace
} // namespace caffe2
void averaged_loss_op_cpu_impl(
const C10Tensor& X_,
const C10Tensor& sum_,
- caffe2::ops::AveragedLoss::State* state,
- BaseContext* context) {
+ caffe2::ops::AveragedLoss::State* state) {
Tensor X(X_);
Tensor sum(sum_);
+ CPUContext context;
sum.Resize(vector<int64_t>());
X.numel(),
X.template data<T>(),
data,
- static_cast<Context*>(context),
+ static_cast<Context*>(&context),
&scratch);
if (X.numel() > 0) {
caffe2::math::Scale<T, T, Context>(
static_cast<T>(1.) / X.numel(),
sum.template data<T>(),
data,
- static_cast<Context*>(context));
+ static_cast<Context*>(&context));
}
}
} // namespace
void batch_gather_op_cpu_impl(
const C10Tensor& data_,
const C10Tensor& indices_,
- const C10Tensor& output_,
- BaseContext* context) {
+ const C10Tensor& output_) {
Tensor data(data_);
Tensor indices(indices_);
Tensor output(output_);
+ CPUContext context;
CAFFE_ENFORCE_GE(data.dim(), 2, "DATA should be at least 2-D");
data.size(1));
auto src = src_base + idx * block_bytesize + batch * data_batch_bytesize;
auto dst = out + i * block_bytesize + batch * gathered_batch_bytesize;
- context->CopyItemsSameDevice(data.dtype(), block_size, src, dst);
+ context.CopyItemsSameDevice(data.dtype(), block_size, src, dst);
}
}
}
int trans_a,
int trans_b,
int broadcast,
- caffe2::ops::BatchMatmul::State* state,
- BaseContext* context) {
+ caffe2::ops::BatchMatmul::State* state) {
Tensor A(A_);
Tensor B(B_);
Tensor Y(Y_);
+ CPUContext context;
using Engine = caffe2::DefaultEngine;
auto ndims_A = A.dim();
"be the same size.");
Y.Resize(1);
math::Dot<T, Context>(
- dims_A[0], data_A, data_B, Y.template mutable_data<T>(), static_cast<Context*>(context));
+ dims_A[0], data_A, data_B, Y.template mutable_data<T>(), static_cast<Context*>(&context));
} else {
bool A_broadcasted = false, B_broadcasted = false;
if (ndims_A == 1) {
0.0f,
Y_data + p * Y_stride,
M * N,
- static_cast<Context*>(context));
+ static_cast<Context*>(&context));
}
}
}
const C10Tensor& output_,
const C10Tensor& split_,
int axis,
- int add_axis,
- BaseContext* context) {
+ int add_axis) {
Tensor output(output_);
Tensor split(split_);
+ CPUContext context;
split.Resize(vector<int64_t>(1, inputs.size()));
int* axis_data = split.template mutable_data<int>();
static_cast<char*>(output.raw_mutable_data(Tensor(inputs[0]).dtype())) +
output_offset,
output_channels * after,
- static_cast<Context*>(context),
+ static_cast<Context*>(&context),
Tensor(inputs[0]).dtype().copy());
output_offset += axis_dim * after * input.itemsize();
}
#include "caffe2/utils/math.h"
#include "caffe2/core/tensor.h"
-using caffe2::BaseContext;
using caffe2::Tensor;
namespace caffe2 {
const C10Tensor& input_,
const C10Tensor& output_,
const std::vector<int>& dims,
- caffe2::ops::ExpandDims::State* state,
- BaseContext* context) {
+ caffe2::ops::ExpandDims::State* state) {
Tensor input(input_);
Tensor output(output_);
state->initialized = true;
}
- output.CopyFrom(input, context);
+ output.CopyFrom(input);
if (state->dims.empty()) {
return;
}
const C10Tensor& Y_,
int axis,
int axis_w,
- caffe2::ops::FullyConnected::Cache* cache,
- BaseContext* context) {
+ caffe2::ops::FullyConnected::Cache* cache) {
Tensor X(X_);
Tensor W(W_);
Tensor b(b_);
Tensor Y(Y_);
+ CPUContext context;
constexpr bool TransposeWeight = true;
W.template data<DataType>(),
0,
Y.template mutable_data<DataType>(),
- static_cast<Context*>(context),
+ static_cast<Context*>(&context),
math_type);
// Add bias term
Tensor bias_multiplier(cache->bias_multiplier_);
M,
caffe2::convert::To<float, DataType>(1),
bias_multiplier.template mutable_data<DataType>(),
- static_cast<Context*>(context));
+ static_cast<Context*>(&context));
}
caffe2::math::Gemm<DataType, Context, caffe2::DefaultEngine>(
CblasNoTrans,
b.template data<DataType>(),
1,
Y.template mutable_data<DataType>(),
- static_cast<Context*>(context),
+ static_cast<Context*>(&context),
math_type);
}
} // namespace
const std::vector<int64_t>& shape,
const std::vector<int>& extra_shape,
bool input_as_shape,
- const C10Tensor& values_,
- BaseContext* context) {
+ const C10Tensor& values_) {
Tensor output(output_);
Tensor values(values_);
+ CPUContext context;
filler_init(inputs, output_, shape, extra_shape, input_as_shape);
auto* data = output.template mutable_data<Type>();
const Type* values_data = values.template data<Type>();
if (output.numel()) {
- context->CopySameDevice(output.numel(), values_data, data);
+ context.CopySameDevice(output.numel(), values_data, data);
}
}
const std::vector<int>& extra_shape,
bool input_as_shape,
int dtype,
- caffe2::ops::ConstantFill::Value value,
- BaseContext* context) {
+ caffe2::ops::ConstantFill::Value value) {
Tensor output(output_);
+ CPUContext context;
filler_init(inputs, output_, shape, extra_shape, input_as_shape);
output.numel(),
value.as_float,
output.template mutable_data<float>(),
- static_cast<CPUContext*>(context));
+ static_cast<CPUContext*>(&context));
} else if (dtype == caffe2::TensorProto_DataType_INT32) {
caffe2::math::Set<int32_t, CPUContext>(
output.numel(),
value.as_int32,
output.template mutable_data<int32_t>(),
- static_cast<CPUContext*>(context));
+ static_cast<CPUContext*>(&context));
} else if (dtype == caffe2::TensorProto_DataType_INT64) {
caffe2::math::Set<int64_t, CPUContext>(
output.numel(),
value.as_int64,
output.template mutable_data<int64_t>(),
- static_cast<CPUContext*>(context));
+ static_cast<CPUContext*>(&context));
} else if (dtype == caffe2::TensorProto_DataType_BOOL) {
caffe2::math::Set<bool, CPUContext>(
output.numel(),
value.as_bool,
output.template mutable_data<bool>(),
- static_cast<CPUContext*>(context));
+ static_cast<CPUContext*>(&context));
} else {
throw std::logic_error(
"Unimplemented data type for ConstantFill: " +
const std::vector<int>& extra_shape,
bool input_as_shape,
float min,
- float max,
- BaseContext* context) {
+ float max) {
Tensor output(output_);
+ CPUContext context;
filler_init(inputs, output_, shape, extra_shape, input_as_shape);
min,
max,
output.template mutable_data<float>(),
- static_cast<CPUContext*>(context));
+ static_cast<CPUContext*>(&context));
}
} // namespace
} // namespace caffe2
void flatten_op_cpu_impl(
const C10Tensor& input_,
const C10Tensor& output_,
- int axis,
- BaseContext* context) {
+ int axis) {
Tensor input(input_);
Tensor output(output_);
+ CPUContext context;
CAFFE_ENFORCE_GE(
input.sizes().size(), axis, "The rank of the tensor must be >= axis.");
output.Resize(input.size_to_dim(axis), input.size_from_dim(axis));
- context->CopyItemsSameDevice(
+ context.CopyItemsSameDevice(
input.dtype(),
input.numel(),
input.raw_data(),
const C10Tensor& B_,
const C10Tensor& C_,
bool legacy_broadcast,
- int axis,
- BaseContext* context) {
+ int axis) {
Tensor A(A_);
Tensor B(B_);
Tensor C(C_);
+ CPUContext context;
const DataType* A_data = A.template data<DataType>();
const DataType* B_data = B.template data<DataType>();
std::vector<int> A_dims;
A.data<DataType>(),
B.data<DataType>(),
C.mutable_data<DataType>(),
- static_cast<CPUContext*>(context));
+ static_cast<CPUContext*>(&context));
}
} // namespace
} // namespace caffe2
template <class DataType>
void stop_gradient_op_cpu_impl(
const C10Tensor& input_,
- const C10Tensor& output_,
- BaseContext* context) {
+ const C10Tensor& output_) {
Tensor input(input_);
Tensor output(output_);
if (output.getIntrusivePtr() != input.getIntrusivePtr()) {
- output.CopyFrom(input, context);
+ output.CopyFrom(input);
}
}
} // namespace
const C10Tensor& input2,
const C10Tensor& output,
bool legacy_broadcast,
- int axis,
- BaseContext* context);
+ int axis);
static constexpr size_t num_dispatch_args() {return 2;}
static constexpr size_t num_outputs() {return 1;}
- static constexpr c10::guts::array<const char*, 6> parameter_names = {
- {"input1", "input2", "output", "legacy_broadcast", "axis", "context"}};
+ static constexpr c10::guts::array<const char*, 5> parameter_names = {
+ {"input1", "input2", "output", "legacy_broadcast", "axis"}};
};
} // namespace ops
using Signature = void(
const C10Tensor& input,
const C10Tensor& output,
- State* state,
- BaseContext* context);
+ State* state);
static constexpr size_t num_dispatch_args() {return 1;}
static constexpr size_t num_outputs() {return 1;}
- static constexpr c10::guts::array<const char*, 4> parameter_names = {
- {"input", "output", "state", "context"}};
+ static constexpr c10::guts::array<const char*, 3> parameter_names = {
+ {"input", "output", "state"}};
};
} // namespace ops
using Signature = void(
const C10Tensor& data,
const C10Tensor& indices,
- const C10Tensor& output,
- BaseContext* context);
+ const C10Tensor& output);
static constexpr size_t num_dispatch_args() {return 2;}
static constexpr size_t num_outputs() {return 1;}
- static constexpr c10::guts::array<const char*, 4> parameter_names = {
- {"data", "indices", "output", "context"}};
+ static constexpr c10::guts::array<const char*, 3> parameter_names = {
+ {"data", "indices", "output"}};
};
} // namespace ops
int trans_a,
int trans_b,
int broadcast,
- State* state,
- BaseContext* context);
+ State* state);
static constexpr size_t num_dispatch_args() {return 2;}
static constexpr size_t num_outputs() {return 1;}
- static constexpr c10::guts::array<const char*, 8> parameter_names = {
+ static constexpr c10::guts::array<const char*, 7> parameter_names = {
{"A",
"B",
"output",
"trans_a",
"trans_b",
"broadcast",
- "state",
- "context"}};
+ "state"}};
};
} // namespace ops
const C10Tensor& output,
const C10Tensor& split_info,
int add,
- int add_axis,
- BaseContext* context);
+ int add_axis);
static constexpr size_t num_outputs() {return 2;}
- static constexpr c10::guts::array<const char*, 6> parameter_names = {
- {"inputs", "output", "split_info_output", "add", "add_axis", "context"}};
+ static constexpr c10::guts::array<const char*, 5> parameter_names = {
+ {"inputs", "output", "split_info_output", "add", "add_axis"}};
static c10::DeviceTypeId dispatch_key(
at::ArrayRef<C10Tensor> inputs,
const C10Tensor& output,
const C10Tensor& split_info,
int add,
- int add_axis,
- BaseContext* context) {
+ int add_axis) {
return c10::DeviceTypeId::CPU;
}
};
const C10Tensor& input,
const C10Tensor& output,
const std::vector<int>& dims,
- State* state,
- BaseContext* context);
+ State* state);
static constexpr size_t num_dispatch_args() {return 1;}
static constexpr size_t num_outputs() {return 1;}
- static constexpr c10::guts::array<const char*, 5> parameter_names = {
- {"input", "output", "dims", "state", "context"}};
+ static constexpr c10::guts::array<const char*, 4> parameter_names = {
+ {"input", "output", "dims", "state"}};
};
} // namespace ops
const C10Tensor& output,
int axis,
int axis_w,
- Cache* cache,
- BaseContext* context);
+ Cache* cache);
static constexpr size_t num_dispatch_args() {return 3;}
static constexpr size_t num_outputs() {return 1;}
- static constexpr c10::guts::array<const char*, 8> parameter_names = {
- {"X", "W", "b", "output", "axis", "axis_w", "cache", "context"}};
+ static constexpr c10::guts::array<const char*, 7> parameter_names = {
+ {"X", "W", "b", "output", "axis", "axis_w", "cache"}};
};
} // namespace ops
const std::vector<int64_t>& shape,
const std::vector<int>& extra_shape,
bool input_as_shape,
- const C10Tensor& values,
- BaseContext* context);
+ const C10Tensor& values);
- static constexpr c10::guts::array<const char*, 7> parameter_names = {
+ static constexpr c10::guts::array<const char*, 6> parameter_names = {
{"inputs",
"output",
"shape",
"extra_shape",
"input_as_shape",
- "values",
- "context"}};
+ "values"}};
static constexpr size_t num_outputs() {return 1;}
const std::vector<int64_t>& shape,
const std::vector<int>& extra_shape,
bool input_as_shape,
- const C10Tensor& values,
- BaseContext* context) {
+ const C10Tensor& values) {
return c10::DeviceTypeId::CPU;
}
};
const std::vector<int>& extra_shape,
bool input_as_shape,
int dtype,
- Value value,
- BaseContext* context);
+ Value value);
static constexpr size_t num_outputs() {return 1;}
- static constexpr c10::guts::array<const char*, 8> parameter_names = {
+ static constexpr c10::guts::array<const char*, 7> parameter_names = {
{"inputs",
"output",
"shape",
"extra_shape",
"input_as_shape",
"dtype",
- "value",
- "context"}};
+ "value"}};
static c10::DeviceTypeId dispatch_key(
at::ArrayRef<C10Tensor> inputs,
const std::vector<int>& extra_shape,
bool input_as_shape,
int dtype,
- Value value,
- BaseContext* context) {
+ Value value) {
return c10::DeviceTypeId::CPU;
}
};
const std::vector<int>& extra_shape,
bool input_as_shape,
float min,
- float max,
- BaseContext* context);
+ float max);
static constexpr size_t num_outputs() {return 1;}
- static constexpr c10::guts::array<const char*, 8> parameter_names = {
+ static constexpr c10::guts::array<const char*, 7> parameter_names = {
{"inputs",
"output",
"shape",
"extra_shape",
"input_as_shape",
"min",
- "max",
- "context"}};
+ "max"}};
static c10::DeviceTypeId dispatch_key(
at::ArrayRef<C10Tensor> inputs,
const std::vector<int>& extra_shape,
bool input_as_shape,
float min,
- float max,
- BaseContext* context) {
+ float max) {
return c10::DeviceTypeId::CPU;
}
};
using Signature = void(
const C10Tensor& input,
const C10Tensor& output,
- int axis,
- BaseContext* context);
+ int axis);
static constexpr size_t num_dispatch_args() {return 1;}
static constexpr size_t num_outputs() {return 1;}
- static constexpr c10::guts::array<const char*, 4> parameter_names = {
- {"input", "output", "axis", "context"}};
+ static constexpr c10::guts::array<const char*, 3> parameter_names = {
+ {"input", "output", "axis"}};
};
} // namespace ops
const C10Tensor& input2,
const C10Tensor& output,
bool legacy_broadcast,
- int axis,
- BaseContext* context);
+ int axis);
static constexpr size_t num_dispatch_args() {return 2;}
static constexpr size_t num_outputs() {return 1;}
- static constexpr c10::guts::array<const char*, 6> parameter_names = {
- {"input1", "input2", "output", "legacy_broadcast", "axis", "context"}};
+ static constexpr c10::guts::array<const char*, 5> parameter_names = {
+ {"input1", "input2", "output", "legacy_broadcast", "axis"}};
};
} // namespace ops
using Signature = void(
const C10Tensor& input,
- const C10Tensor& output,
- BaseContext* context);
+ const C10Tensor& output);
static constexpr size_t num_dispatch_args() {return 1;}
static constexpr size_t num_outputs() {return 1;}
- static constexpr c10::guts::array<const char*, 3> parameter_names = {
- {"input", "output", "context"}};
+ static constexpr c10::guts::array<const char*, 2> parameter_names = {
+ {"input", "output"}};
};
} // namespace ops
#include <c10/core/opschema/layer_norm.h>
#include <torch/csrc/jit/custom_operator.h>
#include <torch/csrc/autograd/variable.h>
-#include <caffe2/core/context.h>
using c10::C10Tensor;