slice.hpp 5.22 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2022 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.
 */
24
25
26
27
28
29
30
#ifndef MIGRAPHX_GUARD_OPERATORS_SLICE_HPP
#define MIGRAPHX_GUARD_OPERATORS_SLICE_HPP

#include <migraphx/check_shapes.hpp>
#include <migraphx/stringutils.hpp>
#include <migraphx/streamutils.hpp>
#include <migraphx/config.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
31
32
#include <migraphx/value.hpp>
#include <migraphx/op/normalize_attribute.hpp>
33
34
#include <cmath>
#include <utility>
Shucai Xiao's avatar
Shucai Xiao committed
35
#include <vector>
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace op {

struct slice
{
    std::vector<int64_t> axes;
    std::vector<int64_t> starts;
    std::vector<int64_t> ends;

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

Shucai Xiao's avatar
Shucai Xiao committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
    value attributes() const
    {
        value normalize     = value::object{};
        normalize["axes"]   = value::array{normalize_attribute::include_min};
        normalize["starts"] = value::array{normalize_attribute::clip_max,
                                           normalize_attribute::clip_min,
                                           normalize_attribute::include_max,
                                           normalize_attribute::use_len,
                                           normalize_attribute::include_min};
        normalize["ends"]   = value::array{normalize_attribute::clip_max,
                                         normalize_attribute::clip_min,
                                         normalize_attribute::include_max,
                                         normalize_attribute::use_len,
                                         normalize_attribute::include_min};
        return {{"normalize_axes", normalize}};
    }

70
71
72
73
74
75
76
77
78
79
80
81
    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
    {
82
        const std::vector<std::size_t>& lens    = s.lens();
83
84
        const std::vector<std::size_t>& strides = s.strides();
        auto offset                             = 0;
85
        if(!axes.empty())
86
        {
87
            for(std::size_t i = 0; i < axes.size(); i++)
88
            {
89
90
                auto axis = axes[i];
                offset += fix_index(lens, axis, starts[i]) * strides[axis];
91
92
93
94
95
96
            }
        }
        else
        {
            for(std::size_t axis = 0; axis < lens.size(); axis++)
            {
97
                offset += fix_index(lens, axis, starts[axis]) * strides[axis];
98
99
100
101
102
            }
        }
        return offset;
    }

Shucai Xiao's avatar
Shucai Xiao committed
103
    shape normalize_compute_shape(std::vector<shape> inputs) const
104
105
106
107
108
    {
        auto input_shape        = inputs[0];
        auto t                  = input_shape.type();
        const auto& old_lens    = input_shape.lens();
        const auto& old_strides = input_shape.strides();
Shucai Xiao's avatar
Shucai Xiao committed
109
110
111
112
113
114
115

        if(std::any_of(
               axes.begin(), axes.end(), [&](auto i) { return (i >= old_lens.size() and i < 0); }))
        {
            MIGRAPHX_THROW("SLICE: input axis " + to_string_range(axes) + " out of range");
        }

116
117
        if(starts.size() != axes.size() || axes.size() != ends.size())
        {
Shucai Xiao's avatar
Shucai Xiao committed
118
            MIGRAPHX_THROW("SLICE: inconsistent sizes");
119
        }
Shucai Xiao's avatar
Shucai Xiao committed
120

121
        std::vector<std::size_t> new_lens = old_lens;
Shucai Xiao's avatar
Shucai Xiao committed
122
        for(std::size_t i = 0; i < axes.size(); i++)
123
        {
Shucai Xiao's avatar
Shucai Xiao committed
124
125
126
            auto axis = axes[i];
            new_lens[axis] =
                fix_index(old_lens, axis, ends[i]) - fix_index(old_lens, axis, starts[i]);
127
128
129
        }
        return shape{t, new_lens, old_strides};
    }
Shucai Xiao's avatar
Shucai Xiao committed
130

131
132
133
134
135
136
    argument compute(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; }};
    }
Paul's avatar
Paul committed
137
    std::ptrdiff_t output_alias(const std::vector<shape>&) const { return 0; }
138
139
140
141
142
143
144
};

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

#endif