Commit c829a9c7 authored by Jaikrishnan Menon's avatar Jaikrishnan Menon

CPU Direct Execution: Implement Ceiling

Also, formatting fixes
parent b33fc6a2
...@@ -94,6 +94,7 @@ ...@@ -94,6 +94,7 @@
#include "ngraph/runtime/cpu/cpu_op_annotations.hpp" #include "ngraph/runtime/cpu/cpu_op_annotations.hpp"
#include "ngraph/runtime/cpu/kernel/abs.hpp" #include "ngraph/runtime/cpu/kernel/abs.hpp"
#include "ngraph/runtime/cpu/kernel/add.hpp" #include "ngraph/runtime/cpu/kernel/add.hpp"
#include "ngraph/runtime/cpu/kernel/ceil.hpp"
#include "ngraph/runtime/cpu/kernel/multiply.hpp" #include "ngraph/runtime/cpu/kernel/multiply.hpp"
#include "ngraph/runtime/cpu/kernel/relu.hpp" #include "ngraph/runtime/cpu/kernel/relu.hpp"
#include "ngraph/runtime/cpu/kernel/result.hpp" #include "ngraph/runtime/cpu/kernel/result.hpp"
...@@ -230,6 +231,25 @@ namespace ngraph ...@@ -230,6 +231,25 @@ namespace ngraph
functors.emplace_back(functor); functors.emplace_back(functor);
} }
template <>
void Builder::BUILDER_DECL(ngraph::op::Ceiling)
{
auto& functors = external_function->get_functors();
auto& tensor_data = external_function->get_tensor_data();
std::function<void(void*, void*, size_t)> kernel;
SELECT_KERNEL(kernel, out[0].get_element_type(), runtime::cpu::kernel::ceil);
auto element_count = out[0].get_size();
auto& arg0_tensor = tensor_data[args[0].get_name()];
auto& out0_tensor = tensor_data[out[0].get_name()];
auto functor = [&, kernel, element_count](CPURuntimeContext* ctx) {
kernel(arg0_tensor, out0_tensor, element_count);
};
functors.emplace_back(functor);
}
template <> template <>
void Builder::BUILDER_DECL(ngraph::op::Relu) void Builder::BUILDER_DECL(ngraph::op::Relu)
{ {
...@@ -307,23 +327,25 @@ namespace ngraph ...@@ -307,23 +327,25 @@ namespace ngraph
const float beta = 0.0f; const float beta = 0.0f;
auto mm_functor = [&, transpose_A, transpose_B, m, n, k, lda, ldb, beta, arg2_shape](CPURuntimeContext* ctx) { auto mm_functor =
cblas::cblas_sgemm(cblas::Layout::RowMajor, [&, transpose_A, transpose_B, m, n, k, lda, ldb, beta, arg2_shape](
transpose_A ? cblas::Transpose::Transpose : cblas::Transpose::None, CPURuntimeContext* ctx) {
transpose_B ? cblas::Transpose::Transpose : cblas::Transpose::None, cblas::cblas_sgemm(
m, cblas::Layout::RowMajor,
n, transpose_A ? cblas::Transpose::Transpose : cblas::Transpose::None,
k, transpose_B ? cblas::Transpose::Transpose : cblas::Transpose::None,
1.0f, m,
static_cast<float*>(arg0_tensor), n,
max(1UL, lda), k,
static_cast<float*>(arg1_tensor), 1.0f,
max(1UL, ldb), static_cast<float*>(arg0_tensor),
beta, max(1UL, lda),
static_cast<float*>(out0_tensor), static_cast<float*>(arg1_tensor),
max(1UL, arg2_shape[1]) max(1UL, ldb),
); beta,
}; static_cast<float*>(out0_tensor),
max(1UL, arg2_shape[1]));
};
function<void(CPURuntimeContext*)> bias_functor = [](CPURuntimeContext* ctx) {}; function<void(CPURuntimeContext*)> bias_functor = [](CPURuntimeContext* ctx) {};
...@@ -338,43 +360,41 @@ namespace ngraph ...@@ -338,43 +360,41 @@ namespace ngraph
{ {
vector<float> ones_row(arg2_shape[0], 1.0f); vector<float> ones_row(arg2_shape[0], 1.0f);
bias_functor = [&, ones_row, arg2_shape](CPURuntimeContext* ctx) { bias_functor = [&, ones_row, arg2_shape](CPURuntimeContext* ctx) {
cblas::cblas_sgemm(cblas::Layout::RowMajor, cblas::cblas_sgemm(cblas::Layout::RowMajor,
cblas::Transpose::None, cblas::Transpose::None,
cblas::Transpose::None, cblas::Transpose::None,
arg2_shape[0], arg2_shape[0],
arg2_shape[1], arg2_shape[1],
1, 1,
1.0f, 1.0f,
ones_row.data(), ones_row.data(),
1UL, 1UL,
static_cast<float*>(arg2_tensor), static_cast<float*>(arg2_tensor),
max(1UL, arg2_shape[1]), max(1UL, arg2_shape[1]),
1.0f, 1.0f,
static_cast<float*>(out0_tensor), static_cast<float*>(out0_tensor),
max(1UL, arg2_shape[1]) max(1UL, arg2_shape[1]));
); };
};
} }
else else
{ {
vector<float> ones_col(arg2_shape[1], 1.0f); vector<float> ones_col(arg2_shape[1], 1.0f);
bias_functor = [&, ones_col, arg2_shape](CPURuntimeContext* ctx) { bias_functor = [&, ones_col, arg2_shape](CPURuntimeContext* ctx) {
cblas::cblas_sgemm(cblas::Layout::RowMajor, cblas::cblas_sgemm(cblas::Layout::RowMajor,
cblas::Transpose::None, cblas::Transpose::None,
cblas::Transpose::None, cblas::Transpose::None,
arg2_shape[0], arg2_shape[0],
arg2_shape[1], arg2_shape[1],
1, 1,
1.0f, 1.0f,
static_cast<float*>(arg2_tensor), static_cast<float*>(arg2_tensor),
1UL, 1UL,
ones_col.data(), ones_col.data(),
max(1UL, arg2_shape[1]), max(1UL, arg2_shape[1]),
1.0f, 1.0f,
static_cast<float*>(out0_tensor), static_cast<float*>(out0_tensor),
max(1UL, arg2_shape[1]) max(1UL, arg2_shape[1]));
); };
};
} }
} }
else else
...@@ -387,29 +407,28 @@ namespace ngraph ...@@ -387,29 +407,28 @@ namespace ngraph
vector<float> ones_scalar(arg2_shape[0], 1.0f); vector<float> ones_scalar(arg2_shape[0], 1.0f);
bias_functor = [&, ones_scalar, arg2_shape](CPURuntimeContext* ctx) { bias_functor = [&, ones_scalar, arg2_shape](CPURuntimeContext* ctx) {
vector<float> bias(arg2_shape[1], *static_cast<float*>(arg2_tensor)); vector<float> bias(arg2_shape[1], *static_cast<float*>(arg2_tensor));
cblas::cblas_sgemm(cblas::Layout::RowMajor, cblas::cblas_sgemm(cblas::Layout::RowMajor,
cblas::Transpose::None, cblas::Transpose::None,
cblas::Transpose::None, cblas::Transpose::None,
arg2_shape[0], arg2_shape[0],
arg2_shape[1], arg2_shape[1],
1, 1,
1.0f, 1.0f,
ones_scalar.data(), ones_scalar.data(),
1UL, 1UL,
bias.data(), bias.data(),
max(1UL, arg2_shape[1]), max(1UL, arg2_shape[1]),
1.0f, 1.0f,
static_cast<float*>(out0_tensor), static_cast<float*>(out0_tensor),
max(1UL, arg2_shape[1]) max(1UL, arg2_shape[1]));
); };
};
} }
} }
auto functor = [&, mm_functor, bias_functor](CPURuntimeContext* ctx) { auto functor = [&, mm_functor, bias_functor](CPURuntimeContext* ctx) {
mm_functor(ctx); mm_functor(ctx);
bias_functor(ctx); bias_functor(ctx);
}; };
functors.emplace_back(functor); functors.emplace_back(functor);
} }
...@@ -446,6 +465,7 @@ namespace ngraph ...@@ -446,6 +465,7 @@ namespace ngraph
{TI(ngraph::op::Multiply), &runtime::cpu::Builder::build<ngraph::op::Multiply>}, {TI(ngraph::op::Multiply), &runtime::cpu::Builder::build<ngraph::op::Multiply>},
{TI(ngraph::op::Parameter), &runtime::cpu::Builder::nop}, {TI(ngraph::op::Parameter), &runtime::cpu::Builder::nop},
{TI(ngraph::op::Abs), &runtime::cpu::Builder::build<ngraph::op::Abs>}, {TI(ngraph::op::Abs), &runtime::cpu::Builder::build<ngraph::op::Abs>},
{TI(ngraph::op::Ceiling), &runtime::cpu::Builder::build<ngraph::op::Ceiling>},
{TI(ngraph::op::Relu), &runtime::cpu::Builder::build<ngraph::op::Relu>}, {TI(ngraph::op::Relu), &runtime::cpu::Builder::build<ngraph::op::Relu>},
{TI(ngraph::op::Result), &runtime::cpu::Builder::build<ngraph::op::Result>}, {TI(ngraph::op::Result), &runtime::cpu::Builder::build<ngraph::op::Result>},
{TI(ngraph::op::MatmulBias), &runtime::cpu::Builder::build<ngraph::op::MatmulBias>}, {TI(ngraph::op::MatmulBias), &runtime::cpu::Builder::build<ngraph::op::MatmulBias>},
......
/*******************************************************************************
* Copyright 2018 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
#define EIGEN_USE_THREADS
#include <unsupported/Eigen/CXX11/Tensor>
#include "ngraph/runtime/cpu/kernel/eigen_thread_pool.hpp"
namespace ngraph
{
namespace runtime
{
namespace cpu
{
namespace kernel
{
template <typename ElementType>
void ceil(void* input0, void* output, size_t count)
{
Eigen::array<Eigen::Index, 1> out_dims, in_dims;
out_dims[0] = in_dims[0] = count;
Eigen::TensorMap<Eigen::Tensor<ElementType, 1, Eigen::RowMajor>> out(
static_cast<ElementType*>(output), out_dims);
Eigen::TensorMap<Eigen::Tensor<ElementType, 1, Eigen::RowMajor>> in0(
static_cast<ElementType*>(input0), in_dims);
out.device(eigen::global_thread_pool_device) = in0.ceil();
}
}
}
}
}
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