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
a9c48b34
Commit
a9c48b34
authored
Sep 26, 2018
by
Paul
Browse files
Merge branch 'mem_color_separate_literal'
parents
036bc485
03e15057
Changes
24
Show whitespace changes
Inline
Side-by-side
Showing
20 changed files
with
690 additions
and
31 deletions
+690
-31
src/CMakeLists.txt
src/CMakeLists.txt
+9
-0
src/eliminate_allocation.cpp
src/eliminate_allocation.cpp
+5
-0
src/include/migraph/instruction.hpp
src/include/migraph/instruction.hpp
+1
-1
src/include/migraph/memory_coloring.hpp
src/include/migraph/memory_coloring.hpp
+18
-0
src/include/migraph/operators.hpp
src/include/migraph/operators.hpp
+0
-1
src/include/migraph/pass_config.hpp
src/include/migraph/pass_config.hpp
+11
-0
src/include/migraph/program.hpp
src/include/migraph/program.hpp
+2
-1
src/opt/common_header.hpp
src/opt/common_header.hpp
+26
-0
src/opt/memory_coloring.cpp
src/opt/memory_coloring.cpp
+14
-0
src/opt/memory_coloring_impl.cpp
src/opt/memory_coloring_impl.cpp
+359
-0
src/opt/memory_coloring_impl.hpp
src/opt/memory_coloring_impl.hpp
+181
-0
src/program.cpp
src/program.cpp
+19
-1
src/targets/cpu/CMakeLists.txt
src/targets/cpu/CMakeLists.txt
+5
-0
src/targets/gpu/CMakeLists.txt
src/targets/gpu/CMakeLists.txt
+9
-0
src/targets/gpu/eliminate_workspace.cpp
src/targets/gpu/eliminate_workspace.cpp
+4
-0
src/targets/gpu/hip.cpp
src/targets/gpu/hip.cpp
+4
-1
src/targets/gpu/include/migraph/gpu/context.hpp
src/targets/gpu/include/migraph/gpu/context.hpp
+1
-2
src/targets/gpu/include/migraph/gpu/hip.hpp
src/targets/gpu/include/migraph/gpu/hip.hpp
+16
-17
src/targets/gpu/include/migraph/gpu/target.hpp
src/targets/gpu/include/migraph/gpu/target.hpp
+0
-2
src/targets/gpu/lowering.cpp
src/targets/gpu/lowering.cpp
+6
-5
No files found.
src/CMakeLists.txt
View file @
a9c48b34
...
@@ -11,6 +11,8 @@ add_library(migraph
...
@@ -11,6 +11,8 @@ add_library(migraph
program.cpp
program.cpp
shape.cpp
shape.cpp
simplify_reshapes.cpp
simplify_reshapes.cpp
opt/memory_coloring.cpp
opt/memory_coloring_impl.cpp
)
)
rocm_clang_tidy_check
(
migraph
)
rocm_clang_tidy_check
(
migraph
)
target_include_directories
(
migraph PUBLIC $<BUILD_INTERFACE:
${
CMAKE_CURRENT_SOURCE_DIR
}
/include>
)
target_include_directories
(
migraph PUBLIC $<BUILD_INTERFACE:
${
CMAKE_CURRENT_SOURCE_DIR
}
/include>
)
...
@@ -20,3 +22,10 @@ add_subdirectory(targets/cpu)
...
@@ -20,3 +22,10 @@ add_subdirectory(targets/cpu)
if
(
MIGRAPH_ENABLE_GPU
)
if
(
MIGRAPH_ENABLE_GPU
)
add_subdirectory
(
targets/gpu
)
add_subdirectory
(
targets/gpu
)
endif
()
endif
()
#install (TARGETS migraph
# LIBRARY DESTINATION /opt/rocm/lib)
#install (DIRECTORY include/migraph DESTINATION /opt/rocm/include)
src/eliminate_allocation.cpp
View file @
a9c48b34
...
@@ -4,12 +4,17 @@
...
@@ -4,12 +4,17 @@
#include <migraph/operators.hpp>
#include <migraph/operators.hpp>
#include <migraph/iterator_for.hpp>
#include <migraph/iterator_for.hpp>
#include <migraph/ranges.hpp>
#include <migraph/ranges.hpp>
#include <migraph/stringutils.hpp>
#include <migraph/pass_config.hpp>
namespace
migraph
{
namespace
migraph
{
void
eliminate_allocation
::
apply
(
program
&
p
)
const
void
eliminate_allocation
::
apply
(
program
&
p
)
const
{
{
assert
(
alignment
>
0
);
assert
(
alignment
>
0
);
if
(
!
enabled
(
MIGRAPH_DISABLE_MEMORY_COLORING
{}))
return
;
std
::
size_t
n
=
0
;
std
::
size_t
n
=
0
;
std
::
vector
<
std
::
pair
<
instruction_ref
,
std
::
size_t
>>
allocs
;
std
::
vector
<
std
::
pair
<
instruction_ref
,
std
::
size_t
>>
allocs
;
for
(
auto
ins
:
iterator_for
(
p
))
for
(
auto
ins
:
iterator_for
(
p
))
...
...
src/include/migraph/instruction.hpp
View file @
a9c48b34
...
@@ -71,13 +71,13 @@ struct instruction
...
@@ -71,13 +71,13 @@ struct instruction
// internal
// internal
void
replace_argument
(
instruction_ref
old
,
instruction_ref
new_ins
);
void
replace_argument
(
instruction_ref
old
,
instruction_ref
new_ins
);
private:
operation
op
;
operation
op
;
shape
result
;
shape
result
;
std
::
vector
<
instruction_ref
>
output
;
std
::
vector
<
instruction_ref
>
output
;
std
::
vector
<
instruction_ref
>
arguments
;
std
::
vector
<
instruction_ref
>
arguments
;
literal
lit
;
literal
lit
;
};
};
}
// namespace migraph
}
// namespace migraph
namespace
std
{
namespace
std
{
...
...
src/include/migraph/memory_coloring.hpp
0 → 100644
View file @
a9c48b34
#ifndef MIGRAPH_GUARD_RTGLIB_MEMORY_COLORING_HPP
#define MIGRAPH_GUARD_RTGLIB_MEMORY_COLORING_HPP
#include <string>
#include <migraph/instruction_ref.hpp>
namespace
migraph
{
struct
program
;
struct
memory_coloring
{
std
::
string
allocation_op
{};
std
::
string
name
()
const
{
return
"memory coloring"
;
}
void
apply
(
program
&
p
)
const
;
};
}
// namespace migraph
#endif
src/include/migraph/operators.hpp
View file @
a9c48b34
...
@@ -570,7 +570,6 @@ struct outline
...
@@ -570,7 +570,6 @@ struct outline
return
{
s
,
nullptr
};
return
{
s
,
nullptr
};
}
}
};
};
}
// namespace migraph
}
// namespace migraph
#endif
#endif
src/include/migraph/pass_config.hpp
0 → 100644
View file @
a9c48b34
#ifndef MIGRAPH_GUARD_PASS_CONFIG_HPP
#define MIGRAPH_GUARD_PASS_CONFIG_HPP
#include <migraph/env.hpp>
namespace
migraph
{
MIGRAPH_DECLARE_ENV_VAR
(
MIGRAPH_DISABLE_MEMORY_COLORING
)
}
// namespace migraph
#endif // MIGRAPH_GUARD_PASS_CONFIG_HPP
src/include/migraph/program.hpp
View file @
a9c48b34
...
@@ -75,6 +75,8 @@ struct program
...
@@ -75,6 +75,8 @@ struct program
shape
get_parameter_shape
(
std
::
string
name
)
const
;
shape
get_parameter_shape
(
std
::
string
name
)
const
;
instruction_ref
get_parameter
(
std
::
string
name
)
const
;
std
::
unordered_map
<
std
::
string
,
shape
>
get_parameter_shapes
()
const
;
std
::
unordered_map
<
std
::
string
,
shape
>
get_parameter_shapes
()
const
;
argument
eval
(
parameter_map
params
)
const
;
argument
eval
(
parameter_map
params
)
const
;
...
@@ -100,7 +102,6 @@ struct program
...
@@ -100,7 +102,6 @@ struct program
private:
private:
std
::
unique_ptr
<
program_impl
>
impl
;
std
::
unique_ptr
<
program_impl
>
impl
;
};
};
}
// namespace migraph
}
// namespace migraph
#endif
#endif
src/opt/common_header.hpp
0 → 100644
View file @
a9c48b34
#ifndef MIGRAPH_GUARD_RTGLIB_COMMON_HEADER_HPP
#define MIGRAPH_GUARD_RTGLIB_COMMON_HEADER_HPP
#include <migraph/program.hpp>
#include <migraph/stringutils.hpp>
#include <migraph/instruction.hpp>
#include <migraph/operators.hpp>
#include <migraph/iterator_for.hpp>
#include <migraph/pass_config.hpp>
#include <set>
#include <list>
#include <vector>
#include <queue>
namespace
migraph
{
//#define MIGRAPH_DEBUG_OPT
#ifdef MIGRAPH_DEBUG_OPT
#define MIGRAPH_DEBUG(s) s
#else
#define MIGRAPH_DEBUG(s)
#endif // MIGRAPH_DEBUG_OPT
}
// namespace migraph
#endif // MIGRAPH_GUARD_RTGLIB_COMMON_HEADER_HPP
src/opt/memory_coloring.cpp
0 → 100644
View file @
a9c48b34
#include <migraph/memory_coloring.hpp>
#include "memory_coloring_impl.hpp"
namespace
migraph
{
void
memory_coloring
::
apply
(
program
&
p
)
const
{
if
(
!
enabled
(
MIGRAPH_DISABLE_MEMORY_COLORING
{}))
{
memory_coloring_impl
opt
(
&
p
,
allocation_op
);
opt
.
run
();
}
}
}
// namespace migraph
src/opt/memory_coloring_impl.cpp
0 → 100644
View file @
a9c48b34
#include "memory_coloring_impl.hpp"
namespace
migraph
{
void
memory_coloring_impl
::
run
()
{
MIGRAPH_DEBUG
(
dump
(
"---Before memory coloring---"
));
MIGRAPH_DEBUG
(
dump_program
());
register_operand_alias
();
build
();
if
(
num_of_lives
!=
0
)
{
MIGRAPH_DEBUG
(
dump_intervals
());
// Coloring
while
(
!
alloc_queue
.
empty
())
{
interval_ptr
interval
=
alloc_queue
.
top
();
allocate
(
interval
);
alloc_queue
.
pop
();
}
rewrite
();
MIGRAPH_DEBUG
(
verify
());
}
}
bool
memory_coloring_impl
::
allocate
(
interval_ptr
interval
)
{
shape
s
=
interval
->
result
;
std
::
size_t
size
=
s
.
bytes
();
if
(
size
==
0
)
return
false
;
std
::
size_t
element_size
=
size
/
s
.
elements
();
live_range
&
segment
=
interval
->
segment
;
int
vn
=
segment
.
vn
;
std
::
priority_queue
<
live_range
*
,
std
::
vector
<
live_range
*>
,
ordering
>
conflict_queue
;
std
::
unordered_map
<
long
long
,
live_range
*>
offset2_live
;
offset2_live
.
clear
();
if
(
conflict_table
.
find
(
vn
)
!=
conflict_table
.
end
())
{
std
::
set
<
int
>&
vn_set
=
conflict_table
[
vn
];
for
(
auto
&
iter
:
vn_set
)
{
live_range
*
range
=
live_ranges
[
iter
];
long
long
offset
=
range
->
offset
;
if
(
offset
!=
invalid_offset
)
{
conflict_queue
.
push
(
range
);
if
(
offset2_live
.
find
(
offset
)
==
offset2_live
.
end
())
{
offset2_live
[
offset
]
=
range
;
}
else
{
live_range
*
prev
=
offset2_live
[
offset
];
assert
(
prev
->
offset
==
offset
);
if
(
prev
->
size
<
range
->
size
)
offset2_live
[
offset
]
=
range
;
}
}
}
}
long
long
offset
=
0
;
while
(
!
conflict_queue
.
empty
())
{
live_range
*
range
=
conflict_queue
.
top
();
long
long
iter_offset
=
range
->
offset
;
if
(
offset
>
iter_offset
)
{
offset
=
std
::
max
(
offset
,
iter_offset
+
range
->
size
);
}
else
if
(
offset2_live
[
iter_offset
]
==
range
)
{
if
((
iter_offset
>
offset
)
&&
(
iter_offset
-
offset
)
>=
size
)
{
break
;
}
offset
=
iter_offset
+
range
->
size
;
}
// alignment
if
((
offset
%
element_size
)
!=
0
)
offset
+=
(
element_size
-
(
offset
%
element_size
));
conflict_queue
.
pop
();
}
segment
.
offset
=
offset
;
MIGRAPH_DEBUG
(
segment
.
dump
());
required_bytes
=
std
::
max
(
required_bytes
,
offset
+
segment
.
size
);
return
true
;
}
void
memory_coloring_impl
::
build
()
{
std
::
size_t
num_of_instrs
=
p_program
->
size
();
if
(
num_of_instrs
==
0
)
return
;
int
cur_points
=
num_of_instrs
*
2
;
instruction_ref
iter
=
p_program
->
end
();
instruction_ref
begin
=
p_program
->
begin
();
std
::
vector
<
instruction_ref
>
dead_instrs
;
std
::
set
<
int
>
live_set
;
// Build live intervals.
live_intervals
.
resize
(
num_of_instrs
);
do
{
iter
=
std
::
prev
(
iter
);
const
instruction
*
p_iter
=
&
(
*
iter
);
interval_ptr
def_interval
=
nullptr
;
bool
is_dead
=
false
;
if
(
instr2_live
.
find
(
p_iter
)
!=
instr2_live
.
end
())
{
def_interval
=
instr2_live
[
p_iter
];
bool
is_lit
=
is_literal
(
iter
);
if
(
is_allocate
(
iter
)
||
is_lit
)
{
live_range
&
range
=
def_interval
->
segment
;
def_interval
->
result
=
iter
->
get_shape
();
def_interval
->
is_literal
=
is_lit
;
if
(
!
is_lit
||
unify_literals
)
alloc_queue
.
push
(
def_interval
);
range
.
begin
=
cur_points
;
def_interval
->
def_point
=
cur_points
;
range
.
size
=
(
iter
->
get_shape
()).
bytes
();
live_set
.
erase
(
range
.
vn
);
}
}
else
if
(
!
is_param
(
iter
)
&&
!
is_outline
(
iter
)
&&
!
is_check_context
(
iter
))
{
is_dead
=
true
;
}
int
tie_ndx
=
get_input_tie_ndx
(
iter
);
int
cnt
=
-
1
;
for
(
auto
&&
arg
:
iter
->
inputs
())
{
cnt
++
;
if
(
is_param
(
arg
)
||
is_outline
(
arg
))
{
if
(
is_output_param
(
arg
))
is_dead
=
false
;
if
(
def_interval
!=
nullptr
)
{
def_interval
->
is_live_on_entry
=
true
;
}
continue
;
}
const
instruction
*
p_arg
=
&
(
*
arg
);
if
(
cnt
==
tie_ndx
&&
(
def_interval
!=
nullptr
))
{
// input memory is used as this instruction's output.
// def is considered as use. Coalesce the live intervals.
def_interval
->
add_use
(
cur_points
);
instr2_live
[
p_arg
]
=
def_interval
;
}
else
if
(
instr2_live
.
find
(
p_arg
)
==
instr2_live
.
end
())
{
// First time see a use, create a live interval.
int
id
=
num_of_lives
++
;
interval_ptr
interval
=
&
(
live_intervals
[
id
]);
interval
->
id
=
id
;
interval
->
segment
.
end
=
cur_points
;
interval
->
segment
.
vn
=
++
max_value_number
;
interval
->
add_use
(
cur_points
);
instr2_live
[
p_arg
]
=
interval
;
add_conflicts
(
live_set
,
max_value_number
);
live_set
.
insert
(
max_value_number
);
live_ranges
[
max_value_number
]
=
&
(
interval
->
segment
);
earliest_end_point
=
cur_points
;
if
(
latest_end_point
==
-
1
)
latest_end_point
=
cur_points
;
}
else
{
interval_ptr
interval
=
instr2_live
[
p_arg
];
interval
->
add_use
(
cur_points
);
assert
(
live_set
.
find
(
interval
->
id
)
!=
live_set
.
end
());
}
}
if
(
is_dead
)
dead_instrs
.
push_back
(
iter
);
cur_points
-=
2
;
}
while
(
iter
!=
begin
);
}
void
memory_coloring_impl
::
register_operand_alias
()
{
operand_alias
[
"hip::allocate"
]
=
-
1
;
operand_alias
[
"@outline"
]
=
-
1
;
operand_alias
[
"check_context"
]
=
-
1
;
operand_alias
[
"@literal"
]
=
-
1
;
operand_alias
[
"@param"
]
=
-
1
;
operand_alias
[
"transpose"
]
=
0
;
operand_alias
[
"flatten"
]
=
0
;
operand_alias
[
"broadcast"
]
=
1
;
operand_alias
[
"reshape"
]
=
0
;
operand_alias
[
"pass"
]
=
0
;
}
void
memory_coloring_impl
::
rewrite
()
{
instruction_ref
end
=
p_program
->
end
();
instruction_ref
scratch_param
=
end
;
std
::
vector
<
std
::
size_t
>
dims
;
dims
.
push_back
(
required_bytes
/
sizeof
(
float
));
shape
s
=
{
shape
::
float_type
,
dims
};
scratch_param
=
p_program
->
add_parameter
(
"scratch"
,
s
);
for
(
auto
ins
:
iterator_for
(
*
p_program
))
{
const
instruction
*
p_iter
=
&
(
*
ins
);
if
(
instr2_live
.
find
(
p_iter
)
!=
instr2_live
.
end
())
{
interval_ptr
interval
=
instr2_live
[
p_iter
];
if
(
interval
->
get_begin
()
==
invalid_offset
)
continue
;
if
(
!
unify_literals
&&
interval
->
is_literal
)
continue
;
std
::
size_t
offset
=
0
;
if
(
interval
->
get_offset
()
==
invalid_offset
)
{
assert
(
interval
->
result
.
bytes
()
==
0
);
}
else
{
offset
=
interval
->
get_offset
();
}
if
(
is_allocate
(
ins
))
{
assert
(
!
ins
->
inputs
().
empty
());
p_program
->
replace_instruction
(
ins
,
load
{
ins
->
inputs
().
at
(
0
)
->
get_shape
(),
offset
},
scratch_param
);
}
else
if
(
is_literal
(
ins
))
{
#if 0
auto pre = p_program->add_literal(ins->lit);
bool pre_copy = (interval->get_begin() < earliest_end_point);
p_program->replace_instruction(
ins, write_literal{offset, pre_copy}, scratch_param, pre);
#endif
}
}
}
MIGRAPH_DEBUG
(
dump
(
"---After rewrite---"
));
MIGRAPH_DEBUG
(
dump_program
());
}
#ifdef MIGRAPH_DEBUG_OPT
void
memory_coloring_impl
::
dump
(
const
std
::
string
&
str
)
{
std
::
cout
<<
str
<<
std
::
endl
;
}
void
memory_coloring_impl
::
dump_program
()
{
std
::
cout
<<
*
p_program
<<
std
::
endl
;
}
void
memory_coloring_impl
::
dump_intervals
()
{
if
(
num_of_lives
>
0
)
{
std
::
cout
<<
"---live intervals ---"
<<
std
::
endl
;
for
(
int
i
=
0
;
i
<
num_of_lives
;
++
i
)
{
live_interval
&
interval
=
live_intervals
[
i
];
interval
.
dump
();
}
std
::
cout
<<
"---conflict table---"
<<
std
::
endl
;
for
(
int
i
=
0
;
i
<=
max_value_number
;
++
i
)
{
std
::
cout
<<
" segment:"
<<
i
;
std
::
cout
<<
" =>"
;
std
::
set
<
int
>&
table
=
conflict_table
[
i
];
for
(
auto
&
iter
:
table
)
{
std
::
cout
<<
(
iter
)
<<
","
;
}
}
std
::
cout
<<
std
::
endl
;
}
}
void
memory_coloring_impl
::
verify
()
{
if
(
num_of_lives
>
0
)
{
for
(
int
i
=
0
;
i
<
num_of_lives
;
++
i
)
{
live_interval
&
interval
=
live_intervals
[
i
];
live_range
&
segment
=
interval
.
segment
;
if
(
segment
.
begin
==
invalid_offset
)
{
assert
(
interval
.
is_live_on_entry
);
continue
;
}
if
(
segment
.
offset
==
invalid_offset
)
{
continue
;
}
int
vn
=
segment
.
vn
;
if
(
conflict_table
.
find
(
vn
)
!=
conflict_table
.
end
())
{
std
::
set
<
int
>&
vn_set
=
conflict_table
[
vn
];
for
(
auto
&
iter
:
vn_set
)
{
live_range
*
range
=
live_ranges
[
iter
];
if
(
range
->
offset
==
invalid_offset
)
continue
;
if
(
!
is_disjoin
(
*
range
,
segment
))
assert
(
false
);
}
}
}
}
}
// map liveness tracking point to instruction enum.
static
int
get_ins_enum
(
int
x
)
{
if
(
x
>
0
)
{
return
(
x
/
2
)
-
1
;
}
else
return
invalid_offset
;
}
void
live_range
::
dump
()
{
std
::
cout
<<
" segment:"
<<
vn
;
std
::
cout
<<
" ["
<<
get_ins_enum
(
begin
)
<<
", "
<<
get_ins_enum
(
end
)
<<
"]"
;
if
(
offset
!=
invalid_offset
)
{
std
::
cout
<<
" mem:"
;
std
::
cout
<<
" ["
<<
offset
<<
","
<<
offset
+
size
-
1
<<
"]"
;
}
std
::
cout
<<
std
::
endl
;
}
void
live_interval
::
dump
()
{
std
::
cout
<<
"id:"
<<
id
;
segment
.
dump
();
std
::
cout
<<
" uses:"
;
for
(
auto
&
iter
:
use_points
)
{
std
::
cout
<<
" "
<<
get_ins_enum
(
iter
)
<<
","
;
}
std
::
cout
<<
" def:"
;
std
::
cout
<<
" "
<<
get_ins_enum
(
def_point
);
if
(
is_literal
)
std
::
cout
<<
" literal"
;
std
::
cout
<<
" "
<<
result
;
std
::
cout
<<
std
::
endl
;
}
#endif
}
// namespace migraph
src/opt/memory_coloring_impl.hpp
0 → 100644
View file @
a9c48b34
#ifndef MIGRAPH_GUARD_RTGLIB_MEMORY_COLORING_IMPL_HPP
#define MIGRAPH_GUARD_RTGLIB_MEMORY_COLORING_IMPL_HPP
#include "common_header.hpp"
namespace
migraph
{
static
const
int
invalid_offset
=
-
1
;
struct
live_range
{
int
begin
;
// begin point in the instruction stream.
int
end
;
// end point in the instruction stream.
long
long
offset
;
// offset to base pointer of allocated memory trunk.
int
vn
;
// value number that identifies this live_range.
long
long
size
;
// size of required memory in bytes
#ifdef MIGRAPH_DEBUG_OPT
void
dump
();
#endif
};
struct
live_interval
{
live_interval
()
:
segment
({
invalid_offset
,
invalid_offset
,
invalid_offset
,
invalid_offset
,
0
})
{
id
=
invalid_offset
;
def_point
=
invalid_offset
;
is_literal
=
false
;
is_live_on_entry
=
false
;
}
void
add_use
(
int
use
)
{
use_points
.
push_front
(
use
);
}
int
get_begin
()
const
{
return
segment
.
begin
;
}
int
get_end
()
const
{
return
segment
.
end
;
}
long
long
get_offset
()
const
{
return
segment
.
offset
;
}
#ifdef MIGRAPH_DEBUG_OPT
void
dump
();
#endif
live_range
segment
;
int
id
;
std
::
list
<
int
>
use_points
;
int
def_point
;
shape
result
;
bool
is_literal
;
bool
is_live_on_entry
;
};
using
interval_ptr
=
live_interval
*
;
struct
memory_coloring_impl
{
memory_coloring_impl
(
program
*
p
,
std
::
string
alloc_op
)
:
p_program
(
p
),
allocation_op
(
std
::
move
(
alloc_op
))
{
instr2_live
.
clear
();
live_ranges
.
clear
();
conflict_table
.
clear
();
num_of_lives
=
0
;
max_value_number
=
-
1
;
required_bytes
=
0
;
operand_alias
.
clear
();
earliest_end_point
=
-
1
;
latest_end_point
=
-
1
;
unify_literals
=
false
;
}
bool
allocate
(
interval_ptr
);
void
add_conflicts
(
std
::
set
<
int
>&
live_set
,
int
val
)
{
for
(
auto
&
iter
:
live_set
)
{
conflict_table
[
iter
].
insert
(
val
);
conflict_table
[
val
].
insert
(
iter
);
}
}
void
build
();
void
run
();
void
register_operand_alias
();
void
rewrite
();
private:
static
bool
is_param
(
const
instruction_ref
ins
)
{
return
ins
->
name
()
==
"@param"
;
}
static
bool
is_output_param
(
const
instruction_ref
ins
)
{
return
is_param
(
ins
)
&&
any_cast
<
builtin
::
param
>
(
ins
->
get_operator
()).
parameter
==
"output"
;
}
bool
is_allocate
(
const
instruction_ref
ins
)
{
return
ins
->
name
()
==
allocation_op
;
}
static
bool
is_outline
(
const
instruction_ref
ins
)
{
return
ins
->
name
()
==
"@outline"
;
}
static
bool
is_literal
(
const
instruction_ref
ins
)
{
return
ins
->
name
()
==
"@literal"
;
}
static
bool
is_check_context
(
const
instruction_ref
ins
)
{
return
ins
->
name
()
==
"check_context"
;
}
// get operand alias info. This is a temporary workaround.
int
get_input_tie_ndx
(
const
instruction_ref
ins
)
{
std
::
string
name
=
ins
->
name
();
if
(
operand_alias
.
find
(
name
)
!=
operand_alias
.
end
())
return
operand_alias
[
name
];
if
(
is_allocate
(
ins
))
{
// This happens to custom allocators.
operand_alias
[
name
]
=
-
1
;
return
-
1
;
}
int
cnt
=
-
1
;
int
last_allocate
=
-
1
;
for
(
auto
&&
arg
:
ins
->
inputs
())
{
cnt
++
;
if
(
is_allocate
(
arg
)
||
is_output_param
(
arg
))
last_allocate
=
cnt
;
}
assert
(
last_allocate
!=
-
1
);
operand_alias
[
name
]
=
last_allocate
;
return
last_allocate
;
}
#ifdef MIGRAPH_DEBUG_OPT
static
bool
is_disjoin
(
live_range
&
range1
,
live_range
&
range2
)
{
if
((
range1
.
size
==
0
)
||
(
range2
.
size
==
0
))
return
false
;
long
long
end1
=
range1
.
offset
+
range1
.
size
-
1
;
long
long
end2
=
range2
.
offset
+
range2
.
size
-
1
;
return
((
end1
<
range2
.
offset
)
||
(
end2
<
range1
.
offset
));
}
void
dump
(
const
std
::
string
&
);
void
dump_program
();
void
dump_intervals
();
void
verify
();
#endif
struct
ordering
{
bool
operator
()(
const
interval_ptr
i1
,
const
interval_ptr
i2
)
const
{
int
len1
=
i1
->
get_end
()
-
i1
->
get_begin
();
int
len2
=
i2
->
get_end
()
-
i2
->
get_begin
();
if
(
len1
!=
len2
)
{
return
(
len1
<
len2
)
?
true
:
false
;
}
else
if
(
i1
->
result
.
bytes
()
!=
i2
->
result
.
bytes
())
{
return
(
i1
->
result
.
bytes
()
<
i2
->
result
.
bytes
())
?
true
:
false
;
}
else
{
return
i1
->
id
>
i2
->
id
;
}
}
bool
operator
()(
const
live_range
*
i1
,
const
live_range
*
i2
)
const
{
return
(
i1
->
offset
>
i2
->
offset
);
}
};
program
*
p_program
;
std
::
unordered_map
<
const
instruction
*
,
interval_ptr
>
instr2_live
;
// universe of live intervals.
std
::
vector
<
live_interval
>
live_intervals
;
// Map live range value number to live range.
std
::
unordered_map
<
int
,
live_range
*>
live_ranges
;
// Map live range value number to a set of conflicting live ranges' value numbers.
std
::
unordered_map
<
int
,
std
::
set
<
int
>>
conflict_table
;
// Priority queue for coloring.
std
::
priority_queue
<
interval_ptr
,
std
::
vector
<
interval_ptr
>
,
ordering
>
alloc_queue
;
std
::
unordered_map
<
std
::
string
,
int
>
operand_alias
;
int
num_of_lives
;
int
max_value_number
;
long
long
required_bytes
;
// The earliest program point where an live interval ends.
int
earliest_end_point
;
// The latest program point where an live interval ends.
int
latest_end_point
;
// Whether to unify literals into coloring.
bool
unify_literals
;
std
::
string
allocation_op
{};
};
}
// namespace migraph
#endif
src/program.cpp
View file @
a9c48b34
...
@@ -203,6 +203,25 @@ shape program::get_parameter_shape(std::string name) const
...
@@ -203,6 +203,25 @@ shape program::get_parameter_shape(std::string name) const
return
{};
return
{};
}
}
instruction_ref
program
::
get_parameter
(
std
::
string
name
)
const
{
auto
ins
=
std
::
find_if
(
impl
->
instructions
.
begin
(),
impl
->
instructions
.
end
(),
[
&
](
const
instruction
&
x
)
{
if
(
x
.
name
()
==
"@param"
)
{
return
any_cast
<
builtin
::
param
>
(
x
.
get_operator
()).
parameter
==
name
;
}
else
{
return
false
;
}
});
if
(
ins
!=
this
->
end
())
return
ins
;
else
return
this
->
end
();
}
std
::
unordered_map
<
std
::
string
,
shape
>
program
::
get_parameter_shapes
()
const
std
::
unordered_map
<
std
::
string
,
shape
>
program
::
get_parameter_shapes
()
const
{
{
std
::
unordered_map
<
std
::
string
,
shape
>
result
;
std
::
unordered_map
<
std
::
string
,
shape
>
result
;
...
@@ -437,5 +456,4 @@ std::ostream& operator<<(std::ostream& os, const program& p)
...
@@ -437,5 +456,4 @@ std::ostream& operator<<(std::ostream& os, const program& p)
print_program
(
os
,
p
,
[](
auto
&&
...)
{});
print_program
(
os
,
p
,
[](
auto
&&
...)
{});
return
os
;
return
os
;
}
}
}
// namespace migraph
}
// namespace migraph
src/targets/cpu/CMakeLists.txt
View file @
a9c48b34
...
@@ -13,3 +13,8 @@ target_link_libraries(migraph_cpu migraph Threads::Threads)
...
@@ -13,3 +13,8 @@ target_link_libraries(migraph_cpu migraph Threads::Threads)
target_include_directories
(
migraph_cpu PUBLIC $<BUILD_INTERFACE:
${
CMAKE_CURRENT_SOURCE_DIR
}
/include>
)
target_include_directories
(
migraph_cpu PUBLIC $<BUILD_INTERFACE:
${
CMAKE_CURRENT_SOURCE_DIR
}
/include>
)
target_include_directories
(
migraph_cpu PRIVATE
${
BLAZE_INCLUDE
}
)
target_include_directories
(
migraph_cpu PRIVATE
${
BLAZE_INCLUDE
}
)
target_compile_definitions
(
migraph_cpu PRIVATE -DBLAZE_USE_CPP_THREADS
)
target_compile_definitions
(
migraph_cpu PRIVATE -DBLAZE_USE_CPP_THREADS
)
#install (TARGETS migraph_cpu
# LIBRARY DESTINATION /opt/rocm/lib)
#install (DIRECTORY include/migraph DESTINATION /opt/rocm/include)
src/targets/gpu/CMakeLists.txt
View file @
a9c48b34
...
@@ -32,3 +32,12 @@ add_library(migraph_gpu
...
@@ -32,3 +32,12 @@ add_library(migraph_gpu
rocm_clang_tidy_check
(
migraph_gpu
)
rocm_clang_tidy_check
(
migraph_gpu
)
target_link_libraries
(
migraph_gpu migraph MIOpen migraph_device roc::rocblas
)
target_link_libraries
(
migraph_gpu migraph MIOpen migraph_device roc::rocblas
)
target_include_directories
(
migraph_gpu PUBLIC $<BUILD_INTERFACE:
${
CMAKE_CURRENT_SOURCE_DIR
}
/include>
)
target_include_directories
(
migraph_gpu PUBLIC $<BUILD_INTERFACE:
${
CMAKE_CURRENT_SOURCE_DIR
}
/include>
)
#install (TARGETS migraph_gpu
# LIBRARY DESTINATION /opt/rocm/lib)
#install (DIRECTORY include/migraph DESTINATION /opt/rocm/include)
#install (TARGETS migraph_device
# LIBRARY DESTINATION /opt/rocm/lib)
#install (DIRECTORY include/migraph DESTINATION /opt/rocm/include)
src/targets/gpu/eliminate_workspace.cpp
View file @
a9c48b34
...
@@ -6,12 +6,16 @@
...
@@ -6,12 +6,16 @@
#include <migraph/iterator_for.hpp>
#include <migraph/iterator_for.hpp>
#include <migraph/ranges.hpp>
#include <migraph/ranges.hpp>
#include <migraph/stringutils.hpp>
#include <migraph/stringutils.hpp>
#include <migraph/pass_config.hpp>
namespace
migraph
{
namespace
migraph
{
namespace
gpu
{
namespace
gpu
{
void
eliminate_workspace
::
apply
(
program
&
p
)
const
void
eliminate_workspace
::
apply
(
program
&
p
)
const
{
{
if
(
!
enabled
(
MIGRAPH_DISABLE_MEMORY_COLORING
{}))
return
;
std
::
size_t
n
=
0
;
std
::
size_t
n
=
0
;
std
::
vector
<
instruction_ref
>
allocs
;
std
::
vector
<
instruction_ref
>
allocs
;
for
(
auto
ins
:
iterator_for
(
p
))
for
(
auto
ins
:
iterator_for
(
p
))
...
...
src/targets/gpu/hip.cpp
View file @
a9c48b34
...
@@ -90,6 +90,9 @@ argument from_gpu(argument arg)
...
@@ -90,6 +90,9 @@ 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
)
{
hipMemcpy
(
dst
,
src
,
size
,
hipMemcpyHostToDevice
);
}
}
// namespace gpu
}
// namespace gpu
}
// namespace migraph
}
// namespace migraph
src/targets/gpu/include/migraph/gpu/context.hpp
View file @
a9c48b34
...
@@ -12,12 +12,11 @@ struct context
...
@@ -12,12 +12,11 @@ struct context
{
{
shared
<
miopen_handle
>
handle
;
shared
<
miopen_handle
>
handle
;
shared
<
rocblas_handle_ptr
>
rbhandle
;
shared
<
rocblas_handle_ptr
>
rbhandle
;
argument
scratch
;
std
::
vector
<
argument
>
literals
{};
std
::
vector
<
argument
>
literals
{};
void
finish
()
const
{
gpu_sync
();
}
void
finish
()
const
{
gpu_sync
();
}
};
};
}
// namespace gpu
}
// namespace gpu
}
// namespace migraph
}
// namespace migraph
#endif
#endif
src/targets/gpu/include/migraph/gpu/hip.hpp
View file @
a9c48b34
...
@@ -15,6 +15,8 @@ migraph::argument from_gpu(migraph::argument arg);
...
@@ -15,6 +15,8 @@ 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
);
struct
hip_allocate
struct
hip_allocate
{
{
std
::
string
tag
{};
std
::
string
tag
{};
...
@@ -30,22 +32,6 @@ struct hip_allocate
...
@@ -30,22 +32,6 @@ struct hip_allocate
}
}
};
};
struct
hip_load
{
shape
s
;
std
::
size_t
offset
=
0
;
std
::
string
name
()
const
{
return
"hip::load"
;
}
shape
compute_shape
(
const
std
::
vector
<
shape
>&
inputs
)
const
{
check_shapes
{
inputs
}.
has
(
1
);
return
s
;
}
argument
compute
(
context
&
,
const
shape
&
,
const
std
::
vector
<
argument
>&
args
)
const
{
return
{
s
,
args
[
0
].
data
()
+
offset
};
}
};
struct
hip_sync
struct
hip_sync
{
{
std
::
string
tag
{};
std
::
string
tag
{};
...
@@ -81,8 +67,21 @@ struct hip_write
...
@@ -81,8 +67,21 @@ struct hip_write
}
}
};
};
struct
hip_memcpy
{
std
::
string
name
()
const
{
return
"hip_memcpy"
;
}
shape
compute_shape
(
std
::
vector
<
shape
>
inputs
)
const
{
return
inputs
.
at
(
1
);
}
argument
compute
(
context
&
,
shape
output_shape
,
std
::
vector
<
argument
>
args
)
const
{
char
*
dst
=
args
.
at
(
0
).
data
()
+
offset
;
const
char
*
src
=
args
.
at
(
1
).
data
();
std
::
size_t
size
=
args
.
at
(
1
).
get_shape
().
bytes
();
copy_to_gpu
(
dst
,
src
,
size
);
return
{
std
::
move
(
output_shape
),
dst
};
}
std
::
size_t
offset
=
0
;
};
}
// namespace gpu
}
// namespace gpu
}
// namespace migraph
}
// namespace migraph
#endif
#endif
src/targets/gpu/include/migraph/gpu/target.hpp
View file @
a9c48b34
...
@@ -12,9 +12,7 @@ struct target
...
@@ -12,9 +12,7 @@ struct target
std
::
vector
<
pass
>
get_passes
(
migraph
::
context
&
gctx
)
const
;
std
::
vector
<
pass
>
get_passes
(
migraph
::
context
&
gctx
)
const
;
migraph
::
context
get_context
()
const
;
migraph
::
context
get_context
()
const
;
};
};
}
// namespace gpu
}
// namespace gpu
}
// namespace migraph
}
// namespace migraph
#endif
#endif
src/targets/gpu/lowering.cpp
View file @
a9c48b34
...
@@ -108,8 +108,11 @@ struct miopen_convolution
...
@@ -108,8 +108,11 @@ struct miopen_convolution
workspace_shape
=
shape
{
shape
::
int8_type
,
{
workspace_size
}};
workspace_shape
=
shape
{
shape
::
int8_type
,
{
workspace_size
}};
auto
x
=
to_gpu
(
generate_argument
(
inputs
[
0
]
->
get_shape
()));
auto
x
=
to_gpu
(
generate_argument
(
inputs
[
0
]
->
get_shape
()));
gpu_sync
();
auto
w
=
to_gpu
(
generate_argument
(
inputs
[
1
]
->
get_shape
()));
auto
w
=
to_gpu
(
generate_argument
(
inputs
[
1
]
->
get_shape
()));
gpu_sync
();
auto
y
=
to_gpu
(
generate_argument
(
output_shape
));
auto
y
=
to_gpu
(
generate_argument
(
output_shape
));
gpu_sync
();
auto
workspace
=
allocate_gpu
(
workspace_shape
);
auto
workspace
=
allocate_gpu
(
workspace_shape
);
int
algo_count
=
1
;
int
algo_count
=
1
;
...
@@ -497,7 +500,5 @@ struct miopen_apply
...
@@ -497,7 +500,5 @@ struct miopen_apply
};
};
void
lowering
::
apply
(
program
&
p
)
const
{
miopen_apply
{
&
p
,
ctx
}.
apply
();
}
void
lowering
::
apply
(
program
&
p
)
const
{
miopen_apply
{
&
p
,
ctx
}.
apply
();
}
}
// namespace gpu
}
// namespace gpu
}
// namespace migraph
}
// namespace migraph
Prev
1
2
Next
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