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
1682b223
Commit
1682b223
authored
Sep 30, 2018
by
Paul
Browse files
Merge from master
parents
e83034cf
1fc7013f
Changes
30
Hide whitespace changes
Inline
Side-by-side
Showing
20 changed files
with
847 additions
and
4 deletions
+847
-4
CMakeLists.txt
CMakeLists.txt
+4
-0
LICENSE
LICENSE
+24
-0
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
+145
-0
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/include/migraph/shape.hpp
src/include/migraph/shape.hpp
+1
-0
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/shape.cpp
src/shape.cpp
+6
-0
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
No files found.
CMakeLists.txt
View file @
1682b223
cmake_minimum_required
(
VERSION 3.5
)
if
(
"
${
CMAKE_SOURCE_DIR
}
"
STREQUAL
"
${
CMAKE_BINARY_DIR
}
"
)
message
(
FATAL_ERROR
"The binary and source directroy cannot be the same"
)
endif
()
project
(
migraphlib
)
find_package
(
ROCM REQUIRED
)
...
...
LICENSE
0 → 100644
View file @
1682b223
/*
The MIT License (MIT)
Copyright (c) 2015-Present Advanced Micro Devices, Inc. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
src/CMakeLists.txt
View file @
1682b223
...
...
@@ -11,6 +11,8 @@ add_library(migraph
program.cpp
shape.cpp
simplify_reshapes.cpp
opt/memory_coloring.cpp
opt/memory_coloring_impl.cpp
)
rocm_clang_tidy_check
(
migraph
)
target_include_directories
(
migraph PUBLIC $<BUILD_INTERFACE:
${
CMAKE_CURRENT_SOURCE_DIR
}
/include>
)
...
...
@@ -20,3 +22,10 @@ add_subdirectory(targets/cpu)
if
(
MIGRAPH_ENABLE_GPU
)
add_subdirectory
(
targets/gpu
)
endif
()
#install (TARGETS migraph
# LIBRARY DESTINATION /opt/rocm/lib)
#install (DIRECTORY include/migraph DESTINATION /opt/rocm/include)
src/eliminate_allocation.cpp
View file @
1682b223
...
...
@@ -4,12 +4,17 @@
#include <migraph/operators.hpp>
#include <migraph/iterator_for.hpp>
#include <migraph/ranges.hpp>
#include <migraph/stringutils.hpp>
#include <migraph/pass_config.hpp>
namespace
migraph
{
void
eliminate_allocation
::
apply
(
program
&
p
)
const
{
assert
(
alignment
>
0
);
if
(
!
enabled
(
MIGRAPH_DISABLE_MEMORY_COLORING
{}))
return
;
std
::
size_t
n
=
0
;
std
::
vector
<
std
::
pair
<
instruction_ref
,
std
::
size_t
>>
allocs
;
for
(
auto
ins
:
iterator_for
(
p
))
...
...
src/include/migraph/instruction.hpp
View file @
1682b223
...
...
@@ -71,13 +71,13 @@ struct instruction
// internal
void
replace_argument
(
instruction_ref
old
,
instruction_ref
new_ins
);
private:
operation
op
;
shape
result
;
std
::
vector
<
instruction_ref
>
output
;
std
::
vector
<
instruction_ref
>
arguments
;
literal
lit
;
};
}
// namespace migraph
namespace
std
{
...
...
src/include/migraph/memory_coloring.hpp
0 → 100644
View file @
1682b223
#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 @
1682b223
...
...
@@ -283,6 +283,151 @@ struct contiguous
}
};
struct
slice
{
std
::
vector
<
int64_t
>
axes
;
std
::
vector
<
int64_t
>
starts
;
std
::
vector
<
int64_t
>
ends
;
std
::
string
name
()
const
{
return
"slice"
;
}
auto
fix_index
(
const
std
::
vector
<
std
::
size_t
>&
lens
,
std
::
size_t
axis
,
int64_t
index
)
const
{
int64_t
r
=
std
::
min
(
index
,
static_cast
<
int64_t
>
(
lens
[
axis
]));
if
(
r
<
0
)
r
+=
lens
[
axis
];
return
std
::
size_t
(
r
);
}
auto
compute_offset
(
const
shape
&
s
)
const
{
const
std
::
vector
<
std
::
size_t
>&
lens
=
s
.
lens
();
const
std
::
vector
<
std
::
size_t
>&
strides
=
s
.
strides
();
auto
offset
=
0
;
if
(
!
axes
.
empty
())
{
for
(
std
::
size_t
i
=
0
;
i
<
axes
.
size
();
i
++
)
{
auto
axis
=
axes
[
i
];
offset
+=
fix_index
(
lens
,
axis
,
starts
[
i
])
*
strides
[
axis
];
}
}
else
{
for
(
std
::
size_t
axis
=
0
;
axis
<
lens
.
size
();
axis
++
)
{
offset
+=
fix_index
(
lens
,
axis
,
starts
[
axis
])
*
strides
[
axis
];
}
}
return
offset
;
}
shape
compute_shape
(
std
::
vector
<
shape
>
inputs
)
const
{
auto
input_shape
=
inputs
[
0
];
auto
t
=
input_shape
.
type
();
const
auto
&
old_lens
=
input_shape
.
lens
();
const
auto
&
old_strides
=
input_shape
.
strides
();
// std::vector<int64_t> t_axes(old_lens.size());
// if(axes.size() == 0)
// {
// std::iota(t_axes.begin(), t_axes.end(), 0);
// }
// else
// {
// std::copy(axes.begin(), axes.end(), t_axes.begin());
// }
if
(
starts
.
size
()
!=
axes
.
size
()
||
axes
.
size
()
!=
ends
.
size
())
{
MIGRAPH_THROW
(
"inconsistent sizes"
);
}
std
::
vector
<
std
::
size_t
>
new_lens
=
old_lens
;
for
(
std
::
size_t
i
=
0
;
i
<
axes
.
size
();
i
++
)
{
auto
axis
=
axes
[
i
];
new_lens
[
axis
]
=
fix_index
(
old_lens
,
axis
,
ends
[
i
])
-
fix_index
(
old_lens
,
axis
,
starts
[
i
]);
}
return
shape
{
t
,
new_lens
,
old_strides
};
}
argument
compute
(
context
&
,
shape
output_shape
,
std
::
vector
<
argument
>
args
)
const
{
auto
input
=
args
[
0
];
auto
offset
=
compute_offset
(
input
.
get_shape
())
*
output_shape
.
type_size
();
return
{
std
::
move
(
output_shape
),
[
=
]
{
return
input
.
data
()
+
offset
;
}};
}
};
struct
squeeze
{
std
::
vector
<
int64_t
>
axes
;
std
::
string
name
()
const
{
return
"squeeze"
;
}
shape
compute_shape
(
std
::
vector
<
shape
>
inputs
)
const
{
auto
input_shape
=
inputs
[
0
];
auto
type
=
input_shape
.
type
();
auto
old_lens
=
input_shape
.
lens
();
if
(
std
::
any_of
(
axes
.
begin
(),
axes
.
end
(),
[
&
](
auto
axis
)
{
return
input_shape
.
lens
()[
axis
]
!=
1
;
}))
{
MIGRAPH_THROW
(
"squeeze axis dimension should be equal to 1"
);
}
std
::
vector
<
std
::
size_t
>
new_lens
;
if
(
axes
.
empty
())
{
std
::
copy_if
(
old_lens
.
begin
(),
old_lens
.
end
(),
std
::
back_inserter
(
new_lens
),
[](
auto
len
)
{
return
len
!=
1
;
});
}
else
{
for
(
std
::
size_t
i
=
0
;
i
<
old_lens
.
size
();
i
++
)
{
if
(
std
::
find
(
axes
.
begin
(),
axes
.
end
(),
i
)
==
axes
.
end
())
{
new_lens
.
push_back
(
old_lens
[
i
]);
}
}
}
return
shape
{
type
,
new_lens
};
}
argument
compute
(
context
&
,
shape
output_shape
,
std
::
vector
<
argument
>
args
)
const
{
return
{
std
::
move
(
output_shape
),
std
::
move
(
args
.
front
().
data
)};
}
};
struct
unsqueeze
{
std
::
vector
<
int64_t
>
axes
;
std
::
string
name
()
const
{
return
"unsqueeze"
;
}
shape
compute_shape
(
std
::
vector
<
shape
>
inputs
)
const
{
auto
input_shape
=
inputs
[
0
];
auto
type
=
input_shape
.
type
();
auto
old_lens
=
input_shape
.
lens
();
std
::
size_t
new_size
=
old_lens
.
size
()
+
axes
.
size
();
std
::
vector
<
std
::
size_t
>
new_lens
(
new_size
);
std
::
size_t
p
=
0
;
for
(
std
::
size_t
i
=
0
;
i
<
new_size
;
i
++
)
{
if
(
std
::
find
(
axes
.
begin
(),
axes
.
end
(),
i
)
!=
axes
.
end
())
{
new_lens
[
i
]
=
1
;
}
else
{
new_lens
[
i
]
=
old_lens
[
p
++
];
}
}
return
shape
{
type
,
new_lens
};
}
argument
compute
(
context
&
,
shape
output_shape
,
std
::
vector
<
argument
>
args
)
const
{
return
{
std
::
move
(
output_shape
),
std
::
move
(
args
.
front
().
data
)};
}
};
struct
reshape
{
std
::
vector
<
int64_t
>
dims
;
...
...
src/include/migraph/pass_config.hpp
0 → 100644
View file @
1682b223
#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 @
1682b223
...
...
@@ -75,6 +75,8 @@ struct program
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
;
argument
eval
(
parameter_map
params
)
const
;
...
...
@@ -100,7 +102,6 @@ struct program
private:
std
::
unique_ptr
<
program_impl
>
impl
;
};
}
// namespace migraph
#endif
src/include/migraph/shape.hpp
View file @
1682b223
...
...
@@ -63,6 +63,7 @@ struct shape
const
std
::
vector
<
std
::
size_t
>&
strides
()
const
;
std
::
size_t
elements
()
const
;
std
::
size_t
bytes
()
const
;
std
::
size_t
type_size
()
const
;
/// Map multiple indices to space index
std
::
size_t
index
(
std
::
initializer_list
<
std
::
size_t
>
l
)
const
;
...
...
src/opt/common_header.hpp
0 → 100644
View file @
1682b223
#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 @
1682b223
#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 @
1682b223
#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
,
op
::
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 @
1682b223
#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 @
1682b223
...
...
@@ -203,6 +203,25 @@ shape program::get_parameter_shape(std::string name) const
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
>
result
;
...
...
@@ -437,5 +456,4 @@ std::ostream& operator<<(std::ostream& os, const program& p)
print_program
(
os
,
p
,
[](
auto
&&
...)
{});
return
os
;
}
}
// namespace migraph
src/shape.cpp
View file @
1682b223
...
...
@@ -98,6 +98,12 @@ std::size_t shape::bytes() const
this
->
visit_type
([
&
](
auto
as
)
{
n
=
as
.
size
();
});
return
n
*
this
->
element_space
();
}
std
::
size_t
shape
::
type_size
()
const
{
std
::
size_t
n
=
0
;
this
->
visit_type
([
&
](
auto
as
)
{
n
=
as
.
size
();
});
return
n
;
}
std
::
size_t
shape
::
index
(
std
::
initializer_list
<
std
::
size_t
>
l
)
const
{
assert
(
l
.
size
()
<=
this
->
lens
().
size
());
...
...
src/targets/cpu/CMakeLists.txt
View file @
1682b223
...
...
@@ -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 PRIVATE
${
BLAZE_INCLUDE
}
)
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 @
1682b223
...
...
@@ -32,3 +32,12 @@ add_library(migraph_gpu
rocm_clang_tidy_check
(
migraph_gpu
)
target_link_libraries
(
migraph_gpu migraph MIOpen migraph_device roc::rocblas
)
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 @
1682b223
...
...
@@ -6,12 +6,16 @@
#include <migraph/iterator_for.hpp>
#include <migraph/ranges.hpp>
#include <migraph/stringutils.hpp>
#include <migraph/pass_config.hpp>
namespace
migraph
{
namespace
gpu
{
void
eliminate_workspace
::
apply
(
program
&
p
)
const
{
if
(
!
enabled
(
MIGRAPH_DISABLE_MEMORY_COLORING
{}))
return
;
std
::
size_t
n
=
0
;
std
::
vector
<
instruction_ref
>
allocs
;
for
(
auto
ins
:
iterator_for
(
p
))
...
...
src/targets/gpu/hip.cpp
View file @
1682b223
...
...
@@ -90,6 +90,9 @@ argument from_gpu(argument arg)
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 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