|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +"concatenate related operators" |
| 18 | +from typing import Optional |
| 19 | +import tvm |
| 20 | +from tvm import te |
| 21 | +import numpy as np |
| 22 | +from ..utils import get_const_int, const_vector |
| 23 | + |
| 24 | + |
| 25 | +def concatenate(data: tvm.te.Tensor, axis: Optional[int] = 0): |
| 26 | + """Join a sequence of arrays along an existing axis. Optimized for CPU exeution. |
| 27 | +
|
| 28 | + Parameters |
| 29 | + ---------- |
| 30 | + data : tuple of tvm.te.Tensor |
| 31 | + The arrays to concatenate |
| 32 | +
|
| 33 | + axis : int, optional |
| 34 | + The axis along which the arrays will be joined. Default is 0. |
| 35 | +
|
| 36 | + Returns |
| 37 | + ------- |
| 38 | + ret : tvm.te.Tensor |
| 39 | + """ |
| 40 | + |
| 41 | + def gen_ir_1d(data_bufs, in_outers_tensor, in_cumsum_tensor, out_buf): |
| 42 | + """Custom conactenation execution.""" |
| 43 | + i_b = tvm.tir.ir_builder.create() |
| 44 | + data_bufs1 = [i_b.buffer_ptr(data_buf) for data_buf in data_bufs] |
| 45 | + out_buf = i_b.buffer_ptr(out_buf) |
| 46 | + outers = i_b.buffer_ptr(in_outers_tensor) |
| 47 | + cumsum = i_b.buffer_ptr(in_cumsum_tensor) |
| 48 | + for i in range(len(data)): |
| 49 | + with i_b.for_range(0, outers[i], name="j") as j: |
| 50 | + out_buf[cumsum[i] + j] = data_bufs1[i][j] |
| 51 | + return i_b.get() |
| 52 | + |
| 53 | + def gen_ir(data_bufs, in_outers_tensor, in_cumsum_tensor, out_buf, inner, outer): |
| 54 | + """Common case of conactenation execution.""" |
| 55 | + i_b = tvm.tir.ir_builder.create() |
| 56 | + data_bufs1 = [i_b.buffer_ptr(data_buf) for data_buf in data_bufs] |
| 57 | + out_buf = i_b.buffer_ptr(out_buf) |
| 58 | + outers = i_b.buffer_ptr(in_outers_tensor) |
| 59 | + cumsum = i_b.buffer_ptr(in_cumsum_tensor) |
| 60 | + if inner > 1: |
| 61 | + with i_b.for_range(0, inner, name="inn", kind="parallel") as inn: |
| 62 | + pos = inn * outer |
| 63 | + for i in range(len(data)): |
| 64 | + offset = inn * outers[i] |
| 65 | + with i_b.for_range(0, outers[i], name="j") as j: |
| 66 | + out_buf[pos + cumsum[i] + j] = data_bufs1[i][offset + j] |
| 67 | + else: |
| 68 | + for i in range(len(data)): |
| 69 | + with i_b.for_range(0, outers[i], name="j", kind="parallel") as j: |
| 70 | + out_buf[cumsum[i] + j] = data_bufs1[i][j] |
| 71 | + return i_b.get() |
| 72 | + |
| 73 | + if axis < 0: |
| 74 | + axis += len(data[0].shape) |
| 75 | + concat_axis_sizes = [int(t.shape[axis]) for t in data] |
| 76 | + join_size = int(np.sum(concat_axis_sizes)) |
| 77 | + in_outers = [int(np.prod(i.shape[axis:])) for i in data] |
| 78 | + in_outers_cumsum = [0, *np.cumsum(in_outers, dtype="int64")[0:-1]] |
| 79 | + dtype = data[0].dtype |
| 80 | + out_shape = data[0].shape[:axis] + [join_size] + data[0].shape[axis + 1 :] |
| 81 | + in_outers_tensor = const_vector(in_outers) |
| 82 | + in_cumsum_tensor = const_vector(in_outers_cumsum, name="cumsum") |
| 83 | + right_val = np.prod(out_shape[axis:]) |
| 84 | + left_val = np.prod(out_shape[:axis]) |
| 85 | + |
| 86 | + if ( |
| 87 | + len(data[0].shape) == 1 |
| 88 | + or right_val == 1 |
| 89 | + or (left_val == 1 and axis == len(data[0].shape) - 1) |
| 90 | + or (left_val == 1 and right_val == 1) |
| 91 | + ): |
| 92 | + # badly parallelized case |
| 93 | + return te.extern( |
| 94 | + [out_shape], |
| 95 | + list(data) + [in_outers_tensor, in_cumsum_tensor], |
| 96 | + lambda ins, outs: gen_ir_1d(ins, ins[-2], ins[-1], outs[0]), |
| 97 | + dtype=dtype, |
| 98 | + name="concatenate_ext", |
| 99 | + ) |
| 100 | + |
| 101 | + inner = get_const_int(int(left_val)) |
| 102 | + outer = get_const_int(int(right_val)) |
| 103 | + return te.extern( |
| 104 | + [out_shape], |
| 105 | + list(data) + [in_outers_tensor, in_cumsum_tensor], |
| 106 | + lambda ins, outs: gen_ir(ins, ins[-2], ins[-1], outs[0], inner, outer), |
| 107 | + dtype=dtype, |
| 108 | + name="concatenate_ext", |
| 109 | + ) |
0 commit comments