Unverified Commit 1a692f60 authored by kahmed10's avatar kahmed10 Committed by GitHub
Browse files

Add recip op for gpu (#488)



* add recip gpu and tests

* formatting

* remove to_hip_type
Co-authored-by: default avatarmvermeulen <5479696+mvermeulen@users.noreply.github.com>
parent 478ddd97
......@@ -60,6 +60,7 @@ struct onnx_parser
add_generic_op("Log", op::log{});
add_generic_op("Floor", op::floor{});
add_generic_op("Identity", op::identity{});
add_generic_op("Reciprocal", op::recip{});
add_generic_op("Relu", op::relu{});
add_generic_op("Round", op::round{});
add_generic_op("Sigmoid", op::sigmoid{});
......
......@@ -47,6 +47,7 @@ add_library(migraphx_device
device/pad.cpp
device/pow.cpp
device/prelu.cpp
device/recip.cpp
device/reduce_max.cpp
device/reduce_mean.cpp
device/reduce_min.cpp
......
#include <migraphx/gpu/device/recip.hpp>
#include <migraphx/gpu/device/nary.hpp>
#include <migraphx/gpu/device/types.hpp>
namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace gpu {
namespace device {
void recip(hipStream_t stream, const argument& result, const argument& arg)
{
nary(stream, result, arg)([](auto x) __device__ { return 1 / x; });
}
} // namespace device
} // namespace gpu
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx
#ifndef MIGRAPHX_GUARD_RTGLIB_DEVICE_RECIP_HPP
#define MIGRAPHX_GUARD_RTGLIB_DEVICE_RECIP_HPP
#include <migraphx/argument.hpp>
#include <migraphx/config.hpp>
#include <hip/hip_runtime_api.h>
namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace gpu {
namespace device {
void recip(hipStream_t stream, const argument& result, const argument& arg);
} // namespace device
} // namespace gpu
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx
#endif
#ifndef MIGRAPHX_GUARD_RTGLIB_RECIP_HPP
#define MIGRAPHX_GUARD_RTGLIB_RECIP_HPP
#include <migraphx/gpu/oper.hpp>
#include <migraphx/gpu/device/recip.hpp>
namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace gpu {
struct hip_recip : unary_device<hip_recip, device::recip>
{
};
} // namespace gpu
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx
#endif
......@@ -71,6 +71,7 @@
#include <migraphx/gpu/sqdiff.hpp>
#include <migraphx/gpu/int8_conv_pack.hpp>
#include <migraphx/gpu/prelu.hpp>
#include <migraphx/gpu/recip.hpp>
#include <utility>
#include <functional>
#include <algorithm>
......@@ -166,6 +167,7 @@ struct miopen_apply
add_generic_op<hip_sigmoid>("sigmoid");
add_generic_op<hip_ceil>("ceil");
add_generic_op<hip_floor>("floor");
add_generic_op<hip_recip>("recip");
add_extend_op<miopen_contiguous, op::contiguous>("contiguous");
add_extend_op<hip_concat, op::concat>("concat");
......
......@@ -2419,4 +2419,19 @@ TEST_CASE(op_capture)
EXPECT(migraphx::verify_range(vec, cap_vec));
}
TEST_CASE(recip_test)
{
migraphx::program p;
migraphx::shape s{migraphx::shape::double_type, {3}};
std::vector<float> data{-0.5f, 0.1f, 0.5f};
auto l = p.add_literal(migraphx::literal{s, data});
p.add_instruction(migraphx::op::recip{}, l);
p.compile(migraphx::cpu::target{});
auto result = p.eval({}).back();
std::vector<float> results_vector(3);
result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
std::vector<float> gold = {-2.0f, 10.0f, 2.0f};
EXPECT(migraphx::verify_range(results_vector, gold));
}
int main(int argc, const char* argv[]) { test::run(argc, argv); }
......@@ -4473,4 +4473,16 @@ struct test_convert : verify_program<test_convert>
};
};
struct test_recip : verify_program<test_recip>
{
migraphx::program create_program() const
{
migraphx::program p;
migraphx::shape s{migraphx::shape::double_type, {3}};
auto x = p.add_parameter("x", s);
p.add_instruction(migraphx::op::recip{}, x);
return p;
}
};
int main(int argc, const char* argv[]) { test::run(argc, argv); }
......@@ -1455,6 +1455,20 @@ def prelu_brcst_test():
return ([node], [arg0, arg1], [arg_out])
@onnx_test
def recip_test():
x = helper.make_tensor_value_info('x', TensorProto.FLOAT, [3])
y = helper.make_tensor_value_info('y', TensorProto.FLOAT, [3])
node = onnx.helper.make_node(
'Reciprocal',
inputs=['x'],
outputs=['y'],
)
return ([node], [x], [y])
@onnx_test
def reducel1_test():
x = helper.make_tensor_value_info('x', TensorProto.FLOAT, [3, 4, 5, 6])
......
......@@ -1133,6 +1133,17 @@ TEST_CASE(prelu_brcst_test)
EXPECT(p == prog);
}
TEST_CASE(recip_test)
{
migraphx::program p;
auto input = p.add_parameter("x", migraphx::shape{migraphx::shape::float_type, {3}});
p.add_instruction(migraphx::op::recip{}, input);
auto prog = optimize_onnx("recip_test.onnx");
EXPECT(p == prog);
}
TEST_CASE(reducel1_test)
{
migraphx::program p;
......

recip_test:B

xy"
Reciprocal
recip_testZ
x

b
y

B
\ No newline at end of file
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