Unverified Commit 94d80ffa authored by Tristan Webb's avatar Tristan Webb Committed by GitHub

Drwebb/gpu backend dot op (#413)

* Drwebb/gpu backend dot op (#387)

* GPU Dot prod emitter switch statement

* cuBLAS dot kernel call

* Flush out arg substitution into gpu dot kernel call

* Drwebb/gpu backend dot op (#392)

* Take in CodeWriter into gpu op emitters

* Introduce GPU function gen based on pass functions

* Additional gpu emitter stubs

* link cublas in to unit test and ngraph

* Use static code gen methods for GPU, add new GPU op stubs

* use pass manager to declare functions / cublas Updates

* Prune down gpu_external_function wip

* Switch back to GPU tensor views in GPU backend

* Pass in cublas handle to GPU external function

* cuMalloc memory in gpu tensor view

* Use cuda runtime malloc and free for tensor view managment c

* change GPU tensor view init, and use GPU tensor view for GPU call frame

* include headers as system dirs

* GPU tensor printing utility function

* cublasSetPointer to device mode / Fix copyright notification lowercasing

* Passing GPU dot product test using cuBLAS

Clean up

* Changes from review
parent 2b0a5489
......@@ -187,8 +187,7 @@ endif()
# GPU backend current requires CPU because they share compiler.cpp,
# and compiler.cpp requires MKLDNN
if(NGRAPH_GPU_ENABLE)
include_directories(${CUDA_INCLUDE_DIRS} ${CUDNN_INCLUDE_DIR})
link_directories(${CUDA_LIBRARIES} ${CUDNN_LIBRARIES})
include_directories(SYSTEM ${CUDA_INCLUDE_DIRS} ${CUDNN_INCLUDE_DIR})
# Add sources for the GPU backend
# and all its dependencies
......@@ -201,6 +200,7 @@ endif()
runtime/gpu/gpu_manager.cpp
runtime/gpu/gpu_tensor_view.cpp
runtime/gpu/gpu_tensor_view_wrapper.cpp
runtime/gpu/gpu_util.cpp
)
set_property(SOURCE codegen/compiler.cpp APPEND_STRING PROPERTY COMPILE_DEFINITIONS
"CUDA_HEADER_PATHS=\"${CUDA_INCLUDE_DIRS}\";")
......@@ -272,8 +272,9 @@ if(NGRAPH_CPU_ENABLE)
target_link_libraries(ngraph PRIVATE ${TBB_IMPORTED_TARGETS})
endif()
# Nvidia
if(NGRAPH_GPU_ENABLE AND CUDA_LIBRARIES)
target_link_libraries(ngraph PRIVATE cuda)
target_link_libraries(ngraph PRIVATE ${CUDA_LIBRARIES} ${CUDA_CUBLAS_LIBRARIES})
endif()
# Argon
......
......@@ -13,7 +13,6 @@
// ----------------------------------------------------------------------------
#include "ngraph/runtime/gpu/gpu_backend.hpp"
#include "ngraph/runtime/cpu/cpu_tensor_view.hpp"
#include "ngraph/runtime/external_function.hpp"
#include "ngraph/runtime/gpu/gpu_tensor_view.hpp"
......@@ -27,9 +26,9 @@ std::shared_ptr<ngraph::runtime::CallFrame> runtime::gpu::GPU_Backend::make_call
}
std::shared_ptr<ngraph::runtime::TensorView>
runtime::gpu::GPU_Backend::make_device_tensor(const ngraph::element::Type& element_type,
const Shape& shape)
runtime::gpu::GPU_Backend::make_primary_tensor_view(const ngraph::element::Type& element_type,
const Shape& shape)
{
auto rc = make_shared<runtime::HostTensorView>(element_type, shape);
auto rc = make_shared<runtime::gpu::GPU_TensorView>(element_type, shape);
return dynamic_pointer_cast<runtime::TensorView>(rc);
}
// ----------------------------------------------------------------------------
// 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
// 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
// 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
// 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 <cstdlib>
#include <fstream>
#include <stdio.h>
#include "ngraph/runtime/cpu/cpu_tensor_view.hpp"
#include <cuda_runtime.h>
#include "cublas_v2.h"
#include "ngraph/runtime/gpu/gpu_call_frame.hpp"
#include "ngraph/runtime/gpu/gpu_external_function.hpp"
......@@ -29,6 +31,13 @@ runtime::gpu::GPU_CallFrame::GPU_CallFrame(std::shared_ptr<GPU_ExternalFunction>
: m_external_function(external_function)
, m_compiled_function(compiled_function)
{
cublasStatus_t stat = cublasCreate(&m_cublas_handle);
if (stat != cudaSuccess)
{
throw runtime_error("cuBLAS create failed");
}
// Pass scalars as reference on the device
cublasSetPointerMode(m_cublas_handle, CUBLAS_POINTER_MODE_DEVICE);
}
void runtime::gpu::GPU_CallFrame::tensor_call(
......@@ -36,24 +45,23 @@ void runtime::gpu::GPU_CallFrame::tensor_call(
const std::vector<std::shared_ptr<ngraph::runtime::TensorView>>& output_tvs)
{
// Host tensors
vector<void*> inputs;
vector<void*> outputs;
vector<void**> inputs;
vector<void**> outputs;
for (size_t i = 0; i < input_tvs.size(); i++)
{
shared_ptr<runtime::HostTensorView> tv =
static_pointer_cast<runtime::HostTensorView>(input_tvs[i]);
inputs.push_back(tv->get_data_ptr());
shared_ptr<runtime::gpu::GPU_TensorView> tv =
static_pointer_cast<runtime::gpu::GPU_TensorView>(input_tvs[i]);
inputs.push_back(tv->m_allocated_buffer_pool);
}
for (size_t i = 0; i < output_tvs.size(); i++)
{
shared_ptr<runtime::HostTensorView> tv =
static_pointer_cast<runtime::HostTensorView>(output_tvs[i]);
outputs.push_back(tv->get_data_ptr());
shared_ptr<runtime::gpu::GPU_TensorView> tv =
static_pointer_cast<runtime::gpu::GPU_TensorView>(output_tvs[i]);
outputs.push_back(tv->m_allocated_buffer_pool);
}
// Invoke compiled computation
m_compiled_function(inputs.data(), outputs.data());
m_compiled_function(inputs.data(), outputs.data(), m_cublas_handle);
}
void runtime::gpu::GPU_CallFrame::call(
......
......@@ -18,6 +18,8 @@
#include <memory>
#include <vector>
#include "cublas_v2.h"
#include "ngraph/function.hpp"
#include "ngraph/runtime/call_frame.hpp"
#include "ngraph/runtime/tensor_view.hpp"
......@@ -33,7 +35,9 @@ namespace ngraph
class GPU_CallFrame;
class GPU_ExternalFunction;
using EntryPoint_t = void(void** inputs, void** outputs);
using EntryPoint_t = void(void*** inputs,
void*** outputs,
cublasHandle_t& cublas_handle);
using EntryPoint = std::function<EntryPoint_t>;
......@@ -44,6 +48,8 @@ namespace ngraph
GPU_CallFrame(std::shared_ptr<GPU_ExternalFunction> external_function,
EntryPoint compiled_function);
~GPU_CallFrame() override = default;
/// @brief Invoke the function with values matching the signature of the function.
///
/// Tuples will be expanded into their tensor views to build the call frame.
......@@ -59,6 +65,7 @@ namespace ngraph
protected:
std::shared_ptr<GPU_ExternalFunction> m_external_function;
EntryPoint m_compiled_function;
cublasHandle_t m_cublas_handle;
};
}
}
......
......@@ -43,80 +43,145 @@
using namespace std;
using namespace ngraph;
void runtime::gpu::GPU_Emitter::EmitNop(const ngraph::Node* n,
void runtime::gpu::GPU_Emitter::EmitNop(codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitAdd(const ngraph::Node* n,
void runtime::gpu::GPU_Emitter::EmitAbs(codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitDot(const ngraph::Node* n,
void runtime::gpu::GPU_Emitter::EmitAdd(codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitDivide(const ngraph::Node* n,
void runtime::gpu::GPU_Emitter::EmitConcat(codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitEqual(const ngraph::Node* n,
void runtime::gpu::GPU_Emitter::EmitDot(codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
const Shape& arg0_shape = args[0].get_shape();
const Shape& arg1_shape = args[1].get_shape();
if (arg0_shape.empty() || arg1_shape.empty())
{
auto& first = (arg0_shape.empty() ? args[0] : args[1]);
auto& second = (arg0_shape.empty() ? args[1] : args[0]);
}
// clang-format off
else if ((arg0_shape.size() == 1) && (arg1_shape.size() == 1))
{
// TODO Assert arg0_shape[0] == arg1_shape[0]?
writer << "{ // " << n->get_name() << "\n";
writer.indent++;
writer << "cublasSdot("
<< "cublas_handle,"
<< arg0_shape[0] << ","
<< args[0].get_name() << ","
// Todo handle striding?
<< "1,"
<< args[1].get_name() << ","
<< "1,"
<< out[0].get_name() << ");\n";
writer.indent--;
writer << "}\n";
}
// clang-format on
else if ((arg0_shape.size() == 2) && (arg1_shape.size() == 1))
{
}
else if ((arg0_shape.size() == 2) && (arg1_shape.size() == 2))
{
// GEMM Call
}
else
{
// General ND Call?
}
}
void runtime::gpu::GPU_Emitter::EmitDivide(codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitEqual(codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitGreater(const ngraph::Node* n,
void runtime::gpu::GPU_Emitter::EmitGreater(codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitGreaterEq(
codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitLess(const ngraph::Node* n,
void runtime::gpu::GPU_Emitter::EmitLess(codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitLessEq(const ngraph::Node* n,
void runtime::gpu::GPU_Emitter::EmitLessEq(codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitLog(const ngraph::Node* n,
void runtime::gpu::GPU_Emitter::EmitLog(codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitMaximum(const ngraph::Node* n,
void runtime::gpu::GPU_Emitter::EmitMaximum(codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitMinimum(const ngraph::Node* n,
void runtime::gpu::GPU_Emitter::EmitMinimum(codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitNegative(
codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
......@@ -124,18 +189,21 @@ void runtime::gpu::GPU_Emitter::EmitNegative(
}
void runtime::gpu::GPU_Emitter::EmitNotEqual(
codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitSelect(const ngraph::Node* n,
void runtime::gpu::GPU_Emitter::EmitSelect(codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitSubtract(
codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
......@@ -143,245 +211,231 @@ void runtime::gpu::GPU_Emitter::EmitSubtract(
}
void runtime::gpu::GPU_Emitter::EmitBroadcast(
codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitConvert(const ngraph::Node* n,
void runtime::gpu::GPU_Emitter::EmitConvert(codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitConstant(
codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitReshape(const ngraph::Node* n,
void runtime::gpu::GPU_Emitter::EmitReshape(codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitFunctionCall(
codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitReduce(const ngraph::Node* n,
void runtime::gpu::GPU_Emitter::EmitReduce(codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitSign(const ngraph::Node* n,
void runtime::gpu::GPU_Emitter::EmitSign(codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitSlice(const ngraph::Node* n,
void runtime::gpu::GPU_Emitter::EmitSlice(codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitSum(const ngraph::Node* n,
void runtime::gpu::GPU_Emitter::EmitSum(codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitExp(const ngraph::Node* n,
void runtime::gpu::GPU_Emitter::EmitMultiply(
codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitExp(codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitSin(const ngraph::Node* n,
void runtime::gpu::GPU_Emitter::EmitSin(codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitSinh(const ngraph::Node* n,
void runtime::gpu::GPU_Emitter::EmitSinh(codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitCos(const ngraph::Node* n,
void runtime::gpu::GPU_Emitter::EmitCos(codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitCosh(const ngraph::Node* n,
void runtime::gpu::GPU_Emitter::EmitCosh(codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitTan(const ngraph::Node* n,
void runtime::gpu::GPU_Emitter::EmitTan(codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitTanh(const ngraph::Node* n,
void runtime::gpu::GPU_Emitter::EmitTanh(codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitAsin(const ngraph::Node* n,
void runtime::gpu::GPU_Emitter::EmitAsin(codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitAcos(const ngraph::Node* n,
void runtime::gpu::GPU_Emitter::EmitAcos(codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitAtan(const ngraph::Node* n,
void runtime::gpu::GPU_Emitter::EmitAtan(codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitPower(const ngraph::Node* n,
void runtime::gpu::GPU_Emitter::EmitPower(codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitReplaceSlice(
codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitOneHot(const ngraph::Node* n,
void runtime::gpu::GPU_Emitter::EmitOneHot(codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitCeiling(const ngraph::Node* n,
void runtime::gpu::GPU_Emitter::EmitCeiling(codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitFloor(const ngraph::Node* n,
void runtime::gpu::GPU_Emitter::EmitFloor(codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitSqrt(const ngraph::Node* n,
void runtime::gpu::GPU_Emitter::EmitSqrt(codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitConvolution(
codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitNot(const ngraph::Node* n,
void runtime::gpu::GPU_Emitter::EmitNot(codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitMaxPool(const ngraph::Node* n,
void runtime::gpu::GPU_Emitter::EmitMaxPool(codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitReverse(const ngraph::Node* n,
void runtime::gpu::GPU_Emitter::EmitReverse(codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
//------------------------------------------------------------------------------------------------
// Utility methods
//------------------------------------------------------------------------------------------------
void runtime::gpu::GPU_Emitter::generate_call(
void runtime::gpu::GPU_Emitter::EmitReduceWindow(
codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out,
shared_ptr<Function> function)
{
vector<string> input_names;
vector<string> output_names;
for (const runtime::gpu::GPU_TensorViewWrapper& input : args)
{
input_names.push_back(input.get_name());
}
for (const runtime::gpu::GPU_TensorViewWrapper& output : out)
{
output_names.push_back(output.get_name());
}
m_out << "void* args[] =\n{";
m_out.indent++;
m_out << "\n" << join(input_names, ",\n");
m_out.indent--;
m_out << "\n};\n";
m_out << "void* out[] =\n{";
m_out.indent++;
m_out << "\n" << join(output_names, ",\n");
m_out.indent--;
m_out << "\n};\n";
m_out << "\n";
m_out << function->get_name() << "(args, out);\n";
}
static string format_name(const string& name)
{
string rc;
if (!name.empty())
{
rc = " " + name;
}
return rc;
}
void runtime::gpu::GPU_Emitter::EmitAbs(const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitConcat(const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
{
}
void runtime::gpu::GPU_Emitter::EmitMultiply(
void runtime::gpu::GPU_Emitter::EmitSelectAndScatter(
codegen::CodeWriter& writer,
const ngraph::Node* n,
const vector<runtime::gpu::GPU_TensorViewWrapper>& args,
const vector<runtime::gpu::GPU_TensorViewWrapper>& out)
......
......@@ -23,7 +23,8 @@
#include "ngraph/runtime/gpu/gpu_tensor_view_wrapper.hpp"
#define EMITTER_DECL(E) \
E(const ngraph::Node* n, \
E(codegen::CodeWriter& writer, \
const ngraph::Node* n, \
const std::vector<ngraph::runtime::gpu::GPU_TensorViewWrapper>& args, \
const std::vector<ngraph::runtime::gpu::GPU_TensorViewWrapper>& out)
......@@ -35,79 +36,60 @@ namespace ngraph
{
class GPU_Emitter
{
protected:
codegen::CodeWriter m_out;
bool m_use_ref_kernels;
public:
GPU_Emitter()
: m_out()
, m_use_ref_kernels(std::getenv("NGRAPH_GPU_USE_REF_KERNELS") != nullptr)
{
}
std::string get_code() { return m_out.get_code(); }
codegen::CodeWriter& get_code_writer() { return m_out; }
void EMITTER_DECL(EmitNop);
void EMITTER_DECL(EmitAdd);
void EMITTER_DECL(EmitDot);
void EMITTER_DECL(EmitMultiply);
void EMITTER_DECL(EmitGetOutputElement);
void EMITTER_DECL(EmitXLAGetTupleElement);
void EMITTER_DECL(EmitTuple);
void EMITTER_DECL(EmitAbs);
void EMITTER_DECL(EmitConcat);
void EMITTER_DECL(EmitDivide);
void EMITTER_DECL(EmitEqual);
void EMITTER_DECL(EmitGreater);
void EMITTER_DECL(EmitGreaterEq);
void EMITTER_DECL(EmitLess);
void EMITTER_DECL(EmitLessEq);
void EMITTER_DECL(EmitLog);
void EMITTER_DECL(EmitMaximum);
void EMITTER_DECL(EmitMinimum);
void EMITTER_DECL(EmitNegative);
void EMITTER_DECL(EmitNotEqual);
void EMITTER_DECL(EmitSelect);
void EMITTER_DECL(EmitSubtract);
void EMITTER_DECL(EmitBroadcast);
void EMITTER_DECL(EmitConvert);
void EMITTER_DECL(EmitConstant);
void EMITTER_DECL(EmitReshape);
void EMITTER_DECL(EmitFunctionCall);
void EMITTER_DECL(EmitReduce);
void EMITTER_DECL(EmitSign);
void EMITTER_DECL(EmitSlice);
void EMITTER_DECL(EmitSum);
void EMITTER_DECL(EmitExp);
void EMITTER_DECL(EmitSin);
void EMITTER_DECL(EmitSinh);
void EMITTER_DECL(EmitCos);
void EMITTER_DECL(EmitCosh);
void EMITTER_DECL(EmitTan);
void EMITTER_DECL(EmitTanh);
void EMITTER_DECL(EmitAsin);
void EMITTER_DECL(EmitAcos);
void EMITTER_DECL(EmitAtan);
void EMITTER_DECL(EmitPower);
void EMITTER_DECL(EmitReplaceSlice);
void EMITTER_DECL(EmitOneHot);
void EMITTER_DECL(EmitFloor);
void EMITTER_DECL(EmitCeiling);
void EMITTER_DECL(EmitSqrt);
void EMITTER_DECL(EmitConvolution);
void EMITTER_DECL(EmitNot);
void EMITTER_DECL(EmitMaxPool);
void EMITTER_DECL(EmitReverse);
private:
void generate_call(const std::vector<GPU_TensorViewWrapper>& args,
const std::vector<GPU_TensorViewWrapper>& out,
std::shared_ptr<Function> function);
std::string emit_vector(const GPU_TensorViewWrapper&, const std::string& name = "");
std::string emit_array1d(const GPU_TensorViewWrapper&,
const std::string& name = "");
std::string emit_matrix(const GPU_TensorViewWrapper&, const std::string& name = "");
static void EMITTER_DECL(EmitNop);
static void EMITTER_DECL(EmitAdd);
static void EMITTER_DECL(EmitDot);
static void EMITTER_DECL(EmitMultiply);
static void EMITTER_DECL(EmitGetOutputElement);
static void EMITTER_DECL(EmitXLAGetTupleElement);
static void EMITTER_DECL(EmitTuple);
static void EMITTER_DECL(EmitAbs);
static void EMITTER_DECL(EmitConcat);
static void EMITTER_DECL(EmitDivide);
static void EMITTER_DECL(EmitEqual);
static void EMITTER_DECL(EmitGreater);
static void EMITTER_DECL(EmitGreaterEq);
static void EMITTER_DECL(EmitLess);
static void EMITTER_DECL(EmitLessEq);
static void EMITTER_DECL(EmitLog);
static void EMITTER_DECL(EmitMaximum);
static void EMITTER_DECL(EmitMinimum);
static void EMITTER_DECL(EmitNegative);
static void EMITTER_DECL(EmitNotEqual);
static void EMITTER_DECL(EmitSelect);
static void EMITTER_DECL(EmitSubtract);
static void EMITTER_DECL(EmitBroadcast);
static void EMITTER_DECL(EmitConvert);
static void EMITTER_DECL(EmitConstant);
static void EMITTER_DECL(EmitReshape);
static void EMITTER_DECL(EmitFunctionCall);
static void EMITTER_DECL(EmitReduce);
static void EMITTER_DECL(EmitSign);
static void EMITTER_DECL(EmitSlice);
static void EMITTER_DECL(EmitSum);
static void EMITTER_DECL(EmitExp);
static void EMITTER_DECL(EmitSin);
static void EMITTER_DECL(EmitSinh);
static void EMITTER_DECL(EmitCos);
static void EMITTER_DECL(EmitCosh);
static void EMITTER_DECL(EmitTan);
static void EMITTER_DECL(EmitTanh);
static void EMITTER_DECL(EmitAsin);
static void EMITTER_DECL(EmitAcos);
static void EMITTER_DECL(EmitAtan);
static void EMITTER_DECL(EmitPower);
static void EMITTER_DECL(EmitReplaceSlice);
static void EMITTER_DECL(EmitOneHot);
static void EMITTER_DECL(EmitFloor);
static void EMITTER_DECL(EmitCeiling);
static void EMITTER_DECL(EmitSqrt);
static void EMITTER_DECL(EmitConvolution);
static void EMITTER_DECL(EmitNot);
static void EMITTER_DECL(EmitMaxPool);
static void EMITTER_DECL(EmitReverse);
static void EMITTER_DECL(EmitReduceWindow);
static void EMITTER_DECL(EmitSelectAndScatter);
};
}
}
......
......@@ -66,10 +66,12 @@
#include "ngraph/ops/one_hot.hpp"
#include "ngraph/ops/power.hpp"
#include "ngraph/ops/reduce.hpp"
#include "ngraph/ops/reduce_window.hpp"
#include "ngraph/ops/replace_slice.hpp"
#include "ngraph/ops/reshape.hpp"
#include "ngraph/ops/reverse.hpp"
#include "ngraph/ops/select.hpp"
#include "ngraph/ops/select_and_scatter.hpp"
#include "ngraph/ops/sign.hpp"
#include "ngraph/ops/sin.hpp"
#include "ngraph/ops/sinh.hpp"
......@@ -186,6 +188,8 @@ static const runtime::gpu::OpMap dispatcher{
{TI(ngraph::op::Not), &runtime::gpu::GPU_Emitter::EmitNot},
{TI(ngraph::op::MaxPool), &runtime::gpu::GPU_Emitter::EmitMaxPool},
{TI(ngraph::op::Reverse), &runtime::gpu::GPU_Emitter::EmitReverse},
{TI(ngraph::op::ReduceWindow), &runtime::gpu::GPU_Emitter::EmitReduceWindow},
{TI(ngraph::op::SelectAndScatter), &runtime::gpu::GPU_Emitter::EmitSelectAndScatter},
};
runtime::gpu::GPU_ExternalFunction::GPU_ExternalFunction(
......@@ -216,11 +220,12 @@ void runtime::gpu::GPU_ExternalFunction::compile()
pass_manager.register_pass<pass::DumpSorted>(dump_filename);
pass_manager.run_passes(m_function);
GPU_Emitter emitter;
codegen::CodeWriter& writer = emitter.get_code_writer();
codegen::CodeWriter writer;
writer +=
R"(// Generated by the NGraph GPU backend
#define IDX2F(i,j,ld) ((((j)-1)*(ld))+((i)-1))
#include <cassert>
#include <cmath>
#include <cstdlib>
......@@ -234,10 +239,10 @@ void runtime::gpu::GPU_ExternalFunction::compile()
#include <typeinfo>
#include <unordered_map>
#include <cuda_runtime.h>
#include "cublas_v2.h"
#include "cuda.h"
#include "ngraph/codegen/code_writer.hpp"
#include "ngraph/codegen/compiler.hpp"
#include "ngraph/codegen/execution_engine.hpp"
#include "ngraph/descriptor/input.hpp"
#include "ngraph/descriptor/layout/dense_tensor_view_layout.hpp"
#include "ngraph/descriptor/output.hpp"
......@@ -299,21 +304,15 @@ void runtime::gpu::GPU_ExternalFunction::compile()
#include "ngraph/pass/manager.hpp"
#include "ngraph/pass/memory_layout.hpp"
#include "ngraph/runtime/aligned_buffer.hpp"
#include "ngraph/runtime/gpu/gpu_util.hpp"
#include "ngraph/util.hpp"
)";
string pch_header_source = writer.get_code();
writer += R"(
using namespace ngraph::runtime;
using namespace ngraph;
using namespace std;
void check_cuda_errors(CUresult err) {
assert(err == CUDA_SUCCESS);
// assert(err == err);
}
)";
// // The "dso_handle" symbol is required by __cxa_atexit()
......@@ -347,591 +346,277 @@ void runtime::gpu::GPU_ExternalFunction::compile()
writer << "// Declare all functions\n";
for (shared_ptr<Function> f : pass_manager.get_state().get_functions())
{
writer << "extern \"C\" void " << f->get_name() << "(void** inputs, void** outputs);\n";
writer << "extern \"C\" void " << f->get_name()
<< "(void** inputs, void** outputs, cublasHandle_t& cublas_handle);\n";
}
writer << "\n";
writer << "extern \"C\" void " << pass_manager.get_state().get_functions()[0]->get_name()
<< "(void** inputs, void** outputs){\n";
writer += R"(
CUdevice device;
CUmodule cuda_module;
CUcontext context;
CUfunction add_function;
CUfunction mult_function;
CUlinkState linker;
int dev_count;
check_cuda_errors(cuInit(0));
check_cuda_errors(cuDeviceGetCount(&dev_count));
check_cuda_errors(cuDeviceGet(&device, 0));
// char name[128];
// check_cuda_errors(cuDeviceGetName(name, 128, device));
// std::cout << "Using CUDA Device [0]: " << name << "\n";
// int dev_major, dev_minor;
// check_cuda_errors(cuDeviceComputeCapability(&dev_major, &dev_minor, device));
// std::cout << "Device Compute Capability: "
// << dev_major << "." << dev_minor << "\n";
// if (dev_major < 2) {
// std::cerr << "ERROR: Device 0 is not SM 2.0 or greater\n";
// }
const auto kernels = R"#(
.version 5.0
.target sm_60
.address_size 64
// .globl _Z7ew_multPfS_S_ // -- Begin function _Z7ew_multPfS_S_
.global .align 1 .b8 threadIdx[1];
// @_Z7ew_multPfS_S_
.visible .entry _Z7ew_multPfS_S_(
.param .u64 _Z7ew_multPfS_S__param_0,
.param .u64 _Z7ew_multPfS_S__param_1,
.param .u64 _Z7ew_multPfS_S__param_2
)
{
.local .align 8 .b8 __local_depot0[24];
.reg .b64 %SP;
.reg .b64 %SPL;
.reg .f32 %f<4>;
.reg .b32 %r<2>;
.reg .b64 %rd<17>;
// BB#0:
mov.u64 %SPL, __local_depot0;
cvta.local.u64 %SP, %SPL;
ld.param.u64 %rd3, [_Z7ew_multPfS_S__param_2];
ld.param.u64 %rd2, [_Z7ew_multPfS_S__param_1];
ld.param.u64 %rd1, [_Z7ew_multPfS_S__param_0];
cvta.to.global.u64 %rd4, %rd3;
cvta.global.u64 %rd5, %rd4;
cvta.to.global.u64 %rd6, %rd2;
cvta.global.u64 %rd7, %rd6;
cvta.to.global.u64 %rd8, %rd1;
cvta.global.u64 %rd9, %rd8;
st.u64 [%SP+0], %rd9;
st.u64 [%SP+8], %rd7;
st.u64 [%SP+16], %rd5;
ld.u64 %rd10, [%SP+0];
mov.u32 %r1, %tid.x;
mul.wide.u32 %rd11, %r1, 4;
add.s64 %rd12, %rd10, %rd11;
ld.f32 %f1, [%rd12];
ld.u64 %rd13, [%SP+8];
add.s64 %rd14, %rd13, %rd11;
ld.f32 %f2, [%rd14];
mul.rn.f32 %f3, %f1, %f2;
ld.u64 %rd15, [%SP+16];
add.s64 %rd16, %rd15, %rd11;
st.f32 [%rd16], %f3;
ret;
}
// -- End function
// .globl _Z6ew_addPfS_S_ // -- Begin function _Z6ew_addPfS_S_
.visible .entry _Z6ew_addPfS_S_(
.param .u64 _Z6ew_addPfS_S__param_0,
.param .u64 _Z6ew_addPfS_S__param_1,
.param .u64 _Z6ew_addPfS_S__param_2
) // @_Z6ew_addPfS_S_
{
.local .align 8 .b8 __local_depot1[24];
.reg .b64 %SP;
.reg .b64 %SPL;
.reg .f32 %f<4>;
.reg .b32 %r<2>;
.reg .b64 %rd<17>;
// BB#0:
mov.u64 %SPL, __local_depot1;
cvta.local.u64 %SP, %SPL;
ld.param.u64 %rd3, [_Z6ew_addPfS_S__param_2];
ld.param.u64 %rd2, [_Z6ew_addPfS_S__param_1];
ld.param.u64 %rd1, [_Z6ew_addPfS_S__param_0];
cvta.to.global.u64 %rd4, %rd3;
cvta.global.u64 %rd5, %rd4;
cvta.to.global.u64 %rd6, %rd2;
cvta.global.u64 %rd7, %rd6;
cvta.to.global.u64 %rd8, %rd1;
cvta.global.u64 %rd9, %rd8;
st.u64 [%SP+0], %rd9;
st.u64 [%SP+8], %rd7;
st.u64 [%SP+16], %rd5;
ld.u64 %rd10, [%SP+0];
mov.u32 %r1, %tid.x;
mul.wide.u32 %rd11, %r1, 4;
add.s64 %rd12, %rd10, %rd11;
ld.f32 %f1, [%rd12];
ld.u64 %rd13, [%SP+8];
add.s64 %rd14, %rd13, %rd11;
ld.f32 %f2, [%rd14];
add.rn.f32 %f3, %f1, %f2;
ld.u64 %rd15, [%SP+16];
add.s64 %rd16, %rd15, %rd11;
st.f32 [%rd16], %f3;
ret;
}
// -- End function
)#";
// Create driver context
check_cuda_errors(cuCtxCreate(&context, 0, device));
// Create module for object
check_cuda_errors(cuModuleLoadDataEx(&cuda_module, kernels, 0, 0, 0));
// Get kernel function
check_cuda_errors(cuModuleGetFunction(&add_function, cuda_module, "_Z6ew_addPfS_S_"));
check_cuda_errors(cuModuleGetFunction(&mult_function, cuda_module, "_Z7ew_multPfS_S_"));
// Device data
CUdeviceptr dev_bufferA;
CUdeviceptr dev_bufferB;
CUdeviceptr dev_bufferC;
check_cuda_errors(cuMemAlloc(&dev_bufferA, sizeof(float) * 4));
check_cuda_errors(cuMemAlloc(&dev_bufferB, sizeof(float) * 4));
check_cuda_errors(cuMemAlloc(&dev_bufferC, sizeof(float) * 4));
float* host_A = new float[4];
float* host_B = new float[4];
float* host_C = new float[4];
// Populate input
memcpy(host_A, (float*)(inputs[0]), sizeof(float) * 4);
memcpy(host_B, (float*)(inputs[1]), sizeof(float) * 4);
memcpy(host_C, (float*)(inputs[2]), sizeof(float) * 4);
check_cuda_errors(cuMemcpyHtoD(dev_bufferA, &host_A[0], sizeof(float) * 4));
check_cuda_errors(cuMemcpyHtoD(dev_bufferB, &host_B[0], sizeof(float) * 4));
// check_cuda_errors(cuMemcpyHtoD(dev_bufferC, &host_C[0], sizeof(float) * 4));
unsigned block_size_X = 4;
unsigned block_size_Y = 1;
unsigned block_size_Z = 1;
unsigned grid_size_X = 1;
unsigned grid_size_Y = 1;
unsigned grid_size_Z = 1;
// Kernel parameters
void* kernel_params[] = {&dev_bufferA, &dev_bufferB, &dev_bufferC};
// Add Kernel launch
check_cuda_errors(cuLaunchKernel(add_function,
grid_size_X,
grid_size_Y,
grid_size_Z,
block_size_X,
block_size_Y,
block_size_Z,
0,
NULL,
kernel_params,
NULL));
check_cuda_errors(cuMemcpyDtoH(&host_A[0], dev_bufferC, sizeof(float) * 4));
host_B = &host_C[0];
check_cuda_errors(cuMemcpyHtoD(dev_bufferA, &host_A[0], sizeof(float) * 4));
check_cuda_errors(cuMemcpyHtoD(dev_bufferB, &host_B[0], sizeof(float) * 4));
// Mult Kernel launch
check_cuda_errors(cuLaunchKernel(mult_function,
grid_size_X,
grid_size_Y,
grid_size_Z,
block_size_X,
block_size_Y,
block_size_Z,
0,
NULL,
kernel_params,
NULL));
// Write final output
check_cuda_errors(cuMemcpyDtoH(&((float*)(outputs[0]))[0], dev_bufferC, sizeof(float) * 4));
// Clean up after ourselves
// // Clean-up must do this in tensor view!!!
check_cuda_errors(cuMemFree(dev_bufferA));
check_cuda_errors(cuMemFree(dev_bufferB));
check_cuda_errors(cuMemFree(dev_bufferC));
check_cuda_errors(cuModuleUnload(cuda_module));
check_cuda_errors(cuCtxDestroy(context));})";
if (m_emit_timing)
unordered_map<Node*, string> match_functions;
for (shared_ptr<Function> current_function : pass_manager.get_state().get_functions())
{
writer << "// Declare debug timers\n";
vector<string> names;
for (shared_ptr<Function> current_function : pass_manager.get_state().get_functions())
bool temporaries_used = false;
size_t worst_case_tmp_size = 0;
set<string> output_names;
for (shared_ptr<Node> op : current_function->get_results())
{
shared_ptr<descriptor::TensorView> tv = op->get_output_tensor_view();
output_names.insert(tv->get_tensor().get_name());
}
set<descriptor::TensorView*> constants;
for (shared_ptr<Node> node : current_function->get_ordered_ops())
{
if (dynamic_cast<op::Constant*>(node.get()))
{
shared_ptr<descriptor::TensorView> tv = node->get_outputs()[0].get_tensor_view();
constants.insert(tv.get());
}
}
writer << "extern \"C\" void " << current_function->get_name();
writer << "(void** inputs, void** outputs, cublasHandle_t& cublas_handle)\n";
writer << "{\n";
writer.indent++;
if (m_emit_timing)
{
for (shared_ptr<Node> node : current_function->get_ordered_ops())
writer << "// Declare debug timers\n";
vector<string> names;
for (shared_ptr<Function> current_function : pass_manager.get_state().get_functions())
{
if (!node->is_parameter() && !node->is_constant())
for (shared_ptr<Node> node : current_function->get_ordered_ops())
{
names.push_back(node->get_name());
if (!node->is_parameter() && !node->is_constant())
{
names.push_back(node->get_name());
}
}
}
for (const string& s : names)
{
writer << "ngraph::stopwatch timer_" << s << ";\n";
}
writer << "extern \"C\" size_t get_debug_timer_count() { return " << names.size()
<< "; }\n";
writer << "extern \"C\" const char* get_debug_timer_name(size_t index)\n";
writer << "{\n";
writer.indent++;
writer << "const char* rc;\n";
writer << "switch(index)\n";
writer << "{\n";
for (size_t i = 0; i < names.size(); i++)
{
writer << "case " << i << ": rc = \"" << names[i] << "\"; break;\n";
}
writer << "default: rc = \"\";\n";
writer << "}\n";
writer << "return rc;\n";
writer.indent--;
writer << "}\n";
writer << "extern \"C\" const size_t get_debug_timer_microseconds(size_t index)\n";
writer << "{\n";
writer.indent++;
writer << "size_t rc;\n";
writer << "switch(index)\n";
writer << "{\n";
for (size_t i = 0; i < names.size(); i++)
{
writer << "case " << i << ": rc = timer_" << names[i]
<< ".get_total_microseconds(); break;\n";
}
writer << "default: rc = 0;\n";
writer << "}\n";
writer << "return rc;\n";
writer.indent--;
writer << "}\n";
writer << "extern \"C\" const size_t get_debug_timer_call_count(size_t index)\n";
writer << "{\n";
writer.indent++;
writer << "size_t rc;\n";
writer << "switch(index)\n";
writer << "{\n";
for (size_t i = 0; i < names.size(); i++)
{
writer << "case " << i << ": rc = timer_" << names[i]
<< ".get_call_count(); break;\n";
}
writer << "default: rc = 0;\n";
writer << "}\n";
writer << "return rc;\n";
writer.indent--;
writer << "}\n";
writer << "\n";
}
for (const string& s : names)
for (shared_ptr<Node> node : current_function->get_ordered_ops())
{
writer << "ngraph::stopwatch timer_" << s << ";\n";
if (node->liveness_new_list.size() > 0)
{
temporaries_used = true;
for (descriptor::Tensor* tensor : node->liveness_new_list)
{
worst_case_tmp_size += tensor->size();
}
}
}
writer << "extern \"C\" size_t get_debug_timer_count() { return " << names.size()
<< "; }\n";
writer << "extern \"C\" const char* get_debug_timer_name(size_t index)\n";
writer << "{\n";
writer.indent++;
writer << "const char* rc;\n";
writer << "switch(index)\n";
writer << "{\n";
for (size_t i = 0; i < names.size(); i++)
if (temporaries_used)
{
writer << "case " << i << ": rc = \"" << names[i] << "\"; break;\n";
// TODO use temporary variables
}
writer << "default: rc = \"\";\n";
writer << "}\n";
writer << "return rc;\n";
writer.indent--;
writer << "}\n";
writer << "extern \"C\" const size_t get_debug_timer_microseconds(size_t index)\n";
writer << "{\n";
writer.indent++;
writer << "size_t rc;\n";
writer << "switch(index)\n";
writer << "{\n";
for (size_t i = 0; i < names.size(); i++)
// Add inputs to the variable name map
size_t arg_index = 0;
for (shared_ptr<op::Parameter> param : current_function->get_parameters())
{
writer << "case " << i << ": rc = timer_" << names[i]
<< ".get_total_microseconds(); break;\n";
for (size_t i = 0; i < param->get_output_size(); ++i)
{
shared_ptr<descriptor::TensorView> tv = param->get_output_tensor_view(i);
const element::Type& et = tv->get_tensor_view_type()->get_element_type();
string type = et.c_type_string();
stringstream ss;
ss << "((" << type << "*)(inputs[" << arg_index << "]))";
m_variable_name_map[tv->get_tensor().get_name()] = ss.str();
arg_index++;
}
}
writer << "default: rc = 0;\n";
writer << "}\n";
writer << "return rc;\n";
writer.indent--;
writer << "}\n";
writer << "extern \"C\" const size_t get_debug_timer_call_count(size_t index)\n";
writer << "{\n";
writer.indent++;
writer << "size_t rc;\n";
writer << "switch(index)\n";
writer << "{\n";
for (size_t i = 0; i < names.size(); i++)
// create output alias map
size_t output_index = 0;
unordered_map<descriptor::TensorView*, vector<size_t>> output_alias_map;
vector<size_t> aliases;
for (size_t i = 0; i < current_function->get_output_size(); ++i)
{
shared_ptr<Node> op = current_function->get_output_op(i);
shared_ptr<descriptor::TensorView> otv = op->get_output_tensor_view();
vector<size_t>& al = output_alias_map[otv.get()];
al.push_back(output_index);
if (al.size() > 1)
{
aliases.push_back(output_index);
}
output_index++;
}
// Add outputs to the variable name map
output_index = 0;
for (size_t i = 0; i < current_function->get_output_size(); ++i)
{
shared_ptr<Node> op = current_function->get_output_op(i);
shared_ptr<descriptor::TensorView> tv = op->get_output_tensor_view();
const element::Type& et = tv->get_tensor_view_type()->get_element_type();
bool parameter_as_output = false;
for (shared_ptr<op::Parameter> param : current_function->get_parameters())
{
for (const descriptor::Output& pout : param->get_outputs())
{
shared_ptr<descriptor::TensorView> ptv = pout.get_tensor_view();
if (tv == ptv)
{
parameter_as_output = true;
writer << "memcpy(static_cast<" << et.c_type_string() << "*>(outputs["
<< output_index << "]), "
<< m_variable_name_map[ptv->get_tensor().get_name()] << ", "
<< ptv->get_tensor().size() << ");\n";
break;
}
}
}
if (!parameter_as_output && !contains(aliases, output_index))
{
if (contains(constants, tv.get()))
{
writer << "memcpy(outputs[" << output_index << "], "
<< tv->get_tensor().get_name() << ", " << tv->get_tensor().size()
<< ");\n";
}
else
{
string type = et.c_type_string();
stringstream ss;
ss << "((" << type << "*)(outputs[" << output_index << "]))";
m_variable_name_map[tv->get_tensor().get_name()] = ss.str();
}
}
output_index++;
}
for (shared_ptr<Node> node : current_function->get_ordered_ops())
{
writer << "case " << i << ": rc = timer_" << names[i] << ".get_call_count(); break;\n";
auto& n = *node; // Work around a compiler warning (*node inside typeid may have effects
// with shared pointers, which is fine here but clang doesn't like it.)
auto handler = dispatcher.find(type_index(typeid(n)));
if (handler == dispatcher.end())
{
throw ngraph_error("Unhandled op during code generation : " + node->description());
}
vector<GPU_TensorViewWrapper> in;
for (const descriptor::Input& input : node->get_inputs())
{
const descriptor::Output& output = input.get_output();
shared_ptr<descriptor::TensorView> tv = output.get_tensor_view();
in.push_back(
GPU_TensorViewWrapper(tv, m_variable_name_map[tv->get_tensor().get_name()]));
}
vector<GPU_TensorViewWrapper> out;
for (const descriptor::Output& output : node->get_outputs())
{
shared_ptr<descriptor::TensorView> tv = output.get_tensor_view();
out.push_back(
GPU_TensorViewWrapper(tv, m_variable_name_map[tv->get_tensor().get_name()]));
}
// Emit operation prologue
if (!node->is_parameter() && !node->is_constant())
{
if (m_emit_timing)
{
emit_debug_function_entry(writer, node.get(), in, out);
}
}
// Emit operation body
string func_name;
auto it = match_functions.find(node.get());
if (it != match_functions.end())
{
func_name = it->second;
}
if (func_name.empty())
{
handler->second(writer, node.get(), in, out);
}
else
{
vector<string> names;
for (const GPU_TensorViewWrapper& tv : in)
{
names.push_back(tv.get_name());
}
for (const GPU_TensorViewWrapper& tv : out)
{
names.push_back(tv.get_name());
}
writer << func_name << "(" << join(names) << ");\n";
}
// Emit operation epilogue
if (!node->is_parameter() && !node->is_constant())
{
handle_output_alias(writer, *node, output_alias_map);
if (m_emit_timing)
{
emit_debug_function_exit(writer, node.get(), in, out);
}
}
}
writer << "default: rc = 0;\n";
writer << "}\n";
writer << "return rc;\n";
writer.indent--;
writer << "}\n";
writer << "\n";
// End generated function
writer += "}\n\n";
}
// // This for loop creates a collection of functions that are called more than once
// // and emitting them as globally callable functions.
// // ops implement the is_functionally_identical method
// unordered_map<Node*, string> match_functions;
// for (shared_ptr<Function> current_function : pass_manager.get_state().get_functions())
// {
// const list<shared_ptr<Node>>& tmp = current_function->get_ordered_ops();
// vector<shared_ptr<Node>> op_list{tmp.begin(), tmp.end()};
// for (size_t i = 0; i < op_list.size() - 1; i++)
// {
// if (op_list[i]->is_constant() || op_list[i]->is_parameter())
// {
// continue;
// }
// if (contains_key(match_functions, op_list[i].get()))
// {
// continue;
// }
// string match_function_name;
// for (size_t j = i + 1; j < op_list.size(); j++)
// {
// if (op_list[i]->is_functionally_identical(*op_list[j]))
// {
// if (match_function_name.empty())
// {
// match_function_name = "func_" + op_list[i]->get_name();
// match_functions.insert({op_list[i].get(), match_function_name});
// }
// match_functions.insert({op_list[j].get(), match_function_name});
// }
// }
// if (!match_function_name.empty())
// {
// writer << "static void " << match_function_name << "(";
// writer.indent++;
// // Work around a compiler warning (*node inside typeid may have effects
// // with shared pointers, which is fine here but clang doesn't like it.)
// auto& n = *op_list[i];
// auto handler = dispatcher.find(type_index(typeid(n)));
// vector<GPU_TensorViewWrapper> in;
// size_t arg_index = 0;
// set<string> arg_names;
// for (const descriptor::Input& input : n.get_inputs())
// {
// const descriptor::Output& output = input.get_output();
// shared_ptr<descriptor::TensorView> tv = output.get_tensor_view();
// GPU_TensorViewWrapper tvw{tv, "_arg" + to_string(arg_index)};
// if (!contains(arg_names, tvw.get_name()))
// {
// arg_names.insert(tvw.get_name());
// if (arg_index++ > 0)
// {
// writer << ",";
// }
// writer << "\n";
// writer << tvw.get_type() << "* " << tvw.get_name();
// }
// in.push_back(tvw);
// }
// vector<GPU_TensorViewWrapper> out;
// for (const descriptor::Output& output : n.get_outputs())
// {
// shared_ptr<descriptor::TensorView> tv = output.get_tensor_view();
// GPU_TensorViewWrapper tvw{tv, "_out" + to_string(arg_index)};
// if (arg_index++ > 0)
// {
// writer << ",";
// }
// writer << "\n";
// writer << tvw.get_type() << "* " << tvw.get_name();
// out.push_back(tvw);
// }
// writer.indent--;
// if (node->liveness_new_list.size() > 0)
// {
// temporaries_used = true;
// for (descriptor::Tensor* tensor : node->liveness_new_list)
// {
// worst_case_tmp_size += tensor->size();
// }
// }
// }
// if (temporaries_used)
// {
// size_t temp_pool_size = current_function->get_temporary_pool_size();
// writer << "// Allocate the memory pool\n";
// writer << "// Memory pool size is " << temp_pool_size << " bytes\n";
// writer << "// Worst case size is " << worst_case_tmp_size << " bytes\n";
// writer << "ngraph::runtime::AlignedBuffer memory_handler(" << temp_pool_size << ", "
// << ngraph::runtime::gpu::alignment << ");\n";
// writer << "size_t pool_gpu_ptr = (size_t)memory_handler.get_ptr();\n";
// writer << "\n";
// // Add temporaries to the variable name map
// for (shared_ptr<Node> node : current_function->get_ordered_ops())
// {
// for (descriptor::Tensor* tensor : node->liveness_new_list)
// {
// stringstream ss;
// ss << "((" << tensor->get_element_type().c_type_string() << "*)(pool_gpu_ptr + "
// << tensor->get_pool_offset() << "))";
// m_variable_name_map[tensor->get_name()] = ss.str();
// }
// }
// }
// // Add inputs to the variable name map
// size_t arg_index = 0;
// for (shared_ptr<op::Parameter> param : current_function->get_parameters())
// {
// for (size_t i = 0; i < param->get_output_size(); ++i)
// {
// shared_ptr<descriptor::TensorView> tv = param->get_output_tensor_view(i);
// const element::Type& et = tv->get_tensor_view_type()->get_element_type();
// string type = et.c_type_string();
// stringstream ss;
// ss << "((" << type << "*)(inputs[" << arg_index << "]))";
// m_variable_name_map[tv->get_tensor().get_name()] = ss.str();
// arg_index++;
// }
// }
// // create output alias map
// size_t output_index = 0;
// unordered_map<descriptor::TensorView*, vector<size_t>> output_alias_map;
// vector<size_t> aliases;
// for (size_t i = 0; i < current_function->get_output_size(); ++i)
// {
// shared_ptr<Node> op = current_function->get_output_op(i);
// shared_ptr<descriptor::TensorView> otv = op->get_output_tensor_view();
// vector<size_t>& al = output_alias_map[otv.get()];
// al.push_back(output_index);
// if (al.size() > 1)
// {
// aliases.push_back(output_index);
// }
// output_index++;
// }
// // Add outputs to the variable name map
// output_index = 0;
// for (size_t i = 0; i < current_function->get_output_size(); ++i)
// {
// shared_ptr<Node> op = current_function->get_output_op(i);
// shared_ptr<descriptor::TensorView> tv = op->get_output_tensor_view();
// const element::Type& et = tv->get_tensor_view_type()->get_element_type();
// bool parameter_as_output = false;
// for (shared_ptr<op::Parameter> param : current_function->get_parameters())
// {
// for (const descriptor::Output& pout : param->get_outputs())
// {
// shared_ptr<descriptor::TensorView> ptv = pout.get_tensor_view();
// if (tv == ptv)
// {
// parameter_as_output = true;
// writer << "memcpy(static_cast<" << et.c_type_string() << "*>(outputs["
// << output_index << "]), "
// << m_variable_name_map[ptv->get_tensor().get_name()] << ", "
// << ptv->get_tensor().size() << ");\n";
// break;
// }
// }
// }
// if (!parameter_as_output && !contains(aliases, output_index))
// {
// if (contains(constants, tv.get()))
// {
// writer << "memcpy(outputs[" << output_index << "], " << tv->get_tensor().get_name()
// << ", " << tv->get_tensor().size() << ");\n";
// }
// else
// {
// string type = et.c_type_string();
// stringstream ss;
// ss << "((" << type << "*)(outputs[" << output_index << "]))";
// m_variable_name_map[tv->get_tensor().get_name()] = ss.str();
// }
// }
// output_index++;
// }
// for (shared_ptr<Node> node : current_function->get_ordered_ops())
// {
// auto& n = *node; // Work around a compiler warning (*node inside typeid may have effects
// // with shared pointers, which is fine here but clang doesn't like it.)
// auto handler = dispatcher.find(type_index(typeid(n)));
// if (handler == dispatcher.end())
// {
// throw ngraph_error("Unhandled op during code generation : " + node->description());
// }
// vector<GPU_TensorViewWrapper> in;
// for (const descriptor::Input& input : node->get_inputs())
// {
// const descriptor::Output& output = input.get_output();
// shared_ptr<descriptor::TensorView> tv = output.get_tensor_view();
// in.push_back(GPU_TensorViewWrapper(tv, m_variable_name_map[tv->get_tensor().get_name()]));
// }
// vector<GPU_TensorViewWrapper> out;
// for (const descriptor::Output& output : node->get_outputs())
// {
// shared_ptr<descriptor::TensorView> tv = output.get_tensor_view();
// out.push_back(GPU_TensorViewWrapper(tv, m_variable_name_map[tv->get_tensor().get_name()]));
// }
// // Emit operation prologue
// if (!node->is_parameter() && !node->is_constant())
// {
// if (m_use_tbb)
// {
// writer << "tbb::flow::continue_node<tbb::flow::continue_msg> "
// "flowgraph_node_"
// << node->get_name() << "(G, [&](const tbb::flow::continue_msg &msg)\n{\n";
// writer.indent++;
// }
// if (m_emit_timing)
// {
// emit_debug_function_entry(writer, node.get(), in, out);
// }
// }
// // Emit operation body
// string func_name;
// auto it = match_functions.find(node.get());
// if (it != match_functions.end())
// {
// func_name = it->second;
// }
// if (func_name.empty())
// {
// handler->second(&emitter, node.get(), in, out);
// }
// else
// {
// vector<string> names;
// for (const GPU_TensorViewWrapper& tv : in)
// {
// names.push_back(tv.get_name());
// }
// for (const GPU_TensorViewWrapper& tv : out)
// {
// names.push_back(tv.get_name());
// }
// writer << func_name << "(" << join(names) << ");\n";
// }
// // Emit operation epilogue
// if (!node->is_parameter() && !node->is_constant())
// {
// handle_output_alias(writer, *node, output_alias_map);
// if (m_emit_timing)
// {
// emit_debug_function_exit(writer, node.get(), in, out);
// }
// if (m_use_tbb)
// {
// writer.indent--;
// writer << "});\n";
// }
// }
// }
// if (m_use_tbb)
// {
// writer << "\n";
// // Build the flow graph
// vector<Node*> dependence_graph_heads;
// traverse_nodes(current_function, [&writer, &dependence_graph_heads](shared_ptr<Node> n) {
// if (!n->is_parameter() && !n->is_constant())
// {
// bool is_head = true;
// for (auto arg : n->get_input_ops())
// {
// if (!arg->is_parameter() && !arg->is_constant())
// {
// is_head = false;
// writer << "tbb::flow::make_edge(flowgraph_node_" << arg->get_name()
// << ", flowgraph_node_" << n->get_name() << ");\n";
// }
// }
// if (is_head)
// {
// dependence_graph_heads.emplace_back(n.get());
// }
// }
// });
// writer << "\n";
// // Execute the flow graph
// if (!dependence_graph_heads.empty())
// {
// for (Node* n : dependence_graph_heads)
// {
// writer << "flowgraph_node_" << n->get_name()
// << ".try_put(tbb::flow::continue_msg());\n";
// }
// writer << "try { G.wait_for_all(); } catch(...) { throw; }\n";
// }
// }
// writer.indent--;
// // End generated function
// writer += "}\n\n";
// }
// TODO: Cleanup and make this a utility function
file_util::make_directory(s_output_dir);
......
......@@ -39,7 +39,7 @@ namespace ngraph
class GPU_CallFrame;
using OpFunction =
std::function<void(GPU_Emitter*,
std::function<void(codegen::CodeWriter&,
const ngraph::Node*,
const std::vector<GPU_TensorViewWrapper>& inputs,
const std::vector<GPU_TensorViewWrapper>& outputs)>;
......
......@@ -14,7 +14,7 @@
#include <memory>
#include <cuda.h>
#include <cuda_runtime.h>
#include "ngraph/descriptor/layout/dense_tensor_view_layout.hpp"
#include "ngraph/descriptor/primary_tensor_view.hpp"
......@@ -33,25 +33,35 @@ runtime::gpu::GPU_TensorView::GPU_TensorView(const ngraph::element::Type& elemen
true,
false))
{
// Need to check type and have host/device tensors
m_descriptor->set_tensor_view_layout(
std::make_shared<ngraph::descriptor::layout::DenseTensorViewLayout>(*m_descriptor));
m_buffer_size = m_descriptor->get_tensor_view_layout()->get_size() * element_type.size();
// cuMemAlloc(&dev_buffer, m_buffer_size);
if (m_buffer_size > 0)
{
cudaMalloc(&m_allocated_buffer_pool, m_buffer_size);
}
}
runtime::gpu::GPU_TensorView::~GPU_TensorView()
{
// cuMemFree(dev_buffer);
cudaFree(m_allocated_buffer_pool);
}
void runtime::gpu::GPU_TensorView::write(const void* source, size_t tensor_offset, size_t n)
{
// cuMemcpyHtoD(dev_buffer, source, n);
if (tensor_offset + n > m_buffer_size)
{
throw out_of_range("write access past end of tensor");
}
cudaMemcpy(m_allocated_buffer_pool, source, n, cudaMemcpyHostToDevice);
}
void runtime::gpu::GPU_TensorView::read(void* target, size_t tensor_offset, size_t n) const
{
// cuMemcpyDtoH(target, dev_buffer, n);
if (tensor_offset + n > m_buffer_size)
{
throw out_of_range("read access past end of tensor");
}
cudaMemcpy(target, m_allocated_buffer_pool, n, cudaMemcpyDeviceToHost);
}
......@@ -49,12 +49,6 @@ public:
/// @param n Number of bytes to read, must be integral number of elements.
void read(void* p, size_t tensor_offset, size_t n) const override;
// const char* get_data_ptr();
// const char* get_data_ptr() const;
private:
CUdeviceptr dev_buffer;
// At some point need to deal with alignment
void** m_allocated_buffer_pool;
size_t m_buffer_size;
};
// ----------------------------------------------------------------------------
// 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 <cassert>
#include <cstdlib>
#include <iostream>
#include <stddef.h>
#include <stdio.h>
#include "cuda.h"
#include "cuda_runtime.h"
#include "ngraph/runtime/gpu/gpu_util.hpp"
using namespace ngraph;
using namespace std;
void runtime::gpu::print_gpu_f32_tensor(void* p, size_t element_count, size_t element_size)
{
float* local;
size_t size_in_bytes = element_size * element_count;
local = static_cast<float*>(malloc(size_in_bytes));
cudaMemcpy(local, p, size_in_bytes, cudaMemcpyDeviceToHost);
for (size_t i = 0; i < element_count; i++)
{
std::cout << local[i] << "\n";
}
}
void runtime::gpu::check_cuda_errors(CUresult err)
{
assert(err == CUDA_SUCCESS);
}
// ----------------------------------------------------------------------------
// 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
namespace ngraph
{
namespace runtime
{
namespace gpu
{
void print_gpu_f32_tensor(void* p, size_t element_count, size_t element_size);
void check_cuda_errors(CUresult err);
}
}
}
......@@ -76,6 +76,8 @@ endif()
if(NGRAPH_GPU_ENABLE AND LLVM_INCLUDE_DIR)
include_directories(SYSTEM ${LLVM_INCLUDE_DIR})
link_directories(${LLVM_LIB_DIR})
link_directories(${CUDA_LIBRARIES})
link_directories(${CUDA_CUBLAS_LIBRARIES})
set(SRC
${SRC}
cudnn.cpp)
......@@ -123,7 +125,7 @@ if(LLVM_INCLUDE_DIR)
endif()
if(CUDA_INCLUDE_DIRS)
target_link_libraries(unit-test ${CUDA_LIBRARIES} ${CUDNN_LIBRARIES})
target_link_libraries(unit-test ${CUDA_LIBRARIES} ${CUDNN_LIBRARIES} ${CUDA_CUBLAS_LIBRARIES})
endif()
target_link_libraries(unit-test ngraph libgtest pthread)
......
......@@ -19,6 +19,7 @@
#include <gtest/gtest.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <cudnn.h>
#include "ngraph/codegen/compiler.hpp"
......@@ -45,6 +46,7 @@ TEST(cudnn, compileTest)
#include <cassert>
#include <fstream>
#include <iostream>
#include "cublas_v2.h"
#include "cuda.h"
void check_cuda_errors(CUresult err) {
......@@ -60,6 +62,15 @@ int main(int argc, char **argv) {
CUlinkState linker;
int dev_count;
// Cublas init
cudaError_t cudaStat;
cublasStatus_t stat;
cublasHandle_t handle;
stat = cublasCreate(&handle);
cublasDestroy(handle);
// CUDA initialization
check_cuda_errors(cuInit(0));
check_cuda_errors(cuDeviceGetCount(&dev_count));
......@@ -251,48 +262,48 @@ const auto str = R"(
auto module = compiler.compile(source);
}
TEST(cudnn, abc)
{
auto shape = Shape{2, 2};
auto A = make_shared<op::Parameter>(element::f32, shape);
auto B = make_shared<op::Parameter>(element::f32, shape);
auto C = make_shared<op::Parameter>(element::f32, shape);
auto f = make_shared<Function>((A + B) * C, op::Parameters{A, B, C});
auto manager = runtime::Manager::get("GPU");
auto external = manager->compile(f);
auto backend = manager->allocate_backend();
auto cf = backend->make_call_frame(external);
// Create some tensors for input/output
shared_ptr<runtime::TensorView> a = backend->make_primary_tensor_view(element::f32, shape);
shared_ptr<runtime::TensorView> b = backend->make_primary_tensor_view(element::f32, shape);
shared_ptr<runtime::TensorView> c = backend->make_primary_tensor_view(element::f32, shape);
shared_ptr<runtime::TensorView> result = backend->make_primary_tensor_view(element::f32, shape);
copy_data(a, test::NDArray<float, 2>({{1, 2}, {3, 4}}).get_vector());
copy_data(b, test::NDArray<float, 2>({{5, 6}, {7, 8}}).get_vector());
copy_data(c, test::NDArray<float, 2>({{9, 10}, {11, 12}}).get_vector());
cf->call({a, b, c}, {result});
EXPECT_EQ(result->read_vector<float>(),
(test::NDArray<float, 2>({{54, 80}, {110, 144}})).get_vector());
cf->call({b, a, c}, {result});
EXPECT_EQ(result->read_vector<float>(),
(test::NDArray<float, 2>({{54, 80}, {110, 144}})).get_vector());
cf->call({a, c, b}, {result});
EXPECT_EQ(result->read_vector<float>(),
(test::NDArray<float, 2>({{50, 72}, {98, 128}})).get_vector());
}
// TEST(cudnn, abc)
// {
// auto shape = Shape{2, 2};
// auto A = make_shared<op::Parameter>(element::f32, shape);
// auto B = make_shared<op::Parameter>(element::f32, shape);
// auto C = make_shared<op::Parameter>(element::f32, shape);
// auto f = make_shared<Function>((A + B) * C, op::Parameters{A, B, C});
// auto manager = runtime::Manager::get("GPU");
// auto external = manager->compile(f);
// auto backend = manager->allocate_backend();
// auto cf = backend->make_call_frame(external);
// // Create some tensors for input/output
// shared_ptr<runtime::TensorView> a = backend->make_primary_tensor_view(element::f32, shape);
// shared_ptr<runtime::TensorView> b = backend->make_primary_tensor_view(element::f32, shape);
// shared_ptr<runtime::TensorView> c = backend->make_primary_tensor_view(element::f32, shape);
// shared_ptr<runtime::TensorView> result = backend->make_primary_tensor_view(element::f32, shape);
// copy_data(a, test::NDArray<float, 2>({{1, 2}, {3, 4}}).get_vector());
// copy_data(b, test::NDArray<float, 2>({{5, 6}, {7, 8}}).get_vector());
// copy_data(c, test::NDArray<float, 2>({{9, 10}, {11, 12}}).get_vector());
// cf->call({a, b, c}, {result});
// EXPECT_EQ(result->read_vector<float>(),
// (test::NDArray<float, 2>({{54, 80}, {110, 144}})).get_vector());
// cf->call({b, a, c}, {result});
// EXPECT_EQ(result->read_vector<float>(),
// (test::NDArray<float, 2>({{54, 80}, {110, 144}})).get_vector());
// cf->call({a, c, b}, {result});
// EXPECT_EQ(result->read_vector<float>(),
// (test::NDArray<float, 2>({{50, 72}, {98, 128}})).get_vector());
// }
TEST(cudnn, dot1d)
{
auto shape = Shape{4};
auto A = make_shared<op::Parameter>(element::f32, shape);
auto B = make_shared<op::Parameter>(element::f32, shape);
auto shape_r = Shape{};
auto shape_r = Shape{1};
auto f = make_shared<Function>(make_shared<op::Dot>(A, B), op::Parameters{A, B});
auto manager = runtime::Manager::get("GPU");
......@@ -308,5 +319,5 @@ TEST(cudnn, dot1d)
auto result = backend->make_primary_tensor_view(element::f32, shape_r);
cf->call({a, b}, {result});
EXPECT_EQ((vector<float>{170}), result->read_vector<float>());
EXPECT_EQ((vector<float>{170}), read_vector<float>(result));
}
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