Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
MIGraphX
Commits
24d68767
Unverified
Commit
24d68767
authored
May 01, 2019
by
mvermeulen
Committed by
GitHub
May 01, 2019
Browse files
Merge pull request #249 from ROCmSoftwarePlatform/copy_program
Copy program support
parents
8245bcaf
a55c6eec
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
155 additions
and
4 deletions
+155
-4
src/include/migraphx/program.hpp
src/include/migraphx/program.hpp
+12
-1
src/program.cpp
src/program.cpp
+64
-2
src/targets/cpu/lowering.cpp
src/targets/cpu/lowering.cpp
+1
-1
test/program_test.cpp
test/program_test.cpp
+78
-0
No files found.
src/include/migraphx/program.hpp
View file @
24d68767
...
@@ -30,8 +30,16 @@ const operation& get_operation(instruction_ref ins);
...
@@ -30,8 +30,16 @@ const operation& get_operation(instruction_ref ins);
struct
program
struct
program
{
{
program
();
program
();
// move constructor
program
(
program
&&
)
noexcept
;
program
(
program
&&
)
noexcept
;
program
&
operator
=
(
program
&&
)
noexcept
;
// copy constructor
program
(
const
program
&
);
// copy assignment operator
program
&
operator
=
(
program
);
~
program
()
noexcept
;
~
program
()
noexcept
;
using
parameter_map
=
std
::
unordered_map
<
std
::
string
,
argument
>
;
using
parameter_map
=
std
::
unordered_map
<
std
::
string
,
argument
>
;
...
@@ -118,6 +126,9 @@ struct program
...
@@ -118,6 +126,9 @@ struct program
friend
bool
operator
==
(
const
program
&
x
,
const
program
&
y
);
friend
bool
operator
==
(
const
program
&
x
,
const
program
&
y
);
friend
bool
operator
!=
(
const
program
&
x
,
const
program
&
y
)
{
return
!
(
x
==
y
);
}
friend
bool
operator
!=
(
const
program
&
x
,
const
program
&
y
)
{
return
!
(
x
==
y
);
}
private:
void
assign
(
const
program
&
p
);
private:
private:
std
::
unique_ptr
<
program_impl
>
impl
;
std
::
unique_ptr
<
program_impl
>
impl
;
};
};
...
...
src/program.cpp
View file @
24d68767
...
@@ -86,8 +86,70 @@ static void print_program(const program& p, F print_func)
...
@@ -86,8 +86,70 @@ static void print_program(const program& p, F print_func)
program
::
program
()
:
impl
(
std
::
make_unique
<
program_impl
>
())
{}
program
::
program
()
:
impl
(
std
::
make_unique
<
program_impl
>
())
{}
program
::
program
(
program
&&
)
noexcept
=
default
;
program
::
program
(
program
&&
)
noexcept
=
default
;
program
&
program
::
operator
=
(
program
&&
)
noexcept
=
default
;
program
::~
program
()
noexcept
=
default
;
program
::~
program
()
noexcept
=
default
;
// copy constructor
program
::
program
(
const
program
&
p
)
{
assign
(
p
);
}
// copy assignment operator
program
&
program
::
operator
=
(
program
p
)
{
std
::
swap
(
p
.
impl
,
this
->
impl
);
return
*
this
;
}
void
program
::
assign
(
const
program
&
p
)
{
// clean the current program
if
(
!
impl
)
{
impl
=
std
::
make_unique
<
program_impl
>
();
}
else
if
(
!
impl
->
instructions
.
empty
())
{
impl
->
instructions
.
clear
();
}
impl
->
ctx
=
p
.
impl
->
ctx
;
std
::
unordered_map
<
instruction_ref
,
instruction_ref
>
ins_map
;
for
(
auto
ins
:
iterator_for
(
p
))
{
instruction_ref
copy_ins
{};
if
(
ins
->
name
()
==
"@literal"
)
{
auto
l
=
ins
->
get_literal
();
copy_ins
=
impl
->
instructions
.
insert
(
impl
->
instructions
.
end
(),
instruction
{
l
});
}
else
if
(
ins
->
name
()
==
"@param"
)
{
auto
&&
name
=
any_cast
<
builtin
::
param
>
(
ins
->
get_operator
()).
parameter
;
auto
s
=
ins
->
get_shape
();
copy_ins
=
impl
->
instructions
.
insert
(
impl
->
instructions
.
end
(),
{
builtin
::
param
{
name
},
std
::
move
(
s
),
{}});
}
else
if
(
ins
->
name
()
==
"@outline"
)
{
auto
s
=
ins
->
get_shape
();
copy_ins
=
impl
->
instructions
.
insert
(
impl
->
instructions
.
end
(),
{
builtin
::
outline
{
s
},
s
,
{}});
}
else
{
// retrieve its mapped input
auto
inputs
=
ins
->
inputs
();
// ensure all inputs have its corresponding copy instructions
assert
(
std
::
all_of
(
inputs
.
begin
(),
inputs
.
end
(),
[
&
](
auto
i
)
{
return
ins_map
.
count
(
i
)
>
0
;
}));
std
::
vector
<
instruction_ref
>
copy_inputs
(
inputs
.
size
());
std
::
transform
(
inputs
.
begin
(),
inputs
.
end
(),
copy_inputs
.
begin
(),
[
&
](
auto
i
)
{
return
ins_map
[
i
];
});
copy_ins
=
add_instruction
(
ins
->
get_operator
(),
copy_inputs
);
}
ins_map
[
ins
]
=
copy_ins
;
}
}
instruction_ref
program
::
add_instruction
(
const
operation
&
op
,
std
::
vector
<
instruction_ref
>
args
)
instruction_ref
program
::
add_instruction
(
const
operation
&
op
,
std
::
vector
<
instruction_ref
>
args
)
{
{
...
...
src/targets/cpu/lowering.cpp
View file @
24d68767
...
@@ -772,7 +772,7 @@ template <typename Op>
...
@@ -772,7 +772,7 @@ template <typename Op>
struct
cpu_binary
struct
cpu_binary
{
{
Op
op
;
Op
op
;
std
::
string
name
()
const
{
return
op
.
name
();
}
std
::
string
name
()
const
{
return
"cpu::"
+
op
.
name
();
}
shape
compute_shape
(
const
std
::
vector
<
shape
>&
inputs
)
const
{
return
inputs
.
front
();
}
shape
compute_shape
(
const
std
::
vector
<
shape
>&
inputs
)
const
{
return
inputs
.
front
();
}
argument
compute
(
context
&
,
const
shape
&
output_shape
,
std
::
vector
<
argument
>
args
)
const
argument
compute
(
context
&
,
const
shape
&
output_shape
,
std
::
vector
<
argument
>
args
)
const
{
{
...
...
test/program_test.cpp
View file @
24d68767
...
@@ -2,6 +2,10 @@
...
@@ -2,6 +2,10 @@
#include <migraphx/program.hpp>
#include <migraphx/program.hpp>
#include <migraphx/iterator_for.hpp>
#include <migraphx/iterator_for.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/op/add.hpp>
#include <migraphx/op/dot.hpp>
#include <migraphx/op/mul.hpp>
#include <migraphx/cpu/target.hpp>
#include <sstream>
#include <sstream>
#include "test.hpp"
#include "test.hpp"
#include <basic_ops.hpp>
#include <basic_ops.hpp>
...
@@ -27,4 +31,78 @@ TEST_CASE(program_equality)
...
@@ -27,4 +31,78 @@ TEST_CASE(program_equality)
EXPECT
(
x
==
y
);
EXPECT
(
x
==
y
);
}
}
TEST_CASE
(
program_copy
)
{
auto
create_program_1
=
[]
{
migraphx
::
program
p
;
migraphx
::
shape
s
{
migraphx
::
shape
::
float_type
,
{
3
,
4
,
5
}};
std
::
vector
<
float
>
data
(
3
*
4
*
5
);
std
::
iota
(
data
.
begin
(),
data
.
end
(),
1.0
f
);
auto
l2
=
p
.
add_literal
(
migraphx
::
literal
(
s
,
data
));
auto
p1
=
p
.
add_parameter
(
"x"
,
s
);
auto
po
=
p
.
add_outline
(
s
);
auto
sum
=
p
.
add_instruction
(
migraphx
::
op
::
add
{},
l2
,
po
);
p
.
add_instruction
(
migraphx
::
op
::
mul
{},
sum
,
p1
);
return
p
;
};
{
auto
p1
=
create_program_1
();
migraphx
::
program
p2
{};
p2
=
p1
;
p2
.
compile
(
migraphx
::
cpu
::
target
{});
EXPECT
(
p1
!=
p2
);
p1
.
compile
(
migraphx
::
cpu
::
target
{});
EXPECT
(
p1
==
p2
);
}
{
auto
p1
=
create_program_1
();
auto
p2
(
p1
);
EXPECT
(
p1
==
p2
);
p1
.
compile
(
migraphx
::
cpu
::
target
{});
EXPECT
(
p1
!=
p2
);
p2
=
p1
;
EXPECT
(
p1
==
p2
);
}
{
auto
p1
=
create_program_1
();
auto
p2
=
create_program
();
EXPECT
(
p1
!=
p2
);
p2
=
p1
;
EXPECT
(
p1
==
p2
);
p1
.
compile
(
migraphx
::
cpu
::
target
{});
p2
.
compile
(
migraphx
::
cpu
::
target
{});
EXPECT
(
p1
==
p2
);
}
{
migraphx
::
program
p1
;
migraphx
::
shape
s1
{
migraphx
::
shape
::
float_type
,
{
2
,
3
}};
migraphx
::
shape
s2
{
migraphx
::
shape
::
float_type
,
{
3
,
6
}};
migraphx
::
shape
s3
{
migraphx
::
shape
::
float_type
,
{
2
,
6
}};
auto
para1
=
p1
.
add_parameter
(
"m1"
,
s1
);
auto
para2
=
p1
.
add_parameter
(
"m2"
,
s2
);
auto
para3
=
p1
.
add_parameter
(
"m3"
,
s3
);
p1
.
add_instruction
(
migraphx
::
op
::
dot
{
0.31
f
,
0.28
f
},
para1
,
para2
,
para3
);
migraphx
::
program
p2
{};
p2
=
p1
;
EXPECT
(
p2
==
p1
);
p1
.
compile
(
migraphx
::
cpu
::
target
{});
p2
.
compile
(
migraphx
::
cpu
::
target
{});
EXPECT
(
p2
==
p1
);
}
}
int
main
(
int
argc
,
const
char
*
argv
[])
{
test
::
run
(
argc
,
argv
);
}
int
main
(
int
argc
,
const
char
*
argv
[])
{
test
::
run
(
argc
,
argv
);
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment