File: TensorArithmetic.tt

package info (click to toggle)
onnxruntime 1.21.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 333,732 kB
  • sloc: cpp: 3,153,079; python: 179,219; ansic: 109,131; asm: 37,791; cs: 34,424; perl: 13,070; java: 11,047; javascript: 6,330; pascal: 4,126; sh: 3,277; xml: 598; objc: 281; makefile: 59
file content (249 lines) | stat: -rw-r--r-- 12,586 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ output extension=".cs" #>
<#@ include file="TensorTemplate.ttinclude" #>// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

// This file is copied and adapted from the following git repository -
// https://github.com/dotnet/corefx
// Commit ID: bdd0814360d4c3a58860919f292a306242f27da1
// Path: /src/System.Numerics.Tensors/tests/TensorArithmetic.cs
// Original license statement below -

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;

namespace Microsoft.ML.OnnxRuntime.Tensors
{
    internal interface ITensorArithmetic<T>
    {
        T One { get; }
        T Zero { get; }
<# foreach (MethodConfiguration method in methodConfiguration) { #>
        <#= method.GetResultMethodSignature("Tensor", "T")#>;
<# } #>
    }

    internal static class TensorArithmetic<T>
    {
        public static ITensorArithmetic<T> Instance => TensorArithmetic.GetArithmetic<T>();
    }

