Unverified Commit e9885eb3 authored by Paul Fultz II's avatar Paul Fultz II Committed by GitHub
Browse files

Merge branch 'develop' into jit-vector-reduce

parents 35579249 d436a723
...@@ -24,16 +24,16 @@ ...@@ -24,16 +24,16 @@
"import os.path\n", "import os.path\n",
"\n", "\n",
"if not os.path.exists(\"./utilities/coco.names\"):\n", "if not os.path.exists(\"./utilities/coco.names\"):\n",
" !wget https://github.com/onnx/models/raw/master/vision/object_detection_segmentation/yolov4/dependencies/coco.names -P ./utilities/\n", " !wget https://github.com/onnx/models/raw/main/vision/object_detection_segmentation/yolov4/dependencies/coco.names -P ./utilities/\n",
"if not os.path.exists(\"./utilities/yolov4_anchors.txt\"):\n", "if not os.path.exists(\"./utilities/yolov4_anchors.txt\"):\n",
" !wget https://github.com/onnx/models/raw/master/vision/object_detection_segmentation/yolov4/dependencies/yolov4_anchors.txt -P ./utilities/\n", " !wget https://github.com/onnx/models/raw/main/vision/object_detection_segmentation/yolov4/dependencies/yolov4_anchors.txt -P ./utilities/\n",
"if not os.path.exists(\"./utilities/input.jpg\"):\n", "if not os.path.exists(\"./utilities/input.jpg\"):\n",
" # The image used is from the COCO dataset (https://cocodataset.org/#explore)\n", " # The image used is from the COCO dataset (https://cocodataset.org/#explore)\n",
" # Other images can be tested by replacing the link below\n", " # Other images can be tested by replacing the link below\n",
" image_link = \"https://farm3.staticflickr.com/2009/2306189268_88cc86b30f_z.jpg\"\n", " image_link = \"https://farm3.staticflickr.com/2009/2306189268_88cc86b30f_z.jpg\"\n",
" !wget -O ./utilities/input.jpg $image_link\n", " !wget -O ./utilities/input.jpg $image_link\n",
"if not os.path.exists(\"./utilities/yolov4.onnx\"):\n", "if not os.path.exists(\"./utilities/yolov4.onnx\"):\n",
" !wget https://github.com/onnx/models/raw/master/vision/object_detection_segmentation/yolov4/model/yolov4.onnx -P ./utilities/" " !wget https://github.com/onnx/models/raw/main/vision/object_detection_segmentation/yolov4/model/yolov4.onnx -P ./utilities/"
] ]
}, },
{ {
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include <migraphx/matcher.hpp> #include <migraphx/matcher.hpp>
#include <migraphx/literal.hpp> #include <migraphx/literal.hpp>
#include <migraphx/functional.hpp> #include <migraphx/functional.hpp>
#include <migraphx/par_for.hpp>
#include <unordered_set> #include <unordered_set>
namespace migraphx { namespace migraphx {
...@@ -20,33 +21,42 @@ bool skip_propogate(instruction_ref ins) ...@@ -20,33 +21,42 @@ bool skip_propogate(instruction_ref ins)
return false; return false;
} }
bool is_const(instruction_ref ins) { return ins->can_eval() and not skip_propogate(ins); }
void propagate_constant::apply(module& m) const void propagate_constant::apply(module& m) const
{ {
std::unordered_set<instruction_ref> const_instrs;
auto last = std::prev(m.end());
// Find instructions that can be evaluated to a literal
for(auto i : iterator_for(m)) for(auto i : iterator_for(m))
{ {
if(i->name() != "@literal") if(is_const(i) and i != last)
continue; continue;
if(i->outputs().empty())
continue; std::copy_if(
fix([&](auto self, auto ins) { i->inputs().begin(),
std::unordered_set<instruction_ref> children(ins->outputs().begin(), i->inputs().end(),
ins->outputs().end()); std::inserter(const_instrs, const_instrs.begin()),
for(auto child : children) [&](const instruction_ref ins) { return is_const(ins) and ins->name() != "@literal"; });
{ }
if(child->name() == "@literal" or skip_propogate(child))
{ // Compute literals in parallel
self(child); std::vector<instruction_ref> const_instrs_vec{const_instrs.begin(), const_instrs.end()};
continue; std::vector<argument> literals(const_instrs_vec.size());
} par_for(const_instrs_vec.size(), 1, [&](const auto i) {
auto r = child->eval(); literals[i] = const_instrs_vec[i]->eval();
if(not r.empty()) });
{
assert(r.get_shape() == child->get_shape()); // Replace instructions in m
auto l = m.add_literal(r.get_shape(), r.data()); for(size_t i = 0; i < const_instrs_vec.size(); i++)
self(m.replace_instruction(child, l)); {
} if(not literals[i].empty())
} {
})(i); assert(literals[i].get_shape() == const_instrs_vec[i]->get_shape());
auto l = m.add_literal(literals[i].get_shape(), literals[i].data());
m.replace_instruction(const_instrs_vec[i], l);
}
} }
} }
......
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