Unverified Commit 5e1d724e authored by Scott Cyphers's avatar Scott Cyphers Committed by GitHub

Merge branch 'master' into cyphers/expdesc

parents 106a9b21 b42f3499
......@@ -43,6 +43,8 @@ namespace
using namespace mlir::edsc::op;
using namespace ngraph::runtime;
using namespace ngraph::runtime::ngmlir;
// Index notation to generate standard (i.e., non-affine) loads and stores.
using StdIndexedValue = TemplatedIndexedValue<intrinsics::std_load, intrinsics::std_store>;
class DialectLoweringPass;
......@@ -682,7 +684,8 @@ namespace
// Create view to write into result.
MemRefView vRes(result), vParams(params), vIndices(indices);
// Indexed Values
IndexedValue iRes(result), iParams(params), iIndices(indices);
IndexedValue iRes(result), iIndices(indices);
StdIndexedValue iParams(params);
// Construct outer loop for params dims. Exclude the axis dim.
SmallVector<ValueHandle, 4> paramsLbs, paramsUbs;
......@@ -894,7 +897,8 @@ namespace
// Views
MemRefView vRes(result), vArg(arg);
// Index Values
IndexedValue iRes(result), iArg(arg);
StdIndexedValue iRes(result), stdArg(arg);
IndexedValue affineArg(arg);
// Bounds Index Handles
auto resLbs = vRes.getLbs();
auto resUbs = vRes.getUbs();
......@@ -944,9 +948,9 @@ namespace
ValueHandle newRedIdx =
std::is_same<RedOp, NGArgMinRedOp>()
? edsc::intrinsics::select(
iArg(allIVs) < iArg(tempIVs), allIVs[axis], currRedIdx)
affineArg(allIVs) < stdArg(tempIVs), allIVs[axis], currRedIdx)
: edsc::intrinsics::select(
iArg(tempIVs) < iArg(allIVs), allIVs[axis], currRedIdx);
stdArg(tempIVs) < affineArg(allIVs), allIVs[axis], currRedIdx);
iRes(nonRedIVs) = ValueHandle::create<IndexCastOp>(newRedIdx, resTy);
});
......
......@@ -173,6 +173,7 @@ namespace ngraph
class AvgPoolBackprop : public Op
{
public:
NGRAPH_API
static const std::string type_name;
const std::string& description() const override { return type_name; }
AvgPoolBackprop() = default;
......
......@@ -92,6 +92,7 @@ namespace ngraph
class BatchNormInference : public Op
{
public:
NGRAPH_API
static const std::string type_name;
const std::string& description() const override { return type_name; }
BatchNormInference() = default;
......
......@@ -21,12 +21,14 @@
using namespace std;
using namespace ngraph;
const string op::Pad::type_name{"Pad"};
op::Pad::Pad(const shared_ptr<Node>& arg,
const shared_ptr<Node>& arg_pad_value,
const CoordinateDiff& padding_below,
const CoordinateDiff& padding_above,
PadMode pad_mode)
: Op("Pad", check_single_output_args({arg, arg_pad_value}))
: Op(check_single_output_args({arg, arg_pad_value}))
, m_padding_below(padding_below)
, m_padding_above(padding_above)
, m_padding_interior_fake(padding_below.size())
......
......@@ -28,6 +28,9 @@ namespace ngraph
class Pad : public Op
{
public:
NGRAPH_API
static const std::string type_name;
const std::string& description() const override { return type_name; }
/// \brief Constructs a generic padding operation.
///
/// \param arg The node producing input tensor to be padded.
......
......@@ -21,10 +21,12 @@
using namespace std;
using namespace ngraph;
const string op::Parameter::type_name{"Parameter"};
op::Parameter::Parameter(const element::Type& element_type,
const PartialShape& pshape,
const bool cacheable)
: Op("Parameter", {})
: Op(NodeVector{})
, m_cacheable(cacheable)
, m_partial_shape(pshape)
, m_element_type(element_type)
......
......@@ -35,6 +35,9 @@ namespace ngraph
const NodeVector& deltas) override;
public:
NGRAPH_API
static const std::string type_name;
const std::string& description() const override { return type_name; }
/// \brief Constructions a tensor-typed parameter node.
///
/// \param element_type The element type of the parameter.
......
......@@ -18,12 +18,17 @@
#include "ngraph/op/passthrough.hpp"
using namespace std;
using namespace ngraph;
const string op::Passthrough::type_name{"Passthrough"};
ngraph::op::Passthrough::Passthrough(const std::string& logical_type,
const std::string& language,
const std::string& function,
const NodeVector& args,
std::vector<std::tuple<element::Type, PartialShape>> outputs)
: Op{"Passthrough", args}
: Op{args}
, m_logical_type{logical_type}
, m_language{language}
, m_function{function}
......@@ -65,5 +70,5 @@ std::shared_ptr<ngraph::Node>
"Passthrough node input counts cannot be changed for a given Passthrough function"};
}
return std::make_shared<Passthrough>(
description(), m_language, m_function, new_args, m_output_shapes);
m_logical_type, m_language, m_function, new_args, m_output_shapes);
}
......@@ -38,6 +38,9 @@ namespace ngraph
class ngraph::op::Passthrough final : public Op
{
public:
NGRAPH_API
static const std::string type_name;
const std::string& description() const override { return type_name; }
Passthrough(const std::string& logical_type, // aka "What this operation is doing"
const std::string& language, // The language the implementation is written in
const std::string& function, // The operation implementation
......
......@@ -22,10 +22,12 @@
using namespace std;
using namespace ngraph;
const string op::Power::type_name{"Power"};
op::Power::Power(const shared_ptr<Node>& arg0,
const shared_ptr<Node>& arg1,
const AutoBroadcastSpec& autob)
: BinaryElementwiseArithmetic("Power", arg0, arg1, autob)
: BinaryElementwiseArithmetic(arg0, arg1, autob)
{
constructor_validate_and_infer_types();
}
......
......@@ -39,6 +39,9 @@ namespace ngraph
class Power : public util::BinaryElementwiseArithmetic
{
public:
NGRAPH_API
static const std::string type_name;
const std::string& description() const override { return type_name; }
/// \brief Constructs an exponentiation operation.
///
/// \param arg0 Node that produces the first input tensor.
......
......@@ -20,8 +20,11 @@
using namespace std;
using namespace ngraph;
const string op::Relu::type_name{"Relu"};
const string op::ReluBackprop::type_name{"ReluBackprop"};
op::Relu::Relu(shared_ptr<Node> arg)
: UnaryElementwiseArithmetic("Relu", {arg})
: UnaryElementwiseArithmetic(arg)
{
constructor_validate_and_infer_types();
}
......@@ -33,7 +36,7 @@ shared_ptr<Node> op::Relu::copy_with_new_args(const NodeVector& new_args) const
}
op::ReluBackprop::ReluBackprop(shared_ptr<Node> arg, shared_ptr<Node> delta)
: BinaryElementwiseArithmetic("ReluBackprop", arg, delta)
: BinaryElementwiseArithmetic(arg, delta)
{
constructor_validate_and_infer_types();
}
......
......@@ -33,6 +33,9 @@ namespace ngraph
class Relu : public ngraph::op::util::UnaryElementwiseArithmetic
{
public:
NGRAPH_API
static const std::string type_name;
const std::string& description() const override { return type_name; }
/// \brief Constructs a Relu operation.
///
/// \param arg Node that produces the input tensor.
......@@ -50,6 +53,9 @@ namespace ngraph
class ReluBackprop : public ngraph::op::util::BinaryElementwiseArithmetic
{
public:
NGRAPH_API
static const std::string type_name;
const std::string& description() const override { return type_name; }
/// \brief Constructs a ReluBackprop operation.
///
/// \param arg Node that produces the relu forward input tensor.
......
......@@ -23,8 +23,10 @@
using namespace std;
using namespace ngraph;
const string op::Reverse::type_name{"Reverse"};
op::Reverse::Reverse(const shared_ptr<Node>& arg, const AxisSet& reversed_axes)
: Op("Reverse", check_single_output_args({arg}))
: Op(check_single_output_args({arg}))
, m_reversed_axes(reversed_axes)
{
constructor_validate_and_infer_types();
......
......@@ -46,6 +46,9 @@ namespace ngraph
class Reverse : public Op
{
public:
NGRAPH_API
static const std::string type_name;
const std::string& description() const override { return type_name; }
/// \brief Constructs a reverse operation.
///
/// \param arg The input tensor, some of whose axes are to be reversed.
......
......@@ -25,11 +25,13 @@
using namespace std;
using namespace ngraph;
const string op::ReverseSequence::type_name{"ReverseSequence"};
op::ReverseSequence::ReverseSequence(const std::shared_ptr<Node> arg,
const std::shared_ptr<Node> seq_indices,
size_t batch_axis,
size_t seq_axis)
: Op("ReverseSequence", check_single_output_args({arg, seq_indices}))
: Op(check_single_output_args({arg, seq_indices}))
, m_batch_axis(batch_axis)
, m_seq_axis(seq_axis)
{
......
......@@ -25,6 +25,9 @@ namespace ngraph
class ReverseSequence : public Op
{
public:
NGRAPH_API
static const std::string type_name;
const std::string& description() const override { return type_name; }
/// \brief Constructs an arcsin operation.
///
/// \param arg Node that produces the input tensor.
......
......@@ -24,6 +24,8 @@ static int INPUTS = 0;
static int INDICES = 1;
static int UPDATES = 2;
const string op::ScatterAdd::type_name{"ScatterAdd"};
shared_ptr<Node> op::ScatterAdd::copy_with_new_args(const NodeVector& new_args) const
{
check_new_args_count(this, new_args);
......
......@@ -26,13 +26,16 @@ namespace ngraph
class ScatterAdd : public Op
{
public:
NGRAPH_API
static const std::string type_name;
const std::string& description() const override { return type_name; }
/// \param inputs Tensor
/// \param indices Index tensor: Data type must be `element::i32` or `element::i64`
/// \param updates Tensor: Must have same type as inputs
ScatterAdd(const std::shared_ptr<Node>& inputs,
const std::shared_ptr<Node>& indices,
const std::shared_ptr<Node>& updates)
: Op("ScatterAdd", check_single_output_args({inputs, indices, updates}))
: Op(check_single_output_args({inputs, indices, updates}))
{
constructor_validate_and_infer_types();
}
......
......@@ -24,6 +24,8 @@ static int INPUTS = 0;
static int INDICES = 1;
static int UPDATES = 2;
const string op::ScatterNDAdd::type_name{"ScatterNDAdd"};
shared_ptr<Node> op::ScatterNDAdd::copy_with_new_args(const NodeVector& new_args) const
{
check_new_args_count(this, new_args);
......
......@@ -26,13 +26,16 @@ namespace ngraph
class ScatterNDAdd : public Op
{
public:
NGRAPH_API
static const std::string type_name;
const std::string& description() const override { return type_name; }
/// \param inputs Tensor
/// \param indices Index tensor: Data type must be `element::i32` or `element::i64`
/// \param updates Tensor: Must have same type as inputs
ScatterNDAdd(const std::shared_ptr<Node>& inputs,
const std::shared_ptr<Node>& indices,
const std::shared_ptr<Node>& updates)
: Op("ScatterNDAdd", check_single_output_args({inputs, indices, updates}))
: Op(check_single_output_args({inputs, indices, updates}))
{
constructor_validate_and_infer_types();
}
......
......@@ -21,6 +21,9 @@
using namespace std;
using namespace ngraph;
const string op::Sigmoid::type_name{"Sigmoid"};
const string op::SigmoidBackprop::type_name{"SigmoidBackprop"};
shared_ptr<Node> op::Sigmoid::copy_with_new_args(const NodeVector& new_args) const
{
check_new_args_count(this, new_args);
......@@ -28,13 +31,13 @@ shared_ptr<Node> op::Sigmoid::copy_with_new_args(const NodeVector& new_args) con
}
op::Sigmoid::Sigmoid(shared_ptr<Node> arg)
: UnaryElementwiseArithmetic("Sigmoid", arg)
: UnaryElementwiseArithmetic(arg)
{
constructor_validate_and_infer_types();
}
op::SigmoidBackprop::SigmoidBackprop(shared_ptr<Node> arg, shared_ptr<Node> delta)
: BinaryElementwiseArithmetic("SigmoidBackprop", arg, delta)
: BinaryElementwiseArithmetic(arg, delta)
{
constructor_validate_and_infer_types();
}
......
......@@ -28,6 +28,9 @@ namespace ngraph
class Sigmoid : public util::UnaryElementwiseArithmetic
{
public:
NGRAPH_API
static const std::string type_name;
const std::string& description() const override { return type_name; }
Sigmoid(std::shared_ptr<Node> arg);
virtual std::shared_ptr<Node>
copy_with_new_args(const NodeVector& new_args) const override;
......@@ -40,6 +43,9 @@ namespace ngraph
class SigmoidBackprop : public util::BinaryElementwiseArithmetic
{
public:
NGRAPH_API
static const std::string type_name;
const std::string& description() const override { return type_name; }
/// \brief Constructs a SigmoidBackprop operation.
///
/// \param arg Node that produces the Sigmoid forward input tensor.
......
......@@ -19,8 +19,10 @@
using namespace std;
using namespace ngraph;
const string op::Sign::type_name{"Sign"};
op::Sign::Sign(const shared_ptr<Node>& arg)
: UnaryElementwiseArithmetic("Sign", arg)
: UnaryElementwiseArithmetic(arg)
{
constructor_validate_and_infer_types();
}
......
......@@ -27,6 +27,9 @@ namespace ngraph
class Sign : public util::UnaryElementwiseArithmetic
{
public:
NGRAPH_API
static const std::string type_name;
const std::string& description() const override { return type_name; }
/// \brief Constructs an elementwise sign operation.
///
/// \param arg Node that produces the input tensor.
......
......@@ -21,8 +21,10 @@
using namespace std;
using namespace ngraph;
const string op::Sin::type_name{"Sin"};
op::Sin::Sin(const shared_ptr<Node>& arg)
: UnaryElementwiseArithmetic("Sin", arg)
: UnaryElementwiseArithmetic(arg)
{
constructor_validate_and_infer_types();
}
......
......@@ -38,6 +38,9 @@ namespace ngraph
class Sin : public util::UnaryElementwiseArithmetic
{
public:
NGRAPH_API
static const std::string type_name;
const std::string& description() const override { return type_name; }
/// \brief Constructs a sine operation.
///
/// \param arg Node that produces the input tensor.
......
......@@ -21,8 +21,10 @@
using namespace std;
using namespace ngraph;
const string op::Sinh::type_name{"Sinh"};
op::Sinh::Sinh(const shared_ptr<Node>& arg)
: UnaryElementwiseArithmetic("Sinh", arg)
: UnaryElementwiseArithmetic(arg)
{
constructor_validate_and_infer_types();
}
......
......@@ -26,6 +26,9 @@ namespace ngraph
class Sinh : public util::UnaryElementwiseArithmetic
{
public:
NGRAPH_API
static const std::string type_name;
const std::string& description() const override { return type_name; }
/// \brief Constructs a hyperbolic sine operation.
///
/// \param arg Node that produces the input tensor.
......
......@@ -29,8 +29,10 @@
using namespace std;
using namespace ngraph;
const string op::Softmax::type_name{"Softmax"};
op::Softmax::Softmax(const shared_ptr<Node>& arg, const AxisSet& axes)
: UnaryElementwiseArithmetic("Softmax", arg)
: UnaryElementwiseArithmetic(arg)
, m_axes(axes)
{
constructor_validate_and_infer_types();
......
......@@ -27,6 +27,9 @@ namespace ngraph
class Softmax : public util::UnaryElementwiseArithmetic
{
public:
NGRAPH_API
static const std::string type_name;
const std::string& description() const override { return type_name; }
/// \brief Constructs a softmax operation.
///
/// \param arg Node that produces the first input tensor.<br>
......
......@@ -21,8 +21,10 @@
using namespace std;
using namespace ngraph;
const string op::Sqrt::type_name{"Sqrt"};
op::Sqrt::Sqrt(const shared_ptr<Node>& arg)
: UnaryElementwiseArithmetic("Sqrt", arg)
: UnaryElementwiseArithmetic(arg)
{
constructor_validate_and_infer_types();
}
......
......@@ -38,6 +38,9 @@ namespace ngraph
class Sqrt : public util::UnaryElementwiseArithmetic
{
public:
NGRAPH_API
static const std::string type_name;
const std::string& description() const override { return type_name; }
/// \brief Constructs a square operation.
///
/// \param arg Node that produces the input tensor.
......
......@@ -21,8 +21,10 @@
using namespace std;
using namespace ngraph;
const string op::StopGradient::type_name{"StopGradient"};
op::StopGradient::StopGradient(const shared_ptr<Node>& arg)
: UnaryElementwiseArithmetic("StopGradient", arg)
: UnaryElementwiseArithmetic(arg)
{
constructor_validate_and_infer_types();
}
......
......@@ -26,6 +26,9 @@ namespace ngraph
class StopGradient : public util::UnaryElementwiseArithmetic
{
public:
NGRAPH_API
static const std::string type_name;
const std::string& description() const override { return type_name; }
/// \brief Constructs StopGradient
///
/// \param arg Node that produces the input tensor.
......
......@@ -22,8 +22,10 @@
using namespace std;
using namespace ngraph;
const string op::Tan::type_name{"Tan"};
op::Tan::Tan(const shared_ptr<Node>& arg)
: UnaryElementwiseArithmetic("Tan", arg)
: UnaryElementwiseArithmetic(arg)
{
constructor_validate_and_infer_types();
}
......
......@@ -38,6 +38,9 @@ namespace ngraph
class Tan : public util::UnaryElementwiseArithmetic
{
public:
NGRAPH_API
static const std::string type_name;
const std::string& description() const override { return type_name; }
/// \brief Constructs a tangent operation.
///
/// \param arg Node that produces the input tensor.
......
......@@ -21,8 +21,10 @@
using namespace std;
using namespace ngraph;
const string op::Tanh::type_name{"Tanh"};
op::Tanh::Tanh(const shared_ptr<Node>& arg)
: UnaryElementwiseArithmetic("Tanh", arg)
: UnaryElementwiseArithmetic(arg)
{
constructor_validate_and_infer_types();
}
......
......@@ -26,6 +26,9 @@ namespace ngraph
class Tanh : public util::UnaryElementwiseArithmetic
{
public:
NGRAPH_API
static const std::string type_name;
const std::string& description() const override { return type_name; }
/// \brief Constructs a hyperbolic tangent operation.
///
/// \param arg Node that produces the input tensor.
......
This diff is collapsed.
......@@ -49,7 +49,9 @@ public:
SLICE,
DYN_SLICE,
DYN_RESHAPE,
TRANSPOSE
TRANSPOSE,
RANGE,
SELECT
};
ConstantFolding(const ngraph::BuildNodeExecutorMap& cfmap = ngraph::BuildNodeExecutorMap())
......@@ -74,6 +76,8 @@ public:
construct_constant_dyn_slice();
construct_constant_dyn_reshape();
construct_constant_transpose();
construct_constant_range();
construct_constant_select();
}
//this allows to specify the order in which matchers will be run
......@@ -105,6 +109,8 @@ public:
case CFTransformations::DYN_SLICE: construct_constant_dyn_slice(); break;
case CFTransformations::DYN_RESHAPE: construct_constant_dyn_reshape(); break;
case CFTransformations::TRANSPOSE: construct_constant_transpose(); break;
case CFTransformations::RANGE: construct_constant_range(); break;
case CFTransformations::SELECT: construct_constant_select(); break;
}
}
}
......@@ -128,6 +134,8 @@ private:
void construct_constant_dyn_slice();
void construct_constant_dyn_reshape();
void construct_constant_transpose();
void construct_constant_range();
void construct_constant_select();
ngraph::BuildNodeExecutorMap m_cfmap;
};
......@@ -30,6 +30,7 @@
#include "ngraph/op/slice.hpp"
#include "ngraph/pattern/matcher.hpp"
#include "ngraph/pattern/op/label.hpp"
#include "ngraph/runtime/reference/range.hpp"
#include "ngraph/slice_plan.hpp"
using namespace std;
......@@ -342,11 +343,10 @@ void pass::DynElimination::construct_dyn_reshape()
}
template <typename T>
std::shared_ptr<op::Constant>
make_range_replacement_integral(const element::Type& et,
const Shape& shape,
const std::shared_ptr<op::Constant>& start_arg,
const std::shared_ptr<op::Constant>& step_arg)
std::shared_ptr<op::Constant> make_range_replacement(const element::Type& et,
const Shape& shape,
const std::shared_ptr<op::Constant>& start_arg,
const std::shared_ptr<op::Constant>& step_arg)
{
std::vector<T> elements(shape_size(shape));
std::vector<T> start_vec = start_arg->get_vector<T>();
......@@ -354,40 +354,7 @@ std::shared_ptr<op::Constant>
NGRAPH_CHECK(start_vec.size() == 1 && step_vec.size() == 1);
T start = start_vec[0];
T step = step_vec[0];
T val = start;
for (size_t i = 0; i < elements.size(); i++)
{
elements[i] = val;
val = val + step;
}
return make_shared<op::Constant>(et, shape, elements);
}
template <typename T>
std::shared_ptr<op::Constant>
make_range_replacement_floating(const element::Type& et,
const Shape& shape,
const std::shared_ptr<op::Constant>& start_arg,
const std::shared_ptr<op::Constant>& step_arg)
{
std::vector<T> elements(shape_size(shape));
std::vector<T> start_vec = start_arg->get_vector<T>();
std::vector<T> step_vec = step_arg->get_vector<T>();
NGRAPH_CHECK(start_vec.size() == 1 && step_vec.size() == 1);
T start = start_vec[0];
T step = step_vec[0];
for (size_t i = 0; i < elements.size(); i++)
{
elements[i] = start + (static_cast<T>(i) * step);
}
runtime::reference::range<T>(start_vec.data(), step_vec.data(), shape, elements.data());
return make_shared<op::Constant>(et, shape, elements);
}
......@@ -426,40 +393,40 @@ void pass::DynElimination::construct_range()
switch (et.get_type_enum())
{
case element::Type_t::bf16:
replacement = make_range_replacement_floating<bfloat16>(et, shape, start_arg, step_arg);
replacement = make_range_replacement<bfloat16>(et, shape, start_arg, step_arg);
break;
case element::Type_t::f16:
replacement = make_range_replacement_floating<float16>(et, shape, start_arg, step_arg);
replacement = make_range_replacement<float16>(et, shape, start_arg, step_arg);
break;
case element::Type_t::f32:
replacement = make_range_replacement_floating<float>(et, shape, start_arg, step_arg);
replacement = make_range_replacement<float>(et, shape, start_arg, step_arg);
break;
case element::Type_t::f64:
replacement = make_range_replacement_floating<double>(et, shape, start_arg, step_arg);
replacement = make_range_replacement<double>(et, shape, start_arg, step_arg);
break;
case element::Type_t::i8:
replacement = make_range_replacement_integral<int8_t>(et, shape, start_arg, step_arg);
replacement = make_range_replacement<int8_t>(et, shape, start_arg, step_arg);
break;
case element::Type_t::i16:
replacement = make_range_replacement_integral<int16_t>(et, shape, start_arg, step_arg);
replacement = make_range_replacement<int16_t>(et, shape, start_arg, step_arg);
break;
case element::Type_t::i32:
replacement = make_range_replacement_integral<int32_t>(et, shape, start_arg, step_arg);
replacement = make_range_replacement<int32_t>(et, shape, start_arg, step_arg);
break;
case element::Type_t::i64:
replacement = make_range_replacement_integral<int64_t>(et, shape, start_arg, step_arg);
replacement = make_range_replacement<int64_t>(et, shape, start_arg, step_arg);
break;
case element::Type_t::u8:
replacement = make_range_replacement_integral<uint8_t>(et, shape, start_arg, step_arg);
replacement = make_range_replacement<uint8_t>(et, shape, start_arg, step_arg);
break;
case element::Type_t::u16:
replacement = make_range_replacement_integral<uint16_t>(et, shape, start_arg, step_arg);
replacement = make_range_replacement<uint16_t>(et, shape, start_arg, step_arg);
break;
case element::Type_t::u32:
replacement = make_range_replacement_integral<uint32_t>(et, shape, start_arg, step_arg);
replacement = make_range_replacement<uint32_t>(et, shape, start_arg, step_arg);
break;
case element::Type_t::u64:
replacement = make_range_replacement_integral<uint64_t>(et, shape, start_arg, step_arg);
replacement = make_range_replacement<uint64_t>(et, shape, start_arg, step_arg);
break;
case element::Type_t::undefined:
case element::Type_t::dynamic:
......
//*****************************************************************************
// Copyright 2017-2019 Intel Corporation
//
// 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
// limitations under the License.
//*****************************************************************************
#pragma once
#include <cmath>
#include <type_traits>
#include "ngraph/axis_vector.hpp"
#include "ngraph/check.hpp"
#include "ngraph/coordinate_transform.hpp"
namespace ngraph
{
namespace runtime
{
namespace reference
{
// Return type is `void`, only enabled if `T` is a built-in FP
// type, or nGraph's `bfloat16` or `float16` type.
template <typename T>
typename std::enable_if<std::is_floating_point<T>::value ||
std::is_same<T, bfloat16>::value ||
std::is_same<T, float16>::value>::type
range(const T* start, const T* step, const Shape& out_shape, T* out)
{
for (size_t i = 0; i < shape_size(out_shape); i++)
{
out[i] = *start + (static_cast<T>(i) * (*step));
}
}
// Return type is `void`, only enabled if `T` is `is_integral`.
template <typename T>
typename std::enable_if<std::is_integral<T>::value>::type
range(const T* start, const T* step, const Shape& out_shape, T* out)
{
T val = *start;
for (size_t i = 0; i < shape_size(out_shape); i++)
{
out[i] = val;
val += *step;
}
}
}
}
}
......@@ -891,6 +891,94 @@ TEST(constant_folding, constant_transpose)
ASSERT_TRUE(test::all_close_f(values_permute, values_out, MIN_FLOAT_TOLERANCE_BITS));
}
void range_test_check(const vector<double>& values_out, const vector<double>& values_expected)
{
ASSERT_TRUE(test::all_close_f(values_out, values_expected, MIN_FLOAT_TOLERANCE_BITS));
}
void range_test_check(const vector<float>& values_out, const vector<float>& values_expected)
{
ASSERT_TRUE(test::all_close_f(values_out, values_expected, MIN_FLOAT_TOLERANCE_BITS));
}
template <typename T>
typename std::enable_if<std::is_integral<T>::value>::type
range_test_check(const vector<T>& values_out, const vector<T>& values_expected)
{
ASSERT_EQ(values_out, values_expected);
}
template <typename T>
void range_test(T start, T stop, T step, const vector<T>& values_expected)
{
vector<T> values_start{start};
vector<T> values_stop{stop};
vector<T> values_step{step};
auto constant_start = make_shared<op::Constant>(element::from<T>(), Shape{}, values_start);
auto constant_stop = make_shared<op::Constant>(element::from<T>(), Shape{}, values_stop);
auto constant_step = make_shared<op::Constant>(element::from<T>(), Shape{}, values_step);
auto range = make_shared<op::Range>(constant_start, constant_stop, constant_step);
auto f = make_shared<Function>(range, ParameterVector{});
pass::Manager pass_manager;
pass_manager.register_pass<pass::ConstantFolding>();
pass_manager.run_passes(f);
ASSERT_EQ(count_ops_of_type<op::Range>(f), 0);
ASSERT_EQ(count_ops_of_type<op::Constant>(f), 1);
auto new_const =
std::dynamic_pointer_cast<op::Constant>(f->get_results().at(0)->get_argument(0));
ASSERT_TRUE(new_const);
auto values_out = new_const->template get_vector<T>();
range_test_check(values_out, values_expected);
}
TEST(constant_folding, constant_range)
{
range_test<int8_t>(5, 12, 2, {5, 7, 9, 11});
range_test<int32_t>(5, 12, 2, {5, 7, 9, 11});
range_test<int64_t>(5, 12, 2, {5, 7, 9, 11});
range_test<uint64_t>(5, 12, 2, {5, 7, 9, 11});
range_test<double>(5, 12, 2, {5, 7, 9, 11});
range_test<float>(5, 12, 2, {5, 7, 9, 11});
range_test<int32_t>(5, 12, -2, {});
range_test<float>(12, 4, -2, {12, 10, 8, 6});
}
TEST(constant_folding, constant_select)
{
Shape shape{2, 4};
vector<char> values_selection{0, 1, 1, 0, 1, 0, 0, 1};
vector<int64_t> values_t{2, 4, 6, 8, 10, 12, 14, 16};
vector<int64_t> values_f{1, 3, 5, 7, 9, 11, 13, 15};
auto constant_selection = make_shared<op::Constant>(element::boolean, shape, values_selection);
auto constant_t = make_shared<op::Constant>(element::i64, shape, values_t);
auto constant_f = make_shared<op::Constant>(element::i64, shape, values_f);
auto select = make_shared<op::Select>(constant_selection, constant_t, constant_f);
auto f = make_shared<Function>(select, ParameterVector{});
pass::Manager pass_manager;
pass_manager.register_pass<pass::ConstantFolding>();
pass_manager.run_passes(f);
ASSERT_EQ(count_ops_of_type<op::Select>(f), 0);
ASSERT_EQ(count_ops_of_type<op::Constant>(f), 1);
auto new_const =
std::dynamic_pointer_cast<op::Constant>(f->get_results().at(0)->get_argument(0));
ASSERT_TRUE(new_const);
auto values_out = new_const->get_vector<int64_t>();
vector<int64_t> values_expected{1, 4, 6, 7, 10, 11, 13, 16};
ASSERT_EQ(values_expected, values_out);
}
TEST(constant_folding, pass_property)
{
auto pass = std::make_shared<ngraph::pass::ConstantFolding>();
......
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