text
stringlengths
0
2.2M
using namespace at::native::xnnpack;
BufHandle Input("Input", {100, 200}, kFloat);
BufHandle ResultBuf("Result", {100, 300}, kFloat);
// Calculate reference result using at::linear.
auto options = at::TensorOptions()
.dtype(at::kFloat)
.layout(at::kStrided)
.device(at::kCPU)
.requires_grad(false);
at::Tensor input =
at::linspace(-10.0, 10.0, 100 * 200, options).resize_({100, 200});
at::Tensor weight =
at::linspace(-10.0, 10.0, 300 * 200, options).resize_({300, 200});
at::Tensor bias = at::linspace(-10.0, 10.0, 300, options);
at::Tensor ref = at::linear(input, weight, bias);
// Create prepacked xnnpack context object.
auto linear_clamp_prepack_op =
c10::Dispatcher::singleton()
.findSchemaOrThrow("prepacked::linear_clamp_prepack", "")
.typed<c10::intrusive_ptr<LinearOpContext>(
at::Tensor,
c10::optional<at::Tensor>,
const c10::optional<at::Scalar>&,
const c10::optional<at::Scalar>&)>();
auto prepacked = linear_clamp_prepack_op.call(
weight, bias, c10::optional<at::Scalar>(), c10::optional<at::Scalar>());
BufHandle DummyPrepacked("DummyPrepacked", {1}, kFloat);
Tensor Result = Tensor(
ResultBuf.node(),
ExternalCall::make(
ResultBuf,
"nnc_prepacked_linear_clamp_run",
{Input, DummyPrepacked},
{}));
LoopNest l({Result});
l.prepareForCodegen();
l.simplify();
at::Tensor nnc_result;
std::vector<float> input_buf(
input.data_ptr<float>(), input.data_ptr<float>() + 100 * 200);
std::vector<float> result_buf(100 * 300, -1.f);
#ifdef TORCH_ENABLE_LLVM
LLVMCodeGen llvm_codegen(l.root_stmt(), {Input, DummyPrepacked, Result});
llvm_codegen.call({input_buf, prepacked.get(), result_buf});
nnc_result = at::from_blob(result_buf.data(), {100, 300}, options);
ASSERT_TRUE(at::allclose(nnc_result, ref));
#endif
SimpleIREvaluator ir_eval(l.root_stmt(), {Input, DummyPrepacked, Result});
ir_eval.call({input_buf, prepacked.get(), result_buf});
nnc_result = at::from_blob(result_buf.data(), {100, 300}, options);
ASSERT_TRUE(at::allclose(nnc_result, ref));
}
TEST(ExternalCall, Prepacked_Conv2d_float) {
using namespace at::native::xnnpack;
BufHandle Input("Input", {1, 3, 224, 224}, kFloat);
BufHandle ResultBuf("Result", {1, 16, 112, 112}, kFloat);
int64_t stride = 2;
int64_t pad = 1;
int64_t dilation = 1;
int64_t groups = 1;
// Calculate reference result using at::conv2d.
auto options = at::TensorOptions()
.dtype(at::kFloat)
.layout(at::kStrided)
.device(at::kCPU)
.requires_grad(false);
at::Tensor input = at::linspace(-10.0, 10.0, 1 * 3 * 224 * 224, options)
.resize_({1, 3, 224, 224});
at::Tensor weight =
at::linspace(-10.0, 10.0, 16 * 3 * 3 * 3, options).resize_({16, 3, 3, 3});
at::Tensor bias = at::linspace(-10.0, 10.0, 16, options);
at::Tensor ref = at::conv2d(
input,
weight,
bias,
{stride, stride},
{pad, pad},
{dilation, dilation},
groups);
// Create prepacked xnnpack context object.
auto conv2d_clamp_prepack_op =
c10::Dispatcher::singleton()
.findSchemaOrThrow("prepacked::conv2d_clamp_prepack", "")
.typed<c10::intrusive_ptr<Conv2dOpContext>(
at::Tensor,
c10::optional<at::Tensor>,
std::vector<int64_t>,