forked from microsoft/DirectML
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TensorUtil.h
64 lines (55 loc) · 1.68 KB
/
TensorUtil.h
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
//-----------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
#pragma once
namespace TensorUtil
{
template <size_t N>
uint32_t GetElementCount(TensorExtents<N> sizes)
{
uint32_t elementCount = 1;
for (size_t i = 0; i < N; ++i)
{
elementCount *= sizes[i];
}
return elementCount;
}
template <size_t N>
TensorExtents<N> GetPackedStrides(TensorExtents<N> sizes)
{
TensorExtents<N> strides;
strides[N - 1] = 1;
for (ptrdiff_t i = static_cast<ptrdiff_t>(N) - 2; i >= 0; --i)
{
strides[i] = strides[i + 1] * sizes[i + 1];
}
return strides;
}
template <size_t N>
uint32_t GetElementOffset(TensorExtents<N> indices, TensorExtents<N> strides)
{
uint32_t elementOffset = 0;
for (size_t i = 0; i < N; ++i)
{
elementOffset += indices[i] * strides[i];
}
return elementOffset;
}
template <size_t N>
TensorExtents<N> GetElementIndices(uint32_t elementIndex, TensorExtents<N> sizes)
{
TensorExtents<N> indices;
for (ptrdiff_t i = static_cast<ptrdiff_t>(N) - 1; i >= 0; --i)
{
uint32_t size = sizes[i];
indices[i] = elementIndex % size;
elementIndex /= size;
}
// The element should have been reduced to zero by all dimensions by now.
// If not, then the passed-in index is out of bounds.
assert(elementIndex == 0);
return indices;
}
}