Commit 73ca4689 authored by wsttiger's avatar wsttiger
Browse files

Merge branch 'concat' of https://github.com/ROCmSoftwarePlatform/RTGLib into concat

parents 4443608d c5697826
...@@ -90,9 +90,15 @@ argument from_gpu(argument arg) ...@@ -90,9 +90,15 @@ argument from_gpu(argument arg)
void gpu_sync() { hipDeviceSynchronize(); } void gpu_sync() { hipDeviceSynchronize(); }
void copy_to_gpu(char* dst, const char* src, std::size_t size) void copy_to_gpu(argument src, argument dst)
{ {
hipMemcpy(dst, src, size, hipMemcpyHostToDevice); std::size_t src_size = src.get_shape().bytes();
std::size_t dst_size = dst.get_shape().bytes();
if(src_size > dst_size)
MIGRAPH_THROW("Not enough memory available in destination to do copy");
auto status = hipMemcpy(dst.data(), src.data(), src_size, hipMemcpyHostToDevice);
if(status != hipSuccess)
MIGRAPH_THROW("Copy to gpu failed: " + hip_error(status));
} }
} // namespace gpu } // namespace gpu
} // namespace migraph } // namespace migraph
...@@ -15,7 +15,7 @@ migraph::argument from_gpu(migraph::argument arg); ...@@ -15,7 +15,7 @@ migraph::argument from_gpu(migraph::argument arg);
void gpu_sync(); void gpu_sync();
void copy_to_gpu(char* dst, const char* src, std::size_t size); void copy_to_gpu(argument src, argument dst);
struct hip_allocate struct hip_allocate
{ {
...@@ -67,19 +67,19 @@ struct hip_write ...@@ -67,19 +67,19 @@ struct hip_write
} }
}; };
struct hip_memcpy struct hip_copy
{ {
std::string name() const { return "hip_memcpy"; } std::string name() const { return "hip_copy"; }
shape compute_shape(std::vector<shape> inputs) const { return inputs.at(1); } shape compute_shape(std::vector<shape> inputs) const
argument compute(context&, shape output_shape, std::vector<argument> args) const
{ {
char* dst = args.at(0).data() + offset; check_shapes{inputs}.has(2);
const char* src = args.at(1).data(); return inputs.at(1);
std::size_t size = args.at(1).get_shape().bytes(); }
copy_to_gpu(dst, src, size); argument compute(context&, const shape&, std::vector<argument> args) const
return {std::move(output_shape), dst}; {
copy_to_gpu(args[0], args[1]);
return args[1];
} }
std::size_t offset = 0;
}; };
} // namespace gpu } // namespace gpu
} // namespace migraph } // namespace migraph
......
...@@ -2,11 +2,14 @@ ...@@ -2,11 +2,14 @@
#include <migraph/iterator_for.hpp> #include <migraph/iterator_for.hpp>
#include <migraph/gpu/hip.hpp> #include <migraph/gpu/hip.hpp>
#include <migraph/instruction.hpp> #include <migraph/instruction.hpp>
#include <migraph/env.hpp>
namespace migraph { namespace migraph {
namespace gpu { namespace gpu {
MIGRAPH_DECLARE_ENV_VAR(MIGRAPH_COPY_LITERALS)
struct hip_load_literal struct hip_load_literal
{ {
shape s; shape s;
...@@ -29,6 +32,16 @@ void write_literals::apply(program& p) const ...@@ -29,6 +32,16 @@ void write_literals::apply(program& p) const
for(auto ins : iterator_for(p)) for(auto ins : iterator_for(p))
{ {
if(ins->name() == "@literal") if(ins->name() == "@literal")
{
if(enabled(MIGRAPH_COPY_LITERALS{}))
{
literal l = ins->get_literal();
auto pre = p.add_literal(l);
auto s = p.add_outline(l.get_shape());
auto alloc = p.insert_instruction(std::next(pre), hip_allocate{}, s);
p.replace_instruction(ins, hip_copy{}, pre, alloc);
}
else
{ {
argument a = to_gpu(ins->get_literal().get_argument()); argument a = to_gpu(ins->get_literal().get_argument());
std::size_t n = ctx->literals.size(); std::size_t n = ctx->literals.size();
...@@ -36,6 +49,7 @@ void write_literals::apply(program& p) const ...@@ -36,6 +49,7 @@ void write_literals::apply(program& p) const
p.replace_instruction(ins, hip_load_literal{a.get_shape(), n}); p.replace_instruction(ins, hip_load_literal{a.get_shape(), n});
} }
} }
}
} }
} // namespace gpu } // namespace gpu
} // namespace migraph } // namespace migraph
#include <test.hpp> #include <test.hpp>
#include <basic_ops.hpp> #include <basic_ops.hpp>
#include <migraph/program.hpp> #include <migraph/program.hpp>
#include <migraph/instruction.hpp>
#include <migraph/generate.hpp> #include <migraph/generate.hpp>
#include <migraph/gpu/target.hpp> #include <migraph/gpu/target.hpp>
#include <migraph/gpu/hip.hpp> #include <migraph/gpu/hip.hpp>
...@@ -11,8 +12,16 @@ void gpu_literal_test() ...@@ -11,8 +12,16 @@ void gpu_literal_test()
auto lit = generate_literal(migraph::shape{migraph::shape::float_type, {4, 3, 3, 3}}); auto lit = generate_literal(migraph::shape{migraph::shape::float_type, {4, 3, 3, 3}});
p.add_literal(lit); p.add_literal(lit);
p.compile(migraph::gpu::target{}); p.compile(migraph::gpu::target{});
auto scratch = p.get_parameter("scratch");
if(scratch == p.end())
{
auto result = p.eval({}); auto result = p.eval({});
EXPECT(lit == migraph::gpu::from_gpu(result)); EXPECT(lit == migraph::gpu::from_gpu(result));
}
else
{
EXPECT(scratch->get_shape().bytes() == lit.get_shape().bytes());
}
} }
int main() { gpu_literal_test(); } int main() { gpu_literal_test(); }
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