    internal static class TensorArithmetic
    { 
        public static ITensorArithmetic<T> GetArithmetic<T>()
        {
<# foreach (TypeConfiguration type in typeConfiguration) { #>
            <#=GenerateIfStatementHeader(type)#>
            {
                return (ITensorArithmetic<T>)new <#=type.ClassPrefix#>Arithmetic();
            }
<# } #>
            return null;
        }
    }
    
<# foreach (TypeConfiguration type in typeConfiguration) { #>
    internal class <#=type.ClassPrefix#>Arithmetic : ITensorArithmetic<<#=type.TypeName#>>
    {
        public <#=type.TypeName#> One => <#=type.OneLiteral#>;
        public <#=type.TypeName#> Zero => <#=type.ZeroLiteral#>;

<# foreach (MethodConfiguration method in methodConfiguration) { #>
        public <#= method.GetResultMethodSignature("Tensor", type.TypeName)#>
        {
<# if ((method.IsNumeric && !type.SupportsNumeric) ||  (method.IsBitwise && !type.SupportsBitwise) || (type.UnsupportedMethods.Contains(method.MethodName))) { #>
            throw new NotSupportedException();
<# } else if (method.Operator != null) { #>

            Span<int> indices = new Span<int>(new int[result.Rank]);
            for(int i = 0; i < <#= method.ResultName #>.Length; i++)
            {
                ArrayUtilities.GetIndices(result.strides, result.IsReversedStride, i, indices);
                <#=method.GetElementOperation(type.TypeName, "[indices]")#>;
            }
            
<# } else if (method.MethodName == "Contract") {#>
            var leftIndices = new int[left.Rank];
            var rightIndices = new int[right.Rank];
            var resultIndices = new int[result.Rank];

            var summingDimensions = new int[leftAxes.Length];
            for(int i = 0; i < leftAxes.Length; i++)
            {
                summingDimensions[i] = left.dimensions[leftAxes[i]];
            }

            var summingStrides = ArrayUtilities.GetStrides(summingDimensions);
            int summingLength = (int)ArrayUtilities.GetProduct(summingDimensions);

            var resultStrides = result.strides;

            // translates from result index to left non-summing dimensions' index portion
            // since left non-summing dimensions are given precedence in result, the end is zero-padded
            int[] leftNonSummingStrides = new int[result.Rank];

            // translates from summing index to left summing dimensions' index portion
            int[] leftSummingStrides = new int[leftAxes.Length];
            ArrayUtilities.SplitStrides(left.strides, leftAxes, leftNonSummingStrides, 0, leftSummingStrides, 0);

            // translates from result index to right non-summing dimensions' index portion
            int[] rightNonSummingStrides = new int[result.Rank];
            //  right non-summing dimensions appear after left non-summing dimensions.
            int rightNonSummingStridesOffset = (left.Rank - leftAxes.Length);

            // translates from summing index to right summing dimensions' index portion
            int[] rightSummingStrides = new int[rightAxes.Length];
            ArrayUtilities.SplitStrides(right.strides, rightAxes, rightNonSummingStrides, rightNonSummingStridesOffset, rightSummingStrides, 0);

            for (int resultIndex = 0; resultIndex < result.Length; resultIndex++)
            {
                <#=type.TypeName#> sum = (<#=type.TypeName#>)0;
                
                ArrayUtilities.GetIndices(result.strides, result.IsReversedStride, resultIndex, resultIndices);

                int leftIndexNonSumming = ArrayUtilities.TransformIndexByStrides(resultIndex, resultStrides, result.IsReversedStride, leftNonSummingStrides);
                int rightIndexNonSumming = ArrayUtilities.TransformIndexByStrides(resultIndex, resultStrides, result.IsReversedStride, rightNonSummingStrides);

                for (int summingIndex = 0; summingIndex < summingLength; summingIndex++)
                {
                    int leftIndexSumming = ArrayUtilities.TransformIndexByStrides(summingIndex, summingStrides, false, leftSummingStrides);
                    int rightIndexSumming = ArrayUtilities.TransformIndexByStrides(summingIndex, summingStrides, false, rightSummingStrides);

                    int leftIndex = leftIndexNonSumming + leftIndexSumming;
                    int rightIndex = rightIndexNonSumming + rightIndexSumming;

                    // todo, make this more efficient
                    ArrayUtilities.GetIndices(left.strides, left.IsReversedStride, leftIndex, leftIndices);
                    ArrayUtilities.GetIndices(right.strides, right.IsReversedStride, rightIndex, rightIndices);

                    sum += (<#=type.TypeName#>)(left[leftIndices] * right[rightIndices]);
                }
                
                result[resultIndices] = sum;
            }
<# } #>
        }
<# } #>

<# foreach (MethodConfiguration method in methodConfiguration) { #>
        public <#= method.GetResultMethodSignature("DenseTensor", type.TypeName)#>
        {
<# if ((method.IsNumeric && !type.SupportsNumeric) ||  (method.IsBitwise && !type.SupportsBitwise) || (type.UnsupportedMethods.Contains(method.MethodName))) { #>
            throw new NotSupportedException();
<# } else if (method.Operator != null) { #>

<# if (method.MethodType == MethodType.UnaryInPlace) { #>
            var <#=method.ResultName #>Span = <#=method.ResultName #>.Buffer.Span;
            var <#=method.Op1Name #>Span = <#=method.Op1Name #>.Buffer.Span;
            for(int i = 0; i < <#=method.ResultName #>Span.Length; i++)
            {
                <#=method.GetElementOperation(type.TypeName, "Span[i]")#>;
            }
<# } else {#>
            var <#=method.ResultName #>Span = <#=method.ResultName #>.Buffer.Span;
            var <#=method.Op1Name #>Span = <#=method.Op1Name #>.Buffer.Span;
<# if ((method.MethodType == MethodType.Binary) || (method.MethodType == MethodType.Comparison)) {#>
            var <#=method.Op2Name #>Span = <#=method.Op2Name #>.Buffer.Span;
<# } #>
            if  <#= method.GetLinearOperationCheck() #>
            {
                for(int i = 0; i < <#= method.ResultName #>Span.Length; i++)
                {
                    <#=method.GetElementOperation(type.TypeName, "Span[i]")#>;
                }
            }
            else
            {
                int rowMajorIndex = 0;
                int colMajorIndex = 0;
                
                ref int resultIndex = ref <#= method.ResultName #>.IsReversedStride ? ref colMajorIndex : ref rowMajorIndex;
                ref int op1Index = ref <#= method.Op1Name #>.IsReversedStride ? ref colMajorIndex : ref rowMajorIndex;
                
<# if ((method.MethodType == MethodType.Binary) || (method.MethodType == MethodType.Comparison)) {#>
                ref int op2Index = ref <#= method.Op2Name #>.IsReversedStride ? ref colMajorIndex : ref rowMajorIndex;

                var rowMajorStrides = !<#= method.ResultName #>.IsReversedStride ? <#= method.ResultName #>.strides :
                                      !<#= method.Op1Name #>.IsReversedStride ? <#= method.Op1Name #>.strides : 
                                      <#= method.Op2Name #>.strides;
                var columnMajorStrides = <#= method.ResultName #>.IsReversedStride ? <#= method.ResultName #>.strides :
                                         <#= method.Op1Name #>.IsReversedStride ? <#= method.Op1Name #>.strides : 
                                         <#= method.Op2Name #>.strides;
<# } else {#>
                var rowMajorStrides = !<#= method.ResultName #>.IsReversedStride ? <#= method.ResultName #>.strides :
                                      <#= method.Op1Name #>.strides;
                var columnMajorStrides = <#= method.ResultName #>.IsReversedStride ? <#= method.ResultName #>.strides :
                                         <#= method.Op1Name #>.strides;
<# } #>
                for(;rowMajorIndex < <#= method.ResultName #>Span.Length; rowMajorIndex++)
                {
                    colMajorIndex = ArrayUtilities.TransformIndexByStrides(rowMajorIndex, rowMajorStrides, false, columnMajorStrides);
                    
                    <#=method.GetElementOperation(type.TypeName, "Span[resultIndex]", "Span[op1Index]", "Span[op2Index]")#>;

                }
            }
<# } #>
<# } else if (method.MethodName == "Contract") {#>
            var summingDimensions = new int[leftAxes.Length];
            for(int i = 0; i < leftAxes.Length; i++)
            {
                summingDimensions[i] = left.dimensions[leftAxes[i]];
            }

            var summingStrides = ArrayUtilities.GetStrides(summingDimensions);
            int summingLength = (int)ArrayUtilities.GetProduct(summingDimensions);

            var resultStrides = result.strides;

            // translates from result index to left non-summing dimensions' index portion
            // since left non-summing dimensions are given precedence in result, the end is zero-padded
            int[] leftNonSummingStrides = new int[result.Rank];

            // translates from summing index to left summing dimensions' index portion
            int[] leftSummingStrides = new int[leftAxes.Length];
            ArrayUtilities.SplitStrides(left.strides, leftAxes, leftNonSummingStrides, 0, leftSummingStrides, 0);

            // translates from result index to right non-summing dimensions' index portion
            int[] rightNonSummingStrides = new int[result.Rank];
            //  right non-summing dimensions appear after left non-summing dimensions.
            int rightNonSummingStridesOffset = (left.Rank - leftAxes.Length);

            // translates from summing index to right summing dimensions' index portion
            int[] rightSummingStrides = new int[rightAxes.Length];
            ArrayUtilities.SplitStrides(right.strides, rightAxes, rightNonSummingStrides, rightNonSummingStridesOffset, rightSummingStrides, 0);
            
            var resultSpan = result.Buffer.Span;
            var leftSpan = left.Buffer.Span;
            var rightSpan = right.Buffer.Span;

            for (int resultIndex = 0; resultIndex < resultSpan.Length; resultIndex++)
            {
                <#=type.TypeName#> sum = (<#=type.TypeName#>)0;

                int leftIndexNonSumming = ArrayUtilities.TransformIndexByStrides(resultIndex, resultStrides, result.IsReversedStride, leftNonSummingStrides);
                int rightIndexNonSumming = ArrayUtilities.TransformIndexByStrides(resultIndex, resultStrides, result.IsReversedStride, rightNonSummingStrides);

                for (int summingIndex = 0; summingIndex < summingLength; summingIndex++)
                {
                    int leftIndexSumming = ArrayUtilities.TransformIndexByStrides(summingIndex, summingStrides, false, leftSummingStrides);
                    int rightIndexSumming = ArrayUtilities.TransformIndexByStrides(summingIndex, summingStrides, false, rightSummingStrides);

                    int leftIndex = leftIndexNonSumming + leftIndexSumming;
                    int rightIndex = rightIndexNonSumming + rightIndexSumming;

                    sum += (<#=type.TypeName#>)(leftSpan[leftIndex] * rightSpan[rightIndex]);
                }

                resultSpan[resultIndex] = sum;
            }
<# } #>
        }
<# } #>
    }
<# } #>
}