Skip to content

Commit

Permalink
Merge branch 'master' into river/cpu_plugin_api_2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
riverlijunjie committed Nov 7, 2023
2 parents be44586 + cdd342e commit 3819fa3
Show file tree
Hide file tree
Showing 12 changed files with 419 additions and 221 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2018-2023 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
import platform

from collections.abc import Iterable
from copy import deepcopy
Expand Down
4 changes: 1 addition & 3 deletions src/core/include/openvino/op/matmul.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ class OPENVINO_API MatMul : public Op {

std::shared_ptr<Node> clone_with_new_inputs(const OutputVector& new_args) const override;

OPENVINO_SUPPRESS_DEPRECATED_START
bool evaluate(const HostTensorVector& outputs, const HostTensorVector& inputs) const override;
OPENVINO_SUPPRESS_DEPRECATED_END
bool evaluate(TensorVector& outputs, const TensorVector& inputs) const override;
bool has_evaluate() const override;

bool get_transpose_a() const {
Expand Down
5 changes: 1 addition & 4 deletions src/core/include/openvino/op/sign.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ class OPENVINO_API Sign : public util::UnaryElementwiseArithmetic {
/// \param arg Node that produces the input tensor.
Sign(const Output<Node>& arg);

bool visit_attributes(AttributeVisitor& visitor) override;
std::shared_ptr<Node> clone_with_new_inputs(const OutputVector& new_args) const override;
OPENVINO_SUPPRESS_DEPRECATED_START
bool evaluate(const HostTensorVector& outputs, const HostTensorVector& inputs) const override;
OPENVINO_SUPPRESS_DEPRECATED_END
bool evaluate(TensorVector& outputs, const TensorVector& inputs) const override;
bool has_evaluate() const override;
};
} // namespace v0
Expand Down
8 changes: 2 additions & 6 deletions src/core/include/openvino/op/softmax.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ class OPENVINO_API Softmax : public Op {
void set_axis(const size_t axis) {
m_axis = axis;
}
OPENVINO_SUPPRESS_DEPRECATED_START
bool evaluate(const HostTensorVector& outputs, const HostTensorVector& inputs) const override;
OPENVINO_SUPPRESS_DEPRECATED_END
bool evaluate(TensorVector& outputs, const TensorVector& inputs) const override;
bool has_evaluate() const override;

private:
Expand Down Expand Up @@ -77,9 +75,7 @@ class OPENVINO_API Softmax : public Op {
void set_axis(const int64_t& axis) {
m_axis = axis;
}
OPENVINO_SUPPRESS_DEPRECATED_START
bool evaluate(const HostTensorVector& outputs, const HostTensorVector& inputs) const override;
OPENVINO_SUPPRESS_DEPRECATED_END
bool evaluate(TensorVector& outputs, const TensorVector& inputs) const override;
bool has_evaluate() const override;

private:
Expand Down
26 changes: 23 additions & 3 deletions src/core/reference/include/openvino/reference/sign.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,35 @@

#pragma once

#include <algorithm>
#include <cstddef>

#include "openvino/reference/utils/type_util.hpp"

namespace ov {
namespace reference {
namespace func {
template <class T, typename std::enable_if<std::is_unsigned<T>::value>::type* = nullptr>
constexpr T sign(const T v) {
return static_cast<T>(static_cast<bool>(v));
}

template <class T, typename std::enable_if<ov::is_floating_point<T>() || std::is_signed<T>::value>::type* = nullptr>
constexpr T sign(const T v) {
return static_cast<T>((T{0} < v) - (v < T{0}));
}
} // namespace func

/**
* @brief Reference implementation of Sign operator.
*
* @param arg Pointer to input data.
* @param out Pointer to output data.
* @param count Number of elements in input buffer.
*/
template <typename T>
void sign(const T* arg, T* out, size_t count) {
for (size_t i = 0; i < count; i++) {
out[i] = (arg[i] < T(0) ? T(-1) : (arg[i] > T(0) ? T(1) : T(0)));
}
std::transform(arg, arg + count, out, func::sign<T>);
}
} // namespace reference
} // namespace ov
142 changes: 67 additions & 75 deletions src/core/src/op/matmul.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,110 +2,99 @@
// SPDX-License-Identifier: Apache-2.0
//

#include "ngraph/op/matmul.hpp"

#include <memory>
#include "openvino/op/matmul.hpp"

#include "element_visitor.hpp"
#include "itt.hpp"
#include "matmul_shape_inference.hpp"
#include "ngraph/attribute_visitor.hpp"
#include "openvino/reference/matmul.hpp"

using namespace std;
using namespace ngraph;
namespace ov {
namespace op {
namespace matmul {

struct Evaluate : element::NoAction<bool> {
using element::NoAction<bool>::visit;

template <element::Type_t ET, class T = fundamental_type_for<ET>>
static result_type visit(const Tensor& arg0,
const Tensor& arg1,
Tensor& out,
const Shape& shape0,
const Shape& shape1,
const Shape& out_shape,
const bool transpose_a,
const bool transpose_b) {
reference::matmul(arg0.data<const T>(),
arg1.data<const T>(),
out.data<T>(),
shape0,
shape1,
out_shape,
transpose_a,
transpose_b);
return true;
}
};
} // namespace matmul

namespace v0 {

op::MatMul::MatMul(const Output<Node>& A, const Output<Node>& B, const bool& transpose_a, const bool& transpose_b)
MatMul::MatMul(const Output<Node>& A, const Output<Node>& B, const bool& transpose_a, const bool& transpose_b)
: Op(OutputVector{A, B}),
m_transpose_a{transpose_a},
m_transpose_b{transpose_b} {
constructor_validate_and_infer_types();
}

bool ngraph::op::v0::MatMul::visit_attributes(AttributeVisitor& visitor) {
bool MatMul::visit_attributes(AttributeVisitor& visitor) {
OV_OP_SCOPE(v0_MatMul_visit_attributes);
visitor.on_attribute("transpose_a", m_transpose_a);
visitor.on_attribute("transpose_b", m_transpose_b);
return true;
}

shared_ptr<Node> op::MatMul::clone_with_new_inputs(const OutputVector& new_args) const {
std::shared_ptr<Node> MatMul::clone_with_new_inputs(const OutputVector& new_args) const {
OV_OP_SCOPE(v0_MatMul_clone_with_new_inputs);
check_new_args_count(this, new_args);
return make_shared<MatMul>(new_args.at(0), new_args.at(1), m_transpose_a, m_transpose_b);
}

OPENVINO_SUPPRESS_DEPRECATED_START
namespace matmul {
namespace {
template <element::Type_t ET>
bool evaluate(const op::MatMul* op, const HostTensorPtr& arg0, const HostTensorPtr& arg1, const HostTensorPtr& output) {
using T = typename element_type_traits<ET>::value_type;

ov::Shape arg0_shape = arg0->get_shape();
ov::Shape arg1_shape = arg1->get_shape();

std::vector<ov::PartialShape> input_shapes = {arg0_shape, arg1_shape};
std::vector<ov::PartialShape> output_shapes = shape_infer(op, input_shapes);

ov::Shape output_shape = output_shapes[0].to_shape();
output->set_element_type(arg0->get_element_type());
output->set_shape(output_shape);

ov::reference::matmul<T>(arg0->get_data_ptr<ET>(),
arg1->get_data_ptr<ET>(),
output->get_data_ptr<ET>(),
arg0_shape,
arg1_shape,
output_shape,
op->get_transpose_a(),
op->get_transpose_b());
return true;
}

bool evaluate_matmul(const op::MatMul* op,
const HostTensorPtr& arg0,
const HostTensorPtr& arg1,
const HostTensorPtr& output) {
bool rc = true;

switch (arg0->get_element_type()) {
OPENVINO_TYPE_CASE(evaluate_matmul, i32, op, arg0, arg1, output);
OPENVINO_TYPE_CASE(evaluate_matmul, i64, op, arg0, arg1, output);
OPENVINO_TYPE_CASE(evaluate_matmul, u32, op, arg0, arg1, output);
OPENVINO_TYPE_CASE(evaluate_matmul, u64, op, arg0, arg1, output);
OPENVINO_TYPE_CASE(evaluate_matmul, f16, op, arg0, arg1, output);
OPENVINO_TYPE_CASE(evaluate_matmul, f32, op, arg0, arg1, output);
default:
rc = false;
break;
}
return rc;
return std::make_shared<MatMul>(new_args.at(0), new_args.at(1), m_transpose_a, m_transpose_b);
}
} // namespace
} // namespace matmul

bool op::MatMul::evaluate(const HostTensorVector& outputs, const HostTensorVector& inputs) const {
bool MatMul::evaluate(TensorVector& outputs, const TensorVector& inputs) const {
OV_OP_SCOPE(v0_MatMul_evaluate);
return matmul::evaluate_matmul(this, inputs[0], inputs[1], outputs[0]);
OPENVINO_ASSERT(outputs.size() == 1);

const auto out_shape = shape_infer(this, ov::util::get_tensors_partial_shapes(inputs)).front().to_shape();
outputs[0].set_shape(out_shape);

using namespace ov::element;
return IfTypeOf<f16, f32, i32, i64, u32, u64>::apply<matmul::Evaluate>(inputs[0].get_element_type(),
inputs[0],
inputs[1],
outputs[0],
inputs[0].get_shape(),
inputs[1].get_shape(),
out_shape,
m_transpose_a,
m_transpose_b);
}

bool op::MatMul::has_evaluate() const {
bool MatMul::has_evaluate() const {
OV_OP_SCOPE(v0_MatMul_has_evaluate);
switch (get_input_element_type(0)) {
case ngraph::element::i32:
case ngraph::element::i64:
case ngraph::element::u32:
case ngraph::element::u64:
case ngraph::element::f16:
case ngraph::element::f32:
case element::f16:
case element::f32:
case element::i32:
case element::i64:
case element::u32:
case element::u64:
return true;
default:
break;
return false;
}
return false;
}

void ngraph::op::v0::MatMul::validate_and_infer_types() {
void MatMul::validate_and_infer_types() {
OV_OP_SCOPE(v0_MatMul_validate_and_infer_types);
element::Type result_et;

Expand All @@ -117,8 +106,11 @@ void ngraph::op::v0::MatMul::validate_and_infer_types() {
get_input_element_type(1),
").");

const auto &A_shape = get_input_partial_shape(0), B_shape = get_input_partial_shape(1);
std::vector<ov::PartialShape> input_shapes = {A_shape, B_shape};
std::vector<ov::PartialShape> output_shapes = shape_infer(this, input_shapes);
const auto& A_shape = get_input_partial_shape(0);
const auto& B_shape = get_input_partial_shape(1);
const auto output_shapes = shape_infer(this, std::vector<ov::PartialShape>{A_shape, B_shape});
set_output_type(0, result_et, output_shapes[0]);
}
} // namespace v0
} // namespace op
} // namespace ov
Loading

0 comments on commit 3819fa3

Please sign in to comment.