Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,6 @@ docs/_toc.yml
# Ignore CMake user presets
CMakeUserPresets.json

# local Python virtual environment
.venv/
.cache/
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ add_library(migraphx
convert_to_json.cpp
cpp_generator.cpp
dead_code_elimination.cpp
dim_like.cpp
dom_info.cpp
dynamic_loader.cpp
eliminate_allocation.cpp
Expand Down
50 changes: 50 additions & 0 deletions src/dim_like.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2026 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.
*/

#include <migraphx/dim_like.hpp>
#include <migraphx/serialize.hpp>
#include <migraphx/value.hpp>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {

void migraphx_to_value(value& v, const dim_like& d)
{
v = visit([](const auto& x) { return migraphx::to_value(x); }, d);
}

void migraphx_from_value(const value& v, dim_like& d)
{
if(v.is_object())
{
shape::dynamic_dimension dd;
migraphx::from_value(v, dd);
d = std::move(dd);
return;
}
d = v.to<int64_t>();
}

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx
68 changes: 68 additions & 0 deletions src/include/migraphx/dim_like.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2026 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.
*/
#ifndef MIGRAPHX_GUARD_MIGRAPHLIB_DIM_LIKE_HPP
#define MIGRAPHX_GUARD_MIGRAPHLIB_DIM_LIKE_HPP

#include <cstdint>
#include <ostream>
#include <type_traits>

#include <migraphx/config.hpp>
#include <migraphx/picked_variant.hpp>
#include <migraphx/requires.hpp>
#include <migraphx/shape.hpp>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {

struct value;

// Routes any integral type through int64_t so call sites don't need casts.
struct dim_like_picker
{
template <class T, MIGRAPHX_REQUIRES(std::is_integral<T>{})>
static int64_t apply(T v)
{
return static_cast<int64_t>(v);
}

static shape::dynamic_dimension apply(shape::dynamic_dimension d) { return d; }
};

// A dim attribute entry that may be either a plain int64_t or a dynamic_dimension.
using dim_like = picked_variant<dim_like_picker, int64_t, shape::dynamic_dimension>;

inline std::ostream& operator<<(std::ostream& os, const dim_like& d)
{
visit([&](const auto& x) { os << x; }, d);
return os;
}

MIGRAPHX_EXPORT void migraphx_to_value(value& v, const dim_like& d);
MIGRAPHX_EXPORT void migraphx_from_value(const value& v, dim_like& d);

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx

#endif
33 changes: 21 additions & 12 deletions src/include/migraphx/op/reshape.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <migraphx/check_shapes.hpp>
#include <migraphx/argument.hpp>
#include <migraphx/config.hpp>
#include <migraphx/dim_like.hpp>
#include <migraphx/value.hpp>
#include <migraphx/dyn_output.hpp>
#include <migraphx/sat_ops.hpp>
Expand Down Expand Up @@ -57,7 +58,7 @@ namespace op {
*/
struct reshape
{
std::vector<int64_t> dims;
std::vector<dim_like> dims;

template <class Self, class F>
static auto reflect(Self& self, F f)
Expand All @@ -71,27 +72,27 @@ struct reshape
// Makes no checks for the validity of the `dims` attribute for the given input shape.
shape dyn_1arg_compute_shape(shape s0) const
{
auto input_dyn_dims = s0.dyn_dims();
const auto neg_dim_num =
std::distance(this->dims.begin(), std::find(this->dims.begin(), this->dims.end(), -1));
auto input_dyn_dims = s0.dyn_dims();
const auto neg_dim_num = std::distance(
this->dims.begin(), std::find(this->dims.begin(), this->dims.end(), dim_like{-1}));
const bool has_negative_dim_attr = neg_dim_num < dims.size();
// construct output dynamic shape from dims attribute
std::vector<shape::dynamic_dimension> output_dyn_dims(dims.size());
// NOTE: input_dyn_dims.size() may not equal dims.size()
for(std::size_t i = 0; i < dims.size(); ++i)
{
auto d = dims.at(i);
if(d == 0)
if(d == dim_like{0})
{
output_dyn_dims.at(i) = input_dyn_dims.at(i);
}
else if(d == -1)
else if(d == dim_like{-1})
{
output_dyn_dims.at(i) = {1, 1};
}
else
{
std::size_t u_dim = d;
std::size_t u_dim = std::get<int64_t>(d);
output_dyn_dims.at(i) = {u_dim, u_dim};
}
Comment thread
shivadbhavsar marked this conversation as resolved.
}
Expand Down Expand Up @@ -139,15 +140,18 @@ struct reshape
{
check_shapes{inputs, *this}.has(1);
auto&& idims = inputs.front().lens();
std::vector<std::size_t> rdims(dims.begin(), dims.end());
std::vector<std::size_t> rdims(dims.size());
std::transform(dims.begin(), dims.end(), rdims.begin(), [](const dim_like& d) {
return std::get<int64_t>(d);
});
Comment thread
shivadbhavsar marked this conversation as resolved.

for(std::size_t i = 0; i < dims.size(); i++)
{
if(dims[i] == 0)
if(dims[i] == dim_like{0})
rdims[i] = idims[i];

// convert -1 to 1 for rdims since rdims uses size_t (-1 is max_int for size_t)
if(dims[i] == -1)
if(dims[i] == dim_like{-1})
rdims[i] = 1;
}

Expand All @@ -158,7 +162,7 @@ struct reshape
std::accumulate(rdims.begin(), rdims.end(), 1, std::multiplies<int64_t>());
for(std::size_t i = 0; i < rdims.size(); i++)
{
if(dims[i] == -1)
if(dims[i] == dim_like{-1})
rdims[i] = missing_dim;
}
}
Expand All @@ -182,7 +186,12 @@ struct reshape
{
check_shapes{inputs, *this, true}.has(1, 2);

auto n_neg_dims = std::count(dims.begin(), dims.end(), -1);
if(std::any_of(dims.begin(), dims.end(), [](const auto& d) {
return std::holds_alternative<shape::dynamic_dimension>(d);
}))
MIGRAPHX_THROW("Reshape: dynamic_dimension dim entries are not currently supported");

auto n_neg_dims = std::count(dims.begin(), dims.end(), dim_like{-1});
if(n_neg_dims > 1)
MIGRAPHX_THROW("Reshape: Dimensions for reshape can only have one -1 dim");

Expand Down
29 changes: 20 additions & 9 deletions src/include/migraphx/op/reshape_lazy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <migraphx/check_shapes.hpp>
#include <migraphx/argument.hpp>
#include <migraphx/config.hpp>
#include <migraphx/dim_like.hpp>
#include <migraphx/value.hpp>
#include <migraphx/dyn_output.hpp>
#include <migraphx/reshape_dims.hpp>
Expand All @@ -37,7 +38,7 @@ namespace op {

struct reshape_lazy
{
std::vector<int64_t> dims;
std::vector<dim_like> dims;

template <class Self, class F>
static auto reflect(Self& self, F f)
Expand Down Expand Up @@ -65,12 +66,12 @@ struct reshape_lazy
{
if(dyn_dims[i].is_fixed())
{
num_dims_ele *= dims[i];
num_dims_ele *= std::get<int64_t>(dims[i]);
num_dd_ele *= dyn_dims[i].get_interval().min;
Comment thread
shivadbhavsar marked this conversation as resolved.
}
else
{
if(dims[i] != 0 and dims[i] != -1)
if(dims[i] != dim_like{0} and dims[i] != dim_like{-1})
{
MIGRAPHX_THROW(
"reshape_lazy: Non-fixed dynamic_dimension doesn't match with 0 or -1 "
Expand All @@ -89,9 +90,10 @@ struct reshape_lazy
dims.cend(),
dyn_dims.cbegin(),
output_dyn_dims.begin(),
[](std::size_t dim, auto dyn_dim) {
[](const dim_like& d, auto dyn_dim) {
if(not dyn_dim.is_fixed())
return dyn_dim;
std::size_t dim = std::get<int64_t>(d);
return shape::dynamic_dimension{dim, dim};
});
Comment thread
shivadbhavsar marked this conversation as resolved.
return {s0.type(), output_dyn_dims};
Expand All @@ -101,16 +103,19 @@ struct reshape_lazy
{
check_shapes{inputs, *this}.has(1);
auto&& idims = inputs.front().lens();
std::vector<std::size_t> rdims(dims.begin(), dims.end());
std::vector<std::size_t> rdims(dims.size());
std::transform(dims.begin(), dims.end(), rdims.begin(), [](const dim_like& d) {
return std::get<int64_t>(d);
});
Comment thread
shivadbhavsar marked this conversation as resolved.

for(std::size_t i = 0; i < dims.size(); i++)
{
if(dims[i] == 0)
if(dims[i] == dim_like{0})
rdims[i] = idims[i];

// since rdims using size_t type, -1 is the max value
// is size_t that cause later compuation incorrect
if(dims[i] == -1)
if(dims[i] == dim_like{-1})
rdims[i] = 1;
}

Expand All @@ -121,7 +126,7 @@ struct reshape_lazy
std::accumulate(rdims.begin(), rdims.end(), 1, std::multiplies<int64_t>());
for(std::size_t i = 0; i < rdims.size(); i++)
{
if(dims[i] == -1)
if(dims[i] == dim_like{-1})
rdims[i] = missing_dim;
}
}
Expand All @@ -143,7 +148,13 @@ struct reshape_lazy
shape compute_shape(std::vector<shape> inputs) const
{
check_shapes{inputs, *this, true}.has(1);
auto n_neg_dims = std::count(dims.begin(), dims.end(), -1);
if(std::any_of(dims.begin(), dims.end(), [](const auto& d) {
return std::holds_alternative<shape::dynamic_dimension>(d);
}))
MIGRAPHX_THROW(
"reshape_lazy: dynamic_dimension dim entries are not currently supported");

auto n_neg_dims = std::count(dims.begin(), dims.end(), dim_like{-1});
if(n_neg_dims > 1)
MIGRAPHX_THROW("reshape_lazy: Dimensions for reshape_lazy can only have one -1 dim");
const auto& s0 = inputs[0];
Expand Down
4 changes: 3 additions & 1 deletion src/include/migraphx/picked_variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ struct picked_variant : std::variant<Ts...>
using base_t = std::variant<Ts...>;
using base_t::base_t; // inherit default, in_place_type, in_place_index ctors

template <class T, MIGRAPHX_REQUIRES(not std::is_base_of<base_t, std::decay_t<T>>{})>
template <class T,
MIGRAPHX_REQUIRES(not std::is_base_of<base_t, std::decay_t<T>>{}),
class = decltype(Picker::apply(std::declval<T>()))>
constexpr picked_variant(T&& x) : base_t(Picker::apply(std::forward<T>(x)))
{
}
Expand Down
Loading
Loading