reshape.hpp 2.55 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
#ifndef MIGRAPHX_GUARD_OPERATORS_RESHAPE_HPP
#define MIGRAPHX_GUARD_OPERATORS_RESHAPE_HPP

#include <array>
#include <migraphx/check_shapes.hpp>
#include <migraphx/stringutils.hpp>
#include <migraphx/streamutils.hpp>
#include <migraphx/literal.hpp>
#include <migraphx/shape_for_each.hpp>
#include <migraphx/config.hpp>
11
#include <migraphx/lifetime.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
12
#include <migraphx/value.hpp>
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <cmath>
#include <utility>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace op {

struct reshape
{
    std::vector<int64_t> dims;

    template <class Self, class F>
    static auto reflect(Self& self, F f)
    {
        return pack(f(self.dims, "dims"));
    }

Shucai Xiao's avatar
Shucai Xiao committed
30
31
    value attributes() const { return {{"require_std_shape", true}}; }

32
33
34
    std::string name() const { return "reshape"; }
    shape compute_shape(std::vector<shape> inputs) const
    {
35
        check_shapes{inputs, *this}.has(1).standard();
36
37
38
39
        auto&& idims = inputs.front().lens();
        std::vector<std::size_t> rdims(dims.begin(), dims.end());
        auto n_neg_dims = std::count(dims.begin(), dims.end(), -1);
        if(n_neg_dims > 1)
Shucai Xiao's avatar
Shucai Xiao committed
40
41
            MIGRAPHX_THROW("Reshape: Dimensions for reshape can only have one -1 dim");

42
43
44
45
46
47
48
49
50
51
        for(std::size_t i = 0; i < dims.size(); i++)
        {
            if(dims[i] == 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)
                rdims[i] = 1;
        }
Shucai Xiao's avatar
Shucai Xiao committed
52

53
54
55
56
57
58
59
60
61
62
63
64
65
66
        if(n_neg_dims > 0)
        {
            size_t missing_dim =
                inputs.front().elements() /
                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)
                    rdims[i] = missing_dim;
            }
        }

        shape s{inputs.front().type(), rdims};
        if(s.elements() != inputs.front().elements())
Shucai Xiao's avatar
Shucai Xiao committed
67
            MIGRAPHX_THROW("Reshape: Wrong number of elements for reshape: reshape has " +
Paul's avatar
Paul committed
68
69
                           std::to_string(s.elements()) + " elements whereas the input has " +
                           std::to_string(inputs.front().elements()));
70
71
        return s;
    }
Shucai Xiao's avatar
Shucai Xiao committed
72

73
74
    argument compute(shape output_shape, std::vector<argument> args) const
    {
Paul Fultz II's avatar
Paul Fultz II committed
75
        return args[0].reshape(output_shape);
76
    }
Shucai Xiao's avatar
Shucai Xiao committed
77

Paul's avatar
Paul committed
78
    std::ptrdiff_t output_alias(const std::vector<shape>&) const { return 0; }
79
80
81
82
83
84
85
};

} // namespace op
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx

#endif