Commit d901282e authored by Adam Procter's avatar Adam Procter Committed by Scott Cyphers

Forward prop for max pooling (#305)

* Definition and type checking for max pool

* Implement kernel, integrate into INTERPRETER, add a few unit tests, make function result type mismatch error message more informative (still need to update tests to reflect that)

* Temporarily delete unit tests to ease merge

* Temporarily delete unit tests to ease merge

* Restore deleted unit tests

* Fix a broken error message check in the unit tests

* Update to handle various TensorViewType-related things going away; add NGVM support

* Add codegen case

* Change various get_blah_shape methods to return const refs, and while we're here, make a similar change where it should have been done in convolution

* Use NDArray for max-pool tests
parent 66d06693
......@@ -48,6 +48,7 @@ set (SRC
ops/function_call.cpp
ops/get_output_element.cpp
ops/log.cpp
ops/max_pool.cpp
ops/maximum.cpp
ops/minimum.cpp
ops/multiply.cpp
......
......@@ -83,6 +83,7 @@
#include "ngraph/ops/less.hpp"
#include "ngraph/ops/less_eq.hpp"
#include "ngraph/ops/log.hpp"
#include "ngraph/ops/max_pool.hpp"
#include "ngraph/ops/maximum.hpp"
#include "ngraph/ops/minimum.hpp"
#include "ngraph/ops/multiply.hpp"
......
......@@ -88,13 +88,13 @@ namespace ngraph
/// \return The number of output channels.
size_t get_output_channel_count() const { return m_output_channel_count; }
/// \return The input image shape.
Shape get_input_image_shape() const { return m_input_image_shape; }
const Shape& get_input_image_shape() const { return m_input_image_shape; }
/// \return The output image shape.
Shape get_output_image_shape() const { return m_output_image_shape; }
const Shape& get_output_image_shape() const { return m_output_image_shape; }
/// \return The physical window shape.
Shape get_window_physical_shape() const { return m_window_physical_shape; }
const Shape& get_window_physical_shape() const { return m_window_physical_shape; }
/// \return The virtual window shape.
Shape get_window_virtual_shape() const { return m_window_virtual_shape; }
const Shape& get_window_virtual_shape() const { return m_window_virtual_shape; }
/// \return The batch size.
size_t get_batch_size() const { return m_batch_size; }
/// \return The number of image dimensions.
......
// ----------------------------------------------------------------------------
// Copyright 2017 Nervana Systems Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// ----------------------------------------------------------------------------
#include "ngraph/ops/max_pool.hpp"
#include "ngraph/util.hpp"
using namespace std;
using namespace ngraph;
op::MaxPool::MaxPool(const std::shared_ptr<Node>& arg,
const Shape& window_shape,
const Strides& window_movement_strides)
: RequiresTensorViewArgs("MaxPool", {arg})
, m_window_shape(window_shape)
, m_window_movement_strides(window_movement_strides)
{
auto& arg_shape = get_inputs().at(0).get_shape();
//
// Make sure arg: NCDi for some Di of rank>0, N != 0, C != 0.
//
if (arg_shape.size() < 3)
{
throw ngraph_error(
"Max pool image batch input must have rank of at least 3 (one batch axis, one "
"channel axis, at least one image dimension).");
}
m_batch_size = arg_shape[0];
if (m_batch_size == 0)
{
throw ngraph_error("Max pool image batch size is zero.");
}
m_channel_count = arg_shape[1];
if (m_channel_count == 0)
{
throw ngraph_error("Max pool requires at least one image depth channel.");
}
m_image_dimension_count = arg_shape.size() - 2;
//
// Make sure window shape and movement strides have same rank as Di.
//
if (m_window_shape.size() != m_image_dimension_count)
{
throw ngraph_error("Max pool window shape rank does not match number of image dimensions.");
}
if (m_window_movement_strides.size() != m_image_dimension_count)
{
throw ngraph_error(
"Max pool window movement stride rank does not match number of image dimensions.");
}
//
// Extract input image shape Di and make sure all dimensions are larger than 0.
//
for (size_t i = 0; i < m_image_dimension_count; i++)
{
m_input_image_shape.push_back(arg_shape[1 + 1 + i]);
if (m_input_image_shape[i] == 0)
{
throw ngraph_error("Max pool input image dimension is zero.");
}
}
//
// Make sure window shape dimensions are all larger than 0.
//
for (size_t i = 0; i < m_image_dimension_count; i++)
{
if (m_window_shape[i] == 0)
{
throw ngraph_error("Max pool window shape has a zero-length axis.");
}
}
//
// Make the max pooling window fits within the image dimensions.
//
for (size_t i = 0; i < m_image_dimension_count; i++)
{
if (m_window_shape[i] > m_input_image_shape[i])
{
throw ngraph_error("Max pool window shape is larger than the image.");
}
}
//
// Compute image output shape Do, checking at the same time that all window movement strides are larger than 0.
//
for (size_t i = 0; i < m_image_dimension_count; i++)
{
if (m_window_movement_strides[i] == 0)
{
throw ngraph_error("Max pool window axis movement stride is zero.");
}
m_output_image_shape.push_back(
ceil_div(m_input_image_shape[i] - m_window_shape[i] + 1, m_window_movement_strides[i]));
}
//
// Construct result shape: NCDo.
//
Shape result_shape(1 + 1 + m_image_dimension_count);
result_shape[0] = m_batch_size;
result_shape[1] = m_channel_count;
std::copy(m_output_image_shape.begin(), m_output_image_shape.end(), result_shape.begin() + 2);
set_value_type_checked(get_inputs().at(0).get_element_type(), result_shape);
}
static Strides default_strides(const std::shared_ptr<Node>& arg)
{
if (arg->get_outputs().size() != 1)
{
throw ngraph_error("Max pool image batch argument must have exactly one output");
}
auto& arg_shape = arg->get_outputs().at(0).get_shape();
if (arg_shape.size() < 3)
{
// For consistency we should throw the same error message here that we throw in the constructor.
throw ngraph_error(
"Max pool image batch input must have rank of at least 3 (one batch axis, one "
"channel axis, at least one image dimension).");
}
return Strides(arg_shape.size() - 2, 1);
}
op::MaxPool::MaxPool(const std::shared_ptr<Node>& arg, const Shape& window_shape)
: MaxPool(arg, window_shape, default_strides(arg))
{
}
/*
void op::MaxPool::generate_adjoints(autodiff::Adjoints& adjoints, const std::shared_ptr<Node>& delta)
{
}
*/
// ----------------------------------------------------------------------------
// Copyright 2017 Nervana Systems Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// ----------------------------------------------------------------------------
#pragma once
#include "ngraph/ops/op.hpp"
namespace ngraph
{
namespace op
{
/// \brief Batched max pooling operation, with optional window stride.
///
/// Max pooling takes as its input an image batch tensor of shape \f$(N,C,d_1,\dots,d_n)\f$ where \f$n > 0\f$, every \f$d_i > 0\f$, and where \f$N\f$ is
/// the batch size, and \f$C > 0\f$ is the number of channels (sometimes called features). It also takes two parameters:
///
/// 1. <i>(the window shape)</i> a size vector \f$(w_1,\dots,w_n)\f$ where every \f$w_i \le d_i\f$; and
/// 2. <i>(the window movement strides, optional)</i> a vector of positive integers \f$(s_1,\dots,s_n)\f$.
///
/// The output has the shape \f$(N,C,d'_1,\dots,d'_n)\f$, where \f$d'_n = \lceil \frac{d_i - w_i + 1}{s_i} \rceil\f$.
///
/// Given an input image batch tensor \f$T_\textit{in}\f$, the output tensor is defined by the equation
///
/// \f[
/// T_\textit{out}[a,c,i_1,\dots,i_n] = \max_{j_1 = i_1, \dots, j_n = i_n}^{j_1 = i_1 + w_1 - 1, \dots, j_n = i_n + w_n - 1} (T_\textit{in}[a,c,j_1,\dots,j_n])
/// \f]
///
class MaxPool : public RequiresTensorViewArgs
{
public:
/// \brief Constructs a batched max pooling operation.
///
/// \param arg The node producing the input image batch tensor.
/// \param window_shape The window shape.
/// \param window_movement_strides The window movement strides.
MaxPool(const std::shared_ptr<Node>& arg,
const Shape& window_shape,
const Strides& window_movement_strides);
/// \brief Constructs an unstrided batched convolution operation (i.e., all window movement strides are 1).
///
/// \param arg The node producing the input image batch tensor.
/// \param window_shape The window shape.
MaxPool(const std::shared_ptr<Node>& arg, const Shape& window_shape);
virtual std::shared_ptr<Node> copy_with_new_args(
const std::vector<std::shared_ptr<Node>>& new_args) const override
{
if (new_args.size() != 1)
throw ngraph_error("Incorrect number of new arguments");
return std::make_shared<MaxPool>(
new_args.at(0), m_window_shape, m_window_movement_strides);
}
/// \return The window shape.
const Shape& get_window_shape() const { return m_window_shape; }
/// \return The window movement strides.
const Strides& get_window_movement_strides() const { return m_window_movement_strides; }
/// \return The number of image channels.
size_t get_channel_count() const { return m_channel_count; }
/// \return The input image shape.
const Shape& get_input_image_shape() const { return m_input_image_shape; }
/// \return The output image shape.
const Shape& get_output_image_shape() const { return m_output_image_shape; }
/// \return The batch size.
size_t get_batch_size() const { return m_batch_size; }
/// \return The number of image dimensions.
size_t get_image_dimension_count() const { return m_image_dimension_count; }
protected:
Shape m_window_shape;
Strides m_window_movement_strides;
size_t m_channel_count;
Shape m_input_image_shape;
Shape m_output_image_shape;
size_t m_batch_size;
size_t m_image_dimension_count;
};
}
}
......@@ -28,6 +28,7 @@
#include "ngraph/ops/dot.hpp"
#include "ngraph/ops/function_call.hpp"
#include "ngraph/ops/get_output_element.hpp"
#include "ngraph/ops/max_pool.hpp"
#include "ngraph/ops/one_hot.hpp"
#include "ngraph/ops/reduce.hpp"
#include "ngraph/ops/replace_slice.hpp"
......@@ -1362,6 +1363,23 @@ void runtime::cpu::CPU_Emitter::EmitNot(const ngraph::Node* n,
<< " " << out[0].get_size() << ");\n";
}
void runtime::cpu::CPU_Emitter::EmitMaxPool(const ngraph::Node* n,
const vector<runtime::cpu::TensorViewWrapper>& args,
const vector<runtime::cpu::TensorViewWrapper>& out)
{
auto max_pool = static_cast<const op::MaxPool*>(n);
auto arg_shape = args[0].get_shape();
auto result_shape = out[0].get_shape();
m_out << "kernel::max_pool<" << out[0].get_type() << ">(" << args[0].get_name() << ",\n";
m_out << " " << out[0].get_name() << ",\n";
m_out << " {" << join(arg_shape) << "},\n";
m_out << " {" << join(result_shape) << "},\n";
m_out << " {" << join(max_pool->get_window_shape()) << "},\n";
m_out << " {" << join(max_pool->get_window_movement_strides()) << "});\n";
}
//------------------------------------------------------------------------------------------------
// Utility methods
//------------------------------------------------------------------------------------------------
......
......@@ -96,6 +96,7 @@ namespace ngraph
void EMITTER_DECL(EmitSqrt);
void EMITTER_DECL(EmitConvolution);
void EMITTER_DECL(EmitNot);
void EMITTER_DECL(EmitMaxPool);
private:
void generate_call(const std::vector<TensorViewWrapper>& args,
......
......@@ -55,6 +55,7 @@
#include "ngraph/ops/less.hpp"
#include "ngraph/ops/less_eq.hpp"
#include "ngraph/ops/log.hpp"
#include "ngraph/ops/max_pool.hpp"
#include "ngraph/ops/maximum.hpp"
#include "ngraph/ops/minimum.hpp"
#include "ngraph/ops/multiply.hpp"
......@@ -149,6 +150,7 @@ static const runtime::cpu::OpMap dispatcher{
{TI(ngraph::op::Sqrt), &runtime::cpu::CPU_Emitter::EmitSqrt},
{TI(ngraph::op::Convolution), &runtime::cpu::CPU_Emitter::EmitConvolution},
{TI(ngraph::op::Not), &runtime::cpu::CPU_Emitter::EmitNot},
{TI(ngraph::op::MaxPool), &runtime::cpu::CPU_Emitter::EmitMaxPool},
};
runtime::cpu::CPU_ExternalFunction::CPU_ExternalFunction(
......@@ -197,6 +199,7 @@ void runtime::cpu::CPU_ExternalFunction::compile()
#include "ngraph/runtime/kernel/concat.hpp"
#include "ngraph/runtime/kernel/convolution.hpp"
#include "ngraph/runtime/kernel/dot.hpp"
#include "ngraph/runtime/kernel/max_pool.hpp"
#include "ngraph/runtime/kernel/not.hpp"
#include "ngraph/runtime/kernel/one_hot.hpp"
#include "ngraph/runtime/kernel/reduce.hpp"
......
......@@ -26,6 +26,7 @@
#include "ngraph/ops/constant.hpp"
#include "ngraph/ops/convolution.hpp"
#include "ngraph/ops/dot.hpp"
#include "ngraph/ops/max_pool.hpp"
#include "ngraph/ops/one_hot.hpp"
#include "ngraph/ops/reduce.hpp"
#include "ngraph/ops/replace_slice.hpp"
......@@ -59,6 +60,7 @@
#include "ngraph/runtime/kernel/less.hpp"
#include "ngraph/runtime/kernel/less_eq.hpp"
#include "ngraph/runtime/kernel/log.hpp"
#include "ngraph/runtime/kernel/max_pool.hpp"
#include "ngraph/runtime/kernel/maximum.hpp"
#include "ngraph/runtime/kernel/minimum.hpp"
#include "ngraph/runtime/kernel/multiply.hpp"
......@@ -384,6 +386,17 @@ private:
reinterpret_cast<T*>(out[0]->get_data_ptr()),
out[0]->get_element_count());
}
else if (node_op == "MaxPool")
{
ngraph::op::MaxPool* max_pool = dynamic_cast<ngraph::op::MaxPool*>(&node);
kernel::max_pool<T>(reinterpret_cast<T*>(args[0]->get_data_ptr()),
reinterpret_cast<T*>(out[0]->get_data_ptr()),
args[0]->get_shape(),
out[0]->get_shape(),
max_pool->get_window_shape(),
max_pool->get_window_movement_strides());
}
else if (node_op == "Minimum")
{
kernel::minimum<T>(reinterpret_cast<T*>(args[0]->get_data_ptr()),
......
// ----------------------------------------------------------------------------
// Copyright 2017 Nervana Systems Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// ----------------------------------------------------------------------------
#pragma once
#include <cmath>
#include "ngraph/common.hpp"
#include "ngraph/coordinate_transform.hpp"
namespace ngraph
{
namespace runtime
{
namespace kernel
{
template <typename T>
void max_pool(T* arg,
T* out,
const Shape& arg_shape,
const Shape& out_shape,
const Shape& window_shape,
const Strides& window_movement_strides)
{
// At the outermost level we will walk over every output coordinate O.
CoordinateTransform output_transform(out_shape);
for (Coordinate out_coord : output_transform)
{
// Our output coordinate O will have the form:
//
// (img,chan,i_1,...,i_n)
size_t img_index = out_coord[0];
size_t channel = out_coord[1];
// For the input images we need to iterate the coordinate:
//
// I:
//
// over the range (noninclusive on the right):
//
// (img,chan,s_1*i_1,s_2*i_2,...,s_n*i_n) ->
//
// (img+1,chan+1,s_1*i_1 + window_shape_1,...,s_n*i_n + window_shape_n)
//
// with unit stride.
size_t n_image_dimensions = arg_shape.size() - 2;
Shape input_batch_transform_start(2 + n_image_dimensions);
Shape input_batch_transform_end(2 + n_image_dimensions);
input_batch_transform_start[0] = img_index;
input_batch_transform_end[0] = img_index + 1;
input_batch_transform_start[1] = channel;
input_batch_transform_end[1] = channel + 1;
for (size_t i = 2; i < n_image_dimensions + 2; i++)
{
size_t window_shape_this_dim = window_shape[i - 2];
size_t movement_stride = window_movement_strides[i - 2];
input_batch_transform_start[i] = movement_stride * out_coord[i];
input_batch_transform_end[i] =
input_batch_transform_start[i] + window_shape_this_dim;
}
CoordinateTransform input_batch_transform(
arg_shape, input_batch_transform_start, input_batch_transform_end);
// As we go, we compute the maximum value:
//
// output[O] = max(output[O],arg[I])
T result = std::numeric_limits<T>::has_infinity
? -std::numeric_limits<T>::infinity()
: std::numeric_limits<T>::min();
for (Coordinate input_batch_coord : input_batch_transform)
{
T x = arg[input_batch_transform.index(input_batch_coord)];
result = x > result ? x : result;
}
out[output_transform.index(out_coord)] = result;
}
}
}
}
}
......@@ -100,7 +100,21 @@ const element::Type& TupleType::get_element_type() const
std::ostream& ngraph::operator<<(std::ostream& out, const ValueType& obj)
{
out << "ValueType()";
auto tvt = dynamic_cast<const TensorViewType*>(&obj);
auto tup = dynamic_cast<const TupleType*>(&obj);
if (tvt != nullptr)
{
out << *tvt;
}
else if (tup != nullptr)
{
out << *tup;
}
else
{
out << "ValueType()";
}
return out;
}
......
......@@ -4218,6 +4218,191 @@ TEST(${BACKEND_NAME}, DISABLED_parameter_to_output)
EXPECT_EQ((vector<float>{1, -2, 0, -4.8f}), result->get_vector<float>());
}
TEST(${BACKEND_NAME}, max_pool_1d_1channel_1image)
{
auto shape_a = Shape{1, 1, 14};
auto window_shape = Shape{3};
auto A = make_shared<op::Parameter>(element::f32, shape_a);
auto shape_r = Shape{1, 1, 12};
auto f = make_shared<Function>(make_shared<op::MaxPool>(A, window_shape), op::Parameters{A});
auto manager = runtime::Manager::get("${BACKEND_NAME}");
auto external = manager->compile(f);
auto backend = manager->allocate_backend();
auto cf = backend->make_call_frame(external);
// Create some tensors for input/output
auto a = backend->make_primary_tensor_view(element::f32, shape_a);
copy_data(a,
test::NDArray<float, 3>{{{0, 1, 0, 2, 1, 0, 3, 2, 0, 0, 2, 0, 0, 0}}}.get_vector());
auto result = backend->make_primary_tensor_view(element::f32, shape_r);
cf->call({a}, {result});
EXPECT_EQ((test::NDArray<float, 3>({{{1, 2, 2, 2, 3, 3, 3, 2, 2, 2, 2, 0}}}).get_vector()),
result->get_vector<float>());
}
TEST(${BACKEND_NAME}, max_pool_1d_1channel_2image)
{
auto shape_a = Shape{2, 1, 14};
auto window_shape = Shape{3};
auto A = make_shared<op::Parameter>(element::f32, shape_a);
auto shape_r = Shape{2, 1, 12};
auto f = make_shared<Function>(make_shared<op::MaxPool>(A, window_shape), op::Parameters{A});
auto manager = runtime::Manager::get("${BACKEND_NAME}");
auto external = manager->compile(f);
auto backend = manager->allocate_backend();
auto cf = backend->make_call_frame(external);
// Create some tensors for input/output
auto a = backend->make_primary_tensor_view(element::f32, shape_a);
copy_data(a,
test::NDArray<float, 3>({{{0, 1, 0, 2, 1, 0, 3, 2, 0, 0, 2, 0, 0, 0}},
{{0, 2, 1, 1, 0, 0, 0, 2, 0, 1, 0, 0, 1, 2}}})
.get_vector());
auto result = backend->make_primary_tensor_view(element::f32, shape_r);
cf->call({a}, {result});
EXPECT_EQ((test::NDArray<float, 3>(
{{{1, 2, 2, 2, 3, 3, 3, 2, 2, 2, 2, 0}}, {{2, 2, 1, 1, 0, 2, 2, 2, 1, 1, 1, 2}}})
.get_vector()),
result->get_vector<float>());
}
TEST(${BACKEND_NAME}, max_pool_1d_2channel_2image)
{
auto shape_a = Shape{2, 2, 14};
auto window_shape = Shape{3};
auto A = make_shared<op::Parameter>(element::f32, shape_a);
auto shape_r = Shape{2, 2, 12};
auto f = make_shared<Function>(make_shared<op::MaxPool>(A, window_shape), op::Parameters{A});
auto manager = runtime::Manager::get("${BACKEND_NAME}");
auto external = manager->compile(f);
auto backend = manager->allocate_backend();
auto cf = backend->make_call_frame(external);
// Create some tensors for input/output
auto a = backend->make_primary_tensor_view(element::f32, shape_a);
copy_data(a,
test::NDArray<float, 3>({{{0, 1, 0, 2, 1, 0, 3, 2, 0, 0, 2, 0, 0, 0},
{0, 0, 0, 2, 0, 0, 2, 3, 0, 1, 2, 0, 1, 0}},
{{0, 2, 1, 1, 0, 0, 0, 2, 0, 1, 0, 0, 1, 2},
{2, 1, 0, 0, 1, 0, 2, 0, 0, 0, 1, 1, 2, 0}}})
.get_vector());
auto result = backend->make_primary_tensor_view(element::f32, shape_r);
cf->call({a}, {result});
EXPECT_EQ((test::NDArray<float, 3>(
{{{1, 2, 2, 2, 3, 3, 3, 2, 2, 2, 2, 0}, {0, 2, 2, 2, 2, 3, 3, 3, 2, 2, 2, 1}},
{{2, 2, 1, 1, 0, 2, 2, 2, 1, 1, 1, 2}, {2, 1, 1, 1, 2, 2, 2, 0, 1, 1, 2, 2}}})
.get_vector()),
result->get_vector<float>());
}
TEST(${BACKEND_NAME}, max_pool_2d_2channel_2image)
{
auto shape_a = Shape{2, 2, 5, 5};
auto window_shape = Shape{2, 3};
auto A = make_shared<op::Parameter>(element::f32, shape_a);
auto shape_r = Shape{2, 2, 4, 3};
auto f = make_shared<Function>(make_shared<op::MaxPool>(A, window_shape), op::Parameters{A});
auto manager = runtime::Manager::get("${BACKEND_NAME}");
auto external = manager->compile(f);
auto backend = manager->allocate_backend();
auto cf = backend->make_call_frame(external);
// Create some tensors for input/output
auto a = backend->make_primary_tensor_view(element::f32, shape_a);
copy_data(a,
test::NDArray<float, 4>({{{{0, 1, 0, 2, 1}, // img 0 chan 0
{0, 3, 2, 0, 0},
{2, 0, 0, 0, 1},
{2, 0, 1, 1, 2},
{0, 2, 1, 0, 0}},
{{0, 0, 0, 2, 0}, // img 0 chan 1
{0, 2, 3, 0, 1},
{2, 0, 1, 0, 2},
{3, 1, 0, 0, 0},
{2, 0, 0, 0, 0}}},
{{{0, 2, 1, 1, 0}, // img 1 chan 0
{0, 0, 2, 0, 1},
{0, 0, 1, 2, 3},
{2, 0, 0, 3, 0},
{0, 0, 0, 0, 0}},
{{2, 1, 0, 0, 1}, // img 1 chan 1
{0, 2, 0, 0, 0},
{1, 1, 2, 0, 2},
{1, 1, 1, 0, 1},
{1, 0, 0, 0, 2}}}})
.get_vector());
auto result = backend->make_primary_tensor_view(element::f32, shape_r);
cf->call({a}, {result});
EXPECT_EQ((test::NDArray<float, 4>({{{{3, 3, 2}, // img 0 chan 0
{3, 3, 2},
{2, 1, 2},
{2, 2, 2}},
{{3, 3, 3}, // img 0 chan 1
{3, 3, 3},
{3, 1, 2},
{3, 1, 0}}},
{{{2, 2, 2}, // img 1 chan 0
{2, 2, 3},
{2, 3, 3},
{2, 3, 3}},
{{2, 2, 1}, // img 1 chan 1
{2, 2, 2},
{2, 2, 2},
{1, 1, 2}}}})
.get_vector()),
result->get_vector<float>());
}
TEST(${BACKEND_NAME}, max_pool_2d_1channel_1image_strided)
{
auto shape_a = Shape{1, 1, 8, 8};
auto window_shape = Shape{2, 3};
auto window_movement_strides = Strides{3, 2};
auto A = make_shared<op::Parameter>(element::f32, shape_a);
auto shape_r = Shape{1, 1, 3, 3};
auto f = make_shared<Function>(
make_shared<op::MaxPool>(A, window_shape, window_movement_strides), op::Parameters{A});
auto manager = runtime::Manager::get("${BACKEND_NAME}");
auto external = manager->compile(f);
auto backend = manager->allocate_backend();
auto cf = backend->make_call_frame(external);
// Create some tensors for input/output
auto a = backend->make_primary_tensor_view(element::f32, shape_a);
copy_data(a,
test::NDArray<float, 4>({{{{0, 1, 0, 2, 1, 2, 0, 0},
{0, 3, 2, 0, 0, 0, 1, 0},
{2, 0, 0, 0, 1, 0, 0, 0},
{2, 0, 1, 1, 2, 2, 3, 0},
{0, 2, 1, 0, 0, 0, 1, 0},
{2, 0, 3, 1, 0, 0, 0, 0},
{1, 2, 0, 0, 0, 1, 2, 0},
{1, 0, 2, 0, 0, 0, 1, 0}}}})
.get_vector());
auto result = backend->make_primary_tensor_view(element::f32, shape_r);
cf->call({a}, {result});
EXPECT_EQ((test::NDArray<float, 4>({{{{3, 2, 2}, {2, 2, 3}, {2, 2, 2}}}}).get_vector()),
result->get_vector<float>());
}
TEST(${BACKEND_NAME}, not)
{
auto shape = Shape{2, 2};
......
File mode changed from 100755 to 100644
......@@ -2358,3 +2358,450 @@ TEST(type_prop, conv_invalid_movement_stride_0)
FAIL() << "Deduced type check failed for unexpected reason";
}
}
TEST(type_prop, max_pool_1d_deduce)
{
// Deduce type
auto param = make_shared<op::Parameter>(element::f32, Shape{64, 3, 100});
auto window_shape = Shape{10};
auto max_pool = make_shared<op::MaxPool>(param, window_shape);
EXPECT_EQ(max_pool->get_element_type(), element::f32);
EXPECT_EQ(max_pool->get_shape(), (Shape{64, 3, 91}));
EXPECT_EQ(max_pool->get_window_movement_strides(), Strides{1});
EXPECT_EQ(max_pool->get_channel_count(), 3);
EXPECT_EQ(max_pool->get_input_image_shape(), Shape{100});
EXPECT_EQ(max_pool->get_output_image_shape(), Shape{91});
EXPECT_EQ(max_pool->get_window_shape(), Shape{10});
EXPECT_EQ(max_pool->get_batch_size(), 64);
EXPECT_EQ(max_pool->get_image_dimension_count(), 1);
}
TEST(type_prop, max_pool_1d_deduce_strided)
{
// Deduce type
auto param = make_shared<op::Parameter>(element::f32, Shape{64, 3, 100});
auto window_shape = Shape{10};
auto move_strides = Strides{2};
auto max_pool = make_shared<op::MaxPool>(param, window_shape, move_strides);
EXPECT_EQ(max_pool->get_element_type(), element::f32);
EXPECT_EQ(max_pool->get_shape(), (Shape{64, 3, 46}));
EXPECT_EQ(max_pool->get_window_movement_strides(), Strides{2});
EXPECT_EQ(max_pool->get_channel_count(), 3);
EXPECT_EQ(max_pool->get_input_image_shape(), Shape{100});
EXPECT_EQ(max_pool->get_output_image_shape(), Shape{46});
EXPECT_EQ(max_pool->get_window_shape(), Shape{10});
EXPECT_EQ(max_pool->get_batch_size(), 64);
EXPECT_EQ(max_pool->get_image_dimension_count(), 1);
}
TEST(type_prop, max_pool_1d_deduce_strided_small_uneven)
{
// Deduce type
auto param = make_shared<op::Parameter>(element::f32, Shape{64, 3, 5});
auto window_shape = Shape{2};
auto move_strides = Strides{2};
auto max_pool = make_shared<op::MaxPool>(param, window_shape, move_strides);
EXPECT_EQ(max_pool->get_element_type(), element::f32);
EXPECT_EQ(max_pool->get_shape(), (Shape{64, 3, 2}));
EXPECT_EQ(max_pool->get_window_movement_strides(), Strides{2});
EXPECT_EQ(max_pool->get_channel_count(), 3);
EXPECT_EQ(max_pool->get_input_image_shape(), Shape{5});
EXPECT_EQ(max_pool->get_output_image_shape(), Shape{2});
EXPECT_EQ(max_pool->get_window_shape(), Shape{2});
EXPECT_EQ(max_pool->get_batch_size(), 64);
EXPECT_EQ(max_pool->get_image_dimension_count(), 1);
}
TEST(type_prop, max_pool_1d_deduce_strided_small_even)
{
// Deduce type
auto param = make_shared<op::Parameter>(element::f32, Shape{64, 3, 6});
auto window_shape = Shape{2};
auto move_strides = Strides{2};
auto max_pool = make_shared<op::MaxPool>(param, window_shape, move_strides);
EXPECT_EQ(max_pool->get_element_type(), element::f32);
EXPECT_EQ(max_pool->get_shape(), (Shape{64, 3, 3}));
EXPECT_EQ(max_pool->get_window_movement_strides(), Strides{2});
EXPECT_EQ(max_pool->get_channel_count(), 3);
EXPECT_EQ(max_pool->get_input_image_shape(), Shape{6});
EXPECT_EQ(max_pool->get_output_image_shape(), Shape{3});
EXPECT_EQ(max_pool->get_window_shape(), Shape{2});
EXPECT_EQ(max_pool->get_batch_size(), 64);
EXPECT_EQ(max_pool->get_image_dimension_count(), 1);
}
TEST(type_prop, max_pool_2d_deduce)
{
// Deduce type
auto param = make_shared<op::Parameter>(element::f32, Shape{64, 3, 100, 150});
auto window_shape = Shape{10, 20};
auto max_pool = make_shared<op::MaxPool>(param, window_shape);
EXPECT_EQ(max_pool->get_element_type(), element::f32);
EXPECT_EQ(max_pool->get_shape(), (Shape{64, 3, 91, 131}));
EXPECT_EQ(max_pool->get_window_movement_strides(), (Strides{1, 1}));
EXPECT_EQ(max_pool->get_channel_count(), 3);
EXPECT_EQ(max_pool->get_input_image_shape(), (Shape{100, 150}));
EXPECT_EQ(max_pool->get_output_image_shape(), (Shape{91, 131}));
EXPECT_EQ(max_pool->get_window_shape(), (Shape{10, 20}));
EXPECT_EQ(max_pool->get_batch_size(), 64);
EXPECT_EQ(max_pool->get_image_dimension_count(), 2);
}
TEST(type_prop, max_pool_2d_deduce_strided)
{
// Deduce type
auto param = make_shared<op::Parameter>(element::f32, Shape{64, 3, 100, 150});
auto window_shape = Shape{10, 20};
auto move_strides = Strides{2, 3};
auto max_pool = make_shared<op::MaxPool>(param, window_shape, move_strides);
EXPECT_EQ(max_pool->get_element_type(), element::f32);
EXPECT_EQ(max_pool->get_shape(), (Shape{64, 3, 46, 44}));
EXPECT_EQ(max_pool->get_window_movement_strides(), (Strides{2, 3}));
EXPECT_EQ(max_pool->get_channel_count(), 3);
EXPECT_EQ(max_pool->get_input_image_shape(), (Shape{100, 150}));
EXPECT_EQ(max_pool->get_output_image_shape(), (Shape{46, 44}));
EXPECT_EQ(max_pool->get_window_shape(), (Shape{10, 20}));
EXPECT_EQ(max_pool->get_batch_size(), 64);
EXPECT_EQ(max_pool->get_image_dimension_count(), 2);
}
TEST(type_prop, max_pool_3d_deduce_strided_small)
{
// Deduce type
auto param = make_shared<op::Parameter>(element::f32, Shape{64, 3, 7, 8, 10});
auto window_shape = Shape{2, 3, 2};
auto move_strides = Strides{2, 3, 4};
auto max_pool = make_shared<op::MaxPool>(param, window_shape, move_strides);
EXPECT_EQ(max_pool->get_element_type(), element::f32);
EXPECT_EQ(max_pool->get_shape(), (Shape{64, 3, 3, 2, 3}));
EXPECT_EQ(max_pool->get_window_movement_strides(), (Strides{2, 3, 4}));
EXPECT_EQ(max_pool->get_channel_count(), 3);
EXPECT_EQ(max_pool->get_input_image_shape(), (Shape{7, 8, 10}));
EXPECT_EQ(max_pool->get_output_image_shape(), (Shape{3, 2, 3}));
EXPECT_EQ(max_pool->get_window_shape(), (Shape{2, 3, 2}));
EXPECT_EQ(max_pool->get_batch_size(), 64);
EXPECT_EQ(max_pool->get_image_dimension_count(), 3);
}
TEST(type_prop, max_pool_invalid_0d_input)
{
// Deduce type
auto param = make_shared<op::Parameter>(element::f32, Shape{});
auto window_shape = Shape{};
try
{
auto max_pool = make_shared<op::MaxPool>(param, window_shape);
// Should have thrown, so fail if it didn't
FAIL() << "Invalid 0D input not detected";
}
catch (const ngraph_error& error)
{
EXPECT_EQ(error.what(),
std::string("Max pool image batch input must have rank of at "
"least 3 (one batch axis, one channel axis, at "
"least one image dimension)."));
}
catch (...)
{
FAIL() << "Deduced type check failed for unexpected reason";
}
}
TEST(type_prop, max_pool_invalid_1d_input)
{
// Deduce type
auto param = make_shared<op::Parameter>(element::f32, Shape{2});
auto window_shape = Shape{};
try
{
auto max_pool = make_shared<op::MaxPool>(param, window_shape);
// Should have thrown, so fail if it didn't
FAIL() << "Invalid 1D input not detected";
}
catch (const ngraph_error& error)
{
EXPECT_EQ(error.what(),
std::string("Max pool image batch input must have rank of at "
"least 3 (one batch axis, one channel axis, at "
"least one image dimension)."));
}
catch (...)
{
FAIL() << "Deduced type check failed for unexpected reason";
}
}
TEST(type_prop, max_pool_invalid_2d_input)
{
// Deduce type
auto param = make_shared<op::Parameter>(element::f32, Shape{2, 6});
auto window_shape = Shape{};
try
{
auto max_pool = make_shared<op::MaxPool>(param, window_shape);
// Should have thrown, so fail if it didn't
FAIL() << "Invalid 2D input not detected";
}
catch (const ngraph_error& error)
{
EXPECT_EQ(error.what(),
std::string("Max pool image batch input must have rank of at "
"least 3 (one batch axis, one channel axis, at "
"least one image dimension)."));
}
catch (...)
{
FAIL() << "Deduced type check failed for unexpected reason";
}
}
TEST(type_prop, max_pool_invalid_0_batch_size)
{
// Deduce type
auto param = make_shared<op::Parameter>(element::f32, Shape{0, 6, 1});
auto window_shape = Shape{1};
try
{
auto max_pool = make_shared<op::MaxPool>(param, window_shape);
// Should have thrown, so fail if it didn't
FAIL() << "Invalid input with 0 batch size not detected";
}
catch (const ngraph_error& error)
{
EXPECT_EQ(error.what(), std::string("Max pool image batch size is zero."));
}
catch (...)
{
FAIL() << "Deduced type check failed for unexpected reason";
}
}
TEST(type_prop, max_pool_invalid_0_channels)
{
// Deduce type
auto param = make_shared<op::Parameter>(element::f32, Shape{6, 0, 1});
auto window_shape = Shape{1};
try
{
auto max_pool = make_shared<op::MaxPool>(param, window_shape);
// Should have thrown, so fail if it didn't
FAIL() << "Invalid input with 0 channels not detected";
}
catch (const ngraph_error& error)
{
EXPECT_EQ(error.what(), std::string("Max pool requires at least one image depth channel."));
}
catch (...)
{
FAIL() << "Deduced type check failed for unexpected reason";
}
}
TEST(type_prop, max_pool_invalid_wrong_number_of_window_dimensions_too_many)
{
// Deduce type
auto param = make_shared<op::Parameter>(element::f32, Shape{6, 2, 10, 10});
auto window_shape = Shape{3, 3, 3};
try
{
auto max_pool = make_shared<op::MaxPool>(param, window_shape);
// Should have thrown, so fail if it didn't
FAIL() << "Invalid input with too many window dimensions not detected";
}
catch (const ngraph_error& error)
{
EXPECT_EQ(
error.what(),
std::string("Max pool window shape rank does not match number of image dimensions."));
}
catch (...)
{
FAIL() << "Deduced type check failed for unexpected reason";
}
}
TEST(type_prop, max_pool_invalid_wrong_number_of_window_dimensions_too_few)
{
// Deduce type
auto param = make_shared<op::Parameter>(element::f32, Shape{6, 2, 10, 10});
auto window_shape = Shape{3};
try
{
auto max_pool = make_shared<op::MaxPool>(param, window_shape);
// Should have thrown, so fail if it didn't
FAIL() << "Invalid input with too few window dimensions not detected";
}
catch (const ngraph_error& error)
{
EXPECT_EQ(
error.what(),
std::string("Max pool window shape rank does not match number of image dimensions."));
}
catch (...)
{
FAIL() << "Deduced type check failed for unexpected reason";
}
}
TEST(type_prop, max_pool_invalid_movement_stride_rank)
{
// Deduce type
auto param = make_shared<op::Parameter>(element::f32, Shape{6, 2, 10, 10});
auto window_shape = Shape{3, 3};
auto move_strides = Strides{2, 3, 8};
try
{
auto max_pool = make_shared<op::MaxPool>(param, window_shape, move_strides);
// Should have thrown, so fail if it didn't
FAIL() << "Invalid input with wrong movement stride rank not detected";
}
catch (const ngraph_error& error)
{
EXPECT_EQ(error.what(),
std::string("Max pool window movement stride rank does not "
"match number of image dimensions."));
}
catch (...)
{
FAIL() << "Deduced type check failed for unexpected reason";
}
}
TEST(type_prop, max_pool_invalid_input_image_size_0)
{
// Deduce type
auto param = make_shared<op::Parameter>(element::f32, Shape{6, 2, 0, 10});
auto window_shape = Shape{3, 3};
try
{
auto max_pool = make_shared<op::MaxPool>(param, window_shape);
// Should have thrown, so fail if it didn't
FAIL() << "Invalid input with zero-length image axis not detected";
}
catch (const ngraph_error& error)
{
EXPECT_EQ(error.what(), std::string("Max pool input image dimension is zero."));
}
catch (...)
{
FAIL() << "Deduced type check failed for unexpected reason";
}
}
TEST(type_prop, max_pool_invalid_window_size_0)
{
// Deduce type
auto param = make_shared<op::Parameter>(element::f32, Shape{6, 2, 10, 10});
auto window_shape = Shape{3, 0};
try
{
auto max_pool = make_shared<op::MaxPool>(param, window_shape);
// Should have thrown, so fail if it didn't
FAIL() << "Invalid input with zero-length window axis not detected";
}
catch (const ngraph_error& error)
{
EXPECT_EQ(error.what(), std::string("Max pool window shape has a zero-length axis."));
}
catch (...)
{
FAIL() << "Deduced type check failed for unexpected reason";
}
}
TEST(type_prop, max_pool_invalid_dilated_too_large)
{
// Deduce type
auto param = make_shared<op::Parameter>(element::f32, Shape{6, 2, 8, 8});
auto window_shape = Shape{9, 9};
try
{
auto max_pool = make_shared<op::MaxPool>(param, window_shape);
// Should have thrown, so fail if it didn't
FAIL() << "Invalid input with oversized window not detected";
}
catch (const ngraph_error& error)
{
EXPECT_EQ(error.what(), std::string("Max pool window shape is larger than the image."));
}
catch (...)
{
FAIL() << "Deduced type check failed for unexpected reason";
}
}
TEST(type_prop, max_pool_invalid_movement_stride_0)
{
// Deduce type
auto param = make_shared<op::Parameter>(element::f32, Shape{6, 2, 10, 10});
auto window_shape = Shape{3, 3};
auto move_strides = Strides{0, 1};
try
{
auto max_pool = make_shared<op::MaxPool>(param, window_shape, move_strides);
// Should have thrown, so fail if it didn't
FAIL() << "Invalid input with 0-length movement stride axis not detected";
}
catch (const ngraph_error& error)
{
EXPECT_EQ(error.what(), std::string("Max pool window axis movement stride is zero."));
}
catch (...)
{
FAIL() << "Deduced type check failed for unexpected reason";
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment