|
| 1 | +/** |
| 2 | + * Copyright (c) 2017-present, Facebook, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +#include "tc/core/cuda/cuda_compilation_cache.h" |
| 17 | + |
| 18 | +#include <version.h> |
| 19 | + |
| 20 | +#include <cstdint> |
| 21 | +#include <fstream> |
| 22 | +#include <numeric> |
| 23 | +#include <tuple> |
| 24 | + |
| 25 | +#include "tc/core/cuda/cuda_mapping_options.h" |
| 26 | +#include "tc/core/utils/math.h" |
| 27 | + |
| 28 | +namespace tc { |
| 29 | + |
| 30 | +namespace { |
| 31 | +uint64_t GetDLTensorAlignment(const DLTensor* t) { |
| 32 | + return (reinterpret_cast<std::uintptr_t>(t->data) + t->byte_offset) % 256; |
| 33 | +} |
| 34 | +} // namespace |
| 35 | + |
| 36 | +detail::TensorInfo::TensorInfo(const DLTensor* t) |
| 37 | + : alignment{GetDLTensorAlignment(t)}, dType(t->dtype) { |
| 38 | + shape.reserve(t->ndim); |
| 39 | + std::copy(t->shape, t->shape + t->ndim, std::back_inserter(shape)); |
| 40 | + if (not t->strides) { |
| 41 | + return; |
| 42 | + } |
| 43 | + strides.reserve(t->ndim); |
| 44 | + std::copy(t->strides, t->strides + t->ndim, std::back_inserter(strides)); |
| 45 | +} |
| 46 | + |
| 47 | +detail::TensorInfo::TensorInfo(const TensorInfoProto& buf) |
| 48 | + : shape{buf.shape().begin(), buf.shape().end()}, |
| 49 | + strides{buf.strides().begin(), buf.strides().end()}, |
| 50 | + alignment{buf.alignment()}, |
| 51 | + dType{static_cast<uint8_t>(buf.dtype().code()), |
| 52 | + static_cast<uint8_t>(buf.dtype().bits()), |
| 53 | + static_cast<uint16_t>(buf.dtype().lanes())} {} |
| 54 | + |
| 55 | +TensorInfoProto detail::TensorInfo::toProtobuf() const { |
| 56 | + TensorInfoProto buf; |
| 57 | + buf.mutable_shape()->Reserve(shape.size()); |
| 58 | + std::copy( |
| 59 | + shape.begin(), |
| 60 | + shape.end(), |
| 61 | + google::protobuf::RepeatedFieldBackInserter(buf.mutable_shape())); |
| 62 | + buf.mutable_strides()->Reserve(strides.size()); |
| 63 | + std::copy( |
| 64 | + strides.begin(), |
| 65 | + strides.end(), |
| 66 | + google::protobuf::RepeatedFieldBackInserter(buf.mutable_strides())); |
| 67 | + buf.set_alignment(alignment); |
| 68 | + buf.mutable_dtype()->set_code(dType.code); |
| 69 | + buf.mutable_dtype()->set_bits(dType.bits); |
| 70 | + buf.mutable_dtype()->set_lanes(dType.lanes); |
| 71 | + return buf; |
| 72 | +} |
| 73 | + |
| 74 | +bool detail::TensorInfo::operator==(const DLTensor* t) const { |
| 75 | + if (t->ndim != static_cast<int>(shape.size())) { |
| 76 | + return false; |
| 77 | + } |
| 78 | + |
| 79 | + auto res = std::mismatch(shape.begin(), shape.end(), t->shape); |
| 80 | + if (res.first != shape.end() || res.second != t->shape + t->ndim) { |
| 81 | + return false; |
| 82 | + } |
| 83 | + |
| 84 | + if (t->strides == nullptr) { |
| 85 | + if (strides.size() > 0) { |
| 86 | + return false; |
| 87 | + } |
| 88 | + } else { |
| 89 | + if (t->ndim != static_cast<int>(strides.size())) { |
| 90 | + return false; |
| 91 | + } |
| 92 | + |
| 93 | + res = std::mismatch(strides.begin(), strides.end(), t->strides); |
| 94 | + if (res.first != strides.end() || res.second != t->strides + t->ndim) { |
| 95 | + return false; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /*This should be enabled when/if tc starts using alignment information |
| 100 | + *if (GetDLTensorAlignment(t) != alignment) { |
| 101 | + * return false; |
| 102 | + *} |
| 103 | + */ |
| 104 | + return std::tie(t->dtype.code, t->dtype.bits, t->dtype.lanes) == |
| 105 | + std::tie(dType.code, dType.bits, dType.lanes); |
| 106 | +} |
| 107 | + |
| 108 | +bool operator==(const DLDataType& a, const DLDataType& b) { |
| 109 | + return a.code == b.code and a.bits == b.bits and a.lanes == b.lanes; |
| 110 | +} |
| 111 | + |
| 112 | +bool operator<(const DLDataType& a, const DLDataType& b) { |
| 113 | + return a.code < b.code and a.bits < b.bits and a.lanes < b.lanes; |
| 114 | +} |
| 115 | + |
| 116 | +bool detail::TensorInfo::operator==(const TensorInfo& t) const { |
| 117 | + return alignment == t.alignment and dType == t.dType and shape == t.shape and |
| 118 | + strides == t.strides; |
| 119 | +} |
| 120 | + |
| 121 | +bool detail::TensorInfo::operator<(const TensorInfo& t) const { |
| 122 | + return alignment < t.alignment and dType < t.dType and shape < t.shape and |
| 123 | + strides < t.strides; |
| 124 | +} |
| 125 | + |
| 126 | +bool operator==( |
| 127 | + const std::vector<const DLTensor*>& inputsTensor, |
| 128 | + const std::vector<detail::TensorInfo>& inputsInfo) { |
| 129 | + if (inputsTensor.size() != inputsInfo.size()) { |
| 130 | + return false; |
| 131 | + } |
| 132 | + CHECK(inputsTensor.size() == inputsInfo.size()); |
| 133 | + for (size_t i = 0, n = inputsInfo.size(); i < n; ++i) { |
| 134 | + if (!(inputsInfo[i] == inputsTensor[i])) { |
| 135 | + return false; |
| 136 | + } |
| 137 | + } |
| 138 | + return true; |
| 139 | +} |
| 140 | + |
| 141 | +} // namespace tc |
0 commit comments