Commit 35bbdce5 authored by Shucai Xiao's avatar Shucai Xiao
Browse files

Add two operators---cos and tan.

parent 1a56cbc1
...@@ -54,6 +54,9 @@ struct onnx_parser ...@@ -54,6 +54,9 @@ struct onnx_parser
// disable dropout for inference // disable dropout for inference
add_generic_op("Dropout", op::identity{}); add_generic_op("Dropout", op::identity{});
add_generic_op("Identity", op::identity{}); add_generic_op("Identity", op::identity{});
add_generic_op("Sin", op::sin{});
add_generic_op("Cos", op::cos{});
add_generic_op("Tan", op::tan{});
add_broadcastable_binary_op("Add", op::add{}); add_broadcastable_binary_op("Add", op::add{});
add_broadcastable_binary_op("Div", op::div{}); add_broadcastable_binary_op("Div", op::div{});
......
...@@ -13,6 +13,8 @@ endif() ...@@ -13,6 +13,8 @@ endif()
add_library(migraphx_device add_library(migraphx_device
device/add.cpp device/add.cpp
device/sin.cpp device/sin.cpp
device/cos.cpp
device/tan.cpp
device/add_relu.cpp device/add_relu.cpp
device/contiguous.cpp device/contiguous.cpp
device/mul.cpp device/mul.cpp
...@@ -40,6 +42,8 @@ add_library(migraphx_gpu ...@@ -40,6 +42,8 @@ add_library(migraphx_gpu
leaky_relu.cpp leaky_relu.cpp
add.cpp add.cpp
sin.cpp sin.cpp
cos.cpp
tan.cpp
mul.cpp mul.cpp
batchnorm.cpp batchnorm.cpp
write_literals.cpp write_literals.cpp
......
#include <migraphx/gpu/cos.hpp>
#include <migraphx/operators.hpp>
#include <migraphx/manage_ptr.hpp>
#include <migraphx/config.hpp>
#include <migraphx/gpu/miopen.hpp>
#include <utility>
namespace migraphx {
inline namespace MIGRAPH_INLINE_NS {
namespace gpu {
shape hip_cos::compute_shape(const std::vector<shape>& inputs) const
{
check_shapes{inputs, *this}.has(2);
return inputs.at(0);
}
argument hip_cos::compute(context& ctx, const shape&, const std::vector<argument>& args) const
{
device::cos(ctx.get_stream().get(), args[1], args[0]);
return args[1];
}
} // namespace gpu
} // namespace MIGRAPH_INLINE_NS
} // namespace migraphx
#include <migraphx/gpu/device/cos.hpp>
#include <migraphx/gpu/device/nary.hpp>
#include <migraphx/gpu/device/types.hpp>
namespace migraphx {
inline namespace MIGRAPH_INLINE_NS {
namespace gpu {
namespace device {
void cos(hipStream_t stream, const argument& result, const argument& arg)
{
nary(stream, result, arg)([](auto x) { return ::cos(to_hip_type(x)); });
}
} // namespace device
} // namespace gpu
} // namespace MIGRAPH_INLINE_NS
} // namespace migraphx
#include <migraphx/gpu/device/tan.hpp>
#include <migraphx/gpu/device/nary.hpp>
#include <migraphx/gpu/device/types.hpp>
namespace migraphx {
inline namespace MIGRAPH_INLINE_NS {
namespace gpu {
namespace device {
void tan(hipStream_t stream, const argument& result, const argument& arg)
{
nary(stream, result, arg)([](auto x) { return ::tan(to_hip_type(x)); });
}
} // namespace device
} // namespace gpu
} // namespace MIGRAPH_INLINE_NS
} // namespace migraphx
#ifndef MIGRAPH_GUARD_RTGLIB_COS_HPP
#define MIGRAPH_GUARD_RTGLIB_COS_HPP
#include <migraphx/gpu/lowering.hpp>
#include <migraphx/manage_ptr.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/operators.hpp>
#include <migraphx/generate.hpp>
#include <migraphx/shape_for_each.hpp>
#include <migraphx/gpu/miopen.hpp>
#include <migraphx/gpu/hip.hpp>
#include <migraphx/dfor.hpp>
#include <migraphx/gpu/device/contiguous.hpp>
#include <migraphx/gpu/device/sin.hpp>
#include <migraphx/gpu/device/cos.hpp>
#include <migraphx/iterator_for.hpp>
#include <migraphx/gpu/rocblas.hpp>
#include <migraphx/gpu/context.hpp>
#include <migraphx/config.hpp>
#include <utility>
namespace migraphx {
inline namespace MIGRAPH_INLINE_NS {
namespace gpu {
struct hip_cos
{
std::string name() const { return "gpu::cos"; }
shape compute_shape(const std::vector<shape>& inputs) const;
argument compute(context&, const shape&, const std::vector<argument>& args) const;
int output_alias(const std::vector<shape>& shapes) const { return shapes.size() - 1; }
};
} // namespace gpu
} // namespace MIGRAPH_INLINE_NS
} // namespace migraphx
#endif
#ifndef MIGRAPH_GUARD_RTGLIB_DEVICE_COS_HPP
#define MIGRAPH_GUARD_RTGLIB_DEVICE_COS_HPP
#include <migraphx/argument.hpp>
#include <migraphx/config.hpp>
#include <hip/hip_runtime_api.h>
namespace migraphx {
inline namespace MIGRAPH_INLINE_NS {
namespace gpu {
namespace device {
void cos(hipStream_t stream, const argument& result, const argument& arg);
} // namespace device
} // namespace gpu
} // namespace MIGRAPH_INLINE_NS
} // namespace migraphx
#endif
#ifndef MIGRAPH_GUARD_RTGLIB_DEVICE_ADD_HPP #ifndef MIGRAPH_GUARD_RTGLIB_DEVICE_SIN_HPP
#define MIGRAPH_GUARD_RTGLIB_DEVICE_ADD_HPP #define MIGRAPH_GUARD_RTGLIB_DEVICE_SIN_HPP
#include <migraphx/argument.hpp> #include <migraphx/argument.hpp>
#include <migraphx/config.hpp> #include <migraphx/config.hpp>
......
#ifndef MIGRAPH_GUARD_RTGLIB_DEVICE_TAN_HPP
#define MIGRAPH_GUARD_RTGLIB_DEVICE_TAN_HPP
#include <migraphx/argument.hpp>
#include <migraphx/config.hpp>
#include <hip/hip_runtime_api.h>
namespace migraphx {
inline namespace MIGRAPH_INLINE_NS {
namespace gpu {
namespace device {
void tan(hipStream_t stream, const argument& result, const argument& arg);
} // namespace device
} // namespace gpu
} // namespace MIGRAPH_INLINE_NS
} // namespace migraphx
#endif
#ifndef MIGRAPH_GUARD_RTGLIB_TAN_HPP
#define MIGRAPH_GUARD_RTGLIB_TAN_HPP
#include <migraphx/gpu/lowering.hpp>
#include <migraphx/manage_ptr.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/operators.hpp>
#include <migraphx/generate.hpp>
#include <migraphx/shape_for_each.hpp>
#include <migraphx/gpu/miopen.hpp>
#include <migraphx/gpu/hip.hpp>
#include <migraphx/dfor.hpp>
#include <migraphx/gpu/device/contiguous.hpp>
#include <migraphx/gpu/device/sin.hpp>
#include <migraphx/gpu/device/cos.hpp>
#include <migraphx/gpu/device/tan.hpp>
#include <migraphx/iterator_for.hpp>
#include <migraphx/gpu/rocblas.hpp>
#include <migraphx/gpu/context.hpp>
#include <migraphx/config.hpp>
#include <utility>
namespace migraphx {
inline namespace MIGRAPH_INLINE_NS {
namespace gpu {
struct hip_tan
{
std::string name() const { return "gpu::tan"; }
shape compute_shape(const std::vector<shape>& inputs) const;
argument compute(context&, const shape&, const std::vector<argument>& args) const;
int output_alias(const std::vector<shape>& shapes) const { return shapes.size() - 1; }
};
} // namespace gpu
} // namespace MIGRAPH_INLINE_NS
} // namespace migraphx
#endif
...@@ -20,6 +20,8 @@ ...@@ -20,6 +20,8 @@
#include <migraphx/gpu/softmax.hpp> #include <migraphx/gpu/softmax.hpp>
#include <migraphx/gpu/add.hpp> #include <migraphx/gpu/add.hpp>
#include <migraphx/gpu/sin.hpp> #include <migraphx/gpu/sin.hpp>
#include <migraphx/gpu/cos.hpp>
#include <migraphx/gpu/tan.hpp>
#include <migraphx/gpu/mul.hpp> #include <migraphx/gpu/mul.hpp>
#include <migraphx/gpu/batchnorm.hpp> #include <migraphx/gpu/batchnorm.hpp>
#include <migraphx/gpu/pooling.hpp> #include <migraphx/gpu/pooling.hpp>
...@@ -68,10 +70,6 @@ struct miopen_apply ...@@ -68,10 +70,6 @@ struct miopen_apply
{ {
check_shape(s, apply_add(it)); check_shape(s, apply_add(it));
} }
else if(it->name() == "sin")
{
check_shape(s, apply_sin(it));
}
else if(it->name() == "mul") else if(it->name() == "mul")
{ {
check_shape(s, apply_mul(it)); check_shape(s, apply_mul(it));
...@@ -80,6 +78,18 @@ struct miopen_apply ...@@ -80,6 +78,18 @@ struct miopen_apply
{ {
check_shape(s, apply_gemm(it)); check_shape(s, apply_gemm(it));
} }
else if(it->name() == "sin")
{
check_shape(s, apply_sin(it));
}
else if(it->name() == "cos")
{
check_shape(s, apply_cos(it));
}
else if(it->name() == "tan")
{
check_shape(s, apply_tan(it));
}
else if(it->name() == "contiguous") else if(it->name() == "contiguous")
{ {
check_shape(s, apply_contiguous(it)); check_shape(s, apply_contiguous(it));
...@@ -176,6 +186,18 @@ struct miopen_apply ...@@ -176,6 +186,18 @@ struct miopen_apply
return prog->replace_instruction(ins, hip_sin{}, ins->inputs().at(0), output); return prog->replace_instruction(ins, hip_sin{}, ins->inputs().at(0), output);
} }
instruction_ref apply_cos(instruction_ref ins)
{
auto output = insert_allocation(ins, ins->get_shape());
return prog->replace_instruction(ins, hip_cos{}, ins->inputs().at(0), output);
}
instruction_ref apply_tan(instruction_ref ins)
{
auto output = insert_allocation(ins, ins->get_shape());
return prog->replace_instruction(ins, hip_tan{}, ins->inputs().at(0), output);
}
instruction_ref apply_mul(instruction_ref ins) instruction_ref apply_mul(instruction_ref ins)
{ {
auto output = insert_allocation(ins, ins->get_shape()); auto output = insert_allocation(ins, ins->get_shape());
......
#include <migraphx/gpu/tan.hpp>
#include <migraphx/operators.hpp>
#include <migraphx/manage_ptr.hpp>
#include <migraphx/config.hpp>
#include <migraphx/gpu/miopen.hpp>
#include <utility>
namespace migraphx {
inline namespace MIGRAPH_INLINE_NS {
namespace gpu {
shape hip_tan::compute_shape(const std::vector<shape>& inputs) const
{
check_shapes{inputs, *this}.has(2);
return inputs.at(0);
}
argument hip_tan::compute(context& ctx, const shape&, const std::vector<argument>& args) const
{
device::tan(ctx.get_stream().get(), args[1], args[0]);
return args[1];
}
} // namespace gpu
} // namespace MIGRAPH_INLINE_NS
} // namespace migraphx
...@@ -215,6 +215,30 @@ struct test_sin ...@@ -215,6 +215,30 @@ struct test_sin
} }
}; };
struct test_cos
{
migraphx::program create_program() const
{
migraphx::program p;
migraphx::shape s{migraphx::shape::double_type, {8}};
auto x = p.add_parameter("x", s);
p.add_instruction(migraphx::op::cos{}, x);
return p;
}
};
struct test_tan
{
migraphx::program create_program() const
{
migraphx::program p;
migraphx::shape s{migraphx::shape::float_type, {16}};
auto x = p.add_parameter("x", s);
p.add_instruction(migraphx::op::tan{}, x);
return p;
}
};
struct test_scale struct test_scale
{ {
migraphx::program create_program() const migraphx::program create_program() const
......
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