Commit 02e0dd2a authored by Paul's avatar Paul
Browse files

Merge branch 'master' into onnx_fixes

parents 78f248e4 b4771050
...@@ -13,9 +13,9 @@ endif() ...@@ -13,9 +13,9 @@ endif()
if(CMAKE_CXX_COMPILER MATCHES ".*hcc") if(CMAKE_CXX_COMPILER MATCHES ".*hcc")
message(STATUS "Enable miopen backend") message(STATUS "Enable miopen backend")
set(MIGRAPH_ENABLE_MIOPEN On CACHE BOOL "") set(MIGRAPH_ENABLE_GPU On CACHE BOOL "")
else() else()
set(MIGRAPH_ENABLE_MIOPEN Off CACHE BOOL "") set(MIGRAPH_ENABLE_GPU Off CACHE BOOL "")
endif() endif()
add_compile_options(-std=c++14) add_compile_options(-std=c++14)
......
...@@ -73,6 +73,8 @@ exclude_patterns = [] ...@@ -73,6 +73,8 @@ exclude_patterns = []
# The name of the Pygments (syntax highlighting) style to use. # The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx' pygments_style = 'sphinx'
highlight_language = 'cpp'
# If true, `todo` and `todoList` produce output, else they produce nothing. # If true, `todo` and `todoList` produce output, else they produce nothing.
todo_include_todos = False todo_include_todos = False
...@@ -155,3 +157,6 @@ texinfo_documents = [ ...@@ -155,3 +157,6 @@ texinfo_documents = [
breathe_default_members = ('members', 'undoc-members') breathe_default_members = ('members', 'undoc-members')
cpp_index_common_prefix = ['migraph::'] cpp_index_common_prefix = ['migraph::']
default_role = 'any'
primary_domain = 'cpp'
...@@ -10,6 +10,7 @@ Welcome to MIGraph's documentation! ...@@ -10,6 +10,7 @@ Welcome to MIGraph's documentation!
:maxdepth: 2 :maxdepth: 2
:caption: Contents: :caption: Contents:
overview
reference/data reference/data
reference/operators reference/operators
reference/program reference/program
......
Overview
========
MIGraph provides an optimized execution engine for deep learning neural networks.
Building a program
------------------
A program consists of a set of instructions to be executed when calling `eval <migraph::program::eval>`. Each instruction has an associated `operation <migraph::operation>` which represents the computation to be performed by the instruction.
We can start by building a simple program to add two numbers together::
program p;
instruction_ref one = p.add_literal(1);
instruction_ref two = p.add_literal(2);
p.add_instruction(add{}, one, two);
The `add_literal <migraph::program::add_literal>` function will add an instruction to the program to store a literal number. The `instruction_ref <migraph::instruction_ref>` is a reference to the instruction in the program, which can be used to compose the output of the instruction with another instruction.
After creating the literals, we then create the instruction to add the numbers together. This is done by using the `add{} <migraph::add>` operation class along with the `instruction_ref <migraph::instruction_ref>` for the input arguments of the instruction.
Finally, we can run this `program <migraph::program>` by compiling it for the cpu and then running it with `eval <migraph::program::eval>`::
p.compile(cpu::target{});
argument result = p.eval({});
The easiest way to see the result is to print it::
std::cout << result;
Which will print ``3``.
We can also compile the program for the gpu as well.
Adding parameters
-----------------
Of course, this program will always produce the same value which is quite uninteresting. Instead, we want to pass an input to a program and compute a value based on the input. This can be done with a parameter. For example, we can modify the program to take an input ``x``::
program p;
instruction_ref x = p.add_parameter("x", {shape::int64_type});
instruction_ref two = p.add_literal(2);
p.add_instruction(add{}, one, two);
p.compile(cpu::target{});
This adds a parameter of type ``int64``, and compiles it for the ``cpu``. To run the program, we need to pass the parameter to it when we call `eval <migraph::program::eval>`::
argument result = p.eval({
{"x", literal{1}.get_argument()}
});
std::cout << result;
This will print ``3``.
A parameter is given as an `argument <migraph::argument>`. In this case, the simplest way of creating an `argument <migraph::argument>` is from a `literal <migraph::literal>`.
Tensor data
-----------
In this example we are just creating numbers, but the `shape <migraph::shape>` class can describe multi-dimensional tensors. For example, we can build a simple network with convolution and relu::
program p;
instruction_ref input = p.add_parameter("x", shape{shape::float_type, {1, 3, 32, 32}});
instruction_ref weights = p.add_parameter("w", shape{shape::float_type, {1, 3, 5, 5}});
instruction_ref conv = p.add_instruction(convolution{}, input, weights);
p.add_instruction(activation{"relu"}, conv);
Here we create two parameters for both the ``input`` and ``weights``. In the previous examples, we just created simple literals, however, most programs will take data from already allocated buffers(usually on the GPU). In this case, we can create `argument <migraph::argument>` objects directly from the pointers to the buffers::
// Compile the program
p.compile(gpu::target{});
// Allocated buffers by the user
float* input = ...;
float* weights = ...;
// Create the arguments
argument input_arg{shape{shape::float_type, {1, 3, 32, 32}}, input};
argument weights_arg{shape{shape::float_type, {1, 3, 32, 32}}, weights};
p.eval({{"x", input_arg}, {"w", weights_arg}})
An `argument <migraph::argument>` can handle memory buffers from either the GPU or the CPU, but when running the `program <migraph::program>`, buffers should be allocated for the corresponding target. That is, when compiling for the CPU, the buffers should be allocated on the CPU, and when compiling for the GPU the buffers should be allocated on the GPU.
Importing from onnx
-------------------
A `program <migraph::program>` can be built directly from an onnx file, which makes it easier to use neural networks directly from other frameworks. In this case, there is an ``parse_onnx`` function::
program p = parse_onnx("model.onnx");
p.compile(gpu::target{});
...@@ -10,3 +10,8 @@ dead_code_elimination ...@@ -10,3 +10,8 @@ dead_code_elimination
--------------------- ---------------------
.. doxygenstruct:: migraph::dead_code_elimination .. doxygenstruct:: migraph::dead_code_elimination
write_literals
--------------
.. doxygenstruct:: migraph::gpu::write_literals
...@@ -6,7 +6,19 @@ instruction ...@@ -6,7 +6,19 @@ instruction
.. doxygenstruct:: migraph::instruction .. doxygenstruct:: migraph::instruction
instruction_ref
---------------
.. cpp:type:: migraph::instruction_ref
References an instruction in the program.
program program
------- -------
.. doxygenstruct:: migraph::program .. doxygenstruct:: migraph::program
parse_onnx
----------
.. doxygenfunction:: migraph::parse_onnx
...@@ -6,3 +6,13 @@ target ...@@ -6,3 +6,13 @@ target
.. doxygenstruct:: migraph::target .. doxygenstruct:: migraph::target
gpu::target
-----------
.. doxygenstruct:: migraph::gpu::target
cpu::target
-----------
.. doxygenstruct:: migraph::cpu::cpu_target
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
namespace migraph { namespace migraph {
/// Create a program from an onnx file
program parse_onnx(const std::string& name); program parse_onnx(const std::string& name);
} // namespace migraph } // namespace migraph
......
...@@ -23,5 +23,5 @@ target_link_libraries(mnist migraph_cpu migraph_onnx) ...@@ -23,5 +23,5 @@ target_link_libraries(mnist migraph_cpu migraph_onnx)
if(MIGRAPH_ENABLE_GPU) if(MIGRAPH_ENABLE_GPU)
add_executable(verify_onnx verify_onnx.cpp) add_executable(verify_onnx verify_onnx.cpp)
rocm_clang_tidy_check(verify_onnx) rocm_clang_tidy_check(verify_onnx)
target_link_libraries(verify_onnx migraph_onnx migraph_cpu migraph_miopen) target_link_libraries(verify_onnx migraph_onnx migraph_cpu migraph_gpu)
endif() endif()
...@@ -2,11 +2,11 @@ ...@@ -2,11 +2,11 @@
#include <migraph/onnx.hpp> #include <migraph/onnx.hpp>
#include <migraph/cpu/cpu_target.hpp> #include <migraph/cpu/cpu_target.hpp>
#include <migraph/miopen/target.hpp> #include <migraph/gpu/target.hpp>
#include <migraph/miopen/hip.hpp> #include <migraph/gpu/hip.hpp>
#include <migraph/generate.hpp> #include <migraph/generate.hpp>
#include <miopen/miopen.h> #include <miopen/miopen.h>
#include <migraph/miopen/miopen.hpp> #include <migraph/gpu/miopen.hpp>
migraph::argument run_cpu(std::string file) migraph::argument run_cpu(std::string file)
{ {
...@@ -24,15 +24,14 @@ migraph::argument run_gpu(std::string file) ...@@ -24,15 +24,14 @@ migraph::argument run_gpu(std::string file)
auto p = migraph::parse_onnx(file); auto p = migraph::parse_onnx(file);
p.compile(migraph::cpu::cpu_target{}); p.compile(migraph::cpu::cpu_target{});
auto s = p.get_parameter_shape("Input3"); auto s = p.get_parameter_shape("Input3");
auto input3 = migraph::miopen::to_gpu(migraph::generate_argument(s)); auto input3 = migraph::gpu::to_gpu(migraph::generate_argument(s));
auto output = auto output = migraph::gpu::to_gpu(migraph::generate_argument(p.get_parameter_shape("output")));
migraph::miopen::to_gpu(migraph::generate_argument(p.get_parameter_shape("output"))); auto handle = migraph::gpu::make_obj<migraph::gpu::miopen_handle>(&miopenCreate);
auto handle = migraph::miopen::make_obj<migraph::miopen::miopen_handle>(&miopenCreate);
auto out = p.eval({{"Input3", input3}, {"output", output}}); auto out = p.eval({{"Input3", input3}, {"output", output}});
std::cout << p << std::endl; std::cout << p << std::endl;
return migraph::miopen::from_gpu(out); return migraph::gpu::from_gpu(out);
} }
int main(int argc, char const* argv[]) int main(int argc, char const* argv[])
......
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