Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/autotune_maps/conv_autotune_maps.h +60 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/autotune_maps/conv_parameters.h +137 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/autotune_maps/conv_parameters.pb.h +1829 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/ctc/ctc_beam_entry.h +154 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/ctc/ctc_beam_scorer.h +77 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/ctc/ctc_beam_search.h +437 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/ctc/ctc_decoder.h +121 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/ctc/ctc_loss_calculator.h +543 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/ctc/ctc_loss_util.h +55 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/proto/decode.h +711 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/proto/descriptor_pool_registry.h +76 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/proto/descriptors.h +55 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/proto/proto_utils.h +70 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/quantization/uniform_quant_ops_attr.pb.h +618 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/quantization/uniform_quant_ops_attr.proto +48 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/quantization/uniform_quant_ops_params.h +121 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/sparse/dim_comparator.h +121 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/sparse/group_iterator.h +153 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/sparse/sparse_tensor.h +672 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/tensor_bundle/byte_swap_array.h +30 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/tensor_bundle/byte_swap_tensor.h +53 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/tensor_bundle/naming.h +47 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/tensor_bundle/tensor_bundle.h +379 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/tensor_format.h +678 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/tensor_ops_util.h +128 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/tensor_slice_reader.h +215 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/tensor_slice_reader_cache.h +90 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/tensor_slice_set.h +94 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/tensor_slice_util.h +191 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/tensor_slice_writer.h +206 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/test_log.pb.h +75 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/test_log.proto +6 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/transform_output_iterator.h +149 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/use_cudnn.h +39 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/util.h +70 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/work_sharder.h +103 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/xla_config_registry.h +63 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/zen_util.h +52 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/include/external/ml_dtypes/_virtual_includes/float8/ml_dtypes/include/float8.h +1401 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/include/external/ml_dtypes/_virtual_includes/int4/ml_dtypes/include/int4.h +262 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/include/external/ml_dtypes/include/float8.h +1401 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/include/external/ml_dtypes/include/int4.h +262 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/lite/kernels/shim/op_kernel.h +277 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/lite/kernels/shim/shape.h +96 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/lite/kernels/shim/status_macros.h +72 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/lite/kernels/shim/tensor_view.h +256 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/lite/kernels/shim/tf_op_shim.h +164 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/lite/kernels/shim/tf_tensor_view.h +99 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/python/client/session_ref.h +91 -0
- miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/python/client/tf_session_helper.h +257 -0
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/autotune_maps/conv_autotune_maps.h
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
|
| 16 |
+
// For Google-internal use only.
|
| 17 |
+
//
|
| 18 |
+
// This file defines the map data structure for storing autotuning results for
|
| 19 |
+
// fused_conv2d_bias_activation_op_kernels.
|
| 20 |
+
//
|
| 21 |
+
// The key of the map uniquely identifies a convolution operation that runs on a
|
| 22 |
+
// particular device model while the value might be the autotuned algorithm we
|
| 23 |
+
// choose for the conv.
|
| 24 |
+
//
|
| 25 |
+
// This map will be merged after fused_conv2d_bias_activation_op_kernels is
|
| 26 |
+
// merged into conv_ops_fused_impl.h (b/177365158, b/189530096)
|
| 27 |
+
|
| 28 |
+
#ifndef TENSORFLOW_CORE_UTIL_AUTOTUNE_MAPS_CONV_AUTOTUNE_MAPS_H_
|
| 29 |
+
#define TENSORFLOW_CORE_UTIL_AUTOTUNE_MAPS_CONV_AUTOTUNE_MAPS_H_
|
| 30 |
+
|
| 31 |
+
#if GOOGLE_CUDA || TENSORFLOW_USE_ROCM
|
| 32 |
+
#include <string>
|
| 33 |
+
|
| 34 |
+
#include "tensorflow/core/kernels/gpu_utils.h"
|
| 35 |
+
#include "tensorflow/core/platform/stream_executor.h"
|
| 36 |
+
#include "tensorflow/core/util/autotune_maps/conv_parameters.h"
|
| 37 |
+
|
| 38 |
+
namespace tensorflow {
|
| 39 |
+
|
| 40 |
+
// A dummy type to group forward convolution autotune results together.
|
| 41 |
+
struct ConvAutotuneGroup {
|
| 42 |
+
static string name() { return "Conv"; }
|
| 43 |
+
};
|
| 44 |
+
|
| 45 |
+
using ConvAutotuneMap = AutotuneSingleton<ConvAutotuneGroup, ConvParameters,
|
| 46 |
+
AutotuneEntry<se::dnn::ConvOp>>;
|
| 47 |
+
|
| 48 |
+
// A dummy type to group fused convolution autotune results together.
|
| 49 |
+
struct ConvFusedAutotuneGroup {
|
| 50 |
+
static string name() { return "FusedConv"; }
|
| 51 |
+
};
|
| 52 |
+
|
| 53 |
+
using FusedConvAutotuneMap =
|
| 54 |
+
AutotuneSingleton<ConvAutotuneGroup, ConvParameters,
|
| 55 |
+
AutotuneEntry<se::dnn::FusedConvOp>>;
|
| 56 |
+
|
| 57 |
+
} // namespace tensorflow
|
| 58 |
+
#endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM
|
| 59 |
+
|
| 60 |
+
#endif // TENSORFLOW_CORE_UTIL_AUTOTUNE_MAPS_CONV_AUTOTUNE_MAPS_H_
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/autotune_maps/conv_parameters.h
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
|
| 16 |
+
#ifndef TENSORFLOW_CORE_UTIL_AUTOTUNE_MAPS_CONV_PARAMETERS_H_
|
| 17 |
+
#define TENSORFLOW_CORE_UTIL_AUTOTUNE_MAPS_CONV_PARAMETERS_H_
|
| 18 |
+
|
| 19 |
+
#if GOOGLE_CUDA || TENSORFLOW_USE_ROCM
|
| 20 |
+
#include "absl/types/optional.h"
|
| 21 |
+
#include "tensorflow/core/platform/stream_executor.h"
|
| 22 |
+
#include "tensorflow/core/util/autotune_maps/conv_parameters.pb.h"
|
| 23 |
+
|
| 24 |
+
namespace tensorflow {
|
| 25 |
+
// Uniquely identifies a convolution operation that runs on a particular device
|
| 26 |
+
// model.
|
| 27 |
+
//
|
| 28 |
+
// This can serve as a hashtable key, where the value might be the autotuned
|
| 29 |
+
// algorithm we choose for the conv.
|
| 30 |
+
//
|
| 31 |
+
// All of the data in this class other than the device_id is stored in the
|
| 32 |
+
// ConvParametersProto, so it can be easily serialized (for the purposes of
|
| 33 |
+
// ahead-of-time autotuning).
|
| 34 |
+
//
|
| 35 |
+
// When using the cudnn frontend API, two autotuning results for two different
|
| 36 |
+
// GPUs of the same model are not interchangeable, because an autotuning result
|
| 37 |
+
// includes a cudnn execution plan, which is tied to the GPU. As a result, we
|
| 38 |
+
// need to create separate ConvParameters objects for them.
|
| 39 |
+
class ConvParameters {
|
| 40 |
+
public:
|
| 41 |
+
struct FusionInfo {
|
| 42 |
+
// For some implementations (e.g. cuDNN new backend) these scales are part
|
| 43 |
+
// of the algorithm, not part of the parameters an algorithm take. They need
|
| 44 |
+
// to be used to distinguish different algorithms.
|
| 45 |
+
double conv_scale;
|
| 46 |
+
double side_input_scale;
|
| 47 |
+
double leakyrelu_alpha;
|
| 48 |
+
stream_executor::dnn::ActivationMode activation_mode;
|
| 49 |
+
bool is_contrib;
|
| 50 |
+
};
|
| 51 |
+
|
| 52 |
+
// LINT.IfChange(conv_parameters_version)
|
| 53 |
+
// A positive number that denotes the version of this class. Should be
|
| 54 |
+
// incremented everytime this class or ConvParametersProto are updated in a
|
| 55 |
+
// way that may invalidate autotune results.
|
| 56 |
+
static constexpr int kVersion = 3;
|
| 57 |
+
// LINT.ThenChange()
|
| 58 |
+
|
| 59 |
+
// We have three kinds of convolutions today. Vanilla unfused convolutions,
|
| 60 |
+
// fused convolutions, and fused convolutions as implemented in the `contrib`
|
| 61 |
+
// directory. The two fused convolutions ultimately correspond to the same
|
| 62 |
+
// cudnn calls, but have slightly different semantics (e.g. they interpret
|
| 63 |
+
// padding differently).
|
| 64 |
+
ConvParameters(
|
| 65 |
+
se::StreamExecutor* stream_exec, int64_t batch, int64_t in_depths,
|
| 66 |
+
absl::Span<const int64_t> in, int data_format, int64_t out_depths,
|
| 67 |
+
absl::Span<const int64_t> filter, absl::Span<const int64_t> dilation,
|
| 68 |
+
absl::Span<const int64_t> stride, absl::Span<const int64_t> padding,
|
| 69 |
+
DataType dtype, int group_count,
|
| 70 |
+
absl::optional<FusionInfo> fusion_info = absl::optional<FusionInfo>(),
|
| 71 |
+
// This argument should be set only for test use.
|
| 72 |
+
int version = kVersion);
|
| 73 |
+
|
| 74 |
+
ConvParameters(int device_id, const ConvParametersProto& proto);
|
| 75 |
+
|
| 76 |
+
ConvParameters(se::StreamExecutor* stream_exec,
|
| 77 |
+
const ConvParametersProto& proto)
|
| 78 |
+
: ConvParameters(stream_exec->device_ordinal(), proto) {}
|
| 79 |
+
|
| 80 |
+
bool operator==(const ConvParameters& other) const;
|
| 81 |
+
|
| 82 |
+
bool operator!=(const ConvParameters& other) const {
|
| 83 |
+
return !(*this == other);
|
| 84 |
+
}
|
| 85 |
+
uint64 hash() const { return hash_code_; }
|
| 86 |
+
|
| 87 |
+
string ToString() const;
|
| 88 |
+
|
| 89 |
+
const ConvParametersProto& proto() const { return proto_; }
|
| 90 |
+
|
| 91 |
+
private:
|
| 92 |
+
int device_id_;
|
| 93 |
+
ConvParametersProto proto_;
|
| 94 |
+
uint64 hash_code_;
|
| 95 |
+
};
|
| 96 |
+
|
| 97 |
+
class MatmulParameters {
|
| 98 |
+
public:
|
| 99 |
+
// LINT.IfChange(matmul_parameters_version)
|
| 100 |
+
// A positive number that denotes the version of this class. Should be
|
| 101 |
+
// incremented everytime this class or ConvParametersProto are updated in a
|
| 102 |
+
// way that may invalidate autotune results.
|
| 103 |
+
static constexpr int kVersion = 2;
|
| 104 |
+
// LINT.ThenChange()
|
| 105 |
+
|
| 106 |
+
MatmulParameters(se::StreamExecutor* stream_exec, DataType ab_dtype,
|
| 107 |
+
DataType c_dtype, bool trans_a, bool trans_b, uint64_t m,
|
| 108 |
+
uint64_t n, uint64_t k, int64_t lda, int64_t ldb,
|
| 109 |
+
int64_t ldc,
|
| 110 |
+
stream_executor::dnn::ActivationMode activation_mode,
|
| 111 |
+
// This argument should be set only for test use.
|
| 112 |
+
int version = kVersion);
|
| 113 |
+
|
| 114 |
+
MatmulParameters(se::StreamExecutor* stream_exec,
|
| 115 |
+
const MatmulParametersProto& proto);
|
| 116 |
+
|
| 117 |
+
bool operator==(const MatmulParameters& other) const;
|
| 118 |
+
|
| 119 |
+
bool operator!=(const MatmulParameters& other) const {
|
| 120 |
+
return !(*this == other);
|
| 121 |
+
}
|
| 122 |
+
uint64 hash() const { return hash_code_; }
|
| 123 |
+
|
| 124 |
+
string ToString() const;
|
| 125 |
+
|
| 126 |
+
const MatmulParametersProto& proto() const { return proto_; }
|
| 127 |
+
|
| 128 |
+
private:
|
| 129 |
+
int device_id_;
|
| 130 |
+
MatmulParametersProto proto_;
|
| 131 |
+
uint64 hash_code_;
|
| 132 |
+
};
|
| 133 |
+
|
| 134 |
+
} // namespace tensorflow
|
| 135 |
+
#endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM
|
| 136 |
+
|
| 137 |
+
#endif // TENSORFLOW_CORE_UTIL_AUTOTUNE_MAPS_CONV_PARAMETERS_H_
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/autotune_maps/conv_parameters.pb.h
ADDED
|
@@ -0,0 +1,1829 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Generated by the protocol buffer compiler. DO NOT EDIT!
|
| 2 |
+
// source: tensorflow/core/util/autotune_maps/conv_parameters.proto
|
| 3 |
+
|
| 4 |
+
#ifndef GOOGLE_PROTOBUF_INCLUDED_tensorflow_2fcore_2futil_2fautotune_5fmaps_2fconv_5fparameters_2eproto
|
| 5 |
+
#define GOOGLE_PROTOBUF_INCLUDED_tensorflow_2fcore_2futil_2fautotune_5fmaps_2fconv_5fparameters_2eproto
|
| 6 |
+
|
| 7 |
+
#include <limits>
|
| 8 |
+
#include <string>
|
| 9 |
+
|
| 10 |
+
#include <google/protobuf/port_def.inc>
|
| 11 |
+
#if PROTOBUF_VERSION < 3021000
|
| 12 |
+
#error This file was generated by a newer version of protoc which is
|
| 13 |
+
#error incompatible with your Protocol Buffer headers. Please update
|
| 14 |
+
#error your headers.
|
| 15 |
+
#endif
|
| 16 |
+
#if 3021009 < PROTOBUF_MIN_PROTOC_VERSION
|
| 17 |
+
#error This file was generated by an older version of protoc which is
|
| 18 |
+
#error incompatible with your Protocol Buffer headers. Please
|
| 19 |
+
#error regenerate this file with a newer version of protoc.
|
| 20 |
+
#endif
|
| 21 |
+
|
| 22 |
+
#include <google/protobuf/port_undef.inc>
|
| 23 |
+
#include <google/protobuf/io/coded_stream.h>
|
| 24 |
+
#include <google/protobuf/arena.h>
|
| 25 |
+
#include <google/protobuf/arenastring.h>
|
| 26 |
+
#include <google/protobuf/generated_message_util.h>
|
| 27 |
+
#include <google/protobuf/metadata_lite.h>
|
| 28 |
+
#include <google/protobuf/generated_message_reflection.h>
|
| 29 |
+
#include <google/protobuf/message.h>
|
| 30 |
+
#include <google/protobuf/repeated_field.h> // IWYU pragma: export
|
| 31 |
+
#include <google/protobuf/extension_set.h> // IWYU pragma: export
|
| 32 |
+
#include <google/protobuf/unknown_field_set.h>
|
| 33 |
+
#include "tensorflow/core/framework/types.pb.h"
|
| 34 |
+
#include "tsl/protobuf/dnn.pb.h"
|
| 35 |
+
// @@protoc_insertion_point(includes)
|
| 36 |
+
#include <google/protobuf/port_def.inc>
|
| 37 |
+
#define PROTOBUF_INTERNAL_EXPORT_tensorflow_2fcore_2futil_2fautotune_5fmaps_2fconv_5fparameters_2eproto
|
| 38 |
+
PROTOBUF_NAMESPACE_OPEN
|
| 39 |
+
namespace internal {
|
| 40 |
+
class AnyMetadata;
|
| 41 |
+
} // namespace internal
|
| 42 |
+
PROTOBUF_NAMESPACE_CLOSE
|
| 43 |
+
|
| 44 |
+
// Internal implementation detail -- do not use these members.
|
| 45 |
+
struct TableStruct_tensorflow_2fcore_2futil_2fautotune_5fmaps_2fconv_5fparameters_2eproto {
|
| 46 |
+
static const uint32_t offsets[];
|
| 47 |
+
};
|
| 48 |
+
extern const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_tensorflow_2fcore_2futil_2fautotune_5fmaps_2fconv_5fparameters_2eproto;
|
| 49 |
+
namespace tensorflow {
|
| 50 |
+
class ConvParametersProto;
|
| 51 |
+
struct ConvParametersProtoDefaultTypeInternal;
|
| 52 |
+
extern ConvParametersProtoDefaultTypeInternal _ConvParametersProto_default_instance_;
|
| 53 |
+
class ConvParametersProto_Fusion;
|
| 54 |
+
struct ConvParametersProto_FusionDefaultTypeInternal;
|
| 55 |
+
extern ConvParametersProto_FusionDefaultTypeInternal _ConvParametersProto_Fusion_default_instance_;
|
| 56 |
+
class MatmulParametersProto;
|
| 57 |
+
struct MatmulParametersProtoDefaultTypeInternal;
|
| 58 |
+
extern MatmulParametersProtoDefaultTypeInternal _MatmulParametersProto_default_instance_;
|
| 59 |
+
} // namespace tensorflow
|
| 60 |
+
PROTOBUF_NAMESPACE_OPEN
|
| 61 |
+
template<> ::tensorflow::ConvParametersProto* Arena::CreateMaybeMessage<::tensorflow::ConvParametersProto>(Arena*);
|
| 62 |
+
template<> ::tensorflow::ConvParametersProto_Fusion* Arena::CreateMaybeMessage<::tensorflow::ConvParametersProto_Fusion>(Arena*);
|
| 63 |
+
template<> ::tensorflow::MatmulParametersProto* Arena::CreateMaybeMessage<::tensorflow::MatmulParametersProto>(Arena*);
|
| 64 |
+
PROTOBUF_NAMESPACE_CLOSE
|
| 65 |
+
namespace tensorflow {
|
| 66 |
+
|
| 67 |
+
// ===================================================================
|
| 68 |
+
|
| 69 |
+
class ConvParametersProto_Fusion final :
|
| 70 |
+
public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:tensorflow.ConvParametersProto.Fusion) */ {
|
| 71 |
+
public:
|
| 72 |
+
inline ConvParametersProto_Fusion() : ConvParametersProto_Fusion(nullptr) {}
|
| 73 |
+
~ConvParametersProto_Fusion() override;
|
| 74 |
+
explicit PROTOBUF_CONSTEXPR ConvParametersProto_Fusion(::PROTOBUF_NAMESPACE_ID::internal::ConstantInitialized);
|
| 75 |
+
|
| 76 |
+
ConvParametersProto_Fusion(const ConvParametersProto_Fusion& from);
|
| 77 |
+
ConvParametersProto_Fusion(ConvParametersProto_Fusion&& from) noexcept
|
| 78 |
+
: ConvParametersProto_Fusion() {
|
| 79 |
+
*this = ::std::move(from);
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
inline ConvParametersProto_Fusion& operator=(const ConvParametersProto_Fusion& from) {
|
| 83 |
+
CopyFrom(from);
|
| 84 |
+
return *this;
|
| 85 |
+
}
|
| 86 |
+
inline ConvParametersProto_Fusion& operator=(ConvParametersProto_Fusion&& from) noexcept {
|
| 87 |
+
if (this == &from) return *this;
|
| 88 |
+
if (GetOwningArena() == from.GetOwningArena()
|
| 89 |
+
#ifdef PROTOBUF_FORCE_COPY_IN_MOVE
|
| 90 |
+
&& GetOwningArena() != nullptr
|
| 91 |
+
#endif // !PROTOBUF_FORCE_COPY_IN_MOVE
|
| 92 |
+
) {
|
| 93 |
+
InternalSwap(&from);
|
| 94 |
+
} else {
|
| 95 |
+
CopyFrom(from);
|
| 96 |
+
}
|
| 97 |
+
return *this;
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() {
|
| 101 |
+
return GetDescriptor();
|
| 102 |
+
}
|
| 103 |
+
static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() {
|
| 104 |
+
return default_instance().GetMetadata().descriptor;
|
| 105 |
+
}
|
| 106 |
+
static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() {
|
| 107 |
+
return default_instance().GetMetadata().reflection;
|
| 108 |
+
}
|
| 109 |
+
static const ConvParametersProto_Fusion& default_instance() {
|
| 110 |
+
return *internal_default_instance();
|
| 111 |
+
}
|
| 112 |
+
static inline const ConvParametersProto_Fusion* internal_default_instance() {
|
| 113 |
+
return reinterpret_cast<const ConvParametersProto_Fusion*>(
|
| 114 |
+
&_ConvParametersProto_Fusion_default_instance_);
|
| 115 |
+
}
|
| 116 |
+
static constexpr int kIndexInFileMessages =
|
| 117 |
+
0;
|
| 118 |
+
|
| 119 |
+
friend void swap(ConvParametersProto_Fusion& a, ConvParametersProto_Fusion& b) {
|
| 120 |
+
a.Swap(&b);
|
| 121 |
+
}
|
| 122 |
+
inline void Swap(ConvParametersProto_Fusion* other) {
|
| 123 |
+
if (other == this) return;
|
| 124 |
+
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
|
| 125 |
+
if (GetOwningArena() != nullptr &&
|
| 126 |
+
GetOwningArena() == other->GetOwningArena()) {
|
| 127 |
+
#else // PROTOBUF_FORCE_COPY_IN_SWAP
|
| 128 |
+
if (GetOwningArena() == other->GetOwningArena()) {
|
| 129 |
+
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
|
| 130 |
+
InternalSwap(other);
|
| 131 |
+
} else {
|
| 132 |
+
::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other);
|
| 133 |
+
}
|
| 134 |
+
}
|
| 135 |
+
void UnsafeArenaSwap(ConvParametersProto_Fusion* other) {
|
| 136 |
+
if (other == this) return;
|
| 137 |
+
GOOGLE_DCHECK(GetOwningArena() == other->GetOwningArena());
|
| 138 |
+
InternalSwap(other);
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
// implements Message ----------------------------------------------
|
| 142 |
+
|
| 143 |
+
ConvParametersProto_Fusion* New(::PROTOBUF_NAMESPACE_ID::Arena* arena = nullptr) const final {
|
| 144 |
+
return CreateMaybeMessage<ConvParametersProto_Fusion>(arena);
|
| 145 |
+
}
|
| 146 |
+
using ::PROTOBUF_NAMESPACE_ID::Message::CopyFrom;
|
| 147 |
+
void CopyFrom(const ConvParametersProto_Fusion& from);
|
| 148 |
+
using ::PROTOBUF_NAMESPACE_ID::Message::MergeFrom;
|
| 149 |
+
void MergeFrom( const ConvParametersProto_Fusion& from) {
|
| 150 |
+
ConvParametersProto_Fusion::MergeImpl(*this, from);
|
| 151 |
+
}
|
| 152 |
+
private:
|
| 153 |
+
static void MergeImpl(::PROTOBUF_NAMESPACE_ID::Message& to_msg, const ::PROTOBUF_NAMESPACE_ID::Message& from_msg);
|
| 154 |
+
public:
|
| 155 |
+
PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final;
|
| 156 |
+
bool IsInitialized() const final;
|
| 157 |
+
|
| 158 |
+
size_t ByteSizeLong() const final;
|
| 159 |
+
const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final;
|
| 160 |
+
uint8_t* _InternalSerialize(
|
| 161 |
+
uint8_t* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final;
|
| 162 |
+
int GetCachedSize() const final { return _impl_._cached_size_.Get(); }
|
| 163 |
+
|
| 164 |
+
private:
|
| 165 |
+
void SharedCtor(::PROTOBUF_NAMESPACE_ID::Arena* arena, bool is_message_owned);
|
| 166 |
+
void SharedDtor();
|
| 167 |
+
void SetCachedSize(int size) const final;
|
| 168 |
+
void InternalSwap(ConvParametersProto_Fusion* other);
|
| 169 |
+
|
| 170 |
+
private:
|
| 171 |
+
friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata;
|
| 172 |
+
static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() {
|
| 173 |
+
return "tensorflow.ConvParametersProto.Fusion";
|
| 174 |
+
}
|
| 175 |
+
protected:
|
| 176 |
+
explicit ConvParametersProto_Fusion(::PROTOBUF_NAMESPACE_ID::Arena* arena,
|
| 177 |
+
bool is_message_owned = false);
|
| 178 |
+
public:
|
| 179 |
+
|
| 180 |
+
static const ClassData _class_data_;
|
| 181 |
+
const ::PROTOBUF_NAMESPACE_ID::Message::ClassData*GetClassData() const final;
|
| 182 |
+
|
| 183 |
+
::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final;
|
| 184 |
+
|
| 185 |
+
// nested types ----------------------------------------------------
|
| 186 |
+
|
| 187 |
+
// accessors -------------------------------------------------------
|
| 188 |
+
|
| 189 |
+
enum : int {
|
| 190 |
+
kIsContribFieldNumber = 1,
|
| 191 |
+
kActivationModeFieldNumber = 2,
|
| 192 |
+
kConvScaleFieldNumber = 3,
|
| 193 |
+
kSideInputScaleFieldNumber = 4,
|
| 194 |
+
};
|
| 195 |
+
// bool is_contrib = 1;
|
| 196 |
+
void clear_is_contrib();
|
| 197 |
+
bool is_contrib() const;
|
| 198 |
+
void set_is_contrib(bool value);
|
| 199 |
+
private:
|
| 200 |
+
bool _internal_is_contrib() const;
|
| 201 |
+
void _internal_set_is_contrib(bool value);
|
| 202 |
+
public:
|
| 203 |
+
|
| 204 |
+
// .stream_executor.dnn.ActivationMode activation_mode = 2;
|
| 205 |
+
void clear_activation_mode();
|
| 206 |
+
::stream_executor::dnn::ActivationMode activation_mode() const;
|
| 207 |
+
void set_activation_mode(::stream_executor::dnn::ActivationMode value);
|
| 208 |
+
private:
|
| 209 |
+
::stream_executor::dnn::ActivationMode _internal_activation_mode() const;
|
| 210 |
+
void _internal_set_activation_mode(::stream_executor::dnn::ActivationMode value);
|
| 211 |
+
public:
|
| 212 |
+
|
| 213 |
+
// double conv_scale = 3;
|
| 214 |
+
void clear_conv_scale();
|
| 215 |
+
double conv_scale() const;
|
| 216 |
+
void set_conv_scale(double value);
|
| 217 |
+
private:
|
| 218 |
+
double _internal_conv_scale() const;
|
| 219 |
+
void _internal_set_conv_scale(double value);
|
| 220 |
+
public:
|
| 221 |
+
|
| 222 |
+
// double side_input_scale = 4;
|
| 223 |
+
void clear_side_input_scale();
|
| 224 |
+
double side_input_scale() const;
|
| 225 |
+
void set_side_input_scale(double value);
|
| 226 |
+
private:
|
| 227 |
+
double _internal_side_input_scale() const;
|
| 228 |
+
void _internal_set_side_input_scale(double value);
|
| 229 |
+
public:
|
| 230 |
+
|
| 231 |
+
// @@protoc_insertion_point(class_scope:tensorflow.ConvParametersProto.Fusion)
|
| 232 |
+
private:
|
| 233 |
+
class _Internal;
|
| 234 |
+
|
| 235 |
+
template <typename T> friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper;
|
| 236 |
+
typedef void InternalArenaConstructable_;
|
| 237 |
+
typedef void DestructorSkippable_;
|
| 238 |
+
struct Impl_ {
|
| 239 |
+
bool is_contrib_;
|
| 240 |
+
int activation_mode_;
|
| 241 |
+
double conv_scale_;
|
| 242 |
+
double side_input_scale_;
|
| 243 |
+
mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_;
|
| 244 |
+
};
|
| 245 |
+
union { Impl_ _impl_; };
|
| 246 |
+
friend struct ::TableStruct_tensorflow_2fcore_2futil_2fautotune_5fmaps_2fconv_5fparameters_2eproto;
|
| 247 |
+
};
|
| 248 |
+
// -------------------------------------------------------------------
|
| 249 |
+
|
| 250 |
+
class ConvParametersProto final :
|
| 251 |
+
public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:tensorflow.ConvParametersProto) */ {
|
| 252 |
+
public:
|
| 253 |
+
inline ConvParametersProto() : ConvParametersProto(nullptr) {}
|
| 254 |
+
~ConvParametersProto() override;
|
| 255 |
+
explicit PROTOBUF_CONSTEXPR ConvParametersProto(::PROTOBUF_NAMESPACE_ID::internal::ConstantInitialized);
|
| 256 |
+
|
| 257 |
+
ConvParametersProto(const ConvParametersProto& from);
|
| 258 |
+
ConvParametersProto(ConvParametersProto&& from) noexcept
|
| 259 |
+
: ConvParametersProto() {
|
| 260 |
+
*this = ::std::move(from);
|
| 261 |
+
}
|
| 262 |
+
|
| 263 |
+
inline ConvParametersProto& operator=(const ConvParametersProto& from) {
|
| 264 |
+
CopyFrom(from);
|
| 265 |
+
return *this;
|
| 266 |
+
}
|
| 267 |
+
inline ConvParametersProto& operator=(ConvParametersProto&& from) noexcept {
|
| 268 |
+
if (this == &from) return *this;
|
| 269 |
+
if (GetOwningArena() == from.GetOwningArena()
|
| 270 |
+
#ifdef PROTOBUF_FORCE_COPY_IN_MOVE
|
| 271 |
+
&& GetOwningArena() != nullptr
|
| 272 |
+
#endif // !PROTOBUF_FORCE_COPY_IN_MOVE
|
| 273 |
+
) {
|
| 274 |
+
InternalSwap(&from);
|
| 275 |
+
} else {
|
| 276 |
+
CopyFrom(from);
|
| 277 |
+
}
|
| 278 |
+
return *this;
|
| 279 |
+
}
|
| 280 |
+
|
| 281 |
+
static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() {
|
| 282 |
+
return GetDescriptor();
|
| 283 |
+
}
|
| 284 |
+
static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() {
|
| 285 |
+
return default_instance().GetMetadata().descriptor;
|
| 286 |
+
}
|
| 287 |
+
static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() {
|
| 288 |
+
return default_instance().GetMetadata().reflection;
|
| 289 |
+
}
|
| 290 |
+
static const ConvParametersProto& default_instance() {
|
| 291 |
+
return *internal_default_instance();
|
| 292 |
+
}
|
| 293 |
+
static inline const ConvParametersProto* internal_default_instance() {
|
| 294 |
+
return reinterpret_cast<const ConvParametersProto*>(
|
| 295 |
+
&_ConvParametersProto_default_instance_);
|
| 296 |
+
}
|
| 297 |
+
static constexpr int kIndexInFileMessages =
|
| 298 |
+
1;
|
| 299 |
+
|
| 300 |
+
friend void swap(ConvParametersProto& a, ConvParametersProto& b) {
|
| 301 |
+
a.Swap(&b);
|
| 302 |
+
}
|
| 303 |
+
inline void Swap(ConvParametersProto* other) {
|
| 304 |
+
if (other == this) return;
|
| 305 |
+
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
|
| 306 |
+
if (GetOwningArena() != nullptr &&
|
| 307 |
+
GetOwningArena() == other->GetOwningArena()) {
|
| 308 |
+
#else // PROTOBUF_FORCE_COPY_IN_SWAP
|
| 309 |
+
if (GetOwningArena() == other->GetOwningArena()) {
|
| 310 |
+
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
|
| 311 |
+
InternalSwap(other);
|
| 312 |
+
} else {
|
| 313 |
+
::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other);
|
| 314 |
+
}
|
| 315 |
+
}
|
| 316 |
+
void UnsafeArenaSwap(ConvParametersProto* other) {
|
| 317 |
+
if (other == this) return;
|
| 318 |
+
GOOGLE_DCHECK(GetOwningArena() == other->GetOwningArena());
|
| 319 |
+
InternalSwap(other);
|
| 320 |
+
}
|
| 321 |
+
|
| 322 |
+
// implements Message ----------------------------------------------
|
| 323 |
+
|
| 324 |
+
ConvParametersProto* New(::PROTOBUF_NAMESPACE_ID::Arena* arena = nullptr) const final {
|
| 325 |
+
return CreateMaybeMessage<ConvParametersProto>(arena);
|
| 326 |
+
}
|
| 327 |
+
using ::PROTOBUF_NAMESPACE_ID::Message::CopyFrom;
|
| 328 |
+
void CopyFrom(const ConvParametersProto& from);
|
| 329 |
+
using ::PROTOBUF_NAMESPACE_ID::Message::MergeFrom;
|
| 330 |
+
void MergeFrom( const ConvParametersProto& from) {
|
| 331 |
+
ConvParametersProto::MergeImpl(*this, from);
|
| 332 |
+
}
|
| 333 |
+
private:
|
| 334 |
+
static void MergeImpl(::PROTOBUF_NAMESPACE_ID::Message& to_msg, const ::PROTOBUF_NAMESPACE_ID::Message& from_msg);
|
| 335 |
+
public:
|
| 336 |
+
PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final;
|
| 337 |
+
bool IsInitialized() const final;
|
| 338 |
+
|
| 339 |
+
size_t ByteSizeLong() const final;
|
| 340 |
+
const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final;
|
| 341 |
+
uint8_t* _InternalSerialize(
|
| 342 |
+
uint8_t* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final;
|
| 343 |
+
int GetCachedSize() const final { return _impl_._cached_size_.Get(); }
|
| 344 |
+
|
| 345 |
+
private:
|
| 346 |
+
void SharedCtor(::PROTOBUF_NAMESPACE_ID::Arena* arena, bool is_message_owned);
|
| 347 |
+
void SharedDtor();
|
| 348 |
+
void SetCachedSize(int size) const final;
|
| 349 |
+
void InternalSwap(ConvParametersProto* other);
|
| 350 |
+
|
| 351 |
+
private:
|
| 352 |
+
friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata;
|
| 353 |
+
static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() {
|
| 354 |
+
return "tensorflow.ConvParametersProto";
|
| 355 |
+
}
|
| 356 |
+
protected:
|
| 357 |
+
explicit ConvParametersProto(::PROTOBUF_NAMESPACE_ID::Arena* arena,
|
| 358 |
+
bool is_message_owned = false);
|
| 359 |
+
public:
|
| 360 |
+
|
| 361 |
+
static const ClassData _class_data_;
|
| 362 |
+
const ::PROTOBUF_NAMESPACE_ID::Message::ClassData*GetClassData() const final;
|
| 363 |
+
|
| 364 |
+
::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final;
|
| 365 |
+
|
| 366 |
+
// nested types ----------------------------------------------------
|
| 367 |
+
|
| 368 |
+
typedef ConvParametersProto_Fusion Fusion;
|
| 369 |
+
|
| 370 |
+
// accessors -------------------------------------------------------
|
| 371 |
+
|
| 372 |
+
enum : int {
|
| 373 |
+
kInFieldNumber = 4,
|
| 374 |
+
kFilterFieldNumber = 6,
|
| 375 |
+
kDilationFieldNumber = 7,
|
| 376 |
+
kStrideFieldNumber = 8,
|
| 377 |
+
kPaddingFieldNumber = 9,
|
| 378 |
+
kDeviceIdentifierFieldNumber = 12,
|
| 379 |
+
kFusionFieldNumber = 13,
|
| 380 |
+
kBatchFieldNumber = 1,
|
| 381 |
+
kInDepthsFieldNumber = 2,
|
| 382 |
+
kOutDepthsFieldNumber = 3,
|
| 383 |
+
kDataFormatFieldNumber = 5,
|
| 384 |
+
kDtypeFieldNumber = 10,
|
| 385 |
+
kGroupCountFieldNumber = 11,
|
| 386 |
+
kVersionFieldNumber = 14,
|
| 387 |
+
};
|
| 388 |
+
// repeated int64 in = 4;
|
| 389 |
+
int in_size() const;
|
| 390 |
+
private:
|
| 391 |
+
int _internal_in_size() const;
|
| 392 |
+
public:
|
| 393 |
+
void clear_in();
|
| 394 |
+
private:
|
| 395 |
+
int64_t _internal_in(int index) const;
|
| 396 |
+
const ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >&
|
| 397 |
+
_internal_in() const;
|
| 398 |
+
void _internal_add_in(int64_t value);
|
| 399 |
+
::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >*
|
| 400 |
+
_internal_mutable_in();
|
| 401 |
+
public:
|
| 402 |
+
int64_t in(int index) const;
|
| 403 |
+
void set_in(int index, int64_t value);
|
| 404 |
+
void add_in(int64_t value);
|
| 405 |
+
const ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >&
|
| 406 |
+
in() const;
|
| 407 |
+
::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >*
|
| 408 |
+
mutable_in();
|
| 409 |
+
|
| 410 |
+
// repeated int64 filter = 6;
|
| 411 |
+
int filter_size() const;
|
| 412 |
+
private:
|
| 413 |
+
int _internal_filter_size() const;
|
| 414 |
+
public:
|
| 415 |
+
void clear_filter();
|
| 416 |
+
private:
|
| 417 |
+
int64_t _internal_filter(int index) const;
|
| 418 |
+
const ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >&
|
| 419 |
+
_internal_filter() const;
|
| 420 |
+
void _internal_add_filter(int64_t value);
|
| 421 |
+
::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >*
|
| 422 |
+
_internal_mutable_filter();
|
| 423 |
+
public:
|
| 424 |
+
int64_t filter(int index) const;
|
| 425 |
+
void set_filter(int index, int64_t value);
|
| 426 |
+
void add_filter(int64_t value);
|
| 427 |
+
const ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >&
|
| 428 |
+
filter() const;
|
| 429 |
+
::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >*
|
| 430 |
+
mutable_filter();
|
| 431 |
+
|
| 432 |
+
// repeated int64 dilation = 7;
|
| 433 |
+
int dilation_size() const;
|
| 434 |
+
private:
|
| 435 |
+
int _internal_dilation_size() const;
|
| 436 |
+
public:
|
| 437 |
+
void clear_dilation();
|
| 438 |
+
private:
|
| 439 |
+
int64_t _internal_dilation(int index) const;
|
| 440 |
+
const ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >&
|
| 441 |
+
_internal_dilation() const;
|
| 442 |
+
void _internal_add_dilation(int64_t value);
|
| 443 |
+
::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >*
|
| 444 |
+
_internal_mutable_dilation();
|
| 445 |
+
public:
|
| 446 |
+
int64_t dilation(int index) const;
|
| 447 |
+
void set_dilation(int index, int64_t value);
|
| 448 |
+
void add_dilation(int64_t value);
|
| 449 |
+
const ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >&
|
| 450 |
+
dilation() const;
|
| 451 |
+
::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >*
|
| 452 |
+
mutable_dilation();
|
| 453 |
+
|
| 454 |
+
// repeated int64 stride = 8;
|
| 455 |
+
int stride_size() const;
|
| 456 |
+
private:
|
| 457 |
+
int _internal_stride_size() const;
|
| 458 |
+
public:
|
| 459 |
+
void clear_stride();
|
| 460 |
+
private:
|
| 461 |
+
int64_t _internal_stride(int index) const;
|
| 462 |
+
const ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >&
|
| 463 |
+
_internal_stride() const;
|
| 464 |
+
void _internal_add_stride(int64_t value);
|
| 465 |
+
::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >*
|
| 466 |
+
_internal_mutable_stride();
|
| 467 |
+
public:
|
| 468 |
+
int64_t stride(int index) const;
|
| 469 |
+
void set_stride(int index, int64_t value);
|
| 470 |
+
void add_stride(int64_t value);
|
| 471 |
+
const ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >&
|
| 472 |
+
stride() const;
|
| 473 |
+
::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >*
|
| 474 |
+
mutable_stride();
|
| 475 |
+
|
| 476 |
+
// repeated int64 padding = 9;
|
| 477 |
+
int padding_size() const;
|
| 478 |
+
private:
|
| 479 |
+
int _internal_padding_size() const;
|
| 480 |
+
public:
|
| 481 |
+
void clear_padding();
|
| 482 |
+
private:
|
| 483 |
+
int64_t _internal_padding(int index) const;
|
| 484 |
+
const ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >&
|
| 485 |
+
_internal_padding() const;
|
| 486 |
+
void _internal_add_padding(int64_t value);
|
| 487 |
+
::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >*
|
| 488 |
+
_internal_mutable_padding();
|
| 489 |
+
public:
|
| 490 |
+
int64_t padding(int index) const;
|
| 491 |
+
void set_padding(int index, int64_t value);
|
| 492 |
+
void add_padding(int64_t value);
|
| 493 |
+
const ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >&
|
| 494 |
+
padding() const;
|
| 495 |
+
::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >*
|
| 496 |
+
mutable_padding();
|
| 497 |
+
|
| 498 |
+
// string device_identifier = 12;
|
| 499 |
+
void clear_device_identifier();
|
| 500 |
+
const std::string& device_identifier() const;
|
| 501 |
+
template <typename ArgT0 = const std::string&, typename... ArgT>
|
| 502 |
+
void set_device_identifier(ArgT0&& arg0, ArgT... args);
|
| 503 |
+
std::string* mutable_device_identifier();
|
| 504 |
+
PROTOBUF_NODISCARD std::string* release_device_identifier();
|
| 505 |
+
void set_allocated_device_identifier(std::string* device_identifier);
|
| 506 |
+
private:
|
| 507 |
+
const std::string& _internal_device_identifier() const;
|
| 508 |
+
inline PROTOBUF_ALWAYS_INLINE void _internal_set_device_identifier(const std::string& value);
|
| 509 |
+
std::string* _internal_mutable_device_identifier();
|
| 510 |
+
public:
|
| 511 |
+
|
| 512 |
+
// .tensorflow.ConvParametersProto.Fusion fusion = 13;
|
| 513 |
+
bool has_fusion() const;
|
| 514 |
+
private:
|
| 515 |
+
bool _internal_has_fusion() const;
|
| 516 |
+
public:
|
| 517 |
+
void clear_fusion();
|
| 518 |
+
const ::tensorflow::ConvParametersProto_Fusion& fusion() const;
|
| 519 |
+
PROTOBUF_NODISCARD ::tensorflow::ConvParametersProto_Fusion* release_fusion();
|
| 520 |
+
::tensorflow::ConvParametersProto_Fusion* mutable_fusion();
|
| 521 |
+
void set_allocated_fusion(::tensorflow::ConvParametersProto_Fusion* fusion);
|
| 522 |
+
private:
|
| 523 |
+
const ::tensorflow::ConvParametersProto_Fusion& _internal_fusion() const;
|
| 524 |
+
::tensorflow::ConvParametersProto_Fusion* _internal_mutable_fusion();
|
| 525 |
+
public:
|
| 526 |
+
void unsafe_arena_set_allocated_fusion(
|
| 527 |
+
::tensorflow::ConvParametersProto_Fusion* fusion);
|
| 528 |
+
::tensorflow::ConvParametersProto_Fusion* unsafe_arena_release_fusion();
|
| 529 |
+
|
| 530 |
+
// int64 batch = 1;
|
| 531 |
+
void clear_batch();
|
| 532 |
+
int64_t batch() const;
|
| 533 |
+
void set_batch(int64_t value);
|
| 534 |
+
private:
|
| 535 |
+
int64_t _internal_batch() const;
|
| 536 |
+
void _internal_set_batch(int64_t value);
|
| 537 |
+
public:
|
| 538 |
+
|
| 539 |
+
// int64 in_depths = 2;
|
| 540 |
+
void clear_in_depths();
|
| 541 |
+
int64_t in_depths() const;
|
| 542 |
+
void set_in_depths(int64_t value);
|
| 543 |
+
private:
|
| 544 |
+
int64_t _internal_in_depths() const;
|
| 545 |
+
void _internal_set_in_depths(int64_t value);
|
| 546 |
+
public:
|
| 547 |
+
|
| 548 |
+
// int64 out_depths = 3;
|
| 549 |
+
void clear_out_depths();
|
| 550 |
+
int64_t out_depths() const;
|
| 551 |
+
void set_out_depths(int64_t value);
|
| 552 |
+
private:
|
| 553 |
+
int64_t _internal_out_depths() const;
|
| 554 |
+
void _internal_set_out_depths(int64_t value);
|
| 555 |
+
public:
|
| 556 |
+
|
| 557 |
+
// int32 data_format = 5;
|
| 558 |
+
void clear_data_format();
|
| 559 |
+
int32_t data_format() const;
|
| 560 |
+
void set_data_format(int32_t value);
|
| 561 |
+
private:
|
| 562 |
+
int32_t _internal_data_format() const;
|
| 563 |
+
void _internal_set_data_format(int32_t value);
|
| 564 |
+
public:
|
| 565 |
+
|
| 566 |
+
// .tensorflow.DataType dtype = 10;
|
| 567 |
+
void clear_dtype();
|
| 568 |
+
::tensorflow::DataType dtype() const;
|
| 569 |
+
void set_dtype(::tensorflow::DataType value);
|
| 570 |
+
private:
|
| 571 |
+
::tensorflow::DataType _internal_dtype() const;
|
| 572 |
+
void _internal_set_dtype(::tensorflow::DataType value);
|
| 573 |
+
public:
|
| 574 |
+
|
| 575 |
+
// int32 group_count = 11;
|
| 576 |
+
void clear_group_count();
|
| 577 |
+
int32_t group_count() const;
|
| 578 |
+
void set_group_count(int32_t value);
|
| 579 |
+
private:
|
| 580 |
+
int32_t _internal_group_count() const;
|
| 581 |
+
void _internal_set_group_count(int32_t value);
|
| 582 |
+
public:
|
| 583 |
+
|
| 584 |
+
// int32 version = 14;
|
| 585 |
+
void clear_version();
|
| 586 |
+
int32_t version() const;
|
| 587 |
+
void set_version(int32_t value);
|
| 588 |
+
private:
|
| 589 |
+
int32_t _internal_version() const;
|
| 590 |
+
void _internal_set_version(int32_t value);
|
| 591 |
+
public:
|
| 592 |
+
|
| 593 |
+
// @@protoc_insertion_point(class_scope:tensorflow.ConvParametersProto)
|
| 594 |
+
private:
|
| 595 |
+
class _Internal;
|
| 596 |
+
|
| 597 |
+
template <typename T> friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper;
|
| 598 |
+
typedef void InternalArenaConstructable_;
|
| 599 |
+
typedef void DestructorSkippable_;
|
| 600 |
+
struct Impl_ {
|
| 601 |
+
::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t > in_;
|
| 602 |
+
mutable std::atomic<int> _in_cached_byte_size_;
|
| 603 |
+
::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t > filter_;
|
| 604 |
+
mutable std::atomic<int> _filter_cached_byte_size_;
|
| 605 |
+
::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t > dilation_;
|
| 606 |
+
mutable std::atomic<int> _dilation_cached_byte_size_;
|
| 607 |
+
::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t > stride_;
|
| 608 |
+
mutable std::atomic<int> _stride_cached_byte_size_;
|
| 609 |
+
::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t > padding_;
|
| 610 |
+
mutable std::atomic<int> _padding_cached_byte_size_;
|
| 611 |
+
::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr device_identifier_;
|
| 612 |
+
::tensorflow::ConvParametersProto_Fusion* fusion_;
|
| 613 |
+
int64_t batch_;
|
| 614 |
+
int64_t in_depths_;
|
| 615 |
+
int64_t out_depths_;
|
| 616 |
+
int32_t data_format_;
|
| 617 |
+
int dtype_;
|
| 618 |
+
int32_t group_count_;
|
| 619 |
+
int32_t version_;
|
| 620 |
+
mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_;
|
| 621 |
+
};
|
| 622 |
+
union { Impl_ _impl_; };
|
| 623 |
+
friend struct ::TableStruct_tensorflow_2fcore_2futil_2fautotune_5fmaps_2fconv_5fparameters_2eproto;
|
| 624 |
+
};
|
| 625 |
+
// -------------------------------------------------------------------
|
| 626 |
+
|
| 627 |
+
class MatmulParametersProto final :
|
| 628 |
+
public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:tensorflow.MatmulParametersProto) */ {
|
| 629 |
+
public:
|
| 630 |
+
inline MatmulParametersProto() : MatmulParametersProto(nullptr) {}
|
| 631 |
+
~MatmulParametersProto() override;
|
| 632 |
+
explicit PROTOBUF_CONSTEXPR MatmulParametersProto(::PROTOBUF_NAMESPACE_ID::internal::ConstantInitialized);
|
| 633 |
+
|
| 634 |
+
MatmulParametersProto(const MatmulParametersProto& from);
|
| 635 |
+
MatmulParametersProto(MatmulParametersProto&& from) noexcept
|
| 636 |
+
: MatmulParametersProto() {
|
| 637 |
+
*this = ::std::move(from);
|
| 638 |
+
}
|
| 639 |
+
|
| 640 |
+
inline MatmulParametersProto& operator=(const MatmulParametersProto& from) {
|
| 641 |
+
CopyFrom(from);
|
| 642 |
+
return *this;
|
| 643 |
+
}
|
| 644 |
+
inline MatmulParametersProto& operator=(MatmulParametersProto&& from) noexcept {
|
| 645 |
+
if (this == &from) return *this;
|
| 646 |
+
if (GetOwningArena() == from.GetOwningArena()
|
| 647 |
+
#ifdef PROTOBUF_FORCE_COPY_IN_MOVE
|
| 648 |
+
&& GetOwningArena() != nullptr
|
| 649 |
+
#endif // !PROTOBUF_FORCE_COPY_IN_MOVE
|
| 650 |
+
) {
|
| 651 |
+
InternalSwap(&from);
|
| 652 |
+
} else {
|
| 653 |
+
CopyFrom(from);
|
| 654 |
+
}
|
| 655 |
+
return *this;
|
| 656 |
+
}
|
| 657 |
+
|
| 658 |
+
static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() {
|
| 659 |
+
return GetDescriptor();
|
| 660 |
+
}
|
| 661 |
+
static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() {
|
| 662 |
+
return default_instance().GetMetadata().descriptor;
|
| 663 |
+
}
|
| 664 |
+
static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() {
|
| 665 |
+
return default_instance().GetMetadata().reflection;
|
| 666 |
+
}
|
| 667 |
+
static const MatmulParametersProto& default_instance() {
|
| 668 |
+
return *internal_default_instance();
|
| 669 |
+
}
|
| 670 |
+
static inline const MatmulParametersProto* internal_default_instance() {
|
| 671 |
+
return reinterpret_cast<const MatmulParametersProto*>(
|
| 672 |
+
&_MatmulParametersProto_default_instance_);
|
| 673 |
+
}
|
| 674 |
+
static constexpr int kIndexInFileMessages =
|
| 675 |
+
2;
|
| 676 |
+
|
| 677 |
+
friend void swap(MatmulParametersProto& a, MatmulParametersProto& b) {
|
| 678 |
+
a.Swap(&b);
|
| 679 |
+
}
|
| 680 |
+
inline void Swap(MatmulParametersProto* other) {
|
| 681 |
+
if (other == this) return;
|
| 682 |
+
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
|
| 683 |
+
if (GetOwningArena() != nullptr &&
|
| 684 |
+
GetOwningArena() == other->GetOwningArena()) {
|
| 685 |
+
#else // PROTOBUF_FORCE_COPY_IN_SWAP
|
| 686 |
+
if (GetOwningArena() == other->GetOwningArena()) {
|
| 687 |
+
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
|
| 688 |
+
InternalSwap(other);
|
| 689 |
+
} else {
|
| 690 |
+
::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other);
|
| 691 |
+
}
|
| 692 |
+
}
|
| 693 |
+
void UnsafeArenaSwap(MatmulParametersProto* other) {
|
| 694 |
+
if (other == this) return;
|
| 695 |
+
GOOGLE_DCHECK(GetOwningArena() == other->GetOwningArena());
|
| 696 |
+
InternalSwap(other);
|
| 697 |
+
}
|
| 698 |
+
|
| 699 |
+
// implements Message ----------------------------------------------
|
| 700 |
+
|
| 701 |
+
MatmulParametersProto* New(::PROTOBUF_NAMESPACE_ID::Arena* arena = nullptr) const final {
|
| 702 |
+
return CreateMaybeMessage<MatmulParametersProto>(arena);
|
| 703 |
+
}
|
| 704 |
+
using ::PROTOBUF_NAMESPACE_ID::Message::CopyFrom;
|
| 705 |
+
void CopyFrom(const MatmulParametersProto& from);
|
| 706 |
+
using ::PROTOBUF_NAMESPACE_ID::Message::MergeFrom;
|
| 707 |
+
void MergeFrom( const MatmulParametersProto& from) {
|
| 708 |
+
MatmulParametersProto::MergeImpl(*this, from);
|
| 709 |
+
}
|
| 710 |
+
private:
|
| 711 |
+
static void MergeImpl(::PROTOBUF_NAMESPACE_ID::Message& to_msg, const ::PROTOBUF_NAMESPACE_ID::Message& from_msg);
|
| 712 |
+
public:
|
| 713 |
+
PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final;
|
| 714 |
+
bool IsInitialized() const final;
|
| 715 |
+
|
| 716 |
+
size_t ByteSizeLong() const final;
|
| 717 |
+
const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final;
|
| 718 |
+
uint8_t* _InternalSerialize(
|
| 719 |
+
uint8_t* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final;
|
| 720 |
+
int GetCachedSize() const final { return _impl_._cached_size_.Get(); }
|
| 721 |
+
|
| 722 |
+
private:
|
| 723 |
+
void SharedCtor(::PROTOBUF_NAMESPACE_ID::Arena* arena, bool is_message_owned);
|
| 724 |
+
void SharedDtor();
|
| 725 |
+
void SetCachedSize(int size) const final;
|
| 726 |
+
void InternalSwap(MatmulParametersProto* other);
|
| 727 |
+
|
| 728 |
+
private:
|
| 729 |
+
friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata;
|
| 730 |
+
static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() {
|
| 731 |
+
return "tensorflow.MatmulParametersProto";
|
| 732 |
+
}
|
| 733 |
+
protected:
|
| 734 |
+
explicit MatmulParametersProto(::PROTOBUF_NAMESPACE_ID::Arena* arena,
|
| 735 |
+
bool is_message_owned = false);
|
| 736 |
+
public:
|
| 737 |
+
|
| 738 |
+
static const ClassData _class_data_;
|
| 739 |
+
const ::PROTOBUF_NAMESPACE_ID::Message::ClassData*GetClassData() const final;
|
| 740 |
+
|
| 741 |
+
::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final;
|
| 742 |
+
|
| 743 |
+
// nested types ----------------------------------------------------
|
| 744 |
+
|
| 745 |
+
// accessors -------------------------------------------------------
|
| 746 |
+
|
| 747 |
+
enum : int {
|
| 748 |
+
kDeviceIdentifierFieldNumber = 12,
|
| 749 |
+
kAbDtypeFieldNumber = 1,
|
| 750 |
+
kCDtypeFieldNumber = 2,
|
| 751 |
+
kMFieldNumber = 5,
|
| 752 |
+
kNFieldNumber = 6,
|
| 753 |
+
kTransAFieldNumber = 3,
|
| 754 |
+
kTransBFieldNumber = 4,
|
| 755 |
+
kActivationModeFieldNumber = 11,
|
| 756 |
+
kKFieldNumber = 7,
|
| 757 |
+
kLdaFieldNumber = 8,
|
| 758 |
+
kLdbFieldNumber = 9,
|
| 759 |
+
kLdcFieldNumber = 10,
|
| 760 |
+
kVersionFieldNumber = 14,
|
| 761 |
+
};
|
| 762 |
+
// string device_identifier = 12;
|
| 763 |
+
void clear_device_identifier();
|
| 764 |
+
const std::string& device_identifier() const;
|
| 765 |
+
template <typename ArgT0 = const std::string&, typename... ArgT>
|
| 766 |
+
void set_device_identifier(ArgT0&& arg0, ArgT... args);
|
| 767 |
+
std::string* mutable_device_identifier();
|
| 768 |
+
PROTOBUF_NODISCARD std::string* release_device_identifier();
|
| 769 |
+
void set_allocated_device_identifier(std::string* device_identifier);
|
| 770 |
+
private:
|
| 771 |
+
const std::string& _internal_device_identifier() const;
|
| 772 |
+
inline PROTOBUF_ALWAYS_INLINE void _internal_set_device_identifier(const std::string& value);
|
| 773 |
+
std::string* _internal_mutable_device_identifier();
|
| 774 |
+
public:
|
| 775 |
+
|
| 776 |
+
// .tensorflow.DataType ab_dtype = 1;
|
| 777 |
+
void clear_ab_dtype();
|
| 778 |
+
::tensorflow::DataType ab_dtype() const;
|
| 779 |
+
void set_ab_dtype(::tensorflow::DataType value);
|
| 780 |
+
private:
|
| 781 |
+
::tensorflow::DataType _internal_ab_dtype() const;
|
| 782 |
+
void _internal_set_ab_dtype(::tensorflow::DataType value);
|
| 783 |
+
public:
|
| 784 |
+
|
| 785 |
+
// .tensorflow.DataType c_dtype = 2;
|
| 786 |
+
void clear_c_dtype();
|
| 787 |
+
::tensorflow::DataType c_dtype() const;
|
| 788 |
+
void set_c_dtype(::tensorflow::DataType value);
|
| 789 |
+
private:
|
| 790 |
+
::tensorflow::DataType _internal_c_dtype() const;
|
| 791 |
+
void _internal_set_c_dtype(::tensorflow::DataType value);
|
| 792 |
+
public:
|
| 793 |
+
|
| 794 |
+
// uint64 m = 5;
|
| 795 |
+
void clear_m();
|
| 796 |
+
uint64_t m() const;
|
| 797 |
+
void set_m(uint64_t value);
|
| 798 |
+
private:
|
| 799 |
+
uint64_t _internal_m() const;
|
| 800 |
+
void _internal_set_m(uint64_t value);
|
| 801 |
+
public:
|
| 802 |
+
|
| 803 |
+
// uint64 n = 6;
|
| 804 |
+
void clear_n();
|
| 805 |
+
uint64_t n() const;
|
| 806 |
+
void set_n(uint64_t value);
|
| 807 |
+
private:
|
| 808 |
+
uint64_t _internal_n() const;
|
| 809 |
+
void _internal_set_n(uint64_t value);
|
| 810 |
+
public:
|
| 811 |
+
|
| 812 |
+
// bool trans_a = 3;
|
| 813 |
+
void clear_trans_a();
|
| 814 |
+
bool trans_a() const;
|
| 815 |
+
void set_trans_a(bool value);
|
| 816 |
+
private:
|
| 817 |
+
bool _internal_trans_a() const;
|
| 818 |
+
void _internal_set_trans_a(bool value);
|
| 819 |
+
public:
|
| 820 |
+
|
| 821 |
+
// bool trans_b = 4;
|
| 822 |
+
void clear_trans_b();
|
| 823 |
+
bool trans_b() const;
|
| 824 |
+
void set_trans_b(bool value);
|
| 825 |
+
private:
|
| 826 |
+
bool _internal_trans_b() const;
|
| 827 |
+
void _internal_set_trans_b(bool value);
|
| 828 |
+
public:
|
| 829 |
+
|
| 830 |
+
// .stream_executor.dnn.ActivationMode activation_mode = 11;
|
| 831 |
+
void clear_activation_mode();
|
| 832 |
+
::stream_executor::dnn::ActivationMode activation_mode() const;
|
| 833 |
+
void set_activation_mode(::stream_executor::dnn::ActivationMode value);
|
| 834 |
+
private:
|
| 835 |
+
::stream_executor::dnn::ActivationMode _internal_activation_mode() const;
|
| 836 |
+
void _internal_set_activation_mode(::stream_executor::dnn::ActivationMode value);
|
| 837 |
+
public:
|
| 838 |
+
|
| 839 |
+
// uint64 k = 7;
|
| 840 |
+
void clear_k();
|
| 841 |
+
uint64_t k() const;
|
| 842 |
+
void set_k(uint64_t value);
|
| 843 |
+
private:
|
| 844 |
+
uint64_t _internal_k() const;
|
| 845 |
+
void _internal_set_k(uint64_t value);
|
| 846 |
+
public:
|
| 847 |
+
|
| 848 |
+
// int64 lda = 8;
|
| 849 |
+
void clear_lda();
|
| 850 |
+
int64_t lda() const;
|
| 851 |
+
void set_lda(int64_t value);
|
| 852 |
+
private:
|
| 853 |
+
int64_t _internal_lda() const;
|
| 854 |
+
void _internal_set_lda(int64_t value);
|
| 855 |
+
public:
|
| 856 |
+
|
| 857 |
+
// int64 ldb = 9;
|
| 858 |
+
void clear_ldb();
|
| 859 |
+
int64_t ldb() const;
|
| 860 |
+
void set_ldb(int64_t value);
|
| 861 |
+
private:
|
| 862 |
+
int64_t _internal_ldb() const;
|
| 863 |
+
void _internal_set_ldb(int64_t value);
|
| 864 |
+
public:
|
| 865 |
+
|
| 866 |
+
// int64 ldc = 10;
|
| 867 |
+
void clear_ldc();
|
| 868 |
+
int64_t ldc() const;
|
| 869 |
+
void set_ldc(int64_t value);
|
| 870 |
+
private:
|
| 871 |
+
int64_t _internal_ldc() const;
|
| 872 |
+
void _internal_set_ldc(int64_t value);
|
| 873 |
+
public:
|
| 874 |
+
|
| 875 |
+
// int32 version = 14;
|
| 876 |
+
void clear_version();
|
| 877 |
+
int32_t version() const;
|
| 878 |
+
void set_version(int32_t value);
|
| 879 |
+
private:
|
| 880 |
+
int32_t _internal_version() const;
|
| 881 |
+
void _internal_set_version(int32_t value);
|
| 882 |
+
public:
|
| 883 |
+
|
| 884 |
+
// @@protoc_insertion_point(class_scope:tensorflow.MatmulParametersProto)
|
| 885 |
+
private:
|
| 886 |
+
class _Internal;
|
| 887 |
+
|
| 888 |
+
template <typename T> friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper;
|
| 889 |
+
typedef void InternalArenaConstructable_;
|
| 890 |
+
typedef void DestructorSkippable_;
|
| 891 |
+
struct Impl_ {
|
| 892 |
+
::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr device_identifier_;
|
| 893 |
+
int ab_dtype_;
|
| 894 |
+
int c_dtype_;
|
| 895 |
+
uint64_t m_;
|
| 896 |
+
uint64_t n_;
|
| 897 |
+
bool trans_a_;
|
| 898 |
+
bool trans_b_;
|
| 899 |
+
int activation_mode_;
|
| 900 |
+
uint64_t k_;
|
| 901 |
+
int64_t lda_;
|
| 902 |
+
int64_t ldb_;
|
| 903 |
+
int64_t ldc_;
|
| 904 |
+
int32_t version_;
|
| 905 |
+
mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_;
|
| 906 |
+
};
|
| 907 |
+
union { Impl_ _impl_; };
|
| 908 |
+
friend struct ::TableStruct_tensorflow_2fcore_2futil_2fautotune_5fmaps_2fconv_5fparameters_2eproto;
|
| 909 |
+
};
|
| 910 |
+
// ===================================================================
|
| 911 |
+
|
| 912 |
+
|
| 913 |
+
// ===================================================================
|
| 914 |
+
|
| 915 |
+
#ifdef __GNUC__
|
| 916 |
+
#pragma GCC diagnostic push
|
| 917 |
+
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
|
| 918 |
+
#endif // __GNUC__
|
| 919 |
+
// ConvParametersProto_Fusion
|
| 920 |
+
|
| 921 |
+
// bool is_contrib = 1;
|
| 922 |
+
inline void ConvParametersProto_Fusion::clear_is_contrib() {
|
| 923 |
+
_impl_.is_contrib_ = false;
|
| 924 |
+
}
|
| 925 |
+
inline bool ConvParametersProto_Fusion::_internal_is_contrib() const {
|
| 926 |
+
return _impl_.is_contrib_;
|
| 927 |
+
}
|
| 928 |
+
inline bool ConvParametersProto_Fusion::is_contrib() const {
|
| 929 |
+
// @@protoc_insertion_point(field_get:tensorflow.ConvParametersProto.Fusion.is_contrib)
|
| 930 |
+
return _internal_is_contrib();
|
| 931 |
+
}
|
| 932 |
+
inline void ConvParametersProto_Fusion::_internal_set_is_contrib(bool value) {
|
| 933 |
+
|
| 934 |
+
_impl_.is_contrib_ = value;
|
| 935 |
+
}
|
| 936 |
+
inline void ConvParametersProto_Fusion::set_is_contrib(bool value) {
|
| 937 |
+
_internal_set_is_contrib(value);
|
| 938 |
+
// @@protoc_insertion_point(field_set:tensorflow.ConvParametersProto.Fusion.is_contrib)
|
| 939 |
+
}
|
| 940 |
+
|
| 941 |
+
// .stream_executor.dnn.ActivationMode activation_mode = 2;
|
| 942 |
+
inline void ConvParametersProto_Fusion::clear_activation_mode() {
|
| 943 |
+
_impl_.activation_mode_ = 0;
|
| 944 |
+
}
|
| 945 |
+
inline ::stream_executor::dnn::ActivationMode ConvParametersProto_Fusion::_internal_activation_mode() const {
|
| 946 |
+
return static_cast< ::stream_executor::dnn::ActivationMode >(_impl_.activation_mode_);
|
| 947 |
+
}
|
| 948 |
+
inline ::stream_executor::dnn::ActivationMode ConvParametersProto_Fusion::activation_mode() const {
|
| 949 |
+
// @@protoc_insertion_point(field_get:tensorflow.ConvParametersProto.Fusion.activation_mode)
|
| 950 |
+
return _internal_activation_mode();
|
| 951 |
+
}
|
| 952 |
+
inline void ConvParametersProto_Fusion::_internal_set_activation_mode(::stream_executor::dnn::ActivationMode value) {
|
| 953 |
+
|
| 954 |
+
_impl_.activation_mode_ = value;
|
| 955 |
+
}
|
| 956 |
+
inline void ConvParametersProto_Fusion::set_activation_mode(::stream_executor::dnn::ActivationMode value) {
|
| 957 |
+
_internal_set_activation_mode(value);
|
| 958 |
+
// @@protoc_insertion_point(field_set:tensorflow.ConvParametersProto.Fusion.activation_mode)
|
| 959 |
+
}
|
| 960 |
+
|
| 961 |
+
// double conv_scale = 3;
|
| 962 |
+
inline void ConvParametersProto_Fusion::clear_conv_scale() {
|
| 963 |
+
_impl_.conv_scale_ = 0;
|
| 964 |
+
}
|
| 965 |
+
inline double ConvParametersProto_Fusion::_internal_conv_scale() const {
|
| 966 |
+
return _impl_.conv_scale_;
|
| 967 |
+
}
|
| 968 |
+
inline double ConvParametersProto_Fusion::conv_scale() const {
|
| 969 |
+
// @@protoc_insertion_point(field_get:tensorflow.ConvParametersProto.Fusion.conv_scale)
|
| 970 |
+
return _internal_conv_scale();
|
| 971 |
+
}
|
| 972 |
+
inline void ConvParametersProto_Fusion::_internal_set_conv_scale(double value) {
|
| 973 |
+
|
| 974 |
+
_impl_.conv_scale_ = value;
|
| 975 |
+
}
|
| 976 |
+
inline void ConvParametersProto_Fusion::set_conv_scale(double value) {
|
| 977 |
+
_internal_set_conv_scale(value);
|
| 978 |
+
// @@protoc_insertion_point(field_set:tensorflow.ConvParametersProto.Fusion.conv_scale)
|
| 979 |
+
}
|
| 980 |
+
|
| 981 |
+
// double side_input_scale = 4;
|
| 982 |
+
inline void ConvParametersProto_Fusion::clear_side_input_scale() {
|
| 983 |
+
_impl_.side_input_scale_ = 0;
|
| 984 |
+
}
|
| 985 |
+
inline double ConvParametersProto_Fusion::_internal_side_input_scale() const {
|
| 986 |
+
return _impl_.side_input_scale_;
|
| 987 |
+
}
|
| 988 |
+
inline double ConvParametersProto_Fusion::side_input_scale() const {
|
| 989 |
+
// @@protoc_insertion_point(field_get:tensorflow.ConvParametersProto.Fusion.side_input_scale)
|
| 990 |
+
return _internal_side_input_scale();
|
| 991 |
+
}
|
| 992 |
+
inline void ConvParametersProto_Fusion::_internal_set_side_input_scale(double value) {
|
| 993 |
+
|
| 994 |
+
_impl_.side_input_scale_ = value;
|
| 995 |
+
}
|
| 996 |
+
inline void ConvParametersProto_Fusion::set_side_input_scale(double value) {
|
| 997 |
+
_internal_set_side_input_scale(value);
|
| 998 |
+
// @@protoc_insertion_point(field_set:tensorflow.ConvParametersProto.Fusion.side_input_scale)
|
| 999 |
+
}
|
| 1000 |
+
|
| 1001 |
+
// -------------------------------------------------------------------
|
| 1002 |
+
|
| 1003 |
+
// ConvParametersProto
|
| 1004 |
+
|
| 1005 |
+
// int64 batch = 1;
|
| 1006 |
+
inline void ConvParametersProto::clear_batch() {
|
| 1007 |
+
_impl_.batch_ = int64_t{0};
|
| 1008 |
+
}
|
| 1009 |
+
inline int64_t ConvParametersProto::_internal_batch() const {
|
| 1010 |
+
return _impl_.batch_;
|
| 1011 |
+
}
|
| 1012 |
+
inline int64_t ConvParametersProto::batch() const {
|
| 1013 |
+
// @@protoc_insertion_point(field_get:tensorflow.ConvParametersProto.batch)
|
| 1014 |
+
return _internal_batch();
|
| 1015 |
+
}
|
| 1016 |
+
inline void ConvParametersProto::_internal_set_batch(int64_t value) {
|
| 1017 |
+
|
| 1018 |
+
_impl_.batch_ = value;
|
| 1019 |
+
}
|
| 1020 |
+
inline void ConvParametersProto::set_batch(int64_t value) {
|
| 1021 |
+
_internal_set_batch(value);
|
| 1022 |
+
// @@protoc_insertion_point(field_set:tensorflow.ConvParametersProto.batch)
|
| 1023 |
+
}
|
| 1024 |
+
|
| 1025 |
+
// int64 in_depths = 2;
|
| 1026 |
+
inline void ConvParametersProto::clear_in_depths() {
|
| 1027 |
+
_impl_.in_depths_ = int64_t{0};
|
| 1028 |
+
}
|
| 1029 |
+
inline int64_t ConvParametersProto::_internal_in_depths() const {
|
| 1030 |
+
return _impl_.in_depths_;
|
| 1031 |
+
}
|
| 1032 |
+
inline int64_t ConvParametersProto::in_depths() const {
|
| 1033 |
+
// @@protoc_insertion_point(field_get:tensorflow.ConvParametersProto.in_depths)
|
| 1034 |
+
return _internal_in_depths();
|
| 1035 |
+
}
|
| 1036 |
+
inline void ConvParametersProto::_internal_set_in_depths(int64_t value) {
|
| 1037 |
+
|
| 1038 |
+
_impl_.in_depths_ = value;
|
| 1039 |
+
}
|
| 1040 |
+
inline void ConvParametersProto::set_in_depths(int64_t value) {
|
| 1041 |
+
_internal_set_in_depths(value);
|
| 1042 |
+
// @@protoc_insertion_point(field_set:tensorflow.ConvParametersProto.in_depths)
|
| 1043 |
+
}
|
| 1044 |
+
|
| 1045 |
+
// int64 out_depths = 3;
|
| 1046 |
+
inline void ConvParametersProto::clear_out_depths() {
|
| 1047 |
+
_impl_.out_depths_ = int64_t{0};
|
| 1048 |
+
}
|
| 1049 |
+
inline int64_t ConvParametersProto::_internal_out_depths() const {
|
| 1050 |
+
return _impl_.out_depths_;
|
| 1051 |
+
}
|
| 1052 |
+
inline int64_t ConvParametersProto::out_depths() const {
|
| 1053 |
+
// @@protoc_insertion_point(field_get:tensorflow.ConvParametersProto.out_depths)
|
| 1054 |
+
return _internal_out_depths();
|
| 1055 |
+
}
|
| 1056 |
+
inline void ConvParametersProto::_internal_set_out_depths(int64_t value) {
|
| 1057 |
+
|
| 1058 |
+
_impl_.out_depths_ = value;
|
| 1059 |
+
}
|
| 1060 |
+
inline void ConvParametersProto::set_out_depths(int64_t value) {
|
| 1061 |
+
_internal_set_out_depths(value);
|
| 1062 |
+
// @@protoc_insertion_point(field_set:tensorflow.ConvParametersProto.out_depths)
|
| 1063 |
+
}
|
| 1064 |
+
|
| 1065 |
+
// repeated int64 in = 4;
|
| 1066 |
+
inline int ConvParametersProto::_internal_in_size() const {
|
| 1067 |
+
return _impl_.in_.size();
|
| 1068 |
+
}
|
| 1069 |
+
inline int ConvParametersProto::in_size() const {
|
| 1070 |
+
return _internal_in_size();
|
| 1071 |
+
}
|
| 1072 |
+
inline void ConvParametersProto::clear_in() {
|
| 1073 |
+
_impl_.in_.Clear();
|
| 1074 |
+
}
|
| 1075 |
+
inline int64_t ConvParametersProto::_internal_in(int index) const {
|
| 1076 |
+
return _impl_.in_.Get(index);
|
| 1077 |
+
}
|
| 1078 |
+
inline int64_t ConvParametersProto::in(int index) const {
|
| 1079 |
+
// @@protoc_insertion_point(field_get:tensorflow.ConvParametersProto.in)
|
| 1080 |
+
return _internal_in(index);
|
| 1081 |
+
}
|
| 1082 |
+
inline void ConvParametersProto::set_in(int index, int64_t value) {
|
| 1083 |
+
_impl_.in_.Set(index, value);
|
| 1084 |
+
// @@protoc_insertion_point(field_set:tensorflow.ConvParametersProto.in)
|
| 1085 |
+
}
|
| 1086 |
+
inline void ConvParametersProto::_internal_add_in(int64_t value) {
|
| 1087 |
+
_impl_.in_.Add(value);
|
| 1088 |
+
}
|
| 1089 |
+
inline void ConvParametersProto::add_in(int64_t value) {
|
| 1090 |
+
_internal_add_in(value);
|
| 1091 |
+
// @@protoc_insertion_point(field_add:tensorflow.ConvParametersProto.in)
|
| 1092 |
+
}
|
| 1093 |
+
inline const ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >&
|
| 1094 |
+
ConvParametersProto::_internal_in() const {
|
| 1095 |
+
return _impl_.in_;
|
| 1096 |
+
}
|
| 1097 |
+
inline const ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >&
|
| 1098 |
+
ConvParametersProto::in() const {
|
| 1099 |
+
// @@protoc_insertion_point(field_list:tensorflow.ConvParametersProto.in)
|
| 1100 |
+
return _internal_in();
|
| 1101 |
+
}
|
| 1102 |
+
inline ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >*
|
| 1103 |
+
ConvParametersProto::_internal_mutable_in() {
|
| 1104 |
+
return &_impl_.in_;
|
| 1105 |
+
}
|
| 1106 |
+
inline ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >*
|
| 1107 |
+
ConvParametersProto::mutable_in() {
|
| 1108 |
+
// @@protoc_insertion_point(field_mutable_list:tensorflow.ConvParametersProto.in)
|
| 1109 |
+
return _internal_mutable_in();
|
| 1110 |
+
}
|
| 1111 |
+
|
| 1112 |
+
// int32 data_format = 5;
|
| 1113 |
+
inline void ConvParametersProto::clear_data_format() {
|
| 1114 |
+
_impl_.data_format_ = 0;
|
| 1115 |
+
}
|
| 1116 |
+
inline int32_t ConvParametersProto::_internal_data_format() const {
|
| 1117 |
+
return _impl_.data_format_;
|
| 1118 |
+
}
|
| 1119 |
+
inline int32_t ConvParametersProto::data_format() const {
|
| 1120 |
+
// @@protoc_insertion_point(field_get:tensorflow.ConvParametersProto.data_format)
|
| 1121 |
+
return _internal_data_format();
|
| 1122 |
+
}
|
| 1123 |
+
inline void ConvParametersProto::_internal_set_data_format(int32_t value) {
|
| 1124 |
+
|
| 1125 |
+
_impl_.data_format_ = value;
|
| 1126 |
+
}
|
| 1127 |
+
inline void ConvParametersProto::set_data_format(int32_t value) {
|
| 1128 |
+
_internal_set_data_format(value);
|
| 1129 |
+
// @@protoc_insertion_point(field_set:tensorflow.ConvParametersProto.data_format)
|
| 1130 |
+
}
|
| 1131 |
+
|
| 1132 |
+
// repeated int64 filter = 6;
|
| 1133 |
+
inline int ConvParametersProto::_internal_filter_size() const {
|
| 1134 |
+
return _impl_.filter_.size();
|
| 1135 |
+
}
|
| 1136 |
+
inline int ConvParametersProto::filter_size() const {
|
| 1137 |
+
return _internal_filter_size();
|
| 1138 |
+
}
|
| 1139 |
+
inline void ConvParametersProto::clear_filter() {
|
| 1140 |
+
_impl_.filter_.Clear();
|
| 1141 |
+
}
|
| 1142 |
+
inline int64_t ConvParametersProto::_internal_filter(int index) const {
|
| 1143 |
+
return _impl_.filter_.Get(index);
|
| 1144 |
+
}
|
| 1145 |
+
inline int64_t ConvParametersProto::filter(int index) const {
|
| 1146 |
+
// @@protoc_insertion_point(field_get:tensorflow.ConvParametersProto.filter)
|
| 1147 |
+
return _internal_filter(index);
|
| 1148 |
+
}
|
| 1149 |
+
inline void ConvParametersProto::set_filter(int index, int64_t value) {
|
| 1150 |
+
_impl_.filter_.Set(index, value);
|
| 1151 |
+
// @@protoc_insertion_point(field_set:tensorflow.ConvParametersProto.filter)
|
| 1152 |
+
}
|
| 1153 |
+
inline void ConvParametersProto::_internal_add_filter(int64_t value) {
|
| 1154 |
+
_impl_.filter_.Add(value);
|
| 1155 |
+
}
|
| 1156 |
+
inline void ConvParametersProto::add_filter(int64_t value) {
|
| 1157 |
+
_internal_add_filter(value);
|
| 1158 |
+
// @@protoc_insertion_point(field_add:tensorflow.ConvParametersProto.filter)
|
| 1159 |
+
}
|
| 1160 |
+
inline const ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >&
|
| 1161 |
+
ConvParametersProto::_internal_filter() const {
|
| 1162 |
+
return _impl_.filter_;
|
| 1163 |
+
}
|
| 1164 |
+
inline const ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >&
|
| 1165 |
+
ConvParametersProto::filter() const {
|
| 1166 |
+
// @@protoc_insertion_point(field_list:tensorflow.ConvParametersProto.filter)
|
| 1167 |
+
return _internal_filter();
|
| 1168 |
+
}
|
| 1169 |
+
inline ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >*
|
| 1170 |
+
ConvParametersProto::_internal_mutable_filter() {
|
| 1171 |
+
return &_impl_.filter_;
|
| 1172 |
+
}
|
| 1173 |
+
inline ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >*
|
| 1174 |
+
ConvParametersProto::mutable_filter() {
|
| 1175 |
+
// @@protoc_insertion_point(field_mutable_list:tensorflow.ConvParametersProto.filter)
|
| 1176 |
+
return _internal_mutable_filter();
|
| 1177 |
+
}
|
| 1178 |
+
|
| 1179 |
+
// repeated int64 dilation = 7;
|
| 1180 |
+
inline int ConvParametersProto::_internal_dilation_size() const {
|
| 1181 |
+
return _impl_.dilation_.size();
|
| 1182 |
+
}
|
| 1183 |
+
inline int ConvParametersProto::dilation_size() const {
|
| 1184 |
+
return _internal_dilation_size();
|
| 1185 |
+
}
|
| 1186 |
+
inline void ConvParametersProto::clear_dilation() {
|
| 1187 |
+
_impl_.dilation_.Clear();
|
| 1188 |
+
}
|
| 1189 |
+
inline int64_t ConvParametersProto::_internal_dilation(int index) const {
|
| 1190 |
+
return _impl_.dilation_.Get(index);
|
| 1191 |
+
}
|
| 1192 |
+
inline int64_t ConvParametersProto::dilation(int index) const {
|
| 1193 |
+
// @@protoc_insertion_point(field_get:tensorflow.ConvParametersProto.dilation)
|
| 1194 |
+
return _internal_dilation(index);
|
| 1195 |
+
}
|
| 1196 |
+
inline void ConvParametersProto::set_dilation(int index, int64_t value) {
|
| 1197 |
+
_impl_.dilation_.Set(index, value);
|
| 1198 |
+
// @@protoc_insertion_point(field_set:tensorflow.ConvParametersProto.dilation)
|
| 1199 |
+
}
|
| 1200 |
+
inline void ConvParametersProto::_internal_add_dilation(int64_t value) {
|
| 1201 |
+
_impl_.dilation_.Add(value);
|
| 1202 |
+
}
|
| 1203 |
+
inline void ConvParametersProto::add_dilation(int64_t value) {
|
| 1204 |
+
_internal_add_dilation(value);
|
| 1205 |
+
// @@protoc_insertion_point(field_add:tensorflow.ConvParametersProto.dilation)
|
| 1206 |
+
}
|
| 1207 |
+
inline const ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >&
|
| 1208 |
+
ConvParametersProto::_internal_dilation() const {
|
| 1209 |
+
return _impl_.dilation_;
|
| 1210 |
+
}
|
| 1211 |
+
inline const ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >&
|
| 1212 |
+
ConvParametersProto::dilation() const {
|
| 1213 |
+
// @@protoc_insertion_point(field_list:tensorflow.ConvParametersProto.dilation)
|
| 1214 |
+
return _internal_dilation();
|
| 1215 |
+
}
|
| 1216 |
+
inline ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >*
|
| 1217 |
+
ConvParametersProto::_internal_mutable_dilation() {
|
| 1218 |
+
return &_impl_.dilation_;
|
| 1219 |
+
}
|
| 1220 |
+
inline ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >*
|
| 1221 |
+
ConvParametersProto::mutable_dilation() {
|
| 1222 |
+
// @@protoc_insertion_point(field_mutable_list:tensorflow.ConvParametersProto.dilation)
|
| 1223 |
+
return _internal_mutable_dilation();
|
| 1224 |
+
}
|
| 1225 |
+
|
| 1226 |
+
// repeated int64 stride = 8;
|
| 1227 |
+
inline int ConvParametersProto::_internal_stride_size() const {
|
| 1228 |
+
return _impl_.stride_.size();
|
| 1229 |
+
}
|
| 1230 |
+
inline int ConvParametersProto::stride_size() const {
|
| 1231 |
+
return _internal_stride_size();
|
| 1232 |
+
}
|
| 1233 |
+
inline void ConvParametersProto::clear_stride() {
|
| 1234 |
+
_impl_.stride_.Clear();
|
| 1235 |
+
}
|
| 1236 |
+
inline int64_t ConvParametersProto::_internal_stride(int index) const {
|
| 1237 |
+
return _impl_.stride_.Get(index);
|
| 1238 |
+
}
|
| 1239 |
+
inline int64_t ConvParametersProto::stride(int index) const {
|
| 1240 |
+
// @@protoc_insertion_point(field_get:tensorflow.ConvParametersProto.stride)
|
| 1241 |
+
return _internal_stride(index);
|
| 1242 |
+
}
|
| 1243 |
+
inline void ConvParametersProto::set_stride(int index, int64_t value) {
|
| 1244 |
+
_impl_.stride_.Set(index, value);
|
| 1245 |
+
// @@protoc_insertion_point(field_set:tensorflow.ConvParametersProto.stride)
|
| 1246 |
+
}
|
| 1247 |
+
inline void ConvParametersProto::_internal_add_stride(int64_t value) {
|
| 1248 |
+
_impl_.stride_.Add(value);
|
| 1249 |
+
}
|
| 1250 |
+
inline void ConvParametersProto::add_stride(int64_t value) {
|
| 1251 |
+
_internal_add_stride(value);
|
| 1252 |
+
// @@protoc_insertion_point(field_add:tensorflow.ConvParametersProto.stride)
|
| 1253 |
+
}
|
| 1254 |
+
inline const ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >&
|
| 1255 |
+
ConvParametersProto::_internal_stride() const {
|
| 1256 |
+
return _impl_.stride_;
|
| 1257 |
+
}
|
| 1258 |
+
inline const ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >&
|
| 1259 |
+
ConvParametersProto::stride() const {
|
| 1260 |
+
// @@protoc_insertion_point(field_list:tensorflow.ConvParametersProto.stride)
|
| 1261 |
+
return _internal_stride();
|
| 1262 |
+
}
|
| 1263 |
+
inline ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >*
|
| 1264 |
+
ConvParametersProto::_internal_mutable_stride() {
|
| 1265 |
+
return &_impl_.stride_;
|
| 1266 |
+
}
|
| 1267 |
+
inline ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >*
|
| 1268 |
+
ConvParametersProto::mutable_stride() {
|
| 1269 |
+
// @@protoc_insertion_point(field_mutable_list:tensorflow.ConvParametersProto.stride)
|
| 1270 |
+
return _internal_mutable_stride();
|
| 1271 |
+
}
|
| 1272 |
+
|
| 1273 |
+
// repeated int64 padding = 9;
|
| 1274 |
+
inline int ConvParametersProto::_internal_padding_size() const {
|
| 1275 |
+
return _impl_.padding_.size();
|
| 1276 |
+
}
|
| 1277 |
+
inline int ConvParametersProto::padding_size() const {
|
| 1278 |
+
return _internal_padding_size();
|
| 1279 |
+
}
|
| 1280 |
+
inline void ConvParametersProto::clear_padding() {
|
| 1281 |
+
_impl_.padding_.Clear();
|
| 1282 |
+
}
|
| 1283 |
+
inline int64_t ConvParametersProto::_internal_padding(int index) const {
|
| 1284 |
+
return _impl_.padding_.Get(index);
|
| 1285 |
+
}
|
| 1286 |
+
inline int64_t ConvParametersProto::padding(int index) const {
|
| 1287 |
+
// @@protoc_insertion_point(field_get:tensorflow.ConvParametersProto.padding)
|
| 1288 |
+
return _internal_padding(index);
|
| 1289 |
+
}
|
| 1290 |
+
inline void ConvParametersProto::set_padding(int index, int64_t value) {
|
| 1291 |
+
_impl_.padding_.Set(index, value);
|
| 1292 |
+
// @@protoc_insertion_point(field_set:tensorflow.ConvParametersProto.padding)
|
| 1293 |
+
}
|
| 1294 |
+
inline void ConvParametersProto::_internal_add_padding(int64_t value) {
|
| 1295 |
+
_impl_.padding_.Add(value);
|
| 1296 |
+
}
|
| 1297 |
+
inline void ConvParametersProto::add_padding(int64_t value) {
|
| 1298 |
+
_internal_add_padding(value);
|
| 1299 |
+
// @@protoc_insertion_point(field_add:tensorflow.ConvParametersProto.padding)
|
| 1300 |
+
}
|
| 1301 |
+
inline const ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >&
|
| 1302 |
+
ConvParametersProto::_internal_padding() const {
|
| 1303 |
+
return _impl_.padding_;
|
| 1304 |
+
}
|
| 1305 |
+
inline const ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >&
|
| 1306 |
+
ConvParametersProto::padding() const {
|
| 1307 |
+
// @@protoc_insertion_point(field_list:tensorflow.ConvParametersProto.padding)
|
| 1308 |
+
return _internal_padding();
|
| 1309 |
+
}
|
| 1310 |
+
inline ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >*
|
| 1311 |
+
ConvParametersProto::_internal_mutable_padding() {
|
| 1312 |
+
return &_impl_.padding_;
|
| 1313 |
+
}
|
| 1314 |
+
inline ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >*
|
| 1315 |
+
ConvParametersProto::mutable_padding() {
|
| 1316 |
+
// @@protoc_insertion_point(field_mutable_list:tensorflow.ConvParametersProto.padding)
|
| 1317 |
+
return _internal_mutable_padding();
|
| 1318 |
+
}
|
| 1319 |
+
|
| 1320 |
+
// .tensorflow.DataType dtype = 10;
|
| 1321 |
+
inline void ConvParametersProto::clear_dtype() {
|
| 1322 |
+
_impl_.dtype_ = 0;
|
| 1323 |
+
}
|
| 1324 |
+
inline ::tensorflow::DataType ConvParametersProto::_internal_dtype() const {
|
| 1325 |
+
return static_cast< ::tensorflow::DataType >(_impl_.dtype_);
|
| 1326 |
+
}
|
| 1327 |
+
inline ::tensorflow::DataType ConvParametersProto::dtype() const {
|
| 1328 |
+
// @@protoc_insertion_point(field_get:tensorflow.ConvParametersProto.dtype)
|
| 1329 |
+
return _internal_dtype();
|
| 1330 |
+
}
|
| 1331 |
+
inline void ConvParametersProto::_internal_set_dtype(::tensorflow::DataType value) {
|
| 1332 |
+
|
| 1333 |
+
_impl_.dtype_ = value;
|
| 1334 |
+
}
|
| 1335 |
+
inline void ConvParametersProto::set_dtype(::tensorflow::DataType value) {
|
| 1336 |
+
_internal_set_dtype(value);
|
| 1337 |
+
// @@protoc_insertion_point(field_set:tensorflow.ConvParametersProto.dtype)
|
| 1338 |
+
}
|
| 1339 |
+
|
| 1340 |
+
// int32 group_count = 11;
|
| 1341 |
+
inline void ConvParametersProto::clear_group_count() {
|
| 1342 |
+
_impl_.group_count_ = 0;
|
| 1343 |
+
}
|
| 1344 |
+
inline int32_t ConvParametersProto::_internal_group_count() const {
|
| 1345 |
+
return _impl_.group_count_;
|
| 1346 |
+
}
|
| 1347 |
+
inline int32_t ConvParametersProto::group_count() const {
|
| 1348 |
+
// @@protoc_insertion_point(field_get:tensorflow.ConvParametersProto.group_count)
|
| 1349 |
+
return _internal_group_count();
|
| 1350 |
+
}
|
| 1351 |
+
inline void ConvParametersProto::_internal_set_group_count(int32_t value) {
|
| 1352 |
+
|
| 1353 |
+
_impl_.group_count_ = value;
|
| 1354 |
+
}
|
| 1355 |
+
inline void ConvParametersProto::set_group_count(int32_t value) {
|
| 1356 |
+
_internal_set_group_count(value);
|
| 1357 |
+
// @@protoc_insertion_point(field_set:tensorflow.ConvParametersProto.group_count)
|
| 1358 |
+
}
|
| 1359 |
+
|
| 1360 |
+
// string device_identifier = 12;
|
| 1361 |
+
inline void ConvParametersProto::clear_device_identifier() {
|
| 1362 |
+
_impl_.device_identifier_.ClearToEmpty();
|
| 1363 |
+
}
|
| 1364 |
+
inline const std::string& ConvParametersProto::device_identifier() const {
|
| 1365 |
+
// @@protoc_insertion_point(field_get:tensorflow.ConvParametersProto.device_identifier)
|
| 1366 |
+
return _internal_device_identifier();
|
| 1367 |
+
}
|
| 1368 |
+
template <typename ArgT0, typename... ArgT>
|
| 1369 |
+
inline PROTOBUF_ALWAYS_INLINE
|
| 1370 |
+
void ConvParametersProto::set_device_identifier(ArgT0&& arg0, ArgT... args) {
|
| 1371 |
+
|
| 1372 |
+
_impl_.device_identifier_.Set(static_cast<ArgT0 &&>(arg0), args..., GetArenaForAllocation());
|
| 1373 |
+
// @@protoc_insertion_point(field_set:tensorflow.ConvParametersProto.device_identifier)
|
| 1374 |
+
}
|
| 1375 |
+
inline std::string* ConvParametersProto::mutable_device_identifier() {
|
| 1376 |
+
std::string* _s = _internal_mutable_device_identifier();
|
| 1377 |
+
// @@protoc_insertion_point(field_mutable:tensorflow.ConvParametersProto.device_identifier)
|
| 1378 |
+
return _s;
|
| 1379 |
+
}
|
| 1380 |
+
inline const std::string& ConvParametersProto::_internal_device_identifier() const {
|
| 1381 |
+
return _impl_.device_identifier_.Get();
|
| 1382 |
+
}
|
| 1383 |
+
inline void ConvParametersProto::_internal_set_device_identifier(const std::string& value) {
|
| 1384 |
+
|
| 1385 |
+
_impl_.device_identifier_.Set(value, GetArenaForAllocation());
|
| 1386 |
+
}
|
| 1387 |
+
inline std::string* ConvParametersProto::_internal_mutable_device_identifier() {
|
| 1388 |
+
|
| 1389 |
+
return _impl_.device_identifier_.Mutable(GetArenaForAllocation());
|
| 1390 |
+
}
|
| 1391 |
+
inline std::string* ConvParametersProto::release_device_identifier() {
|
| 1392 |
+
// @@protoc_insertion_point(field_release:tensorflow.ConvParametersProto.device_identifier)
|
| 1393 |
+
return _impl_.device_identifier_.Release();
|
| 1394 |
+
}
|
| 1395 |
+
inline void ConvParametersProto::set_allocated_device_identifier(std::string* device_identifier) {
|
| 1396 |
+
if (device_identifier != nullptr) {
|
| 1397 |
+
|
| 1398 |
+
} else {
|
| 1399 |
+
|
| 1400 |
+
}
|
| 1401 |
+
_impl_.device_identifier_.SetAllocated(device_identifier, GetArenaForAllocation());
|
| 1402 |
+
#ifdef PROTOBUF_FORCE_COPY_DEFAULT_STRING
|
| 1403 |
+
if (_impl_.device_identifier_.IsDefault()) {
|
| 1404 |
+
_impl_.device_identifier_.Set("", GetArenaForAllocation());
|
| 1405 |
+
}
|
| 1406 |
+
#endif // PROTOBUF_FORCE_COPY_DEFAULT_STRING
|
| 1407 |
+
// @@protoc_insertion_point(field_set_allocated:tensorflow.ConvParametersProto.device_identifier)
|
| 1408 |
+
}
|
| 1409 |
+
|
| 1410 |
+
// .tensorflow.ConvParametersProto.Fusion fusion = 13;
|
| 1411 |
+
inline bool ConvParametersProto::_internal_has_fusion() const {
|
| 1412 |
+
return this != internal_default_instance() && _impl_.fusion_ != nullptr;
|
| 1413 |
+
}
|
| 1414 |
+
inline bool ConvParametersProto::has_fusion() const {
|
| 1415 |
+
return _internal_has_fusion();
|
| 1416 |
+
}
|
| 1417 |
+
inline void ConvParametersProto::clear_fusion() {
|
| 1418 |
+
if (GetArenaForAllocation() == nullptr && _impl_.fusion_ != nullptr) {
|
| 1419 |
+
delete _impl_.fusion_;
|
| 1420 |
+
}
|
| 1421 |
+
_impl_.fusion_ = nullptr;
|
| 1422 |
+
}
|
| 1423 |
+
inline const ::tensorflow::ConvParametersProto_Fusion& ConvParametersProto::_internal_fusion() const {
|
| 1424 |
+
const ::tensorflow::ConvParametersProto_Fusion* p = _impl_.fusion_;
|
| 1425 |
+
return p != nullptr ? *p : reinterpret_cast<const ::tensorflow::ConvParametersProto_Fusion&>(
|
| 1426 |
+
::tensorflow::_ConvParametersProto_Fusion_default_instance_);
|
| 1427 |
+
}
|
| 1428 |
+
inline const ::tensorflow::ConvParametersProto_Fusion& ConvParametersProto::fusion() const {
|
| 1429 |
+
// @@protoc_insertion_point(field_get:tensorflow.ConvParametersProto.fusion)
|
| 1430 |
+
return _internal_fusion();
|
| 1431 |
+
}
|
| 1432 |
+
inline void ConvParametersProto::unsafe_arena_set_allocated_fusion(
|
| 1433 |
+
::tensorflow::ConvParametersProto_Fusion* fusion) {
|
| 1434 |
+
if (GetArenaForAllocation() == nullptr) {
|
| 1435 |
+
delete reinterpret_cast<::PROTOBUF_NAMESPACE_ID::MessageLite*>(_impl_.fusion_);
|
| 1436 |
+
}
|
| 1437 |
+
_impl_.fusion_ = fusion;
|
| 1438 |
+
if (fusion) {
|
| 1439 |
+
|
| 1440 |
+
} else {
|
| 1441 |
+
|
| 1442 |
+
}
|
| 1443 |
+
// @@protoc_insertion_point(field_unsafe_arena_set_allocated:tensorflow.ConvParametersProto.fusion)
|
| 1444 |
+
}
|
| 1445 |
+
inline ::tensorflow::ConvParametersProto_Fusion* ConvParametersProto::release_fusion() {
|
| 1446 |
+
|
| 1447 |
+
::tensorflow::ConvParametersProto_Fusion* temp = _impl_.fusion_;
|
| 1448 |
+
_impl_.fusion_ = nullptr;
|
| 1449 |
+
#ifdef PROTOBUF_FORCE_COPY_IN_RELEASE
|
| 1450 |
+
auto* old = reinterpret_cast<::PROTOBUF_NAMESPACE_ID::MessageLite*>(temp);
|
| 1451 |
+
temp = ::PROTOBUF_NAMESPACE_ID::internal::DuplicateIfNonNull(temp);
|
| 1452 |
+
if (GetArenaForAllocation() == nullptr) { delete old; }
|
| 1453 |
+
#else // PROTOBUF_FORCE_COPY_IN_RELEASE
|
| 1454 |
+
if (GetArenaForAllocation() != nullptr) {
|
| 1455 |
+
temp = ::PROTOBUF_NAMESPACE_ID::internal::DuplicateIfNonNull(temp);
|
| 1456 |
+
}
|
| 1457 |
+
#endif // !PROTOBUF_FORCE_COPY_IN_RELEASE
|
| 1458 |
+
return temp;
|
| 1459 |
+
}
|
| 1460 |
+
inline ::tensorflow::ConvParametersProto_Fusion* ConvParametersProto::unsafe_arena_release_fusion() {
|
| 1461 |
+
// @@protoc_insertion_point(field_release:tensorflow.ConvParametersProto.fusion)
|
| 1462 |
+
|
| 1463 |
+
::tensorflow::ConvParametersProto_Fusion* temp = _impl_.fusion_;
|
| 1464 |
+
_impl_.fusion_ = nullptr;
|
| 1465 |
+
return temp;
|
| 1466 |
+
}
|
| 1467 |
+
inline ::tensorflow::ConvParametersProto_Fusion* ConvParametersProto::_internal_mutable_fusion() {
|
| 1468 |
+
|
| 1469 |
+
if (_impl_.fusion_ == nullptr) {
|
| 1470 |
+
auto* p = CreateMaybeMessage<::tensorflow::ConvParametersProto_Fusion>(GetArenaForAllocation());
|
| 1471 |
+
_impl_.fusion_ = p;
|
| 1472 |
+
}
|
| 1473 |
+
return _impl_.fusion_;
|
| 1474 |
+
}
|
| 1475 |
+
inline ::tensorflow::ConvParametersProto_Fusion* ConvParametersProto::mutable_fusion() {
|
| 1476 |
+
::tensorflow::ConvParametersProto_Fusion* _msg = _internal_mutable_fusion();
|
| 1477 |
+
// @@protoc_insertion_point(field_mutable:tensorflow.ConvParametersProto.fusion)
|
| 1478 |
+
return _msg;
|
| 1479 |
+
}
|
| 1480 |
+
inline void ConvParametersProto::set_allocated_fusion(::tensorflow::ConvParametersProto_Fusion* fusion) {
|
| 1481 |
+
::PROTOBUF_NAMESPACE_ID::Arena* message_arena = GetArenaForAllocation();
|
| 1482 |
+
if (message_arena == nullptr) {
|
| 1483 |
+
delete _impl_.fusion_;
|
| 1484 |
+
}
|
| 1485 |
+
if (fusion) {
|
| 1486 |
+
::PROTOBUF_NAMESPACE_ID::Arena* submessage_arena =
|
| 1487 |
+
::PROTOBUF_NAMESPACE_ID::Arena::InternalGetOwningArena(fusion);
|
| 1488 |
+
if (message_arena != submessage_arena) {
|
| 1489 |
+
fusion = ::PROTOBUF_NAMESPACE_ID::internal::GetOwnedMessage(
|
| 1490 |
+
message_arena, fusion, submessage_arena);
|
| 1491 |
+
}
|
| 1492 |
+
|
| 1493 |
+
} else {
|
| 1494 |
+
|
| 1495 |
+
}
|
| 1496 |
+
_impl_.fusion_ = fusion;
|
| 1497 |
+
// @@protoc_insertion_point(field_set_allocated:tensorflow.ConvParametersProto.fusion)
|
| 1498 |
+
}
|
| 1499 |
+
|
| 1500 |
+
// int32 version = 14;
|
| 1501 |
+
inline void ConvParametersProto::clear_version() {
|
| 1502 |
+
_impl_.version_ = 0;
|
| 1503 |
+
}
|
| 1504 |
+
inline int32_t ConvParametersProto::_internal_version() const {
|
| 1505 |
+
return _impl_.version_;
|
| 1506 |
+
}
|
| 1507 |
+
inline int32_t ConvParametersProto::version() const {
|
| 1508 |
+
// @@protoc_insertion_point(field_get:tensorflow.ConvParametersProto.version)
|
| 1509 |
+
return _internal_version();
|
| 1510 |
+
}
|
| 1511 |
+
inline void ConvParametersProto::_internal_set_version(int32_t value) {
|
| 1512 |
+
|
| 1513 |
+
_impl_.version_ = value;
|
| 1514 |
+
}
|
| 1515 |
+
inline void ConvParametersProto::set_version(int32_t value) {
|
| 1516 |
+
_internal_set_version(value);
|
| 1517 |
+
// @@protoc_insertion_point(field_set:tensorflow.ConvParametersProto.version)
|
| 1518 |
+
}
|
| 1519 |
+
|
| 1520 |
+
// -------------------------------------------------------------------
|
| 1521 |
+
|
| 1522 |
+
// MatmulParametersProto
|
| 1523 |
+
|
| 1524 |
+
// .tensorflow.DataType ab_dtype = 1;
|
| 1525 |
+
inline void MatmulParametersProto::clear_ab_dtype() {
|
| 1526 |
+
_impl_.ab_dtype_ = 0;
|
| 1527 |
+
}
|
| 1528 |
+
inline ::tensorflow::DataType MatmulParametersProto::_internal_ab_dtype() const {
|
| 1529 |
+
return static_cast< ::tensorflow::DataType >(_impl_.ab_dtype_);
|
| 1530 |
+
}
|
| 1531 |
+
inline ::tensorflow::DataType MatmulParametersProto::ab_dtype() const {
|
| 1532 |
+
// @@protoc_insertion_point(field_get:tensorflow.MatmulParametersProto.ab_dtype)
|
| 1533 |
+
return _internal_ab_dtype();
|
| 1534 |
+
}
|
| 1535 |
+
inline void MatmulParametersProto::_internal_set_ab_dtype(::tensorflow::DataType value) {
|
| 1536 |
+
|
| 1537 |
+
_impl_.ab_dtype_ = value;
|
| 1538 |
+
}
|
| 1539 |
+
inline void MatmulParametersProto::set_ab_dtype(::tensorflow::DataType value) {
|
| 1540 |
+
_internal_set_ab_dtype(value);
|
| 1541 |
+
// @@protoc_insertion_point(field_set:tensorflow.MatmulParametersProto.ab_dtype)
|
| 1542 |
+
}
|
| 1543 |
+
|
| 1544 |
+
// .tensorflow.DataType c_dtype = 2;
|
| 1545 |
+
inline void MatmulParametersProto::clear_c_dtype() {
|
| 1546 |
+
_impl_.c_dtype_ = 0;
|
| 1547 |
+
}
|
| 1548 |
+
inline ::tensorflow::DataType MatmulParametersProto::_internal_c_dtype() const {
|
| 1549 |
+
return static_cast< ::tensorflow::DataType >(_impl_.c_dtype_);
|
| 1550 |
+
}
|
| 1551 |
+
inline ::tensorflow::DataType MatmulParametersProto::c_dtype() const {
|
| 1552 |
+
// @@protoc_insertion_point(field_get:tensorflow.MatmulParametersProto.c_dtype)
|
| 1553 |
+
return _internal_c_dtype();
|
| 1554 |
+
}
|
| 1555 |
+
inline void MatmulParametersProto::_internal_set_c_dtype(::tensorflow::DataType value) {
|
| 1556 |
+
|
| 1557 |
+
_impl_.c_dtype_ = value;
|
| 1558 |
+
}
|
| 1559 |
+
inline void MatmulParametersProto::set_c_dtype(::tensorflow::DataType value) {
|
| 1560 |
+
_internal_set_c_dtype(value);
|
| 1561 |
+
// @@protoc_insertion_point(field_set:tensorflow.MatmulParametersProto.c_dtype)
|
| 1562 |
+
}
|
| 1563 |
+
|
| 1564 |
+
// bool trans_a = 3;
|
| 1565 |
+
inline void MatmulParametersProto::clear_trans_a() {
|
| 1566 |
+
_impl_.trans_a_ = false;
|
| 1567 |
+
}
|
| 1568 |
+
inline bool MatmulParametersProto::_internal_trans_a() const {
|
| 1569 |
+
return _impl_.trans_a_;
|
| 1570 |
+
}
|
| 1571 |
+
inline bool MatmulParametersProto::trans_a() const {
|
| 1572 |
+
// @@protoc_insertion_point(field_get:tensorflow.MatmulParametersProto.trans_a)
|
| 1573 |
+
return _internal_trans_a();
|
| 1574 |
+
}
|
| 1575 |
+
inline void MatmulParametersProto::_internal_set_trans_a(bool value) {
|
| 1576 |
+
|
| 1577 |
+
_impl_.trans_a_ = value;
|
| 1578 |
+
}
|
| 1579 |
+
inline void MatmulParametersProto::set_trans_a(bool value) {
|
| 1580 |
+
_internal_set_trans_a(value);
|
| 1581 |
+
// @@protoc_insertion_point(field_set:tensorflow.MatmulParametersProto.trans_a)
|
| 1582 |
+
}
|
| 1583 |
+
|
| 1584 |
+
// bool trans_b = 4;
|
| 1585 |
+
inline void MatmulParametersProto::clear_trans_b() {
|
| 1586 |
+
_impl_.trans_b_ = false;
|
| 1587 |
+
}
|
| 1588 |
+
inline bool MatmulParametersProto::_internal_trans_b() const {
|
| 1589 |
+
return _impl_.trans_b_;
|
| 1590 |
+
}
|
| 1591 |
+
inline bool MatmulParametersProto::trans_b() const {
|
| 1592 |
+
// @@protoc_insertion_point(field_get:tensorflow.MatmulParametersProto.trans_b)
|
| 1593 |
+
return _internal_trans_b();
|
| 1594 |
+
}
|
| 1595 |
+
inline void MatmulParametersProto::_internal_set_trans_b(bool value) {
|
| 1596 |
+
|
| 1597 |
+
_impl_.trans_b_ = value;
|
| 1598 |
+
}
|
| 1599 |
+
inline void MatmulParametersProto::set_trans_b(bool value) {
|
| 1600 |
+
_internal_set_trans_b(value);
|
| 1601 |
+
// @@protoc_insertion_point(field_set:tensorflow.MatmulParametersProto.trans_b)
|
| 1602 |
+
}
|
| 1603 |
+
|
| 1604 |
+
// uint64 m = 5;
|
| 1605 |
+
inline void MatmulParametersProto::clear_m() {
|
| 1606 |
+
_impl_.m_ = uint64_t{0u};
|
| 1607 |
+
}
|
| 1608 |
+
inline uint64_t MatmulParametersProto::_internal_m() const {
|
| 1609 |
+
return _impl_.m_;
|
| 1610 |
+
}
|
| 1611 |
+
inline uint64_t MatmulParametersProto::m() const {
|
| 1612 |
+
// @@protoc_insertion_point(field_get:tensorflow.MatmulParametersProto.m)
|
| 1613 |
+
return _internal_m();
|
| 1614 |
+
}
|
| 1615 |
+
inline void MatmulParametersProto::_internal_set_m(uint64_t value) {
|
| 1616 |
+
|
| 1617 |
+
_impl_.m_ = value;
|
| 1618 |
+
}
|
| 1619 |
+
inline void MatmulParametersProto::set_m(uint64_t value) {
|
| 1620 |
+
_internal_set_m(value);
|
| 1621 |
+
// @@protoc_insertion_point(field_set:tensorflow.MatmulParametersProto.m)
|
| 1622 |
+
}
|
| 1623 |
+
|
| 1624 |
+
// uint64 n = 6;
|
| 1625 |
+
inline void MatmulParametersProto::clear_n() {
|
| 1626 |
+
_impl_.n_ = uint64_t{0u};
|
| 1627 |
+
}
|
| 1628 |
+
inline uint64_t MatmulParametersProto::_internal_n() const {
|
| 1629 |
+
return _impl_.n_;
|
| 1630 |
+
}
|
| 1631 |
+
inline uint64_t MatmulParametersProto::n() const {
|
| 1632 |
+
// @@protoc_insertion_point(field_get:tensorflow.MatmulParametersProto.n)
|
| 1633 |
+
return _internal_n();
|
| 1634 |
+
}
|
| 1635 |
+
inline void MatmulParametersProto::_internal_set_n(uint64_t value) {
|
| 1636 |
+
|
| 1637 |
+
_impl_.n_ = value;
|
| 1638 |
+
}
|
| 1639 |
+
inline void MatmulParametersProto::set_n(uint64_t value) {
|
| 1640 |
+
_internal_set_n(value);
|
| 1641 |
+
// @@protoc_insertion_point(field_set:tensorflow.MatmulParametersProto.n)
|
| 1642 |
+
}
|
| 1643 |
+
|
| 1644 |
+
// uint64 k = 7;
|
| 1645 |
+
inline void MatmulParametersProto::clear_k() {
|
| 1646 |
+
_impl_.k_ = uint64_t{0u};
|
| 1647 |
+
}
|
| 1648 |
+
inline uint64_t MatmulParametersProto::_internal_k() const {
|
| 1649 |
+
return _impl_.k_;
|
| 1650 |
+
}
|
| 1651 |
+
inline uint64_t MatmulParametersProto::k() const {
|
| 1652 |
+
// @@protoc_insertion_point(field_get:tensorflow.MatmulParametersProto.k)
|
| 1653 |
+
return _internal_k();
|
| 1654 |
+
}
|
| 1655 |
+
inline void MatmulParametersProto::_internal_set_k(uint64_t value) {
|
| 1656 |
+
|
| 1657 |
+
_impl_.k_ = value;
|
| 1658 |
+
}
|
| 1659 |
+
inline void MatmulParametersProto::set_k(uint64_t value) {
|
| 1660 |
+
_internal_set_k(value);
|
| 1661 |
+
// @@protoc_insertion_point(field_set:tensorflow.MatmulParametersProto.k)
|
| 1662 |
+
}
|
| 1663 |
+
|
| 1664 |
+
// int64 lda = 8;
|
| 1665 |
+
inline void MatmulParametersProto::clear_lda() {
|
| 1666 |
+
_impl_.lda_ = int64_t{0};
|
| 1667 |
+
}
|
| 1668 |
+
inline int64_t MatmulParametersProto::_internal_lda() const {
|
| 1669 |
+
return _impl_.lda_;
|
| 1670 |
+
}
|
| 1671 |
+
inline int64_t MatmulParametersProto::lda() const {
|
| 1672 |
+
// @@protoc_insertion_point(field_get:tensorflow.MatmulParametersProto.lda)
|
| 1673 |
+
return _internal_lda();
|
| 1674 |
+
}
|
| 1675 |
+
inline void MatmulParametersProto::_internal_set_lda(int64_t value) {
|
| 1676 |
+
|
| 1677 |
+
_impl_.lda_ = value;
|
| 1678 |
+
}
|
| 1679 |
+
inline void MatmulParametersProto::set_lda(int64_t value) {
|
| 1680 |
+
_internal_set_lda(value);
|
| 1681 |
+
// @@protoc_insertion_point(field_set:tensorflow.MatmulParametersProto.lda)
|
| 1682 |
+
}
|
| 1683 |
+
|
| 1684 |
+
// int64 ldb = 9;
|
| 1685 |
+
inline void MatmulParametersProto::clear_ldb() {
|
| 1686 |
+
_impl_.ldb_ = int64_t{0};
|
| 1687 |
+
}
|
| 1688 |
+
inline int64_t MatmulParametersProto::_internal_ldb() const {
|
| 1689 |
+
return _impl_.ldb_;
|
| 1690 |
+
}
|
| 1691 |
+
inline int64_t MatmulParametersProto::ldb() const {
|
| 1692 |
+
// @@protoc_insertion_point(field_get:tensorflow.MatmulParametersProto.ldb)
|
| 1693 |
+
return _internal_ldb();
|
| 1694 |
+
}
|
| 1695 |
+
inline void MatmulParametersProto::_internal_set_ldb(int64_t value) {
|
| 1696 |
+
|
| 1697 |
+
_impl_.ldb_ = value;
|
| 1698 |
+
}
|
| 1699 |
+
inline void MatmulParametersProto::set_ldb(int64_t value) {
|
| 1700 |
+
_internal_set_ldb(value);
|
| 1701 |
+
// @@protoc_insertion_point(field_set:tensorflow.MatmulParametersProto.ldb)
|
| 1702 |
+
}
|
| 1703 |
+
|
| 1704 |
+
// int64 ldc = 10;
|
| 1705 |
+
inline void MatmulParametersProto::clear_ldc() {
|
| 1706 |
+
_impl_.ldc_ = int64_t{0};
|
| 1707 |
+
}
|
| 1708 |
+
inline int64_t MatmulParametersProto::_internal_ldc() const {
|
| 1709 |
+
return _impl_.ldc_;
|
| 1710 |
+
}
|
| 1711 |
+
inline int64_t MatmulParametersProto::ldc() const {
|
| 1712 |
+
// @@protoc_insertion_point(field_get:tensorflow.MatmulParametersProto.ldc)
|
| 1713 |
+
return _internal_ldc();
|
| 1714 |
+
}
|
| 1715 |
+
inline void MatmulParametersProto::_internal_set_ldc(int64_t value) {
|
| 1716 |
+
|
| 1717 |
+
_impl_.ldc_ = value;
|
| 1718 |
+
}
|
| 1719 |
+
inline void MatmulParametersProto::set_ldc(int64_t value) {
|
| 1720 |
+
_internal_set_ldc(value);
|
| 1721 |
+
// @@protoc_insertion_point(field_set:tensorflow.MatmulParametersProto.ldc)
|
| 1722 |
+
}
|
| 1723 |
+
|
| 1724 |
+
// .stream_executor.dnn.ActivationMode activation_mode = 11;
|
| 1725 |
+
inline void MatmulParametersProto::clear_activation_mode() {
|
| 1726 |
+
_impl_.activation_mode_ = 0;
|
| 1727 |
+
}
|
| 1728 |
+
inline ::stream_executor::dnn::ActivationMode MatmulParametersProto::_internal_activation_mode() const {
|
| 1729 |
+
return static_cast< ::stream_executor::dnn::ActivationMode >(_impl_.activation_mode_);
|
| 1730 |
+
}
|
| 1731 |
+
inline ::stream_executor::dnn::ActivationMode MatmulParametersProto::activation_mode() const {
|
| 1732 |
+
// @@protoc_insertion_point(field_get:tensorflow.MatmulParametersProto.activation_mode)
|
| 1733 |
+
return _internal_activation_mode();
|
| 1734 |
+
}
|
| 1735 |
+
inline void MatmulParametersProto::_internal_set_activation_mode(::stream_executor::dnn::ActivationMode value) {
|
| 1736 |
+
|
| 1737 |
+
_impl_.activation_mode_ = value;
|
| 1738 |
+
}
|
| 1739 |
+
inline void MatmulParametersProto::set_activation_mode(::stream_executor::dnn::ActivationMode value) {
|
| 1740 |
+
_internal_set_activation_mode(value);
|
| 1741 |
+
// @@protoc_insertion_point(field_set:tensorflow.MatmulParametersProto.activation_mode)
|
| 1742 |
+
}
|
| 1743 |
+
|
| 1744 |
+
// string device_identifier = 12;
|
| 1745 |
+
inline void MatmulParametersProto::clear_device_identifier() {
|
| 1746 |
+
_impl_.device_identifier_.ClearToEmpty();
|
| 1747 |
+
}
|
| 1748 |
+
inline const std::string& MatmulParametersProto::device_identifier() const {
|
| 1749 |
+
// @@protoc_insertion_point(field_get:tensorflow.MatmulParametersProto.device_identifier)
|
| 1750 |
+
return _internal_device_identifier();
|
| 1751 |
+
}
|
| 1752 |
+
template <typename ArgT0, typename... ArgT>
|
| 1753 |
+
inline PROTOBUF_ALWAYS_INLINE
|
| 1754 |
+
void MatmulParametersProto::set_device_identifier(ArgT0&& arg0, ArgT... args) {
|
| 1755 |
+
|
| 1756 |
+
_impl_.device_identifier_.Set(static_cast<ArgT0 &&>(arg0), args..., GetArenaForAllocation());
|
| 1757 |
+
// @@protoc_insertion_point(field_set:tensorflow.MatmulParametersProto.device_identifier)
|
| 1758 |
+
}
|
| 1759 |
+
inline std::string* MatmulParametersProto::mutable_device_identifier() {
|
| 1760 |
+
std::string* _s = _internal_mutable_device_identifier();
|
| 1761 |
+
// @@protoc_insertion_point(field_mutable:tensorflow.MatmulParametersProto.device_identifier)
|
| 1762 |
+
return _s;
|
| 1763 |
+
}
|
| 1764 |
+
inline const std::string& MatmulParametersProto::_internal_device_identifier() const {
|
| 1765 |
+
return _impl_.device_identifier_.Get();
|
| 1766 |
+
}
|
| 1767 |
+
inline void MatmulParametersProto::_internal_set_device_identifier(const std::string& value) {
|
| 1768 |
+
|
| 1769 |
+
_impl_.device_identifier_.Set(value, GetArenaForAllocation());
|
| 1770 |
+
}
|
| 1771 |
+
inline std::string* MatmulParametersProto::_internal_mutable_device_identifier() {
|
| 1772 |
+
|
| 1773 |
+
return _impl_.device_identifier_.Mutable(GetArenaForAllocation());
|
| 1774 |
+
}
|
| 1775 |
+
inline std::string* MatmulParametersProto::release_device_identifier() {
|
| 1776 |
+
// @@protoc_insertion_point(field_release:tensorflow.MatmulParametersProto.device_identifier)
|
| 1777 |
+
return _impl_.device_identifier_.Release();
|
| 1778 |
+
}
|
| 1779 |
+
inline void MatmulParametersProto::set_allocated_device_identifier(std::string* device_identifier) {
|
| 1780 |
+
if (device_identifier != nullptr) {
|
| 1781 |
+
|
| 1782 |
+
} else {
|
| 1783 |
+
|
| 1784 |
+
}
|
| 1785 |
+
_impl_.device_identifier_.SetAllocated(device_identifier, GetArenaForAllocation());
|
| 1786 |
+
#ifdef PROTOBUF_FORCE_COPY_DEFAULT_STRING
|
| 1787 |
+
if (_impl_.device_identifier_.IsDefault()) {
|
| 1788 |
+
_impl_.device_identifier_.Set("", GetArenaForAllocation());
|
| 1789 |
+
}
|
| 1790 |
+
#endif // PROTOBUF_FORCE_COPY_DEFAULT_STRING
|
| 1791 |
+
// @@protoc_insertion_point(field_set_allocated:tensorflow.MatmulParametersProto.device_identifier)
|
| 1792 |
+
}
|
| 1793 |
+
|
| 1794 |
+
// int32 version = 14;
|
| 1795 |
+
inline void MatmulParametersProto::clear_version() {
|
| 1796 |
+
_impl_.version_ = 0;
|
| 1797 |
+
}
|
| 1798 |
+
inline int32_t MatmulParametersProto::_internal_version() const {
|
| 1799 |
+
return _impl_.version_;
|
| 1800 |
+
}
|
| 1801 |
+
inline int32_t MatmulParametersProto::version() const {
|
| 1802 |
+
// @@protoc_insertion_point(field_get:tensorflow.MatmulParametersProto.version)
|
| 1803 |
+
return _internal_version();
|
| 1804 |
+
}
|
| 1805 |
+
inline void MatmulParametersProto::_internal_set_version(int32_t value) {
|
| 1806 |
+
|
| 1807 |
+
_impl_.version_ = value;
|
| 1808 |
+
}
|
| 1809 |
+
inline void MatmulParametersProto::set_version(int32_t value) {
|
| 1810 |
+
_internal_set_version(value);
|
| 1811 |
+
// @@protoc_insertion_point(field_set:tensorflow.MatmulParametersProto.version)
|
| 1812 |
+
}
|
| 1813 |
+
|
| 1814 |
+
#ifdef __GNUC__
|
| 1815 |
+
#pragma GCC diagnostic pop
|
| 1816 |
+
#endif // __GNUC__
|
| 1817 |
+
// -------------------------------------------------------------------
|
| 1818 |
+
|
| 1819 |
+
// -------------------------------------------------------------------
|
| 1820 |
+
|
| 1821 |
+
|
| 1822 |
+
// @@protoc_insertion_point(namespace_scope)
|
| 1823 |
+
|
| 1824 |
+
} // namespace tensorflow
|
| 1825 |
+
|
| 1826 |
+
// @@protoc_insertion_point(global_scope)
|
| 1827 |
+
|
| 1828 |
+
#include <google/protobuf/port_undef.inc>
|
| 1829 |
+
#endif // GOOGLE_PROTOBUF_INCLUDED_GOOGLE_PROTOBUF_INCLUDED_tensorflow_2fcore_2futil_2fautotune_5fmaps_2fconv_5fparameters_2eproto
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/ctc/ctc_beam_entry.h
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
// LINT.IfChange
|
| 16 |
+
|
| 17 |
+
#ifndef TENSORFLOW_CORE_UTIL_CTC_CTC_BEAM_ENTRY_H_
|
| 18 |
+
#define TENSORFLOW_CORE_UTIL_CTC_CTC_BEAM_ENTRY_H_
|
| 19 |
+
|
| 20 |
+
#include <algorithm>
|
| 21 |
+
#include <memory>
|
| 22 |
+
#include <vector>
|
| 23 |
+
|
| 24 |
+
#include "Eigen/Core" // from @eigen_archive
|
| 25 |
+
#include "tensorflow/core/lib/gtl/flatmap.h"
|
| 26 |
+
#include "tensorflow/core/platform/logging.h"
|
| 27 |
+
#include "tensorflow/core/platform/macros.h"
|
| 28 |
+
#include "tensorflow/core/platform/types.h"
|
| 29 |
+
#include "tensorflow/core/util/ctc/ctc_loss_util.h"
|
| 30 |
+
|
| 31 |
+
namespace tensorflow {
|
| 32 |
+
namespace ctc {
|
| 33 |
+
|
| 34 |
+
// The ctc_beam_search namespace holds several classes meant to be accessed only
|
| 35 |
+
// in case of extending the CTCBeamSearch decoder to allow custom scoring
|
| 36 |
+
// functions.
|
| 37 |
+
//
|
| 38 |
+
// BeamEntry is exposed through template arguments BeamScorer and BeamComparer
|
| 39 |
+
// of CTCBeamSearch (ctc_beam_search.h).
|
| 40 |
+
namespace ctc_beam_search {
|
| 41 |
+
|
| 42 |
+
struct EmptyBeamState {};
|
| 43 |
+
|
| 44 |
+
template <typename T>
|
| 45 |
+
struct BeamProbability {
|
| 46 |
+
BeamProbability()
|
| 47 |
+
: total(kLogZero<T>()), blank(kLogZero<T>()), label(kLogZero<T>()) {}
|
| 48 |
+
void Reset() {
|
| 49 |
+
total = kLogZero<T>();
|
| 50 |
+
blank = kLogZero<T>();
|
| 51 |
+
label = kLogZero<T>();
|
| 52 |
+
}
|
| 53 |
+
T total;
|
| 54 |
+
T blank;
|
| 55 |
+
T label;
|
| 56 |
+
};
|
| 57 |
+
|
| 58 |
+
template <class T, class CTCBeamState>
|
| 59 |
+
class BeamRoot;
|
| 60 |
+
|
| 61 |
+
template <class T, class CTCBeamState = EmptyBeamState>
|
| 62 |
+
struct BeamEntry {
|
| 63 |
+
// BeamRoot<CTCBeamState>::AddEntry() serves as the factory method.
|
| 64 |
+
friend BeamEntry<T, CTCBeamState>* BeamRoot<T, CTCBeamState>::AddEntry(
|
| 65 |
+
BeamEntry<T, CTCBeamState>* p, int l);
|
| 66 |
+
inline bool Active() const { return newp.total != kLogZero<T>(); }
|
| 67 |
+
// Return the child at the given index, or construct a new one in-place if
|
| 68 |
+
// none was found.
|
| 69 |
+
BeamEntry<T, CTCBeamState>& GetChild(int ind) {
|
| 70 |
+
auto entry = children.emplace(ind, nullptr);
|
| 71 |
+
auto& child_entry = entry.first->second;
|
| 72 |
+
// If this is a new child, populate the BeamEntry<CTCBeamState>*.
|
| 73 |
+
if (entry.second) {
|
| 74 |
+
child_entry = beam_root->AddEntry(this, ind);
|
| 75 |
+
}
|
| 76 |
+
return *child_entry;
|
| 77 |
+
}
|
| 78 |
+
std::vector<int> LabelSeq(bool merge_repeated) const {
|
| 79 |
+
std::vector<int> labels;
|
| 80 |
+
int prev_label = -1;
|
| 81 |
+
const BeamEntry<T, CTCBeamState>* c = this;
|
| 82 |
+
while (c->parent != nullptr) { // Checking c->parent to skip root leaf.
|
| 83 |
+
if (!merge_repeated || c->label != prev_label) {
|
| 84 |
+
labels.push_back(c->label);
|
| 85 |
+
}
|
| 86 |
+
prev_label = c->label;
|
| 87 |
+
c = c->parent;
|
| 88 |
+
}
|
| 89 |
+
std::reverse(labels.begin(), labels.end());
|
| 90 |
+
return labels;
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
BeamEntry<T, CTCBeamState>* parent;
|
| 94 |
+
int label;
|
| 95 |
+
// All instances of child BeamEntry are owned by *beam_root.
|
| 96 |
+
gtl::FlatMap<int, BeamEntry<T, CTCBeamState>*> children;
|
| 97 |
+
BeamProbability<T> oldp;
|
| 98 |
+
BeamProbability<T> newp;
|
| 99 |
+
CTCBeamState state;
|
| 100 |
+
|
| 101 |
+
private:
|
| 102 |
+
// Constructor giving parent, label, and the beam_root.
|
| 103 |
+
// The object pointed to by p cannot be copied and should not be moved,
|
| 104 |
+
// otherwise parent will become invalid.
|
| 105 |
+
// This private constructor is only called through the factory method
|
| 106 |
+
// BeamRoot<CTCBeamState>::AddEntry().
|
| 107 |
+
BeamEntry(BeamEntry* p, int l, BeamRoot<T, CTCBeamState>* beam_root)
|
| 108 |
+
: parent(p), label(l), beam_root(beam_root) {}
|
| 109 |
+
BeamRoot<T, CTCBeamState>* beam_root;
|
| 110 |
+
BeamEntry(const BeamEntry&) = delete;
|
| 111 |
+
void operator=(const BeamEntry&) = delete;
|
| 112 |
+
};
|
| 113 |
+
|
| 114 |
+
// This class owns all instances of BeamEntry. This is used to avoid recursive
|
| 115 |
+
// destructor call during destruction.
|
| 116 |
+
template <class T, class CTCBeamState = EmptyBeamState>
|
| 117 |
+
class BeamRoot {
|
| 118 |
+
public:
|
| 119 |
+
BeamRoot(BeamEntry<T, CTCBeamState>* p, int l) {
|
| 120 |
+
root_entry_ = AddEntry(p, l);
|
| 121 |
+
}
|
| 122 |
+
BeamRoot(const BeamRoot&) = delete;
|
| 123 |
+
BeamRoot& operator=(const BeamRoot&) = delete;
|
| 124 |
+
|
| 125 |
+
BeamEntry<T, CTCBeamState>* AddEntry(BeamEntry<T, CTCBeamState>* p, int l) {
|
| 126 |
+
auto* new_entry = new BeamEntry<T, CTCBeamState>(p, l, this);
|
| 127 |
+
beam_entries_.emplace_back(new_entry);
|
| 128 |
+
return new_entry;
|
| 129 |
+
}
|
| 130 |
+
BeamEntry<T, CTCBeamState>* RootEntry() const { return root_entry_; }
|
| 131 |
+
|
| 132 |
+
private:
|
| 133 |
+
BeamEntry<T, CTCBeamState>* root_entry_ = nullptr;
|
| 134 |
+
std::vector<std::unique_ptr<BeamEntry<T, CTCBeamState>>> beam_entries_;
|
| 135 |
+
};
|
| 136 |
+
|
| 137 |
+
// BeamComparer is the default beam comparer provided in CTCBeamSearch.
|
| 138 |
+
template <class T, class CTCBeamState = EmptyBeamState>
|
| 139 |
+
class BeamComparer {
|
| 140 |
+
public:
|
| 141 |
+
virtual ~BeamComparer() {}
|
| 142 |
+
virtual bool inline operator()(const BeamEntry<T, CTCBeamState>* a,
|
| 143 |
+
const BeamEntry<T, CTCBeamState>* b) const {
|
| 144 |
+
return a->newp.total > b->newp.total;
|
| 145 |
+
}
|
| 146 |
+
};
|
| 147 |
+
|
| 148 |
+
} // namespace ctc_beam_search
|
| 149 |
+
|
| 150 |
+
} // namespace ctc
|
| 151 |
+
} // namespace tensorflow
|
| 152 |
+
|
| 153 |
+
#endif // TENSORFLOW_CORE_UTIL_CTC_CTC_BEAM_ENTRY_H_
|
| 154 |
+
// LINT.ThenChange(//tensorflow/lite/kernels/ctc/ctc_beam_entry.h)
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/ctc/ctc_beam_scorer.h
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
// LINT.IfChange
|
| 16 |
+
|
| 17 |
+
// Collection of scoring classes that can be extended and provided to the
|
| 18 |
+
// CTCBeamSearchDecoder to incorporate additional scoring logic (such as a
|
| 19 |
+
// language model).
|
| 20 |
+
//
|
| 21 |
+
// To build a custom scorer extend and implement the pure virtual methods from
|
| 22 |
+
// BeamScorerInterface. The default CTC decoding behavior is implemented
|
| 23 |
+
// through BaseBeamScorer.
|
| 24 |
+
|
| 25 |
+
#ifndef TENSORFLOW_CORE_UTIL_CTC_CTC_BEAM_SCORER_H_
|
| 26 |
+
#define TENSORFLOW_CORE_UTIL_CTC_CTC_BEAM_SCORER_H_
|
| 27 |
+
|
| 28 |
+
#include "tensorflow/core/util/ctc/ctc_beam_entry.h"
|
| 29 |
+
|
| 30 |
+
namespace tensorflow {
|
| 31 |
+
namespace ctc {
|
| 32 |
+
|
| 33 |
+
// Base implementation of a beam scorer used by default by the decoder that can
|
| 34 |
+
// be subclassed and provided as an argument to CTCBeamSearchDecoder, if complex
|
| 35 |
+
// scoring is required. Its main purpose is to provide a thin layer for
|
| 36 |
+
// integrating language model scoring easily.
|
| 37 |
+
template <typename T, typename CTCBeamState>
|
| 38 |
+
class BaseBeamScorer {
|
| 39 |
+
public:
|
| 40 |
+
virtual ~BaseBeamScorer() {}
|
| 41 |
+
// State initialization.
|
| 42 |
+
virtual void InitializeState(CTCBeamState* root) const {}
|
| 43 |
+
// ExpandState is called when expanding a beam to one of its children.
|
| 44 |
+
// Called at most once per child beam. In the simplest case, no state
|
| 45 |
+
// expansion is done.
|
| 46 |
+
virtual void ExpandState(const CTCBeamState& from_state, int from_label,
|
| 47 |
+
CTCBeamState* to_state, int to_label) const {}
|
| 48 |
+
// ExpandStateEnd is called after decoding has finished. Its purpose is to
|
| 49 |
+
// allow a final scoring of the beam in its current state, before resorting
|
| 50 |
+
// and retrieving the TopN requested candidates. Called at most once per beam.
|
| 51 |
+
virtual void ExpandStateEnd(CTCBeamState* state) const {}
|
| 52 |
+
// GetStateExpansionScore should be an inexpensive method to retrieve the
|
| 53 |
+
// (cached) expansion score computed within ExpandState. The score is
|
| 54 |
+
// multiplied (log-addition) with the input score at the current step from
|
| 55 |
+
// the network.
|
| 56 |
+
//
|
| 57 |
+
// The score returned should be a log-probability. In the simplest case, as
|
| 58 |
+
// there's no state expansion logic, the expansion score is zero.
|
| 59 |
+
virtual T GetStateExpansionScore(const CTCBeamState& state,
|
| 60 |
+
T previous_score) const {
|
| 61 |
+
return previous_score;
|
| 62 |
+
}
|
| 63 |
+
// GetStateEndExpansionScore should be an inexpensive method to retrieve the
|
| 64 |
+
// (cached) expansion score computed within ExpandStateEnd. The score is
|
| 65 |
+
// multiplied (log-addition) with the final probability of the beam.
|
| 66 |
+
//
|
| 67 |
+
// The score returned should be a log-probability.
|
| 68 |
+
virtual T GetStateEndExpansionScore(const CTCBeamState& state) const {
|
| 69 |
+
return T(0);
|
| 70 |
+
}
|
| 71 |
+
};
|
| 72 |
+
|
| 73 |
+
} // namespace ctc
|
| 74 |
+
} // namespace tensorflow
|
| 75 |
+
|
| 76 |
+
#endif // TENSORFLOW_CORE_UTIL_CTC_CTC_BEAM_SCORER_H_
|
| 77 |
+
// LINT.ThenChange(//tensorflow/lite/kernels/ctc/ctc_beam_scorer.h)
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
// LINT.IfChange
|
| 16 |
+
|
| 17 |
+
#ifndef TENSORFLOW_CORE_UTIL_CTC_CTC_BEAM_SEARCH_H_
|
| 18 |
+
#define TENSORFLOW_CORE_UTIL_CTC_CTC_BEAM_SEARCH_H_
|
| 19 |
+
|
| 20 |
+
#include <algorithm>
|
| 21 |
+
#include <cmath>
|
| 22 |
+
#include <limits>
|
| 23 |
+
#include <memory>
|
| 24 |
+
#include <vector>
|
| 25 |
+
|
| 26 |
+
#include "Eigen/Core" // from @eigen_archive
|
| 27 |
+
#include "tensorflow/core/lib/core/errors.h"
|
| 28 |
+
#include "tensorflow/core/lib/core/status.h"
|
| 29 |
+
#include "tensorflow/core/lib/gtl/top_n.h"
|
| 30 |
+
#include "tensorflow/core/platform/logging.h"
|
| 31 |
+
#include "tensorflow/core/platform/macros.h"
|
| 32 |
+
#include "tensorflow/core/platform/types.h"
|
| 33 |
+
#include "tensorflow/core/util/ctc/ctc_beam_entry.h"
|
| 34 |
+
#include "tensorflow/core/util/ctc/ctc_beam_scorer.h"
|
| 35 |
+
#include "tensorflow/core/util/ctc/ctc_decoder.h"
|
| 36 |
+
#include "tensorflow/core/util/ctc/ctc_loss_util.h"
|
| 37 |
+
|
| 38 |
+
namespace tensorflow {
|
| 39 |
+
namespace ctc {
|
| 40 |
+
|
| 41 |
+
template <typename T, typename CTCBeamState = ctc_beam_search::EmptyBeamState,
|
| 42 |
+
typename CTCBeamComparer =
|
| 43 |
+
ctc_beam_search::BeamComparer<T, CTCBeamState>>
|
| 44 |
+
class CTCBeamSearchDecoder : public CTCDecoder<T> {
|
| 45 |
+
// Beam Search
|
| 46 |
+
//
|
| 47 |
+
// Example (GravesTh Fig. 7.5):
|
| 48 |
+
// a -
|
| 49 |
+
// P = [ 0.3 0.7 ] t = 0
|
| 50 |
+
// [ 0.4 0.6 ] t = 1
|
| 51 |
+
//
|
| 52 |
+
// Then P(l = -) = P(--) = 0.7 * 0.6 = 0.42
|
| 53 |
+
// P(l = a) = P(a-) + P(aa) + P(-a) = 0.3*0.4 + ... = 0.58
|
| 54 |
+
//
|
| 55 |
+
// In this case, Best Path decoding is suboptimal.
|
| 56 |
+
//
|
| 57 |
+
// For Beam Search, we use the following main recurrence relations:
|
| 58 |
+
//
|
| 59 |
+
// Relation 1:
|
| 60 |
+
// ---------------------------------------------------------- Eq. 1
|
| 61 |
+
// P(l=abcd @ t=7) = P(l=abc @ t=6) * P(d @ 7)
|
| 62 |
+
// + P(l=abcd @ t=6) * (P(d @ 7) + P(- @ 7))
|
| 63 |
+
// where P(l=? @ t=7), ? = a, ab, abc, abcd are all stored and
|
| 64 |
+
// updated recursively in the beam entry.
|
| 65 |
+
//
|
| 66 |
+
// Relation 2:
|
| 67 |
+
// ---------------------------------------------------------- Eq. 2
|
| 68 |
+
// P(l=abc? @ t=3) = P(l=abc @ t=2) * P(? @ 3)
|
| 69 |
+
// for ? in a, b, d, ..., (not including c or the blank index),
|
| 70 |
+
// and the recurrence starts from the beam entry for P(l=abc @ t=2).
|
| 71 |
+
//
|
| 72 |
+
// For this case, the length of the new sequence equals t+1 (t
|
| 73 |
+
// starts at 0). This special case can be calculated as:
|
| 74 |
+
// P(l=abc? @ t=3) = P(a @ 0)*P(b @ 1)*P(c @ 2)*P(? @ 3)
|
| 75 |
+
// but we calculate it recursively for speed purposes.
|
| 76 |
+
typedef ctc_beam_search::BeamEntry<T, CTCBeamState> BeamEntry;
|
| 77 |
+
typedef ctc_beam_search::BeamRoot<T, CTCBeamState> BeamRoot;
|
| 78 |
+
typedef ctc_beam_search::BeamProbability<T> BeamProbability;
|
| 79 |
+
|
| 80 |
+
public:
|
| 81 |
+
typedef BaseBeamScorer<T, CTCBeamState> DefaultBeamScorer;
|
| 82 |
+
|
| 83 |
+
// The beam search decoder is constructed specifying the beam_width (number of
|
| 84 |
+
// candidates to keep at each decoding timestep) and a beam scorer (used for
|
| 85 |
+
// custom scoring, for example enabling the use of a language model).
|
| 86 |
+
// The ownership of the scorer remains with the caller. The default
|
| 87 |
+
// implementation, CTCBeamSearchDecoder<>::DefaultBeamScorer, generates the
|
| 88 |
+
// standard beam search.
|
| 89 |
+
CTCBeamSearchDecoder(int num_classes, int beam_width,
|
| 90 |
+
BaseBeamScorer<T, CTCBeamState>* scorer,
|
| 91 |
+
int batch_size = 1, bool merge_repeated = false)
|
| 92 |
+
: CTCDecoder<T>(num_classes, batch_size, merge_repeated),
|
| 93 |
+
beam_width_(beam_width),
|
| 94 |
+
leaves_(beam_width),
|
| 95 |
+
beam_scorer_(CHECK_NOTNULL(scorer)) {
|
| 96 |
+
Reset();
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
~CTCBeamSearchDecoder() override {}
|
| 100 |
+
|
| 101 |
+
// Run the hibernating beam search algorithm on the given input.
|
| 102 |
+
Status Decode(const typename CTCDecoder<T>::SequenceLength& seq_len,
|
| 103 |
+
const std::vector<typename CTCDecoder<T>::Input>& input,
|
| 104 |
+
std::vector<typename CTCDecoder<T>::Output>* output,
|
| 105 |
+
typename CTCDecoder<T>::ScoreOutput* scores) override;
|
| 106 |
+
|
| 107 |
+
// Calculate the next step of the beam search and update the internal state.
|
| 108 |
+
template <typename Vector>
|
| 109 |
+
void Step(const Vector& log_input_t);
|
| 110 |
+
|
| 111 |
+
template <typename Vector>
|
| 112 |
+
T GetTopK(const int K, const Vector& input, std::vector<T>* top_k_logits,
|
| 113 |
+
std::vector<int>* top_k_indices);
|
| 114 |
+
|
| 115 |
+
// Retrieve the beam scorer instance used during decoding.
|
| 116 |
+
BaseBeamScorer<T, CTCBeamState>* GetBeamScorer() const {
|
| 117 |
+
return beam_scorer_;
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
// Set label selection parameters for faster decoding.
|
| 121 |
+
// See comments for label_selection_size_ and label_selection_margin_.
|
| 122 |
+
void SetLabelSelectionParameters(int label_selection_size,
|
| 123 |
+
T label_selection_margin) {
|
| 124 |
+
label_selection_size_ = label_selection_size;
|
| 125 |
+
label_selection_margin_ = label_selection_margin;
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
// Reset the beam search
|
| 129 |
+
void Reset();
|
| 130 |
+
|
| 131 |
+
// Extract the top n paths at current time step
|
| 132 |
+
Status TopPaths(int n, std::vector<std::vector<int>>* paths,
|
| 133 |
+
std::vector<T>* log_probs, bool merge_repeated) const;
|
| 134 |
+
|
| 135 |
+
private:
|
| 136 |
+
int beam_width_;
|
| 137 |
+
|
| 138 |
+
// Label selection is designed to avoid possibly very expensive scorer calls,
|
| 139 |
+
// by pruning the hypotheses based on the input alone.
|
| 140 |
+
// Label selection size controls how many items in each beam are passed
|
| 141 |
+
// through to the beam scorer. Only items with top N input scores are
|
| 142 |
+
// considered.
|
| 143 |
+
// Label selection margin controls the difference between minimal input score
|
| 144 |
+
// (versus the best scoring label) for an item to be passed to the beam
|
| 145 |
+
// scorer. This margin is expressed in terms of log-probability.
|
| 146 |
+
// Default is to do no label selection.
|
| 147 |
+
// For more detail: https://research.google.com/pubs/pub44823.html
|
| 148 |
+
int label_selection_size_ = 0; // zero means unlimited
|
| 149 |
+
T label_selection_margin_ = -1; // -1 means unlimited.
|
| 150 |
+
|
| 151 |
+
gtl::TopN<BeamEntry*, CTCBeamComparer> leaves_;
|
| 152 |
+
std::unique_ptr<BeamRoot> beam_root_;
|
| 153 |
+
BaseBeamScorer<T, CTCBeamState>* beam_scorer_;
|
| 154 |
+
|
| 155 |
+
CTCBeamSearchDecoder(const CTCBeamSearchDecoder&) = delete;
|
| 156 |
+
void operator=(const CTCBeamSearchDecoder&) = delete;
|
| 157 |
+
};
|
| 158 |
+
|
| 159 |
+
template <typename T, typename CTCBeamState, typename CTCBeamComparer>
|
| 160 |
+
Status CTCBeamSearchDecoder<T, CTCBeamState, CTCBeamComparer>::Decode(
|
| 161 |
+
const typename CTCDecoder<T>::SequenceLength& seq_len,
|
| 162 |
+
const std::vector<typename CTCDecoder<T>::Input>& input,
|
| 163 |
+
std::vector<typename CTCDecoder<T>::Output>* output,
|
| 164 |
+
typename CTCDecoder<T>::ScoreOutput* scores) {
|
| 165 |
+
// Storage for top paths.
|
| 166 |
+
std::vector<std::vector<int>> beams;
|
| 167 |
+
std::vector<T> beam_log_probabilities;
|
| 168 |
+
int top_n = output->size();
|
| 169 |
+
if (std::any_of(output->begin(), output->end(),
|
| 170 |
+
[this](const typename CTCDecoder<T>::Output& output) -> bool {
|
| 171 |
+
return output.size() < this->batch_size_;
|
| 172 |
+
})) {
|
| 173 |
+
return errors::InvalidArgument(
|
| 174 |
+
"output needs to be of size at least (top_n, batch_size).");
|
| 175 |
+
}
|
| 176 |
+
if (scores->rows() < this->batch_size_ || scores->cols() < top_n) {
|
| 177 |
+
return errors::InvalidArgument(
|
| 178 |
+
"scores needs to be of size at least (batch_size, top_n).");
|
| 179 |
+
}
|
| 180 |
+
|
| 181 |
+
for (int b = 0; b < this->batch_size_; ++b) {
|
| 182 |
+
int seq_len_b = seq_len[b];
|
| 183 |
+
Reset();
|
| 184 |
+
|
| 185 |
+
for (int t = 0; t < seq_len_b; ++t) {
|
| 186 |
+
// Pass log-probabilities for this example + time.
|
| 187 |
+
Step(input[t].row(b));
|
| 188 |
+
} // for (int t...
|
| 189 |
+
|
| 190 |
+
// O(n * log(n))
|
| 191 |
+
std::unique_ptr<std::vector<BeamEntry*>> branches(leaves_.Extract());
|
| 192 |
+
leaves_.Reset();
|
| 193 |
+
for (int i = 0; i < branches->size(); ++i) {
|
| 194 |
+
BeamEntry* entry = (*branches)[i];
|
| 195 |
+
beam_scorer_->ExpandStateEnd(&entry->state);
|
| 196 |
+
entry->newp.total +=
|
| 197 |
+
beam_scorer_->GetStateEndExpansionScore(entry->state);
|
| 198 |
+
leaves_.push(entry);
|
| 199 |
+
}
|
| 200 |
+
|
| 201 |
+
Status status =
|
| 202 |
+
TopPaths(top_n, &beams, &beam_log_probabilities, this->merge_repeated_);
|
| 203 |
+
if (!status.ok()) {
|
| 204 |
+
return status;
|
| 205 |
+
}
|
| 206 |
+
|
| 207 |
+
CHECK_EQ(top_n, beam_log_probabilities.size());
|
| 208 |
+
CHECK_EQ(beams.size(), beam_log_probabilities.size());
|
| 209 |
+
|
| 210 |
+
for (int i = 0; i < top_n; ++i) {
|
| 211 |
+
// Copy output to the correct beam + batch
|
| 212 |
+
(*output)[i][b].swap(beams[i]);
|
| 213 |
+
(*scores)(b, i) = -beam_log_probabilities[i];
|
| 214 |
+
}
|
| 215 |
+
} // for (int b...
|
| 216 |
+
return OkStatus();
|
| 217 |
+
}
|
| 218 |
+
|
| 219 |
+
template <typename T, typename CTCBeamState, typename CTCBeamComparer>
|
| 220 |
+
template <typename Vector>
|
| 221 |
+
T CTCBeamSearchDecoder<T, CTCBeamState, CTCBeamComparer>::GetTopK(
|
| 222 |
+
const int K, const Vector& input, std::vector<T>* top_k_logits,
|
| 223 |
+
std::vector<int>* top_k_indices) {
|
| 224 |
+
// Find Top K choices, complexity nk in worst case. The array input is read
|
| 225 |
+
// just once.
|
| 226 |
+
CHECK_EQ(this->num_classes_, input.size());
|
| 227 |
+
top_k_logits->clear();
|
| 228 |
+
top_k_indices->clear();
|
| 229 |
+
top_k_logits->resize(K, -INFINITY);
|
| 230 |
+
top_k_indices->resize(K, -1);
|
| 231 |
+
for (int j = 0; j < this->num_classes_ - 1; ++j) {
|
| 232 |
+
const T logit = input(j);
|
| 233 |
+
if (logit > (*top_k_logits)[K - 1]) {
|
| 234 |
+
int k = K - 1;
|
| 235 |
+
while (k > 0 && logit > (*top_k_logits)[k - 1]) {
|
| 236 |
+
(*top_k_logits)[k] = (*top_k_logits)[k - 1];
|
| 237 |
+
(*top_k_indices)[k] = (*top_k_indices)[k - 1];
|
| 238 |
+
k--;
|
| 239 |
+
}
|
| 240 |
+
(*top_k_logits)[k] = logit;
|
| 241 |
+
(*top_k_indices)[k] = j;
|
| 242 |
+
}
|
| 243 |
+
}
|
| 244 |
+
// Return max value which is in 0th index or blank character logit
|
| 245 |
+
return std::max((*top_k_logits)[0], input(this->num_classes_ - 1));
|
| 246 |
+
}
|
| 247 |
+
|
| 248 |
+
template <typename T, typename CTCBeamState, typename CTCBeamComparer>
|
| 249 |
+
template <typename Vector>
|
| 250 |
+
void CTCBeamSearchDecoder<T, CTCBeamState, CTCBeamComparer>::Step(
|
| 251 |
+
const Vector& raw_input) {
|
| 252 |
+
std::vector<T> top_k_logits;
|
| 253 |
+
std::vector<int> top_k_indices;
|
| 254 |
+
const bool top_k =
|
| 255 |
+
(label_selection_size_ > 0 && label_selection_size_ < raw_input.size());
|
| 256 |
+
// Number of character classes to consider in each step.
|
| 257 |
+
const int max_classes =
|
| 258 |
+
top_k ? label_selection_size_ : (this->num_classes_ - 1);
|
| 259 |
+
// Get max coefficient and remove it from raw_input later.
|
| 260 |
+
T max_coeff;
|
| 261 |
+
if (top_k) {
|
| 262 |
+
max_coeff = GetTopK(label_selection_size_, raw_input, &top_k_logits,
|
| 263 |
+
&top_k_indices);
|
| 264 |
+
} else {
|
| 265 |
+
max_coeff = raw_input.maxCoeff();
|
| 266 |
+
}
|
| 267 |
+
// Get normalization term of softmax: log(sum(exp(logit[j]-max_coeff))).
|
| 268 |
+
T logsumexp = T(0.0);
|
| 269 |
+
for (int j = 0; j < raw_input.size(); ++j) {
|
| 270 |
+
logsumexp += Eigen::numext::exp(raw_input(j) - max_coeff);
|
| 271 |
+
}
|
| 272 |
+
logsumexp = Eigen::numext::log(logsumexp);
|
| 273 |
+
// Final normalization offset to get correct log probabilities.
|
| 274 |
+
T norm_offset = max_coeff + logsumexp;
|
| 275 |
+
|
| 276 |
+
const T label_selection_input_min =
|
| 277 |
+
(label_selection_margin_ >= 0) ? (max_coeff - label_selection_margin_)
|
| 278 |
+
: -std::numeric_limits<T>::infinity();
|
| 279 |
+
|
| 280 |
+
// Extract the beams sorted in decreasing new probability
|
| 281 |
+
CHECK_EQ(this->num_classes_, raw_input.size());
|
| 282 |
+
|
| 283 |
+
std::unique_ptr<std::vector<BeamEntry*>> branches(leaves_.Extract());
|
| 284 |
+
leaves_.Reset();
|
| 285 |
+
|
| 286 |
+
for (BeamEntry* b : *branches) {
|
| 287 |
+
// P(.. @ t) becomes the new P(.. @ t-1)
|
| 288 |
+
b->oldp = b->newp;
|
| 289 |
+
}
|
| 290 |
+
|
| 291 |
+
for (BeamEntry* b : *branches) {
|
| 292 |
+
if (b->parent != nullptr) { // if not the root
|
| 293 |
+
if (b->parent->Active()) {
|
| 294 |
+
// If last two sequence characters are identical:
|
| 295 |
+
// Plabel(l=acc @ t=6) = (Plabel(l=acc @ t=5)
|
| 296 |
+
// + Pblank(l=ac @ t=5))
|
| 297 |
+
// else:
|
| 298 |
+
// Plabel(l=abc @ t=6) = (Plabel(l=abc @ t=5)
|
| 299 |
+
// + P(l=ab @ t=5))
|
| 300 |
+
T previous = (b->label == b->parent->label) ? b->parent->oldp.blank
|
| 301 |
+
: b->parent->oldp.total;
|
| 302 |
+
b->newp.label =
|
| 303 |
+
LogSumExp(b->newp.label,
|
| 304 |
+
beam_scorer_->GetStateExpansionScore(b->state, previous));
|
| 305 |
+
}
|
| 306 |
+
// Plabel(l=abc @ t=6) *= P(c @ 6)
|
| 307 |
+
b->newp.label += raw_input(b->label) - norm_offset;
|
| 308 |
+
}
|
| 309 |
+
// Pblank(l=abc @ t=6) = P(l=abc @ t=5) * P(- @ 6)
|
| 310 |
+
b->newp.blank = b->oldp.total + raw_input(this->blank_index_) - norm_offset;
|
| 311 |
+
// P(l=abc @ t=6) = Plabel(l=abc @ t=6) + Pblank(l=abc @ t=6)
|
| 312 |
+
b->newp.total = LogSumExp(b->newp.blank, b->newp.label);
|
| 313 |
+
|
| 314 |
+
// Push the entry back to the top paths list.
|
| 315 |
+
// Note, this will always fill leaves back up in sorted order.
|
| 316 |
+
leaves_.push(b);
|
| 317 |
+
}
|
| 318 |
+
|
| 319 |
+
// we need to resort branches in descending oldp order.
|
| 320 |
+
|
| 321 |
+
// branches is in descending oldp order because it was
|
| 322 |
+
// originally in descending newp order and we copied newp to oldp.
|
| 323 |
+
|
| 324 |
+
// Grow new leaves
|
| 325 |
+
for (BeamEntry* b : *branches) {
|
| 326 |
+
// A new leaf (represented by its BeamProbability) is a candidate
|
| 327 |
+
// iff its total probability is nonzero and either the beam list
|
| 328 |
+
// isn't full, or the lowest probability entry in the beam has a
|
| 329 |
+
// lower probability than the leaf.
|
| 330 |
+
auto is_candidate = [this](const BeamProbability& prob) {
|
| 331 |
+
return (prob.total > kLogZero<T>() &&
|
| 332 |
+
(leaves_.size() < beam_width_ ||
|
| 333 |
+
prob.total > leaves_.peek_bottom()->newp.total));
|
| 334 |
+
};
|
| 335 |
+
|
| 336 |
+
if (!is_candidate(b->oldp)) {
|
| 337 |
+
continue;
|
| 338 |
+
}
|
| 339 |
+
|
| 340 |
+
for (int ind = 0; ind < max_classes; ind++) {
|
| 341 |
+
const int label = top_k ? top_k_indices[ind] : ind;
|
| 342 |
+
const T logit = top_k ? top_k_logits[ind] : raw_input(ind);
|
| 343 |
+
// Perform label selection: if input for this label looks very
|
| 344 |
+
// unpromising, never evaluate it with a scorer.
|
| 345 |
+
// We may compare logits instead of log probabilities,
|
| 346 |
+
// since the difference is the same in both cases.
|
| 347 |
+
if (logit < label_selection_input_min) {
|
| 348 |
+
continue;
|
| 349 |
+
}
|
| 350 |
+
BeamEntry& c = b->GetChild(label);
|
| 351 |
+
if (!c.Active()) {
|
| 352 |
+
// Pblank(l=abcd @ t=6) = 0
|
| 353 |
+
c.newp.blank = kLogZero<T>();
|
| 354 |
+
// If new child label is identical to beam label:
|
| 355 |
+
// Plabel(l=abcc @ t=6) = Pblank(l=abc @ t=5) * P(c @ 6)
|
| 356 |
+
// Otherwise:
|
| 357 |
+
// Plabel(l=abcd @ t=6) = P(l=abc @ t=5) * P(d @ 6)
|
| 358 |
+
beam_scorer_->ExpandState(b->state, b->label, &c.state, c.label);
|
| 359 |
+
T previous = (c.label == b->label) ? b->oldp.blank : b->oldp.total;
|
| 360 |
+
c.newp.label = logit - norm_offset +
|
| 361 |
+
beam_scorer_->GetStateExpansionScore(c.state, previous);
|
| 362 |
+
// P(l=abcd @ t=6) = Plabel(l=abcd @ t=6)
|
| 363 |
+
c.newp.total = c.newp.label;
|
| 364 |
+
|
| 365 |
+
if (is_candidate(c.newp)) {
|
| 366 |
+
// Before adding the new node to the beam, check if the beam
|
| 367 |
+
// is already at maximum width.
|
| 368 |
+
if (leaves_.size() == beam_width_) {
|
| 369 |
+
// Bottom is no longer in the beam search. Reset
|
| 370 |
+
// its probability; signal it's no longer in the beam search.
|
| 371 |
+
BeamEntry* bottom = leaves_.peek_bottom();
|
| 372 |
+
bottom->newp.Reset();
|
| 373 |
+
}
|
| 374 |
+
leaves_.push(&c);
|
| 375 |
+
} else {
|
| 376 |
+
// Deactivate child.
|
| 377 |
+
c.oldp.Reset();
|
| 378 |
+
c.newp.Reset();
|
| 379 |
+
}
|
| 380 |
+
}
|
| 381 |
+
}
|
| 382 |
+
} // for (BeamEntry* b...
|
| 383 |
+
}
|
| 384 |
+
|
| 385 |
+
template <typename T, typename CTCBeamState, typename CTCBeamComparer>
|
| 386 |
+
void CTCBeamSearchDecoder<T, CTCBeamState, CTCBeamComparer>::Reset() {
|
| 387 |
+
leaves_.Reset();
|
| 388 |
+
|
| 389 |
+
// This beam root, and all of its children, will be in memory until
|
| 390 |
+
// the next reset.
|
| 391 |
+
beam_root_.reset(new BeamRoot(nullptr, -1));
|
| 392 |
+
beam_root_->RootEntry()->newp.total = T(0.0); // ln(1)
|
| 393 |
+
beam_root_->RootEntry()->newp.blank = T(0.0); // ln(1)
|
| 394 |
+
|
| 395 |
+
// Add the root as the initial leaf.
|
| 396 |
+
leaves_.push(beam_root_->RootEntry());
|
| 397 |
+
|
| 398 |
+
// Call initialize state on the root object.
|
| 399 |
+
beam_scorer_->InitializeState(&beam_root_->RootEntry()->state);
|
| 400 |
+
}
|
| 401 |
+
|
| 402 |
+
template <typename T, typename CTCBeamState, typename CTCBeamComparer>
|
| 403 |
+
Status CTCBeamSearchDecoder<T, CTCBeamState, CTCBeamComparer>::TopPaths(
|
| 404 |
+
int n, std::vector<std::vector<int>>* paths, std::vector<T>* log_probs,
|
| 405 |
+
bool merge_repeated) const {
|
| 406 |
+
CHECK_NOTNULL(paths)->clear();
|
| 407 |
+
CHECK_NOTNULL(log_probs)->clear();
|
| 408 |
+
if (n > beam_width_) {
|
| 409 |
+
return errors::InvalidArgument("requested more paths than the beam width.");
|
| 410 |
+
}
|
| 411 |
+
if (n > leaves_.size()) {
|
| 412 |
+
return errors::InvalidArgument(
|
| 413 |
+
"Less leaves in the beam search than requested.");
|
| 414 |
+
}
|
| 415 |
+
|
| 416 |
+
gtl::TopN<BeamEntry*, CTCBeamComparer> top_branches(n);
|
| 417 |
+
|
| 418 |
+
// O(beam_width_ * log(n)), space complexity is O(n)
|
| 419 |
+
for (auto it = leaves_.unsorted_begin(); it != leaves_.unsorted_end(); ++it) {
|
| 420 |
+
top_branches.push(*it);
|
| 421 |
+
}
|
| 422 |
+
// O(n * log(n))
|
| 423 |
+
std::unique_ptr<std::vector<BeamEntry*>> branches(top_branches.Extract());
|
| 424 |
+
|
| 425 |
+
for (int i = 0; i < n; ++i) {
|
| 426 |
+
BeamEntry* e((*branches)[i]);
|
| 427 |
+
paths->push_back(e->LabelSeq(merge_repeated));
|
| 428 |
+
log_probs->push_back(e->newp.total);
|
| 429 |
+
}
|
| 430 |
+
return OkStatus();
|
| 431 |
+
}
|
| 432 |
+
|
| 433 |
+
} // namespace ctc
|
| 434 |
+
} // namespace tensorflow
|
| 435 |
+
|
| 436 |
+
#endif // TENSORFLOW_CORE_UTIL_CTC_CTC_BEAM_SEARCH_H_
|
| 437 |
+
// LINT.ThenChange(//tensorflow/lite/kernels/ctc/ctc_beam_search.h)
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/ctc/ctc_decoder.h
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
// LINT.IfChange
|
| 16 |
+
|
| 17 |
+
#ifndef TENSORFLOW_CORE_UTIL_CTC_CTC_DECODER_H_
|
| 18 |
+
#define TENSORFLOW_CORE_UTIL_CTC_CTC_DECODER_H_
|
| 19 |
+
|
| 20 |
+
#include <memory>
|
| 21 |
+
#include <vector>
|
| 22 |
+
|
| 23 |
+
#include "Eigen/Core" // from @eigen_archive
|
| 24 |
+
#include "tensorflow/core/lib/core/errors.h"
|
| 25 |
+
#include "tensorflow/core/lib/core/status.h"
|
| 26 |
+
|
| 27 |
+
namespace tensorflow {
|
| 28 |
+
namespace ctc {
|
| 29 |
+
|
| 30 |
+
// The CTCDecoder is an abstract interface to be implemented when providing a
|
| 31 |
+
// decoding method on the timestep output of a RNN trained with CTC loss.
|
| 32 |
+
//
|
| 33 |
+
// The two types of decoding available are:
|
| 34 |
+
// - greedy path, through the CTCGreedyDecoder
|
| 35 |
+
// - beam search, through the CTCBeamSearchDecoder
|
| 36 |
+
template <class T>
|
| 37 |
+
class CTCDecoder {
|
| 38 |
+
public:
|
| 39 |
+
typedef Eigen::Map<const Eigen::ArrayXi> SequenceLength;
|
| 40 |
+
typedef Eigen::Map<const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>>
|
| 41 |
+
Input;
|
| 42 |
+
typedef std::vector<std::vector<int>> Output;
|
| 43 |
+
typedef Eigen::Map<Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>>
|
| 44 |
+
ScoreOutput;
|
| 45 |
+
|
| 46 |
+
CTCDecoder(int num_classes, int batch_size, bool merge_repeated)
|
| 47 |
+
: num_classes_(num_classes),
|
| 48 |
+
blank_index_(num_classes - 1),
|
| 49 |
+
batch_size_(batch_size),
|
| 50 |
+
merge_repeated_(merge_repeated) {}
|
| 51 |
+
|
| 52 |
+
virtual ~CTCDecoder() {}
|
| 53 |
+
|
| 54 |
+
// Dimensionality of the input/output is expected to be:
|
| 55 |
+
// - seq_len[b] - b = 0 to batch_size_
|
| 56 |
+
// - input[t].rows(b) - t = 0 to timesteps; b = 0 t batch_size_
|
| 57 |
+
// - output.size() specifies the number of beams to be returned.
|
| 58 |
+
// - scores(b, i) - b = 0 to batch_size; i = 0 to output.size()
|
| 59 |
+
virtual Status Decode(const SequenceLength& seq_len,
|
| 60 |
+
const std::vector<Input>& input,
|
| 61 |
+
std::vector<Output>* output, ScoreOutput* scores) = 0;
|
| 62 |
+
|
| 63 |
+
int batch_size() { return batch_size_; }
|
| 64 |
+
int num_classes() { return num_classes_; }
|
| 65 |
+
|
| 66 |
+
protected:
|
| 67 |
+
int num_classes_;
|
| 68 |
+
int blank_index_;
|
| 69 |
+
int batch_size_;
|
| 70 |
+
bool merge_repeated_;
|
| 71 |
+
};
|
| 72 |
+
|
| 73 |
+
// CTCGreedyDecoder is an implementation of the simple best path decoding
|
| 74 |
+
// algorithm, selecting at each timestep the most likely class at each timestep.
|
| 75 |
+
template <class T>
|
| 76 |
+
class CTCGreedyDecoder : public CTCDecoder<T> {
|
| 77 |
+
public:
|
| 78 |
+
typedef CTCDecoder<T> Decoder;
|
| 79 |
+
CTCGreedyDecoder(int num_classes, int batch_size, bool merge_repeated)
|
| 80 |
+
: CTCDecoder<T>(num_classes, batch_size, merge_repeated) {}
|
| 81 |
+
|
| 82 |
+
Status Decode(const typename CTCDecoder<T>::SequenceLength& seq_len,
|
| 83 |
+
const std::vector<typename CTCDecoder<T>::Input>& input,
|
| 84 |
+
std::vector<typename CTCDecoder<T>::Output>* output,
|
| 85 |
+
typename CTCDecoder<T>::ScoreOutput* scores) override {
|
| 86 |
+
if (output->empty() || (*output)[0].size() < Decoder::batch_size_) {
|
| 87 |
+
return errors::InvalidArgument(
|
| 88 |
+
"output needs to be of size at least (1, batch_size).");
|
| 89 |
+
}
|
| 90 |
+
if (scores->rows() < Decoder::batch_size_ || scores->cols() == 0) {
|
| 91 |
+
return errors::InvalidArgument(
|
| 92 |
+
"scores needs to be of size at least (batch_size, 1).");
|
| 93 |
+
}
|
| 94 |
+
// For each batch entry, identify the transitions
|
| 95 |
+
for (int b = 0; b < Decoder::batch_size_; ++b) {
|
| 96 |
+
int seq_len_b = seq_len[b];
|
| 97 |
+
// Only writing to beam 0
|
| 98 |
+
std::vector<int>& output_b = (*output)[0][b];
|
| 99 |
+
|
| 100 |
+
int prev_class_ix = -1;
|
| 101 |
+
(*scores)(b, 0) = 0;
|
| 102 |
+
for (int t = 0; t < seq_len_b; ++t) {
|
| 103 |
+
auto row = input[t].row(b);
|
| 104 |
+
int max_class_ix;
|
| 105 |
+
(*scores)(b, 0) += -row.maxCoeff(&max_class_ix);
|
| 106 |
+
if (max_class_ix != Decoder::blank_index_ &&
|
| 107 |
+
!(Decoder::merge_repeated_ && max_class_ix == prev_class_ix)) {
|
| 108 |
+
output_b.push_back(max_class_ix);
|
| 109 |
+
}
|
| 110 |
+
prev_class_ix = max_class_ix;
|
| 111 |
+
}
|
| 112 |
+
}
|
| 113 |
+
return OkStatus();
|
| 114 |
+
}
|
| 115 |
+
};
|
| 116 |
+
|
| 117 |
+
} // namespace ctc
|
| 118 |
+
} // namespace tensorflow
|
| 119 |
+
|
| 120 |
+
#endif // TENSORFLOW_CORE_UTIL_CTC_CTC_DECODER_H_
|
| 121 |
+
// LINT.ThenChange(//tensorflow/lite/kernels/ctc/ctc_decoder.h)
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/ctc/ctc_loss_calculator.h
ADDED
|
@@ -0,0 +1,543 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
|
| 16 |
+
#ifndef TENSORFLOW_CORE_UTIL_CTC_CTC_LOSS_CALCULATOR_H_
|
| 17 |
+
#define TENSORFLOW_CORE_UTIL_CTC_CTC_LOSS_CALCULATOR_H_
|
| 18 |
+
|
| 19 |
+
#include <vector>
|
| 20 |
+
|
| 21 |
+
#include "Eigen/Core" // from @eigen_archive
|
| 22 |
+
#include "tensorflow/core/framework/device_base.h"
|
| 23 |
+
#include "tensorflow/core/lib/core/errors.h"
|
| 24 |
+
#include "tensorflow/core/lib/core/status.h"
|
| 25 |
+
#include "tensorflow/core/lib/strings/str_util.h"
|
| 26 |
+
#include "tensorflow/core/lib/strings/strcat.h"
|
| 27 |
+
#include "tensorflow/core/util/ctc/ctc_loss_util.h"
|
| 28 |
+
#include "tensorflow/core/util/work_sharder.h"
|
| 29 |
+
|
| 30 |
+
namespace tensorflow {
|
| 31 |
+
namespace ctc {
|
| 32 |
+
|
| 33 |
+
template <class T>
|
| 34 |
+
class CTCLossCalculator {
|
| 35 |
+
// Connectionist Temporal Classification Loss
|
| 36 |
+
//
|
| 37 |
+
// Implementation by kanishkarao@, posenhuang@, and ebrevdo@.
|
| 38 |
+
//
|
| 39 |
+
// The CTC Loss layer learns a *transition* probability value for each
|
| 40 |
+
// input time step. The transitions are on the class alphabet
|
| 41 |
+
// {0, 1, ..., N-2}
|
| 42 |
+
// where N is the depth of the input layer (the size of the alphabet is N-1).
|
| 43 |
+
// Note: The token N-1 is reserved for the "no transition" output, so
|
| 44 |
+
// make sure that your input layer has a depth that's one larger than
|
| 45 |
+
// the set of classes you're training on. Also make sure that your
|
| 46 |
+
// training labels do not have a class value of N-1, as training will skip
|
| 47 |
+
// these examples.
|
| 48 |
+
//
|
| 49 |
+
// Reference materials:
|
| 50 |
+
// GravesTh: Alex Graves, "Supervised Sequence Labeling with Recurrent
|
| 51 |
+
// Neural Networks" (PhD Thesis), Technische Universit¨at M¨unchen.
|
| 52 |
+
public:
|
| 53 |
+
typedef std::vector<std::vector<int>> LabelSequences;
|
| 54 |
+
using Matrix = Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>;
|
| 55 |
+
// typedef Eigen::MatrixXd Matrix;
|
| 56 |
+
using Array = Eigen::Array<T, Eigen::Dynamic, 1>;
|
| 57 |
+
// typedef Eigen::ArrayXd Array;
|
| 58 |
+
using InputMap = Eigen::Map<const Matrix>;
|
| 59 |
+
// typedef Eigen::Map<const Eigen::MatrixXd> InputMap;
|
| 60 |
+
using OutputMap = Eigen::Map<Matrix>;
|
| 61 |
+
// typedef Eigen::Map<Eigen::MatrixXd> OutputMap;
|
| 62 |
+
|
| 63 |
+
CTCLossCalculator(int blank_index, int output_delay)
|
| 64 |
+
: blank_index_(blank_index), output_delay_(output_delay) {}
|
| 65 |
+
|
| 66 |
+
template <typename VectorIn, typename VectorOut, typename MatrixIn,
|
| 67 |
+
typename MatrixOut>
|
| 68 |
+
Status CalculateLoss(const VectorIn& seq_len, const LabelSequences& labels,
|
| 69 |
+
const std::vector<MatrixIn>& inputs,
|
| 70 |
+
bool preprocess_collapse_repeated,
|
| 71 |
+
bool ctc_merge_repeated,
|
| 72 |
+
bool ignore_longer_outputs_than_inputs, VectorOut* loss,
|
| 73 |
+
std::vector<MatrixOut>* gradients,
|
| 74 |
+
DeviceBase::CpuWorkerThreads* workers = nullptr) const;
|
| 75 |
+
|
| 76 |
+
private:
|
| 77 |
+
void CalculateForwardVariables(const std::vector<int>& l_prime,
|
| 78 |
+
const Matrix& y, bool ctc_merge_repeated,
|
| 79 |
+
Matrix* log_alpha) const;
|
| 80 |
+
|
| 81 |
+
void CalculateBackwardVariables(const std::vector<int>& l_prime,
|
| 82 |
+
const Matrix& y, bool ctc_merge_repeated,
|
| 83 |
+
Matrix* log_beta) const;
|
| 84 |
+
|
| 85 |
+
void CalculateGradient(const std::vector<int>& l_prime, const Matrix& y,
|
| 86 |
+
const Matrix& log_alpha, const Matrix& log_beta,
|
| 87 |
+
T log_p_z_x, Matrix* dy) const;
|
| 88 |
+
|
| 89 |
+
void GetLPrimeIndices(const std::vector<int>& l,
|
| 90 |
+
std::vector<int>* l_prime) const;
|
| 91 |
+
|
| 92 |
+
// Helper function that calculates the l_prime indices for all
|
| 93 |
+
// batches at the same time, and identifies errors for any given
|
| 94 |
+
// batch. Return value:
|
| 95 |
+
// max_{b in batch_size} l_primes[b].size()
|
| 96 |
+
template <typename Vector>
|
| 97 |
+
Status PopulateLPrimes(bool preprocess_collapse_repeated,
|
| 98 |
+
bool ignore_longer_outputs_than_inputs, int batch_size,
|
| 99 |
+
int num_classes, const Vector& seq_len,
|
| 100 |
+
const LabelSequences& labels, size_t* max_u_prime,
|
| 101 |
+
LabelSequences* l_primes) const;
|
| 102 |
+
|
| 103 |
+
// Utility indices for the CTC algorithm.
|
| 104 |
+
int blank_index_;
|
| 105 |
+
|
| 106 |
+
// Delay for target labels in time steps.
|
| 107 |
+
// The delay in time steps before the output sequence.
|
| 108 |
+
const int output_delay_;
|
| 109 |
+
};
|
| 110 |
+
|
| 111 |
+
template <class T>
|
| 112 |
+
template <typename VectorIn, typename VectorOut, typename MatrixIn,
|
| 113 |
+
typename MatrixOut>
|
| 114 |
+
Status CTCLossCalculator<T>::CalculateLoss(
|
| 115 |
+
const VectorIn& seq_len, const LabelSequences& labels,
|
| 116 |
+
const std::vector<MatrixIn>& inputs, bool preprocess_collapse_repeated,
|
| 117 |
+
bool ctc_merge_repeated, bool ignore_longer_outputs_than_inputs,
|
| 118 |
+
VectorOut* loss, std::vector<MatrixOut>* gradients,
|
| 119 |
+
DeviceBase::CpuWorkerThreads* workers) const {
|
| 120 |
+
using Eigen::numext::log;
|
| 121 |
+
|
| 122 |
+
auto num_time_steps = inputs.size();
|
| 123 |
+
|
| 124 |
+
if (loss == nullptr) {
|
| 125 |
+
return errors::InvalidArgument("loss == nullptr");
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
bool requires_backprop = (gradients != nullptr);
|
| 129 |
+
|
| 130 |
+
auto batch_size = inputs[0].rows();
|
| 131 |
+
auto num_classes = inputs[0].cols();
|
| 132 |
+
|
| 133 |
+
if (loss->size() != batch_size) {
|
| 134 |
+
return errors::InvalidArgument("loss.size() != batch_size");
|
| 135 |
+
}
|
| 136 |
+
loss->setZero();
|
| 137 |
+
|
| 138 |
+
for (int t = 1; t < num_time_steps; ++t) {
|
| 139 |
+
if (inputs[t].rows() != batch_size) {
|
| 140 |
+
return errors::InvalidArgument("Expected batch size at t: ", t,
|
| 141 |
+
" to be: ", batch_size,
|
| 142 |
+
" but got: ", inputs[t].rows());
|
| 143 |
+
}
|
| 144 |
+
if (inputs[t].cols() != num_classes) {
|
| 145 |
+
return errors::InvalidArgument("Expected class count at t: ", t,
|
| 146 |
+
" to be: ", num_classes,
|
| 147 |
+
" but got: ", inputs[t].cols());
|
| 148 |
+
}
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
// Check validity of sequence_length array values.
|
| 152 |
+
auto max_seq_len = seq_len(0);
|
| 153 |
+
for (int b = 0; b < batch_size; b++) {
|
| 154 |
+
if (seq_len(b) < 0) {
|
| 155 |
+
return errors::InvalidArgument("seq_len(", b, ") < 0");
|
| 156 |
+
}
|
| 157 |
+
if (seq_len(b) > num_time_steps) {
|
| 158 |
+
return errors::InvalidArgument("seq_len(", b, ") > num_time_steps");
|
| 159 |
+
}
|
| 160 |
+
max_seq_len = std::max(seq_len(b), max_seq_len);
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
// Calculate the modified label sequence l' for each batch element,
|
| 164 |
+
// and calculate the maximum necessary allocation size.
|
| 165 |
+
LabelSequences l_primes(batch_size);
|
| 166 |
+
size_t max_u_prime = 0;
|
| 167 |
+
Status l_p_ret = PopulateLPrimes(
|
| 168 |
+
preprocess_collapse_repeated, ignore_longer_outputs_than_inputs,
|
| 169 |
+
batch_size, num_classes, seq_len, labels, &max_u_prime, &l_primes);
|
| 170 |
+
if (!l_p_ret.ok()) {
|
| 171 |
+
return l_p_ret;
|
| 172 |
+
}
|
| 173 |
+
|
| 174 |
+
// Process each item in a batch in parallel, using at most kMaxThreads.
|
| 175 |
+
auto ComputeLossAndGradients = [this, num_classes, &labels, &l_primes,
|
| 176 |
+
&seq_len, &inputs, requires_backprop,
|
| 177 |
+
ctc_merge_repeated,
|
| 178 |
+
ignore_longer_outputs_than_inputs, &loss,
|
| 179 |
+
&gradients](int64_t start_row,
|
| 180 |
+
int64_t limit_row) {
|
| 181 |
+
for (int b = start_row; b < limit_row; b++) {
|
| 182 |
+
// Return zero gradient for empty sequences or sequences with labels
|
| 183 |
+
// longer than input, which is not supported by CTC.
|
| 184 |
+
if (seq_len(b) == 0 ||
|
| 185 |
+
(ignore_longer_outputs_than_inputs &&
|
| 186 |
+
labels[b].size() > seq_len(b) - this->output_delay_)) {
|
| 187 |
+
VLOG(1) << "The sequence length is either zero or shorter than the "
|
| 188 |
+
"target output (CTC works only with shorter target sequence "
|
| 189 |
+
"than input sequence). You can turn this into a warning by "
|
| 190 |
+
"using the flag ignore_longer_outputs_than_inputs - "
|
| 191 |
+
<< b << ": " << str_util::Join(labels[b], " ");
|
| 192 |
+
continue;
|
| 193 |
+
}
|
| 194 |
+
|
| 195 |
+
// For each batch element, log(alpha) and log(beta).
|
| 196 |
+
// row size is: u_prime == l_prime.size()
|
| 197 |
+
// col size is: seq_len[b] - output_delay_
|
| 198 |
+
const std::vector<int>& l_prime = l_primes[b];
|
| 199 |
+
|
| 200 |
+
Matrix log_alpha_b(l_prime.size(), seq_len(b) - this->output_delay_);
|
| 201 |
+
Matrix log_beta_b(l_prime.size(), seq_len(b) - this->output_delay_);
|
| 202 |
+
|
| 203 |
+
// Work matrices, pre-allocated to the size required by this batch item.
|
| 204 |
+
Matrix y(num_classes, seq_len(b));
|
| 205 |
+
Matrix dy;
|
| 206 |
+
if (requires_backprop) {
|
| 207 |
+
dy = Matrix::Zero(y.rows(), y.cols());
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
// For this batch, we'll only work with this shortened sequence_length.
|
| 211 |
+
Matrix y_b = y.leftCols(seq_len(b));
|
| 212 |
+
|
| 213 |
+
// Convert label from DistBelief
|
| 214 |
+
// y, prob are in num_classes x seq_len(b)
|
| 215 |
+
// Output activations.
|
| 216 |
+
Array y_b_col;
|
| 217 |
+
for (int t = 0; t < seq_len(b); t++) {
|
| 218 |
+
// Calculate the softmax of y_b. Use original precision
|
| 219 |
+
// arithmetic for the sum.
|
| 220 |
+
T max_coeff = inputs[t].row(b).maxCoeff();
|
| 221 |
+
y_b_col = (inputs[t].row(b).array() - max_coeff).exp();
|
| 222 |
+
y_b.col(t) = y_b_col / y_b_col.sum();
|
| 223 |
+
}
|
| 224 |
+
|
| 225 |
+
// Compute forward, backward.
|
| 226 |
+
// Forward variables.
|
| 227 |
+
CalculateForwardVariables(l_prime, y_b, ctc_merge_repeated, &log_alpha_b);
|
| 228 |
+
// Backward variables.
|
| 229 |
+
CalculateBackwardVariables(l_prime, y_b, ctc_merge_repeated, &log_beta_b);
|
| 230 |
+
|
| 231 |
+
// The loss is computed as the log(p(z|x)) between the target and
|
| 232 |
+
// prediction. Do lazy evaluation of log_prob here.
|
| 233 |
+
T log_p_z_x = kLogZero<T>();
|
| 234 |
+
for (int u = 0; u < l_prime.size(); ++u) {
|
| 235 |
+
// (GravesTh) Eq 7.26, sum over all paths for t = 0.
|
| 236 |
+
log_p_z_x = LogSumExp(log_p_z_x, log_alpha_b(u, 0) + log_beta_b(u, 0));
|
| 237 |
+
}
|
| 238 |
+
|
| 239 |
+
(*loss)(b) = -log_p_z_x; // Use negative log loss for display.
|
| 240 |
+
|
| 241 |
+
// We compute the derivative if needed.
|
| 242 |
+
if (requires_backprop) {
|
| 243 |
+
// Gradients with respect to input activations.
|
| 244 |
+
// Calculate gradient.
|
| 245 |
+
dy.setZero();
|
| 246 |
+
CalculateGradient(l_prime, y_b, log_alpha_b, log_beta_b, log_p_z_x,
|
| 247 |
+
&dy);
|
| 248 |
+
|
| 249 |
+
// Convert gradient for current sample to DistBelief.
|
| 250 |
+
for (int t = 0; t < seq_len(b); t++) {
|
| 251 |
+
(*gradients)[t].row(b).array() = dy.col(t);
|
| 252 |
+
}
|
| 253 |
+
}
|
| 254 |
+
} // for (int b = ...
|
| 255 |
+
};
|
| 256 |
+
if (workers) {
|
| 257 |
+
// *Rough* estimate of the cost for one item in the batch.
|
| 258 |
+
// Forward, Backward: O(T * U (= 2L + 1)), Gradients: O(T * (U + L)).
|
| 259 |
+
//
|
| 260 |
+
// softmax: T * L * (Cost(Exp) + Cost(Div))softmax +
|
| 261 |
+
// fwd,bwd: T * 2 * (2*L + 1) * (Cost(LogSumExp) + Cost(Log)) +
|
| 262 |
+
// grad: T * ((2L + 1) * Cost(LogSumExp) + L * (Cost(Expf) + Cost(Add)).
|
| 263 |
+
const int64_t cost_exp = Eigen::internal::functor_traits<
|
| 264 |
+
Eigen::internal::scalar_exp_op<T>>::Cost;
|
| 265 |
+
const int64_t cost_log = Eigen::internal::functor_traits<
|
| 266 |
+
Eigen::internal::scalar_log_op<T>>::Cost;
|
| 267 |
+
const int64_t cost_log_sum_exp =
|
| 268 |
+
Eigen::TensorOpCost::AddCost<T>() + cost_exp + cost_log;
|
| 269 |
+
const int64_t cost =
|
| 270 |
+
max_seq_len * num_classes *
|
| 271 |
+
(cost_exp + Eigen::TensorOpCost::DivCost<T>()) +
|
| 272 |
+
max_seq_len * 2 * (2 * num_classes + 1) *
|
| 273 |
+
(cost_log_sum_exp + cost_log) +
|
| 274 |
+
max_seq_len *
|
| 275 |
+
((2 * num_classes + 1) * cost_log_sum_exp +
|
| 276 |
+
num_classes * (cost_exp + Eigen::TensorOpCost::AddCost<T>()));
|
| 277 |
+
Shard(workers->num_threads, workers->workers, batch_size, cost,
|
| 278 |
+
ComputeLossAndGradients);
|
| 279 |
+
} else {
|
| 280 |
+
ComputeLossAndGradients(0, batch_size);
|
| 281 |
+
}
|
| 282 |
+
return OkStatus();
|
| 283 |
+
}
|
| 284 |
+
|
| 285 |
+
template <class T>
|
| 286 |
+
template <typename Vector>
|
| 287 |
+
Status CTCLossCalculator<T>::PopulateLPrimes(
|
| 288 |
+
bool preprocess_collapse_repeated, bool ignore_longer_outputs_than_inputs,
|
| 289 |
+
int batch_size, int num_classes, const Vector& seq_len,
|
| 290 |
+
const LabelSequences& labels, size_t* max_u_prime,
|
| 291 |
+
LabelSequences* l_primes) const {
|
| 292 |
+
// labels is a Label array of size batch_size
|
| 293 |
+
if (labels.size() != batch_size) {
|
| 294 |
+
return errors::InvalidArgument(
|
| 295 |
+
"labels.size() != batch_size: ", labels.size(), " vs. ", batch_size);
|
| 296 |
+
}
|
| 297 |
+
|
| 298 |
+
*max_u_prime = 0; // keep track of longest l' modified label sequence.
|
| 299 |
+
for (int b = 0; b < batch_size; b++) {
|
| 300 |
+
// Assume label is in Label proto
|
| 301 |
+
const std::vector<int>& label = labels[b];
|
| 302 |
+
if (label.size() == 0) {
|
| 303 |
+
return errors::InvalidArgument("Labels length is zero in batch ", b);
|
| 304 |
+
}
|
| 305 |
+
|
| 306 |
+
// If debugging: output the labels coming into training.
|
| 307 |
+
//
|
| 308 |
+
VLOG(2) << "label for batch: " << b << ": " << str_util::Join(label, " ");
|
| 309 |
+
|
| 310 |
+
// Target indices, length = U.
|
| 311 |
+
std::vector<int> l;
|
| 312 |
+
|
| 313 |
+
// Convert label from DistBelief
|
| 314 |
+
bool finished_sequence = false;
|
| 315 |
+
for (int i = 0; i < label.size(); ++i) {
|
| 316 |
+
if (i == 0 || !preprocess_collapse_repeated || label[i] != label[i - 1]) {
|
| 317 |
+
if (label[i] >= num_classes - 1) {
|
| 318 |
+
finished_sequence = true;
|
| 319 |
+
} else {
|
| 320 |
+
if (finished_sequence) {
|
| 321 |
+
// Saw an invalid sequence with non-null following null
|
| 322 |
+
// labels.
|
| 323 |
+
return errors::InvalidArgument(
|
| 324 |
+
"Saw a non-null label (index >= num_classes - 1) "
|
| 325 |
+
"following a ",
|
| 326 |
+
"null label, batch: ", b, " num_classes: ", num_classes,
|
| 327 |
+
" labels: ", str_util::Join(label, ","),
|
| 328 |
+
" labels seen so far: ", str_util::Join(l, ","));
|
| 329 |
+
}
|
| 330 |
+
l.push_back(label[i]);
|
| 331 |
+
}
|
| 332 |
+
}
|
| 333 |
+
}
|
| 334 |
+
|
| 335 |
+
for (int l_i : l) {
|
| 336 |
+
if (l_i < 0) {
|
| 337 |
+
return errors::InvalidArgument(
|
| 338 |
+
"All labels must be nonnegative integers, batch: ", b,
|
| 339 |
+
" labels: ", str_util::Join(l, ","));
|
| 340 |
+
} else if (l_i >= num_classes) {
|
| 341 |
+
return errors::InvalidArgument(
|
| 342 |
+
"No label may be greater than num_classes. ",
|
| 343 |
+
"num_classes: ", num_classes, ", batch: ", b,
|
| 344 |
+
" labels: ", str_util::Join(l, ","));
|
| 345 |
+
}
|
| 346 |
+
}
|
| 347 |
+
if (!ignore_longer_outputs_than_inputs) {
|
| 348 |
+
// Make sure there is enough time to output the target indices.
|
| 349 |
+
int time = seq_len(b) - output_delay_;
|
| 350 |
+
int required_time = label.size();
|
| 351 |
+
if (required_time > time) {
|
| 352 |
+
return errors::InvalidArgument(
|
| 353 |
+
"Not enough time for target transition sequence ("
|
| 354 |
+
"required: ",
|
| 355 |
+
required_time, ", available: ", time, ")", b,
|
| 356 |
+
"You can turn this error into a warning by using the flag "
|
| 357 |
+
"ignore_longer_outputs_than_inputs");
|
| 358 |
+
}
|
| 359 |
+
}
|
| 360 |
+
// Target indices with blanks before each index and a blank at the end.
|
| 361 |
+
// Length U' = 2U + 1.
|
| 362 |
+
// Convert l to l_prime
|
| 363 |
+
GetLPrimeIndices(l, &l_primes->at(b));
|
| 364 |
+
*max_u_prime = std::max(*max_u_prime, l_primes->at(b).size());
|
| 365 |
+
}
|
| 366 |
+
return OkStatus();
|
| 367 |
+
}
|
| 368 |
+
|
| 369 |
+
// Calculates the alpha(t, u) as described in (GravesTh) Section 7.3.
|
| 370 |
+
// Starting with t = 0 instead of t = 1 used in the text.
|
| 371 |
+
// Based on Kanishka's CTC.
|
| 372 |
+
template <typename TT>
|
| 373 |
+
void CTCLossCalculator<TT>::CalculateForwardVariables(
|
| 374 |
+
const std::vector<int>& l_prime, const Matrix& y, bool ctc_merge_repeated,
|
| 375 |
+
Matrix* log_alpha) const {
|
| 376 |
+
using Eigen::numext::log;
|
| 377 |
+
|
| 378 |
+
// Number of cols is the number of time steps = number of cols in target
|
| 379 |
+
// after the output delay.
|
| 380 |
+
log_alpha->setConstant(kLogZero<TT>());
|
| 381 |
+
|
| 382 |
+
int U = l_prime.size();
|
| 383 |
+
int T = log_alpha->cols();
|
| 384 |
+
|
| 385 |
+
CHECK_EQ(U, log_alpha->rows());
|
| 386 |
+
|
| 387 |
+
// Initial alpha values in (GravesTh) Eq 7.5 and Eq 7.6.
|
| 388 |
+
log_alpha->coeffRef(0, 0) = log(y(blank_index_, output_delay_));
|
| 389 |
+
// Below, l_prime[1] == labels[0]
|
| 390 |
+
auto label_0 = (l_prime.size() > 1) ? l_prime[1] : blank_index_;
|
| 391 |
+
log_alpha->coeffRef(1, 0) = log(y(label_0, output_delay_));
|
| 392 |
+
|
| 393 |
+
for (int t = 1; t < T; ++t) {
|
| 394 |
+
// If there is not enough time to output the remaining labels or
|
| 395 |
+
// some labels have been skipped, then let log_alpha(u, t) continue to
|
| 396 |
+
// be kLogZero.
|
| 397 |
+
for (int u = std::max(0, U - (2 * (T - t))); u < std::min(U, 2 * (t + 1));
|
| 398 |
+
++u) {
|
| 399 |
+
// Begin (GravesTh) Eq 7.9
|
| 400 |
+
// Add in the u, t - 1 term.
|
| 401 |
+
auto sum_log_alpha = kLogZero<TT>();
|
| 402 |
+
if (ctc_merge_repeated || l_prime[u] == blank_index_) {
|
| 403 |
+
sum_log_alpha = log_alpha->coeff(u, t - 1);
|
| 404 |
+
}
|
| 405 |
+
|
| 406 |
+
// Add in the u - 1, t - 1 term.
|
| 407 |
+
if (u > 0) {
|
| 408 |
+
sum_log_alpha =
|
| 409 |
+
LogSumExp(sum_log_alpha, log_alpha->coeff(u - 1, t - 1));
|
| 410 |
+
}
|
| 411 |
+
|
| 412 |
+
// Add in the u - 2, t - 1 term if l_prime(u) != blank or l_prime(u-2).
|
| 413 |
+
if (u > 1) {
|
| 414 |
+
const bool matching_labels_merge =
|
| 415 |
+
ctc_merge_repeated && (l_prime[u] == l_prime[u - 2]);
|
| 416 |
+
if (l_prime[u] != blank_index_ && !matching_labels_merge) {
|
| 417 |
+
sum_log_alpha =
|
| 418 |
+
LogSumExp(sum_log_alpha, log_alpha->coeff(u - 2, t - 1));
|
| 419 |
+
}
|
| 420 |
+
}
|
| 421 |
+
// Multiply the summed alphas with the activation log probability.
|
| 422 |
+
log_alpha->coeffRef(u, t) =
|
| 423 |
+
log(y(l_prime[u], output_delay_ + t)) + sum_log_alpha;
|
| 424 |
+
} // End (GravesTh) Eq 7.9.
|
| 425 |
+
}
|
| 426 |
+
}
|
| 427 |
+
|
| 428 |
+
// Calculates the beta(t, u) as described in (GravesTh) Section 7.3.
|
| 429 |
+
template <class TT>
|
| 430 |
+
void CTCLossCalculator<TT>::CalculateBackwardVariables(
|
| 431 |
+
const std::vector<int>& l_prime, const Matrix& y, bool ctc_merge_repeated,
|
| 432 |
+
Matrix* log_beta) const {
|
| 433 |
+
// Number of cols is the number of time steps = number of cols in target.
|
| 434 |
+
// Matrix log_beta =
|
| 435 |
+
// Matrix::Constant(l_prime.size(), y.cols() - output_delay_,
|
| 436 |
+
// kLogZero);
|
| 437 |
+
using Eigen::numext::log;
|
| 438 |
+
|
| 439 |
+
log_beta->setConstant(kLogZero<TT>());
|
| 440 |
+
int T = log_beta->cols();
|
| 441 |
+
int U = l_prime.size();
|
| 442 |
+
CHECK_EQ(U, log_beta->rows());
|
| 443 |
+
|
| 444 |
+
// Initial beta values in (GravesTh) Eq 7.13: log of probability 1.
|
| 445 |
+
for (int u = U - 2; u < U; ++u) log_beta->coeffRef(u, T - 1) = 0;
|
| 446 |
+
|
| 447 |
+
for (int t = T - 1 - 1; t >= 0; --t) {
|
| 448 |
+
// If there is not enough time to output the remaining labels or
|
| 449 |
+
// some labels have been skipped, then let log_beta(u, t) continue to
|
| 450 |
+
// be kLogZero.
|
| 451 |
+
for (int u = std::max(0, U - (2 * (T - t))); u < std::min(U, 2 * (t + 1));
|
| 452 |
+
++u) {
|
| 453 |
+
// Begin (GravesTh) Eq 7.15
|
| 454 |
+
// Add in the u, t + 1 term.
|
| 455 |
+
if (ctc_merge_repeated || l_prime[u] == blank_index_) {
|
| 456 |
+
log_beta->coeffRef(u, t) =
|
| 457 |
+
LogSumExp(log_beta->coeff(u, t),
|
| 458 |
+
log_beta->coeff(u, t + 1) +
|
| 459 |
+
log(y(l_prime[u], output_delay_ + t + 1)));
|
| 460 |
+
}
|
| 461 |
+
|
| 462 |
+
// Add in the u + 1, t + 1 term.
|
| 463 |
+
if (u + 1 < U) {
|
| 464 |
+
log_beta->coeffRef(u, t) =
|
| 465 |
+
LogSumExp(log_beta->coeff(u, t),
|
| 466 |
+
log_beta->coeff(u + 1, t + 1) +
|
| 467 |
+
log(y(l_prime[u + 1], output_delay_ + t + 1)));
|
| 468 |
+
}
|
| 469 |
+
|
| 470 |
+
// Add in the u + 2, t + 1 term if l_prime(u) != blank or l_prime(u+2).
|
| 471 |
+
if (u + 2 < U) {
|
| 472 |
+
const bool matching_labels_merge =
|
| 473 |
+
ctc_merge_repeated && (l_prime[u] == l_prime[u + 2]);
|
| 474 |
+
if (l_prime[u] != blank_index_ && !matching_labels_merge) {
|
| 475 |
+
// Add in u + 2 term.
|
| 476 |
+
log_beta->coeffRef(u, t) =
|
| 477 |
+
LogSumExp(log_beta->coeff(u, t),
|
| 478 |
+
log_beta->coeff(u + 2, t + 1) +
|
| 479 |
+
log(y(l_prime[u + 2], output_delay_ + t + 1)));
|
| 480 |
+
}
|
| 481 |
+
} // End (GravesTh) Eq. 7.15
|
| 482 |
+
}
|
| 483 |
+
}
|
| 484 |
+
}
|
| 485 |
+
|
| 486 |
+
// Using (GravesTh) Eq 7.26 & 7.34.
|
| 487 |
+
template <typename TT>
|
| 488 |
+
void CTCLossCalculator<TT>::CalculateGradient(const std::vector<int>& l_prime,
|
| 489 |
+
const Matrix& y,
|
| 490 |
+
const Matrix& log_alpha,
|
| 491 |
+
const Matrix& log_beta,
|
| 492 |
+
TT log_p_z_x, Matrix* dy) const {
|
| 493 |
+
// Only working with the leftmost part of dy for this batch element.
|
| 494 |
+
auto dy_b = dy->leftCols(y.cols());
|
| 495 |
+
|
| 496 |
+
// It is possible that no valid path is found if the activations for the
|
| 497 |
+
// targets are zero.
|
| 498 |
+
if (log_p_z_x == kLogZero<TT>()) {
|
| 499 |
+
LOG(WARNING) << "No valid path found.";
|
| 500 |
+
dy_b = y;
|
| 501 |
+
return;
|
| 502 |
+
}
|
| 503 |
+
|
| 504 |
+
int L = y.rows();
|
| 505 |
+
int T = y.cols();
|
| 506 |
+
int U = l_prime.size();
|
| 507 |
+
|
| 508 |
+
for (int t = 0; t < T - output_delay_; ++t) {
|
| 509 |
+
Array prob_sum(L);
|
| 510 |
+
prob_sum.setConstant(kLogZero<TT>());
|
| 511 |
+
|
| 512 |
+
for (int u = 0; u < U; ++u) {
|
| 513 |
+
int l = l_prime[u];
|
| 514 |
+
prob_sum[l] = LogSumExp(prob_sum[l], log_alpha(u, t) + log_beta(u, t));
|
| 515 |
+
}
|
| 516 |
+
|
| 517 |
+
for (int l = 0; l < L; ++l) {
|
| 518 |
+
// Negative term in (GravesTh) Eq 7.28.
|
| 519 |
+
auto negative_term = expf(prob_sum[l] - log_p_z_x);
|
| 520 |
+
|
| 521 |
+
dy_b(l, output_delay_ + t) = y(l, output_delay_ + t) - negative_term;
|
| 522 |
+
}
|
| 523 |
+
}
|
| 524 |
+
}
|
| 525 |
+
|
| 526 |
+
template <class TT>
|
| 527 |
+
void CTCLossCalculator<TT>::GetLPrimeIndices(const std::vector<int>& l,
|
| 528 |
+
std::vector<int>* l_prime) const {
|
| 529 |
+
// Assumption is that l_prime is empty.
|
| 530 |
+
l_prime->reserve(2 * l.size() + 1);
|
| 531 |
+
|
| 532 |
+
for (auto label : l) {
|
| 533 |
+
l_prime->push_back(blank_index_);
|
| 534 |
+
l_prime->push_back(label);
|
| 535 |
+
}
|
| 536 |
+
// Add final blank to l'.
|
| 537 |
+
l_prime->push_back(blank_index_);
|
| 538 |
+
}
|
| 539 |
+
|
| 540 |
+
} // namespace ctc
|
| 541 |
+
} // namespace tensorflow
|
| 542 |
+
|
| 543 |
+
#endif // TENSORFLOW_CORE_UTIL_CTC_CTC_LOSS_CALCULATOR_H_
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/ctc/ctc_loss_util.h
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
// LINT.IfChange
|
| 16 |
+
|
| 17 |
+
#ifndef TENSORFLOW_CORE_UTIL_CTC_CTC_LOSS_UTIL_H_
|
| 18 |
+
#define TENSORFLOW_CORE_UTIL_CTC_CTC_LOSS_UTIL_H_
|
| 19 |
+
|
| 20 |
+
#include <cmath>
|
| 21 |
+
#include <limits>
|
| 22 |
+
|
| 23 |
+
namespace tensorflow {
|
| 24 |
+
namespace ctc {
|
| 25 |
+
|
| 26 |
+
template <class T>
|
| 27 |
+
constexpr T kLogZero() {
|
| 28 |
+
return -std::numeric_limits<T>::infinity(); // NOLINT
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
// Add logarithmic probabilities using:
|
| 32 |
+
// ln(a + b) = ln(a) + ln(1 + exp(ln(b) - ln(a)))
|
| 33 |
+
// The two inputs are assumed to be log probabilities.
|
| 34 |
+
// (GravesTh) Eq. 7.18
|
| 35 |
+
template <typename T>
|
| 36 |
+
inline T LogSumExp(T log_prob_1, T log_prob_2) {
|
| 37 |
+
// const T kLogZero = -std::numeric_limits<T>::infinity();
|
| 38 |
+
// Always have 'b' be the smaller number to avoid the exponential from
|
| 39 |
+
// blowing up.
|
| 40 |
+
if (log_prob_1 == kLogZero<T>()) {
|
| 41 |
+
return log_prob_2;
|
| 42 |
+
} else if (log_prob_2 == kLogZero<T>()) {
|
| 43 |
+
return log_prob_1;
|
| 44 |
+
} else {
|
| 45 |
+
return (log_prob_1 > log_prob_2)
|
| 46 |
+
? log_prob_1 + log1pf(expf(log_prob_2 - log_prob_1))
|
| 47 |
+
: log_prob_2 + log1pf(expf(log_prob_1 - log_prob_2));
|
| 48 |
+
}
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
} // namespace ctc
|
| 52 |
+
} // namespace tensorflow
|
| 53 |
+
|
| 54 |
+
#endif // TENSORFLOW_CORE_UTIL_CTC_CTC_LOSS_UTIL_H_
|
| 55 |
+
// LINT.ThenChange(//tensorflow/lite/kernels/ctc/ctc_loss_util.h)
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/proto/decode.h
ADDED
|
@@ -0,0 +1,711 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
|
| 16 |
+
// Inline functions for parsing the protocol buffers wire format.
|
| 17 |
+
//
|
| 18 |
+
// These functions have been optimized at the expense of safety.
|
| 19 |
+
// They are broken out into a separate file for readability but are
|
| 20 |
+
// not intended for use by clients other than the decode_proto op.
|
| 21 |
+
//
|
| 22 |
+
// The calling code in the decode_proto op does some fairly
|
| 23 |
+
// complicated things to ensure that this code is called
|
| 24 |
+
// safely. Changes to this code should be thoroughly fuzz tested.
|
| 25 |
+
|
| 26 |
+
#ifndef TENSORFLOW_CORE_UTIL_PROTO_DECODE_H_
|
| 27 |
+
#define TENSORFLOW_CORE_UTIL_PROTO_DECODE_H_
|
| 28 |
+
|
| 29 |
+
#include "tensorflow/core/framework/tensor.h"
|
| 30 |
+
#include "tensorflow/core/framework/types.h"
|
| 31 |
+
#include "tensorflow/core/platform/protobuf.h"
|
| 32 |
+
#include "tensorflow/core/platform/types.h"
|
| 33 |
+
|
| 34 |
+
namespace tensorflow {
|
| 35 |
+
namespace internal {
|
| 36 |
+
|
| 37 |
+
using tensorflow::protobuf::internal::WireFormatLite;
|
| 38 |
+
using tensorflow::protobuf::io::CodedInputStream;
|
| 39 |
+
using tensorflow::protobuf::io::CodedOutputStream;
|
| 40 |
+
using tensorflow::protobuf::io::StringOutputStream;
|
| 41 |
+
|
| 42 |
+
// Converts an uint64 to an int64 without loss of information.
|
| 43 |
+
// Unsigned values greater than INT64_MAX are represented as
|
| 44 |
+
// negative numbers by wrapping (same as twos-complement bit equivalence).
|
| 45 |
+
inline int64_t WrapUnsignedAsSigned64(uint64 unsigned_value) {
|
| 46 |
+
// For a detailed explanation of why this works to wrap unsigned ints, see
|
| 47 |
+
// http://stackoverflow.com/questions/13150449/efficient-unsigned-to-signed-cast-avoiding-implementation-defined-behavior
|
| 48 |
+
// Both if tests should be optimized out.
|
| 49 |
+
if (unsigned_value <= INT64_MAX) {
|
| 50 |
+
return static_cast<int64_t>(unsigned_value);
|
| 51 |
+
}
|
| 52 |
+
// The C++ spec allows an architecture where this test is required.
|
| 53 |
+
if (unsigned_value >= INT64_MIN) {
|
| 54 |
+
return static_cast<int64_t>(unsigned_value - INT64_MIN) + INT64_MIN;
|
| 55 |
+
}
|
| 56 |
+
return 0; // This should never occur.
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
// Converts an uint32 to an int32 without loss of information.
|
| 60 |
+
// Unsigned values greater than INT_MAX are represented as
|
| 61 |
+
// negative numbers by wrapping (same as twos-complement bit equivalence).
|
| 62 |
+
inline int32 WrapUnsignedAsSigned32(uint32 unsigned_value) {
|
| 63 |
+
// For a detailed explanation of why this works to wrap unsigned ints, see
|
| 64 |
+
// http://stackoverflow.com/questions/13150449/efficient-unsigned-to-signed-cast-avoiding-implementation-defined-behavior
|
| 65 |
+
// Both if tests should be optimized out.
|
| 66 |
+
if (unsigned_value <= INT_MAX) {
|
| 67 |
+
return static_cast<int32>(unsigned_value);
|
| 68 |
+
}
|
| 69 |
+
// The C++ spec allows an architecture where this test is required.
|
| 70 |
+
if (unsigned_value >= INT_MIN) {
|
| 71 |
+
return static_cast<int32>(unsigned_value - INT_MIN) + INT_MIN;
|
| 72 |
+
}
|
| 73 |
+
return 0; // This should never occur.
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
// Reads a single varint32 from a byte array.
|
| 77 |
+
// It is the caller's responsibility to ensure that there is enough
|
| 78 |
+
// space in the buffer.
|
| 79 |
+
// The ok value will be set to false if the buffer does not contain
|
| 80 |
+
// a valid varint.
|
| 81 |
+
inline const uint8* ReadVarint64FromArray(const uint8* buffer, bool* ok,
|
| 82 |
+
uint64* value);
|
| 83 |
+
|
| 84 |
+
// Reads a single varint32 from a byte array.
|
| 85 |
+
// It is the caller's responsibility to ensure that there is enough
|
| 86 |
+
// space in the buffer.
|
| 87 |
+
// The ok value will be set to false if the buffer does not contain
|
| 88 |
+
// a valid varint.
|
| 89 |
+
// This is slightly less efficient than the private version in
|
| 90 |
+
// coded_stream.cc but we duplicate less code by calling
|
| 91 |
+
// the 64 bit version instead of copying the code.
|
| 92 |
+
inline const uint8* ReadVarint32FromArray(const uint8* buffer, bool* ok,
|
| 93 |
+
uint32* value) {
|
| 94 |
+
uint64 tmp = 0;
|
| 95 |
+
const uint8* buf = ReadVarint64FromArray(buffer, ok, &tmp);
|
| 96 |
+
*value = tmp & 0xffffffff;
|
| 97 |
+
return buf;
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
// Reads a single proto field value from a byte array into an array.
|
| 101 |
+
// The array is part of a Tensor that was allocated by the caller
|
| 102 |
+
// with type TensorType, while DeclaredType is the proto field type.
|
| 103 |
+
template <class TensorType, enum WireFormatLite::FieldType DeclaredType>
|
| 104 |
+
const uint8* ReadFromArray(const uint8* buf, TensorType* value);
|
| 105 |
+
|
| 106 |
+
template <>
|
| 107 |
+
inline const uint8* ReadFromArray<int64_t, WireFormatLite::TYPE_INT32>(
|
| 108 |
+
const uint8* buf, int64_t* value) {
|
| 109 |
+
uint32 temp = 0;
|
| 110 |
+
bool unused_ok; // The Counting pass would have failed if this were corrupt.
|
| 111 |
+
buf = ReadVarint32FromArray(buf, &unused_ok, &temp);
|
| 112 |
+
*value = static_cast<int64_t>(temp);
|
| 113 |
+
return buf;
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
template <>
|
| 117 |
+
inline const uint8* ReadFromArray<int32, WireFormatLite::TYPE_INT32>(
|
| 118 |
+
const uint8* buf, int32* value) {
|
| 119 |
+
uint32 temp = 0;
|
| 120 |
+
bool unused_ok; // The Counting pass would have failed if this were corrupt.
|
| 121 |
+
buf = ReadVarint32FromArray(buf, &unused_ok, &temp);
|
| 122 |
+
*value = static_cast<int32>(temp);
|
| 123 |
+
return buf;
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
template <>
|
| 127 |
+
inline const uint8* ReadFromArray<int64_t, WireFormatLite::TYPE_INT64>(
|
| 128 |
+
const uint8* buf, int64_t* value) {
|
| 129 |
+
uint64 temp = 0;
|
| 130 |
+
bool unused_ok; // The Counting pass would have failed if this were corrupt.
|
| 131 |
+
buf = ReadVarint64FromArray(buf, &unused_ok, &temp);
|
| 132 |
+
*value = WrapUnsignedAsSigned64(temp);
|
| 133 |
+
return buf;
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
template <>
|
| 137 |
+
inline const uint8* ReadFromArray<uint64, WireFormatLite::TYPE_UINT32>(
|
| 138 |
+
const uint8* buf, uint64* value) {
|
| 139 |
+
uint32 temp = 0;
|
| 140 |
+
bool unused_ok; // The Counting pass would have failed if this were corrupt.
|
| 141 |
+
buf = ReadVarint32FromArray(buf, &unused_ok, &temp);
|
| 142 |
+
*value = temp;
|
| 143 |
+
return buf;
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
template <>
|
| 147 |
+
inline const uint8* ReadFromArray<uint32, WireFormatLite::TYPE_UINT32>(
|
| 148 |
+
const uint8* buf, uint32* value) {
|
| 149 |
+
bool unused_ok; // The Counting pass would have failed if this were corrupt.
|
| 150 |
+
return ReadVarint32FromArray(buf, &unused_ok, value);
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
template <>
|
| 154 |
+
inline const uint8* ReadFromArray<uint64, WireFormatLite::TYPE_UINT64>(
|
| 155 |
+
const uint8* buf, uint64* value) {
|
| 156 |
+
bool unused_ok; // The Counting pass would have failed if this were corrupt.
|
| 157 |
+
return ReadVarint64FromArray(buf, &unused_ok, value);
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
template <>
|
| 161 |
+
inline const uint8* ReadFromArray<int64_t, WireFormatLite::TYPE_SINT32>(
|
| 162 |
+
const uint8* buf, int64_t* value) {
|
| 163 |
+
uint64 temp = 0;
|
| 164 |
+
bool unused_ok; // The Counting pass would have failed if this were corrupt.
|
| 165 |
+
buf = ReadVarint64FromArray(buf, &unused_ok, &temp);
|
| 166 |
+
*value = WireFormatLite::ZigZagDecode32(temp);
|
| 167 |
+
return buf;
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
template <>
|
| 171 |
+
inline const uint8* ReadFromArray<int32, WireFormatLite::TYPE_SINT32>(
|
| 172 |
+
const uint8* buf, int32* value) {
|
| 173 |
+
uint32 temp = 0;
|
| 174 |
+
bool unused_ok; // The Counting pass would have failed if this were corrupt.
|
| 175 |
+
buf = ReadVarint32FromArray(buf, &unused_ok, &temp);
|
| 176 |
+
*value = WireFormatLite::ZigZagDecode32(temp);
|
| 177 |
+
return buf;
|
| 178 |
+
}
|
| 179 |
+
|
| 180 |
+
template <>
|
| 181 |
+
inline const uint8* ReadFromArray<int64_t, WireFormatLite::TYPE_SINT64>(
|
| 182 |
+
const uint8* buf, int64_t* value) {
|
| 183 |
+
uint64 temp = 0;
|
| 184 |
+
bool unused_ok; // The Counting pass would have failed if this were corrupt.
|
| 185 |
+
buf = ReadVarint64FromArray(buf, &unused_ok, &temp);
|
| 186 |
+
*value = WireFormatLite::ZigZagDecode64(temp);
|
| 187 |
+
return buf;
|
| 188 |
+
}
|
| 189 |
+
|
| 190 |
+
template <>
|
| 191 |
+
inline const uint8* ReadFromArray<uint64, WireFormatLite::TYPE_FIXED32>(
|
| 192 |
+
const uint8* buf, uint64* value) {
|
| 193 |
+
uint32 temp;
|
| 194 |
+
buf = WireFormatLite::ReadPrimitiveFromArray<uint32,
|
| 195 |
+
WireFormatLite::TYPE_FIXED32>(
|
| 196 |
+
buf, &temp);
|
| 197 |
+
*value = temp;
|
| 198 |
+
return buf;
|
| 199 |
+
}
|
| 200 |
+
|
| 201 |
+
template <>
|
| 202 |
+
inline const uint8* ReadFromArray<uint32, WireFormatLite::TYPE_FIXED32>(
|
| 203 |
+
const uint8* buf, uint32* value) {
|
| 204 |
+
uint32 temp;
|
| 205 |
+
buf = WireFormatLite::ReadPrimitiveFromArray<uint32,
|
| 206 |
+
WireFormatLite::TYPE_FIXED32>(
|
| 207 |
+
buf, &temp);
|
| 208 |
+
*value = WrapUnsignedAsSigned32(temp);
|
| 209 |
+
return buf;
|
| 210 |
+
}
|
| 211 |
+
|
| 212 |
+
template <>
|
| 213 |
+
inline const uint8* ReadFromArray<uint64, WireFormatLite::TYPE_FIXED64>(
|
| 214 |
+
const uint8* buf, uint64* value) {
|
| 215 |
+
protobuf_uint64 temp;
|
| 216 |
+
buf = WireFormatLite::ReadPrimitiveFromArray<protobuf_uint64,
|
| 217 |
+
WireFormatLite::TYPE_FIXED64>(
|
| 218 |
+
buf, &temp);
|
| 219 |
+
*value = WrapUnsignedAsSigned64(temp);
|
| 220 |
+
return buf;
|
| 221 |
+
}
|
| 222 |
+
|
| 223 |
+
template <>
|
| 224 |
+
inline const uint8* ReadFromArray<int64_t, WireFormatLite::TYPE_SFIXED32>(
|
| 225 |
+
const uint8* buf, int64_t* value) {
|
| 226 |
+
int32_t temp;
|
| 227 |
+
buf = WireFormatLite::ReadPrimitiveFromArray<int32,
|
| 228 |
+
WireFormatLite::TYPE_SFIXED32>(
|
| 229 |
+
buf, &temp);
|
| 230 |
+
*value = temp;
|
| 231 |
+
return buf;
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
template <>
|
| 235 |
+
inline const uint8* ReadFromArray<int32, WireFormatLite::TYPE_SFIXED32>(
|
| 236 |
+
const uint8* buf, int32* value) {
|
| 237 |
+
return WireFormatLite::ReadPrimitiveFromArray<int32,
|
| 238 |
+
WireFormatLite::TYPE_SFIXED32>(
|
| 239 |
+
buf, value);
|
| 240 |
+
}
|
| 241 |
+
|
| 242 |
+
template <>
|
| 243 |
+
inline const uint8* ReadFromArray<int64_t, WireFormatLite::TYPE_SFIXED64>(
|
| 244 |
+
const uint8* buf, int64_t* value) {
|
| 245 |
+
protobuf_int64 temp;
|
| 246 |
+
buf = WireFormatLite::ReadPrimitiveFromArray<protobuf_int64,
|
| 247 |
+
WireFormatLite::TYPE_SFIXED64>(
|
| 248 |
+
buf, &temp);
|
| 249 |
+
*value = temp;
|
| 250 |
+
return buf;
|
| 251 |
+
}
|
| 252 |
+
|
| 253 |
+
template <>
|
| 254 |
+
inline const uint8* ReadFromArray<float, WireFormatLite::TYPE_FLOAT>(
|
| 255 |
+
const uint8* buf, float* value) {
|
| 256 |
+
return WireFormatLite::ReadPrimitiveFromArray<float,
|
| 257 |
+
WireFormatLite::TYPE_FLOAT>(
|
| 258 |
+
buf, value);
|
| 259 |
+
}
|
| 260 |
+
|
| 261 |
+
template <>
|
| 262 |
+
inline const uint8* ReadFromArray<double, WireFormatLite::TYPE_FLOAT>(
|
| 263 |
+
const uint8* buf, double* value) {
|
| 264 |
+
float temp;
|
| 265 |
+
buf =
|
| 266 |
+
WireFormatLite::ReadPrimitiveFromArray<float, WireFormatLite::TYPE_FLOAT>(
|
| 267 |
+
buf, &temp);
|
| 268 |
+
*value = temp;
|
| 269 |
+
return buf;
|
| 270 |
+
}
|
| 271 |
+
|
| 272 |
+
template <>
|
| 273 |
+
inline const uint8* ReadFromArray<double, WireFormatLite::TYPE_DOUBLE>(
|
| 274 |
+
const uint8* buf, double* value) {
|
| 275 |
+
return WireFormatLite::ReadPrimitiveFromArray<double,
|
| 276 |
+
WireFormatLite::TYPE_DOUBLE>(
|
| 277 |
+
buf, value);
|
| 278 |
+
}
|
| 279 |
+
|
| 280 |
+
template <>
|
| 281 |
+
inline const uint8* ReadFromArray<bool, WireFormatLite::TYPE_BOOL>(
|
| 282 |
+
const uint8* buf, bool* value) {
|
| 283 |
+
uint64 temp = 0;
|
| 284 |
+
bool unused_ok; // The Counting pass would have failed if this were corrupt.
|
| 285 |
+
buf = ReadVarint64FromArray(buf, &unused_ok, &temp);
|
| 286 |
+
*value = temp != 0;
|
| 287 |
+
return buf;
|
| 288 |
+
}
|
| 289 |
+
|
| 290 |
+
template <>
|
| 291 |
+
inline const uint8* ReadFromArray<int, WireFormatLite::TYPE_ENUM>(
|
| 292 |
+
const uint8* buf, int* value) {
|
| 293 |
+
uint32 temp = 0;
|
| 294 |
+
bool unused_ok; // The Counting pass would have failed if this were corrupt.
|
| 295 |
+
buf = ReadVarint32FromArray(buf, &unused_ok, &temp);
|
| 296 |
+
*value = static_cast<int>(temp);
|
| 297 |
+
return buf;
|
| 298 |
+
}
|
| 299 |
+
|
| 300 |
+
// Reads packed values from an array.
|
| 301 |
+
// Stride is set to 1 for repeated fields, and 0 for non-repeated fields
|
| 302 |
+
// (where any value overwrites previous values).
|
| 303 |
+
template <class TensorType, enum WireFormatLite::FieldType DeclaredType>
|
| 304 |
+
inline int ReadPackedPrimitives(const void* bufp, const size_t len,
|
| 305 |
+
const int index, const int stride,
|
| 306 |
+
void* datap) {
|
| 307 |
+
const uint8* buf = reinterpret_cast<const uint8*>(bufp);
|
| 308 |
+
const uint8* bound = buf + len;
|
| 309 |
+
TensorType* data = reinterpret_cast<TensorType*>(datap) + index;
|
| 310 |
+
int count;
|
| 311 |
+
|
| 312 |
+
// This could overrun the bound by stride-1. This is defended
|
| 313 |
+
// against in the caller, where it ensures that the input buffer
|
| 314 |
+
// contains complete values.
|
| 315 |
+
for (count = 0; buf < bound; count += stride) {
|
| 316 |
+
buf = ReadFromArray<TensorType, DeclaredType>(buf, data + count);
|
| 317 |
+
}
|
| 318 |
+
return count;
|
| 319 |
+
}
|
| 320 |
+
|
| 321 |
+
// Reads a value of a primitive type field from a serialized proto.
|
| 322 |
+
// The value is parsed from the serialized format, then static_cast
|
| 323 |
+
// to the desired type for TensorFlow and stored.
|
| 324 |
+
template <class ValueType, class TensorType,
|
| 325 |
+
enum WireFormatLite::FieldType DeclaredType>
|
| 326 |
+
inline Status ReadPrimitive(CodedInputStream* input, int index, void* data) {
|
| 327 |
+
ValueType v;
|
| 328 |
+
if (!WireFormatLite::ReadPrimitive<ValueType, DeclaredType>(input, &v)) {
|
| 329 |
+
return errors::DataLoss("Failed reading primitive");
|
| 330 |
+
}
|
| 331 |
+
|
| 332 |
+
reinterpret_cast<TensorType*>(data)[index] = v;
|
| 333 |
+
return OkStatus();
|
| 334 |
+
}
|
| 335 |
+
|
| 336 |
+
// Reads a string, submessage, or other variable-length field from a
|
| 337 |
+
// serialized proto.
|
| 338 |
+
// May read all or part of a repeated field.
|
| 339 |
+
inline Status ReadBytes(CodedInputStream* input, int index, void* datap) {
|
| 340 |
+
tstring* data = reinterpret_cast<tstring*>(datap) + index;
|
| 341 |
+
|
| 342 |
+
uint32 length;
|
| 343 |
+
if (!input->ReadVarint32(&length)) {
|
| 344 |
+
return errors::DataLoss("Failed reading bytes");
|
| 345 |
+
}
|
| 346 |
+
|
| 347 |
+
data->resize_uninitialized(length);
|
| 348 |
+
|
| 349 |
+
if (!input->ReadRaw(data->data(), length)) {
|
| 350 |
+
return errors::DataLoss("Failed reading bytes");
|
| 351 |
+
}
|
| 352 |
+
return OkStatus();
|
| 353 |
+
}
|
| 354 |
+
|
| 355 |
+
// Reads a tag-delimited field (TYPE_GROUP) from a serialized proto,
|
| 356 |
+
// as a bytestring.
|
| 357 |
+
inline Status ReadGroupBytes(CodedInputStream* input, int field_number,
|
| 358 |
+
int index, void* datap) {
|
| 359 |
+
// WireFormatLite::SkipField has an option to emit the
|
| 360 |
+
// skipped bytes to an output stream. We could do better by implementing our
|
| 361 |
+
// own scanner but this is simpler for now.
|
| 362 |
+
// TODO(nix): there is a faster way to grab TYPE_GROUP bytes by relying
|
| 363 |
+
// on input->IsFlat() == true and using input->GetDirectBufferPointer()
|
| 364 |
+
// with input->CurrentPosition().
|
| 365 |
+
tstring* data = reinterpret_cast<tstring*>(datap) + index;
|
| 366 |
+
// TODO(dero): To mitigate the string to tstring copy, we can implement our
|
| 367 |
+
// own scanner as described above. We would first need to obtain the length
|
| 368 |
+
// in an initial pass and resize/reserve the tstring. But, given that
|
| 369 |
+
// TYPE_GROUP is deprecated and currently no tests in
|
| 370 |
+
// tensorflow/python/kernel_tests/proto:decode_proto_op_test target a
|
| 371 |
+
// TYPE_GROUP tag, we use std::string as a read buffer.
|
| 372 |
+
string buf;
|
| 373 |
+
StringOutputStream string_stream(&buf);
|
| 374 |
+
{
|
| 375 |
+
CodedOutputStream out(&string_stream);
|
| 376 |
+
if (!WireFormatLite::SkipField(
|
| 377 |
+
input,
|
| 378 |
+
WireFormatLite::MakeTag(field_number,
|
| 379 |
+
WireFormatLite::WIRETYPE_START_GROUP),
|
| 380 |
+
&out)) {
|
| 381 |
+
return errors::DataLoss("Failed reading group");
|
| 382 |
+
}
|
| 383 |
+
}
|
| 384 |
+
*data = buf;
|
| 385 |
+
return OkStatus();
|
| 386 |
+
}
|
| 387 |
+
|
| 388 |
+
// Reads a single field value from a CodedInputStream into a tensor.
|
| 389 |
+
inline Status ReadValue(CodedInputStream* input,
|
| 390 |
+
WireFormatLite::FieldType field_type, int field_number,
|
| 391 |
+
DataType dtype, int index, void* datap) {
|
| 392 |
+
// Dispatch to the appropriately typed field reader based on the schema type.
|
| 393 |
+
switch (field_type) {
|
| 394 |
+
case WireFormatLite::TYPE_DOUBLE:
|
| 395 |
+
return ReadPrimitive<double, double, WireFormatLite::TYPE_DOUBLE>(
|
| 396 |
+
input, index, datap);
|
| 397 |
+
case WireFormatLite::TYPE_FLOAT:
|
| 398 |
+
switch (dtype) {
|
| 399 |
+
case DataType::DT_DOUBLE:
|
| 400 |
+
return ReadPrimitive<float, double, WireFormatLite::TYPE_FLOAT>(
|
| 401 |
+
input, index, datap);
|
| 402 |
+
case DataType::DT_FLOAT:
|
| 403 |
+
return ReadPrimitive<float, float, WireFormatLite::TYPE_FLOAT>(
|
| 404 |
+
input, index, datap);
|
| 405 |
+
default:
|
| 406 |
+
return errors::DataLoss("Failed reading TYPE_FLOAT for ",
|
| 407 |
+
DataTypeString(dtype));
|
| 408 |
+
}
|
| 409 |
+
case WireFormatLite::TYPE_INT64:
|
| 410 |
+
return ReadPrimitive<protobuf_int64, int64_t, WireFormatLite::TYPE_INT64>(
|
| 411 |
+
input, index, datap);
|
| 412 |
+
case WireFormatLite::TYPE_UINT64:
|
| 413 |
+
return ReadPrimitive<protobuf_uint64, uint64,
|
| 414 |
+
WireFormatLite::TYPE_UINT64>(input, index, datap);
|
| 415 |
+
case WireFormatLite::TYPE_INT32:
|
| 416 |
+
switch (dtype) {
|
| 417 |
+
case DataType::DT_INT64:
|
| 418 |
+
return ReadPrimitive<int32, int64_t, WireFormatLite::TYPE_INT32>(
|
| 419 |
+
input, index, datap);
|
| 420 |
+
case DataType::DT_INT32:
|
| 421 |
+
return ReadPrimitive<int32, int32, WireFormatLite::TYPE_INT32>(
|
| 422 |
+
input, index, datap);
|
| 423 |
+
default:
|
| 424 |
+
return errors::DataLoss("Failed reading TYPE_INT32 for ",
|
| 425 |
+
DataTypeString(dtype));
|
| 426 |
+
}
|
| 427 |
+
case WireFormatLite::TYPE_FIXED64:
|
| 428 |
+
return ReadPrimitive<protobuf_uint64, uint64,
|
| 429 |
+
WireFormatLite::TYPE_FIXED64>(input, index, datap);
|
| 430 |
+
case WireFormatLite::TYPE_FIXED32:
|
| 431 |
+
switch (dtype) {
|
| 432 |
+
case DataType::DT_UINT64:
|
| 433 |
+
return ReadPrimitive<uint32, uint64, WireFormatLite::TYPE_FIXED32>(
|
| 434 |
+
input, index, datap);
|
| 435 |
+
case DataType::DT_UINT32:
|
| 436 |
+
return ReadPrimitive<uint32, uint32, WireFormatLite::TYPE_FIXED32>(
|
| 437 |
+
input, index, datap);
|
| 438 |
+
default:
|
| 439 |
+
return errors::DataLoss("Failed reading TYPE_FIXED32 for ",
|
| 440 |
+
DataTypeString(dtype));
|
| 441 |
+
}
|
| 442 |
+
case WireFormatLite::TYPE_BOOL:
|
| 443 |
+
return ReadPrimitive<bool, bool, WireFormatLite::TYPE_BOOL>(input, index,
|
| 444 |
+
datap);
|
| 445 |
+
case WireFormatLite::TYPE_STRING:
|
| 446 |
+
return ReadBytes(input, index, datap);
|
| 447 |
+
case WireFormatLite::TYPE_GROUP:
|
| 448 |
+
return ReadGroupBytes(input, field_number, index, datap);
|
| 449 |
+
case WireFormatLite::TYPE_MESSAGE:
|
| 450 |
+
return ReadBytes(input, index, datap);
|
| 451 |
+
case WireFormatLite::TYPE_BYTES:
|
| 452 |
+
return ReadBytes(input, index, datap);
|
| 453 |
+
case WireFormatLite::TYPE_UINT32:
|
| 454 |
+
switch (dtype) {
|
| 455 |
+
case DataType::DT_UINT64:
|
| 456 |
+
return ReadPrimitive<uint32, uint64, WireFormatLite::TYPE_UINT32>(
|
| 457 |
+
input, index, datap);
|
| 458 |
+
case DataType::DT_UINT32:
|
| 459 |
+
return ReadPrimitive<uint32, uint32, WireFormatLite::TYPE_UINT32>(
|
| 460 |
+
input, index, datap);
|
| 461 |
+
default:
|
| 462 |
+
return errors::DataLoss("Failed reading TYPE_UINT32 for ",
|
| 463 |
+
DataTypeString(dtype));
|
| 464 |
+
}
|
| 465 |
+
case WireFormatLite::TYPE_ENUM:
|
| 466 |
+
return ReadPrimitive<int32, int32, WireFormatLite::TYPE_ENUM>(
|
| 467 |
+
input, index, datap);
|
| 468 |
+
case WireFormatLite::TYPE_SFIXED32:
|
| 469 |
+
switch (dtype) {
|
| 470 |
+
case DataType::DT_INT64:
|
| 471 |
+
return ReadPrimitive<int32, int64_t, WireFormatLite::TYPE_SFIXED32>(
|
| 472 |
+
input, index, datap);
|
| 473 |
+
case DataType::DT_INT32:
|
| 474 |
+
return ReadPrimitive<int32, int32, WireFormatLite::TYPE_SFIXED32>(
|
| 475 |
+
input, index, datap);
|
| 476 |
+
default:
|
| 477 |
+
return errors::DataLoss("Failed reading TYPE_SFIXED32 for ",
|
| 478 |
+
DataTypeString(dtype));
|
| 479 |
+
}
|
| 480 |
+
case WireFormatLite::TYPE_SFIXED64:
|
| 481 |
+
return ReadPrimitive<protobuf_int64, int64_t,
|
| 482 |
+
WireFormatLite::TYPE_SFIXED64>(input, index, datap);
|
| 483 |
+
case WireFormatLite::TYPE_SINT32:
|
| 484 |
+
switch (dtype) {
|
| 485 |
+
case DataType::DT_INT64:
|
| 486 |
+
return ReadPrimitive<int32, int64_t, WireFormatLite::TYPE_SINT32>(
|
| 487 |
+
input, index, datap);
|
| 488 |
+
case DataType::DT_INT32:
|
| 489 |
+
return ReadPrimitive<int32, int32, WireFormatLite::TYPE_SINT32>(
|
| 490 |
+
input, index, datap);
|
| 491 |
+
default:
|
| 492 |
+
return errors::DataLoss("Failed reading TYPE_SINT32 for ",
|
| 493 |
+
DataTypeString(dtype));
|
| 494 |
+
}
|
| 495 |
+
case WireFormatLite::TYPE_SINT64:
|
| 496 |
+
return ReadPrimitive<protobuf_int64, int64_t,
|
| 497 |
+
WireFormatLite::TYPE_SINT64>(input, index, datap);
|
| 498 |
+
// default: intentionally omitted in order to enable static checking.
|
| 499 |
+
}
|
| 500 |
+
// Unreachable.
|
| 501 |
+
return errors::DataLoss("Failed reading unknown wire type");
|
| 502 |
+
}
|
| 503 |
+
|
| 504 |
+
// Reads and stores a length-delimited list of values.
|
| 505 |
+
inline Status ReadPackedFromArray(const void* buf, size_t buf_size,
|
| 506 |
+
const WireFormatLite::FieldType field_type,
|
| 507 |
+
const int field_number, const DataType dtype,
|
| 508 |
+
const int stride, int* index, void* data) {
|
| 509 |
+
// Dispatch to the appropriately typed field reader based on the schema type.
|
| 510 |
+
switch (field_type) {
|
| 511 |
+
case WireFormatLite::TYPE_DOUBLE:
|
| 512 |
+
*index += ReadPackedPrimitives<double, WireFormatLite::TYPE_DOUBLE>(
|
| 513 |
+
buf, buf_size, *index, stride, data);
|
| 514 |
+
return OkStatus();
|
| 515 |
+
case WireFormatLite::TYPE_FLOAT:
|
| 516 |
+
switch (dtype) {
|
| 517 |
+
case DataType::DT_DOUBLE:
|
| 518 |
+
*index += ReadPackedPrimitives<double, WireFormatLite::TYPE_FLOAT>(
|
| 519 |
+
buf, buf_size, *index, stride, data);
|
| 520 |
+
return OkStatus();
|
| 521 |
+
case DataType::DT_FLOAT:
|
| 522 |
+
*index += ReadPackedPrimitives<float, WireFormatLite::TYPE_FLOAT>(
|
| 523 |
+
buf, buf_size, *index, stride, data);
|
| 524 |
+
return OkStatus();
|
| 525 |
+
default:
|
| 526 |
+
return errors::DataLoss("Failed reading TYPE_FLOAT for ",
|
| 527 |
+
DataTypeString(dtype));
|
| 528 |
+
}
|
| 529 |
+
case WireFormatLite::TYPE_INT64:
|
| 530 |
+
*index += ReadPackedPrimitives<int64_t, WireFormatLite::TYPE_INT64>(
|
| 531 |
+
buf, buf_size, *index, stride, data);
|
| 532 |
+
return OkStatus();
|
| 533 |
+
case WireFormatLite::TYPE_UINT64:
|
| 534 |
+
*index += ReadPackedPrimitives<uint64, WireFormatLite::TYPE_UINT64>(
|
| 535 |
+
buf, buf_size, *index, stride, data);
|
| 536 |
+
return OkStatus();
|
| 537 |
+
case WireFormatLite::TYPE_INT32:
|
| 538 |
+
switch (dtype) {
|
| 539 |
+
case DataType::DT_INT64:
|
| 540 |
+
*index += ReadPackedPrimitives<int64_t, WireFormatLite::TYPE_INT32>(
|
| 541 |
+
buf, buf_size, *index, stride, data);
|
| 542 |
+
return OkStatus();
|
| 543 |
+
case DataType::DT_INT32:
|
| 544 |
+
*index += ReadPackedPrimitives<int32, WireFormatLite::TYPE_INT32>(
|
| 545 |
+
buf, buf_size, *index, stride, data);
|
| 546 |
+
return OkStatus();
|
| 547 |
+
default:
|
| 548 |
+
return errors::DataLoss("Failed reading TYPE_INT32 for ",
|
| 549 |
+
DataTypeString(dtype));
|
| 550 |
+
}
|
| 551 |
+
case WireFormatLite::TYPE_FIXED64:
|
| 552 |
+
*index += ReadPackedPrimitives<uint64, WireFormatLite::TYPE_FIXED64>(
|
| 553 |
+
buf, buf_size, *index, stride, data);
|
| 554 |
+
return OkStatus();
|
| 555 |
+
case WireFormatLite::TYPE_FIXED32:
|
| 556 |
+
switch (dtype) {
|
| 557 |
+
case DataType::DT_UINT64:
|
| 558 |
+
*index += ReadPackedPrimitives<uint64, WireFormatLite::TYPE_FIXED32>(
|
| 559 |
+
buf, buf_size, *index, stride, data);
|
| 560 |
+
return OkStatus();
|
| 561 |
+
case DataType::DT_UINT32:
|
| 562 |
+
*index += ReadPackedPrimitives<uint32, WireFormatLite::TYPE_FIXED32>(
|
| 563 |
+
buf, buf_size, *index, stride, data);
|
| 564 |
+
return OkStatus();
|
| 565 |
+
default:
|
| 566 |
+
return errors::DataLoss("Failed reading TYPE_FIXED32 for ",
|
| 567 |
+
DataTypeString(dtype));
|
| 568 |
+
}
|
| 569 |
+
case WireFormatLite::TYPE_BOOL:
|
| 570 |
+
*index += ReadPackedPrimitives<bool, WireFormatLite::TYPE_BOOL>(
|
| 571 |
+
buf, buf_size, *index, stride, data);
|
| 572 |
+
return OkStatus();
|
| 573 |
+
case WireFormatLite::TYPE_STRING:
|
| 574 |
+
case WireFormatLite::TYPE_GROUP:
|
| 575 |
+
case WireFormatLite::TYPE_MESSAGE:
|
| 576 |
+
case WireFormatLite::TYPE_BYTES:
|
| 577 |
+
return errors::DataLoss("Non-primitive type encountered as packed");
|
| 578 |
+
case WireFormatLite::TYPE_UINT32:
|
| 579 |
+
switch (dtype) {
|
| 580 |
+
case DataType::DT_UINT64:
|
| 581 |
+
*index += ReadPackedPrimitives<uint64, WireFormatLite::TYPE_UINT32>(
|
| 582 |
+
buf, buf_size, *index, stride, data);
|
| 583 |
+
return OkStatus();
|
| 584 |
+
case DataType::DT_UINT32:
|
| 585 |
+
*index += ReadPackedPrimitives<uint32, WireFormatLite::TYPE_UINT32>(
|
| 586 |
+
buf, buf_size, *index, stride, data);
|
| 587 |
+
return OkStatus();
|
| 588 |
+
default:
|
| 589 |
+
return errors::DataLoss("Failed reading TYPE_UINT32 for ",
|
| 590 |
+
DataTypeString(dtype));
|
| 591 |
+
}
|
| 592 |
+
case WireFormatLite::TYPE_ENUM:
|
| 593 |
+
*index += ReadPackedPrimitives<int32, WireFormatLite::TYPE_ENUM>(
|
| 594 |
+
buf, buf_size, *index, stride, data);
|
| 595 |
+
return OkStatus();
|
| 596 |
+
case WireFormatLite::TYPE_SFIXED32:
|
| 597 |
+
switch (dtype) {
|
| 598 |
+
case DataType::DT_INT64:
|
| 599 |
+
*index +=
|
| 600 |
+
ReadPackedPrimitives<int64_t, WireFormatLite::TYPE_SFIXED32>(
|
| 601 |
+
buf, buf_size, *index, stride, data);
|
| 602 |
+
return OkStatus();
|
| 603 |
+
case DataType::DT_INT32:
|
| 604 |
+
*index += ReadPackedPrimitives<int32, WireFormatLite::TYPE_SFIXED32>(
|
| 605 |
+
buf, buf_size, *index, stride, data);
|
| 606 |
+
return OkStatus();
|
| 607 |
+
default:
|
| 608 |
+
return errors::DataLoss("Failed reading TYPE_INT32 for ",
|
| 609 |
+
DataTypeString(dtype));
|
| 610 |
+
}
|
| 611 |
+
case WireFormatLite::TYPE_SFIXED64:
|
| 612 |
+
*index += ReadPackedPrimitives<int64_t, WireFormatLite::TYPE_SFIXED64>(
|
| 613 |
+
buf, buf_size, *index, stride, data);
|
| 614 |
+
return OkStatus();
|
| 615 |
+
|
| 616 |
+
case WireFormatLite::TYPE_SINT32:
|
| 617 |
+
switch (dtype) {
|
| 618 |
+
case DataType::DT_INT64:
|
| 619 |
+
*index += ReadPackedPrimitives<int64_t, WireFormatLite::TYPE_SINT32>(
|
| 620 |
+
buf, buf_size, *index, stride, data);
|
| 621 |
+
return OkStatus();
|
| 622 |
+
case DataType::DT_INT32:
|
| 623 |
+
*index += ReadPackedPrimitives<int32, WireFormatLite::TYPE_SINT32>(
|
| 624 |
+
buf, buf_size, *index, stride, data);
|
| 625 |
+
return OkStatus();
|
| 626 |
+
default:
|
| 627 |
+
return errors::DataLoss("Failed reading TYPE_SINT32 for ",
|
| 628 |
+
DataTypeString(dtype));
|
| 629 |
+
}
|
| 630 |
+
case WireFormatLite::TYPE_SINT64:
|
| 631 |
+
*index += ReadPackedPrimitives<int64_t, WireFormatLite::TYPE_SINT64>(
|
| 632 |
+
buf, buf_size, *index, stride, data);
|
| 633 |
+
return OkStatus();
|
| 634 |
+
// default: intentionally omitted in order to enable static checking.
|
| 635 |
+
}
|
| 636 |
+
// Unreachable.
|
| 637 |
+
return errors::DataLoss("Failed reading unknown wire type");
|
| 638 |
+
}
|
| 639 |
+
|
| 640 |
+
// Reads a varint from the given buffer, write it to *value, and return the
|
| 641 |
+
// new buffer pointer.
|
| 642 |
+
// This was copied from coded_stream.cc where it is private.
|
| 643 |
+
// Important: This routine may read as much as kMaxVarintBytes from
|
| 644 |
+
// the buffer. It is the caller's responsibility to make sure that there is
|
| 645 |
+
// enough space in the buffer.
|
| 646 |
+
inline const uint8* ReadVarint64FromArray(const uint8* buffer, bool* ok,
|
| 647 |
+
uint64* value) {
|
| 648 |
+
const uint8* ptr = buffer;
|
| 649 |
+
uint32 b;
|
| 650 |
+
|
| 651 |
+
// Splitting into 32-bit pieces gives better performance on 32-bit
|
| 652 |
+
// processors.
|
| 653 |
+
uint32 part0 = 0, part1 = 0, part2 = 0;
|
| 654 |
+
|
| 655 |
+
b = *(ptr++);
|
| 656 |
+
part0 = b;
|
| 657 |
+
if (!(b & 0x80)) goto done;
|
| 658 |
+
part0 -= 0x80;
|
| 659 |
+
b = *(ptr++);
|
| 660 |
+
part0 += b << 7;
|
| 661 |
+
if (!(b & 0x80)) goto done;
|
| 662 |
+
part0 -= 0x80 << 7;
|
| 663 |
+
b = *(ptr++);
|
| 664 |
+
part0 += b << 14;
|
| 665 |
+
if (!(b & 0x80)) goto done;
|
| 666 |
+
part0 -= 0x80 << 14;
|
| 667 |
+
b = *(ptr++);
|
| 668 |
+
part0 += b << 21;
|
| 669 |
+
if (!(b & 0x80)) goto done;
|
| 670 |
+
part0 -= 0x80 << 21;
|
| 671 |
+
b = *(ptr++);
|
| 672 |
+
part1 = b;
|
| 673 |
+
if (!(b & 0x80)) goto done;
|
| 674 |
+
part1 -= 0x80;
|
| 675 |
+
b = *(ptr++);
|
| 676 |
+
part1 += b << 7;
|
| 677 |
+
if (!(b & 0x80)) goto done;
|
| 678 |
+
part1 -= 0x80 << 7;
|
| 679 |
+
b = *(ptr++);
|
| 680 |
+
part1 += b << 14;
|
| 681 |
+
if (!(b & 0x80)) goto done;
|
| 682 |
+
part1 -= 0x80 << 14;
|
| 683 |
+
b = *(ptr++);
|
| 684 |
+
part1 += b << 21;
|
| 685 |
+
if (!(b & 0x80)) goto done;
|
| 686 |
+
part1 -= 0x80 << 21;
|
| 687 |
+
b = *(ptr++);
|
| 688 |
+
part2 = b;
|
| 689 |
+
if (!(b & 0x80)) goto done;
|
| 690 |
+
part2 -= 0x80;
|
| 691 |
+
b = *(ptr++);
|
| 692 |
+
part2 += b << 7;
|
| 693 |
+
if (!(b & 0x80)) goto done;
|
| 694 |
+
// "part2 -= 0x80 << 7" is irrelevant because (0x80 << 7) << 56 is 0.
|
| 695 |
+
|
| 696 |
+
// We have overrun the maximum size of a varint (10 bytes). Assume
|
| 697 |
+
// the data is corrupt.
|
| 698 |
+
*ok = false;
|
| 699 |
+
return ptr;
|
| 700 |
+
|
| 701 |
+
done:
|
| 702 |
+
*ok = true;
|
| 703 |
+
*value = (static_cast<uint64>(part0)) | (static_cast<uint64>(part1) << 28) |
|
| 704 |
+
(static_cast<uint64>(part2) << 56);
|
| 705 |
+
return ptr;
|
| 706 |
+
}
|
| 707 |
+
|
| 708 |
+
} // namespace internal
|
| 709 |
+
} // namespace tensorflow
|
| 710 |
+
|
| 711 |
+
#endif // TENSORFLOW_CORE_UTIL_PROTO_DECODE_H_
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/proto/descriptor_pool_registry.h
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
|
| 16 |
+
#ifndef TENSORFLOW_CORE_UTIL_PROTO_DESCRIPTOR_POOL_REGISTRY_H_
|
| 17 |
+
#define TENSORFLOW_CORE_UTIL_PROTO_DESCRIPTOR_POOL_REGISTRY_H_
|
| 18 |
+
|
| 19 |
+
#include <functional>
|
| 20 |
+
#include <memory>
|
| 21 |
+
#include <string>
|
| 22 |
+
#include <unordered_map>
|
| 23 |
+
#include <utility>
|
| 24 |
+
|
| 25 |
+
#include "tensorflow/core/lib/core/status.h"
|
| 26 |
+
#include "tensorflow/core/lib/hash/hash.h"
|
| 27 |
+
#include "tensorflow/core/platform/protobuf.h"
|
| 28 |
+
|
| 29 |
+
namespace tensorflow {
|
| 30 |
+
|
| 31 |
+
class DescriptorPoolRegistry {
|
| 32 |
+
public:
|
| 33 |
+
typedef std::function<Status(
|
| 34 |
+
tensorflow::protobuf::DescriptorPool const** desc_pool,
|
| 35 |
+
std::unique_ptr<tensorflow::protobuf::DescriptorPool>* owned_desc_pool)>
|
| 36 |
+
DescriptorPoolFn;
|
| 37 |
+
|
| 38 |
+
// Returns a pointer to a global DescriptorPoolRegistry object.
|
| 39 |
+
static DescriptorPoolRegistry* Global();
|
| 40 |
+
|
| 41 |
+
// Returns a pointer to a descriptor pool function for the given source.
|
| 42 |
+
DescriptorPoolFn* Get(const string& source);
|
| 43 |
+
|
| 44 |
+
// Registers a descriptor pool factory.
|
| 45 |
+
void Register(const string& source, const DescriptorPoolFn& pool_fn);
|
| 46 |
+
|
| 47 |
+
private:
|
| 48 |
+
std::map<string, DescriptorPoolFn> fns_;
|
| 49 |
+
};
|
| 50 |
+
|
| 51 |
+
namespace descriptor_pool_registration {
|
| 52 |
+
|
| 53 |
+
class DescriptorPoolRegistration {
|
| 54 |
+
public:
|
| 55 |
+
DescriptorPoolRegistration(
|
| 56 |
+
const string& source,
|
| 57 |
+
const DescriptorPoolRegistry::DescriptorPoolFn& pool_fn) {
|
| 58 |
+
DescriptorPoolRegistry::Global()->Register(source, pool_fn);
|
| 59 |
+
}
|
| 60 |
+
};
|
| 61 |
+
|
| 62 |
+
} // namespace descriptor_pool_registration
|
| 63 |
+
|
| 64 |
+
#define REGISTER_DESCRIPTOR_POOL(source, pool_fn) \
|
| 65 |
+
REGISTER_DESCRIPTOR_POOL_UNIQ_HELPER(__COUNTER__, source, pool_fn)
|
| 66 |
+
|
| 67 |
+
#define REGISTER_DESCRIPTOR_POOL_UNIQ_HELPER(ctr, source, pool_fn) \
|
| 68 |
+
REGISTER_DESCRIPTOR_POOL_UNIQ(ctr, source, pool_fn)
|
| 69 |
+
|
| 70 |
+
#define REGISTER_DESCRIPTOR_POOL_UNIQ(ctr, source, pool_fn) \
|
| 71 |
+
static descriptor_pool_registration::DescriptorPoolRegistration \
|
| 72 |
+
descriptor_pool_registration_fn_##ctr(source, pool_fn)
|
| 73 |
+
|
| 74 |
+
} // namespace tensorflow
|
| 75 |
+
|
| 76 |
+
#endif // TENSORFLOW_CORE_UTIL_PROTO_DESCRIPTOR_POOL_REGISTRY_H_
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/proto/descriptors.h
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
|
| 16 |
+
#ifndef TENSORFLOW_CORE_UTIL_PROTO_DESCRIPTORS_H_
|
| 17 |
+
#define TENSORFLOW_CORE_UTIL_PROTO_DESCRIPTORS_H_
|
| 18 |
+
|
| 19 |
+
#include <memory>
|
| 20 |
+
#include <string>
|
| 21 |
+
|
| 22 |
+
#include "tensorflow/core/platform/protobuf.h"
|
| 23 |
+
#include "tensorflow/core/platform/status.h"
|
| 24 |
+
|
| 25 |
+
namespace tsl {
|
| 26 |
+
class Env;
|
| 27 |
+
} // namespace tsl
|
| 28 |
+
namespace tensorflow {
|
| 29 |
+
using tsl::Env;
|
| 30 |
+
|
| 31 |
+
// Gets a `DescriptorPool` object from the `descriptor_source`. This may be:
|
| 32 |
+
//
|
| 33 |
+
// 1) An empty string or "local://", in which case the local descriptor pool
|
| 34 |
+
// created for proto definitions linked to the binary is returned.
|
| 35 |
+
//
|
| 36 |
+
// 2) A file path, in which case the descriptor pool is created from the
|
| 37 |
+
// contents of the file, which is expected to contain a `FileDescriptorSet`
|
| 38 |
+
// serialized as a string. The descriptor pool ownership is transferred to the
|
| 39 |
+
// caller via `owned_desc_pool`.
|
| 40 |
+
//
|
| 41 |
+
// 3) A "bytes://<bytes>", in which case the descriptor pool is created from
|
| 42 |
+
// `<bytes>`, which is expected to be a `FileDescriptorSet` serialized as a
|
| 43 |
+
// string. The descriptor pool ownership is transferred to the caller via
|
| 44 |
+
// `owned_desc_pool`.
|
| 45 |
+
//
|
| 46 |
+
// Custom schemas can be supported by registering a handler with the
|
| 47 |
+
// `DescriptorPoolRegistry`.
|
| 48 |
+
Status GetDescriptorPool(
|
| 49 |
+
Env* env, string const& descriptor_source,
|
| 50 |
+
protobuf::DescriptorPool const** desc_pool,
|
| 51 |
+
std::unique_ptr<protobuf::DescriptorPool>* owned_desc_pool);
|
| 52 |
+
|
| 53 |
+
} // namespace tensorflow
|
| 54 |
+
|
| 55 |
+
#endif // TENSORFLOW_CORE_UTIL_PROTO_DESCRIPTORS_H_
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/proto/proto_utils.h
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
|
| 16 |
+
#ifndef TENSORFLOW_CORE_UTIL_PROTO_PROTO_UTILS_H_
|
| 17 |
+
#define TENSORFLOW_CORE_UTIL_PROTO_PROTO_UTILS_H_
|
| 18 |
+
|
| 19 |
+
#include "absl/strings/string_view.h"
|
| 20 |
+
#include "tensorflow/core/framework/types.h"
|
| 21 |
+
#include "tensorflow/core/lib/core/status.h"
|
| 22 |
+
#include "tensorflow/core/platform/protobuf.h"
|
| 23 |
+
#include "tsl/util/proto/proto_utils.h"
|
| 24 |
+
|
| 25 |
+
namespace tensorflow {
|
| 26 |
+
namespace proto_utils {
|
| 27 |
+
|
| 28 |
+
using tensorflow::protobuf::FieldDescriptor;
|
| 29 |
+
// NOLINTBEGIN(misc-unused-using-decls)
|
| 30 |
+
using tsl::proto_utils::FromDurationProto;
|
| 31 |
+
using tsl::proto_utils::ToDurationProto;
|
| 32 |
+
// NOLINTEND(misc-unused-using-decls)
|
| 33 |
+
|
| 34 |
+
// Returns true if the proto field type can be converted to the tensor dtype.
|
| 35 |
+
bool IsCompatibleType(FieldDescriptor::Type field_type, DataType dtype);
|
| 36 |
+
|
| 37 |
+
// Parses a text-formatted protobuf from a string into the given Message* output
|
| 38 |
+
// and returns status OK if valid, or INVALID_ARGUMENT with an accompanying
|
| 39 |
+
// parser error message if the text format is invalid.
|
| 40 |
+
Status ParseTextFormatFromString(absl::string_view input,
|
| 41 |
+
protobuf::Message* output);
|
| 42 |
+
|
| 43 |
+
class StringErrorCollector : public protobuf::io::ErrorCollector {
|
| 44 |
+
public:
|
| 45 |
+
// String error_text is unowned and must remain valid during the use of
|
| 46 |
+
// StringErrorCollector.
|
| 47 |
+
explicit StringErrorCollector(string* error_text);
|
| 48 |
+
// If one_indexing is set to true, all line and column numbers will be
|
| 49 |
+
// increased by one for cases when provided indices are 0-indexed and
|
| 50 |
+
// 1-indexed error messages are desired
|
| 51 |
+
StringErrorCollector(string* error_text, bool one_indexing);
|
| 52 |
+
StringErrorCollector(const StringErrorCollector&) = delete;
|
| 53 |
+
StringErrorCollector& operator=(const StringErrorCollector&) = delete;
|
| 54 |
+
|
| 55 |
+
// Implementation of protobuf::io::ErrorCollector::AddError.
|
| 56 |
+
void AddError(int line, int column, const string& message) override;
|
| 57 |
+
|
| 58 |
+
// Implementation of protobuf::io::ErrorCollector::AddWarning.
|
| 59 |
+
void AddWarning(int line, int column, const string& message) override;
|
| 60 |
+
|
| 61 |
+
private:
|
| 62 |
+
string* const error_text_;
|
| 63 |
+
const int index_offset_;
|
| 64 |
+
};
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
} // namespace proto_utils
|
| 68 |
+
} // namespace tensorflow
|
| 69 |
+
|
| 70 |
+
#endif // TENSORFLOW_CORE_UTIL_PROTO_PROTO_UTILS_H_
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/quantization/uniform_quant_ops_attr.pb.h
ADDED
|
@@ -0,0 +1,618 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Generated by the protocol buffer compiler. DO NOT EDIT!
|
| 2 |
+
// source: tensorflow/core/util/quantization/uniform_quant_ops_attr.proto
|
| 3 |
+
|
| 4 |
+
#ifndef GOOGLE_PROTOBUF_INCLUDED_tensorflow_2fcore_2futil_2fquantization_2funiform_5fquant_5fops_5fattr_2eproto
|
| 5 |
+
#define GOOGLE_PROTOBUF_INCLUDED_tensorflow_2fcore_2futil_2fquantization_2funiform_5fquant_5fops_5fattr_2eproto
|
| 6 |
+
|
| 7 |
+
#include <limits>
|
| 8 |
+
#include <string>
|
| 9 |
+
|
| 10 |
+
#include <google/protobuf/port_def.inc>
|
| 11 |
+
#if PROTOBUF_VERSION < 3021000
|
| 12 |
+
#error This file was generated by a newer version of protoc which is
|
| 13 |
+
#error incompatible with your Protocol Buffer headers. Please update
|
| 14 |
+
#error your headers.
|
| 15 |
+
#endif
|
| 16 |
+
#if 3021009 < PROTOBUF_MIN_PROTOC_VERSION
|
| 17 |
+
#error This file was generated by an older version of protoc which is
|
| 18 |
+
#error incompatible with your Protocol Buffer headers. Please
|
| 19 |
+
#error regenerate this file with a newer version of protoc.
|
| 20 |
+
#endif
|
| 21 |
+
|
| 22 |
+
#include <google/protobuf/port_undef.inc>
|
| 23 |
+
#include <google/protobuf/io/coded_stream.h>
|
| 24 |
+
#include <google/protobuf/arena.h>
|
| 25 |
+
#include <google/protobuf/arenastring.h>
|
| 26 |
+
#include <google/protobuf/generated_message_util.h>
|
| 27 |
+
#include <google/protobuf/metadata_lite.h>
|
| 28 |
+
#include <google/protobuf/generated_message_reflection.h>
|
| 29 |
+
#include <google/protobuf/message.h>
|
| 30 |
+
#include <google/protobuf/repeated_field.h> // IWYU pragma: export
|
| 31 |
+
#include <google/protobuf/extension_set.h> // IWYU pragma: export
|
| 32 |
+
#include <google/protobuf/unknown_field_set.h>
|
| 33 |
+
// @@protoc_insertion_point(includes)
|
| 34 |
+
#include <google/protobuf/port_def.inc>
|
| 35 |
+
#define PROTOBUF_INTERNAL_EXPORT_tensorflow_2fcore_2futil_2fquantization_2funiform_5fquant_5fops_5fattr_2eproto
|
| 36 |
+
PROTOBUF_NAMESPACE_OPEN
|
| 37 |
+
namespace internal {
|
| 38 |
+
class AnyMetadata;
|
| 39 |
+
} // namespace internal
|
| 40 |
+
PROTOBUF_NAMESPACE_CLOSE
|
| 41 |
+
|
| 42 |
+
// Internal implementation detail -- do not use these members.
|
| 43 |
+
struct TableStruct_tensorflow_2fcore_2futil_2fquantization_2funiform_5fquant_5fops_5fattr_2eproto {
|
| 44 |
+
static const uint32_t offsets[];
|
| 45 |
+
};
|
| 46 |
+
extern const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_tensorflow_2fcore_2futil_2fquantization_2funiform_5fquant_5fops_5fattr_2eproto;
|
| 47 |
+
namespace tensorflow {
|
| 48 |
+
class UniformQuantizedConvolutionDimensionNumbersAttr;
|
| 49 |
+
struct UniformQuantizedConvolutionDimensionNumbersAttrDefaultTypeInternal;
|
| 50 |
+
extern UniformQuantizedConvolutionDimensionNumbersAttrDefaultTypeInternal _UniformQuantizedConvolutionDimensionNumbersAttr_default_instance_;
|
| 51 |
+
} // namespace tensorflow
|
| 52 |
+
PROTOBUF_NAMESPACE_OPEN
|
| 53 |
+
template<> ::tensorflow::UniformQuantizedConvolutionDimensionNumbersAttr* Arena::CreateMaybeMessage<::tensorflow::UniformQuantizedConvolutionDimensionNumbersAttr>(Arena*);
|
| 54 |
+
PROTOBUF_NAMESPACE_CLOSE
|
| 55 |
+
namespace tensorflow {
|
| 56 |
+
|
| 57 |
+
// ===================================================================
|
| 58 |
+
|
| 59 |
+
class UniformQuantizedConvolutionDimensionNumbersAttr final :
|
| 60 |
+
public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:tensorflow.UniformQuantizedConvolutionDimensionNumbersAttr) */ {
|
| 61 |
+
public:
|
| 62 |
+
inline UniformQuantizedConvolutionDimensionNumbersAttr() : UniformQuantizedConvolutionDimensionNumbersAttr(nullptr) {}
|
| 63 |
+
~UniformQuantizedConvolutionDimensionNumbersAttr() override;
|
| 64 |
+
explicit PROTOBUF_CONSTEXPR UniformQuantizedConvolutionDimensionNumbersAttr(::PROTOBUF_NAMESPACE_ID::internal::ConstantInitialized);
|
| 65 |
+
|
| 66 |
+
UniformQuantizedConvolutionDimensionNumbersAttr(const UniformQuantizedConvolutionDimensionNumbersAttr& from);
|
| 67 |
+
UniformQuantizedConvolutionDimensionNumbersAttr(UniformQuantizedConvolutionDimensionNumbersAttr&& from) noexcept
|
| 68 |
+
: UniformQuantizedConvolutionDimensionNumbersAttr() {
|
| 69 |
+
*this = ::std::move(from);
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
inline UniformQuantizedConvolutionDimensionNumbersAttr& operator=(const UniformQuantizedConvolutionDimensionNumbersAttr& from) {
|
| 73 |
+
CopyFrom(from);
|
| 74 |
+
return *this;
|
| 75 |
+
}
|
| 76 |
+
inline UniformQuantizedConvolutionDimensionNumbersAttr& operator=(UniformQuantizedConvolutionDimensionNumbersAttr&& from) noexcept {
|
| 77 |
+
if (this == &from) return *this;
|
| 78 |
+
if (GetOwningArena() == from.GetOwningArena()
|
| 79 |
+
#ifdef PROTOBUF_FORCE_COPY_IN_MOVE
|
| 80 |
+
&& GetOwningArena() != nullptr
|
| 81 |
+
#endif // !PROTOBUF_FORCE_COPY_IN_MOVE
|
| 82 |
+
) {
|
| 83 |
+
InternalSwap(&from);
|
| 84 |
+
} else {
|
| 85 |
+
CopyFrom(from);
|
| 86 |
+
}
|
| 87 |
+
return *this;
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() {
|
| 91 |
+
return GetDescriptor();
|
| 92 |
+
}
|
| 93 |
+
static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() {
|
| 94 |
+
return default_instance().GetMetadata().descriptor;
|
| 95 |
+
}
|
| 96 |
+
static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() {
|
| 97 |
+
return default_instance().GetMetadata().reflection;
|
| 98 |
+
}
|
| 99 |
+
static const UniformQuantizedConvolutionDimensionNumbersAttr& default_instance() {
|
| 100 |
+
return *internal_default_instance();
|
| 101 |
+
}
|
| 102 |
+
static inline const UniformQuantizedConvolutionDimensionNumbersAttr* internal_default_instance() {
|
| 103 |
+
return reinterpret_cast<const UniformQuantizedConvolutionDimensionNumbersAttr*>(
|
| 104 |
+
&_UniformQuantizedConvolutionDimensionNumbersAttr_default_instance_);
|
| 105 |
+
}
|
| 106 |
+
static constexpr int kIndexInFileMessages =
|
| 107 |
+
0;
|
| 108 |
+
|
| 109 |
+
friend void swap(UniformQuantizedConvolutionDimensionNumbersAttr& a, UniformQuantizedConvolutionDimensionNumbersAttr& b) {
|
| 110 |
+
a.Swap(&b);
|
| 111 |
+
}
|
| 112 |
+
inline void Swap(UniformQuantizedConvolutionDimensionNumbersAttr* other) {
|
| 113 |
+
if (other == this) return;
|
| 114 |
+
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
|
| 115 |
+
if (GetOwningArena() != nullptr &&
|
| 116 |
+
GetOwningArena() == other->GetOwningArena()) {
|
| 117 |
+
#else // PROTOBUF_FORCE_COPY_IN_SWAP
|
| 118 |
+
if (GetOwningArena() == other->GetOwningArena()) {
|
| 119 |
+
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
|
| 120 |
+
InternalSwap(other);
|
| 121 |
+
} else {
|
| 122 |
+
::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other);
|
| 123 |
+
}
|
| 124 |
+
}
|
| 125 |
+
void UnsafeArenaSwap(UniformQuantizedConvolutionDimensionNumbersAttr* other) {
|
| 126 |
+
if (other == this) return;
|
| 127 |
+
GOOGLE_DCHECK(GetOwningArena() == other->GetOwningArena());
|
| 128 |
+
InternalSwap(other);
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
// implements Message ----------------------------------------------
|
| 132 |
+
|
| 133 |
+
UniformQuantizedConvolutionDimensionNumbersAttr* New(::PROTOBUF_NAMESPACE_ID::Arena* arena = nullptr) const final {
|
| 134 |
+
return CreateMaybeMessage<UniformQuantizedConvolutionDimensionNumbersAttr>(arena);
|
| 135 |
+
}
|
| 136 |
+
using ::PROTOBUF_NAMESPACE_ID::Message::CopyFrom;
|
| 137 |
+
void CopyFrom(const UniformQuantizedConvolutionDimensionNumbersAttr& from);
|
| 138 |
+
using ::PROTOBUF_NAMESPACE_ID::Message::MergeFrom;
|
| 139 |
+
void MergeFrom( const UniformQuantizedConvolutionDimensionNumbersAttr& from) {
|
| 140 |
+
UniformQuantizedConvolutionDimensionNumbersAttr::MergeImpl(*this, from);
|
| 141 |
+
}
|
| 142 |
+
private:
|
| 143 |
+
static void MergeImpl(::PROTOBUF_NAMESPACE_ID::Message& to_msg, const ::PROTOBUF_NAMESPACE_ID::Message& from_msg);
|
| 144 |
+
public:
|
| 145 |
+
PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final;
|
| 146 |
+
bool IsInitialized() const final;
|
| 147 |
+
|
| 148 |
+
size_t ByteSizeLong() const final;
|
| 149 |
+
const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final;
|
| 150 |
+
uint8_t* _InternalSerialize(
|
| 151 |
+
uint8_t* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final;
|
| 152 |
+
int GetCachedSize() const final { return _impl_._cached_size_.Get(); }
|
| 153 |
+
|
| 154 |
+
private:
|
| 155 |
+
void SharedCtor(::PROTOBUF_NAMESPACE_ID::Arena* arena, bool is_message_owned);
|
| 156 |
+
void SharedDtor();
|
| 157 |
+
void SetCachedSize(int size) const final;
|
| 158 |
+
void InternalSwap(UniformQuantizedConvolutionDimensionNumbersAttr* other);
|
| 159 |
+
|
| 160 |
+
private:
|
| 161 |
+
friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata;
|
| 162 |
+
static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() {
|
| 163 |
+
return "tensorflow.UniformQuantizedConvolutionDimensionNumbersAttr";
|
| 164 |
+
}
|
| 165 |
+
protected:
|
| 166 |
+
explicit UniformQuantizedConvolutionDimensionNumbersAttr(::PROTOBUF_NAMESPACE_ID::Arena* arena,
|
| 167 |
+
bool is_message_owned = false);
|
| 168 |
+
public:
|
| 169 |
+
|
| 170 |
+
static const ClassData _class_data_;
|
| 171 |
+
const ::PROTOBUF_NAMESPACE_ID::Message::ClassData*GetClassData() const final;
|
| 172 |
+
|
| 173 |
+
::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final;
|
| 174 |
+
|
| 175 |
+
// nested types ----------------------------------------------------
|
| 176 |
+
|
| 177 |
+
// accessors -------------------------------------------------------
|
| 178 |
+
|
| 179 |
+
enum : int {
|
| 180 |
+
kInputSpatialDimensionsFieldNumber = 3,
|
| 181 |
+
kKernelSpatialDimensionsFieldNumber = 6,
|
| 182 |
+
kOutputSpatialDimensionsFieldNumber = 9,
|
| 183 |
+
kInputBatchDimensionFieldNumber = 1,
|
| 184 |
+
kInputFeatureDimensionFieldNumber = 2,
|
| 185 |
+
kKernelInputFeatureDimensionFieldNumber = 4,
|
| 186 |
+
kKernelOutputFeatureDimensionFieldNumber = 5,
|
| 187 |
+
kOutputBatchDimensionFieldNumber = 7,
|
| 188 |
+
kOutputFeatureDimensionFieldNumber = 8,
|
| 189 |
+
};
|
| 190 |
+
// repeated int64 input_spatial_dimensions = 3;
|
| 191 |
+
int input_spatial_dimensions_size() const;
|
| 192 |
+
private:
|
| 193 |
+
int _internal_input_spatial_dimensions_size() const;
|
| 194 |
+
public:
|
| 195 |
+
void clear_input_spatial_dimensions();
|
| 196 |
+
private:
|
| 197 |
+
int64_t _internal_input_spatial_dimensions(int index) const;
|
| 198 |
+
const ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >&
|
| 199 |
+
_internal_input_spatial_dimensions() const;
|
| 200 |
+
void _internal_add_input_spatial_dimensions(int64_t value);
|
| 201 |
+
::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >*
|
| 202 |
+
_internal_mutable_input_spatial_dimensions();
|
| 203 |
+
public:
|
| 204 |
+
int64_t input_spatial_dimensions(int index) const;
|
| 205 |
+
void set_input_spatial_dimensions(int index, int64_t value);
|
| 206 |
+
void add_input_spatial_dimensions(int64_t value);
|
| 207 |
+
const ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >&
|
| 208 |
+
input_spatial_dimensions() const;
|
| 209 |
+
::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >*
|
| 210 |
+
mutable_input_spatial_dimensions();
|
| 211 |
+
|
| 212 |
+
// repeated int64 kernel_spatial_dimensions = 6;
|
| 213 |
+
int kernel_spatial_dimensions_size() const;
|
| 214 |
+
private:
|
| 215 |
+
int _internal_kernel_spatial_dimensions_size() const;
|
| 216 |
+
public:
|
| 217 |
+
void clear_kernel_spatial_dimensions();
|
| 218 |
+
private:
|
| 219 |
+
int64_t _internal_kernel_spatial_dimensions(int index) const;
|
| 220 |
+
const ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >&
|
| 221 |
+
_internal_kernel_spatial_dimensions() const;
|
| 222 |
+
void _internal_add_kernel_spatial_dimensions(int64_t value);
|
| 223 |
+
::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >*
|
| 224 |
+
_internal_mutable_kernel_spatial_dimensions();
|
| 225 |
+
public:
|
| 226 |
+
int64_t kernel_spatial_dimensions(int index) const;
|
| 227 |
+
void set_kernel_spatial_dimensions(int index, int64_t value);
|
| 228 |
+
void add_kernel_spatial_dimensions(int64_t value);
|
| 229 |
+
const ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >&
|
| 230 |
+
kernel_spatial_dimensions() const;
|
| 231 |
+
::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >*
|
| 232 |
+
mutable_kernel_spatial_dimensions();
|
| 233 |
+
|
| 234 |
+
// repeated int64 output_spatial_dimensions = 9;
|
| 235 |
+
int output_spatial_dimensions_size() const;
|
| 236 |
+
private:
|
| 237 |
+
int _internal_output_spatial_dimensions_size() const;
|
| 238 |
+
public:
|
| 239 |
+
void clear_output_spatial_dimensions();
|
| 240 |
+
private:
|
| 241 |
+
int64_t _internal_output_spatial_dimensions(int index) const;
|
| 242 |
+
const ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >&
|
| 243 |
+
_internal_output_spatial_dimensions() const;
|
| 244 |
+
void _internal_add_output_spatial_dimensions(int64_t value);
|
| 245 |
+
::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >*
|
| 246 |
+
_internal_mutable_output_spatial_dimensions();
|
| 247 |
+
public:
|
| 248 |
+
int64_t output_spatial_dimensions(int index) const;
|
| 249 |
+
void set_output_spatial_dimensions(int index, int64_t value);
|
| 250 |
+
void add_output_spatial_dimensions(int64_t value);
|
| 251 |
+
const ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >&
|
| 252 |
+
output_spatial_dimensions() const;
|
| 253 |
+
::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >*
|
| 254 |
+
mutable_output_spatial_dimensions();
|
| 255 |
+
|
| 256 |
+
// int64 input_batch_dimension = 1;
|
| 257 |
+
void clear_input_batch_dimension();
|
| 258 |
+
int64_t input_batch_dimension() const;
|
| 259 |
+
void set_input_batch_dimension(int64_t value);
|
| 260 |
+
private:
|
| 261 |
+
int64_t _internal_input_batch_dimension() const;
|
| 262 |
+
void _internal_set_input_batch_dimension(int64_t value);
|
| 263 |
+
public:
|
| 264 |
+
|
| 265 |
+
// int64 input_feature_dimension = 2;
|
| 266 |
+
void clear_input_feature_dimension();
|
| 267 |
+
int64_t input_feature_dimension() const;
|
| 268 |
+
void set_input_feature_dimension(int64_t value);
|
| 269 |
+
private:
|
| 270 |
+
int64_t _internal_input_feature_dimension() const;
|
| 271 |
+
void _internal_set_input_feature_dimension(int64_t value);
|
| 272 |
+
public:
|
| 273 |
+
|
| 274 |
+
// int64 kernel_input_feature_dimension = 4;
|
| 275 |
+
void clear_kernel_input_feature_dimension();
|
| 276 |
+
int64_t kernel_input_feature_dimension() const;
|
| 277 |
+
void set_kernel_input_feature_dimension(int64_t value);
|
| 278 |
+
private:
|
| 279 |
+
int64_t _internal_kernel_input_feature_dimension() const;
|
| 280 |
+
void _internal_set_kernel_input_feature_dimension(int64_t value);
|
| 281 |
+
public:
|
| 282 |
+
|
| 283 |
+
// int64 kernel_output_feature_dimension = 5;
|
| 284 |
+
void clear_kernel_output_feature_dimension();
|
| 285 |
+
int64_t kernel_output_feature_dimension() const;
|
| 286 |
+
void set_kernel_output_feature_dimension(int64_t value);
|
| 287 |
+
private:
|
| 288 |
+
int64_t _internal_kernel_output_feature_dimension() const;
|
| 289 |
+
void _internal_set_kernel_output_feature_dimension(int64_t value);
|
| 290 |
+
public:
|
| 291 |
+
|
| 292 |
+
// int64 output_batch_dimension = 7;
|
| 293 |
+
void clear_output_batch_dimension();
|
| 294 |
+
int64_t output_batch_dimension() const;
|
| 295 |
+
void set_output_batch_dimension(int64_t value);
|
| 296 |
+
private:
|
| 297 |
+
int64_t _internal_output_batch_dimension() const;
|
| 298 |
+
void _internal_set_output_batch_dimension(int64_t value);
|
| 299 |
+
public:
|
| 300 |
+
|
| 301 |
+
// int64 output_feature_dimension = 8;
|
| 302 |
+
void clear_output_feature_dimension();
|
| 303 |
+
int64_t output_feature_dimension() const;
|
| 304 |
+
void set_output_feature_dimension(int64_t value);
|
| 305 |
+
private:
|
| 306 |
+
int64_t _internal_output_feature_dimension() const;
|
| 307 |
+
void _internal_set_output_feature_dimension(int64_t value);
|
| 308 |
+
public:
|
| 309 |
+
|
| 310 |
+
// @@protoc_insertion_point(class_scope:tensorflow.UniformQuantizedConvolutionDimensionNumbersAttr)
|
| 311 |
+
private:
|
| 312 |
+
class _Internal;
|
| 313 |
+
|
| 314 |
+
template <typename T> friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper;
|
| 315 |
+
typedef void InternalArenaConstructable_;
|
| 316 |
+
typedef void DestructorSkippable_;
|
| 317 |
+
struct Impl_ {
|
| 318 |
+
::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t > input_spatial_dimensions_;
|
| 319 |
+
mutable std::atomic<int> _input_spatial_dimensions_cached_byte_size_;
|
| 320 |
+
::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t > kernel_spatial_dimensions_;
|
| 321 |
+
mutable std::atomic<int> _kernel_spatial_dimensions_cached_byte_size_;
|
| 322 |
+
::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t > output_spatial_dimensions_;
|
| 323 |
+
mutable std::atomic<int> _output_spatial_dimensions_cached_byte_size_;
|
| 324 |
+
int64_t input_batch_dimension_;
|
| 325 |
+
int64_t input_feature_dimension_;
|
| 326 |
+
int64_t kernel_input_feature_dimension_;
|
| 327 |
+
int64_t kernel_output_feature_dimension_;
|
| 328 |
+
int64_t output_batch_dimension_;
|
| 329 |
+
int64_t output_feature_dimension_;
|
| 330 |
+
mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_;
|
| 331 |
+
};
|
| 332 |
+
union { Impl_ _impl_; };
|
| 333 |
+
friend struct ::TableStruct_tensorflow_2fcore_2futil_2fquantization_2funiform_5fquant_5fops_5fattr_2eproto;
|
| 334 |
+
};
|
| 335 |
+
// ===================================================================
|
| 336 |
+
|
| 337 |
+
|
| 338 |
+
// ===================================================================
|
| 339 |
+
|
| 340 |
+
#ifdef __GNUC__
|
| 341 |
+
#pragma GCC diagnostic push
|
| 342 |
+
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
|
| 343 |
+
#endif // __GNUC__
|
| 344 |
+
// UniformQuantizedConvolutionDimensionNumbersAttr
|
| 345 |
+
|
| 346 |
+
// int64 input_batch_dimension = 1;
|
| 347 |
+
inline void UniformQuantizedConvolutionDimensionNumbersAttr::clear_input_batch_dimension() {
|
| 348 |
+
_impl_.input_batch_dimension_ = int64_t{0};
|
| 349 |
+
}
|
| 350 |
+
inline int64_t UniformQuantizedConvolutionDimensionNumbersAttr::_internal_input_batch_dimension() const {
|
| 351 |
+
return _impl_.input_batch_dimension_;
|
| 352 |
+
}
|
| 353 |
+
inline int64_t UniformQuantizedConvolutionDimensionNumbersAttr::input_batch_dimension() const {
|
| 354 |
+
// @@protoc_insertion_point(field_get:tensorflow.UniformQuantizedConvolutionDimensionNumbersAttr.input_batch_dimension)
|
| 355 |
+
return _internal_input_batch_dimension();
|
| 356 |
+
}
|
| 357 |
+
inline void UniformQuantizedConvolutionDimensionNumbersAttr::_internal_set_input_batch_dimension(int64_t value) {
|
| 358 |
+
|
| 359 |
+
_impl_.input_batch_dimension_ = value;
|
| 360 |
+
}
|
| 361 |
+
inline void UniformQuantizedConvolutionDimensionNumbersAttr::set_input_batch_dimension(int64_t value) {
|
| 362 |
+
_internal_set_input_batch_dimension(value);
|
| 363 |
+
// @@protoc_insertion_point(field_set:tensorflow.UniformQuantizedConvolutionDimensionNumbersAttr.input_batch_dimension)
|
| 364 |
+
}
|
| 365 |
+
|
| 366 |
+
// int64 input_feature_dimension = 2;
|
| 367 |
+
inline void UniformQuantizedConvolutionDimensionNumbersAttr::clear_input_feature_dimension() {
|
| 368 |
+
_impl_.input_feature_dimension_ = int64_t{0};
|
| 369 |
+
}
|
| 370 |
+
inline int64_t UniformQuantizedConvolutionDimensionNumbersAttr::_internal_input_feature_dimension() const {
|
| 371 |
+
return _impl_.input_feature_dimension_;
|
| 372 |
+
}
|
| 373 |
+
inline int64_t UniformQuantizedConvolutionDimensionNumbersAttr::input_feature_dimension() const {
|
| 374 |
+
// @@protoc_insertion_point(field_get:tensorflow.UniformQuantizedConvolutionDimensionNumbersAttr.input_feature_dimension)
|
| 375 |
+
return _internal_input_feature_dimension();
|
| 376 |
+
}
|
| 377 |
+
inline void UniformQuantizedConvolutionDimensionNumbersAttr::_internal_set_input_feature_dimension(int64_t value) {
|
| 378 |
+
|
| 379 |
+
_impl_.input_feature_dimension_ = value;
|
| 380 |
+
}
|
| 381 |
+
inline void UniformQuantizedConvolutionDimensionNumbersAttr::set_input_feature_dimension(int64_t value) {
|
| 382 |
+
_internal_set_input_feature_dimension(value);
|
| 383 |
+
// @@protoc_insertion_point(field_set:tensorflow.UniformQuantizedConvolutionDimensionNumbersAttr.input_feature_dimension)
|
| 384 |
+
}
|
| 385 |
+
|
| 386 |
+
// repeated int64 input_spatial_dimensions = 3;
|
| 387 |
+
inline int UniformQuantizedConvolutionDimensionNumbersAttr::_internal_input_spatial_dimensions_size() const {
|
| 388 |
+
return _impl_.input_spatial_dimensions_.size();
|
| 389 |
+
}
|
| 390 |
+
inline int UniformQuantizedConvolutionDimensionNumbersAttr::input_spatial_dimensions_size() const {
|
| 391 |
+
return _internal_input_spatial_dimensions_size();
|
| 392 |
+
}
|
| 393 |
+
inline void UniformQuantizedConvolutionDimensionNumbersAttr::clear_input_spatial_dimensions() {
|
| 394 |
+
_impl_.input_spatial_dimensions_.Clear();
|
| 395 |
+
}
|
| 396 |
+
inline int64_t UniformQuantizedConvolutionDimensionNumbersAttr::_internal_input_spatial_dimensions(int index) const {
|
| 397 |
+
return _impl_.input_spatial_dimensions_.Get(index);
|
| 398 |
+
}
|
| 399 |
+
inline int64_t UniformQuantizedConvolutionDimensionNumbersAttr::input_spatial_dimensions(int index) const {
|
| 400 |
+
// @@protoc_insertion_point(field_get:tensorflow.UniformQuantizedConvolutionDimensionNumbersAttr.input_spatial_dimensions)
|
| 401 |
+
return _internal_input_spatial_dimensions(index);
|
| 402 |
+
}
|
| 403 |
+
inline void UniformQuantizedConvolutionDimensionNumbersAttr::set_input_spatial_dimensions(int index, int64_t value) {
|
| 404 |
+
_impl_.input_spatial_dimensions_.Set(index, value);
|
| 405 |
+
// @@protoc_insertion_point(field_set:tensorflow.UniformQuantizedConvolutionDimensionNumbersAttr.input_spatial_dimensions)
|
| 406 |
+
}
|
| 407 |
+
inline void UniformQuantizedConvolutionDimensionNumbersAttr::_internal_add_input_spatial_dimensions(int64_t value) {
|
| 408 |
+
_impl_.input_spatial_dimensions_.Add(value);
|
| 409 |
+
}
|
| 410 |
+
inline void UniformQuantizedConvolutionDimensionNumbersAttr::add_input_spatial_dimensions(int64_t value) {
|
| 411 |
+
_internal_add_input_spatial_dimensions(value);
|
| 412 |
+
// @@protoc_insertion_point(field_add:tensorflow.UniformQuantizedConvolutionDimensionNumbersAttr.input_spatial_dimensions)
|
| 413 |
+
}
|
| 414 |
+
inline const ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >&
|
| 415 |
+
UniformQuantizedConvolutionDimensionNumbersAttr::_internal_input_spatial_dimensions() const {
|
| 416 |
+
return _impl_.input_spatial_dimensions_;
|
| 417 |
+
}
|
| 418 |
+
inline const ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >&
|
| 419 |
+
UniformQuantizedConvolutionDimensionNumbersAttr::input_spatial_dimensions() const {
|
| 420 |
+
// @@protoc_insertion_point(field_list:tensorflow.UniformQuantizedConvolutionDimensionNumbersAttr.input_spatial_dimensions)
|
| 421 |
+
return _internal_input_spatial_dimensions();
|
| 422 |
+
}
|
| 423 |
+
inline ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >*
|
| 424 |
+
UniformQuantizedConvolutionDimensionNumbersAttr::_internal_mutable_input_spatial_dimensions() {
|
| 425 |
+
return &_impl_.input_spatial_dimensions_;
|
| 426 |
+
}
|
| 427 |
+
inline ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >*
|
| 428 |
+
UniformQuantizedConvolutionDimensionNumbersAttr::mutable_input_spatial_dimensions() {
|
| 429 |
+
// @@protoc_insertion_point(field_mutable_list:tensorflow.UniformQuantizedConvolutionDimensionNumbersAttr.input_spatial_dimensions)
|
| 430 |
+
return _internal_mutable_input_spatial_dimensions();
|
| 431 |
+
}
|
| 432 |
+
|
| 433 |
+
// int64 kernel_input_feature_dimension = 4;
|
| 434 |
+
inline void UniformQuantizedConvolutionDimensionNumbersAttr::clear_kernel_input_feature_dimension() {
|
| 435 |
+
_impl_.kernel_input_feature_dimension_ = int64_t{0};
|
| 436 |
+
}
|
| 437 |
+
inline int64_t UniformQuantizedConvolutionDimensionNumbersAttr::_internal_kernel_input_feature_dimension() const {
|
| 438 |
+
return _impl_.kernel_input_feature_dimension_;
|
| 439 |
+
}
|
| 440 |
+
inline int64_t UniformQuantizedConvolutionDimensionNumbersAttr::kernel_input_feature_dimension() const {
|
| 441 |
+
// @@protoc_insertion_point(field_get:tensorflow.UniformQuantizedConvolutionDimensionNumbersAttr.kernel_input_feature_dimension)
|
| 442 |
+
return _internal_kernel_input_feature_dimension();
|
| 443 |
+
}
|
| 444 |
+
inline void UniformQuantizedConvolutionDimensionNumbersAttr::_internal_set_kernel_input_feature_dimension(int64_t value) {
|
| 445 |
+
|
| 446 |
+
_impl_.kernel_input_feature_dimension_ = value;
|
| 447 |
+
}
|
| 448 |
+
inline void UniformQuantizedConvolutionDimensionNumbersAttr::set_kernel_input_feature_dimension(int64_t value) {
|
| 449 |
+
_internal_set_kernel_input_feature_dimension(value);
|
| 450 |
+
// @@protoc_insertion_point(field_set:tensorflow.UniformQuantizedConvolutionDimensionNumbersAttr.kernel_input_feature_dimension)
|
| 451 |
+
}
|
| 452 |
+
|
| 453 |
+
// int64 kernel_output_feature_dimension = 5;
|
| 454 |
+
inline void UniformQuantizedConvolutionDimensionNumbersAttr::clear_kernel_output_feature_dimension() {
|
| 455 |
+
_impl_.kernel_output_feature_dimension_ = int64_t{0};
|
| 456 |
+
}
|
| 457 |
+
inline int64_t UniformQuantizedConvolutionDimensionNumbersAttr::_internal_kernel_output_feature_dimension() const {
|
| 458 |
+
return _impl_.kernel_output_feature_dimension_;
|
| 459 |
+
}
|
| 460 |
+
inline int64_t UniformQuantizedConvolutionDimensionNumbersAttr::kernel_output_feature_dimension() const {
|
| 461 |
+
// @@protoc_insertion_point(field_get:tensorflow.UniformQuantizedConvolutionDimensionNumbersAttr.kernel_output_feature_dimension)
|
| 462 |
+
return _internal_kernel_output_feature_dimension();
|
| 463 |
+
}
|
| 464 |
+
inline void UniformQuantizedConvolutionDimensionNumbersAttr::_internal_set_kernel_output_feature_dimension(int64_t value) {
|
| 465 |
+
|
| 466 |
+
_impl_.kernel_output_feature_dimension_ = value;
|
| 467 |
+
}
|
| 468 |
+
inline void UniformQuantizedConvolutionDimensionNumbersAttr::set_kernel_output_feature_dimension(int64_t value) {
|
| 469 |
+
_internal_set_kernel_output_feature_dimension(value);
|
| 470 |
+
// @@protoc_insertion_point(field_set:tensorflow.UniformQuantizedConvolutionDimensionNumbersAttr.kernel_output_feature_dimension)
|
| 471 |
+
}
|
| 472 |
+
|
| 473 |
+
// repeated int64 kernel_spatial_dimensions = 6;
|
| 474 |
+
inline int UniformQuantizedConvolutionDimensionNumbersAttr::_internal_kernel_spatial_dimensions_size() const {
|
| 475 |
+
return _impl_.kernel_spatial_dimensions_.size();
|
| 476 |
+
}
|
| 477 |
+
inline int UniformQuantizedConvolutionDimensionNumbersAttr::kernel_spatial_dimensions_size() const {
|
| 478 |
+
return _internal_kernel_spatial_dimensions_size();
|
| 479 |
+
}
|
| 480 |
+
inline void UniformQuantizedConvolutionDimensionNumbersAttr::clear_kernel_spatial_dimensions() {
|
| 481 |
+
_impl_.kernel_spatial_dimensions_.Clear();
|
| 482 |
+
}
|
| 483 |
+
inline int64_t UniformQuantizedConvolutionDimensionNumbersAttr::_internal_kernel_spatial_dimensions(int index) const {
|
| 484 |
+
return _impl_.kernel_spatial_dimensions_.Get(index);
|
| 485 |
+
}
|
| 486 |
+
inline int64_t UniformQuantizedConvolutionDimensionNumbersAttr::kernel_spatial_dimensions(int index) const {
|
| 487 |
+
// @@protoc_insertion_point(field_get:tensorflow.UniformQuantizedConvolutionDimensionNumbersAttr.kernel_spatial_dimensions)
|
| 488 |
+
return _internal_kernel_spatial_dimensions(index);
|
| 489 |
+
}
|
| 490 |
+
inline void UniformQuantizedConvolutionDimensionNumbersAttr::set_kernel_spatial_dimensions(int index, int64_t value) {
|
| 491 |
+
_impl_.kernel_spatial_dimensions_.Set(index, value);
|
| 492 |
+
// @@protoc_insertion_point(field_set:tensorflow.UniformQuantizedConvolutionDimensionNumbersAttr.kernel_spatial_dimensions)
|
| 493 |
+
}
|
| 494 |
+
inline void UniformQuantizedConvolutionDimensionNumbersAttr::_internal_add_kernel_spatial_dimensions(int64_t value) {
|
| 495 |
+
_impl_.kernel_spatial_dimensions_.Add(value);
|
| 496 |
+
}
|
| 497 |
+
inline void UniformQuantizedConvolutionDimensionNumbersAttr::add_kernel_spatial_dimensions(int64_t value) {
|
| 498 |
+
_internal_add_kernel_spatial_dimensions(value);
|
| 499 |
+
// @@protoc_insertion_point(field_add:tensorflow.UniformQuantizedConvolutionDimensionNumbersAttr.kernel_spatial_dimensions)
|
| 500 |
+
}
|
| 501 |
+
inline const ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >&
|
| 502 |
+
UniformQuantizedConvolutionDimensionNumbersAttr::_internal_kernel_spatial_dimensions() const {
|
| 503 |
+
return _impl_.kernel_spatial_dimensions_;
|
| 504 |
+
}
|
| 505 |
+
inline const ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >&
|
| 506 |
+
UniformQuantizedConvolutionDimensionNumbersAttr::kernel_spatial_dimensions() const {
|
| 507 |
+
// @@protoc_insertion_point(field_list:tensorflow.UniformQuantizedConvolutionDimensionNumbersAttr.kernel_spatial_dimensions)
|
| 508 |
+
return _internal_kernel_spatial_dimensions();
|
| 509 |
+
}
|
| 510 |
+
inline ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >*
|
| 511 |
+
UniformQuantizedConvolutionDimensionNumbersAttr::_internal_mutable_kernel_spatial_dimensions() {
|
| 512 |
+
return &_impl_.kernel_spatial_dimensions_;
|
| 513 |
+
}
|
| 514 |
+
inline ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >*
|
| 515 |
+
UniformQuantizedConvolutionDimensionNumbersAttr::mutable_kernel_spatial_dimensions() {
|
| 516 |
+
// @@protoc_insertion_point(field_mutable_list:tensorflow.UniformQuantizedConvolutionDimensionNumbersAttr.kernel_spatial_dimensions)
|
| 517 |
+
return _internal_mutable_kernel_spatial_dimensions();
|
| 518 |
+
}
|
| 519 |
+
|
| 520 |
+
// int64 output_batch_dimension = 7;
|
| 521 |
+
inline void UniformQuantizedConvolutionDimensionNumbersAttr::clear_output_batch_dimension() {
|
| 522 |
+
_impl_.output_batch_dimension_ = int64_t{0};
|
| 523 |
+
}
|
| 524 |
+
inline int64_t UniformQuantizedConvolutionDimensionNumbersAttr::_internal_output_batch_dimension() const {
|
| 525 |
+
return _impl_.output_batch_dimension_;
|
| 526 |
+
}
|
| 527 |
+
inline int64_t UniformQuantizedConvolutionDimensionNumbersAttr::output_batch_dimension() const {
|
| 528 |
+
// @@protoc_insertion_point(field_get:tensorflow.UniformQuantizedConvolutionDimensionNumbersAttr.output_batch_dimension)
|
| 529 |
+
return _internal_output_batch_dimension();
|
| 530 |
+
}
|
| 531 |
+
inline void UniformQuantizedConvolutionDimensionNumbersAttr::_internal_set_output_batch_dimension(int64_t value) {
|
| 532 |
+
|
| 533 |
+
_impl_.output_batch_dimension_ = value;
|
| 534 |
+
}
|
| 535 |
+
inline void UniformQuantizedConvolutionDimensionNumbersAttr::set_output_batch_dimension(int64_t value) {
|
| 536 |
+
_internal_set_output_batch_dimension(value);
|
| 537 |
+
// @@protoc_insertion_point(field_set:tensorflow.UniformQuantizedConvolutionDimensionNumbersAttr.output_batch_dimension)
|
| 538 |
+
}
|
| 539 |
+
|
| 540 |
+
// int64 output_feature_dimension = 8;
|
| 541 |
+
inline void UniformQuantizedConvolutionDimensionNumbersAttr::clear_output_feature_dimension() {
|
| 542 |
+
_impl_.output_feature_dimension_ = int64_t{0};
|
| 543 |
+
}
|
| 544 |
+
inline int64_t UniformQuantizedConvolutionDimensionNumbersAttr::_internal_output_feature_dimension() const {
|
| 545 |
+
return _impl_.output_feature_dimension_;
|
| 546 |
+
}
|
| 547 |
+
inline int64_t UniformQuantizedConvolutionDimensionNumbersAttr::output_feature_dimension() const {
|
| 548 |
+
// @@protoc_insertion_point(field_get:tensorflow.UniformQuantizedConvolutionDimensionNumbersAttr.output_feature_dimension)
|
| 549 |
+
return _internal_output_feature_dimension();
|
| 550 |
+
}
|
| 551 |
+
inline void UniformQuantizedConvolutionDimensionNumbersAttr::_internal_set_output_feature_dimension(int64_t value) {
|
| 552 |
+
|
| 553 |
+
_impl_.output_feature_dimension_ = value;
|
| 554 |
+
}
|
| 555 |
+
inline void UniformQuantizedConvolutionDimensionNumbersAttr::set_output_feature_dimension(int64_t value) {
|
| 556 |
+
_internal_set_output_feature_dimension(value);
|
| 557 |
+
// @@protoc_insertion_point(field_set:tensorflow.UniformQuantizedConvolutionDimensionNumbersAttr.output_feature_dimension)
|
| 558 |
+
}
|
| 559 |
+
|
| 560 |
+
// repeated int64 output_spatial_dimensions = 9;
|
| 561 |
+
inline int UniformQuantizedConvolutionDimensionNumbersAttr::_internal_output_spatial_dimensions_size() const {
|
| 562 |
+
return _impl_.output_spatial_dimensions_.size();
|
| 563 |
+
}
|
| 564 |
+
inline int UniformQuantizedConvolutionDimensionNumbersAttr::output_spatial_dimensions_size() const {
|
| 565 |
+
return _internal_output_spatial_dimensions_size();
|
| 566 |
+
}
|
| 567 |
+
inline void UniformQuantizedConvolutionDimensionNumbersAttr::clear_output_spatial_dimensions() {
|
| 568 |
+
_impl_.output_spatial_dimensions_.Clear();
|
| 569 |
+
}
|
| 570 |
+
inline int64_t UniformQuantizedConvolutionDimensionNumbersAttr::_internal_output_spatial_dimensions(int index) const {
|
| 571 |
+
return _impl_.output_spatial_dimensions_.Get(index);
|
| 572 |
+
}
|
| 573 |
+
inline int64_t UniformQuantizedConvolutionDimensionNumbersAttr::output_spatial_dimensions(int index) const {
|
| 574 |
+
// @@protoc_insertion_point(field_get:tensorflow.UniformQuantizedConvolutionDimensionNumbersAttr.output_spatial_dimensions)
|
| 575 |
+
return _internal_output_spatial_dimensions(index);
|
| 576 |
+
}
|
| 577 |
+
inline void UniformQuantizedConvolutionDimensionNumbersAttr::set_output_spatial_dimensions(int index, int64_t value) {
|
| 578 |
+
_impl_.output_spatial_dimensions_.Set(index, value);
|
| 579 |
+
// @@protoc_insertion_point(field_set:tensorflow.UniformQuantizedConvolutionDimensionNumbersAttr.output_spatial_dimensions)
|
| 580 |
+
}
|
| 581 |
+
inline void UniformQuantizedConvolutionDimensionNumbersAttr::_internal_add_output_spatial_dimensions(int64_t value) {
|
| 582 |
+
_impl_.output_spatial_dimensions_.Add(value);
|
| 583 |
+
}
|
| 584 |
+
inline void UniformQuantizedConvolutionDimensionNumbersAttr::add_output_spatial_dimensions(int64_t value) {
|
| 585 |
+
_internal_add_output_spatial_dimensions(value);
|
| 586 |
+
// @@protoc_insertion_point(field_add:tensorflow.UniformQuantizedConvolutionDimensionNumbersAttr.output_spatial_dimensions)
|
| 587 |
+
}
|
| 588 |
+
inline const ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >&
|
| 589 |
+
UniformQuantizedConvolutionDimensionNumbersAttr::_internal_output_spatial_dimensions() const {
|
| 590 |
+
return _impl_.output_spatial_dimensions_;
|
| 591 |
+
}
|
| 592 |
+
inline const ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >&
|
| 593 |
+
UniformQuantizedConvolutionDimensionNumbersAttr::output_spatial_dimensions() const {
|
| 594 |
+
// @@protoc_insertion_point(field_list:tensorflow.UniformQuantizedConvolutionDimensionNumbersAttr.output_spatial_dimensions)
|
| 595 |
+
return _internal_output_spatial_dimensions();
|
| 596 |
+
}
|
| 597 |
+
inline ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >*
|
| 598 |
+
UniformQuantizedConvolutionDimensionNumbersAttr::_internal_mutable_output_spatial_dimensions() {
|
| 599 |
+
return &_impl_.output_spatial_dimensions_;
|
| 600 |
+
}
|
| 601 |
+
inline ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t >*
|
| 602 |
+
UniformQuantizedConvolutionDimensionNumbersAttr::mutable_output_spatial_dimensions() {
|
| 603 |
+
// @@protoc_insertion_point(field_mutable_list:tensorflow.UniformQuantizedConvolutionDimensionNumbersAttr.output_spatial_dimensions)
|
| 604 |
+
return _internal_mutable_output_spatial_dimensions();
|
| 605 |
+
}
|
| 606 |
+
|
| 607 |
+
#ifdef __GNUC__
|
| 608 |
+
#pragma GCC diagnostic pop
|
| 609 |
+
#endif // __GNUC__
|
| 610 |
+
|
| 611 |
+
// @@protoc_insertion_point(namespace_scope)
|
| 612 |
+
|
| 613 |
+
} // namespace tensorflow
|
| 614 |
+
|
| 615 |
+
// @@protoc_insertion_point(global_scope)
|
| 616 |
+
|
| 617 |
+
#include <google/protobuf/port_undef.inc>
|
| 618 |
+
#endif // GOOGLE_PROTOBUF_INCLUDED_GOOGLE_PROTOBUF_INCLUDED_tensorflow_2fcore_2futil_2fquantization_2funiform_5fquant_5fops_5fattr_2eproto
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/quantization/uniform_quant_ops_attr.proto
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2022 The TensorFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
|
| 16 |
+
syntax = "proto3";
|
| 17 |
+
|
| 18 |
+
package tensorflow;
|
| 19 |
+
|
| 20 |
+
option go_package = "github.com/tensorflow/tensorflow/tensorflow/go/core/protobuf/for_core_protos_go_proto";
|
| 21 |
+
|
| 22 |
+
// Describes the dimension numbers for Convolution op. Corresponds to
|
| 23 |
+
// ::mlir::mhlo::ConvDimensionNumbersAttr.
|
| 24 |
+
message UniformQuantizedConvolutionDimensionNumbersAttr {
|
| 25 |
+
// The dimension that represents batch in the input.
|
| 26 |
+
int64 input_batch_dimension = 1;
|
| 27 |
+
// The dimension that represents features in the input.
|
| 28 |
+
int64 input_feature_dimension = 2;
|
| 29 |
+
// The dimensions that represents spatial dimensions in the input. Length must
|
| 30 |
+
// be rank-2 for the tensor rank for Convolution op.
|
| 31 |
+
repeated int64 input_spatial_dimensions = 3;
|
| 32 |
+
|
| 33 |
+
// The dimension that represents input features in the kernel (rhs).
|
| 34 |
+
int64 kernel_input_feature_dimension = 4;
|
| 35 |
+
// The dimension that represents output features in the kernel (rhs).
|
| 36 |
+
int64 kernel_output_feature_dimension = 5;
|
| 37 |
+
// The dimensions that represents spatial dimensions in the kernel (rhs).
|
| 38 |
+
// Length must be rank-2 for the tensor rank for Convolution op.
|
| 39 |
+
repeated int64 kernel_spatial_dimensions = 6;
|
| 40 |
+
|
| 41 |
+
// The dimension that represents batch in the output.
|
| 42 |
+
int64 output_batch_dimension = 7;
|
| 43 |
+
// The dimension that represents features in the output.
|
| 44 |
+
int64 output_feature_dimension = 8;
|
| 45 |
+
// The dimensions that represents spatial dimensions in the output. Length
|
| 46 |
+
// must be rank-2 for the tensor rank for Convolution op.
|
| 47 |
+
repeated int64 output_spatial_dimensions = 9;
|
| 48 |
+
}
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/quantization/uniform_quant_ops_params.h
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2022 The TensorFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
#ifndef TENSORFLOW_CORE_UTIL_QUANTIZATION_UNIFORM_QUANT_OPS_PARAMS_H_
|
| 16 |
+
#define TENSORFLOW_CORE_UTIL_QUANTIZATION_UNIFORM_QUANT_OPS_PARAMS_H_
|
| 17 |
+
|
| 18 |
+
#include <string>
|
| 19 |
+
#include <vector>
|
| 20 |
+
|
| 21 |
+
#include "tensorflow/core/framework/op_kernel.h"
|
| 22 |
+
#include "tensorflow/core/framework/shape_inference.h"
|
| 23 |
+
#include "tensorflow/core/framework/tensor_shape.h"
|
| 24 |
+
#include "tensorflow/core/platform/status.h"
|
| 25 |
+
#include "tensorflow/core/util/quantization/uniform_quant_ops_attr.pb.h"
|
| 26 |
+
|
| 27 |
+
namespace tensorflow {
|
| 28 |
+
|
| 29 |
+
// Utility class to load and retrieve params for
|
| 30 |
+
// UniformQuantizedConvolution{Hybrid} Op kernels.
|
| 31 |
+
//
|
| 32 |
+
// NOTE: This class instance is fully loaded and valid, only after (1) One of
|
| 33 |
+
// LoadFromAttrs() is called (2) ValidateOrFillParamsAndValidateShape is called.
|
| 34 |
+
// Member retrieve and CalculateOutputShape() can be used only after both are
|
| 35 |
+
// called.
|
| 36 |
+
class UniformQuantizedConvolutionParams {
|
| 37 |
+
public:
|
| 38 |
+
UniformQuantizedConvolutionParams() = default;
|
| 39 |
+
// Only for unit tests.
|
| 40 |
+
UniformQuantizedConvolutionParams(
|
| 41 |
+
const std::vector<int>& window_strides,
|
| 42 |
+
const std::vector<int>& lhs_dilation,
|
| 43 |
+
const std::vector<int>& rhs_dilation,
|
| 44 |
+
const UniformQuantizedConvolutionDimensionNumbersAttr& dimension_numbers,
|
| 45 |
+
int feature_group_count, int batch_group_count,
|
| 46 |
+
const std::string& padding, const std::vector<int>& padding_list = {})
|
| 47 |
+
: window_strides_(window_strides),
|
| 48 |
+
lhs_dilation_(lhs_dilation),
|
| 49 |
+
rhs_dilation_(rhs_dilation),
|
| 50 |
+
dimension_numbers_(dimension_numbers),
|
| 51 |
+
feature_group_count_(feature_group_count),
|
| 52 |
+
batch_group_count_(batch_group_count),
|
| 53 |
+
padding_(padding),
|
| 54 |
+
padding_list_(padding_list) {}
|
| 55 |
+
|
| 56 |
+
const std::vector<int>& window_strides() const { return window_strides_; }
|
| 57 |
+
const std::vector<int>& lhs_dilation() const { return lhs_dilation_; }
|
| 58 |
+
const std::vector<int>& rhs_dilation() const { return rhs_dilation_; }
|
| 59 |
+
const UniformQuantizedConvolutionDimensionNumbersAttr& dimension_numbers()
|
| 60 |
+
const {
|
| 61 |
+
return dimension_numbers_;
|
| 62 |
+
}
|
| 63 |
+
int batch_group_count() const { return batch_group_count_; }
|
| 64 |
+
|
| 65 |
+
const std::vector<int>& padding_list() const { return padding_list_; }
|
| 66 |
+
int feature_group_count() const { return feature_group_count_; }
|
| 67 |
+
|
| 68 |
+
// Load UniformQuantizedConvolutionParams members by reading op attrs.
|
| 69 |
+
Status LoadFromAttrs(const OpKernelConstruction& context);
|
| 70 |
+
Status LoadFromAttrs(const shape_inference::InferenceContext& context);
|
| 71 |
+
|
| 72 |
+
// Check if UniformQuantizedConvolutionParams members loaded from Attr are
|
| 73 |
+
// valid regarding the lhs_shape and rhs_shape, and fill param values if
|
| 74 |
+
// required. (Set default of empty optional Attrs, and fill padding_list_ if
|
| 75 |
+
// required.)
|
| 76 |
+
// Then, validate given lhs_shape and rhs_shape.
|
| 77 |
+
//
|
| 78 |
+
// NOTE: This method should be called only after calling one of
|
| 79 |
+
// LoadFromAttrs().
|
| 80 |
+
Status ValidateOrFillParamsAndValidateShape(const TensorShape& lhs_shape,
|
| 81 |
+
const TensorShape& rhs_shape);
|
| 82 |
+
|
| 83 |
+
// Calculate output shape using lhs_shape, rhs_shape, and the params.
|
| 84 |
+
//
|
| 85 |
+
// NOTE: this method can be used only after calling both LoadFromAttrs() and
|
| 86 |
+
// ValidateOrFillParamsAndValidateShape().
|
| 87 |
+
// Reference:
|
| 88 |
+
// https://github.com/google/jax/blob/0584c6a1c405b23317deb1596c2c161eb5709c84/jax/_src/lax/convolution.py#L349
|
| 89 |
+
StatusOr<TensorShape> CalculateOutputShape(
|
| 90 |
+
const TensorShape& lhs_shape, const TensorShape& rhs_shape) const;
|
| 91 |
+
|
| 92 |
+
// Given the original size of a dimension and a dilation, calculate the
|
| 93 |
+
// resulting size after dilation is applied.
|
| 94 |
+
inline static int64_t DilatedSize(int64_t size, int dilation) {
|
| 95 |
+
return size == 0 ? 0 : size + (dilation - 1) * (size - 1);
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
private:
|
| 99 |
+
template <typename ContextT>
|
| 100 |
+
Status LoadFromAttrsInternal(const ContextT& context);
|
| 101 |
+
Status ValidateShape(const TensorShape& lhs_shape,
|
| 102 |
+
const TensorShape& rhs_shape);
|
| 103 |
+
Status ValidateOrFillPaddingList(const TensorShape& lhs_shape,
|
| 104 |
+
const TensorShape& rhs_shape);
|
| 105 |
+
|
| 106 |
+
// Params from Attrs.
|
| 107 |
+
std::vector<int> window_strides_;
|
| 108 |
+
std::vector<int> lhs_dilation_;
|
| 109 |
+
std::vector<int> rhs_dilation_;
|
| 110 |
+
UniformQuantizedConvolutionDimensionNumbersAttr dimension_numbers_;
|
| 111 |
+
int feature_group_count_;
|
| 112 |
+
int batch_group_count_;
|
| 113 |
+
std::string padding_;
|
| 114 |
+
|
| 115 |
+
// Params derived from Attrs and Inputs.
|
| 116 |
+
std::vector<int> padding_list_;
|
| 117 |
+
};
|
| 118 |
+
|
| 119 |
+
} // namespace tensorflow
|
| 120 |
+
|
| 121 |
+
#endif // TENSORFLOW_CORE_UTIL_QUANTIZATION_UNIFORM_QUANT_OPS_PARAMS_H_
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/sparse/dim_comparator.h
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
|
| 16 |
+
#ifndef TENSORFLOW_CORE_UTIL_SPARSE_DIM_COMPARATOR_H_
|
| 17 |
+
#define TENSORFLOW_CORE_UTIL_SPARSE_DIM_COMPARATOR_H_
|
| 18 |
+
|
| 19 |
+
#include "unsupported/Eigen/CXX11/Tensor" // from @eigen_archive
|
| 20 |
+
#include "tensorflow/core/framework/bounds_check.h"
|
| 21 |
+
#include "tensorflow/core/framework/tensor_types.h"
|
| 22 |
+
#include "tensorflow/core/lib/gtl/array_slice.h"
|
| 23 |
+
#include "tensorflow/core/platform/logging.h"
|
| 24 |
+
#include "tensorflow/core/platform/types.h"
|
| 25 |
+
|
| 26 |
+
namespace tensorflow {
|
| 27 |
+
namespace sparse {
|
| 28 |
+
|
| 29 |
+
/////////////////
|
| 30 |
+
// DimComparator
|
| 31 |
+
/////////////////
|
| 32 |
+
//
|
| 33 |
+
// Helper class, mainly used by the IndexSortOrder. This comparator
|
| 34 |
+
// can be passed to e.g. std::sort, or any other sorter, to sort two
|
| 35 |
+
// rows of an index matrix according to the dimension(s) of interest.
|
| 36 |
+
// The dimensions to sort by are passed to the constructor as "order".
|
| 37 |
+
//
|
| 38 |
+
// Example: if given index matrix IX, two rows ai and bi, and order = {2,1}.
|
| 39 |
+
// operator() compares
|
| 40 |
+
// IX(ai,2) < IX(bi,2).
|
| 41 |
+
// If IX(ai,2) == IX(bi,2), it compares
|
| 42 |
+
// IX(ai,1) < IX(bi,1).
|
| 43 |
+
//
|
| 44 |
+
// This can be used to sort a vector of row indices into IX according to
|
| 45 |
+
// the values in IX in particular columns (dimensions) of interest.
|
| 46 |
+
class DimComparator {
|
| 47 |
+
public:
|
| 48 |
+
typedef typename gtl::ArraySlice<int64_t> VarDimArray;
|
| 49 |
+
|
| 50 |
+
DimComparator(const TTypes<int64_t>::Matrix& ix, const VarDimArray& order,
|
| 51 |
+
const VarDimArray& shape)
|
| 52 |
+
: ix_(ix), order_(order), dims_(shape.size()) {
|
| 53 |
+
DCHECK_GT(order.size(), size_t{0}) << "Must order using at least one index";
|
| 54 |
+
DCHECK_LE(order.size(), shape.size()) << "Can only sort up to dims";
|
| 55 |
+
for (size_t d = 0; d < order.size(); ++d) {
|
| 56 |
+
DCHECK_GE(order[d], 0);
|
| 57 |
+
DCHECK_LT(order[d], shape.size());
|
| 58 |
+
}
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
inline bool operator()(const int64_t i, const int64_t j) const {
|
| 62 |
+
for (int di = 0; di < dims_; ++di) {
|
| 63 |
+
const int64_t d = order_[di];
|
| 64 |
+
if (ix_(i, d) < ix_(j, d)) return true;
|
| 65 |
+
if (ix_(i, d) > ix_(j, d)) return false;
|
| 66 |
+
}
|
| 67 |
+
return false;
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
// Compares two indices taken from corresponding index matrices, using the
|
| 71 |
+
// standard, row-major (or lexicographic) order. Useful for cases that need
|
| 72 |
+
// to distinguish between all three orderings (<, ==, >).
|
| 73 |
+
inline static int cmp(const TTypes<int64_t>::ConstMatrix& a_idx,
|
| 74 |
+
const TTypes<int64_t>::ConstMatrix& b_idx,
|
| 75 |
+
const int64_t a_row, const int64_t b_row,
|
| 76 |
+
const int dims) {
|
| 77 |
+
for (int d = 0; d < dims; ++d) {
|
| 78 |
+
const int64_t a = a_idx(a_row, d);
|
| 79 |
+
const int64_t b = b_idx(b_row, d);
|
| 80 |
+
if (a < b) {
|
| 81 |
+
return -1;
|
| 82 |
+
} else if (a > b) {
|
| 83 |
+
return 1;
|
| 84 |
+
}
|
| 85 |
+
}
|
| 86 |
+
return 0;
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
protected:
|
| 90 |
+
const TTypes<int64_t>::Matrix ix_;
|
| 91 |
+
const VarDimArray order_;
|
| 92 |
+
const int dims_;
|
| 93 |
+
const std::vector<int64_t>* ix_order_;
|
| 94 |
+
};
|
| 95 |
+
|
| 96 |
+
template <int ORDER_DIM>
|
| 97 |
+
class FixedDimComparator : DimComparator {
|
| 98 |
+
public:
|
| 99 |
+
FixedDimComparator(const TTypes<int64_t>::Matrix& ix,
|
| 100 |
+
const VarDimArray& order, const VarDimArray& shape)
|
| 101 |
+
: DimComparator(ix, order, shape) {
|
| 102 |
+
DCHECK_EQ(order.size(), ORDER_DIM);
|
| 103 |
+
}
|
| 104 |
+
inline bool operator()(const int64_t i, const int64_t j) const {
|
| 105 |
+
bool value = false;
|
| 106 |
+
for (int di = 0; di < ORDER_DIM; ++di) {
|
| 107 |
+
const int64_t d = order_[di];
|
| 108 |
+
if (ix_(i, d) < ix_(j, d)) {
|
| 109 |
+
value = true;
|
| 110 |
+
break;
|
| 111 |
+
}
|
| 112 |
+
if (ix_(i, d) > ix_(j, d)) break;
|
| 113 |
+
}
|
| 114 |
+
return value;
|
| 115 |
+
}
|
| 116 |
+
};
|
| 117 |
+
|
| 118 |
+
} // namespace sparse
|
| 119 |
+
} // namespace tensorflow
|
| 120 |
+
|
| 121 |
+
#endif // TENSORFLOW_CORE_UTIL_SPARSE_DIM_COMPARATOR_H_
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/sparse/group_iterator.h
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
|
| 16 |
+
#ifndef TENSORFLOW_CORE_UTIL_SPARSE_GROUP_ITERATOR_H_
|
| 17 |
+
#define TENSORFLOW_CORE_UTIL_SPARSE_GROUP_ITERATOR_H_
|
| 18 |
+
|
| 19 |
+
#include <vector>
|
| 20 |
+
#include "unsupported/Eigen/CXX11/Tensor" // from @eigen_archive
|
| 21 |
+
#include "tensorflow/core/framework/tensor.h"
|
| 22 |
+
#include "tensorflow/core/lib/core/status.h"
|
| 23 |
+
#include "tensorflow/core/platform/logging.h"
|
| 24 |
+
#include "tensorflow/core/platform/types.h"
|
| 25 |
+
|
| 26 |
+
namespace tensorflow {
|
| 27 |
+
namespace sparse {
|
| 28 |
+
|
| 29 |
+
class GroupIterable; // Predeclare GroupIterable for Group.
|
| 30 |
+
|
| 31 |
+
// This class is returned when dereferencing a GroupIterable iterator.
|
| 32 |
+
// It provides the methods group(), indices(), and values(), which
|
| 33 |
+
// provide access into the underlying SparseTensor.
|
| 34 |
+
class Group {
|
| 35 |
+
public:
|
| 36 |
+
Group(GroupIterable* iter, int64_t loc, int64_t next_loc)
|
| 37 |
+
: iter_(iter), loc_(loc), next_loc_(next_loc) {}
|
| 38 |
+
|
| 39 |
+
std::vector<int64_t> group() const;
|
| 40 |
+
int64_t group_at(size_t index) const;
|
| 41 |
+
TTypes<int64_t>::UnalignedConstMatrix indices() const;
|
| 42 |
+
template <typename T>
|
| 43 |
+
typename TTypes<T>::UnalignedVec values() const;
|
| 44 |
+
|
| 45 |
+
private:
|
| 46 |
+
GroupIterable* iter_;
|
| 47 |
+
int64_t loc_;
|
| 48 |
+
int64_t next_loc_;
|
| 49 |
+
};
|
| 50 |
+
|
| 51 |
+
/////////////////
|
| 52 |
+
// GroupIterable
|
| 53 |
+
/////////////////
|
| 54 |
+
//
|
| 55 |
+
// Returned when calling sparse_tensor.group({dim0, dim1, ...}).
|
| 56 |
+
//
|
| 57 |
+
// Please note: the sparse_tensor should already be ordered according
|
| 58 |
+
// to {dim0, dim1, ...}. Otherwise this iteration will return invalid groups.
|
| 59 |
+
//
|
| 60 |
+
// Allows grouping and iteration of the SparseTensor according to the
|
| 61 |
+
// subset of dimensions provided to the group call.
|
| 62 |
+
//
|
| 63 |
+
// The actual grouping dimensions are stored in the
|
| 64 |
+
// internal vector group_dims_. Iterators inside the iterable provide
|
| 65 |
+
// the three methods:
|
| 66 |
+
//
|
| 67 |
+
// * group(): returns a vector with the current group dimension values.
|
| 68 |
+
// * indices(): a map of index, providing the indices in
|
| 69 |
+
// this group.
|
| 70 |
+
// * values(): a map of values, providing the values in
|
| 71 |
+
// this group.
|
| 72 |
+
//
|
| 73 |
+
// To iterate across GroupIterable, see examples in README.md.
|
| 74 |
+
//
|
| 75 |
+
|
| 76 |
+
// Forward declaration of SparseTensor
|
| 77 |
+
class GroupIterable {
|
| 78 |
+
public:
|
| 79 |
+
typedef gtl::ArraySlice<int64_t> VarDimArray;
|
| 80 |
+
|
| 81 |
+
GroupIterable(Tensor ix, Tensor vals, int dims, const VarDimArray& group_dims)
|
| 82 |
+
: ix_(ix),
|
| 83 |
+
ix_matrix_(ix_.matrix<int64_t>()),
|
| 84 |
+
vals_(vals),
|
| 85 |
+
dims_(dims),
|
| 86 |
+
group_dims_(group_dims.begin(), group_dims.end()) {}
|
| 87 |
+
|
| 88 |
+
class IteratorStep;
|
| 89 |
+
|
| 90 |
+
IteratorStep begin() { return IteratorStep(this, 0); }
|
| 91 |
+
IteratorStep at(int64_t loc) {
|
| 92 |
+
CHECK(loc >= 0 && loc <= ix_.dim_size(0))
|
| 93 |
+
<< "loc provided must lie between 0 and " << ix_.dim_size(0);
|
| 94 |
+
return IteratorStep(this, loc);
|
| 95 |
+
}
|
| 96 |
+
IteratorStep end() { return IteratorStep(this, ix_.dim_size(0)); }
|
| 97 |
+
|
| 98 |
+
template <typename TIX>
|
| 99 |
+
inline bool GroupMatches(const TIX& ix, int64_t loc_a, int64_t loc_b) const {
|
| 100 |
+
for (int d : group_dims_) {
|
| 101 |
+
if (ix(loc_a, d) != ix(loc_b, d)) {
|
| 102 |
+
return false;
|
| 103 |
+
}
|
| 104 |
+
}
|
| 105 |
+
return true;
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
class IteratorStep {
|
| 109 |
+
public:
|
| 110 |
+
IteratorStep(GroupIterable* iter, int64_t loc)
|
| 111 |
+
: iter_(iter), loc_(loc), next_loc_(loc_) {
|
| 112 |
+
UpdateEndOfGroup();
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
void UpdateEndOfGroup();
|
| 116 |
+
bool operator!=(const IteratorStep& rhs) const;
|
| 117 |
+
bool operator==(const IteratorStep& rhs) const;
|
| 118 |
+
IteratorStep& operator++(); // prefix ++
|
| 119 |
+
IteratorStep operator++(int); // postfix ++
|
| 120 |
+
Group operator*() const { return Group(iter_, loc_, next_loc_); }
|
| 121 |
+
int64_t loc() const { return loc_; }
|
| 122 |
+
|
| 123 |
+
private:
|
| 124 |
+
GroupIterable* iter_;
|
| 125 |
+
int64_t loc_;
|
| 126 |
+
int64_t next_loc_;
|
| 127 |
+
};
|
| 128 |
+
|
| 129 |
+
private:
|
| 130 |
+
friend class Group;
|
| 131 |
+
const Tensor ix_;
|
| 132 |
+
const TTypes<int64_t>::ConstMatrix ix_matrix_;
|
| 133 |
+
Tensor vals_;
|
| 134 |
+
const int dims_;
|
| 135 |
+
const gtl::InlinedVector<int64_t, 8> group_dims_;
|
| 136 |
+
};
|
| 137 |
+
|
| 138 |
+
inline int64_t Group::group_at(size_t index) const {
|
| 139 |
+
const auto& ix_t = iter_->ix_matrix_;
|
| 140 |
+
return ix_t(loc_, index);
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
// Implementation of Group::values<T>()
|
| 144 |
+
template <typename T>
|
| 145 |
+
typename TTypes<T>::UnalignedVec Group::values() const {
|
| 146 |
+
return typename TTypes<T>::UnalignedVec(&(iter_->vals_.vec<T>()(loc_)),
|
| 147 |
+
next_loc_ - loc_);
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
} // namespace sparse
|
| 151 |
+
} // namespace tensorflow
|
| 152 |
+
|
| 153 |
+
#endif // TENSORFLOW_CORE_UTIL_SPARSE_GROUP_ITERATOR_H_
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/sparse/sparse_tensor.h
ADDED
|
@@ -0,0 +1,672 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
|
| 16 |
+
#ifndef TENSORFLOW_CORE_UTIL_SPARSE_SPARSE_TENSOR_H_
|
| 17 |
+
#define TENSORFLOW_CORE_UTIL_SPARSE_SPARSE_TENSOR_H_
|
| 18 |
+
|
| 19 |
+
#include <limits>
|
| 20 |
+
#include <numeric>
|
| 21 |
+
#include <vector>
|
| 22 |
+
|
| 23 |
+
#include "absl/base/macros.h"
|
| 24 |
+
#include "unsupported/Eigen/CXX11/Tensor" // from @eigen_archive
|
| 25 |
+
#include "tensorflow/core/framework/bounds_check.h"
|
| 26 |
+
#include "tensorflow/core/framework/tensor.h"
|
| 27 |
+
#include "tensorflow/core/framework/tensor_types.h"
|
| 28 |
+
#include "tensorflow/core/framework/types.h"
|
| 29 |
+
#include "tensorflow/core/framework/types.pb.h"
|
| 30 |
+
#include "tensorflow/core/lib/core/errors.h"
|
| 31 |
+
#include "tensorflow/core/lib/core/status.h"
|
| 32 |
+
#include "tensorflow/core/lib/strings/str_util.h"
|
| 33 |
+
#include "tensorflow/core/platform/errors.h"
|
| 34 |
+
#include "tensorflow/core/platform/logging.h"
|
| 35 |
+
#include "tensorflow/core/platform/types.h"
|
| 36 |
+
#include "tensorflow/core/util/sparse/dim_comparator.h"
|
| 37 |
+
#include "tensorflow/core/util/sparse/group_iterator.h"
|
| 38 |
+
|
| 39 |
+
namespace tensorflow {
|
| 40 |
+
namespace sparse {
|
| 41 |
+
|
| 42 |
+
class SparseTensor {
|
| 43 |
+
public:
|
| 44 |
+
typedef typename gtl::ArraySlice<int64_t> VarDimArray;
|
| 45 |
+
typedef typename gtl::InlinedVector<int64_t, 8> ShapeArray;
|
| 46 |
+
|
| 47 |
+
static Status Create(Tensor ix, Tensor vals, const VarDimArray shape,
|
| 48 |
+
const VarDimArray order, SparseTensor* result);
|
| 49 |
+
|
| 50 |
+
static Status Create(Tensor ix, Tensor vals, const TensorShape& shape,
|
| 51 |
+
SparseTensor* result);
|
| 52 |
+
|
| 53 |
+
static Status Create(Tensor ix, Tensor vals, const VarDimArray shape,
|
| 54 |
+
SparseTensor* result);
|
| 55 |
+
|
| 56 |
+
static Status Create(Tensor ix, Tensor vals, const TensorShape& shape,
|
| 57 |
+
const VarDimArray order, SparseTensor* result);
|
| 58 |
+
|
| 59 |
+
SparseTensor() : dims_(0) {}
|
| 60 |
+
|
| 61 |
+
ABSL_DEPRECATED("Use Create() functions instead of constructors directly.")
|
| 62 |
+
SparseTensor(Tensor ix, Tensor vals, const TensorShape& shape)
|
| 63 |
+
: SparseTensor(std::move(ix), std::move(vals), TensorShapeToVector(shape),
|
| 64 |
+
UndefinedOrder(TensorShapeToVector(shape))) {}
|
| 65 |
+
|
| 66 |
+
ABSL_DEPRECATED("Use Create() functions instead of constructors directly.")
|
| 67 |
+
SparseTensor(Tensor ix, Tensor vals, const VarDimArray shape)
|
| 68 |
+
: SparseTensor(std::move(ix), std::move(vals), shape,
|
| 69 |
+
UndefinedOrder(shape)) {}
|
| 70 |
+
|
| 71 |
+
ABSL_DEPRECATED("use Create() functions instead of constructors directly.")
|
| 72 |
+
SparseTensor(Tensor ix, Tensor vals, const TensorShape& shape,
|
| 73 |
+
const VarDimArray order)
|
| 74 |
+
: SparseTensor(std::move(ix), std::move(vals), TensorShapeToVector(shape),
|
| 75 |
+
order) {}
|
| 76 |
+
|
| 77 |
+
ABSL_DEPRECATED("Use Create() functions instead of constructors directly.")
|
| 78 |
+
SparseTensor(Tensor ix, Tensor vals, const VarDimArray shape,
|
| 79 |
+
const VarDimArray order);
|
| 80 |
+
|
| 81 |
+
SparseTensor(const SparseTensor& other)
|
| 82 |
+
: SparseTensor(other.ix_, other.vals_, other.shape_, other.order_) {}
|
| 83 |
+
|
| 84 |
+
SparseTensor(SparseTensor&& other)
|
| 85 |
+
: SparseTensor(std::move(other.ix_), std::move(other.vals_),
|
| 86 |
+
std::move(other.shape_), std::move(other.order_)) {}
|
| 87 |
+
|
| 88 |
+
SparseTensor& operator=(const SparseTensor& other) {
|
| 89 |
+
ix_ = other.ix_;
|
| 90 |
+
vals_ = other.vals_;
|
| 91 |
+
shape_ = other.shape_;
|
| 92 |
+
order_ = other.order_;
|
| 93 |
+
dims_ = other.dims_;
|
| 94 |
+
return *this;
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
SparseTensor& operator=(SparseTensor&& other) {
|
| 98 |
+
ix_ = std::move(other.ix_);
|
| 99 |
+
vals_ = std::move(other.vals_);
|
| 100 |
+
shape_ = std::move(other.shape_);
|
| 101 |
+
order_ = std::move(other.order_);
|
| 102 |
+
dims_ = std::move(other.dims_);
|
| 103 |
+
return *this;
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
std::size_t num_entries() const { return ix_.dim_size(0); }
|
| 107 |
+
|
| 108 |
+
int dims() const { return shape_.size(); }
|
| 109 |
+
|
| 110 |
+
const Tensor& indices() const { return ix_; }
|
| 111 |
+
|
| 112 |
+
const Tensor& values() const { return vals_; }
|
| 113 |
+
|
| 114 |
+
DataType dtype() const { return vals_.dtype(); }
|
| 115 |
+
|
| 116 |
+
Status IndicesValid() const;
|
| 117 |
+
|
| 118 |
+
VarDimArray shape() const { return shape_; }
|
| 119 |
+
|
| 120 |
+
VarDimArray order() const { return order_; }
|
| 121 |
+
|
| 122 |
+
// Resorts the indices and values according to the dimensions in order.
|
| 123 |
+
template <typename T>
|
| 124 |
+
void Reorder(const VarDimArray& order);
|
| 125 |
+
|
| 126 |
+
// Returns a group iterable that can be used for clumping indices
|
| 127 |
+
// and values according to the group indices of interest.
|
| 128 |
+
//
|
| 129 |
+
// Precondition: order()[0..group_ix.size()] == group_ix.
|
| 130 |
+
//
|
| 131 |
+
// See the README.md in this directory for more usage information.
|
| 132 |
+
GroupIterable group(const VarDimArray& group_ix) const {
|
| 133 |
+
DCHECK_LE(group_ix.size(), dims_);
|
| 134 |
+
for (std::size_t di = 0; di < group_ix.size(); ++di) {
|
| 135 |
+
DCHECK_GE(group_ix[di], 0) << "Group dimension out of range";
|
| 136 |
+
DCHECK_LT(group_ix[di], dims_) << "Group dimension out of range";
|
| 137 |
+
DCHECK_EQ(group_ix[di], order_[di])
|
| 138 |
+
<< "Group dimension does not match sorted order";
|
| 139 |
+
}
|
| 140 |
+
return GroupIterable(ix_, vals_, dims_, group_ix);
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
// Stores the sparse indices into the dense tensor out.
|
| 144 |
+
// Preconditions:
|
| 145 |
+
// out->shape().dims() == shape().dims()
|
| 146 |
+
// out->shape().dim_size(d) >= shape(d) for all d
|
| 147 |
+
//
|
| 148 |
+
// Returns true on success. False on failure (mismatched dimensions
|
| 149 |
+
// or out-of-bounds indices).
|
| 150 |
+
//
|
| 151 |
+
// If initialize==True, ToDense first overwrites all coefficients in out to 0.
|
| 152 |
+
//
|
| 153 |
+
template <typename T>
|
| 154 |
+
bool ToDense(Tensor* out, bool initialize = true);
|
| 155 |
+
|
| 156 |
+
// Concat() will concatenate all the tensors according to their first order
|
| 157 |
+
// dimension. All tensors must have identical shape except for
|
| 158 |
+
// the first order dimension. All tensors orders' first dimension
|
| 159 |
+
// must match.
|
| 160 |
+
//
|
| 161 |
+
// If all of the tensors have identical ordering, then the output
|
| 162 |
+
// will have this ordering. Otherwise the output is set as not
|
| 163 |
+
// having any order and a Reorder<T>() should be called on it before
|
| 164 |
+
// performing any subsequent operations.
|
| 165 |
+
template <typename T>
|
| 166 |
+
static SparseTensor Concat(const gtl::ArraySlice<SparseTensor>& tensors);
|
| 167 |
+
|
| 168 |
+
// Split() will split the input SparseTensor into a list of num_split
|
| 169 |
+
// SparseTensor given a splitting dimension. If the input dimension range
|
| 170 |
+
// isn't an integer multiple of split_dim, we add one extra dimension for
|
| 171 |
+
// each slice.
|
| 172 |
+
template <typename T>
|
| 173 |
+
static Status Split(const SparseTensor& tensor, const int split_dim,
|
| 174 |
+
const int num_split, std::vector<SparseTensor>* result);
|
| 175 |
+
|
| 176 |
+
// Slice() will slice the input SparseTensor into a SparseTensor based on
|
| 177 |
+
// specified start and size. Both start and size are 1-D array with each
|
| 178 |
+
// element of the array representing one dimension. The start is the start
|
| 179 |
+
// index at each dimension and the size is the size at each dimension.
|
| 180 |
+
template <typename T>
|
| 181 |
+
static StatusOr<SparseTensor> Slice(const SparseTensor& tensor,
|
| 182 |
+
const gtl::ArraySlice<int64_t> start,
|
| 183 |
+
const gtl::ArraySlice<int64_t> size);
|
| 184 |
+
|
| 185 |
+
// Picks out the dimensions according to `dim_indices`.
|
| 186 |
+
std::vector<int64_t> PickDims(gtl::ArraySlice<int64_t> dim_indices) const {
|
| 187 |
+
std::vector<int64_t> res(dim_indices.size());
|
| 188 |
+
for (size_t i = 0; i < dim_indices.size(); ++i) {
|
| 189 |
+
res[i] = shape_[dim_indices[i]];
|
| 190 |
+
}
|
| 191 |
+
return res;
|
| 192 |
+
}
|
| 193 |
+
|
| 194 |
+
private:
|
| 195 |
+
static inline ShapeArray UndefinedOrder(const VarDimArray shape) {
|
| 196 |
+
return ShapeArray(shape.size(), -1);
|
| 197 |
+
}
|
| 198 |
+
|
| 199 |
+
static inline ShapeArray TensorShapeToVector(const TensorShape& shape) {
|
| 200 |
+
ShapeArray vec(shape.dims());
|
| 201 |
+
for (int i = 0; i < shape.dims(); ++i) vec[i] = shape.dim_size(i);
|
| 202 |
+
return vec;
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
// Optimized implementation of `IndicesValid` for 1-D sparse tensors.
|
| 206 |
+
// REQUIRES: `shape_.size() == 1`.
|
| 207 |
+
bool IndicesValidVectorFastPath() const;
|
| 208 |
+
|
| 209 |
+
// Optimized implementation of `IndicesValid` for 2-D sparse tensors whose
|
| 210 |
+
// indices fit within the range of an `int32`.
|
| 211 |
+
// REQUIRES: `shape_.size() == 2`.
|
| 212 |
+
bool IndicesValidMatrix32BitFastPath() const;
|
| 213 |
+
|
| 214 |
+
template <bool standard_order>
|
| 215 |
+
Status IndicesValidHelper() const;
|
| 216 |
+
|
| 217 |
+
// Helper for ToDense<T>()
|
| 218 |
+
template <typename T>
|
| 219 |
+
bool ValidateAndInitializeToDense(Tensor* out, bool initialize);
|
| 220 |
+
|
| 221 |
+
// Helper for Split() that returns the slice index.
|
| 222 |
+
static inline int GetSliceIndex(const int dim, const int split_size,
|
| 223 |
+
const int residual) {
|
| 224 |
+
DCHECK_GT(split_size, 0);
|
| 225 |
+
DCHECK_GE(dim, 0);
|
| 226 |
+
if (residual == 0) return dim / split_size;
|
| 227 |
+
const int offset = residual * (split_size + 1);
|
| 228 |
+
if (dim < offset) {
|
| 229 |
+
return dim / (split_size + 1);
|
| 230 |
+
} else {
|
| 231 |
+
return residual + ((dim - offset) / split_size);
|
| 232 |
+
}
|
| 233 |
+
}
|
| 234 |
+
|
| 235 |
+
// Helper for Split() that returns the dimension in the slice.
|
| 236 |
+
static inline int GetDimensionInSlice(const int dim, const int split_size,
|
| 237 |
+
const int residual) {
|
| 238 |
+
DCHECK_GT(split_size, 0);
|
| 239 |
+
DCHECK_GE(dim, 0);
|
| 240 |
+
if (residual == 0) return dim % split_size;
|
| 241 |
+
const int offset = residual * (split_size + 1);
|
| 242 |
+
if (dim < offset) {
|
| 243 |
+
return dim % (split_size + 1);
|
| 244 |
+
} else {
|
| 245 |
+
return (dim - offset) % split_size;
|
| 246 |
+
}
|
| 247 |
+
}
|
| 248 |
+
|
| 249 |
+
// Helper for Split() that returns the shape given a slice index.
|
| 250 |
+
static inline int GetSliceShape(const int slice_index, const int split_size,
|
| 251 |
+
const int residual) {
|
| 252 |
+
DCHECK_GT(split_size, 0);
|
| 253 |
+
DCHECK_GE(slice_index, 0);
|
| 254 |
+
if (residual == 0) return split_size;
|
| 255 |
+
if (slice_index < residual) {
|
| 256 |
+
return split_size + 1;
|
| 257 |
+
} else {
|
| 258 |
+
return split_size;
|
| 259 |
+
}
|
| 260 |
+
}
|
| 261 |
+
|
| 262 |
+
Tensor ix_;
|
| 263 |
+
Tensor vals_;
|
| 264 |
+
ShapeArray shape_;
|
| 265 |
+
ShapeArray order_;
|
| 266 |
+
int dims_;
|
| 267 |
+
};
|
| 268 |
+
|
| 269 |
+
// This operation updates the indices and values Tensor rows, so it is
|
| 270 |
+
// an in-place algorithm. It requires O(N log N) time and O(N)
|
| 271 |
+
// temporary space.
|
| 272 |
+
template <typename T>
|
| 273 |
+
inline void SparseTensor::Reorder(const VarDimArray& order) {
|
| 274 |
+
DCHECK_EQ(DataTypeToEnum<T>::v(), dtype())
|
| 275 |
+
<< "Reorder requested with the wrong datatype";
|
| 276 |
+
DCHECK_EQ(order.size(), dims_) << "Order length must be SparseTensor rank";
|
| 277 |
+
auto ix_t = ix_.matrix<int64_t>();
|
| 278 |
+
auto vals_t = vals_.vec<T>();
|
| 279 |
+
|
| 280 |
+
std::vector<int64_t> reorder(num_entries());
|
| 281 |
+
std::iota(reorder.begin(), reorder.end(), 0);
|
| 282 |
+
|
| 283 |
+
// Sort to get order of indices
|
| 284 |
+
switch (order.size()) {
|
| 285 |
+
#define CASE_SORT(ORDER_SIZE) \
|
| 286 |
+
case ORDER_SIZE: { \
|
| 287 |
+
FixedDimComparator<ORDER_SIZE> sorter(ix_t, order, shape()); \
|
| 288 |
+
std::sort(reorder.begin(), reorder.end(), sorter); \
|
| 289 |
+
break; \
|
| 290 |
+
}
|
| 291 |
+
CASE_SORT(0);
|
| 292 |
+
CASE_SORT(1);
|
| 293 |
+
CASE_SORT(2);
|
| 294 |
+
CASE_SORT(3);
|
| 295 |
+
CASE_SORT(4);
|
| 296 |
+
CASE_SORT(5);
|
| 297 |
+
#undef CASE_SORT
|
| 298 |
+
default: {
|
| 299 |
+
DimComparator sorter(ix_t, order, shape());
|
| 300 |
+
std::sort(reorder.begin(), reorder.end(), sorter);
|
| 301 |
+
}
|
| 302 |
+
}
|
| 303 |
+
|
| 304 |
+
// We have a forward reordering, but what we'll need is a
|
| 305 |
+
// permutation (the inverse). This can be calculated with O(1)
|
| 306 |
+
// additional
|
| 307 |
+
// and O(n) time (INVPERM) but we just do the simple thing here.
|
| 308 |
+
std::vector<size_t> permutation(reorder.size());
|
| 309 |
+
for (std::size_t n = 0; n < reorder.size(); ++n) {
|
| 310 |
+
permutation[reorder[n]] = n;
|
| 311 |
+
}
|
| 312 |
+
|
| 313 |
+
// Update indices & values by converting the permutations to
|
| 314 |
+
// a product of transpositions. Iterate over the cycles in the
|
| 315 |
+
// permutation, and convert each of those into a product of
|
| 316 |
+
// transpositions (swaps):
|
| 317 |
+
// https://en.wikipedia.org/wiki/Cyclic_permutation
|
| 318 |
+
// This is N swaps, 2*N comparisons.
|
| 319 |
+
for (std::size_t n = 0; n + 1 < permutation.size(); ++n) {
|
| 320 |
+
while (n != permutation[n]) {
|
| 321 |
+
std::size_t r = permutation[n];
|
| 322 |
+
std::swap_ranges(&(ix_t(n, 0)), &(ix_t(n + 1, 0)), &(ix_t(r, 0)));
|
| 323 |
+
std::swap(vals_t(n), vals_t(r));
|
| 324 |
+
std::swap(permutation[n], permutation[r]);
|
| 325 |
+
}
|
| 326 |
+
}
|
| 327 |
+
|
| 328 |
+
order_ = ShapeArray(order.begin(), order.end());
|
| 329 |
+
}
|
| 330 |
+
|
| 331 |
+
template <typename T>
|
| 332 |
+
inline bool SparseTensor::ValidateAndInitializeToDense(Tensor* out,
|
| 333 |
+
bool initialize) {
|
| 334 |
+
DCHECK_EQ(DataTypeToEnum<T>::v(), dtype())
|
| 335 |
+
<< "ToDense requested with the wrong datatype";
|
| 336 |
+
|
| 337 |
+
DCHECK_EQ(out->shape().dims(), dims_)
|
| 338 |
+
<< "Incompatible dimensions between SparseTensor and output";
|
| 339 |
+
|
| 340 |
+
DCHECK_EQ(out->dtype(), DataTypeToEnum<T>::v())
|
| 341 |
+
<< "Output must be type: " << DataTypeToEnum<T>::v()
|
| 342 |
+
<< " but got: " << out->dtype();
|
| 343 |
+
|
| 344 |
+
// Make sure the dense output is the same rank and has room
|
| 345 |
+
// to hold the SparseTensor.
|
| 346 |
+
const auto& out_shape = out->shape();
|
| 347 |
+
if (shape_.size() != out_shape.dims()) return false;
|
| 348 |
+
for (int d = 0; d < shape_.size(); ++d) {
|
| 349 |
+
if (shape_[d] > out_shape.dim_size(d)) return false;
|
| 350 |
+
}
|
| 351 |
+
|
| 352 |
+
if (initialize) {
|
| 353 |
+
auto out_t = out->flat<T>();
|
| 354 |
+
out_t.setConstant(T());
|
| 355 |
+
}
|
| 356 |
+
|
| 357 |
+
return true;
|
| 358 |
+
}
|
| 359 |
+
|
| 360 |
+
template <typename T>
|
| 361 |
+
inline bool SparseTensor::ToDense(Tensor* out, bool initialize) {
|
| 362 |
+
if (!ValidateAndInitializeToDense<T>(out, initialize)) return false;
|
| 363 |
+
|
| 364 |
+
auto out_t = out->flat<T>();
|
| 365 |
+
auto vals_t = vals_.vec<T>();
|
| 366 |
+
auto ix_t = ix_.matrix<int64_t>();
|
| 367 |
+
const int64_t* const ix_ptr = ix_t.data();
|
| 368 |
+
|
| 369 |
+
if (dims_ == 1) {
|
| 370 |
+
// Fast path for sparse vectors.
|
| 371 |
+
const int64_t out_length = out->shape().dim_size(0);
|
| 372 |
+
for (int n = 0; n < vals_t.dimension(0); ++n) {
|
| 373 |
+
const int64_t index = internal::SubtleMustCopy(ix_ptr[n]);
|
| 374 |
+
if (!FastBoundsCheck(index, out_length)) return false;
|
| 375 |
+
out_t(index) = vals_t(n);
|
| 376 |
+
}
|
| 377 |
+
return true;
|
| 378 |
+
} else if (dims_ == 2) {
|
| 379 |
+
// Fast path for sparse matrices.
|
| 380 |
+
const auto& out_shape = out->shape();
|
| 381 |
+
const int64_t out_rows = out_shape.dim_size(0);
|
| 382 |
+
const int64_t out_cols = out_shape.dim_size(1);
|
| 383 |
+
for (int n = 0; n < vals_t.dimension(0); ++n) {
|
| 384 |
+
const int64_t row_index = internal::SubtleMustCopy(ix_ptr[n * 2]);
|
| 385 |
+
const int64_t col_index = internal::SubtleMustCopy(ix_ptr[n * 2 + 1]);
|
| 386 |
+
if (!(FastBoundsCheck(row_index, out_rows) &&
|
| 387 |
+
FastBoundsCheck(col_index, out_cols))) {
|
| 388 |
+
return false;
|
| 389 |
+
}
|
| 390 |
+
out_t(row_index * out_cols + col_index) = vals_t(n);
|
| 391 |
+
}
|
| 392 |
+
return true;
|
| 393 |
+
} else {
|
| 394 |
+
// General path for N-dimensional sparse tensors.
|
| 395 |
+
gtl::InlinedVector<int64_t, 4> strides(dims_);
|
| 396 |
+
const auto& out_shape = out->shape().dim_sizes();
|
| 397 |
+
if (dims_ > 0) {
|
| 398 |
+
strides[dims_ - 1] = 1;
|
| 399 |
+
}
|
| 400 |
+
for (int d = dims_ - 2; d >= 0; --d) {
|
| 401 |
+
strides[d] = strides[d + 1] * out_shape[d + 1];
|
| 402 |
+
}
|
| 403 |
+
|
| 404 |
+
for (int n = 0; n < vals_t.dimension(0); ++n) {
|
| 405 |
+
bool invalid_dims = false;
|
| 406 |
+
int64_t ix = 0;
|
| 407 |
+
for (int d = 0; d < dims_; ++d) {
|
| 408 |
+
const int64_t ix_n_d = internal::SubtleMustCopy(ix_ptr[n * dims_ + d]);
|
| 409 |
+
if (!FastBoundsCheck(ix_n_d, out_shape[d])) {
|
| 410 |
+
invalid_dims = true;
|
| 411 |
+
}
|
| 412 |
+
ix += strides[d] * ix_n_d;
|
| 413 |
+
}
|
| 414 |
+
if (invalid_dims) return false;
|
| 415 |
+
out_t(ix) = vals_t(n);
|
| 416 |
+
}
|
| 417 |
+
return true;
|
| 418 |
+
}
|
| 419 |
+
}
|
| 420 |
+
|
| 421 |
+
template <typename T>
|
| 422 |
+
inline SparseTensor SparseTensor::Concat(
|
| 423 |
+
const gtl::ArraySlice<SparseTensor>& tensors) {
|
| 424 |
+
DCHECK_GE(tensors.size(), size_t{1}) << "Cannot concat 0 SparseTensors";
|
| 425 |
+
const int dims = tensors[0].dims_;
|
| 426 |
+
DCHECK_GE(dims, 1) << "Cannot concat 0-dimensional SparseTensors";
|
| 427 |
+
auto order_0 = tensors[0].order();
|
| 428 |
+
const int primary_dim = order_0[0];
|
| 429 |
+
ShapeArray final_order(order_0.begin(), order_0.end());
|
| 430 |
+
ShapeArray final_shape(tensors[0].shape().begin(), tensors[0].shape().end());
|
| 431 |
+
final_shape[primary_dim] = 0; // We'll build this up as we go along.
|
| 432 |
+
int num_entries = 0;
|
| 433 |
+
|
| 434 |
+
bool fully_ordered = true;
|
| 435 |
+
for (const SparseTensor& st : tensors) {
|
| 436 |
+
DCHECK_EQ(st.dims_, dims) << "All SparseTensors must have the same rank.";
|
| 437 |
+
DCHECK_EQ(DataTypeToEnum<T>::v(), st.dtype())
|
| 438 |
+
<< "Concat requested with the wrong data type";
|
| 439 |
+
DCHECK_GE(st.order()[0], 0) << "SparseTensor must be ordered";
|
| 440 |
+
DCHECK_EQ(st.order()[0], primary_dim)
|
| 441 |
+
<< "All SparseTensors' order[0] must match. This is the concat dim.";
|
| 442 |
+
if (st.order() != final_order) fully_ordered = false;
|
| 443 |
+
const VarDimArray& st_shape = st.shape();
|
| 444 |
+
for (int d = 0; d < dims - 1; ++d) {
|
| 445 |
+
const int cdim = (d < primary_dim) ? d : d + 1;
|
| 446 |
+
DCHECK_EQ(final_shape[cdim], st_shape[cdim])
|
| 447 |
+
<< "All SparseTensors' shapes must match except on the concat dim. "
|
| 448 |
+
<< "Concat dim: " << primary_dim
|
| 449 |
+
<< ", mismatched shape at dim: " << cdim
|
| 450 |
+
<< ". Expecting shape like: [" << str_util::Join(final_shape, ",")
|
| 451 |
+
<< "] but saw shape: [" << str_util::Join(st_shape, ",") << "]";
|
| 452 |
+
}
|
| 453 |
+
|
| 454 |
+
// Update dimension of final shape
|
| 455 |
+
final_shape[primary_dim] =
|
| 456 |
+
(final_shape[primary_dim] + st_shape[primary_dim]);
|
| 457 |
+
|
| 458 |
+
num_entries += st.num_entries(); // Update number of entries
|
| 459 |
+
}
|
| 460 |
+
|
| 461 |
+
// If nonconsistent ordering among inputs, set final order to -1s.
|
| 462 |
+
if (!fully_ordered) {
|
| 463 |
+
final_order = UndefinedOrder(final_shape);
|
| 464 |
+
}
|
| 465 |
+
|
| 466 |
+
Tensor output_ix(DT_INT64, TensorShape({num_entries, dims}));
|
| 467 |
+
Tensor output_vals(DataTypeToEnum<T>::v(), TensorShape({num_entries}));
|
| 468 |
+
|
| 469 |
+
TTypes<int64_t>::Matrix ix_t = output_ix.matrix<int64_t>();
|
| 470 |
+
typename TTypes<T>::Vec vals_t = output_vals.vec<T>();
|
| 471 |
+
|
| 472 |
+
Eigen::DenseIndex offset = 0;
|
| 473 |
+
int64_t shape_offset = 0;
|
| 474 |
+
for (const SparseTensor& st : tensors) {
|
| 475 |
+
const int st_num_entries = st.num_entries();
|
| 476 |
+
|
| 477 |
+
// Fill in indices & values.
|
| 478 |
+
if (st_num_entries > 0) {
|
| 479 |
+
std::copy_n(&st.vals_.vec<T>()(0), st_num_entries, &vals_t(offset));
|
| 480 |
+
|
| 481 |
+
const auto* st_ix = &st.ix_.matrix<int64_t>()(0, 0);
|
| 482 |
+
auto* ix_out = &ix_t(offset, 0);
|
| 483 |
+
for (std::size_t i = 0; i < st_num_entries * dims; ++i) {
|
| 484 |
+
*ix_out++ = *st_ix++ + ((i % dims == primary_dim) ? shape_offset : 0);
|
| 485 |
+
}
|
| 486 |
+
}
|
| 487 |
+
|
| 488 |
+
offset += st_num_entries;
|
| 489 |
+
shape_offset += st.shape()[primary_dim];
|
| 490 |
+
}
|
| 491 |
+
|
| 492 |
+
return SparseTensor(output_ix, output_vals, final_shape, final_order);
|
| 493 |
+
}
|
| 494 |
+
|
| 495 |
+
template <typename T>
|
| 496 |
+
inline Status SparseTensor::Split(const SparseTensor& input_tensor,
|
| 497 |
+
const int split_dim, const int num_split,
|
| 498 |
+
std::vector<SparseTensor>* result) {
|
| 499 |
+
std::vector<Tensor> output_indices;
|
| 500 |
+
std::vector<Tensor> output_values;
|
| 501 |
+
std::vector<TensorShape> output_shapes;
|
| 502 |
+
output_indices.reserve(num_split);
|
| 503 |
+
output_values.reserve(num_split);
|
| 504 |
+
output_shapes.reserve(num_split);
|
| 505 |
+
|
| 506 |
+
std::vector<typename TTypes<int64_t>::Matrix> output_indices_t;
|
| 507 |
+
std::vector<typename TTypes<T>::Vec> output_values_t;
|
| 508 |
+
output_indices_t.reserve(num_split);
|
| 509 |
+
output_values_t.reserve(num_split);
|
| 510 |
+
auto input_values_t = input_tensor.values().vec<T>();
|
| 511 |
+
auto input_indices_t = input_tensor.indices().matrix<int64_t>();
|
| 512 |
+
|
| 513 |
+
std::vector<int> num_values(num_split, 0);
|
| 514 |
+
const int num_dim = input_tensor.shape().size();
|
| 515 |
+
const int split_dim_size = input_tensor.shape()[split_dim];
|
| 516 |
+
const int split_size = split_dim_size / num_split;
|
| 517 |
+
|
| 518 |
+
if (!(num_split > 0 && num_split <= split_dim_size)) {
|
| 519 |
+
return errors::InvalidArgument("num_split must be in the interval (0, ",
|
| 520 |
+
split_dim_size, "]");
|
| 521 |
+
}
|
| 522 |
+
if (!(split_dim >= 0 && split_dim < num_dim)) {
|
| 523 |
+
return errors::InvalidArgument("num_dim must be in the interval [0, ",
|
| 524 |
+
num_dim, ")");
|
| 525 |
+
}
|
| 526 |
+
|
| 527 |
+
const int residual = split_dim_size % num_split;
|
| 528 |
+
for (int i = 0; i < input_tensor.indices().dim_size(0); ++i) {
|
| 529 |
+
const int dim = input_tensor.indices().matrix<int64_t>()(i, split_dim);
|
| 530 |
+
int slice_index = GetSliceIndex(dim, split_size, residual);
|
| 531 |
+
if (slice_index >= num_values.size()) {
|
| 532 |
+
return errors::InvalidArgument("Slice index ", slice_index,
|
| 533 |
+
" is larger than num_split.");
|
| 534 |
+
}
|
| 535 |
+
num_values[slice_index]++;
|
| 536 |
+
}
|
| 537 |
+
|
| 538 |
+
for (int i = 0; i < num_split; ++i) {
|
| 539 |
+
// TODO(ataei): Pass an allocator to avoid allocating large memory buffer.
|
| 540 |
+
output_indices.emplace_back(DT_INT64,
|
| 541 |
+
TensorShape({num_values[i], num_dim}));
|
| 542 |
+
output_values.emplace_back(DataTypeToEnum<T>::v(),
|
| 543 |
+
TensorShape({num_values[i]}));
|
| 544 |
+
output_shapes.emplace_back(input_tensor.shape());
|
| 545 |
+
output_indices_t.emplace_back(output_indices[i].matrix<int64_t>());
|
| 546 |
+
output_values_t.emplace_back(output_values[i].vec<T>());
|
| 547 |
+
const int size = GetSliceShape(i, split_size, residual);
|
| 548 |
+
output_shapes[i].set_dim(split_dim, size);
|
| 549 |
+
}
|
| 550 |
+
|
| 551 |
+
std::vector<int> values_inserted_in_slice(num_split, 0);
|
| 552 |
+
for (int i = 0; i < input_tensor.indices().dim_size(0); ++i) {
|
| 553 |
+
const int dim = input_indices_t(i, split_dim);
|
| 554 |
+
const int slice_index = GetSliceIndex(dim, split_size, residual);
|
| 555 |
+
const int slice_dim = values_inserted_in_slice[slice_index]++;
|
| 556 |
+
output_values_t[slice_index](slice_dim) = input_values_t(i);
|
| 557 |
+
for (int j = 0; j < num_dim; ++j) {
|
| 558 |
+
const int64_t original_dim = input_indices_t(i, j);
|
| 559 |
+
output_indices_t[slice_index](slice_dim, j) =
|
| 560 |
+
(j == split_dim)
|
| 561 |
+
? GetDimensionInSlice(original_dim, split_size, residual)
|
| 562 |
+
: original_dim;
|
| 563 |
+
}
|
| 564 |
+
}
|
| 565 |
+
|
| 566 |
+
result->clear();
|
| 567 |
+
result->reserve(num_split);
|
| 568 |
+
for (int i = 0; i < num_split; ++i) {
|
| 569 |
+
SparseTensor tensor;
|
| 570 |
+
Status create_status =
|
| 571 |
+
Create(output_indices[i], output_values[i], output_shapes[i], &tensor);
|
| 572 |
+
if (!create_status.ok()) {
|
| 573 |
+
return create_status;
|
| 574 |
+
}
|
| 575 |
+
result->push_back(std::move(tensor));
|
| 576 |
+
}
|
| 577 |
+
return OkStatus();
|
| 578 |
+
}
|
| 579 |
+
|
| 580 |
+
template <typename T>
|
| 581 |
+
inline StatusOr<SparseTensor> SparseTensor::Slice(
|
| 582 |
+
const SparseTensor& input_tensor, const gtl::ArraySlice<int64_t> start,
|
| 583 |
+
const gtl::ArraySlice<int64_t> size) {
|
| 584 |
+
TensorShape output_shape(input_tensor.shape());
|
| 585 |
+
|
| 586 |
+
const int dims = input_tensor.dims();
|
| 587 |
+
for (int dim = 0; dim < dims; dim++) {
|
| 588 |
+
// Determine the size of the result; if the selected slice goes beyond the
|
| 589 |
+
// input boundary, the result will correspond to the size of the overlap
|
| 590 |
+
// between the input and the selected slice.
|
| 591 |
+
const int64_t input_size = output_shape.dim_size(dim);
|
| 592 |
+
const int64_t start_index = start[dim];
|
| 593 |
+
const int64_t slice_size = size[dim];
|
| 594 |
+
|
| 595 |
+
if (start_index < input_size - slice_size) {
|
| 596 |
+
// The entire selection is within input boundaries.
|
| 597 |
+
TF_RETURN_IF_ERROR(output_shape.SetDimWithStatus(dim, slice_size));
|
| 598 |
+
} else if (start_index < input_size) {
|
| 599 |
+
// The selection starts within input boundaries, but goes beyond them.
|
| 600 |
+
TF_RETURN_IF_ERROR(
|
| 601 |
+
output_shape.SetDimWithStatus(dim, input_size - start_index));
|
| 602 |
+
} else {
|
| 603 |
+
// The selection is entirely out of input boundaries.
|
| 604 |
+
TF_RETURN_IF_ERROR(output_shape.SetDimWithStatus(dim, 0));
|
| 605 |
+
}
|
| 606 |
+
}
|
| 607 |
+
|
| 608 |
+
auto input_indices_t = input_tensor.indices().matrix<int64_t>();
|
| 609 |
+
auto input_values_t = input_tensor.values().vec<T>();
|
| 610 |
+
|
| 611 |
+
// Find the number of indices that fall inside start and size.
|
| 612 |
+
int count = 0;
|
| 613 |
+
for (int i = 0; i < input_tensor.indices().dim_size(0); i++) {
|
| 614 |
+
// The following will check to see if an input is within the
|
| 615 |
+
// range specified by start and size.
|
| 616 |
+
// The for loop below iterates through all dimensions. In case
|
| 617 |
+
// the index falls outside of the start and size at any dimension,
|
| 618 |
+
// it will be considered as a "no hit" (hit = false). In this
|
| 619 |
+
// case, it will not be counted as the index that fall inside
|
| 620 |
+
// the range specified by start and size.
|
| 621 |
+
bool hit = true;
|
| 622 |
+
for (int dim = 0; dim < dims; dim++) {
|
| 623 |
+
if (!(start[dim] <= input_indices_t(i, dim) &&
|
| 624 |
+
input_indices_t(i, dim) < start[dim] + size[dim])) {
|
| 625 |
+
hit = false;
|
| 626 |
+
break;
|
| 627 |
+
}
|
| 628 |
+
}
|
| 629 |
+
if (!hit) {
|
| 630 |
+
continue;
|
| 631 |
+
}
|
| 632 |
+
count++;
|
| 633 |
+
}
|
| 634 |
+
|
| 635 |
+
Tensor output_values(DataTypeToEnum<T>::v(), TensorShape({count}));
|
| 636 |
+
Tensor output_indices(DT_INT64, TensorShape({count, dims}));
|
| 637 |
+
|
| 638 |
+
auto output_values_t = output_values.vec<T>();
|
| 639 |
+
auto output_indices_t = output_indices.matrix<int64_t>();
|
| 640 |
+
|
| 641 |
+
// Obtain the output indices that fall inside start and size.
|
| 642 |
+
int index = 0;
|
| 643 |
+
for (int i = 0; i < input_tensor.indices().dim_size(0) && index < count;
|
| 644 |
+
i++) {
|
| 645 |
+
// The logic here is similar as the above except that the above
|
| 646 |
+
// only count the number of indices while here we actually generate
|
| 647 |
+
// the output.
|
| 648 |
+
bool hit = true;
|
| 649 |
+
for (int dim = 0; dim < dims; dim++) {
|
| 650 |
+
if (!(start[dim] <= input_indices_t(i, dim) &&
|
| 651 |
+
input_indices_t(i, dim) < start[dim] + size[dim])) {
|
| 652 |
+
hit = false;
|
| 653 |
+
break;
|
| 654 |
+
}
|
| 655 |
+
}
|
| 656 |
+
if (!hit) {
|
| 657 |
+
continue;
|
| 658 |
+
}
|
| 659 |
+
output_values_t(index) = input_values_t(i);
|
| 660 |
+
for (int dim = 0; dim < dims; dim++) {
|
| 661 |
+
output_indices_t(index, dim) = input_indices_t(i, dim) - start[dim];
|
| 662 |
+
}
|
| 663 |
+
index++;
|
| 664 |
+
}
|
| 665 |
+
|
| 666 |
+
return SparseTensor(output_indices, output_values, output_shape);
|
| 667 |
+
}
|
| 668 |
+
|
| 669 |
+
} // namespace sparse
|
| 670 |
+
} // namespace tensorflow
|
| 671 |
+
|
| 672 |
+
#endif // TENSORFLOW_CORE_UTIL_SPARSE_SPARSE_TENSOR_H_
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/tensor_bundle/byte_swap_array.h
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
|
| 16 |
+
#ifndef TENSORFLOW_CORE_UTIL_TENSOR_BUNDLE_BYTE_SWAP_ARRAY_H_
|
| 17 |
+
#define TENSORFLOW_CORE_UTIL_TENSOR_BUNDLE_BYTE_SWAP_ARRAY_H_
|
| 18 |
+
|
| 19 |
+
#include "tensorflow/core/platform/byte_order.h"
|
| 20 |
+
#include "tensorflow/core/platform/errors.h"
|
| 21 |
+
#include "tensorflow/core/platform/status.h"
|
| 22 |
+
#include "tsl/util/byte_swap_array.h"
|
| 23 |
+
|
| 24 |
+
namespace tensorflow {
|
| 25 |
+
|
| 26 |
+
using tsl::ByteSwapArray; // NOLINT(misc-unused-using-decls)
|
| 27 |
+
|
| 28 |
+
} // namespace tensorflow
|
| 29 |
+
|
| 30 |
+
#endif // TENSORFLOW_CORE_UTIL_TENSOR_BUNDLE_BYTE_SWAP_ARRAY_H_
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/tensor_bundle/byte_swap_tensor.h
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
|
| 16 |
+
#ifndef TENSORFLOW_CORE_UTIL_TENSOR_BUNDLE_BYTE_SWAP_TENSOR_H_
|
| 17 |
+
#define TENSORFLOW_CORE_UTIL_TENSOR_BUNDLE_BYTE_SWAP_TENSOR_H_
|
| 18 |
+
|
| 19 |
+
#include "tensorflow/core/framework/node_def.pb.h"
|
| 20 |
+
#include "tensorflow/core/framework/tensor.h"
|
| 21 |
+
#include "tensorflow/core/platform/byte_order.h"
|
| 22 |
+
#include "tensorflow/core/protobuf/meta_graph.pb.h"
|
| 23 |
+
#include "tensorflow/core/util/tensor_bundle/byte_swap_array.h"
|
| 24 |
+
|
| 25 |
+
namespace tensorflow {
|
| 26 |
+
|
| 27 |
+
// Check if a data type is byte swappable.
|
| 28 |
+
bool IsByteSwappable(DataType dtype);
|
| 29 |
+
|
| 30 |
+
// Byte-swap a tensor's backing buffer in place.
|
| 31 |
+
//
|
| 32 |
+
// Args:
|
| 33 |
+
// t: Tensor to be modified IN PLACE. Any tensors that share a backing
|
| 34 |
+
// buffer with this one will also end up byte-swapped.
|
| 35 |
+
// Returns: OkStatus() on success, -1 otherwise
|
| 36 |
+
// TODO(frreiss): Should this be a member of the Tensor class?
|
| 37 |
+
Status ByteSwapTensor(Tensor *t);
|
| 38 |
+
|
| 39 |
+
// Swap tensor_content field of Const Op Tensors in the named functions
|
| 40 |
+
// in NodeDef
|
| 41 |
+
Status ByteSwapTensorContentInNode(NodeDef& node);
|
| 42 |
+
|
| 43 |
+
// Swap tensor_content field of Const Op Tensors in the named functions
|
| 44 |
+
// in MetaGraphDef
|
| 45 |
+
Status ByteSwapTensorContentInMetaGraphDef(MetaGraphDef* meta_graph_def);
|
| 46 |
+
|
| 47 |
+
// Swap tensor_content field of Const Op Tensors in the named functions
|
| 48 |
+
// in GraphDef
|
| 49 |
+
Status ByteSwapTensorContentInGraphDef(GraphDef* graph_def);
|
| 50 |
+
|
| 51 |
+
} // namespace tensorflow
|
| 52 |
+
|
| 53 |
+
#endif // TENSORFLOW_CORE_UTIL_TENSOR_BUNDLE_BYTE_SWAP_TENSOR_H_
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/tensor_bundle/naming.h
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
|
| 16 |
+
// A minimal library exposing the naming logic used in tensor_bundle.
|
| 17 |
+
//
|
| 18 |
+
// A tensor bundle contains a metadata file and sharded data files, which all
|
| 19 |
+
// share a common pathname prefix.
|
| 20 |
+
//
|
| 21 |
+
// Given the prefix, the actual pathnames of the files can be queried via:
|
| 22 |
+
//
|
| 23 |
+
// MetaFilename(prefix): pathname of the metadata file.
|
| 24 |
+
// DataFilename(prefix, shard_id, num_shards): pathname of a data file.
|
| 25 |
+
//
|
| 26 |
+
// Typical usage includes forming a filepattern to match files on disk:
|
| 27 |
+
//
|
| 28 |
+
// // To find the unique metadata file.
|
| 29 |
+
// const string metadata_file = MetaFilename("/fs/train/ckpt-step");
|
| 30 |
+
// Env::Default()->GetMatchingFiles(metadata_file, &path);
|
| 31 |
+
//
|
| 32 |
+
// Regexp can also be used: e.g. R"<prefix>.data-\d{5}-of-\d{5}" for data files.
|
| 33 |
+
|
| 34 |
+
#ifndef TENSORFLOW_CORE_UTIL_TENSOR_BUNDLE_NAMING_H_
|
| 35 |
+
#define TENSORFLOW_CORE_UTIL_TENSOR_BUNDLE_NAMING_H_
|
| 36 |
+
|
| 37 |
+
#include "tensorflow/core/lib/core/stringpiece.h"
|
| 38 |
+
#include "tensorflow/core/platform/types.h"
|
| 39 |
+
|
| 40 |
+
namespace tensorflow {
|
| 41 |
+
|
| 42 |
+
string MetaFilename(StringPiece prefix);
|
| 43 |
+
string DataFilename(StringPiece prefix, int32_t shard_id, int32_t num_shards);
|
| 44 |
+
|
| 45 |
+
} // namespace tensorflow
|
| 46 |
+
|
| 47 |
+
#endif // TENSORFLOW_CORE_UTIL_TENSOR_BUNDLE_NAMING_H_
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/tensor_bundle/tensor_bundle.h
ADDED
|
@@ -0,0 +1,379 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
|
| 16 |
+
// A tensor bundle is a set of immutable persistent files storing a set of named
|
| 17 |
+
// tensors. It is designed for checkpointing TensorFlow tensors.
|
| 18 |
+
//
|
| 19 |
+
// The paths of the managed files share a common prefix; e.g., with the prefix:
|
| 20 |
+
// /fs/model/train/ckpt-step/ckpt
|
| 21 |
+
//
|
| 22 |
+
// the bundle may contain a metadata file, and sharded data files:
|
| 23 |
+
// /fs/model/train/ckpt-step/
|
| 24 |
+
// ckpt.index
|
| 25 |
+
// ckpt.data-00000-of-00020
|
| 26 |
+
// ckpt.data-00001-of-00020
|
| 27 |
+
// ...
|
| 28 |
+
// ckpt.data-00019-of-00020
|
| 29 |
+
//
|
| 30 |
+
// The ".index" file is a string-string immutable table
|
| 31 |
+
// (tensorflow::table::Table). Each key is a name of a tensor and its value is
|
| 32 |
+
// a serialized BundleEntryProto. Each BundleEntryProto describes the metadata
|
| 33 |
+
// of a tensor: which of the "data" files contains the content of a tensor, the
|
| 34 |
+
// offset into that file, checksum, some auxiliary data, etc.
|
| 35 |
+
//
|
| 36 |
+
// A tensor bundle can be accessed randomly using a BundleReader. Usage:
|
| 37 |
+
//
|
| 38 |
+
// BundleReader reader(env, "/fs/model/train/ckpt-step/ckpt");
|
| 39 |
+
// reader.Lookup("name", &tensor);
|
| 40 |
+
//
|
| 41 |
+
// A tensor bundle can be built using BundleWriter. Each BundleWriter builds a
|
| 42 |
+
// single data file bundle. Multiple bundles can then be merged by
|
| 43 |
+
// MergeBundles() without reading and writing large chunk of data: it reads the
|
| 44 |
+
// metadata files and outputs a single merged metadata. Typical usage:
|
| 45 |
+
//
|
| 46 |
+
// worker 0:
|
| 47 |
+
// BundleWriter writer(env, "/fs/model/train/ckpt-step/tmp/worker0-step");
|
| 48 |
+
// writer.Add(...); // Adds the tensors on this worker.
|
| 49 |
+
// writer.Finish(); // Flushes.
|
| 50 |
+
// worker 1:
|
| 51 |
+
// BundleWriter writer(env, "/fs/model/train/ckpt-step/tmp/worker1-step");
|
| 52 |
+
// writer.Add(...);
|
| 53 |
+
// writer.Finish();
|
| 54 |
+
// worker 2:
|
| 55 |
+
// MergeBundles(env,
|
| 56 |
+
// {"/fs/model/train/ckpt-step/tmp/worker0-step",
|
| 57 |
+
// "/fs/model/train/ckpt-step/tmp/worker1-step"},
|
| 58 |
+
// "/fs/model/train/ckpt-step/ckpt" /* merged prefix */);
|
| 59 |
+
//
|
| 60 |
+
|
| 61 |
+
#ifndef TENSORFLOW_CORE_UTIL_TENSOR_BUNDLE_TENSOR_BUNDLE_H_
|
| 62 |
+
#define TENSORFLOW_CORE_UTIL_TENSOR_BUNDLE_TENSOR_BUNDLE_H_
|
| 63 |
+
|
| 64 |
+
#include <cstdint>
|
| 65 |
+
#include <map>
|
| 66 |
+
#include <memory>
|
| 67 |
+
#include <string>
|
| 68 |
+
#include <unordered_map>
|
| 69 |
+
#include <vector>
|
| 70 |
+
|
| 71 |
+
#include "absl/algorithm/container.h"
|
| 72 |
+
#include "absl/container/flat_hash_map.h"
|
| 73 |
+
#include "absl/functional/function_ref.h"
|
| 74 |
+
#include "absl/strings/string_view.h"
|
| 75 |
+
#include "tensorflow/core/framework/tensor.h"
|
| 76 |
+
#include "tensorflow/core/framework/tensor_shape.h"
|
| 77 |
+
#include "tensorflow/core/framework/tensor_slice.h"
|
| 78 |
+
#include "tensorflow/core/lib/gtl/array_slice.h"
|
| 79 |
+
#include "tensorflow/core/lib/io/cache.h"
|
| 80 |
+
#include "tensorflow/core/lib/io/inputbuffer.h"
|
| 81 |
+
#include "tensorflow/core/lib/io/iterator.h"
|
| 82 |
+
#include "tensorflow/core/lib/io/table.h"
|
| 83 |
+
#include "tensorflow/core/platform/env.h"
|
| 84 |
+
#include "tensorflow/core/platform/file_system.h"
|
| 85 |
+
#include "tensorflow/core/platform/macros.h"
|
| 86 |
+
#include "tensorflow/core/platform/status.h"
|
| 87 |
+
#include "tensorflow/core/platform/tstring.h"
|
| 88 |
+
#include "tensorflow/core/protobuf/tensor_bundle.pb.h"
|
| 89 |
+
#include "tensorflow/core/util/tensor_slice_set.h"
|
| 90 |
+
#include "tsl/lib/io/buffered_file.h"
|
| 91 |
+
#include "tsl/platform/errors.h"
|
| 92 |
+
|
| 93 |
+
namespace tensorflow {
|
| 94 |
+
|
| 95 |
+
// Versioning of the tensor bundle format.
|
| 96 |
+
// Follows the same rules as 3p/tf/core/public/version.h.
|
| 97 |
+
//
|
| 98 |
+
// History:
|
| 99 |
+
// 0. Any tensor bundles produced before this field was added.
|
| 100 |
+
// 1. Added this field (2016-09-14).
|
| 101 |
+
extern const int kTensorBundleMinProducer;
|
| 102 |
+
extern const int kTensorBundleMinConsumer;
|
| 103 |
+
extern const int kTensorBundleVersion;
|
| 104 |
+
|
| 105 |
+
// The empty string, hence always the first key in the metadata table. Its
|
| 106 |
+
// corresponding value is a BundleHeaderProto.
|
| 107 |
+
extern const char* const kHeaderEntryKey;
|
| 108 |
+
|
| 109 |
+
// Builds a string-string table of tensor names to BundleEntryProto (metadata).
|
| 110 |
+
//
|
| 111 |
+
// On construction, attempts to create a directory given by the dirname of
|
| 112 |
+
// "prefix", so "status()" must be checked before calling any member functions.
|
| 113 |
+
//
|
| 114 |
+
// All threads accessing the same BundleWriter must synchronize.
|
| 115 |
+
class BundleWriter {
|
| 116 |
+
public:
|
| 117 |
+
struct Options {
|
| 118 |
+
Options() {}
|
| 119 |
+
// Alignment, in bytes, for tensor data.
|
| 120 |
+
// Must be >= 1. The default size of 1 densely packs tensors.
|
| 121 |
+
int data_alignment{1};
|
| 122 |
+
};
|
| 123 |
+
BundleWriter(Env* env, absl::string_view prefix,
|
| 124 |
+
const Options& options = Options());
|
| 125 |
+
|
| 126 |
+
// Adds the tensor "val" under key "key".
|
| 127 |
+
// Across calls "key" must be unique but can be added in any order.
|
| 128 |
+
Status Add(absl::string_view key, const Tensor& val);
|
| 129 |
+
|
| 130 |
+
// Partitioned variables support.
|
| 131 |
+
// A slice of a full tensor is stored in two entries in the metadata table:
|
| 132 |
+
//
|
| 133 |
+
// full_tensor_key -> BundleEntryProto, describing all stored slices
|
| 134 |
+
// of this full tensor. Does not append to the data
|
| 135 |
+
// file.
|
| 136 |
+
// encoded slice key -> BundleEntryProto, describing one particular slice.
|
| 137 |
+
// Appends values of this slice to the data file.
|
| 138 |
+
//
|
| 139 |
+
// Slices of a full tensor can be added in any order.
|
| 140 |
+
//
|
| 141 |
+
// If a full tensor has slices placed on N devices and N BundleWriter's are
|
| 142 |
+
// concurrently used, the caller must use MergeBundles() to ensure that a
|
| 143 |
+
// consistent entry for "full_tensor_key" is produced.
|
| 144 |
+
//
|
| 145 |
+
// Returns an error if the same slice is added the second time.
|
| 146 |
+
Status AddSlice(absl::string_view full_tensor_key,
|
| 147 |
+
const TensorShape& full_tensor_shape,
|
| 148 |
+
const TensorSlice& slice_spec, const Tensor& slice_tensor);
|
| 149 |
+
|
| 150 |
+
// Finishes the writer and flushes.
|
| 151 |
+
Status Finish() TF_MUST_USE_RESULT;
|
| 152 |
+
|
| 153 |
+
Status status() const { return status_; }
|
| 154 |
+
|
| 155 |
+
private:
|
| 156 |
+
Env* const env_; // Not owned.
|
| 157 |
+
const Options options_;
|
| 158 |
+
const std::string prefix_;
|
| 159 |
+
std::string metadata_path_;
|
| 160 |
+
std::string data_path_;
|
| 161 |
+
bool use_temp_file_;
|
| 162 |
+
std::unique_ptr<tsl::BufferedWritableFile> out_;
|
| 163 |
+
int64_t size_; // Number of bytes written into out_.
|
| 164 |
+
std::map<std::string, BundleEntryProto> entries_;
|
| 165 |
+
Status status_;
|
| 166 |
+
|
| 167 |
+
BundleWriter(const BundleWriter&) = delete;
|
| 168 |
+
void operator=(const BundleWriter&) = delete;
|
| 169 |
+
};
|
| 170 |
+
|
| 171 |
+
// Merges a set of bundles (given their prefixes) into a single bundle with the
|
| 172 |
+
// given "merged_prefix". The merged metadata is guaranteed to be consistent.
|
| 173 |
+
//
|
| 174 |
+
// If there are N bundles in "prefixes", during the merge the data files will be
|
| 175 |
+
// renamed to contain a proper sharded file spec, with num_shards set to the sum
|
| 176 |
+
// of num_shards across the N input bundles.
|
| 177 |
+
//
|
| 178 |
+
// The caller should only rely on the metadata file of the merged bundle to
|
| 179 |
+
// query information about a tensor. In particular, this function does not
|
| 180 |
+
// guarantee not to re-order the input data files.
|
| 181 |
+
//
|
| 182 |
+
// Once merged, makes a best effort to delete the old metadata files.
|
| 183 |
+
// Returns OK iff all bundles are successfully merged.
|
| 184 |
+
//
|
| 185 |
+
// "allow_missing_files": If set to true, merges "prefixes" as long as
|
| 186 |
+
// at least one file exists. (Defaults to false.)
|
| 187 |
+
//
|
| 188 |
+
// Returns an InvalidArgumentError when "allow_missing_files" is set to true
|
| 189 |
+
// and all data files named in "prefixes" do not exist.
|
| 190 |
+
//
|
| 191 |
+
// Returns a NotFoundError when "allow_missing_files" is set to false and
|
| 192 |
+
// any data file named in "prefixes" does not exist.
|
| 193 |
+
Status MergeBundles(Env* env, gtl::ArraySlice<tstring> prefixes,
|
| 194 |
+
absl::string_view merged_prefix,
|
| 195 |
+
bool allow_missing_files = false);
|
| 196 |
+
|
| 197 |
+
// On construction, silently attempts to read the metadata associated with
|
| 198 |
+
// "prefix". If caller intends to call any function afterwards, "status()"
|
| 199 |
+
// must be checked.
|
| 200 |
+
// All threads accessing the same BundleReader must synchronize.
|
| 201 |
+
class BundleReader {
|
| 202 |
+
public:
|
| 203 |
+
BundleReader(Env* const env, absl::string_view prefix,
|
| 204 |
+
bool enable_multi_threading_for_testing = false);
|
| 205 |
+
~BundleReader();
|
| 206 |
+
|
| 207 |
+
// Is ok() iff the reader construction is successful (completed the read of
|
| 208 |
+
// the metadata).
|
| 209 |
+
Status status() const { return status_; }
|
| 210 |
+
|
| 211 |
+
// Queries whether the bundle contains an entry keyed by "key". Calls Seek()
|
| 212 |
+
// internally, so this call invalidates the reader's current position.
|
| 213 |
+
// REQUIRES: status().ok()
|
| 214 |
+
bool Contains(absl::string_view key);
|
| 215 |
+
|
| 216 |
+
// Sorts a `container` of tensors to read such that when `Seek(key)` is called
|
| 217 |
+
// on the elements of the sorted container, the underlying file access is
|
| 218 |
+
// sequential. Sorting can greatly improve overall read speed.
|
| 219 |
+
//
|
| 220 |
+
// `get_key` should be a function that when passed an element in `container`,
|
| 221 |
+
// returns the `key` of the tensor.
|
| 222 |
+
//
|
| 223 |
+
// REQUIRES: status().ok()
|
| 224 |
+
template <class T>
|
| 225 |
+
Status SortForSequentialAccess(
|
| 226 |
+
std::vector<T>& container,
|
| 227 |
+
absl::FunctionRef<std::string(const T&)> get_key);
|
| 228 |
+
|
| 229 |
+
// Looks up the dtype and the shape of the tensor keyed by "key".
|
| 230 |
+
// REQUIRES: status().ok()
|
| 231 |
+
Status LookupDtypeAndShape(absl::string_view key, DataType* dtype,
|
| 232 |
+
TensorShape* shape) TF_MUST_USE_RESULT;
|
| 233 |
+
|
| 234 |
+
// Looks up the shape of the tensor keyed by "key".
|
| 235 |
+
// Clears "shape" if not found.
|
| 236 |
+
// REQUIRES: status().ok()
|
| 237 |
+
Status LookupTensorShape(absl::string_view key,
|
| 238 |
+
TensorShape* shape) TF_MUST_USE_RESULT;
|
| 239 |
+
|
| 240 |
+
// Looks up the tensor keyed by "key". If "key" refers to a partitioned
|
| 241 |
+
// tensor, attempts to look up the full contents using all stored slices.
|
| 242 |
+
//
|
| 243 |
+
// Caller must make sure "val" has the same shape and dtype as the
|
| 244 |
+
// corresponding contents, so that its buffer can be filled without needing
|
| 245 |
+
// extra allocation. These can be queried via "LookupDtypeAndShape()".
|
| 246 |
+
//
|
| 247 |
+
// On error, "val" may contain nonsense data. Returns a NotFound error if
|
| 248 |
+
// tensor keyed by "key" does not exist in this bundle.
|
| 249 |
+
//
|
| 250 |
+
// Validates the stored crc32c checksum against the restored bytes.
|
| 251 |
+
// REQUIRES: status().ok()
|
| 252 |
+
Status Lookup(absl::string_view key, Tensor* val) TF_MUST_USE_RESULT;
|
| 253 |
+
|
| 254 |
+
// Looks up the tensor pointed to by the internal iterator.
|
| 255 |
+
//
|
| 256 |
+
// On error, "val" may contain nonsense data.
|
| 257 |
+
//
|
| 258 |
+
// Validates the stored crc32c checksum against the restored bytes.
|
| 259 |
+
// REQUIRES: status().ok() && Valid()
|
| 260 |
+
Status ReadCurrent(Tensor* val) TF_MUST_USE_RESULT;
|
| 261 |
+
|
| 262 |
+
// Looks up the slices of the tensor keyed by "key". On OK, "slices"
|
| 263 |
+
// is non-empty if and only if the tensor is a partitioned tensor.
|
| 264 |
+
//
|
| 265 |
+
// Warning - there is no guaranteed ordering for the returned slices, so
|
| 266 |
+
// a slice with a larger start index in some dimension could come before
|
| 267 |
+
// another slice with a smaller start index in the same dimension.
|
| 268 |
+
// REQUIRES: status().ok()
|
| 269 |
+
Status LookupTensorSlices(absl::string_view key,
|
| 270 |
+
std::vector<TensorSlice>* slices)
|
| 271 |
+
TF_MUST_USE_RESULT;
|
| 272 |
+
|
| 273 |
+
// Looks up a specific slice of a partitioned tensor.
|
| 274 |
+
// It is only required that the stored slices cover the requested slice,
|
| 275 |
+
// namely "slice_spec" is a subset of the union of the stored slices.
|
| 276 |
+
// REQUIRES: status().ok()
|
| 277 |
+
Status LookupSlice(absl::string_view full_tensor_key,
|
| 278 |
+
const TensorSlice& slice_spec,
|
| 279 |
+
Tensor* val) TF_MUST_USE_RESULT;
|
| 280 |
+
|
| 281 |
+
// Seeks to the first position in the bundle whose key is no less than "key".
|
| 282 |
+
// REQUIRES: status().ok()
|
| 283 |
+
void Seek(absl::string_view key) { return iter_->Seek(key); }
|
| 284 |
+
// Moves to the next position in the bundle.
|
| 285 |
+
// REQUIRES: status().ok()
|
| 286 |
+
void Next() const { iter_->Next(); }
|
| 287 |
+
// Returns true iff the reader is positioned to a key/val pair.
|
| 288 |
+
// REQUIRES: status().ok()
|
| 289 |
+
bool Valid() const { return iter_->Valid(); }
|
| 290 |
+
|
| 291 |
+
// Returns the key at the current position.
|
| 292 |
+
// REQUIRES: status().ok() && Valid()
|
| 293 |
+
absl::string_view key() const { return iter_->key(); }
|
| 294 |
+
// Returns the raw value at the current position.
|
| 295 |
+
// REQUIRES: status().ok() && Valid()
|
| 296 |
+
absl::string_view value() const { return iter_->value(); }
|
| 297 |
+
|
| 298 |
+
std::string DebugString();
|
| 299 |
+
|
| 300 |
+
private:
|
| 301 |
+
// Seeks for "key" and reads the metadata proto.
|
| 302 |
+
// On non-OK return, clears "entry" for the caller.
|
| 303 |
+
// REQUIRES: status().ok()
|
| 304 |
+
Status GetBundleEntryProto(absl::string_view key,
|
| 305 |
+
BundleEntryProto* entry) TF_MUST_USE_RESULT;
|
| 306 |
+
|
| 307 |
+
// Reads the tensor value described by the metadata proto "entry".
|
| 308 |
+
// Usage for "val" follows the comment of "Lookup()".
|
| 309 |
+
Status GetValue(const BundleEntryProto& entry,
|
| 310 |
+
Tensor* val) TF_MUST_USE_RESULT;
|
| 311 |
+
|
| 312 |
+
// Reads the slice described by "slice_spec". The corresponding full tensor
|
| 313 |
+
// has key "ful_tensor_key" and metadata proto "full_tensor_entry".
|
| 314 |
+
// REQUIRES: full_tensor_entry.slices_size() > 0
|
| 315 |
+
Status GetSliceValue(absl::string_view full_tensor_key,
|
| 316 |
+
const BundleEntryProto& full_tensor_entry,
|
| 317 |
+
const TensorSlice& slice_spec,
|
| 318 |
+
Tensor* val) TF_MUST_USE_RESULT;
|
| 319 |
+
|
| 320 |
+
Env* env_; // Not owned.
|
| 321 |
+
const std::string prefix_;
|
| 322 |
+
|
| 323 |
+
Status status_;
|
| 324 |
+
RandomAccessFile* metadata_; // Owned.
|
| 325 |
+
table::Table* table_;
|
| 326 |
+
table::Cache* index_cache_;
|
| 327 |
+
table::Iterator* iter_;
|
| 328 |
+
// Owned the InputBuffer objects and their underlying RandomAccessFile's.
|
| 329 |
+
std::unordered_map<int32_t, io::InputBuffer*> data_;
|
| 330 |
+
|
| 331 |
+
// Maps each partitioned tensor's key to its stored slices (represented in a
|
| 332 |
+
// TensorSliceSet). Populated on-demand.
|
| 333 |
+
std::unordered_map<std::string, checkpoint::TensorSliceSet*> tensor_slices_;
|
| 334 |
+
|
| 335 |
+
// Expected number of data file shards in the bundle. Extracted by reading
|
| 336 |
+
// the header entry in the metadata table.
|
| 337 |
+
int num_shards_;
|
| 338 |
+
|
| 339 |
+
// Flag that this class sets to true when the endianness of the target bundle
|
| 340 |
+
// differs from that of the current system's processor architecture.
|
| 341 |
+
bool need_to_swap_bytes_;
|
| 342 |
+
|
| 343 |
+
friend class TensorBundleAlignmentTest; // For testing data alignment.
|
| 344 |
+
|
| 345 |
+
bool enable_multi_threading_for_testing_ = false;
|
| 346 |
+
|
| 347 |
+
BundleReader(const BundleReader&) = delete;
|
| 348 |
+
void operator=(const BundleReader&) = delete;
|
| 349 |
+
};
|
| 350 |
+
|
| 351 |
+
template <class T>
|
| 352 |
+
Status BundleReader::SortForSequentialAccess(
|
| 353 |
+
std::vector<T>& container,
|
| 354 |
+
absl::FunctionRef<std::string(const T&)> get_key) {
|
| 355 |
+
struct FileOffset {
|
| 356 |
+
int32_t shard_id;
|
| 357 |
+
int64_t offset;
|
| 358 |
+
};
|
| 359 |
+
absl::flat_hash_map<std::string, FileOffset> file_offsets;
|
| 360 |
+
for (const T& element : container) {
|
| 361 |
+
BundleEntryProto entry;
|
| 362 |
+
TF_RETURN_IF_ERROR(GetBundleEntryProto(get_key(element), &entry));
|
| 363 |
+
file_offsets[get_key(element)] = {entry.shard_id(), entry.offset()};
|
| 364 |
+
}
|
| 365 |
+
absl::c_sort(container, [&get_key, &file_offsets](const T& a, const T& b) {
|
| 366 |
+
const FileOffset& file_offset_a = file_offsets[get_key(a)];
|
| 367 |
+
const FileOffset& file_offset_b = file_offsets[get_key(b)];
|
| 368 |
+
if (file_offset_a.shard_id == file_offset_b.shard_id) {
|
| 369 |
+
return file_offset_a.offset < file_offset_b.offset;
|
| 370 |
+
} else {
|
| 371 |
+
return file_offset_a.shard_id < file_offset_b.shard_id;
|
| 372 |
+
}
|
| 373 |
+
});
|
| 374 |
+
return OkStatus();
|
| 375 |
+
}
|
| 376 |
+
|
| 377 |
+
} // namespace tensorflow
|
| 378 |
+
|
| 379 |
+
#endif // TENSORFLOW_CORE_UTIL_TENSOR_BUNDLE_TENSOR_BUNDLE_H_
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/tensor_format.h
ADDED
|
@@ -0,0 +1,678 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
|
| 16 |
+
#ifndef TENSORFLOW_CORE_UTIL_TENSOR_FORMAT_H_
|
| 17 |
+
#define TENSORFLOW_CORE_UTIL_TENSOR_FORMAT_H_
|
| 18 |
+
|
| 19 |
+
#include <array>
|
| 20 |
+
#include <string>
|
| 21 |
+
#include <vector>
|
| 22 |
+
|
| 23 |
+
#include "absl/strings/string_view.h"
|
| 24 |
+
#include "tensorflow/core/framework/tensor.h"
|
| 25 |
+
#include "tensorflow/core/lib/gtl/array_slice.h"
|
| 26 |
+
#include "tensorflow/core/lib/gtl/inlined_vector.h"
|
| 27 |
+
#include "tensorflow/core/platform/types.h"
|
| 28 |
+
|
| 29 |
+
namespace tensorflow {
|
| 30 |
+
|
| 31 |
+
// Tensor format for input/output activations used in convolution operations.
|
| 32 |
+
// The mnemonics specify the meaning of each tensor dimension sorted from
|
| 33 |
+
// largest to smallest memory stride.
|
| 34 |
+
// N = Batch, H = Image Height, W = Image Width, C = Number of Channels.
|
| 35 |
+
// TODO(pauldonnelly): It would probably be better to switch to a registration
|
| 36 |
+
// process for tensor formats, so specialized formats could be defined more
|
| 37 |
+
// locally to where they are used.
|
| 38 |
+
enum TensorFormat {
|
| 39 |
+
// FORMAT_NHWC is the default format in TensorFlow.
|
| 40 |
+
FORMAT_NHWC = 0,
|
| 41 |
+
|
| 42 |
+
// FORMAT_NCHW often improves performance on GPUs.
|
| 43 |
+
FORMAT_NCHW = 1,
|
| 44 |
+
|
| 45 |
+
// NCHW_VECT_C is the most performant tensor format for cudnn6's quantized
|
| 46 |
+
// int8 convolution and fused convolution. It is laid out in the same order
|
| 47 |
+
// as NCHW, except that the size of the Channels dimension is divided by 4,
|
| 48 |
+
// and a new dimension of size 4 is appended, which packs 4 adjacent channel
|
| 49 |
+
// activations for the same pixel into an int32. Thus an NCHW format tensor
|
| 50 |
+
// with dimensions [N, C, H, W] would have dimensions [N, C/4, H, W, 4] in
|
| 51 |
+
// NCHW_VECT_C format.
|
| 52 |
+
// A pre-condition of this format is that C must be a multiple of 4.
|
| 53 |
+
FORMAT_NCHW_VECT_C = 2,
|
| 54 |
+
|
| 55 |
+
// Similar to NHWC, but the size of the W dimension is divided by 4, and a
|
| 56 |
+
// new dimension of size 4 is appended, which packs 4 adjacent activations
|
| 57 |
+
// in the width dimension.
|
| 58 |
+
FORMAT_NHWC_VECT_W = 3,
|
| 59 |
+
|
| 60 |
+
// Note: although the current code in this file assumes VECT_C and VECT_W
|
| 61 |
+
// enums imply int8x4 vectors, this should not be relied upon.
|
| 62 |
+
// In the future we may change the meaning of these enums to include vectors
|
| 63 |
+
// of other types such as int16x2, with op implementations automatically
|
| 64 |
+
// determining which format is implied based on the datatype.
|
| 65 |
+
|
| 66 |
+
// FORMAT_HWNC is for TPUs.
|
| 67 |
+
FORMAT_HWNC = 4,
|
| 68 |
+
|
| 69 |
+
// FORMAT_HWCN is for TPUs.
|
| 70 |
+
FORMAT_HWCN = 5,
|
| 71 |
+
};
|
| 72 |
+
|
| 73 |
+
// Tensor format for convolutional filters.
|
| 74 |
+
// The mnemonics specify the meaning of each tensor dimension sorted
|
| 75 |
+
// from largest to smallest memory stride.
|
| 76 |
+
// H = Kernel Height, W = Kernel Width, I = Input Channels, O = Output Channels.
|
| 77 |
+
// Note: In cudnnGetFilter4dDescriptor(), 'O' is called 'K', 'I' is called 'C'.
|
| 78 |
+
enum FilterTensorFormat {
|
| 79 |
+
// FORMAT_HWIO is the default filter format in TensorFlow.
|
| 80 |
+
// Ops that do not have a 'filter_format' attribute will assume this format.
|
| 81 |
+
FORMAT_HWIO = 0,
|
| 82 |
+
|
| 83 |
+
// FORMAT_OIHW often improves performance on GPUs.
|
| 84 |
+
FORMAT_OIHW = 1,
|
| 85 |
+
|
| 86 |
+
// FORMAT_OHWI used by cuDNN for NHWC convolutions.
|
| 87 |
+
FORMAT_OHWI = 2,
|
| 88 |
+
|
| 89 |
+
// OIHW_VECT_I is the most performant tensor format for cudnn6's quantized
|
| 90 |
+
// int8 convolution and fused convolution. It is analogous to the NCHW_VECT_C
|
| 91 |
+
// data format. It is laid out in the same order as OIHW, except that the size
|
| 92 |
+
// of the Input Channels dimension is divided by 4, and a new dimension of
|
| 93 |
+
// size 4 is appended, which packs 4 adjacent input channel weights into an
|
| 94 |
+
// int32. Thus an OIHW format filter with dimensions [O, I, H, W] would have
|
| 95 |
+
// dimensions [O, I/4, H, W, 4] in OIHW_VECT_I format.
|
| 96 |
+
// A pre-condition of this format is that I must be a multiple of 4.
|
| 97 |
+
FORMAT_OIHW_VECT_I = 3,
|
| 98 |
+
};
|
| 99 |
+
|
| 100 |
+
// Parse tensor format from the given string.
|
| 101 |
+
// Return true if the parsing succeeds, and false if it fails.
|
| 102 |
+
bool FormatFromString(absl::string_view format_str, TensorFormat* format);
|
| 103 |
+
|
| 104 |
+
// Parse tensor format from the given string.
|
| 105 |
+
// Return true if the parsing succeeds, and false if it fails.
|
| 106 |
+
bool FilterFormatFromString(absl::string_view format_str,
|
| 107 |
+
FilterTensorFormat* format);
|
| 108 |
+
|
| 109 |
+
// Convert a tensor format into string.
|
| 110 |
+
std::string ToString(TensorFormat format);
|
| 111 |
+
|
| 112 |
+
// Convert a filter tensor format into string.
|
| 113 |
+
std::string ToString(FilterTensorFormat format);
|
| 114 |
+
|
| 115 |
+
// Returns the number of spatial dims of a tensor of rank 'num_dims' and tensor
|
| 116 |
+
// format 'format'.
|
| 117 |
+
inline int GetTensorSpatialDims(int num_dims, TensorFormat format) {
|
| 118 |
+
switch (format) {
|
| 119 |
+
case FORMAT_NHWC:
|
| 120 |
+
case FORMAT_NCHW:
|
| 121 |
+
case FORMAT_HWNC:
|
| 122 |
+
case FORMAT_HWCN:
|
| 123 |
+
return num_dims - 2; // Exclude N,C.
|
| 124 |
+
case FORMAT_NCHW_VECT_C:
|
| 125 |
+
case FORMAT_NHWC_VECT_W:
|
| 126 |
+
// Note: the VECT_W is not counted as an independent spatial dim here,
|
| 127 |
+
// since it just a component of the width dimension.
|
| 128 |
+
return num_dims - 3; // Exclude N,C,VectDim.
|
| 129 |
+
default:
|
| 130 |
+
LOG(FATAL) << "Unknown format " << format;
|
| 131 |
+
return -1; // Avoid compiler warning about missing return value
|
| 132 |
+
}
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
inline int GetFilterTensorSpatialDims(int num_dims, FilterTensorFormat format) {
|
| 136 |
+
if (format == FORMAT_OIHW_VECT_I) {
|
| 137 |
+
return num_dims - 3; // Exclude O,I,InnerI.
|
| 138 |
+
} else {
|
| 139 |
+
return num_dims - 2; // Exclude O,I.
|
| 140 |
+
}
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
// Returns the rank of a tensor with 'num_spatial_dims' spatial dimensions and
|
| 144 |
+
// tensor format 'format'. This is the inverse of GetTensorSpatialDims.
|
| 145 |
+
inline int GetTensorDimsFromSpatialDims(int num_spatial_dims,
|
| 146 |
+
TensorFormat format) {
|
| 147 |
+
switch (format) {
|
| 148 |
+
case FORMAT_NHWC:
|
| 149 |
+
case FORMAT_NCHW:
|
| 150 |
+
case FORMAT_HWNC:
|
| 151 |
+
case FORMAT_HWCN:
|
| 152 |
+
return num_spatial_dims + 2; // Include N,C.
|
| 153 |
+
case FORMAT_NCHW_VECT_C:
|
| 154 |
+
case FORMAT_NHWC_VECT_W:
|
| 155 |
+
return num_spatial_dims + 3; // Include N,C,VectDim.
|
| 156 |
+
default:
|
| 157 |
+
LOG(FATAL) << "Unknown format " << format;
|
| 158 |
+
return -1; // Avoid compiler warning about missing return value
|
| 159 |
+
}
|
| 160 |
+
}
|
| 161 |
+
|
| 162 |
+
// Returns the rank of a tensor with 'num_spatial_dims' spatial dimensions and
|
| 163 |
+
// filter tensor format 'format'.
|
| 164 |
+
inline int GetFilterTensorDimsFromSpatialDims(int num_spatial_dims,
|
| 165 |
+
FilterTensorFormat format) {
|
| 166 |
+
if (format == FORMAT_OIHW_VECT_I) {
|
| 167 |
+
return num_spatial_dims + 3; // Include O,I,InnerI.
|
| 168 |
+
} else {
|
| 169 |
+
return num_spatial_dims + 2; // Include O,I.
|
| 170 |
+
}
|
| 171 |
+
}
|
| 172 |
+
|
| 173 |
+
// Returns the index of the batch dimension.
|
| 174 |
+
inline int GetTensorBatchDimIndex(int num_dims, TensorFormat format) {
|
| 175 |
+
switch (format) {
|
| 176 |
+
case FORMAT_NHWC:
|
| 177 |
+
case FORMAT_NCHW:
|
| 178 |
+
case FORMAT_NCHW_VECT_C:
|
| 179 |
+
case FORMAT_NHWC_VECT_W:
|
| 180 |
+
return 0;
|
| 181 |
+
case FORMAT_HWNC:
|
| 182 |
+
return num_dims - 2;
|
| 183 |
+
case FORMAT_HWCN:
|
| 184 |
+
return num_dims - 1;
|
| 185 |
+
default:
|
| 186 |
+
LOG(FATAL) << "Unknown format " << format;
|
| 187 |
+
return -1; // Avoid compiler warning about missing return value
|
| 188 |
+
}
|
| 189 |
+
}
|
| 190 |
+
|
| 191 |
+
// Returns the index of the feature dimension. If format is NCHW_VECT_C, returns
|
| 192 |
+
// the index of the outer feature dimension (i.e. dimension 1, whose size would
|
| 193 |
+
// be num_features / 4 in this case).
|
| 194 |
+
inline int GetTensorFeatureDimIndex(int num_dims, TensorFormat format) {
|
| 195 |
+
switch (format) {
|
| 196 |
+
case FORMAT_NHWC:
|
| 197 |
+
case FORMAT_HWNC:
|
| 198 |
+
return num_dims - 1;
|
| 199 |
+
case FORMAT_NHWC_VECT_W:
|
| 200 |
+
case FORMAT_HWCN:
|
| 201 |
+
return num_dims - 2;
|
| 202 |
+
case FORMAT_NCHW:
|
| 203 |
+
case FORMAT_NCHW_VECT_C:
|
| 204 |
+
return 1;
|
| 205 |
+
default:
|
| 206 |
+
LOG(FATAL) << "Unknown format " << format;
|
| 207 |
+
return -1; // Avoid compiler warning about missing return value
|
| 208 |
+
}
|
| 209 |
+
}
|
| 210 |
+
|
| 211 |
+
// Returns the index of the inner feature dimension.
|
| 212 |
+
inline int GetTensorInnerFeatureDimIndex(int num_dims, TensorFormat format) {
|
| 213 |
+
DCHECK_EQ(format, FORMAT_NCHW_VECT_C);
|
| 214 |
+
return num_dims - 1;
|
| 215 |
+
}
|
| 216 |
+
|
| 217 |
+
// Returns the index of the inner width dimension.
|
| 218 |
+
inline int GetTensorInnerWidthDimIndex(int num_dims, TensorFormat format) {
|
| 219 |
+
DCHECK_EQ(format, FORMAT_NHWC_VECT_W);
|
| 220 |
+
return num_dims - 1;
|
| 221 |
+
}
|
| 222 |
+
|
| 223 |
+
// Returns the dimension index of the specified 'spatial_dim' within an
|
| 224 |
+
// activation tensor. If format is NHWC_VECT_W and spatial_dim is 1, returns
|
| 225 |
+
// the index of the outer width dimension (i.e. dimension 2, whose size would
|
| 226 |
+
// be width / 4 in this case).
|
| 227 |
+
inline int GetTensorSpatialDimIndex(int num_dims, TensorFormat format,
|
| 228 |
+
int spatial_dim) {
|
| 229 |
+
CHECK(spatial_dim >= 0 &&
|
| 230 |
+
spatial_dim < GetTensorSpatialDims(num_dims, format))
|
| 231 |
+
<< spatial_dim << " " << num_dims << " " << ToString(format);
|
| 232 |
+
switch (format) {
|
| 233 |
+
case FORMAT_NHWC:
|
| 234 |
+
case FORMAT_NHWC_VECT_W:
|
| 235 |
+
return spatial_dim + 1;
|
| 236 |
+
case FORMAT_NCHW:
|
| 237 |
+
case FORMAT_NCHW_VECT_C:
|
| 238 |
+
return spatial_dim + 2;
|
| 239 |
+
case FORMAT_HWNC:
|
| 240 |
+
case FORMAT_HWCN:
|
| 241 |
+
return spatial_dim;
|
| 242 |
+
default:
|
| 243 |
+
LOG(FATAL) << "Unknown format " << format;
|
| 244 |
+
return -1; // Avoid compiler warning about missing return value
|
| 245 |
+
}
|
| 246 |
+
}
|
| 247 |
+
|
| 248 |
+
inline int GetFilterTensorSpatialDimIndex(int num_dims,
|
| 249 |
+
FilterTensorFormat format, int dim) {
|
| 250 |
+
CHECK(dim >= 0 && dim < GetFilterTensorSpatialDims(num_dims, format))
|
| 251 |
+
<< dim << " " << num_dims << " " << ToString(format);
|
| 252 |
+
switch (format) {
|
| 253 |
+
case FORMAT_HWIO:
|
| 254 |
+
return dim;
|
| 255 |
+
case FORMAT_OIHW:
|
| 256 |
+
case FORMAT_OIHW_VECT_I:
|
| 257 |
+
return dim + 2;
|
| 258 |
+
default:
|
| 259 |
+
LOG(FATAL) << "Unknown format " << format;
|
| 260 |
+
return -1; // Avoid compiler warning about missing return value
|
| 261 |
+
}
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
// Returns the index of the inner input channels dimension.
|
| 265 |
+
inline int GetFilterTensorInnerInputChannelsDimIndex(
|
| 266 |
+
int num_dims, FilterTensorFormat format) {
|
| 267 |
+
DCHECK_EQ(format, FORMAT_OIHW_VECT_I);
|
| 268 |
+
return num_dims - 1;
|
| 269 |
+
}
|
| 270 |
+
|
| 271 |
+
// Returns the index of the input channels dimension.
|
| 272 |
+
// If 'format' is FORMAT_OIHW_VECT_I, returns the dimension index of the
|
| 273 |
+
// outer input channel (i.e. 1), which holds num_input_channels / 4.
|
| 274 |
+
inline int GetFilterTensorInputChannelsDimIndex(int num_dims,
|
| 275 |
+
FilterTensorFormat format) {
|
| 276 |
+
switch (format) {
|
| 277 |
+
case FORMAT_HWIO:
|
| 278 |
+
return num_dims - 2;
|
| 279 |
+
case FORMAT_OIHW:
|
| 280 |
+
case FORMAT_OIHW_VECT_I:
|
| 281 |
+
return 1;
|
| 282 |
+
default:
|
| 283 |
+
LOG(FATAL) << "Unknown format " << format;
|
| 284 |
+
return -1; // Avoid compiler warning about missing return value
|
| 285 |
+
}
|
| 286 |
+
}
|
| 287 |
+
|
| 288 |
+
// Returns the index of the output channels dimension.
|
| 289 |
+
inline int GetFilterTensorOutputChannelsDimIndex(int num_dims,
|
| 290 |
+
FilterTensorFormat format) {
|
| 291 |
+
switch (format) {
|
| 292 |
+
case FORMAT_HWIO:
|
| 293 |
+
return num_dims - 1;
|
| 294 |
+
case FORMAT_OIHW:
|
| 295 |
+
case FORMAT_OIHW_VECT_I:
|
| 296 |
+
return 0;
|
| 297 |
+
default:
|
| 298 |
+
LOG(FATAL) << "Unknown format " << format;
|
| 299 |
+
return -1; // Avoid compiler warning about missing return value
|
| 300 |
+
}
|
| 301 |
+
}
|
| 302 |
+
|
| 303 |
+
// TODO(pauldonnelly): Replace these tensor dimension index functions with
|
| 304 |
+
// constant structs to improve performance and reduce code size in Compute()
|
| 305 |
+
// functions.
|
| 306 |
+
|
| 307 |
+
// Return the dimension index for the specified 'dimension' of the specified
|
| 308 |
+
// data 'tensor_format'. 'dimension' is a char that can be 'N' (batch size),
|
| 309 |
+
// 'C' (channels), 'H' (height), 'W' (width), or a numbered spatial dimension:
|
| 310 |
+
// '0', .. (NUM_SPATIAL_DIMS-1)..
|
| 311 |
+
// If 'format' is NCHW_VECT_C and 'dimension' is 'C', returns the index of
|
| 312 |
+
// the outer channel dimension (i.e. 1).
|
| 313 |
+
template <int NUM_SPATIAL_DIMS>
|
| 314 |
+
inline int32 GetTensorDimIndex(TensorFormat format, char dimension) {
|
| 315 |
+
if (format == FORMAT_NHWC || format == FORMAT_NHWC_VECT_W) {
|
| 316 |
+
// clang-format off
|
| 317 |
+
switch (dimension) {
|
| 318 |
+
case 'N': return 0;
|
| 319 |
+
case '0': return 1;
|
| 320 |
+
case '1': return 2;
|
| 321 |
+
case '2': return 3;
|
| 322 |
+
case 'H': return NUM_SPATIAL_DIMS - 1;
|
| 323 |
+
case 'W': return NUM_SPATIAL_DIMS;
|
| 324 |
+
case 'C': return NUM_SPATIAL_DIMS + 1;
|
| 325 |
+
default:
|
| 326 |
+
LOG(FATAL) << "Invalid dimension: " << dimension;
|
| 327 |
+
return -1; // Avoid compiler warning about missing return value
|
| 328 |
+
}
|
| 329 |
+
} else if (format == FORMAT_NCHW || format == FORMAT_NCHW_VECT_C) {
|
| 330 |
+
switch (dimension) {
|
| 331 |
+
case 'N': return 0;
|
| 332 |
+
case 'C': return 1;
|
| 333 |
+
case '0': return 2;
|
| 334 |
+
case '1': return 3;
|
| 335 |
+
case '2': return 4;
|
| 336 |
+
case 'H': return NUM_SPATIAL_DIMS;
|
| 337 |
+
case 'W': return NUM_SPATIAL_DIMS + 1;
|
| 338 |
+
default:
|
| 339 |
+
LOG(FATAL) << "Invalid dimension: " << dimension;
|
| 340 |
+
return -1; // Avoid compiler warning about missing return value
|
| 341 |
+
}
|
| 342 |
+
} else if (format == FORMAT_HWNC) {
|
| 343 |
+
switch (dimension) {
|
| 344 |
+
case '0': return 0;
|
| 345 |
+
case '1': return 1;
|
| 346 |
+
case '2': return 2;
|
| 347 |
+
case 'H': return NUM_SPATIAL_DIMS - 2;
|
| 348 |
+
case 'W': return NUM_SPATIAL_DIMS - 1;
|
| 349 |
+
case 'N': return NUM_SPATIAL_DIMS;
|
| 350 |
+
case 'C': return NUM_SPATIAL_DIMS + 1;
|
| 351 |
+
default:
|
| 352 |
+
LOG(FATAL) << "Invalid dimension: " << dimension;
|
| 353 |
+
return -1; // Avoid compiler warning about missing return value
|
| 354 |
+
}
|
| 355 |
+
} else if (format == FORMAT_HWCN) {
|
| 356 |
+
switch (dimension) {
|
| 357 |
+
case '0': return 0;
|
| 358 |
+
case '1': return 1;
|
| 359 |
+
case '2': return 2;
|
| 360 |
+
case 'H': return NUM_SPATIAL_DIMS - 2;
|
| 361 |
+
case 'W': return NUM_SPATIAL_DIMS - 1;
|
| 362 |
+
case 'C': return NUM_SPATIAL_DIMS;
|
| 363 |
+
case 'N': return NUM_SPATIAL_DIMS + 1;
|
| 364 |
+
default:
|
| 365 |
+
LOG(FATAL) << "Invalid dimension: " << dimension;
|
| 366 |
+
return -1; // Avoid compiler warning about missing return value
|
| 367 |
+
}
|
| 368 |
+
} else {
|
| 369 |
+
LOG(FATAL) << "Invalid format: " << static_cast<int>(format);
|
| 370 |
+
return -1; // Avoid compiler warning about missing return value
|
| 371 |
+
}
|
| 372 |
+
// clang-format on
|
| 373 |
+
}
|
| 374 |
+
|
| 375 |
+
// Return the dimension index for the specified 'dimension' of the specified
|
| 376 |
+
// 'filter_tensor_format'. 'dimension' is a char that can be 'O' (num output
|
| 377 |
+
// channels), 'I' (num input channels), 'H' (height), 'W' (width), or a
|
| 378 |
+
// numbered spatial dimension: '0', .. (NUM_SPATIAL_DIMS-1).
|
| 379 |
+
// If 'format' is OIHW_VECT_I and 'dimension' is 'I', returns the index of the
|
| 380 |
+
// outer input channels dimension (i.e. 1).
|
| 381 |
+
template <int NUM_SPATIAL_DIMS>
|
| 382 |
+
inline int GetFilterDimIndex(FilterTensorFormat filter_tensor_format,
|
| 383 |
+
char dimension) {
|
| 384 |
+
// clang-format off
|
| 385 |
+
if (filter_tensor_format == FORMAT_HWIO) {
|
| 386 |
+
switch (dimension) {
|
| 387 |
+
case '0': return 0;
|
| 388 |
+
case '1': return 1;
|
| 389 |
+
case '2': return 2;
|
| 390 |
+
case 'H': return NUM_SPATIAL_DIMS - 2;
|
| 391 |
+
case 'W': return NUM_SPATIAL_DIMS - 1;
|
| 392 |
+
case 'I': return NUM_SPATIAL_DIMS;
|
| 393 |
+
case 'O': return NUM_SPATIAL_DIMS + 1;
|
| 394 |
+
default:
|
| 395 |
+
LOG(FATAL) << "Invalid dimension: " << dimension;
|
| 396 |
+
return -1; // Avoid compiler warning about missing return value
|
| 397 |
+
}
|
| 398 |
+
} else if (filter_tensor_format == FORMAT_OIHW ||
|
| 399 |
+
filter_tensor_format == FORMAT_OIHW_VECT_I) {
|
| 400 |
+
switch (dimension) {
|
| 401 |
+
case 'O': return 0;
|
| 402 |
+
case 'I': return 1;
|
| 403 |
+
case '0': return 2;
|
| 404 |
+
case '1': return 3;
|
| 405 |
+
case '2': return 4;
|
| 406 |
+
case 'H': return NUM_SPATIAL_DIMS;
|
| 407 |
+
case 'W': return NUM_SPATIAL_DIMS + 1;
|
| 408 |
+
default:
|
| 409 |
+
LOG(FATAL) << "Invalid dimension: " << dimension;
|
| 410 |
+
return -1; // Avoid compiler warning about missing return value
|
| 411 |
+
}
|
| 412 |
+
} else {
|
| 413 |
+
LOG(FATAL) << "Invalid format: " << static_cast<int>(filter_tensor_format);
|
| 414 |
+
return -1; // Avoid compiler warning about missing return value
|
| 415 |
+
}
|
| 416 |
+
// clang-format on
|
| 417 |
+
}
|
| 418 |
+
|
| 419 |
+
inline int32 GetTensorDimIndex(TensorFormat format, char dimension) {
|
| 420 |
+
return GetTensorDimIndex<2>(format, dimension);
|
| 421 |
+
}
|
| 422 |
+
|
| 423 |
+
inline int32 GetTensorDimIndex(TensorFormat format, char dimension,
|
| 424 |
+
int num_total_dims) {
|
| 425 |
+
int32_t index = (GetTensorSpatialDims(num_total_dims, format) == 3)
|
| 426 |
+
? GetTensorDimIndex<3>(format, dimension)
|
| 427 |
+
: GetTensorDimIndex<2>(format, dimension);
|
| 428 |
+
CHECK(index >= 0 && index < num_total_dims) // Crash OK.
|
| 429 |
+
<< "Invalid index from the dimension: " << index << ", " << format << ", "
|
| 430 |
+
<< dimension;
|
| 431 |
+
return index;
|
| 432 |
+
}
|
| 433 |
+
|
| 434 |
+
// Return the element from 'dimension_attributes' that corresponds to the
|
| 435 |
+
// specified 'dimension' according to 'tensor_format'.
|
| 436 |
+
template <typename T>
|
| 437 |
+
T GetTensorDim(gtl::ArraySlice<T> dimension_attributes,
|
| 438 |
+
TensorFormat tensor_format, char dimension) {
|
| 439 |
+
int index =
|
| 440 |
+
GetTensorDimIndex(tensor_format, dimension, dimension_attributes.size());
|
| 441 |
+
return dimension_attributes[index];
|
| 442 |
+
}
|
| 443 |
+
|
| 444 |
+
// Return the element from 'dimension_attribute' that corresponds to the
|
| 445 |
+
// specified 'dimension' according to 'filter_tensor_format'.
|
| 446 |
+
template <typename T>
|
| 447 |
+
T GetFilterDim(gtl::ArraySlice<T> dimension_attribute,
|
| 448 |
+
FilterTensorFormat filter_tensor_format, char dimension) {
|
| 449 |
+
int index = (GetFilterTensorSpatialDims(dimension_attribute.size(),
|
| 450 |
+
filter_tensor_format) == 3)
|
| 451 |
+
? GetFilterDimIndex<3>(filter_tensor_format, dimension)
|
| 452 |
+
: GetFilterDimIndex<2>(filter_tensor_format, dimension);
|
| 453 |
+
using size_type = typename gtl::ArraySlice<T>::size_type;
|
| 454 |
+
CHECK(index >= 0 &&
|
| 455 |
+
static_cast<size_type>(index) < dimension_attribute.size())
|
| 456 |
+
<< "Invalid index from the dimension: " << index << ", "
|
| 457 |
+
<< filter_tensor_format << ", " << dimension;
|
| 458 |
+
return dimension_attribute[index];
|
| 459 |
+
}
|
| 460 |
+
|
| 461 |
+
template <typename T>
|
| 462 |
+
T GetTensorDim(const std::vector<T>& attributes, TensorFormat format,
|
| 463 |
+
char dimension) {
|
| 464 |
+
return GetTensorDim(gtl::ArraySlice<T>(attributes), format, dimension);
|
| 465 |
+
}
|
| 466 |
+
|
| 467 |
+
// Return the size of the specified 'dimension' within 'tensor_shape'
|
| 468 |
+
// according to 'tensor_format'.
|
| 469 |
+
inline int64_t GetTensorDim(const TensorShape& tensor_shape,
|
| 470 |
+
TensorFormat tensor_format, char dimension) {
|
| 471 |
+
return GetTensorDim(gtl::ArraySlice<int64_t>(tensor_shape.dim_sizes()),
|
| 472 |
+
tensor_format, dimension);
|
| 473 |
+
}
|
| 474 |
+
|
| 475 |
+
// Return the size of the specified 'dimension' within 'tensor_shape'
|
| 476 |
+
// according to 'tensor_filter_format'.
|
| 477 |
+
inline int64_t GetFilterDim(const TensorShape& tensor_shape,
|
| 478 |
+
FilterTensorFormat tensor_filter_format,
|
| 479 |
+
char dimension) {
|
| 480 |
+
return GetFilterDim(gtl::ArraySlice<int64_t>(tensor_shape.dim_sizes()),
|
| 481 |
+
tensor_filter_format, dimension);
|
| 482 |
+
}
|
| 483 |
+
|
| 484 |
+
// Return the size of the specified 'dimension' of 'tensor' according to
|
| 485 |
+
// 'tensor_format'.
|
| 486 |
+
inline int64_t GetTensorDim(const Tensor& tensor, TensorFormat tensor_format,
|
| 487 |
+
char dimension) {
|
| 488 |
+
return GetTensorDim(tensor.shape(), tensor_format, dimension);
|
| 489 |
+
}
|
| 490 |
+
|
| 491 |
+
// Return the size of the specified 'dimension' of 'tensor' according to
|
| 492 |
+
// 'filter_tensor_format'.
|
| 493 |
+
inline int64_t GetFilterDim(const Tensor& tensor,
|
| 494 |
+
FilterTensorFormat filter_tensor_format,
|
| 495 |
+
char dimension) {
|
| 496 |
+
return GetFilterDim(tensor.shape(), filter_tensor_format, dimension);
|
| 497 |
+
}
|
| 498 |
+
|
| 499 |
+
inline void GetExplicitPaddingForDim(
|
| 500 |
+
const std::vector<int64_t>& explicit_paddings, TensorFormat tensor_format,
|
| 501 |
+
char dimension, int64_t* padding_before, int64_t* padding_after) {
|
| 502 |
+
int index =
|
| 503 |
+
GetTensorDimIndex(tensor_format, dimension, explicit_paddings.size() / 2);
|
| 504 |
+
*padding_before = explicit_paddings[2 * index];
|
| 505 |
+
*padding_after = explicit_paddings[2 * index + 1];
|
| 506 |
+
}
|
| 507 |
+
|
| 508 |
+
// Return the string that specifies the data format for convnet operations.
|
| 509 |
+
std::string GetConvnetDataFormatAttrString();
|
| 510 |
+
std::string GetConvnet3dDataFormatAttrString();
|
| 511 |
+
|
| 512 |
+
// Return the string that specifies the filter format for convnet operations.
|
| 513 |
+
std::string GetConvnetFilterFormatAttrString();
|
| 514 |
+
std::string GetConvnet3dFilterFormatAttrString();
|
| 515 |
+
std::string GetConvnetDataFormat2D3DAttrString();
|
| 516 |
+
|
| 517 |
+
// Returns a tensor shape for the specified format and dimension sizes.
|
| 518 |
+
// Works for both 2D and 3D operations. The output shapes are as follows:
|
| 519 |
+
// FORMAT_NHWC: (N, spatial, C); rank = spatial.size() + 2
|
| 520 |
+
// FORMAT_NCHW: (N, C, spatial); rank = spatial.size() + 2
|
| 521 |
+
// FORMAT_NCHW_VECT_C: (N, C, spatial, InnerC); rank = spatial.size() + 3
|
| 522 |
+
// FORMAT_NHWC_VECT_W: (N, spatial, C, InnerW); rank = spatial.size() + 3
|
| 523 |
+
inline Status ShapeFromFormatWithStatus(TensorFormat format, int64_t N,
|
| 524 |
+
gtl::ArraySlice<int64_t> spatial,
|
| 525 |
+
int64_t C, TensorShape* shape) {
|
| 526 |
+
const int dims = GetTensorDimsFromSpatialDims(spatial.size(), format);
|
| 527 |
+
gtl::InlinedVector<int64_t, 6> dim_sizes(dims);
|
| 528 |
+
dim_sizes[GetTensorBatchDimIndex(dims, format)] = N;
|
| 529 |
+
for (int dim = 0; static_cast<size_t>(dim) < spatial.size(); dim++) {
|
| 530 |
+
auto dim_size = spatial[dim];
|
| 531 |
+
if (format == FORMAT_NHWC_VECT_W &&
|
| 532 |
+
static_cast<size_t>(dim) == spatial.size() - 1) {
|
| 533 |
+
CHECK_EQ(0, dim_size % 4)
|
| 534 |
+
<< "FORMAT_NHWC_VECT_W requires W to be a multiple of 4, but W="
|
| 535 |
+
<< dim_size;
|
| 536 |
+
dim_sizes[GetTensorInnerWidthDimIndex(dims, format)] = 4;
|
| 537 |
+
dim_size /= 4;
|
| 538 |
+
}
|
| 539 |
+
dim_sizes[GetTensorSpatialDimIndex(dims, format, dim)] = dim_size;
|
| 540 |
+
}
|
| 541 |
+
|
| 542 |
+
int feature_index = GetTensorFeatureDimIndex(dims, format);
|
| 543 |
+
if (format == FORMAT_NCHW_VECT_C) {
|
| 544 |
+
CHECK_EQ(0, C % 4) << "NCHW_VECT_C requires C to be a multiple of 4, but C="
|
| 545 |
+
<< C;
|
| 546 |
+
C /= 4;
|
| 547 |
+
dim_sizes[GetTensorInnerFeatureDimIndex(dims, format)] = 4;
|
| 548 |
+
}
|
| 549 |
+
dim_sizes[feature_index] = C;
|
| 550 |
+
return TensorShapeUtils::MakeShape(dim_sizes, shape);
|
| 551 |
+
}
|
| 552 |
+
|
| 553 |
+
inline TensorShape ShapeFromFormat(TensorFormat format, int64_t N,
|
| 554 |
+
gtl::ArraySlice<int64_t> spatial,
|
| 555 |
+
int64_t C) {
|
| 556 |
+
TensorShape shape;
|
| 557 |
+
TF_CHECK_OK(ShapeFromFormatWithStatus(format, N, spatial, C, &shape));
|
| 558 |
+
return shape;
|
| 559 |
+
}
|
| 560 |
+
|
| 561 |
+
// Return a tensor shape of the specified 'format', and dimensions.
|
| 562 |
+
// Works for both 2D and 3D operations. If 'format' is OIHW_VECT_I,
|
| 563 |
+
// the output TensorShape has spatial.size() + 3 dimensions, otherwise
|
| 564 |
+
// it has spatial.size() + 2 dimensions.
|
| 565 |
+
inline TensorShape ShapeFromFilterTensorFormat(FilterTensorFormat format,
|
| 566 |
+
gtl::ArraySlice<int64_t> spatial,
|
| 567 |
+
int64_t I, int64_t O) {
|
| 568 |
+
const int dims = GetFilterTensorDimsFromSpatialDims(spatial.size(), format);
|
| 569 |
+
gtl::InlinedVector<int64_t, 6> dim_sizes(dims);
|
| 570 |
+
dim_sizes[GetFilterTensorOutputChannelsDimIndex(dims, format)] = O;
|
| 571 |
+
for (int dim = 0; static_cast<size_t>(dim) < spatial.size(); dim++) {
|
| 572 |
+
dim_sizes[GetFilterTensorSpatialDimIndex(dims, format, dim)] = spatial[dim];
|
| 573 |
+
}
|
| 574 |
+
|
| 575 |
+
if (format == FORMAT_OIHW_VECT_I) {
|
| 576 |
+
CHECK_EQ(0, I % 4) << "OIHW_VECT_I requires I to be a multiple of 4, but I="
|
| 577 |
+
<< I;
|
| 578 |
+
I /= 4;
|
| 579 |
+
dim_sizes[GetFilterTensorInnerInputChannelsDimIndex(dims, format)] = 4;
|
| 580 |
+
}
|
| 581 |
+
dim_sizes[GetFilterTensorInputChannelsDimIndex(dims, format)] = I;
|
| 582 |
+
return TensorShape(dim_sizes);
|
| 583 |
+
}
|
| 584 |
+
|
| 585 |
+
// Return a tensor shape of the specified 'format', and dimensions.
|
| 586 |
+
inline Status ShapeFromFormatWithStatus(TensorFormat format, int64_t N,
|
| 587 |
+
int64_t H, int64_t W, int64_t C,
|
| 588 |
+
TensorShape* shape) {
|
| 589 |
+
return ShapeFromFormatWithStatus(format, N, {H, W}, C, shape);
|
| 590 |
+
}
|
| 591 |
+
|
| 592 |
+
// Return a tensor shape of the specified 'format', and dimensions.
|
| 593 |
+
inline TensorShape ShapeFromFormat(TensorFormat format, int64_t N, int64_t H,
|
| 594 |
+
int64_t W, int64_t C) {
|
| 595 |
+
TensorShape shape;
|
| 596 |
+
TF_CHECK_OK(ShapeFromFormatWithStatus(format, N, {H, W}, C, &shape));
|
| 597 |
+
return shape;
|
| 598 |
+
}
|
| 599 |
+
|
| 600 |
+
// Return a filter tensor shape of the specified 'format', and dimensions.
|
| 601 |
+
inline TensorShape ShapeFromFilterTensorFormat(FilterTensorFormat format,
|
| 602 |
+
int64_t H, int64_t W, int64_t I,
|
| 603 |
+
int64_t O) {
|
| 604 |
+
return ShapeFromFilterTensorFormat(format, {H, W}, I, O);
|
| 605 |
+
}
|
| 606 |
+
|
| 607 |
+
// Returns a copy of the specified tensor 'src_shape' converted from
|
| 608 |
+
// 'src_format' to 'dst_format'.
|
| 609 |
+
inline Status ShapeFromFormatWithStatus(TensorFormat dst_format,
|
| 610 |
+
const TensorShape& src_shape,
|
| 611 |
+
TensorFormat src_format,
|
| 612 |
+
TensorShape* shape) {
|
| 613 |
+
if (src_format == dst_format) {
|
| 614 |
+
*shape = src_shape;
|
| 615 |
+
return OkStatus();
|
| 616 |
+
}
|
| 617 |
+
|
| 618 |
+
const int64_t batch = GetTensorDim(src_shape, src_format, 'N');
|
| 619 |
+
const int64_t channels = GetTensorDim(src_shape, src_format, 'C') *
|
| 620 |
+
(src_format == FORMAT_NCHW_VECT_C ? 4 : 1);
|
| 621 |
+
const int num_src_spatial_dims =
|
| 622 |
+
GetTensorSpatialDims(src_shape.dims(), src_format);
|
| 623 |
+
std::vector<int64_t> spatial_dims(num_src_spatial_dims);
|
| 624 |
+
for (int spatial_dim = 0; spatial_dim < num_src_spatial_dims; ++spatial_dim) {
|
| 625 |
+
spatial_dims[spatial_dim] = gtl::ArraySlice<int64_t>(
|
| 626 |
+
src_shape.dim_sizes())[GetTensorSpatialDimIndex(
|
| 627 |
+
src_shape.dims(), src_format, spatial_dim)];
|
| 628 |
+
}
|
| 629 |
+
if (src_format == FORMAT_NHWC_VECT_W) {
|
| 630 |
+
spatial_dims[num_src_spatial_dims - 1] *= 4;
|
| 631 |
+
}
|
| 632 |
+
return ShapeFromFormatWithStatus(dst_format, batch, {spatial_dims}, channels,
|
| 633 |
+
shape);
|
| 634 |
+
}
|
| 635 |
+
|
| 636 |
+
inline TensorShape ShapeFromFormat(TensorFormat dst_format,
|
| 637 |
+
const TensorShape& src_shape,
|
| 638 |
+
TensorFormat src_format) {
|
| 639 |
+
TensorShape shape;
|
| 640 |
+
TF_CHECK_OK(
|
| 641 |
+
ShapeFromFormatWithStatus(dst_format, src_shape, src_format, &shape));
|
| 642 |
+
return shape;
|
| 643 |
+
}
|
| 644 |
+
|
| 645 |
+
// Returns a copy of the specified filter tensor 'src_shape' converted from
|
| 646 |
+
// 'src_filter_format' to 'dst_filter_format'.
|
| 647 |
+
inline TensorShape ShapeFromFilterFormat(FilterTensorFormat dst_filter_format,
|
| 648 |
+
const TensorShape& src_shape,
|
| 649 |
+
FilterTensorFormat src_filter_format) {
|
| 650 |
+
if (src_filter_format == dst_filter_format) {
|
| 651 |
+
return src_shape;
|
| 652 |
+
}
|
| 653 |
+
|
| 654 |
+
const int64_t output_channels =
|
| 655 |
+
GetFilterDim(src_shape, src_filter_format, 'O');
|
| 656 |
+
const int64_t input_channels =
|
| 657 |
+
GetFilterDim(src_shape, src_filter_format, 'I') *
|
| 658 |
+
(src_filter_format == FORMAT_OIHW_VECT_I ? 4 : 1);
|
| 659 |
+
|
| 660 |
+
if (GetFilterTensorSpatialDims(src_shape.dims(), src_filter_format) == 3) {
|
| 661 |
+
return ShapeFromFilterTensorFormat(
|
| 662 |
+
dst_filter_format,
|
| 663 |
+
{{GetFilterDim(src_shape, src_filter_format, '0'),
|
| 664 |
+
GetFilterDim(src_shape, src_filter_format, '1'),
|
| 665 |
+
GetFilterDim(src_shape, src_filter_format, '2')}},
|
| 666 |
+
input_channels, output_channels);
|
| 667 |
+
}
|
| 668 |
+
|
| 669 |
+
return ShapeFromFilterTensorFormat(
|
| 670 |
+
dst_filter_format,
|
| 671 |
+
{{GetFilterDim(src_shape, src_filter_format, 'H'),
|
| 672 |
+
GetFilterDim(src_shape, src_filter_format, 'W')}},
|
| 673 |
+
input_channels, output_channels);
|
| 674 |
+
}
|
| 675 |
+
|
| 676 |
+
} // namespace tensorflow
|
| 677 |
+
|
| 678 |
+
#endif // TENSORFLOW_CORE_UTIL_TENSOR_FORMAT_H_
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/tensor_ops_util.h
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
#ifndef TENSORFLOW_CORE_UTIL_TENSOR_OPS_UTIL_H_
|
| 16 |
+
#define TENSORFLOW_CORE_UTIL_TENSOR_OPS_UTIL_H_
|
| 17 |
+
|
| 18 |
+
#define EIGEN_USE_THREADS
|
| 19 |
+
|
| 20 |
+
#include "unsupported/Eigen/CXX11/Tensor" // from @eigen_archive
|
| 21 |
+
#include "tensorflow/core/framework/op_kernel.h"
|
| 22 |
+
#include "tensorflow/core/framework/register_types.h"
|
| 23 |
+
#include "tensorflow/core/framework/tensor.h"
|
| 24 |
+
#include "tensorflow/core/framework/types.h"
|
| 25 |
+
#include "tensorflow/core/framework/variant_op_registry.h"
|
| 26 |
+
#include "tensorflow/core/lib/core/status.h"
|
| 27 |
+
|
| 28 |
+
namespace tensorflow {
|
| 29 |
+
|
| 30 |
+
typedef Eigen::ThreadPoolDevice CPUDevice;
|
| 31 |
+
typedef Eigen::GpuDevice GPUDevice;
|
| 32 |
+
|
| 33 |
+
template <typename Device>
|
| 34 |
+
Status ZerosLikeTensor(OpKernelContext* ctx, const Tensor& x, Tensor* out) {
|
| 35 |
+
AllocatorAttributes attr;
|
| 36 |
+
if (x.dtype() == DT_VARIANT) {
|
| 37 |
+
attr.set_on_host(true);
|
| 38 |
+
}
|
| 39 |
+
TF_RETURN_IF_ERROR(ctx->allocate_temp(x.dtype(), x.shape(), out, attr));
|
| 40 |
+
|
| 41 |
+
switch (out->dtype()) {
|
| 42 |
+
#define DTYPE_CASE(dtype) \
|
| 43 |
+
case DataTypeToEnum<dtype>::value: \
|
| 44 |
+
/* TODO(skyewm): use SetZeroFunctor like in ZerosLikeOp? */ \
|
| 45 |
+
out->flat<dtype>().device(ctx->eigen_device<Device>()) = \
|
| 46 |
+
out->flat<dtype>().constant(dtype(0)); \
|
| 47 |
+
break;
|
| 48 |
+
|
| 49 |
+
TF_CALL_POD_TYPES(DTYPE_CASE)
|
| 50 |
+
#undef DTYPE_CASE
|
| 51 |
+
|
| 52 |
+
case DT_INVALID: {
|
| 53 |
+
*out = Tensor(DT_INVALID);
|
| 54 |
+
break;
|
| 55 |
+
}
|
| 56 |
+
case DataTypeToEnum<Variant>::value: {
|
| 57 |
+
Variant* out_variant = out->scalar<Variant>().data();
|
| 58 |
+
TF_RETURN_IF_ERROR(
|
| 59 |
+
UnaryOpVariant<Device>(ctx, ZEROS_LIKE_VARIANT_UNARY_OP,
|
| 60 |
+
x.scalar<Variant>()(), out_variant));
|
| 61 |
+
break;
|
| 62 |
+
}
|
| 63 |
+
default:
|
| 64 |
+
return errors::InvalidArgument(
|
| 65 |
+
"Trying to compute zeros_like for unsupported dtype ",
|
| 66 |
+
DataTypeString(out->dtype()));
|
| 67 |
+
}
|
| 68 |
+
return OkStatus();
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
template <typename Device>
|
| 72 |
+
Status BinaryAddTensors(OpKernelContext* ctx, const Tensor& a, const Tensor& b,
|
| 73 |
+
Tensor* out) {
|
| 74 |
+
if (a.dtype() == DT_INVALID) {
|
| 75 |
+
*out = b;
|
| 76 |
+
return OkStatus();
|
| 77 |
+
}
|
| 78 |
+
if (b.dtype() == DT_INVALID) {
|
| 79 |
+
*out = a;
|
| 80 |
+
return OkStatus();
|
| 81 |
+
}
|
| 82 |
+
if (a.dtype() != b.dtype()) {
|
| 83 |
+
return errors::InvalidArgument(
|
| 84 |
+
"Trying to add two tensors with incompatible element types. ",
|
| 85 |
+
"One is ", DataTypeString(a.dtype()), " and the other is ",
|
| 86 |
+
DataTypeString(b.dtype()));
|
| 87 |
+
}
|
| 88 |
+
if (a.shape() != b.shape()) {
|
| 89 |
+
// TODO(apassos) support broadcasting additions here?
|
| 90 |
+
return errors::InvalidArgument(
|
| 91 |
+
"Trying to add two tensors with incompatible element shapes. ",
|
| 92 |
+
"One is ", a.shape().DebugString(), " and the other is ",
|
| 93 |
+
b.shape().DebugString());
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
AllocatorAttributes attr;
|
| 97 |
+
if (a.dtype() == DT_VARIANT) {
|
| 98 |
+
attr.set_on_host(true);
|
| 99 |
+
}
|
| 100 |
+
TF_RETURN_IF_ERROR(ctx->allocate_temp(a.dtype(), a.shape(), out, attr));
|
| 101 |
+
|
| 102 |
+
switch (out->dtype()) {
|
| 103 |
+
#define DTYPE_CASE(dtype) \
|
| 104 |
+
case DataTypeToEnum<dtype>::value: \
|
| 105 |
+
out->flat<dtype>().device(ctx->eigen_device<Device>()) = \
|
| 106 |
+
a.flat<dtype>() + b.flat<dtype>(); \
|
| 107 |
+
break;
|
| 108 |
+
|
| 109 |
+
TF_CALL_NUMBER_TYPES(DTYPE_CASE)
|
| 110 |
+
#undef DTYPE_CASE
|
| 111 |
+
|
| 112 |
+
case DataTypeToEnum<Variant>::value: {
|
| 113 |
+
Variant* out_variant = out->scalar<Variant>().data();
|
| 114 |
+
TF_RETURN_IF_ERROR(BinaryOpVariants<Device>(
|
| 115 |
+
ctx, ADD_VARIANT_BINARY_OP, a.scalar<Variant>()(),
|
| 116 |
+
b.scalar<Variant>()(), out_variant));
|
| 117 |
+
break;
|
| 118 |
+
}
|
| 119 |
+
default:
|
| 120 |
+
return errors::InvalidArgument("Trying to add unsupported dtype ",
|
| 121 |
+
out->dtype());
|
| 122 |
+
}
|
| 123 |
+
return OkStatus();
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
} // namespace tensorflow
|
| 127 |
+
|
| 128 |
+
#endif // TENSORFLOW_CORE_UTIL_TENSOR_OPS_UTIL_H_
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
|
| 16 |
+
// The utility to read checkpoints for google brain tensor ops and v3
|
| 17 |
+
// checkpoints for dist_belief.
|
| 18 |
+
|
| 19 |
+
#ifndef TENSORFLOW_CORE_UTIL_TENSOR_SLICE_READER_H_
|
| 20 |
+
#define TENSORFLOW_CORE_UTIL_TENSOR_SLICE_READER_H_
|
| 21 |
+
|
| 22 |
+
#include <functional>
|
| 23 |
+
#include <memory>
|
| 24 |
+
#include <unordered_map>
|
| 25 |
+
#include <utility>
|
| 26 |
+
#include <vector>
|
| 27 |
+
|
| 28 |
+
#include "tensorflow/core/framework/tensor.h"
|
| 29 |
+
#include "tensorflow/core/framework/tensor_shape.h"
|
| 30 |
+
#include "tensorflow/core/framework/tensor_slice.h"
|
| 31 |
+
#include "tensorflow/core/framework/types.pb.h"
|
| 32 |
+
#include "tensorflow/core/lib/core/status.h"
|
| 33 |
+
#include "tensorflow/core/lib/core/stringpiece.h"
|
| 34 |
+
#include "tensorflow/core/lib/gtl/map_util.h"
|
| 35 |
+
#include "tensorflow/core/platform/logging.h"
|
| 36 |
+
#include "tensorflow/core/platform/macros.h"
|
| 37 |
+
#include "tensorflow/core/platform/mutex.h"
|
| 38 |
+
#include "tensorflow/core/platform/protobuf.h"
|
| 39 |
+
#include "tensorflow/core/platform/types.h"
|
| 40 |
+
#include "tensorflow/core/util/saved_tensor_slice.pb.h"
|
| 41 |
+
#include "tensorflow/core/util/saved_tensor_slice_util.h"
|
| 42 |
+
#include "tensorflow/core/util/tensor_slice_set.h"
|
| 43 |
+
#include "tensorflow/core/util/tensor_slice_util.h"
|
| 44 |
+
|
| 45 |
+
namespace tensorflow {
|
| 46 |
+
|
| 47 |
+
namespace checkpoint {
|
| 48 |
+
|
| 49 |
+
// The reader reads in all the meta data about all the tensor slices. Then it
|
| 50 |
+
// will try to read the relevant data on-demand to produce the data for the
|
| 51 |
+
// slices needed.
|
| 52 |
+
// NOTE(yangke): another way to do this is to first load a list of the tensor
|
| 53 |
+
// slices needed and then just selectively read some of the meta data. That
|
| 54 |
+
// might optimize the loading but makes the logic a bit more complicated. We
|
| 55 |
+
// might want to revisit that.
|
| 56 |
+
// TODO(yangke): consider moving to TensorProto.
|
| 57 |
+
class TensorSliceReader {
|
| 58 |
+
public:
|
| 59 |
+
// Abstract interface for reading data out of a tensor slice checkpoint file
|
| 60 |
+
class Table {
|
| 61 |
+
public:
|
| 62 |
+
virtual ~Table();
|
| 63 |
+
virtual bool Get(const string& key, string* value) = 0;
|
| 64 |
+
};
|
| 65 |
+
typedef std::function<Status(const string&, Table**)> OpenTableFunction;
|
| 66 |
+
|
| 67 |
+
static constexpr int kLoadAllShards = -1;
|
| 68 |
+
TensorSliceReader(const string& filepattern);
|
| 69 |
+
TensorSliceReader(const string& filepattern, OpenTableFunction open_function);
|
| 70 |
+
TensorSliceReader(const string& filepattern, OpenTableFunction open_function,
|
| 71 |
+
int preferred_shard);
|
| 72 |
+
virtual ~TensorSliceReader();
|
| 73 |
+
|
| 74 |
+
// Get the filename this reader is attached to.
|
| 75 |
+
const string& filepattern() const { return filepattern_; }
|
| 76 |
+
|
| 77 |
+
// Get the number of files matched.
|
| 78 |
+
int num_files() const { return sss_.size(); }
|
| 79 |
+
|
| 80 |
+
// Get the status of the reader.
|
| 81 |
+
Status status() const { return status_; }
|
| 82 |
+
|
| 83 |
+
// Checks if the reader contains any slice of a tensor. In case the reader
|
| 84 |
+
// does contain the tensor, if "shape" is not nullptr, fill "shape" with the
|
| 85 |
+
// shape of the tensor; if "type" is not nullptr, fill "type" with the type
|
| 86 |
+
// of the tensor.
|
| 87 |
+
bool HasTensor(const string& name, TensorShape* shape, DataType* type) const;
|
| 88 |
+
|
| 89 |
+
// Checks if the reader contains all the data about a tensor slice, and if
|
| 90 |
+
// yes, copies the data of the slice to "data". The caller needs to make sure
|
| 91 |
+
// that "data" points to a buffer that holds enough data.
|
| 92 |
+
// This is a slow function since it needs to read sstables.
|
| 93 |
+
template <typename T>
|
| 94 |
+
bool CopySliceData(const string& name, const TensorSlice& slice,
|
| 95 |
+
T* data) const;
|
| 96 |
+
|
| 97 |
+
// Get the tensors.
|
| 98 |
+
const std::unordered_map<string, TensorSliceSet*>& Tensors() const {
|
| 99 |
+
return tensors_;
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
// Returns value for one tensor. Only single slice checkpoints are supported
|
| 103 |
+
// at the moment.
|
| 104 |
+
Status GetTensor(const string& name,
|
| 105 |
+
std::unique_ptr<tensorflow::Tensor>* out_tensor) const;
|
| 106 |
+
|
| 107 |
+
typedef std::unordered_map<string, TensorShape> VarToShapeMap;
|
| 108 |
+
typedef std::unordered_map<string, DataType> VarToDataTypeMap;
|
| 109 |
+
|
| 110 |
+
// Returns a map from tensor name to shape.
|
| 111 |
+
VarToShapeMap GetVariableToShapeMap() const;
|
| 112 |
+
|
| 113 |
+
// Returns a map from tensor name to data type.
|
| 114 |
+
VarToDataTypeMap GetVariableToDataTypeMap() const;
|
| 115 |
+
|
| 116 |
+
// Returns a string containing names and shapes of all the tensors.
|
| 117 |
+
const string DebugString() const;
|
| 118 |
+
|
| 119 |
+
private:
|
| 120 |
+
friend class TensorSliceWriteTestHelper;
|
| 121 |
+
|
| 122 |
+
void LoadShard(int shard) const;
|
| 123 |
+
void LoadAllShards() const;
|
| 124 |
+
|
| 125 |
+
const TensorSliceSet* FindTensorSlice(
|
| 126 |
+
const string& name, const TensorSlice& slice,
|
| 127 |
+
std::vector<std::pair<TensorSlice, string>>* details) const;
|
| 128 |
+
|
| 129 |
+
const string filepattern_;
|
| 130 |
+
const OpenTableFunction open_function_;
|
| 131 |
+
std::vector<string> fnames_;
|
| 132 |
+
std::unordered_map<string, int> fname_to_index_;
|
| 133 |
+
|
| 134 |
+
// Guards the attributes below.
|
| 135 |
+
mutable mutex mu_;
|
| 136 |
+
mutable bool all_shards_loaded_ = false;
|
| 137 |
+
mutable std::vector<std::unique_ptr<Table>> sss_;
|
| 138 |
+
mutable std::unordered_map<string, TensorSliceSet*> tensors_;
|
| 139 |
+
mutable Status status_;
|
| 140 |
+
|
| 141 |
+
TensorSliceReader(const TensorSliceReader&) = delete;
|
| 142 |
+
void operator=(const TensorSliceReader&) = delete;
|
| 143 |
+
};
|
| 144 |
+
|
| 145 |
+
Status OpenTableTensorSliceReader(const string& fname,
|
| 146 |
+
TensorSliceReader::Table** result);
|
| 147 |
+
|
| 148 |
+
template <typename T>
|
| 149 |
+
bool TensorSliceReader::CopySliceData(const string& name,
|
| 150 |
+
const TensorSlice& slice, T* data) const {
|
| 151 |
+
std::vector<std::pair<TensorSlice, string>> details;
|
| 152 |
+
const TensorSliceSet* tss;
|
| 153 |
+
{
|
| 154 |
+
mutex_lock l(mu_);
|
| 155 |
+
tss = FindTensorSlice(name, slice, &details);
|
| 156 |
+
if (!tss && !all_shards_loaded_) {
|
| 157 |
+
VLOG(1) << "Did not find slice in preferred shard, loading all shards."
|
| 158 |
+
<< name << ": " << slice.DebugString();
|
| 159 |
+
LoadAllShards();
|
| 160 |
+
tss = FindTensorSlice(name, slice, &details);
|
| 161 |
+
}
|
| 162 |
+
if (!tss) {
|
| 163 |
+
// No such tensor
|
| 164 |
+
return false;
|
| 165 |
+
}
|
| 166 |
+
}
|
| 167 |
+
// We have the data -- copy it over.
|
| 168 |
+
string value;
|
| 169 |
+
for (const auto& x : details) {
|
| 170 |
+
const TensorSlice& slice_s = x.first;
|
| 171 |
+
const string& fname = x.second;
|
| 172 |
+
int idx = gtl::FindWithDefault(fname_to_index_, fname, -1);
|
| 173 |
+
CHECK_GE(idx, 0) << "Failed to find the index for filename " << fname;
|
| 174 |
+
// We read a record in the corresponding sstable
|
| 175 |
+
const string key = EncodeTensorNameSlice(name, slice_s);
|
| 176 |
+
if (!sss_[idx]->Get(key, &value)) {
|
| 177 |
+
VLOG(1) << "Failed to seek to the record for tensor " << name
|
| 178 |
+
<< ", slice " << slice_s.DebugString()
|
| 179 |
+
<< ": computed key = " << key;
|
| 180 |
+
return false;
|
| 181 |
+
}
|
| 182 |
+
SavedTensorSlices sts;
|
| 183 |
+
if (!ParseProtoUnlimited(&sts, value)) {
|
| 184 |
+
VLOG(1) << "Failed to parse the record for tensor " << name << ", slice "
|
| 185 |
+
<< slice_s.DebugString() << ": computed key = " << key;
|
| 186 |
+
return false;
|
| 187 |
+
}
|
| 188 |
+
// Ensure the TensorSlice contains the expected amount of data.
|
| 189 |
+
TensorShape shp_s;
|
| 190 |
+
Status s = slice_s.SliceTensorShape(tss->shape(), &shp_s);
|
| 191 |
+
if (!s.ok()) {
|
| 192 |
+
VLOG(1) << "Failed to slice tensor " << name << ", slice "
|
| 193 |
+
<< slice_s.DebugString() << ": " << s;
|
| 194 |
+
return false;
|
| 195 |
+
}
|
| 196 |
+
if (checkpoint::TensorProtoDataSize<T>(sts.data().data()) !=
|
| 197 |
+
shp_s.num_elements()) {
|
| 198 |
+
VLOG(1) << "Tensor " << name << ", slice " << slice_s.DebugString()
|
| 199 |
+
<< " had an unexpected amount of data: expected = "
|
| 200 |
+
<< shp_s.num_elements() << ", got = "
|
| 201 |
+
<< checkpoint::TensorProtoDataSize<T>(sts.data().data());
|
| 202 |
+
return false;
|
| 203 |
+
}
|
| 204 |
+
CopyDataFromTensorSliceToTensorSlice(
|
| 205 |
+
tss->shape(), slice_s, slice,
|
| 206 |
+
checkpoint::TensorProtoData<T>(sts.data().data()), data);
|
| 207 |
+
}
|
| 208 |
+
return true;
|
| 209 |
+
}
|
| 210 |
+
|
| 211 |
+
} // namespace checkpoint
|
| 212 |
+
|
| 213 |
+
} // namespace tensorflow
|
| 214 |
+
|
| 215 |
+
#endif // TENSORFLOW_CORE_UTIL_TENSOR_SLICE_READER_H_
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/tensor_slice_reader_cache.h
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
|
| 16 |
+
// The utility to read checkpoints for google brain tensor ops and v3
|
| 17 |
+
// checkpoints for dist_belief.
|
| 18 |
+
|
| 19 |
+
#ifndef TENSORFLOW_CORE_UTIL_TENSOR_SLICE_READER_CACHE_H_
|
| 20 |
+
#define TENSORFLOW_CORE_UTIL_TENSOR_SLICE_READER_CACHE_H_
|
| 21 |
+
|
| 22 |
+
#include <set>
|
| 23 |
+
#include <unordered_map>
|
| 24 |
+
#include <utility>
|
| 25 |
+
|
| 26 |
+
#include "tensorflow/core/lib/core/status.h"
|
| 27 |
+
#include "tensorflow/core/platform/mutex.h"
|
| 28 |
+
#include "tensorflow/core/platform/types.h"
|
| 29 |
+
#include "tensorflow/core/util/tensor_slice_reader.h"
|
| 30 |
+
|
| 31 |
+
namespace tensorflow {
|
| 32 |
+
|
| 33 |
+
namespace checkpoint {
|
| 34 |
+
|
| 35 |
+
class TensorSliceReaderCache;
|
| 36 |
+
|
| 37 |
+
// Wrapper to a lazily allocated TensorSliceReaderCache.
|
| 38 |
+
class TensorSliceReaderCacheWrapper {
|
| 39 |
+
public:
|
| 40 |
+
TensorSliceReaderCacheWrapper();
|
| 41 |
+
~TensorSliceReaderCacheWrapper();
|
| 42 |
+
|
| 43 |
+
// Same as TensorSliceReaderCache::GetReader().
|
| 44 |
+
const TensorSliceReader* GetReader(
|
| 45 |
+
const string& filepattern,
|
| 46 |
+
TensorSliceReader::OpenTableFunction open_function,
|
| 47 |
+
int preferred_shard) const;
|
| 48 |
+
|
| 49 |
+
private:
|
| 50 |
+
mutable mutex mu_;
|
| 51 |
+
mutable TensorSliceReaderCache* cache_ = nullptr;
|
| 52 |
+
};
|
| 53 |
+
|
| 54 |
+
// A cache of TensorSliceReaders.
|
| 55 |
+
class TensorSliceReaderCache {
|
| 56 |
+
public:
|
| 57 |
+
TensorSliceReaderCache();
|
| 58 |
+
~TensorSliceReaderCache();
|
| 59 |
+
|
| 60 |
+
// Returns the TensorSliceReader corresponding to 'filepattern' and the
|
| 61 |
+
// open_function. May return nullptr if we can not create a new
|
| 62 |
+
// TensorSliceReader for the filepattern/open_function combination.
|
| 63 |
+
const TensorSliceReader* GetReader(
|
| 64 |
+
const string& filepattern,
|
| 65 |
+
TensorSliceReader::OpenTableFunction open_function, int preferred_shard);
|
| 66 |
+
|
| 67 |
+
private:
|
| 68 |
+
// Need to use a regular function type in the key map as std::function does
|
| 69 |
+
// not support ==.
|
| 70 |
+
typedef Status (*OpenFuncType)(const string&, TensorSliceReader::Table**);
|
| 71 |
+
|
| 72 |
+
// Protects attributes below.
|
| 73 |
+
mutex mu_;
|
| 74 |
+
|
| 75 |
+
// Maps of opened readers.
|
| 76 |
+
std::unordered_map<string, std::pair<OpenFuncType, TensorSliceReader*>>
|
| 77 |
+
readers_;
|
| 78 |
+
|
| 79 |
+
// Set of keys that a previous GetReader() call is still trying to populate.
|
| 80 |
+
std::set<string> still_opening_;
|
| 81 |
+
|
| 82 |
+
// Condition variable to notify when a reader has been created.
|
| 83 |
+
condition_variable cv_;
|
| 84 |
+
};
|
| 85 |
+
|
| 86 |
+
} // namespace checkpoint
|
| 87 |
+
|
| 88 |
+
} // namespace tensorflow
|
| 89 |
+
|
| 90 |
+
#endif // TENSORFLOW_CORE_UTIL_TENSOR_SLICE_READER_CACHE_H_
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/tensor_slice_set.h
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
|
| 16 |
+
// A class to manage slices of a tensor. You can "register" set of slices for a
|
| 17 |
+
// tensor and then "query" if we have data for a given slice.
|
| 18 |
+
|
| 19 |
+
#ifndef TENSORFLOW_CORE_UTIL_TENSOR_SLICE_SET_H_
|
| 20 |
+
#define TENSORFLOW_CORE_UTIL_TENSOR_SLICE_SET_H_
|
| 21 |
+
|
| 22 |
+
#include <string> // for string
|
| 23 |
+
#include <unordered_map>
|
| 24 |
+
#include <utility>
|
| 25 |
+
#include <vector>
|
| 26 |
+
|
| 27 |
+
#include "tensorflow/core/framework/tensor_shape.h"
|
| 28 |
+
#include "tensorflow/core/framework/tensor_slice.h"
|
| 29 |
+
#include "tensorflow/core/framework/types.h"
|
| 30 |
+
#include "tensorflow/core/lib/core/status.h" // for Status
|
| 31 |
+
#include "tensorflow/core/lib/core/stringpiece.h" // for StringPiece
|
| 32 |
+
#include "tensorflow/core/platform/types.h" // for int64
|
| 33 |
+
|
| 34 |
+
namespace tensorflow {
|
| 35 |
+
|
| 36 |
+
namespace checkpoint {
|
| 37 |
+
|
| 38 |
+
class TensorSliceSet {
|
| 39 |
+
public:
|
| 40 |
+
TensorSliceSet(const TensorShape& shape, DataType type);
|
| 41 |
+
virtual ~TensorSliceSet();
|
| 42 |
+
|
| 43 |
+
const TensorShape& shape() const { return shape_; }
|
| 44 |
+
DataType type() const { return type_; }
|
| 45 |
+
|
| 46 |
+
// Register a new slice for the tensor. The "tag" is an arbitrary string
|
| 47 |
+
// associated with the slice (in one application it denotes the name of the
|
| 48 |
+
// file that contains the slice); the "data" points to the data of the tensor
|
| 49 |
+
// slice (it can be a nullptr).
|
| 50 |
+
Status Register(const TensorSlice& slice, const string& tag);
|
| 51 |
+
|
| 52 |
+
// Alternative way of querying about a new slice: instead of copying the
|
| 53 |
+
// data, it returns a list of meta data about the stored slices that will
|
| 54 |
+
// supply data for the slice.
|
| 55 |
+
bool QueryMeta(
|
| 56 |
+
const TensorSlice& slice,
|
| 57 |
+
std::vector<std::pair<tensorflow::TensorSlice, string>>* results) const;
|
| 58 |
+
|
| 59 |
+
struct SliceInfo {
|
| 60 |
+
TensorSlice slice;
|
| 61 |
+
const string tag;
|
| 62 |
+
int64_t num_floats;
|
| 63 |
+
};
|
| 64 |
+
|
| 65 |
+
// Returns the map from slice string to SliceInfo.
|
| 66 |
+
const std::unordered_map<string, SliceInfo>& Slices() const {
|
| 67 |
+
return slices_;
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
private:
|
| 71 |
+
const TensorShape shape_;
|
| 72 |
+
const DataType type_;
|
| 73 |
+
// We maintain a mapping from the slice string to the slice information.
|
| 74 |
+
std::unordered_map<string, SliceInfo> slices_;
|
| 75 |
+
|
| 76 |
+
// Minimal slice which contains all presented slices. Used for speeding up
|
| 77 |
+
// overlap check when slices are being added consequently.
|
| 78 |
+
TensorSlice slices_hull_;
|
| 79 |
+
};
|
| 80 |
+
|
| 81 |
+
// Registers "slice" in the TensorSliceSet stored in "tensor_slices", under key
|
| 82 |
+
// "name". Other arguments are used for validations. Does not modify the map
|
| 83 |
+
// or its values on non-OK.
|
| 84 |
+
// REQUIRES: tensor_slices != nullptr
|
| 85 |
+
Status RegisterTensorSlice(
|
| 86 |
+
const string& name, const TensorShape& shape, DataType type,
|
| 87 |
+
const string& tag, const TensorSlice& slice,
|
| 88 |
+
std::unordered_map<string, TensorSliceSet*>* tensor_slices);
|
| 89 |
+
|
| 90 |
+
} // namespace checkpoint
|
| 91 |
+
|
| 92 |
+
} // namespace tensorflow
|
| 93 |
+
|
| 94 |
+
#endif // TENSORFLOW_CORE_UTIL_TENSOR_SLICE_SET_H_
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/tensor_slice_util.h
ADDED
|
@@ -0,0 +1,191 @@
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
|
| 16 |
+
#ifndef TENSORFLOW_CORE_UTIL_TENSOR_SLICE_UTIL_H_
|
| 17 |
+
#define TENSORFLOW_CORE_UTIL_TENSOR_SLICE_UTIL_H_
|
| 18 |
+
|
| 19 |
+
#include "tensorflow/core/framework/tensor_shape.h"
|
| 20 |
+
#include "tensorflow/core/framework/tensor_slice.h"
|
| 21 |
+
#include "tensorflow/core/platform/logging.h"
|
| 22 |
+
|
| 23 |
+
namespace tensorflow {
|
| 24 |
+
|
| 25 |
+
namespace {
|
| 26 |
+
|
| 27 |
+
// Some hackery to invoke eigen tensor to copy over tensor slices with variable
|
| 28 |
+
// dimension tensors.
|
| 29 |
+
// TODO(yangke): get rid of that once the variable dimension tensor support is
|
| 30 |
+
// in.
|
| 31 |
+
static const int kTensorSliceMaxRank = 8;
|
| 32 |
+
|
| 33 |
+
// Create a tensor map with the given shape: we support up to 8 dimensions. If
|
| 34 |
+
// the shape has less than 8 dimensions, we pad the remaining dimension with 1.
|
| 35 |
+
template <typename T>
|
| 36 |
+
Eigen::TensorMap<Eigen::Tensor<T, kTensorSliceMaxRank, Eigen::RowMajor>>
|
| 37 |
+
GetEigenTensorMapFromTensorShape(const TensorShape& shape, T* data) {
|
| 38 |
+
Eigen::DSizes<Eigen::DenseIndex, kTensorSliceMaxRank> dsizes =
|
| 39 |
+
shape.AsEigenDSizesWithPadding<kTensorSliceMaxRank>();
|
| 40 |
+
Eigen::TensorMap<Eigen::Tensor<T, kTensorSliceMaxRank, Eigen::RowMajor>> eig(
|
| 41 |
+
data, dsizes);
|
| 42 |
+
return eig;
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
// For everything except string, a standard Eigen cast and assignment works
|
| 46 |
+
template <typename DstT>
|
| 47 |
+
struct CopyThatWorksWithStringPointer {
|
| 48 |
+
template <typename SrcTensor, typename DstTensor, typename Shape>
|
| 49 |
+
static void Copy(const SrcTensor& s, Shape s_start, Shape len, DstTensor& d,
|
| 50 |
+
Shape d_start) {
|
| 51 |
+
d.slice(d_start, len) = s.slice(s_start, len).template cast<DstT>();
|
| 52 |
+
}
|
| 53 |
+
};
|
| 54 |
+
|
| 55 |
+
// Eigen makes it extremely difficult to dereference a tensor of string* into
|
| 56 |
+
// string, so we roll our own loop instead.
|
| 57 |
+
template <>
|
| 58 |
+
struct CopyThatWorksWithStringPointer<tstring> {
|
| 59 |
+
template <typename SrcTensor, typename DstTensor, typename Shape>
|
| 60 |
+
static void Copy(const SrcTensor& s, Shape s_start, Shape len, DstTensor& d,
|
| 61 |
+
Shape d_start) {
|
| 62 |
+
typedef typename SrcTensor::Index Index;
|
| 63 |
+
static_assert(kTensorSliceMaxRank == 8,
|
| 64 |
+
"If kTensorSliceMaxRank changes, modify the loop below.");
|
| 65 |
+
for (Index i0 = 0; i0 < len[0]; i0++) {
|
| 66 |
+
for (Index i1 = 0; i1 < len[1]; i1++) {
|
| 67 |
+
for (Index i2 = 0; i2 < len[2]; i2++) {
|
| 68 |
+
for (Index i3 = 0; i3 < len[3]; i3++) {
|
| 69 |
+
for (Index i4 = 0; i4 < len[4]; i4++) {
|
| 70 |
+
for (Index i5 = 0; i5 < len[5]; i5++) {
|
| 71 |
+
for (Index i6 = 0; i6 < len[6]; i6++) {
|
| 72 |
+
for (Index i7 = 0; i7 < len[7]; i7++) {
|
| 73 |
+
d(d_start[0] + i0, d_start[1] + i1, d_start[2] + i2,
|
| 74 |
+
d_start[3] + i3, d_start[4] + i4, d_start[5] + i5,
|
| 75 |
+
d_start[6] + i6, d_start[7] + i7) =
|
| 76 |
+
*s(s_start[0] + i0, s_start[1] + i1, s_start[2] + i2,
|
| 77 |
+
s_start[3] + i3, s_start[4] + i4, s_start[5] + i5,
|
| 78 |
+
s_start[6] + i6, s_start[7] + i7);
|
| 79 |
+
}
|
| 80 |
+
}
|
| 81 |
+
}
|
| 82 |
+
}
|
| 83 |
+
}
|
| 84 |
+
}
|
| 85 |
+
}
|
| 86 |
+
}
|
| 87 |
+
}
|
| 88 |
+
};
|
| 89 |
+
|
| 90 |
+
// Checkpointing of half is done by storing the raw 16 bits as a signed 32bit
|
| 91 |
+
// integer. To restore the checkpoint we need to do the reverse operation by
|
| 92 |
+
// reinterpreting the integer as a 16 bit float. This prevents us from using
|
| 93 |
+
// the default cast operation.
|
| 94 |
+
template <>
|
| 95 |
+
struct CopyThatWorksWithStringPointer<Eigen::half> {
|
| 96 |
+
template <typename SrcTensor, typename DstTensor, typename Shape>
|
| 97 |
+
static void Copy(const SrcTensor& s, Shape s_start, Shape len, DstTensor& d,
|
| 98 |
+
Shape d_start) {
|
| 99 |
+
typedef typename SrcTensor::Index Index;
|
| 100 |
+
static_assert(kTensorSliceMaxRank == 8,
|
| 101 |
+
"If kTensorSliceMaxRank changes, modify the loop below.");
|
| 102 |
+
for (Index i0 = 0; i0 < len[0]; i0++) {
|
| 103 |
+
for (Index i1 = 0; i1 < len[1]; i1++) {
|
| 104 |
+
for (Index i2 = 0; i2 < len[2]; i2++) {
|
| 105 |
+
for (Index i3 = 0; i3 < len[3]; i3++) {
|
| 106 |
+
for (Index i4 = 0; i4 < len[4]; i4++) {
|
| 107 |
+
for (Index i5 = 0; i5 < len[5]; i5++) {
|
| 108 |
+
for (Index i6 = 0; i6 < len[6]; i6++) {
|
| 109 |
+
for (Index i7 = 0; i7 < len[7]; i7++) {
|
| 110 |
+
d(d_start[0] + i0, d_start[1] + i1, d_start[2] + i2,
|
| 111 |
+
d_start[3] + i3, d_start[4] + i4, d_start[5] + i5,
|
| 112 |
+
d_start[6] + i6, d_start[7] + i7) =
|
| 113 |
+
Eigen::numext::bit_cast<Eigen::half, uint16_t>(
|
| 114 |
+
s(s_start[0] + i0, s_start[1] + i1, s_start[2] + i2,
|
| 115 |
+
s_start[3] + i3, s_start[4] + i4, s_start[5] + i5,
|
| 116 |
+
s_start[6] + i6, s_start[7] + i7));
|
| 117 |
+
}
|
| 118 |
+
}
|
| 119 |
+
}
|
| 120 |
+
}
|
| 121 |
+
}
|
| 122 |
+
}
|
| 123 |
+
}
|
| 124 |
+
}
|
| 125 |
+
}
|
| 126 |
+
};
|
| 127 |
+
|
| 128 |
+
// Given a tensor described by "shape", two slices "slice_s" and "slice_d",
|
| 129 |
+
// and two pointers "ptr_s" and "ptr_d", where "ptr_s" points to a chunk of
|
| 130 |
+
// memory that stores the data for "slice_s" and "ptr_d" points to a chunk of
|
| 131 |
+
// memory that stores the data for "slice_d". This function copies the data
|
| 132 |
+
// that belongs to the intersection of the two slices from slice_s to
|
| 133 |
+
// slice_d. Uses Tensor cast<DstT>() to convert from SrcT to DstT. Returns true
|
| 134 |
+
// iff the two slices share any intersection (and thus some data is copied).
|
| 135 |
+
// TODO(yangke): figure out if we can make it private.
|
| 136 |
+
template <typename SrcT, typename DstT>
|
| 137 |
+
static bool CopyDataFromTensorSliceToTensorSlice(const TensorShape& shape,
|
| 138 |
+
const TensorSlice& slice_s,
|
| 139 |
+
const TensorSlice& slice_d,
|
| 140 |
+
const SrcT* ptr_s,
|
| 141 |
+
DstT* ptr_d) {
|
| 142 |
+
CHECK_LE(shape.dims(), kTensorSliceMaxRank)
|
| 143 |
+
<< "Only tensors of size up to " << kTensorSliceMaxRank
|
| 144 |
+
<< " are supported";
|
| 145 |
+
// We need to compute the intersection of the two slices.
|
| 146 |
+
TensorSlice inter;
|
| 147 |
+
if (!slice_s.Intersect(slice_d, &inter)) {
|
| 148 |
+
// There is no intersection: returns false.
|
| 149 |
+
return false;
|
| 150 |
+
} else {
|
| 151 |
+
// We need to compute the applied shapes after applying slice_s and
|
| 152 |
+
// slice_d.
|
| 153 |
+
TensorShape shp_s, shp_d;
|
| 154 |
+
Status s;
|
| 155 |
+
s = slice_s.SliceTensorShape(shape, &shp_s);
|
| 156 |
+
if (!s.ok()) {
|
| 157 |
+
LOG(WARNING) << s;
|
| 158 |
+
return false;
|
| 159 |
+
}
|
| 160 |
+
s = slice_d.SliceTensorShape(shape, &shp_d);
|
| 161 |
+
if (!s.ok()) {
|
| 162 |
+
LOG(WARNING) << s;
|
| 163 |
+
return false;
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
// We need to compute the relative slice of "inter" w.r.t. both slice_s and
|
| 167 |
+
// slice_d.
|
| 168 |
+
TensorSlice rel_s, rel_d;
|
| 169 |
+
slice_s.ComputeRelative(inter, &rel_s);
|
| 170 |
+
slice_d.ComputeRelative(inter, &rel_d);
|
| 171 |
+
|
| 172 |
+
// Get the eigen tensor maps to the data.
|
| 173 |
+
auto t_s = GetEigenTensorMapFromTensorShape(shp_s, ptr_s);
|
| 174 |
+
auto t_d = GetEigenTensorMapFromTensorShape(shp_d, ptr_d);
|
| 175 |
+
|
| 176 |
+
Eigen::DSizes<Eigen::DenseIndex, kTensorSliceMaxRank> s_start, s_len,
|
| 177 |
+
d_start, d_len;
|
| 178 |
+
|
| 179 |
+
rel_s.FillIndicesAndSizes<kTensorSliceMaxRank>(shp_s, &s_start, &s_len);
|
| 180 |
+
rel_d.FillIndicesAndSizes<kTensorSliceMaxRank>(shp_d, &d_start, &d_len);
|
| 181 |
+
CopyThatWorksWithStringPointer<DstT>::Copy(t_s, s_start, s_len, t_d,
|
| 182 |
+
d_start);
|
| 183 |
+
return true;
|
| 184 |
+
}
|
| 185 |
+
}
|
| 186 |
+
|
| 187 |
+
} // namespace
|
| 188 |
+
|
| 189 |
+
} // namespace tensorflow
|
| 190 |
+
|
| 191 |
+
#endif // TENSORFLOW_CORE_UTIL_TENSOR_SLICE_UTIL_H_
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/tensor_slice_writer.h
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
|
| 16 |
+
// The utility to write checkpoints for google brain tensor ops and v3
|
| 17 |
+
// checkpoints for dist_belief.
|
| 18 |
+
|
| 19 |
+
#ifndef TENSORFLOW_CORE_UTIL_TENSOR_SLICE_WRITER_H_
|
| 20 |
+
#define TENSORFLOW_CORE_UTIL_TENSOR_SLICE_WRITER_H_
|
| 21 |
+
|
| 22 |
+
#include <functional>
|
| 23 |
+
#include <map>
|
| 24 |
+
#include <unordered_map>
|
| 25 |
+
#include <utility>
|
| 26 |
+
|
| 27 |
+
#include "tensorflow/core/framework/tensor_shape.h"
|
| 28 |
+
#include "tensorflow/core/framework/tensor_slice.h"
|
| 29 |
+
#include "tensorflow/core/framework/types.h"
|
| 30 |
+
#include "tensorflow/core/lib/core/errors.h"
|
| 31 |
+
#include "tensorflow/core/lib/core/status.h"
|
| 32 |
+
#include "tensorflow/core/lib/core/stringpiece.h"
|
| 33 |
+
#include "tensorflow/core/lib/gtl/map_util.h"
|
| 34 |
+
#include "tensorflow/core/lib/strings/stringprintf.h"
|
| 35 |
+
#include "tensorflow/core/platform/logging.h"
|
| 36 |
+
#include "tensorflow/core/platform/macros.h"
|
| 37 |
+
#include "tensorflow/core/platform/types.h"
|
| 38 |
+
#include "tensorflow/core/util/saved_tensor_slice.pb.h"
|
| 39 |
+
#include "tensorflow/core/util/saved_tensor_slice_util.h"
|
| 40 |
+
|
| 41 |
+
namespace tensorflow {
|
| 42 |
+
|
| 43 |
+
namespace checkpoint {
|
| 44 |
+
|
| 45 |
+
class TensorSliceWriter {
|
| 46 |
+
public:
|
| 47 |
+
// Abstract interface that TensorSliceWriter uses for building
|
| 48 |
+
class Builder {
|
| 49 |
+
public:
|
| 50 |
+
virtual ~Builder() = default;
|
| 51 |
+
virtual void Add(StringPiece key, StringPiece value) = 0;
|
| 52 |
+
virtual Status Finish(int64_t* file_size) = 0;
|
| 53 |
+
};
|
| 54 |
+
typedef std::function<Status(const string&, Builder**)> CreateBuilderFunction;
|
| 55 |
+
|
| 56 |
+
TensorSliceWriter(const string& filename,
|
| 57 |
+
CreateBuilderFunction create_builder);
|
| 58 |
+
virtual ~TensorSliceWriter() = default;
|
| 59 |
+
// Adds a slice. We support float and int32 for now.
|
| 60 |
+
// TODO(yangke): add more supports
|
| 61 |
+
template <typename T>
|
| 62 |
+
Status Add(const string& name, const TensorShape& shape,
|
| 63 |
+
const TensorSlice& slice, const T* data);
|
| 64 |
+
Status Finish();
|
| 65 |
+
|
| 66 |
+
// Allocate "num_elements" elements in "ss" and save the data in "data"
|
| 67 |
+
// there.
|
| 68 |
+
template <typename T>
|
| 69 |
+
static Status SaveData(const T* data, int64_t num_elements, SavedSlice* ss);
|
| 70 |
+
|
| 71 |
+
static size_t MaxBytesPerElement(DataType dt);
|
| 72 |
+
|
| 73 |
+
private:
|
| 74 |
+
static size_t MaxBytesPerElementOrZero(DataType dt);
|
| 75 |
+
|
| 76 |
+
static constexpr size_t kMaxMessageBytes = 1LL << 31;
|
| 77 |
+
// Filling in the TensorProto in a SavedSlice will add the following
|
| 78 |
+
// header bytes, in addition to the data:
|
| 79 |
+
// - 1 byte: TensorProto tag and wire format
|
| 80 |
+
// - <= 5 bytes: TensorProto length
|
| 81 |
+
// - 1 byte: Repeated *_val tag and wire format
|
| 82 |
+
// - <= 5 bytes: *_val length
|
| 83 |
+
// However, we add 1KB of slack, to be conservative and guard
|
| 84 |
+
// against other additions to the TensorProto.
|
| 85 |
+
static constexpr size_t kTensorProtoHeaderBytes = 1 << 10;
|
| 86 |
+
|
| 87 |
+
const string filename_;
|
| 88 |
+
const CreateBuilderFunction create_builder_;
|
| 89 |
+
const string tmpname_;
|
| 90 |
+
|
| 91 |
+
// A mapping from the tensor names to their index in meta_.saved_slice_meta()
|
| 92 |
+
std::unordered_map<string, int> name_to_index_;
|
| 93 |
+
// The metadata that holds all the saved tensor slices.
|
| 94 |
+
SavedTensorSlices sts_;
|
| 95 |
+
// The data to be written to the builder
|
| 96 |
+
std::map<string, string> data_;
|
| 97 |
+
// Total number of slices written
|
| 98 |
+
int slices_;
|
| 99 |
+
TensorSliceWriter(const TensorSliceWriter&) = delete;
|
| 100 |
+
void operator=(const TensorSliceWriter&) = delete;
|
| 101 |
+
};
|
| 102 |
+
|
| 103 |
+
template <typename T>
|
| 104 |
+
Status TensorSliceWriter::Add(const string& name, const TensorShape& shape,
|
| 105 |
+
const TensorSlice& slice, const T* data) {
|
| 106 |
+
// The tensor and the slice have to be compatible
|
| 107 |
+
if (shape.dims() != slice.dims()) {
|
| 108 |
+
return errors::Internal("Incompatible tensor shape and slice: ", "shape = ",
|
| 109 |
+
shape.DebugString(),
|
| 110 |
+
", slice = ", slice.DebugString());
|
| 111 |
+
}
|
| 112 |
+
DataType dt = DataTypeToEnum<T>::value;
|
| 113 |
+
// We need to add an entry for "name" if there isn't an entry already.
|
| 114 |
+
int index = gtl::FindWithDefault(name_to_index_, name, -1);
|
| 115 |
+
if (index >= 0) {
|
| 116 |
+
// The same tensor has been registered -- we verify that the shapes and the
|
| 117 |
+
// type agree.
|
| 118 |
+
const SavedSliceMeta& ssm = sts_.meta().tensor(index);
|
| 119 |
+
CHECK_EQ(name, ssm.name()) << ssm.ShortDebugString();
|
| 120 |
+
TensorShape ssm_shape(ssm.shape());
|
| 121 |
+
if (!shape.IsSameSize(ssm_shape)) {
|
| 122 |
+
return errors::Internal(
|
| 123 |
+
"Mismatching shapes: existing tensor = ", ssm_shape.DebugString(),
|
| 124 |
+
", trying to add name ", name, ", shape = ", shape.DebugString());
|
| 125 |
+
}
|
| 126 |
+
if (dt != ssm.type()) {
|
| 127 |
+
return errors::Internal(
|
| 128 |
+
"Mismatching types: existing type = ", DataTypeString(ssm.type()),
|
| 129 |
+
", trying to add name ", name, ", type = ", DataTypeString(dt));
|
| 130 |
+
}
|
| 131 |
+
} else {
|
| 132 |
+
// Insert the new tensor name with the shape information
|
| 133 |
+
index = sts_.meta().tensor_size();
|
| 134 |
+
name_to_index_.insert(std::make_pair(name, index));
|
| 135 |
+
SavedSliceMeta* ssm = sts_.mutable_meta()->add_tensor();
|
| 136 |
+
ssm->set_name(name);
|
| 137 |
+
shape.AsProto(ssm->mutable_shape());
|
| 138 |
+
ssm->set_type(dt);
|
| 139 |
+
}
|
| 140 |
+
// Now we need to add the slice info the list of slices.
|
| 141 |
+
SavedSliceMeta* ssm = sts_.mutable_meta()->mutable_tensor(index);
|
| 142 |
+
slice.AsProto(ssm->add_slice());
|
| 143 |
+
|
| 144 |
+
// Now we need to add the real data.
|
| 145 |
+
{
|
| 146 |
+
SavedTensorSlices sts;
|
| 147 |
+
SavedSlice* ss = sts.mutable_data();
|
| 148 |
+
ss->set_name(name);
|
| 149 |
+
slice.AsProto(ss->mutable_slice());
|
| 150 |
+
TensorShape saved_shape(ssm->shape());
|
| 151 |
+
TensorShape sliced_shape;
|
| 152 |
+
TF_RETURN_IF_ERROR(slice.SliceTensorShape(saved_shape, &sliced_shape));
|
| 153 |
+
TF_RETURN_IF_ERROR(SaveData(data, sliced_shape.num_elements(), ss));
|
| 154 |
+
string key = EncodeTensorNameSlice(name, slice);
|
| 155 |
+
// TODO(yangke): consider doing a two-pass thing where the first pass just
|
| 156 |
+
// list the tensor slices we want to save and then another pass to actually
|
| 157 |
+
// set the data. Need to figure out if the interface works well.
|
| 158 |
+
std::pair<string, string> key_value(key, "");
|
| 159 |
+
if (!sts.AppendToString(&key_value.second)) {
|
| 160 |
+
return errors::Internal("Error writing Tensor. Possible size overflow.");
|
| 161 |
+
}
|
| 162 |
+
data_.insert(key_value);
|
| 163 |
+
}
|
| 164 |
+
++slices_;
|
| 165 |
+
return OkStatus();
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
template <typename T>
|
| 169 |
+
Status TensorSliceWriter::SaveData(const T* data, int64_t num_elements,
|
| 170 |
+
SavedSlice* ss) {
|
| 171 |
+
size_t max_bytes_per_element =
|
| 172 |
+
MaxBytesPerElementOrZero(DataTypeToEnum<T>::value);
|
| 173 |
+
if (max_bytes_per_element == 0) {
|
| 174 |
+
return errors::InvalidArgument(
|
| 175 |
+
"Tensor slice serialization not implemented for dtype ",
|
| 176 |
+
DataTypeToEnum<T>::value);
|
| 177 |
+
}
|
| 178 |
+
size_t size_bound = ss->ByteSize() + kTensorProtoHeaderBytes +
|
| 179 |
+
(max_bytes_per_element * num_elements);
|
| 180 |
+
if (size_bound > kMaxMessageBytes) {
|
| 181 |
+
return errors::InvalidArgument(
|
| 182 |
+
"Tensor slice is too large to serialize (conservative estimate: ",
|
| 183 |
+
size_bound, " bytes)");
|
| 184 |
+
}
|
| 185 |
+
Fill(data, num_elements, ss->mutable_data());
|
| 186 |
+
DCHECK_GE(ss->ByteSize(), 0);
|
| 187 |
+
DCHECK_LE(ss->ByteSize(), size_bound);
|
| 188 |
+
return OkStatus();
|
| 189 |
+
}
|
| 190 |
+
|
| 191 |
+
template <>
|
| 192 |
+
Status TensorSliceWriter::SaveData(const tstring* data, int64_t num_elements,
|
| 193 |
+
SavedSlice* ss);
|
| 194 |
+
|
| 195 |
+
// Create a table builder that will write to "filename" in
|
| 196 |
+
// tensorflow::io::Table format. If successful, return OK
|
| 197 |
+
// and set "*builder" to the allocated builder. Otherwise, return a
|
| 198 |
+
// non-OK status.
|
| 199 |
+
Status CreateTableTensorSliceBuilder(const string& filename,
|
| 200 |
+
TensorSliceWriter::Builder** builder);
|
| 201 |
+
|
| 202 |
+
} // namespace checkpoint
|
| 203 |
+
|
| 204 |
+
} // namespace tensorflow
|
| 205 |
+
|
| 206 |
+
#endif // TENSORFLOW_CORE_UTIL_TENSOR_SLICE_WRITER_H_
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/test_log.pb.h
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Generated by the protocol buffer compiler. DO NOT EDIT!
|
| 2 |
+
// source: tensorflow/core/util/test_log.proto
|
| 3 |
+
|
| 4 |
+
#ifndef GOOGLE_PROTOBUF_INCLUDED_tensorflow_2fcore_2futil_2ftest_5flog_2eproto
|
| 5 |
+
#define GOOGLE_PROTOBUF_INCLUDED_tensorflow_2fcore_2futil_2ftest_5flog_2eproto
|
| 6 |
+
|
| 7 |
+
#include <limits>
|
| 8 |
+
#include <string>
|
| 9 |
+
|
| 10 |
+
#include <google/protobuf/port_def.inc>
|
| 11 |
+
#if PROTOBUF_VERSION < 3021000
|
| 12 |
+
#error This file was generated by a newer version of protoc which is
|
| 13 |
+
#error incompatible with your Protocol Buffer headers. Please update
|
| 14 |
+
#error your headers.
|
| 15 |
+
#endif
|
| 16 |
+
#if 3021009 < PROTOBUF_MIN_PROTOC_VERSION
|
| 17 |
+
#error This file was generated by an older version of protoc which is
|
| 18 |
+
#error incompatible with your Protocol Buffer headers. Please
|
| 19 |
+
#error regenerate this file with a newer version of protoc.
|
| 20 |
+
#endif
|
| 21 |
+
|
| 22 |
+
#include <google/protobuf/port_undef.inc>
|
| 23 |
+
#include <google/protobuf/io/coded_stream.h>
|
| 24 |
+
#include <google/protobuf/arena.h>
|
| 25 |
+
#include <google/protobuf/arenastring.h>
|
| 26 |
+
#include <google/protobuf/generated_message_util.h>
|
| 27 |
+
#include <google/protobuf/metadata_lite.h>
|
| 28 |
+
#include <google/protobuf/generated_message_reflection.h>
|
| 29 |
+
#include <google/protobuf/repeated_field.h> // IWYU pragma: export
|
| 30 |
+
#include <google/protobuf/extension_set.h> // IWYU pragma: export
|
| 31 |
+
#include "tsl/protobuf/test_log.pb.h"
|
| 32 |
+
// @@protoc_insertion_point(includes)
|
| 33 |
+
#include <google/protobuf/port_def.inc>
|
| 34 |
+
#define PROTOBUF_INTERNAL_EXPORT_tensorflow_2fcore_2futil_2ftest_5flog_2eproto
|
| 35 |
+
PROTOBUF_NAMESPACE_OPEN
|
| 36 |
+
namespace internal {
|
| 37 |
+
class AnyMetadata;
|
| 38 |
+
} // namespace internal
|
| 39 |
+
PROTOBUF_NAMESPACE_CLOSE
|
| 40 |
+
|
| 41 |
+
// Internal implementation detail -- do not use these members.
|
| 42 |
+
struct TableStruct_tensorflow_2fcore_2futil_2ftest_5flog_2eproto {
|
| 43 |
+
static const uint32_t offsets[];
|
| 44 |
+
};
|
| 45 |
+
extern const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_tensorflow_2fcore_2futil_2ftest_5flog_2eproto;
|
| 46 |
+
PROTOBUF_NAMESPACE_OPEN
|
| 47 |
+
PROTOBUF_NAMESPACE_CLOSE
|
| 48 |
+
namespace tensorflow {
|
| 49 |
+
namespace dummy {
|
| 50 |
+
|
| 51 |
+
// ===================================================================
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
// ===================================================================
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
// ===================================================================
|
| 58 |
+
|
| 59 |
+
#ifdef __GNUC__
|
| 60 |
+
#pragma GCC diagnostic push
|
| 61 |
+
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
|
| 62 |
+
#endif // __GNUC__
|
| 63 |
+
#ifdef __GNUC__
|
| 64 |
+
#pragma GCC diagnostic pop
|
| 65 |
+
#endif // __GNUC__
|
| 66 |
+
|
| 67 |
+
// @@protoc_insertion_point(namespace_scope)
|
| 68 |
+
|
| 69 |
+
} // namespace dummy
|
| 70 |
+
} // namespace tensorflow
|
| 71 |
+
|
| 72 |
+
// @@protoc_insertion_point(global_scope)
|
| 73 |
+
|
| 74 |
+
#include <google/protobuf/port_undef.inc>
|
| 75 |
+
#endif // GOOGLE_PROTOBUF_INCLUDED_GOOGLE_PROTOBUF_INCLUDED_tensorflow_2fcore_2futil_2ftest_5flog_2eproto
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/test_log.proto
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Protocol messages for describing the results of benchmarks and unit tests.
|
| 2 |
+
syntax = "proto3";
|
| 3 |
+
|
| 4 |
+
package tensorflow.dummy;
|
| 5 |
+
|
| 6 |
+
import public "tsl/protobuf/test_log.proto";
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/transform_output_iterator.h
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
|
| 16 |
+
#ifndef TENSORFLOW_CORE_UTIL_TRANSFORM_OUTPUT_ITERATOR_H_
|
| 17 |
+
#define TENSORFLOW_CORE_UTIL_TRANSFORM_OUTPUT_ITERATOR_H_
|
| 18 |
+
|
| 19 |
+
#include <iostream>
|
| 20 |
+
#include <iterator>
|
| 21 |
+
|
| 22 |
+
namespace tensorflow {
|
| 23 |
+
|
| 24 |
+
template <typename StoreType, typename InputType, typename ConversionOp,
|
| 25 |
+
typename OffsetT = ptrdiff_t>
|
| 26 |
+
class TransformOutputIterator {
|
| 27 |
+
protected:
|
| 28 |
+
// Proxy object
|
| 29 |
+
struct Reference {
|
| 30 |
+
StoreType* ptr;
|
| 31 |
+
ConversionOp conversion_op;
|
| 32 |
+
|
| 33 |
+
/// Constructor
|
| 34 |
+
__host__ __device__ __forceinline__ Reference(StoreType* ptr,
|
| 35 |
+
ConversionOp conversion_op)
|
| 36 |
+
: ptr(ptr), conversion_op(conversion_op) {}
|
| 37 |
+
|
| 38 |
+
/// Assignment
|
| 39 |
+
__host__ __device__ __forceinline__ InputType operator=(InputType val) {
|
| 40 |
+
*ptr = conversion_op(val);
|
| 41 |
+
return val;
|
| 42 |
+
}
|
| 43 |
+
};
|
| 44 |
+
|
| 45 |
+
public:
|
| 46 |
+
// Required iterator traits
|
| 47 |
+
typedef TransformOutputIterator self_type; ///< My own type
|
| 48 |
+
typedef OffsetT difference_type; ///< Type to express the result of
|
| 49 |
+
///< subtracting one iterator from another
|
| 50 |
+
typedef void
|
| 51 |
+
value_type; ///< The type of the element the iterator can point to
|
| 52 |
+
typedef void pointer; ///< The type of a pointer to an element the iterator
|
| 53 |
+
///< can point to
|
| 54 |
+
typedef Reference reference; ///< The type of a reference to an element the
|
| 55 |
+
///< iterator can point to
|
| 56 |
+
|
| 57 |
+
typedef std::random_access_iterator_tag
|
| 58 |
+
iterator_category; ///< The iterator category
|
| 59 |
+
|
| 60 |
+
/*private:*/
|
| 61 |
+
|
| 62 |
+
StoreType* ptr;
|
| 63 |
+
ConversionOp conversion_op;
|
| 64 |
+
|
| 65 |
+
public:
|
| 66 |
+
/// Constructor
|
| 67 |
+
template <typename QualifiedStoreType>
|
| 68 |
+
__host__ __device__ __forceinline__ TransformOutputIterator(
|
| 69 |
+
QualifiedStoreType* ptr,
|
| 70 |
+
ConversionOp conversionOp) ///< Native pointer to wrap
|
| 71 |
+
: ptr(ptr), conversion_op(conversionOp) {}
|
| 72 |
+
|
| 73 |
+
/// Postfix increment
|
| 74 |
+
__host__ __device__ __forceinline__ self_type operator++(int) {
|
| 75 |
+
self_type retval = *this;
|
| 76 |
+
ptr++;
|
| 77 |
+
return retval;
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
/// Prefix increment
|
| 81 |
+
__host__ __device__ __forceinline__ self_type operator++() {
|
| 82 |
+
ptr++;
|
| 83 |
+
return *this;
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
/// Indirection
|
| 87 |
+
__host__ __device__ __forceinline__ reference operator*() const {
|
| 88 |
+
return Reference(ptr, conversion_op);
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
/// Addition
|
| 92 |
+
template <typename Distance>
|
| 93 |
+
__host__ __device__ __forceinline__ self_type operator+(Distance n) const {
|
| 94 |
+
self_type retval(ptr + n, conversion_op);
|
| 95 |
+
return retval;
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
/// Addition assignment
|
| 99 |
+
template <typename Distance>
|
| 100 |
+
__host__ __device__ __forceinline__ self_type& operator+=(Distance n) {
|
| 101 |
+
ptr += n;
|
| 102 |
+
return *this;
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
/// Subtraction
|
| 106 |
+
template <typename Distance>
|
| 107 |
+
__host__ __device__ __forceinline__ self_type operator-(Distance n) const {
|
| 108 |
+
self_type retval(ptr - n, conversion_op);
|
| 109 |
+
return retval;
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
/// Subtraction assignment
|
| 113 |
+
template <typename Distance>
|
| 114 |
+
__host__ __device__ __forceinline__ self_type& operator-=(Distance n) {
|
| 115 |
+
ptr -= n;
|
| 116 |
+
return *this;
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
/// Distance
|
| 120 |
+
__host__ __device__ __forceinline__ difference_type
|
| 121 |
+
operator-(self_type other) const {
|
| 122 |
+
return ptr - other.ptr;
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
/// Array subscript
|
| 126 |
+
template <typename Distance>
|
| 127 |
+
__host__ __device__ __forceinline__ reference operator[](Distance n) const {
|
| 128 |
+
return Reference(ptr + n, conversion_op);
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
/// Equal to
|
| 132 |
+
__host__ __device__ __forceinline__ bool operator==(const self_type& rhs) {
|
| 133 |
+
return (ptr == rhs.ptr);
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
/// Not equal to
|
| 137 |
+
__host__ __device__ __forceinline__ bool operator!=(const self_type& rhs) {
|
| 138 |
+
return (ptr != rhs.ptr);
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
/// ostream operator
|
| 142 |
+
friend std::ostream& operator<<(std::ostream& os, const self_type& itr) {
|
| 143 |
+
return os;
|
| 144 |
+
}
|
| 145 |
+
};
|
| 146 |
+
|
| 147 |
+
} // end namespace tensorflow
|
| 148 |
+
|
| 149 |
+
#endif // TENSORFLOW_CORE_UTIL_TRANSFORM_OUTPUT_ITERATOR_H_
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/use_cudnn.h
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
|
| 16 |
+
// The utility to check Cudnn dependency and set Cudnn-related flags.
|
| 17 |
+
|
| 18 |
+
#ifndef TENSORFLOW_CORE_UTIL_USE_CUDNN_H_
|
| 19 |
+
#define TENSORFLOW_CORE_UTIL_USE_CUDNN_H_
|
| 20 |
+
|
| 21 |
+
#include <cstdint>
|
| 22 |
+
|
| 23 |
+
#include "tsl/util/use_cudnn.h"
|
| 24 |
+
|
| 25 |
+
namespace tensorflow {
|
| 26 |
+
|
| 27 |
+
using tsl::CudnnDisableConv1x1Optimization;
|
| 28 |
+
using tsl::CudnnRnnUseAutotune;
|
| 29 |
+
using tsl::CudnnUseAutotune;
|
| 30 |
+
using tsl::CudnnUseFrontend;
|
| 31 |
+
using tsl::CudnnUseRuntimeFusion;
|
| 32 |
+
using tsl::DebugCudnnRnn;
|
| 33 |
+
using tsl::DebugCudnnRnnAlgo;
|
| 34 |
+
using tsl::DebugCudnnRnnUseTensorOps;
|
| 35 |
+
using tsl::ShouldCudnnGroupedConvolutionBeUsed;
|
| 36 |
+
|
| 37 |
+
} // namespace tensorflow
|
| 38 |
+
|
| 39 |
+
#endif // TENSORFLOW_CORE_UTIL_USE_CUDNN_H_
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/util.h
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
|
| 16 |
+
#ifndef TENSORFLOW_CORE_UTIL_UTIL_H_
|
| 17 |
+
#define TENSORFLOW_CORE_UTIL_UTIL_H_
|
| 18 |
+
|
| 19 |
+
#include <string>
|
| 20 |
+
|
| 21 |
+
#include "tensorflow/core/framework/tensor_shape.h"
|
| 22 |
+
#include "tensorflow/core/lib/core/stringpiece.h"
|
| 23 |
+
#include "tensorflow/core/platform/cpu_info.h"
|
| 24 |
+
|
| 25 |
+
namespace tensorflow {
|
| 26 |
+
|
| 27 |
+
// If op_name has '/' in it, then return everything before the first '/'.
|
| 28 |
+
// Otherwise return empty string.
|
| 29 |
+
StringPiece NodeNamePrefix(const StringPiece& op_name);
|
| 30 |
+
|
| 31 |
+
// If op_name has '/' in it, then return everything before the last '/'.
|
| 32 |
+
// Otherwise return empty string.
|
| 33 |
+
StringPiece NodeNameFullPrefix(const StringPiece& op_name);
|
| 34 |
+
|
| 35 |
+
class MovingAverage {
|
| 36 |
+
public:
|
| 37 |
+
explicit MovingAverage(int window);
|
| 38 |
+
~MovingAverage();
|
| 39 |
+
|
| 40 |
+
void Clear();
|
| 41 |
+
|
| 42 |
+
double GetAverage() const;
|
| 43 |
+
void AddValue(double v);
|
| 44 |
+
|
| 45 |
+
private:
|
| 46 |
+
const int window_; // Max size of interval
|
| 47 |
+
double sum_; // Sum over interval
|
| 48 |
+
double* data_; // Actual data values
|
| 49 |
+
int head_; // Offset of the newest statistic in data_
|
| 50 |
+
int count_; // # of valid data elements in window
|
| 51 |
+
};
|
| 52 |
+
|
| 53 |
+
// Returns a string printing bytes in ptr[0..n). The output looks
|
| 54 |
+
// like "00 01 ef cd cd ef".
|
| 55 |
+
std::string PrintMemory(const char* ptr, size_t n);
|
| 56 |
+
|
| 57 |
+
// Given a flattened index into a tensor, computes a string s so that
|
| 58 |
+
// StrAppend("tensor", s) is a Python indexing expression. E.g.,
|
| 59 |
+
// "tensor", "tensor[i]", "tensor[i, j]", etc.
|
| 60 |
+
std::string SliceDebugString(const TensorShape& shape, int64_t flat);
|
| 61 |
+
|
| 62 |
+
// Check if MKL is enabled in runtime
|
| 63 |
+
bool IsMKLEnabled();
|
| 64 |
+
|
| 65 |
+
// Check if BF16 is supported
|
| 66 |
+
bool IsBF16SupportedByOneDNNOnThisCPU();
|
| 67 |
+
|
| 68 |
+
} // namespace tensorflow
|
| 69 |
+
|
| 70 |
+
#endif // TENSORFLOW_CORE_UTIL_UTIL_H_
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/work_sharder.h
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
|
| 16 |
+
#ifndef TENSORFLOW_CORE_UTIL_WORK_SHARDER_H_
|
| 17 |
+
#define TENSORFLOW_CORE_UTIL_WORK_SHARDER_H_
|
| 18 |
+
|
| 19 |
+
#include <functional>
|
| 20 |
+
|
| 21 |
+
#include "tensorflow/core/lib/core/threadpool.h"
|
| 22 |
+
#include "tensorflow/core/platform/types.h"
|
| 23 |
+
|
| 24 |
+
namespace tensorflow {
|
| 25 |
+
|
| 26 |
+
// DEPRECATED: Prefer threadpool->ParallelFor with SchedulingStrategy, which
|
| 27 |
+
// allows you to specify the strategy for choosing shard sizes, including using
|
| 28 |
+
// a fixed shard size. Use this function only if you want to manually cap
|
| 29 |
+
// parallelism.
|
| 30 |
+
//
|
| 31 |
+
// Shards the "total" unit of work assuming each unit of work having
|
| 32 |
+
// roughly "cost_per_unit". Each unit of work is indexed 0, 1, ...,
|
| 33 |
+
// total - 1. Each shard contains 1 or more units of work and the
|
| 34 |
+
// total cost of each shard is roughly the same. The calling thread and the
|
| 35 |
+
// "workers" are used to compute each shard (calling work(start,
|
| 36 |
+
// limit). A common configuration is that "workers" is a thread pool
|
| 37 |
+
// with at least "max_parallelism" threads.
|
| 38 |
+
//
|
| 39 |
+
// "cost_per_unit" is an estimate of the number of CPU cycles (or nanoseconds
|
| 40 |
+
// if not CPU-bound) to complete a unit of work. Overestimating creates too
|
| 41 |
+
// many shards and CPU time will be dominated by per-shard overhead, such as
|
| 42 |
+
// Context creation. Underestimating may not fully make use of the specified
|
| 43 |
+
// parallelism.
|
| 44 |
+
//
|
| 45 |
+
// "work" should be a callable taking (int64, int64) arguments.
|
| 46 |
+
// work(start, limit) computes the work units from [start,
|
| 47 |
+
// limit), i.e., [start, limit) is a shard.
|
| 48 |
+
//
|
| 49 |
+
// Too much parallelism can also cause excessive thread switches,
|
| 50 |
+
// therefore, Shard() often limits the maximum parallelism. Each
|
| 51 |
+
// caller can provide the 1st argument max_parallelism. A thread can
|
| 52 |
+
// call SetMaxParallelism() so that all Shard() calls later limits the
|
| 53 |
+
// thread parallelism.
|
| 54 |
+
//
|
| 55 |
+
// REQUIRES: max_parallelism >= 0
|
| 56 |
+
// REQUIRES: workers != nullptr
|
| 57 |
+
// REQUIRES: total >= 0
|
| 58 |
+
// REQUIRES: cost_per_unit >= 0
|
| 59 |
+
void Shard(int max_parallelism, thread::ThreadPool* workers, int64_t total,
|
| 60 |
+
int64_t cost_per_unit, std::function<void(int64_t, int64_t)> work);
|
| 61 |
+
|
| 62 |
+
// Each thread has an associated option to express the desired maximum
|
| 63 |
+
// parallelism. Its default is a very large quantity.
|
| 64 |
+
//
|
| 65 |
+
// Within TF runtime, per-thread max parallelism affects Shard() and
|
| 66 |
+
// intra-op parallelism. E.g., if SetPerThreadMaxParallelism(1) is
|
| 67 |
+
// arranged to be called by a tf_compute thread, Shard() calls and
|
| 68 |
+
// eigen device assignment happens in that thread afterwards becomes
|
| 69 |
+
// single-threaded.
|
| 70 |
+
void SetPerThreadMaxParallelism(int max_parallelism);
|
| 71 |
+
int GetPerThreadMaxParallelism();
|
| 72 |
+
|
| 73 |
+
// Helper to set and unset per-thread max parallelism.
|
| 74 |
+
class ScopedPerThreadMaxParallelism {
|
| 75 |
+
public:
|
| 76 |
+
ScopedPerThreadMaxParallelism(int max_parallelism)
|
| 77 |
+
: previous_(GetPerThreadMaxParallelism()) {
|
| 78 |
+
SetPerThreadMaxParallelism(max_parallelism);
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
~ScopedPerThreadMaxParallelism() { SetPerThreadMaxParallelism(previous_); }
|
| 82 |
+
|
| 83 |
+
private:
|
| 84 |
+
int previous_ = -1;
|
| 85 |
+
};
|
| 86 |
+
|
| 87 |
+
// Implementation details for Shard().
|
| 88 |
+
class Sharder {
|
| 89 |
+
public:
|
| 90 |
+
typedef std::function<void()> Closure;
|
| 91 |
+
typedef std::function<void(Closure)> Runner;
|
| 92 |
+
typedef std::function<void(int64_t, int64_t)> Work;
|
| 93 |
+
|
| 94 |
+
// Refers to Shard()'s comment for the meaning of total,
|
| 95 |
+
// cost_per_unit, work, max_parallelism. runner is an interface to
|
| 96 |
+
// schedule a closure. Shard() uses thread::ThreadPool instead.
|
| 97 |
+
static void Do(int64_t total, int64_t cost_per_unit, const Work& work,
|
| 98 |
+
const Runner& runner, int max_parallelism);
|
| 99 |
+
};
|
| 100 |
+
|
| 101 |
+
} // end namespace tensorflow
|
| 102 |
+
|
| 103 |
+
#endif // TENSORFLOW_CORE_UTIL_WORK_SHARDER_H_
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/xla_config_registry.h
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
|
| 16 |
+
#ifndef TENSORFLOW_CORE_UTIL_XLA_CONFIG_REGISTRY_H_
|
| 17 |
+
#define TENSORFLOW_CORE_UTIL_XLA_CONFIG_REGISTRY_H_
|
| 18 |
+
|
| 19 |
+
#include <functional>
|
| 20 |
+
|
| 21 |
+
#include "tensorflow/core/framework/logging.h"
|
| 22 |
+
#include "tensorflow/core/platform/mutex.h"
|
| 23 |
+
#include "tensorflow/core/protobuf/config.pb.h"
|
| 24 |
+
|
| 25 |
+
namespace tensorflow {
|
| 26 |
+
|
| 27 |
+
namespace xla_config_registry {
|
| 28 |
+
|
| 29 |
+
// XlaGlobalJitLevel is used by XLA to expose its JIT level for processing
|
| 30 |
+
// single gpu and general (multi-gpu) graphs.
|
| 31 |
+
struct XlaGlobalJitLevel {
|
| 32 |
+
OptimizerOptions::GlobalJitLevel single_gpu;
|
| 33 |
+
OptimizerOptions::GlobalJitLevel general;
|
| 34 |
+
};
|
| 35 |
+
|
| 36 |
+
// Input is the jit_level in session config, and return value is the jit_level
|
| 37 |
+
// from XLA, reflecting the effect of the environment variable flags.
|
| 38 |
+
typedef std::function<XlaGlobalJitLevel(
|
| 39 |
+
const OptimizerOptions::GlobalJitLevel&)>
|
| 40 |
+
GlobalJitLevelGetterTy;
|
| 41 |
+
|
| 42 |
+
void RegisterGlobalJitLevelGetter(GlobalJitLevelGetterTy getter);
|
| 43 |
+
|
| 44 |
+
XlaGlobalJitLevel GetGlobalJitLevel(
|
| 45 |
+
OptimizerOptions::GlobalJitLevel jit_level_in_session_opts);
|
| 46 |
+
|
| 47 |
+
#define REGISTER_XLA_CONFIG_GETTER(getter) \
|
| 48 |
+
REGISTER_XLA_CONFIG_GETTER_UNIQ_HELPER(__COUNTER__, getter)
|
| 49 |
+
|
| 50 |
+
#define REGISTER_XLA_CONFIG_GETTER_UNIQ_HELPER(ctr, getter) \
|
| 51 |
+
REGISTER_XLA_CONFIG_GETTER_UNIQ(ctr, getter)
|
| 52 |
+
|
| 53 |
+
#define REGISTER_XLA_CONFIG_GETTER_UNIQ(ctr, getter) \
|
| 54 |
+
static bool xla_config_registry_registration_##ctr = \
|
| 55 |
+
(::tensorflow::xla_config_registry::RegisterGlobalJitLevelGetter( \
|
| 56 |
+
getter), \
|
| 57 |
+
true)
|
| 58 |
+
|
| 59 |
+
} // namespace xla_config_registry
|
| 60 |
+
|
| 61 |
+
} // namespace tensorflow
|
| 62 |
+
|
| 63 |
+
#endif // TENSORFLOW_CORE_UTIL_XLA_CONFIG_REGISTRY_H_
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/core/util/zen_util.h
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2023 The TensorFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
|
| 16 |
+
#ifndef TENSORFLOW_CORE_UTIL_ZEN_UTIL_H_
|
| 17 |
+
#define TENSORFLOW_CORE_UTIL_ZEN_UTIL_H_
|
| 18 |
+
|
| 19 |
+
#ifdef AMD_ZENDNN
|
| 20 |
+
|
| 21 |
+
#include "absl/base/call_once.h"
|
| 22 |
+
#include "tensorflow/core/util/env_var.h"
|
| 23 |
+
|
| 24 |
+
namespace tensorflow {
|
| 25 |
+
|
| 26 |
+
inline int64_t GetMempool() {
|
| 27 |
+
static absl::once_flag once;
|
| 28 |
+
static int64_t mempool = 1;
|
| 29 |
+
absl::call_once(once, [&] {
|
| 30 |
+
TF_CHECK_OK(
|
| 31 |
+
ReadInt64FromEnvVar("ZENDNN_ENABLE_MEMPOOL", mempool, &mempool));
|
| 32 |
+
return mempool;
|
| 33 |
+
});
|
| 34 |
+
return mempool;
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
inline bool IsBlockedFormatEnabled() {
|
| 38 |
+
static absl::once_flag once;
|
| 39 |
+
static bool blocked_format = false;
|
| 40 |
+
static int64_t conv_algo = 1;
|
| 41 |
+
absl::call_once(once, [&] {
|
| 42 |
+
TF_CHECK_OK(ReadInt64FromEnvVar("ZENDNN_CONV_ALGO", conv_algo, &conv_algo));
|
| 43 |
+
blocked_format = (conv_algo == 3);
|
| 44 |
+
return blocked_format;
|
| 45 |
+
});
|
| 46 |
+
return blocked_format;
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
} // namespace tensorflow
|
| 50 |
+
|
| 51 |
+
#endif // AMD_ZENDNN
|
| 52 |
+
#endif // TENSORFLOW_CORE_UTIL_ZEN_UTIL_H_
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/include/external/ml_dtypes/_virtual_includes/float8/ml_dtypes/include/float8.h
ADDED
|
@@ -0,0 +1,1401 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
|
| 16 |
+
#ifndef ML_DTYPES_FLOAT8_H_
|
| 17 |
+
#define ML_DTYPES_FLOAT8_H_
|
| 18 |
+
|
| 19 |
+
// 8-bit Floating Point Interchange Format, as described by
|
| 20 |
+
// https://arxiv.org/abs/2209.05433
|
| 21 |
+
|
| 22 |
+
#include <algorithm>
|
| 23 |
+
#include <cmath>
|
| 24 |
+
#include <cstdint>
|
| 25 |
+
#include <limits>
|
| 26 |
+
#include <ostream>
|
| 27 |
+
#include <type_traits>
|
| 28 |
+
#include <utility>
|
| 29 |
+
|
| 30 |
+
#ifdef __has_include
|
| 31 |
+
# if __has_include(<version>)
|
| 32 |
+
# include <version>
|
| 33 |
+
# endif
|
| 34 |
+
#endif
|
| 35 |
+
|
| 36 |
+
#if (defined(__cpp_lib_bitops) && __cpp_lib_bitops >= 201907L)
|
| 37 |
+
#include <bit>
|
| 38 |
+
#endif
|
| 39 |
+
|
| 40 |
+
#include "Eigen/Core"
|
| 41 |
+
|
| 42 |
+
namespace ml_dtypes {
|
| 43 |
+
namespace float8_internal {
|
| 44 |
+
|
| 45 |
+
// Forward-declarations of classes.
|
| 46 |
+
class float8_e4m3fn;
|
| 47 |
+
class float8_e4m3fnuz;
|
| 48 |
+
class float8_e4m3b11fnuz;
|
| 49 |
+
class float8_e5m2;
|
| 50 |
+
class float8_e5m2fnuz;
|
| 51 |
+
|
| 52 |
+
template <typename Derived>
|
| 53 |
+
class float8_base {
|
| 54 |
+
protected:
|
| 55 |
+
// Constructor tag to allow constexpr construction from bit representation.
|
| 56 |
+
struct ConstructFromRepTag {};
|
| 57 |
+
constexpr float8_base(uint8_t rep, ConstructFromRepTag) : rep_{rep} {}
|
| 58 |
+
|
| 59 |
+
public:
|
| 60 |
+
constexpr float8_base() : rep_(0) {}
|
| 61 |
+
|
| 62 |
+
template <typename T,
|
| 63 |
+
typename EnableIf = std::enable_if<std::is_arithmetic_v<T>>>
|
| 64 |
+
explicit EIGEN_DEVICE_FUNC float8_base(T f)
|
| 65 |
+
: float8_base(ConvertFrom(static_cast<float>(f)).rep(),
|
| 66 |
+
ConstructFromRepTag{}) {}
|
| 67 |
+
explicit EIGEN_DEVICE_FUNC float8_base(double f64)
|
| 68 |
+
: float8_base(ConvertFrom(f64).rep(), ConstructFromRepTag{}) {}
|
| 69 |
+
explicit EIGEN_DEVICE_FUNC float8_base(float f32)
|
| 70 |
+
: float8_base(ConvertFrom(f32).rep(), ConstructFromRepTag{}) {}
|
| 71 |
+
explicit EIGEN_DEVICE_FUNC float8_base(Eigen::bfloat16 bf16)
|
| 72 |
+
: float8_base(ConvertFrom(bf16).rep(), ConstructFromRepTag{}) {}
|
| 73 |
+
explicit EIGEN_DEVICE_FUNC float8_base(Eigen::half f16)
|
| 74 |
+
: float8_base(ConvertFrom(f16).rep(), ConstructFromRepTag{}) {}
|
| 75 |
+
|
| 76 |
+
constexpr uint8_t rep() const { return rep_; }
|
| 77 |
+
|
| 78 |
+
template <typename T,
|
| 79 |
+
typename EnableIf = std::enable_if<std::is_arithmetic_v<T>>>
|
| 80 |
+
explicit EIGEN_DEVICE_FUNC operator T() const {
|
| 81 |
+
return static_cast<T>(static_cast<float>(derived()));
|
| 82 |
+
}
|
| 83 |
+
explicit EIGEN_DEVICE_FUNC operator double() const {
|
| 84 |
+
return ConvertTo<double>(derived());
|
| 85 |
+
}
|
| 86 |
+
explicit EIGEN_DEVICE_FUNC operator float() const {
|
| 87 |
+
return ConvertTo<float>(derived());
|
| 88 |
+
}
|
| 89 |
+
explicit EIGEN_DEVICE_FUNC operator Eigen::bfloat16() const {
|
| 90 |
+
return ConvertTo<Eigen::bfloat16>(derived());
|
| 91 |
+
}
|
| 92 |
+
explicit EIGEN_DEVICE_FUNC operator Eigen::half() const {
|
| 93 |
+
return ConvertTo<Eigen::half>(derived());
|
| 94 |
+
}
|
| 95 |
+
explicit EIGEN_DEVICE_FUNC operator bool() const {
|
| 96 |
+
return (rep() & 0x7F) != 0;
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
constexpr Derived operator-() const {
|
| 100 |
+
return Derived(static_cast<uint8_t>(rep() ^ 0x80), ConstructFromRepTag{});
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
constexpr const Derived& derived() const {
|
| 104 |
+
return *static_cast<const Derived*>(this);
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
constexpr Derived& derived() { return *static_cast<Derived*>(this); }
|
| 108 |
+
|
| 109 |
+
static constexpr Derived FromRep(uint8_t rep) {
|
| 110 |
+
return Derived(rep, ConstructFromRepTag{});
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
// Conversions allowing saturation and truncation.
|
| 114 |
+
template <bool kSaturate = false, bool kTruncate = false, typename From>
|
| 115 |
+
static inline EIGEN_DEVICE_FUNC Derived ConvertFrom(const From& from);
|
| 116 |
+
|
| 117 |
+
template <typename To, bool kSaturate = false, bool kTruncate = false>
|
| 118 |
+
static inline EIGEN_DEVICE_FUNC To ConvertTo(const Derived& from);
|
| 119 |
+
|
| 120 |
+
// Operators via float32.
|
| 121 |
+
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Derived
|
| 122 |
+
operator+(const Derived& other) const {
|
| 123 |
+
return Derived{float{derived()} + float{other}};
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Derived
|
| 127 |
+
operator-(const Derived& other) const {
|
| 128 |
+
return Derived{float{derived()} - float{other}};
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Derived
|
| 132 |
+
operator*(const Derived& other) const {
|
| 133 |
+
return Derived{float{derived()} * float{other}};
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Derived
|
| 137 |
+
operator/(const Derived& other) const {
|
| 138 |
+
return Derived{float{derived()} / float{other}};
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
constexpr bool operator==(const Derived& other) const {
|
| 142 |
+
return Compare(derived(), other) == Ordering::kEquivalent;
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
constexpr bool operator!=(const Derived& other) const {
|
| 146 |
+
return Compare(derived(), other) != Ordering::kEquivalent;
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator<(
|
| 150 |
+
const Derived& other) const {
|
| 151 |
+
return Compare(derived(), other) == Ordering::kLess;
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator<=(
|
| 155 |
+
const Derived& other) const {
|
| 156 |
+
return Compare(derived(), other) <= Ordering::kEquivalent;
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
+
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator>(
|
| 160 |
+
const Derived& other) const {
|
| 161 |
+
return Compare(derived(), other) == Ordering::kGreater;
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator>=(
|
| 165 |
+
const Derived& other) const {
|
| 166 |
+
Ordering ordering = Compare(derived(), other);
|
| 167 |
+
return ordering == Ordering::kGreater || ordering == Ordering::kEquivalent;
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
// Compound assignment.
|
| 171 |
+
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Derived& operator+=(
|
| 172 |
+
const Derived& other) {
|
| 173 |
+
derived() = derived() + other;
|
| 174 |
+
return derived();
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Derived& operator-=(
|
| 178 |
+
const Derived& other) {
|
| 179 |
+
derived() = derived() - other;
|
| 180 |
+
return derived();
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Derived& operator*=(
|
| 184 |
+
const Derived& other) {
|
| 185 |
+
derived() = derived() * other;
|
| 186 |
+
return derived();
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
+
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Derived& operator/=(
|
| 190 |
+
const Derived& other) {
|
| 191 |
+
derived() = derived() / other;
|
| 192 |
+
return derived();
|
| 193 |
+
}
|
| 194 |
+
|
| 195 |
+
private:
|
| 196 |
+
static EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC std::pair<uint8_t, uint8_t>
|
| 197 |
+
SignAndMagnitude(Derived x) {
|
| 198 |
+
const uint8_t x_abs_bits =
|
| 199 |
+
Eigen::numext::bit_cast<uint8_t>(Eigen::numext::abs(x));
|
| 200 |
+
const uint8_t x_bits = Eigen::numext::bit_cast<uint8_t>(x);
|
| 201 |
+
const uint8_t x_sign = x_bits ^ x_abs_bits;
|
| 202 |
+
return {x_sign, x_abs_bits};
|
| 203 |
+
}
|
| 204 |
+
static EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC int8_t
|
| 205 |
+
SignAndMagnitudeToTwosComplement(uint8_t sign, uint8_t magnitude) {
|
| 206 |
+
return magnitude ^ (static_cast<int8_t>(sign) < 0 ? -1 : 0);
|
| 207 |
+
}
|
| 208 |
+
|
| 209 |
+
enum Ordering : int8_t {
|
| 210 |
+
kLess = -1,
|
| 211 |
+
kEquivalent = 0,
|
| 212 |
+
kGreater = 1,
|
| 213 |
+
kUnordered = 2,
|
| 214 |
+
};
|
| 215 |
+
|
| 216 |
+
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC friend Ordering Compare(
|
| 217 |
+
const Derived& lhs, const Derived& rhs) {
|
| 218 |
+
if (Eigen::numext::isnan(lhs) || Eigen::numext::isnan(rhs)) {
|
| 219 |
+
return Ordering::kUnordered;
|
| 220 |
+
}
|
| 221 |
+
auto [lhs_sign, lhs_mag] = SignAndMagnitude(lhs);
|
| 222 |
+
auto [rhs_sign, rhs_mag] = SignAndMagnitude(rhs);
|
| 223 |
+
if (lhs_mag == 0 && rhs_mag == 0) {
|
| 224 |
+
return Ordering::kEquivalent;
|
| 225 |
+
}
|
| 226 |
+
int8_t lhs_twos_complement =
|
| 227 |
+
SignAndMagnitudeToTwosComplement(lhs_sign, lhs_mag);
|
| 228 |
+
int8_t rhs_twos_complement =
|
| 229 |
+
SignAndMagnitudeToTwosComplement(rhs_sign, rhs_mag);
|
| 230 |
+
if (lhs_twos_complement < rhs_twos_complement) {
|
| 231 |
+
return Ordering::kLess;
|
| 232 |
+
}
|
| 233 |
+
if (lhs_twos_complement > rhs_twos_complement) {
|
| 234 |
+
return Ordering::kGreater;
|
| 235 |
+
}
|
| 236 |
+
return Ordering::kEquivalent;
|
| 237 |
+
}
|
| 238 |
+
|
| 239 |
+
uint8_t rep_;
|
| 240 |
+
};
|
| 241 |
+
|
| 242 |
+
class float8_e4m3fn : public float8_base<float8_e4m3fn> {
|
| 243 |
+
// Exponent: 4, Mantissa: 3, bias: 7.
|
| 244 |
+
// Extended range: no inf, NaN represented by 0bS111'1111.
|
| 245 |
+
// The "fn" suffix is for consistency with the corresponding LLVM/MLIR type,
|
| 246 |
+
// signaling this type is not consistent with IEEE-754. The "f" indicates
|
| 247 |
+
// it is finite values only. The "n" indicates it includes NaNs, but only
|
| 248 |
+
// at the outer range.
|
| 249 |
+
private:
|
| 250 |
+
using Base = float8_base<float8_e4m3fn>;
|
| 251 |
+
friend class float8_base<float8_e4m3fn>;
|
| 252 |
+
using Base::Base;
|
| 253 |
+
|
| 254 |
+
public:
|
| 255 |
+
explicit EIGEN_DEVICE_FUNC float8_e4m3fn(const float8_e5m2& f8)
|
| 256 |
+
: float8_e4m3fn(ConvertFrom(f8)) {}
|
| 257 |
+
explicit EIGEN_DEVICE_FUNC float8_e4m3fn(const float8_e4m3b11fnuz& f8)
|
| 258 |
+
: float8_e4m3fn(ConvertFrom(f8)) {}
|
| 259 |
+
};
|
| 260 |
+
|
| 261 |
+
class float8_e4m3b11fnuz : public float8_base<float8_e4m3b11fnuz> {
|
| 262 |
+
// Exponent: 4, Mantissa: 3, bias: 11.
|
| 263 |
+
// Extended range: no inf, NaN represented by 0b1000'0000.
|
| 264 |
+
private:
|
| 265 |
+
using Base = float8_base<float8_e4m3b11fnuz>;
|
| 266 |
+
friend class float8_base<float8_e4m3b11fnuz>;
|
| 267 |
+
using Base::Base;
|
| 268 |
+
|
| 269 |
+
public:
|
| 270 |
+
explicit EIGEN_DEVICE_FUNC float8_e4m3b11fnuz(const float8_e5m2& f8)
|
| 271 |
+
: float8_e4m3b11fnuz(ConvertFrom(f8)) {}
|
| 272 |
+
explicit EIGEN_DEVICE_FUNC float8_e4m3b11fnuz(const float8_e5m2fnuz& f8)
|
| 273 |
+
: float8_e4m3b11fnuz(ConvertFrom(f8)) {}
|
| 274 |
+
explicit EIGEN_DEVICE_FUNC float8_e4m3b11fnuz(const float8_e4m3fn& f8)
|
| 275 |
+
: float8_e4m3b11fnuz(ConvertFrom(f8)) {}
|
| 276 |
+
explicit EIGEN_DEVICE_FUNC float8_e4m3b11fnuz(const float8_e4m3fnuz& f8)
|
| 277 |
+
: float8_e4m3b11fnuz(ConvertFrom(f8)) {}
|
| 278 |
+
|
| 279 |
+
constexpr float8_e4m3b11fnuz operator-() const {
|
| 280 |
+
if ((rep() & 0x7f) == 0x00) {
|
| 281 |
+
return *this;
|
| 282 |
+
}
|
| 283 |
+
return Base::operator-();
|
| 284 |
+
}
|
| 285 |
+
|
| 286 |
+
float8_e4m3b11fnuz operator-(const float8_e4m3b11fnuz& other) const {
|
| 287 |
+
return Base::operator-(other);
|
| 288 |
+
}
|
| 289 |
+
|
| 290 |
+
explicit EIGEN_DEVICE_FUNC operator bool() const { return rep() != 0; }
|
| 291 |
+
};
|
| 292 |
+
|
| 293 |
+
// Legacy name used in XLA (TODO(jewillco): remove).
|
| 294 |
+
using float8_e4m3b11 = float8_e4m3b11fnuz;
|
| 295 |
+
|
| 296 |
+
class float8_e4m3fnuz : public float8_base<float8_e4m3fnuz> {
|
| 297 |
+
// 8-bit floating point with 3 bit mantissa.
|
| 298 |
+
//
|
| 299 |
+
// An 8-bit floating point type with 1 sign bit, 4 bits exponent and 3 bits
|
| 300 |
+
// mantissa. The suffix "fnuz" is consistent with LLVM/MLIR naming and is
|
| 301 |
+
// derived from the differences to IEEE floating point conventions. `F` is
|
| 302 |
+
// for "finite" (no infinities), `N` for with special NaN encoding, `UZ` for
|
| 303 |
+
// unsigned zero.
|
| 304 |
+
//
|
| 305 |
+
// This type has the following characteristics:
|
| 306 |
+
// * bit encoding: S1E4M3 - `0bSEEEEMMM`
|
| 307 |
+
// * exponent bias: 8
|
| 308 |
+
// * infinities: Not supported
|
| 309 |
+
// * NaNs: Supported with sign bit set to 1, exponent bits and mantissa bits
|
| 310 |
+
// set to all 0s - `0b10000000`
|
| 311 |
+
// * denormals when exponent is 0
|
| 312 |
+
private:
|
| 313 |
+
using Base = float8_base<float8_e4m3fnuz>;
|
| 314 |
+
friend class float8_base<float8_e4m3fnuz>;
|
| 315 |
+
using Base::Base;
|
| 316 |
+
|
| 317 |
+
public:
|
| 318 |
+
explicit EIGEN_DEVICE_FUNC float8_e4m3fnuz(const float8_e5m2& f8)
|
| 319 |
+
: float8_e4m3fnuz(ConvertFrom(f8)) {}
|
| 320 |
+
explicit EIGEN_DEVICE_FUNC float8_e4m3fnuz(const float8_e5m2fnuz& f8)
|
| 321 |
+
: float8_e4m3fnuz(ConvertFrom(f8)) {}
|
| 322 |
+
explicit EIGEN_DEVICE_FUNC float8_e4m3fnuz(const float8_e4m3b11fnuz& f8)
|
| 323 |
+
: float8_e4m3fnuz(ConvertFrom(f8)) {}
|
| 324 |
+
explicit EIGEN_DEVICE_FUNC float8_e4m3fnuz(const float8_e4m3fn& f8)
|
| 325 |
+
: float8_e4m3fnuz(ConvertFrom(f8)) {}
|
| 326 |
+
|
| 327 |
+
constexpr float8_e4m3fnuz operator-() const {
|
| 328 |
+
if ((rep() & 0x7f) == 0x00) {
|
| 329 |
+
return *this;
|
| 330 |
+
}
|
| 331 |
+
return Base::operator-();
|
| 332 |
+
}
|
| 333 |
+
|
| 334 |
+
float8_e4m3fnuz operator-(const float8_e4m3fnuz& other) const {
|
| 335 |
+
return Base::operator-(other);
|
| 336 |
+
}
|
| 337 |
+
|
| 338 |
+
explicit EIGEN_DEVICE_FUNC operator bool() const { return rep() != 0; }
|
| 339 |
+
};
|
| 340 |
+
|
| 341 |
+
class float8_e5m2 : public float8_base<float8_e5m2> {
|
| 342 |
+
// Exponent: 5, Mantissa: 2, bias: 15.
|
| 343 |
+
// IEEE 754.
|
| 344 |
+
private:
|
| 345 |
+
using Base = float8_base<float8_e5m2>;
|
| 346 |
+
friend class float8_base<float8_e5m2>;
|
| 347 |
+
using Base::Base;
|
| 348 |
+
|
| 349 |
+
public:
|
| 350 |
+
explicit EIGEN_DEVICE_FUNC float8_e5m2(float8_e4m3fn f8)
|
| 351 |
+
: float8_e5m2(ConvertFrom(f8)) {}
|
| 352 |
+
explicit EIGEN_DEVICE_FUNC float8_e5m2(float8_e4m3fnuz f8)
|
| 353 |
+
: float8_e5m2(ConvertFrom(f8)) {}
|
| 354 |
+
explicit EIGEN_DEVICE_FUNC float8_e5m2(float8_e4m3b11fnuz f8)
|
| 355 |
+
: float8_e5m2(ConvertFrom(f8)) {}
|
| 356 |
+
explicit EIGEN_DEVICE_FUNC float8_e5m2(float8_e5m2fnuz& f8)
|
| 357 |
+
: float8_e5m2(ConvertFrom(f8)) {}
|
| 358 |
+
};
|
| 359 |
+
|
| 360 |
+
class float8_e5m2fnuz : public float8_base<float8_e5m2fnuz> {
|
| 361 |
+
// 8-bit floating point with 2 bit mantissa.
|
| 362 |
+
//
|
| 363 |
+
// An 8-bit floating point type with 1 sign bit, 5 bits exponent and 2 bits
|
| 364 |
+
// mantissa. The suffix "fnuz" is consistent with LLVM/MLIR naming and is
|
| 365 |
+
// derived from the differences to IEEE floating point conventions. `F` is
|
| 366 |
+
// for "finite" (no infinities), `N` for with special NaN encoding, `UZ` for
|
| 367 |
+
// unsigned zero.
|
| 368 |
+
//
|
| 369 |
+
// This type has the following characteristics:
|
| 370 |
+
// * bit encoding: S1E5M2 - `0bSEEEEEMM`
|
| 371 |
+
// * exponent bias: 16
|
| 372 |
+
// * infinities: Not supported
|
| 373 |
+
// * NaNs: Supported with sign bit set to 1, exponent bits and mantissa bits
|
| 374 |
+
// set to all 0s - `0b10000000`
|
| 375 |
+
// * denormals when exponent is 0
|
| 376 |
+
private:
|
| 377 |
+
using Base = float8_base<float8_e5m2fnuz>;
|
| 378 |
+
friend class float8_base<float8_e5m2fnuz>;
|
| 379 |
+
using Base::Base;
|
| 380 |
+
|
| 381 |
+
public:
|
| 382 |
+
explicit EIGEN_DEVICE_FUNC float8_e5m2fnuz(const float8_e5m2& f8)
|
| 383 |
+
: float8_e5m2fnuz(ConvertFrom(f8)) {}
|
| 384 |
+
explicit EIGEN_DEVICE_FUNC float8_e5m2fnuz(const float8_e4m3b11fnuz& f8)
|
| 385 |
+
: float8_e5m2fnuz(ConvertFrom(f8)) {}
|
| 386 |
+
explicit EIGEN_DEVICE_FUNC float8_e5m2fnuz(const float8_e4m3fn& f8)
|
| 387 |
+
: float8_e5m2fnuz(ConvertFrom(f8)) {}
|
| 388 |
+
explicit EIGEN_DEVICE_FUNC float8_e5m2fnuz(const float8_e4m3fnuz& f8)
|
| 389 |
+
: float8_e5m2fnuz(ConvertFrom(f8)) {}
|
| 390 |
+
|
| 391 |
+
constexpr float8_e5m2fnuz operator-() const {
|
| 392 |
+
if ((rep() & 0x7f) == 0x00) {
|
| 393 |
+
return *this;
|
| 394 |
+
}
|
| 395 |
+
return Base::operator-();
|
| 396 |
+
}
|
| 397 |
+
|
| 398 |
+
float8_e5m2fnuz operator-(const float8_e5m2fnuz& other) const {
|
| 399 |
+
return Base::operator-(other);
|
| 400 |
+
}
|
| 401 |
+
|
| 402 |
+
explicit EIGEN_DEVICE_FUNC operator bool() const { return rep() != 0; }
|
| 403 |
+
};
|
| 404 |
+
|
| 405 |
+
constexpr double ConstexprAbs(double x) { return x < 0.0 ? -x : x; }
|
| 406 |
+
|
| 407 |
+
constexpr double ConstexprCeil(double x) {
|
| 408 |
+
constexpr double kIntegerThreshold =
|
| 409 |
+
uint64_t{1} << (std::numeric_limits<double>::digits - 1);
|
| 410 |
+
// Too big or NaN inputs get returned unchanged.
|
| 411 |
+
if (!(ConstexprAbs(x) < kIntegerThreshold)) {
|
| 412 |
+
return x;
|
| 413 |
+
}
|
| 414 |
+
const double x_trunc = static_cast<double>(static_cast<int64_t>(x));
|
| 415 |
+
return x_trunc < x ? x_trunc + 1.0 : x_trunc;
|
| 416 |
+
}
|
| 417 |
+
|
| 418 |
+
constexpr double ConstexprFloor(double x) { return -ConstexprCeil(-x); }
|
| 419 |
+
|
| 420 |
+
constexpr double kLog10Of2 = 0.3010299956639812;
|
| 421 |
+
// C17 5.2.4.2.2p11:
|
| 422 |
+
// "number of decimal digits, q, such that any floating-point number with q
|
| 423 |
+
// decimal digits can be rounded into a floating-point number with p radix b
|
| 424 |
+
// digits and back again without change to the q decimal digits"
|
| 425 |
+
// floor((p - 1) * log10(2));
|
| 426 |
+
constexpr int Digits10FromDigits(int digits) {
|
| 427 |
+
return static_cast<int>(ConstexprFloor((digits - 1) * kLog10Of2));
|
| 428 |
+
}
|
| 429 |
+
|
| 430 |
+
// C17 5.2.4.2.2p11:
|
| 431 |
+
// "number of decimal digits, n, such that any floating-point number with p
|
| 432 |
+
// radix b digits can be rounded to a floating-point number with n decimal
|
| 433 |
+
// digits and back again without change to the value"
|
| 434 |
+
// ceil(1 + p * log10(2));
|
| 435 |
+
constexpr int MaxDigits10FromDigits(int digits) {
|
| 436 |
+
return static_cast<int>(ConstexprCeil(1.0 + (digits * kLog10Of2)));
|
| 437 |
+
}
|
| 438 |
+
|
| 439 |
+
// C17 5.2.4.2.2p11:
|
| 440 |
+
// "minimum negative integer such that 10 raised to that power is in the range
|
| 441 |
+
// of normalized floating-point numbers"
|
| 442 |
+
// ceil(log10(2**(emin - 1))) == ceil((emin - 1) * log10(2));
|
| 443 |
+
constexpr int MinExponent10FromMinExponent(int min_exponent) {
|
| 444 |
+
return static_cast<int>(ConstexprCeil((min_exponent - 1) * kLog10Of2));
|
| 445 |
+
}
|
| 446 |
+
|
| 447 |
+
// C17 5.2.4.2.2p11:
|
| 448 |
+
// "maximum integer such that 10 raised to that power is in the range of
|
| 449 |
+
// representable finite floating-point numbers"
|
| 450 |
+
// floor(log10((1 - 2**-p) * 2**emax)) == floor(log10(1 - 2**-p) +
|
| 451 |
+
// emax * log10(2))
|
| 452 |
+
constexpr int MaxExponent10FromMaxExponentAndDigits(int max_exponent,
|
| 453 |
+
int digits) {
|
| 454 |
+
// We only support digits in {3,4}. This table would grow if we wanted to
|
| 455 |
+
// handle more values.
|
| 456 |
+
constexpr double kLog10OfOnePredecessor[] = {
|
| 457 |
+
// log10(1 - 2**-3)
|
| 458 |
+
-0.057991946977686754,
|
| 459 |
+
// log10(1 - 2**-4)
|
| 460 |
+
-0.028028723600243537,
|
| 461 |
+
};
|
| 462 |
+
return static_cast<int>(ConstexprFloor(kLog10OfOnePredecessor[digits - 3] +
|
| 463 |
+
max_exponent * kLog10Of2));
|
| 464 |
+
}
|
| 465 |
+
|
| 466 |
+
// Structures for use in specializing std::numeric_limits.
|
| 467 |
+
struct numeric_limits_float8_base {
|
| 468 |
+
// NOLINTBEGIN: these names must match std::numeric_limits.
|
| 469 |
+
static inline constexpr const bool is_specialized = true;
|
| 470 |
+
static inline constexpr const bool is_signed = true;
|
| 471 |
+
static inline constexpr const bool is_integer = false;
|
| 472 |
+
static inline constexpr const bool is_exact = false;
|
| 473 |
+
static inline constexpr const bool has_quiet_NaN = true;
|
| 474 |
+
static inline constexpr const std::float_denorm_style has_denorm =
|
| 475 |
+
std::denorm_present;
|
| 476 |
+
static inline constexpr const bool has_denorm_loss = false;
|
| 477 |
+
static inline constexpr const std::float_round_style round_style =
|
| 478 |
+
std::round_to_nearest;
|
| 479 |
+
static inline constexpr const bool is_bounded = true;
|
| 480 |
+
static inline constexpr const bool is_modulo = false;
|
| 481 |
+
static inline constexpr const int radix = std::numeric_limits<float>::radix;
|
| 482 |
+
static inline constexpr const bool traps = std::numeric_limits<float>::traps;
|
| 483 |
+
static inline constexpr const bool tinyness_before =
|
| 484 |
+
std::numeric_limits<float>::tinyness_before;
|
| 485 |
+
// NOLINTEND
|
| 486 |
+
};
|
| 487 |
+
|
| 488 |
+
struct numeric_limits_float8_e4m3fn : public numeric_limits_float8_base {
|
| 489 |
+
private:
|
| 490 |
+
static inline constexpr const int kExponentBias = 7;
|
| 491 |
+
static inline constexpr const int kMantissaBits = 3;
|
| 492 |
+
|
| 493 |
+
public:
|
| 494 |
+
// NOLINTBEGIN: these names must match std::numeric_limits.
|
| 495 |
+
static inline constexpr const int digits = kMantissaBits + 1;
|
| 496 |
+
static inline constexpr const int digits10 = Digits10FromDigits(digits);
|
| 497 |
+
static inline constexpr const int max_digits10 =
|
| 498 |
+
MaxDigits10FromDigits(digits);
|
| 499 |
+
static inline constexpr const int min_exponent = (1 - kExponentBias) + 1;
|
| 500 |
+
static inline constexpr const int min_exponent10 =
|
| 501 |
+
MinExponent10FromMinExponent(min_exponent);
|
| 502 |
+
static inline constexpr const int max_exponent =
|
| 503 |
+
(0b1111 - 7) + 1; // Extended format.
|
| 504 |
+
static inline constexpr const int max_exponent10 =
|
| 505 |
+
MaxExponent10FromMaxExponentAndDigits(max_exponent, digits);
|
| 506 |
+
static inline constexpr const bool is_iec559 = false;
|
| 507 |
+
static inline constexpr const bool has_infinity = false;
|
| 508 |
+
static inline constexpr const bool has_signaling_NaN = false;
|
| 509 |
+
// NOLINTEND
|
| 510 |
+
|
| 511 |
+
// 1.0 * 2^(0b0001 - 7) = 1.0 * 2^-6 = 0.015625
|
| 512 |
+
static constexpr float8_e4m3fn min() {
|
| 513 |
+
return float8_e4m3fn::FromRep(0b0'0001 << kMantissaBits);
|
| 514 |
+
}
|
| 515 |
+
// -(1 + 0b110 * 2^-3) * 2^(0b1111 - 7) = -1.75 * 2^8 = 448
|
| 516 |
+
static constexpr float8_e4m3fn lowest() {
|
| 517 |
+
return float8_e4m3fn::FromRep(0b1'1111'110);
|
| 518 |
+
}
|
| 519 |
+
// (1 + 0b110 * 2^-3) * 2**(0b1111 - 7) = 1.75 * 2^8 = 448
|
| 520 |
+
static constexpr float8_e4m3fn max() {
|
| 521 |
+
return float8_e4m3fn::FromRep(0b0'1111'110);
|
| 522 |
+
}
|
| 523 |
+
// 1.0 * 2^-3 = 0.125
|
| 524 |
+
static constexpr float8_e4m3fn epsilon() {
|
| 525 |
+
return float8_e4m3fn::FromRep((-kMantissaBits + kExponentBias)
|
| 526 |
+
<< kMantissaBits);
|
| 527 |
+
}
|
| 528 |
+
// 1.0 * 2^-1 = 0.5
|
| 529 |
+
static constexpr float8_e4m3fn round_error() {
|
| 530 |
+
return float8_e4m3fn::FromRep((-1 + kExponentBias) << kMantissaBits);
|
| 531 |
+
}
|
| 532 |
+
static constexpr float8_e4m3fn infinity() {
|
| 533 |
+
return float8_e4m3fn::FromRep(0b0'1111'111);
|
| 534 |
+
}
|
| 535 |
+
// NaN.
|
| 536 |
+
static constexpr float8_e4m3fn quiet_NaN() {
|
| 537 |
+
return float8_e4m3fn::FromRep(0b0'1111'111);
|
| 538 |
+
}
|
| 539 |
+
static constexpr float8_e4m3fn signaling_NaN() {
|
| 540 |
+
return float8_e4m3fn::FromRep(0b0'1111'111);
|
| 541 |
+
}
|
| 542 |
+
// 1.0 * 2^(-7 - 3 + 1) = 1.0 * 2^-9 = 0.001953125
|
| 543 |
+
static constexpr float8_e4m3fn denorm_min() {
|
| 544 |
+
return float8_e4m3fn::FromRep(0b0'0000'001);
|
| 545 |
+
}
|
| 546 |
+
};
|
| 547 |
+
|
| 548 |
+
struct numeric_limits_float8_e4m3b11fnuz : public numeric_limits_float8_base {
|
| 549 |
+
private:
|
| 550 |
+
static inline constexpr const int kExponentBias = 11;
|
| 551 |
+
static inline constexpr const int kMantissaBits = 3;
|
| 552 |
+
|
| 553 |
+
public:
|
| 554 |
+
// NOLINTBEGIN: these names must match std::numeric_limits.
|
| 555 |
+
static inline constexpr const int digits = kMantissaBits + 1;
|
| 556 |
+
static inline constexpr const int digits10 = Digits10FromDigits(digits);
|
| 557 |
+
static inline constexpr const int max_digits10 =
|
| 558 |
+
MaxDigits10FromDigits(digits);
|
| 559 |
+
static inline constexpr const int min_exponent = (1 - kExponentBias) + 1;
|
| 560 |
+
static inline constexpr const int min_exponent10 =
|
| 561 |
+
MinExponent10FromMinExponent(min_exponent);
|
| 562 |
+
static inline constexpr const int max_exponent =
|
| 563 |
+
(0b1111 - kExponentBias) + 1; // Extended format.
|
| 564 |
+
static inline constexpr const int max_exponent10 =
|
| 565 |
+
MaxExponent10FromMaxExponentAndDigits(max_exponent, digits);
|
| 566 |
+
static inline constexpr const bool is_iec559 = false;
|
| 567 |
+
static inline constexpr const bool has_infinity = false;
|
| 568 |
+
static inline constexpr const bool has_signaling_NaN = false;
|
| 569 |
+
// NOLINTEND
|
| 570 |
+
|
| 571 |
+
// 1.0 * 2^(0b0001 - 11) = 1.0 * 2^-10 = 0.0009765625
|
| 572 |
+
static constexpr float8_e4m3b11fnuz min() {
|
| 573 |
+
return float8_e4m3b11fnuz::FromRep(1 << kMantissaBits);
|
| 574 |
+
}
|
| 575 |
+
// -(1 + 0b111 * 2^-3) * 2^(0b1111 - 11) = -1.875 * 2^4 = -30
|
| 576 |
+
static constexpr float8_e4m3b11fnuz lowest() {
|
| 577 |
+
return float8_e4m3b11fnuz::FromRep(0b1'1111'111);
|
| 578 |
+
}
|
| 579 |
+
// (1 + 0b111 * 2^-3) * 2^(0b1111 - 11) = 1.875 * 2^4 = 30
|
| 580 |
+
static constexpr float8_e4m3b11fnuz max() {
|
| 581 |
+
return float8_e4m3b11fnuz::FromRep(0b0'1111'111);
|
| 582 |
+
}
|
| 583 |
+
// 1.0 * 2^-3 = 0.125
|
| 584 |
+
static constexpr float8_e4m3b11fnuz epsilon() {
|
| 585 |
+
return float8_e4m3b11fnuz::FromRep((-kMantissaBits + kExponentBias)
|
| 586 |
+
<< kMantissaBits);
|
| 587 |
+
}
|
| 588 |
+
// 1.0 * 2^-1 = 0.5
|
| 589 |
+
static constexpr float8_e4m3b11fnuz round_error() {
|
| 590 |
+
return float8_e4m3b11fnuz::FromRep((-1 + kExponentBias) << kMantissaBits);
|
| 591 |
+
}
|
| 592 |
+
static constexpr float8_e4m3b11fnuz infinity() {
|
| 593 |
+
return float8_e4m3b11fnuz::FromRep(0b1'0000'000);
|
| 594 |
+
}
|
| 595 |
+
// NaN.
|
| 596 |
+
static constexpr float8_e4m3b11fnuz quiet_NaN() {
|
| 597 |
+
return float8_e4m3b11fnuz::FromRep(0b1'0000'000);
|
| 598 |
+
}
|
| 599 |
+
static constexpr float8_e4m3b11fnuz signaling_NaN() {
|
| 600 |
+
return float8_e4m3b11fnuz::FromRep(0b1'0000'000);
|
| 601 |
+
}
|
| 602 |
+
// 1.0 * 2^(-11 - 3 + 1) = 1.0 * 2^-13 = 0.0001220703125
|
| 603 |
+
static constexpr float8_e4m3b11fnuz denorm_min() {
|
| 604 |
+
return float8_e4m3b11fnuz::FromRep(0b0'0000'001);
|
| 605 |
+
}
|
| 606 |
+
};
|
| 607 |
+
|
| 608 |
+
struct numeric_limits_float8_e4m3fnuz : public numeric_limits_float8_base {
|
| 609 |
+
private:
|
| 610 |
+
static inline constexpr const int kExponentBias = 8;
|
| 611 |
+
static inline constexpr const int kMantissaBits = 3;
|
| 612 |
+
|
| 613 |
+
public:
|
| 614 |
+
// NOLINTBEGIN: these names must match std::numeric_limits.
|
| 615 |
+
static inline constexpr const int digits = kMantissaBits + 1;
|
| 616 |
+
static inline constexpr const int digits10 = Digits10FromDigits(digits);
|
| 617 |
+
static inline constexpr const int max_digits10 =
|
| 618 |
+
MaxDigits10FromDigits(digits);
|
| 619 |
+
static inline constexpr const int min_exponent = (1 - kExponentBias) + 1;
|
| 620 |
+
static inline constexpr const int min_exponent10 =
|
| 621 |
+
MinExponent10FromMinExponent(min_exponent);
|
| 622 |
+
static inline constexpr const int max_exponent =
|
| 623 |
+
(0b1111 - kExponentBias) + 1; // Extended format.
|
| 624 |
+
static inline constexpr const int max_exponent10 =
|
| 625 |
+
MaxExponent10FromMaxExponentAndDigits(max_exponent, digits);
|
| 626 |
+
static inline constexpr const bool is_iec559 = false;
|
| 627 |
+
static inline constexpr const bool has_infinity = false;
|
| 628 |
+
static inline constexpr const bool has_signaling_NaN = false;
|
| 629 |
+
// NOLINTEND
|
| 630 |
+
|
| 631 |
+
static constexpr float8_e4m3fnuz min() {
|
| 632 |
+
return float8_e4m3fnuz::FromRep(0x08);
|
| 633 |
+
}
|
| 634 |
+
static constexpr float8_e4m3fnuz lowest() {
|
| 635 |
+
return float8_e4m3fnuz::FromRep(0xFF);
|
| 636 |
+
}
|
| 637 |
+
static constexpr float8_e4m3fnuz max() {
|
| 638 |
+
return float8_e4m3fnuz::FromRep(0x7F);
|
| 639 |
+
}
|
| 640 |
+
static constexpr float8_e4m3fnuz epsilon() {
|
| 641 |
+
return float8_e4m3fnuz::FromRep((-kMantissaBits + kExponentBias)
|
| 642 |
+
<< kMantissaBits);
|
| 643 |
+
}
|
| 644 |
+
static constexpr float8_e4m3fnuz round_error() {
|
| 645 |
+
return float8_e4m3fnuz::FromRep((-1 + kExponentBias) << kMantissaBits);
|
| 646 |
+
}
|
| 647 |
+
static constexpr float8_e4m3fnuz infinity() {
|
| 648 |
+
return float8_e4m3fnuz::FromRep(0x80);
|
| 649 |
+
} // NaN.
|
| 650 |
+
static constexpr float8_e4m3fnuz quiet_NaN() {
|
| 651 |
+
return float8_e4m3fnuz::FromRep(0x80);
|
| 652 |
+
}
|
| 653 |
+
static constexpr float8_e4m3fnuz signaling_NaN() {
|
| 654 |
+
return float8_e4m3fnuz::FromRep(0x80);
|
| 655 |
+
}
|
| 656 |
+
static constexpr float8_e4m3fnuz denorm_min() {
|
| 657 |
+
return float8_e4m3fnuz::FromRep(0x01);
|
| 658 |
+
}
|
| 659 |
+
};
|
| 660 |
+
|
| 661 |
+
struct numeric_limits_float8_e5m2 : public numeric_limits_float8_base {
|
| 662 |
+
private:
|
| 663 |
+
static inline constexpr const int kExponentBias = 15;
|
| 664 |
+
static inline constexpr const int kMantissaBits = 2;
|
| 665 |
+
|
| 666 |
+
public:
|
| 667 |
+
// NOLINTBEGIN: these names must match std::numeric_limits.
|
| 668 |
+
static inline constexpr const int digits = kMantissaBits + 1;
|
| 669 |
+
static inline constexpr const int digits10 = Digits10FromDigits(digits);
|
| 670 |
+
static inline constexpr const int max_digits10 =
|
| 671 |
+
MaxDigits10FromDigits(digits);
|
| 672 |
+
static inline constexpr const int min_exponent = (1 - kExponentBias) + 1;
|
| 673 |
+
static inline constexpr const int min_exponent10 =
|
| 674 |
+
MinExponent10FromMinExponent(min_exponent);
|
| 675 |
+
static inline constexpr const int max_exponent = 0b11111 - kExponentBias;
|
| 676 |
+
static inline constexpr const int max_exponent10 =
|
| 677 |
+
MaxExponent10FromMaxExponentAndDigits(max_exponent, digits);
|
| 678 |
+
static inline constexpr const bool is_iec559 = true;
|
| 679 |
+
static inline constexpr const bool has_infinity = true;
|
| 680 |
+
static inline constexpr const bool has_signaling_NaN = true;
|
| 681 |
+
// NOLINTEND
|
| 682 |
+
|
| 683 |
+
// 1.0 * 2^(0b00001 - 15) = 1.0 * 2^-14 = 0.00006103515625
|
| 684 |
+
static constexpr float8_e5m2 min() {
|
| 685 |
+
return float8_e5m2::FromRep(1 << kMantissaBits);
|
| 686 |
+
}
|
| 687 |
+
// -(1 + 0b11 * 2^-2) * 2^(0b11110 - 15) = -1.75 * 2^15 = -57344
|
| 688 |
+
static constexpr float8_e5m2 lowest() {
|
| 689 |
+
return float8_e5m2::FromRep(0b1'11110'11);
|
| 690 |
+
}
|
| 691 |
+
// (1 + 0b11 * 2^-2) * 2^(0b11110 - 15) = 1.75 * 2^15 = 57344
|
| 692 |
+
static constexpr float8_e5m2 max() {
|
| 693 |
+
return float8_e5m2::FromRep(0b0'11110'11);
|
| 694 |
+
}
|
| 695 |
+
// 1.0 * 2^-2 = 0.25
|
| 696 |
+
static constexpr float8_e5m2 epsilon() {
|
| 697 |
+
return float8_e5m2::FromRep((-kMantissaBits + kExponentBias)
|
| 698 |
+
<< kMantissaBits);
|
| 699 |
+
}
|
| 700 |
+
// 1.0 * 2^-1 = 0.5
|
| 701 |
+
static constexpr float8_e5m2 round_error() {
|
| 702 |
+
return float8_e5m2::FromRep((-1 + kExponentBias) << kMantissaBits);
|
| 703 |
+
}
|
| 704 |
+
static constexpr float8_e5m2 infinity() {
|
| 705 |
+
return float8_e5m2::FromRep(0b0'11111'00);
|
| 706 |
+
}
|
| 707 |
+
static constexpr float8_e5m2 quiet_NaN() {
|
| 708 |
+
// IEEE 754-2019 6.2.1: "All binary NaN bit strings have the sign bit S set
|
| 709 |
+
// to 0 or 1 and all the bits of the biased exponent field E set to 1
|
| 710 |
+
// (see 3.4). A quiet NaN bit string should be encoded with the first bit
|
| 711 |
+
// (d1) of the trailing significand field T being 1."
|
| 712 |
+
return float8_e5m2::FromRep(0b0'11111'10);
|
| 713 |
+
}
|
| 714 |
+
static constexpr float8_e5m2 signaling_NaN() {
|
| 715 |
+
// IEEE 754-2019 6.2.1: "A signaling NaN bit string should be encoded with
|
| 716 |
+
// the first bit of the trailing significand field being 0."
|
| 717 |
+
return float8_e5m2::FromRep(0b0'11111'01);
|
| 718 |
+
}
|
| 719 |
+
// 1.0 * 2^(-15 - 2 + 1) = 1.0 * 2^-16 = 0.0000152587890625
|
| 720 |
+
static constexpr float8_e5m2 denorm_min() {
|
| 721 |
+
return float8_e5m2::FromRep(0b0'00000'01);
|
| 722 |
+
}
|
| 723 |
+
};
|
| 724 |
+
|
| 725 |
+
struct numeric_limits_float8_e5m2fnuz : public numeric_limits_float8_base {
|
| 726 |
+
private:
|
| 727 |
+
static inline constexpr const int kExponentBias = 16;
|
| 728 |
+
static inline constexpr const int kMantissaBits = 2;
|
| 729 |
+
|
| 730 |
+
public:
|
| 731 |
+
// NOLINTBEGIN: these names must match std::numeric_limits.
|
| 732 |
+
static inline constexpr const int digits = kMantissaBits + 1;
|
| 733 |
+
static inline constexpr const int digits10 = Digits10FromDigits(digits);
|
| 734 |
+
static inline constexpr const int max_digits10 =
|
| 735 |
+
MaxDigits10FromDigits(digits);
|
| 736 |
+
static inline constexpr const int min_exponent = (1 - kExponentBias) + 1;
|
| 737 |
+
static inline constexpr const int min_exponent10 =
|
| 738 |
+
MinExponent10FromMinExponent(min_exponent);
|
| 739 |
+
static inline constexpr const int max_exponent =
|
| 740 |
+
(0b11111 - kExponentBias) + 1;
|
| 741 |
+
static inline constexpr const int max_exponent10 =
|
| 742 |
+
MaxExponent10FromMaxExponentAndDigits(max_exponent, digits);
|
| 743 |
+
static inline constexpr const bool is_iec559 = false;
|
| 744 |
+
static inline constexpr const bool has_infinity = false;
|
| 745 |
+
static inline constexpr const bool has_signaling_NaN = false;
|
| 746 |
+
// NOLINTEND
|
| 747 |
+
|
| 748 |
+
static constexpr float8_e5m2fnuz min() {
|
| 749 |
+
return float8_e5m2fnuz::FromRep(0x04);
|
| 750 |
+
}
|
| 751 |
+
static constexpr float8_e5m2fnuz lowest() {
|
| 752 |
+
return float8_e5m2fnuz::FromRep(0xFF);
|
| 753 |
+
}
|
| 754 |
+
static constexpr float8_e5m2fnuz max() {
|
| 755 |
+
return float8_e5m2fnuz::FromRep(0x7F);
|
| 756 |
+
}
|
| 757 |
+
static constexpr float8_e5m2fnuz epsilon() {
|
| 758 |
+
return float8_e5m2fnuz::FromRep((-kMantissaBits + kExponentBias)
|
| 759 |
+
<< kMantissaBits);
|
| 760 |
+
}
|
| 761 |
+
static constexpr float8_e5m2fnuz round_error() {
|
| 762 |
+
return float8_e5m2fnuz::FromRep((-1 + kExponentBias) << kMantissaBits);
|
| 763 |
+
}
|
| 764 |
+
static constexpr float8_e5m2fnuz infinity() {
|
| 765 |
+
return float8_e5m2fnuz::FromRep(0x80);
|
| 766 |
+
} // NaN.
|
| 767 |
+
static constexpr float8_e5m2fnuz quiet_NaN() {
|
| 768 |
+
return float8_e5m2fnuz::FromRep(0x80);
|
| 769 |
+
}
|
| 770 |
+
static constexpr float8_e5m2fnuz signaling_NaN() {
|
| 771 |
+
return float8_e5m2fnuz::FromRep(0x80);
|
| 772 |
+
}
|
| 773 |
+
static constexpr float8_e5m2fnuz denorm_min() {
|
| 774 |
+
return float8_e5m2fnuz::FromRep(0x01);
|
| 775 |
+
}
|
| 776 |
+
};
|
| 777 |
+
|
| 778 |
+
} // namespace float8_internal
|
| 779 |
+
} // namespace ml_dtypes
|
| 780 |
+
|
| 781 |
+
namespace std {
|
| 782 |
+
// Standard-library overrides. Note that these are picked up by Eigen as well.
|
| 783 |
+
template <>
|
| 784 |
+
struct numeric_limits<ml_dtypes::float8_internal::float8_e4m3fn>
|
| 785 |
+
: public ml_dtypes::float8_internal::numeric_limits_float8_e4m3fn {};
|
| 786 |
+
|
| 787 |
+
template <>
|
| 788 |
+
struct numeric_limits<ml_dtypes::float8_internal::float8_e4m3b11fnuz>
|
| 789 |
+
: public ml_dtypes::float8_internal::numeric_limits_float8_e4m3b11fnuz {};
|
| 790 |
+
|
| 791 |
+
template <>
|
| 792 |
+
struct numeric_limits<ml_dtypes::float8_internal::float8_e4m3fnuz>
|
| 793 |
+
: public ml_dtypes::float8_internal::numeric_limits_float8_e4m3fnuz {};
|
| 794 |
+
|
| 795 |
+
template <>
|
| 796 |
+
struct numeric_limits<ml_dtypes::float8_internal::float8_e5m2>
|
| 797 |
+
: public ml_dtypes::float8_internal::numeric_limits_float8_e5m2 {};
|
| 798 |
+
|
| 799 |
+
template <>
|
| 800 |
+
struct numeric_limits<ml_dtypes::float8_internal::float8_e5m2fnuz>
|
| 801 |
+
: public ml_dtypes::float8_internal::numeric_limits_float8_e5m2fnuz {};
|
| 802 |
+
} // namespace std
|
| 803 |
+
|
| 804 |
+
namespace ml_dtypes {
|
| 805 |
+
namespace float8_internal {
|
| 806 |
+
|
| 807 |
+
// Free-functions for use with ADL and in Eigen.
|
| 808 |
+
constexpr inline float8_e4m3fn abs(const float8_e4m3fn& a) {
|
| 809 |
+
return float8_e4m3fn::FromRep(a.rep() & 0b0'1111'111);
|
| 810 |
+
}
|
| 811 |
+
|
| 812 |
+
constexpr inline bool(isnan)(const float8_e4m3fn& a) {
|
| 813 |
+
return abs(a).rep() == std::numeric_limits<float8_e4m3fn>::quiet_NaN().rep();
|
| 814 |
+
}
|
| 815 |
+
|
| 816 |
+
constexpr inline float8_e4m3b11fnuz abs(const float8_e4m3b11fnuz& a) {
|
| 817 |
+
return (a.rep() & 0b0'1111'111) == 0
|
| 818 |
+
? float8_e4m3b11fnuz::FromRep(a.rep())
|
| 819 |
+
: float8_e4m3b11fnuz::FromRep(a.rep() & 0b0'1111'111);
|
| 820 |
+
}
|
| 821 |
+
|
| 822 |
+
constexpr inline bool(isnan)(const float8_e4m3b11fnuz& a) {
|
| 823 |
+
return a.rep() == std::numeric_limits<float8_e4m3b11fnuz>::quiet_NaN().rep();
|
| 824 |
+
}
|
| 825 |
+
|
| 826 |
+
constexpr inline float8_e4m3fnuz abs(const float8_e4m3fnuz& a) {
|
| 827 |
+
return (a.rep() & 0x7F) == 0 ? float8_e4m3fnuz::FromRep(a.rep())
|
| 828 |
+
: float8_e4m3fnuz::FromRep(a.rep() & 0x7F);
|
| 829 |
+
}
|
| 830 |
+
|
| 831 |
+
constexpr inline bool(isnan)(const float8_e4m3fnuz& a) {
|
| 832 |
+
return abs(a).rep() ==
|
| 833 |
+
std::numeric_limits<float8_e4m3fnuz>::quiet_NaN().rep();
|
| 834 |
+
}
|
| 835 |
+
|
| 836 |
+
constexpr inline float8_e5m2 abs(const float8_e5m2& a) {
|
| 837 |
+
return float8_e5m2::FromRep(a.rep() & 0b0'11111'11);
|
| 838 |
+
}
|
| 839 |
+
|
| 840 |
+
constexpr inline bool(isnan)(const float8_e5m2& a) {
|
| 841 |
+
return abs(a).rep() > std::numeric_limits<float8_e5m2>::infinity().rep();
|
| 842 |
+
}
|
| 843 |
+
|
| 844 |
+
constexpr inline float8_e5m2fnuz abs(const float8_e5m2fnuz& a) {
|
| 845 |
+
return (a.rep() & 0x7F) == 0 ? float8_e5m2fnuz::FromRep(a.rep())
|
| 846 |
+
: float8_e5m2fnuz::FromRep(a.rep() & 0x7F);
|
| 847 |
+
}
|
| 848 |
+
|
| 849 |
+
constexpr inline bool isnan(const float8_e5m2fnuz& a) {
|
| 850 |
+
return a.rep() == 0x80;
|
| 851 |
+
}
|
| 852 |
+
|
| 853 |
+
template <typename Float8>
|
| 854 |
+
constexpr inline bool(isinf)(const float8_base<Float8>& a) {
|
| 855 |
+
return std::numeric_limits<Float8>::has_infinity
|
| 856 |
+
? abs(a.derived()).rep() ==
|
| 857 |
+
std::numeric_limits<Float8>::infinity().rep()
|
| 858 |
+
: false; // No inf representation.
|
| 859 |
+
}
|
| 860 |
+
|
| 861 |
+
template <typename Float8>
|
| 862 |
+
constexpr inline bool(isfinite)(const float8_base<Float8>& a) {
|
| 863 |
+
return !isnan(a.derived()) && !isinf(a.derived());
|
| 864 |
+
}
|
| 865 |
+
|
| 866 |
+
template <typename Float8>
|
| 867 |
+
std::ostream& operator<<(std::ostream& os, const float8_base<Float8>& f8) {
|
| 868 |
+
os << static_cast<float>(f8.derived());
|
| 869 |
+
return os;
|
| 870 |
+
}
|
| 871 |
+
|
| 872 |
+
//==============================================================================
|
| 873 |
+
// Inline conversion routines between float8 and other types.
|
| 874 |
+
//==============================================================================
|
| 875 |
+
|
| 876 |
+
// Helper for getting a bit representation provided a byte size.
|
| 877 |
+
template <int kNumBytes>
|
| 878 |
+
using GetUnsignedInteger =
|
| 879 |
+
typename Eigen::numext::get_integer_by_size<kNumBytes>::unsigned_type;
|
| 880 |
+
|
| 881 |
+
// Converts between two floating-point types.
|
| 882 |
+
template <typename From, typename To, bool kSaturate, bool kTruncate,
|
| 883 |
+
typename EnableIf = void>
|
| 884 |
+
struct ConvertImpl;
|
| 885 |
+
|
| 886 |
+
// Convert to same type. We need explicit specializations for all combinations
|
| 887 |
+
// of template parameters to avoid ambiguities.
|
| 888 |
+
template <typename Scalar>
|
| 889 |
+
struct IdentityConversion {
|
| 890 |
+
static EIGEN_DEVICE_FUNC inline Scalar run(const Scalar& from) {
|
| 891 |
+
return from;
|
| 892 |
+
}
|
| 893 |
+
};
|
| 894 |
+
|
| 895 |
+
template <typename Scalar>
|
| 896 |
+
struct ConvertImpl<Scalar, Scalar, /*kSaturate=*/false, /*kTruncate=*/false,
|
| 897 |
+
/*EnableIf=*/void> : public IdentityConversion<Scalar> {};
|
| 898 |
+
template <typename Scalar>
|
| 899 |
+
struct ConvertImpl<Scalar, Scalar, /*kSaturate=*/false, /*kTruncate=*/true,
|
| 900 |
+
/*EnableIf=*/void> : public IdentityConversion<Scalar> {};
|
| 901 |
+
template <typename Scalar>
|
| 902 |
+
struct ConvertImpl<Scalar, Scalar, /*kSaturate=*/true, /*kTruncate=*/false,
|
| 903 |
+
/*EnableIf=*/void> : public IdentityConversion<Scalar> {};
|
| 904 |
+
template <typename Scalar>
|
| 905 |
+
struct ConvertImpl<Scalar, Scalar, /*kSaturate=*/true, /*kTruncate=*/true,
|
| 906 |
+
/*EnableIf=*/void> : public IdentityConversion<Scalar> {};
|
| 907 |
+
|
| 908 |
+
template <typename Float>
|
| 909 |
+
struct TraitsBase {
|
| 910 |
+
using BitsType = GetUnsignedInteger<sizeof(Float)>;
|
| 911 |
+
static constexpr int kBits = sizeof(Float) * CHAR_BIT;
|
| 912 |
+
static constexpr int kMantissaBits = Eigen::NumTraits<Float>::digits() - 1;
|
| 913 |
+
static constexpr int kExponentBits = kBits - kMantissaBits - 1;
|
| 914 |
+
static constexpr BitsType kExponentMask = ((BitsType{1} << kExponentBits) - 1)
|
| 915 |
+
<< kMantissaBits;
|
| 916 |
+
static constexpr BitsType kMantissaMask = (BitsType{1} << kMantissaBits) - 1;
|
| 917 |
+
static constexpr int kExponentBias = (1 << (kExponentBits - 1)) - 1;
|
| 918 |
+
};
|
| 919 |
+
|
| 920 |
+
template <typename Float>
|
| 921 |
+
struct Traits : public TraitsBase<Float> {};
|
| 922 |
+
|
| 923 |
+
template <>
|
| 924 |
+
struct Traits<float8_e4m3b11fnuz> : public TraitsBase<float8_e4m3b11fnuz> {
|
| 925 |
+
static constexpr int kExponentBias = 11;
|
| 926 |
+
};
|
| 927 |
+
|
| 928 |
+
template <>
|
| 929 |
+
struct Traits<float8_e4m3fnuz> : public TraitsBase<float8_e4m3fnuz> {
|
| 930 |
+
using Base = TraitsBase<float8_e4m3fnuz>;
|
| 931 |
+
static constexpr int kExponentBias = Base::kExponentBias + 1;
|
| 932 |
+
};
|
| 933 |
+
|
| 934 |
+
template <>
|
| 935 |
+
struct Traits<float8_e5m2fnuz> : public TraitsBase<float8_e5m2fnuz> {
|
| 936 |
+
using Base = TraitsBase<float8_e5m2fnuz>;
|
| 937 |
+
static constexpr int kExponentBias = Base::kExponentBias + 1;
|
| 938 |
+
};
|
| 939 |
+
|
| 940 |
+
template <typename Bits>
|
| 941 |
+
constexpr inline Bits RoundBitsToNearestEven(Bits bits, int roundoff) {
|
| 942 |
+
// Round to nearest even by adding a bias term.
|
| 943 |
+
// Consider a bit pattern
|
| 944 |
+
// FFF...FLRTT...T,
|
| 945 |
+
// where bits RTT...T need to be rounded-off. We add a bias term to the
|
| 946 |
+
// bit pattern s.t. a carry is introduced to round up only if
|
| 947 |
+
// - L is 1, R is 1, OR
|
| 948 |
+
// - L is 0, R is 1, any T is one.
|
| 949 |
+
// We do this by adding L to a bit pattern consisting of all T = 1.
|
| 950 |
+
Bits bias = roundoff == 0
|
| 951 |
+
? 0
|
| 952 |
+
: ((bits >> roundoff) & 1) + (Bits{1} << (roundoff - 1)) - 1;
|
| 953 |
+
return bits + bias;
|
| 954 |
+
}
|
| 955 |
+
|
| 956 |
+
#if (defined(__cpp_lib_bitops) && __cpp_lib_bitops >= 201907L)
|
| 957 |
+
using std::countl_zero;
|
| 958 |
+
#else
|
| 959 |
+
static constexpr inline int countl_zero(uint64_t x) {
|
| 960 |
+
int zeroes = 60;
|
| 961 |
+
if (x >> 32) {
|
| 962 |
+
zeroes -= 32;
|
| 963 |
+
x >>= 32;
|
| 964 |
+
}
|
| 965 |
+
if (x >> 16) {
|
| 966 |
+
zeroes -= 16;
|
| 967 |
+
x >>= 16;
|
| 968 |
+
}
|
| 969 |
+
if (x >> 8) {
|
| 970 |
+
zeroes -= 8;
|
| 971 |
+
x >>= 8;
|
| 972 |
+
}
|
| 973 |
+
if (x >> 4) {
|
| 974 |
+
zeroes -= 4;
|
| 975 |
+
x >>= 4;
|
| 976 |
+
}
|
| 977 |
+
return "\4\3\2\2\1\1\1\1\0\0\0\0\0\0\0"[x] + zeroes;
|
| 978 |
+
}
|
| 979 |
+
static constexpr inline int countl_zero(uint32_t x) {
|
| 980 |
+
int zeroes = 28;
|
| 981 |
+
if (x >> 16) {
|
| 982 |
+
zeroes -= 16;
|
| 983 |
+
x >>= 16;
|
| 984 |
+
}
|
| 985 |
+
if (x >> 8) {
|
| 986 |
+
zeroes -= 8;
|
| 987 |
+
x >>= 8;
|
| 988 |
+
}
|
| 989 |
+
if (x >> 4) {
|
| 990 |
+
zeroes -= 4;
|
| 991 |
+
x >>= 4;
|
| 992 |
+
}
|
| 993 |
+
return "\4\3\2\2\1\1\1\1\0\0\0\0\0\0\0"[x] + zeroes;
|
| 994 |
+
}
|
| 995 |
+
static constexpr inline int countl_zero(uint16_t x) {
|
| 996 |
+
int zeroes = 12;
|
| 997 |
+
if (x >> 8) {
|
| 998 |
+
zeroes -= 8;
|
| 999 |
+
x >>= 8;
|
| 1000 |
+
}
|
| 1001 |
+
if (x >> 4) {
|
| 1002 |
+
zeroes -= 4;
|
| 1003 |
+
x >>= 4;
|
| 1004 |
+
}
|
| 1005 |
+
return "\4\3\2\2\1\1\1\1\0\0\0\0\0\0\0"[x] + zeroes;
|
| 1006 |
+
}
|
| 1007 |
+
static constexpr inline int countl_zero(uint8_t x) {
|
| 1008 |
+
int zeroes = 4;
|
| 1009 |
+
if (x >> 4) {
|
| 1010 |
+
zeroes -= 4;
|
| 1011 |
+
x >>= 4;
|
| 1012 |
+
}
|
| 1013 |
+
return "\4\3\2\2\1\1\1\1\0\0\0\0\0\0\0"[x] + zeroes;
|
| 1014 |
+
}
|
| 1015 |
+
#endif
|
| 1016 |
+
|
| 1017 |
+
template <typename From, typename To, bool kSaturate, bool kTruncate>
|
| 1018 |
+
struct ConvertImpl<From, To, kSaturate, kTruncate,
|
| 1019 |
+
std::enable_if_t<!std::is_same_v<From, To>>> {
|
| 1020 |
+
using FromTraits = Traits<From>;
|
| 1021 |
+
using FromBits = typename FromTraits::BitsType;
|
| 1022 |
+
static constexpr int kFromBits = FromTraits::kBits;
|
| 1023 |
+
static constexpr int kFromMantissaBits = FromTraits::kMantissaBits;
|
| 1024 |
+
static constexpr int kFromExponentBits = FromTraits::kExponentBits;
|
| 1025 |
+
static constexpr int kFromExponentBias = FromTraits::kExponentBias;
|
| 1026 |
+
static constexpr FromBits kFromExponentMask = FromTraits::kExponentMask;
|
| 1027 |
+
|
| 1028 |
+
using ToTraits = Traits<To>;
|
| 1029 |
+
using ToBits = typename ToTraits::BitsType;
|
| 1030 |
+
static constexpr int kToBits = ToTraits::kBits;
|
| 1031 |
+
static constexpr int kToMantissaBits = ToTraits::kMantissaBits;
|
| 1032 |
+
static constexpr int kToExponentBits = ToTraits::kExponentBits;
|
| 1033 |
+
static constexpr int kToExponentBias = ToTraits::kExponentBias;
|
| 1034 |
+
static constexpr ToBits kToExponentMask = ToTraits::kExponentMask;
|
| 1035 |
+
|
| 1036 |
+
// `WideBits` is wide enough to accommodate the largest exponent and mantissa
|
| 1037 |
+
// in either `From` or `To`.
|
| 1038 |
+
static constexpr int kWideBits =
|
| 1039 |
+
(std::max(kToMantissaBits, kFromMantissaBits)) + // Max significand.
|
| 1040 |
+
(std::max(kToExponentBits, kFromExponentBits)); // Max exponent.
|
| 1041 |
+
static constexpr int kWideBytes = (kWideBits + (CHAR_BIT - 1)) / CHAR_BIT;
|
| 1042 |
+
using WideBits = GetUnsignedInteger<kWideBytes>;
|
| 1043 |
+
static constexpr int kExponentOffset = kToExponentBias - kFromExponentBias;
|
| 1044 |
+
static constexpr int kDigitShift = kToMantissaBits - kFromMantissaBits;
|
| 1045 |
+
|
| 1046 |
+
static EIGEN_DEVICE_FUNC inline To run(const From& from) {
|
| 1047 |
+
// Shift bits to destination type, without sign bit.
|
| 1048 |
+
const bool from_sign_bit =
|
| 1049 |
+
Eigen::numext::bit_cast<FromBits>(from) >> (kFromBits - 1);
|
| 1050 |
+
const FromBits from_bits =
|
| 1051 |
+
Eigen::numext::bit_cast<FromBits>(Eigen::numext::abs(from));
|
| 1052 |
+
|
| 1053 |
+
// Special values, preserving sign.
|
| 1054 |
+
if (Eigen::numext::isinf(from)) {
|
| 1055 |
+
return from_sign_bit ? -Eigen::NumTraits<To>::infinity()
|
| 1056 |
+
: Eigen::NumTraits<To>::infinity();
|
| 1057 |
+
}
|
| 1058 |
+
if (Eigen::numext::isnan(from)) {
|
| 1059 |
+
return from_sign_bit ? -Eigen::NumTraits<To>::quiet_NaN()
|
| 1060 |
+
: Eigen::NumTraits<To>::quiet_NaN();
|
| 1061 |
+
}
|
| 1062 |
+
if (from_bits == 0) {
|
| 1063 |
+
return from_sign_bit ? -To{} : To{};
|
| 1064 |
+
}
|
| 1065 |
+
|
| 1066 |
+
const int biased_from_exponent = from_bits >> kFromMantissaBits;
|
| 1067 |
+
|
| 1068 |
+
// `To` supports more exponents near zero which means that some subnormal
|
| 1069 |
+
// values in `From` may become normal.
|
| 1070 |
+
if constexpr (std::numeric_limits<To>::min_exponent <
|
| 1071 |
+
std::numeric_limits<From>::min_exponent) {
|
| 1072 |
+
if (biased_from_exponent == 0) {
|
| 1073 |
+
// Subnormals.
|
| 1074 |
+
WideBits bits = from_bits;
|
| 1075 |
+
|
| 1076 |
+
// Determine exponent in target type.
|
| 1077 |
+
const int normalization_factor =
|
| 1078 |
+
countl_zero(from_bits) - (kFromBits - kFromMantissaBits) + 1;
|
| 1079 |
+
const int biased_exponent = kExponentOffset - normalization_factor + 1;
|
| 1080 |
+
if (biased_exponent <= 0) {
|
| 1081 |
+
// Result is subnormal. Adjust the subnormal bits to account for
|
| 1082 |
+
// the difference in exponent bias.
|
| 1083 |
+
if constexpr (kExponentOffset < sizeof(WideBits) * CHAR_BIT) {
|
| 1084 |
+
bits <<= kExponentOffset;
|
| 1085 |
+
}
|
| 1086 |
+
} else {
|
| 1087 |
+
// Result is normal. Shift the mantissa to account for the number of
|
| 1088 |
+
// leading zero digits, and clear the hidden bit.
|
| 1089 |
+
bits <<= normalization_factor;
|
| 1090 |
+
bits &= ~(WideBits{1} << kFromMantissaBits);
|
| 1091 |
+
// Insert the exponent bits.
|
| 1092 |
+
bits |= static_cast<WideBits>(biased_exponent) << kFromMantissaBits;
|
| 1093 |
+
}
|
| 1094 |
+
|
| 1095 |
+
// Truncate/round mantissa if necessary.
|
| 1096 |
+
if constexpr (kDigitShift > 0) {
|
| 1097 |
+
bits <<= kDigitShift;
|
| 1098 |
+
} else {
|
| 1099 |
+
if constexpr (!kTruncate) {
|
| 1100 |
+
bits = RoundBitsToNearestEven(bits, -kDigitShift);
|
| 1101 |
+
}
|
| 1102 |
+
bits >>= -kDigitShift;
|
| 1103 |
+
}
|
| 1104 |
+
To to = Eigen::numext::bit_cast<To>(static_cast<ToBits>(bits));
|
| 1105 |
+
return from_sign_bit ? -to : to;
|
| 1106 |
+
}
|
| 1107 |
+
}
|
| 1108 |
+
// `To` supports fewer exponents near zero which means that some values in
|
| 1109 |
+
// `From` may become subnormal.
|
| 1110 |
+
if constexpr (std::numeric_limits<To>::min_exponent >
|
| 1111 |
+
std::numeric_limits<From>::min_exponent) {
|
| 1112 |
+
const int unbiased_exponent = biased_from_exponent - kFromExponentBias;
|
| 1113 |
+
const int biased_to_exponent = unbiased_exponent + kToExponentBias;
|
| 1114 |
+
// Subnormals and zero.
|
| 1115 |
+
if (biased_to_exponent <= 0) {
|
| 1116 |
+
// Round and shift mantissa down.
|
| 1117 |
+
FromBits from_has_leading_one = (biased_from_exponent > 0 ? 1 : 0);
|
| 1118 |
+
int exponent_shift =
|
| 1119 |
+
-kDigitShift - biased_to_exponent + from_has_leading_one;
|
| 1120 |
+
// Insert the implicit leading 1 bit on the mantissa for normalized
|
| 1121 |
+
// inputs.
|
| 1122 |
+
FromBits rounded_from_bits =
|
| 1123 |
+
(from_bits & FromTraits::kMantissaMask) |
|
| 1124 |
+
(from_has_leading_one << kFromMantissaBits);
|
| 1125 |
+
ToBits bits = 0;
|
| 1126 |
+
// To avoid UB, limit rounding and shifting to the full mantissa plus
|
| 1127 |
+
// leading 1.
|
| 1128 |
+
if (exponent_shift <= kFromMantissaBits + 1) {
|
| 1129 |
+
if constexpr (!kTruncate) {
|
| 1130 |
+
// NOTE: we need to round again from the original from_bits,
|
| 1131 |
+
// otherwise the lower precision bits may already be lost. There is
|
| 1132 |
+
// an edge-case where rounding to a normalized value would normally
|
| 1133 |
+
// round down, but for a subnormal, we need to round up.
|
| 1134 |
+
rounded_from_bits =
|
| 1135 |
+
RoundBitsToNearestEven(rounded_from_bits, exponent_shift);
|
| 1136 |
+
}
|
| 1137 |
+
bits = (rounded_from_bits >> exponent_shift);
|
| 1138 |
+
}
|
| 1139 |
+
// Insert sign and return.
|
| 1140 |
+
To to = Eigen::numext::bit_cast<To>(bits);
|
| 1141 |
+
return from_sign_bit ? -to : to;
|
| 1142 |
+
}
|
| 1143 |
+
}
|
| 1144 |
+
|
| 1145 |
+
// Round the mantissa if it is shrinking.
|
| 1146 |
+
WideBits rounded_from_bits = from_bits;
|
| 1147 |
+
if constexpr (kDigitShift < 0) {
|
| 1148 |
+
if constexpr (!kTruncate) {
|
| 1149 |
+
rounded_from_bits = RoundBitsToNearestEven(from_bits, -kDigitShift);
|
| 1150 |
+
}
|
| 1151 |
+
// Zero-out tail bits.
|
| 1152 |
+
rounded_from_bits &= ~((WideBits{1} << (-kDigitShift)) - 1);
|
| 1153 |
+
}
|
| 1154 |
+
|
| 1155 |
+
// Re-bias the exponent.
|
| 1156 |
+
rounded_from_bits += static_cast<WideBits>(kExponentOffset)
|
| 1157 |
+
<< kFromMantissaBits;
|
| 1158 |
+
|
| 1159 |
+
ToBits bits;
|
| 1160 |
+
// Check for overflows by aligning the significands. We always align the
|
| 1161 |
+
// narrower significand to the wider significand.
|
| 1162 |
+
const WideBits kToHighestRep =
|
| 1163 |
+
Eigen::numext::bit_cast<ToBits>(Eigen::NumTraits<To>::highest());
|
| 1164 |
+
WideBits aligned_highest{kToHighestRep};
|
| 1165 |
+
if constexpr (kDigitShift < 0) {
|
| 1166 |
+
aligned_highest <<= -kDigitShift;
|
| 1167 |
+
// Shift down, all dropped bits should already be zero.
|
| 1168 |
+
bits = static_cast<ToBits>(rounded_from_bits >> -kDigitShift);
|
| 1169 |
+
} else if constexpr (kDigitShift >= 0) {
|
| 1170 |
+
// Shift up, inserting zeros in the newly created digits.
|
| 1171 |
+
rounded_from_bits <<= kDigitShift;
|
| 1172 |
+
bits = ToBits{rounded_from_bits};
|
| 1173 |
+
}
|
| 1174 |
+
|
| 1175 |
+
To to = Eigen::numext::bit_cast<To>(bits);
|
| 1176 |
+
// `From` supports larger values than `To`, we may overflow.
|
| 1177 |
+
if constexpr (std::make_pair(std::numeric_limits<To>::max_exponent,
|
| 1178 |
+
std::numeric_limits<To>::digits) <
|
| 1179 |
+
std::make_pair(std::numeric_limits<From>::max_exponent,
|
| 1180 |
+
std::numeric_limits<From>::digits)) {
|
| 1181 |
+
if (rounded_from_bits > aligned_highest) {
|
| 1182 |
+
// Overflowed values map to highest or infinity depending on kSaturate.
|
| 1183 |
+
to = kSaturate ? Eigen::NumTraits<To>::highest()
|
| 1184 |
+
: Eigen::NumTraits<To>::infinity();
|
| 1185 |
+
}
|
| 1186 |
+
}
|
| 1187 |
+
// Insert sign bit.
|
| 1188 |
+
return from_sign_bit ? -to : to;
|
| 1189 |
+
}
|
| 1190 |
+
};
|
| 1191 |
+
|
| 1192 |
+
// Saturation has no impact when casting e4m3 to e5m2.
|
| 1193 |
+
template <bool kTruncate>
|
| 1194 |
+
struct ConvertImpl<float8_e4m3fn, float8_e5m2, true, kTruncate> {
|
| 1195 |
+
static EIGEN_DEVICE_FUNC inline float8_e5m2 run(const float8_e4m3fn& from) {
|
| 1196 |
+
return ConvertImpl<float8_e4m3fn, float8_e5m2, false, kTruncate>::run(from);
|
| 1197 |
+
}
|
| 1198 |
+
};
|
| 1199 |
+
|
| 1200 |
+
template <bool kSaturate, bool kTruncate>
|
| 1201 |
+
struct ConvertImpl<Eigen::half, float8_e5m2, kSaturate, kTruncate> {
|
| 1202 |
+
static EIGEN_DEVICE_FUNC inline float8_e5m2 run(const Eigen::half& from) {
|
| 1203 |
+
uint16_t from_bits = Eigen::numext::bit_cast<uint16_t>(from);
|
| 1204 |
+
|
| 1205 |
+
// Special values (Inf or NaN).
|
| 1206 |
+
uint16_t abs_bits = from_bits & 0x7FFF;
|
| 1207 |
+
if (abs_bits == 0x7C00) {
|
| 1208 |
+
return float8_e5m2::FromRep(from_bits >> 8);
|
| 1209 |
+
} else if (abs_bits > 0x7C00) {
|
| 1210 |
+
// IEEE 754-2019 6.2.1: "A quiet NaN bit string should be encoded with the
|
| 1211 |
+
// first bit (d1) of the trailing significand field T being 1."
|
| 1212 |
+
// IEEE 754-2019 6.2.3: "Conversion of a quiet NaN to a floating-point
|
| 1213 |
+
// format of the same or a different radix that does not allow the payload
|
| 1214 |
+
// to be preserved, shall return a quiet NaN [...]"
|
| 1215 |
+
return float8_e5m2::FromRep((from_bits >> 8) | 0b0'00000'10);
|
| 1216 |
+
}
|
| 1217 |
+
|
| 1218 |
+
if constexpr (!kTruncate) {
|
| 1219 |
+
from_bits = RoundBitsToNearestEven(from_bits, 8);
|
| 1220 |
+
// Rounding can cause an overflow to infinity. Clamp to the largest finite
|
| 1221 |
+
// value if saturation is requested.
|
| 1222 |
+
if constexpr (kSaturate) {
|
| 1223 |
+
const float8_e5m2 kHighest = Eigen::NumTraits<float8_e5m2>::highest();
|
| 1224 |
+
if ((from_bits & 0x7F00) > static_cast<uint16_t>(kHighest.rep()) << 8) {
|
| 1225 |
+
const bool from_sign_bit = from_bits >> 15;
|
| 1226 |
+
return from_sign_bit ? -kHighest : kHighest;
|
| 1227 |
+
}
|
| 1228 |
+
}
|
| 1229 |
+
}
|
| 1230 |
+
return float8_e5m2::FromRep(from_bits >> 8);
|
| 1231 |
+
}
|
| 1232 |
+
};
|
| 1233 |
+
|
| 1234 |
+
template <>
|
| 1235 |
+
struct ConvertImpl<float8_e5m2, Eigen::half, /*kSaturate=*/false,
|
| 1236 |
+
/*kTruncate=*/false> {
|
| 1237 |
+
static EIGEN_DEVICE_FUNC inline Eigen::half run(const float8_e5m2& from) {
|
| 1238 |
+
return Eigen::numext::bit_cast<Eigen::half>(
|
| 1239 |
+
static_cast<uint16_t>(static_cast<uint16_t>(from.rep()) << 8));
|
| 1240 |
+
}
|
| 1241 |
+
};
|
| 1242 |
+
|
| 1243 |
+
// Direct casts of e5m2 to Eigen::half simply shifts bits over.
|
| 1244 |
+
template <bool kSaturate, bool kTruncate>
|
| 1245 |
+
struct ConvertImpl<float8_e5m2, Eigen::half, kSaturate, kTruncate> {
|
| 1246 |
+
static EIGEN_DEVICE_FUNC inline Eigen::half run(const float8_e5m2& from) {
|
| 1247 |
+
return Eigen::numext::bit_cast<Eigen::half>(
|
| 1248 |
+
static_cast<uint16_t>(static_cast<uint16_t>(from.rep()) << 8));
|
| 1249 |
+
}
|
| 1250 |
+
};
|
| 1251 |
+
|
| 1252 |
+
template <typename Derived>
|
| 1253 |
+
template <bool kSaturate, bool kTruncate, typename From>
|
| 1254 |
+
EIGEN_DEVICE_FUNC Derived float8_base<Derived>::ConvertFrom(const From& from) {
|
| 1255 |
+
return ConvertImpl<From, Derived, kSaturate, kTruncate>::run(from);
|
| 1256 |
+
}
|
| 1257 |
+
|
| 1258 |
+
template <typename Derived>
|
| 1259 |
+
template <typename To, bool kSaturate, bool kTruncate>
|
| 1260 |
+
EIGEN_DEVICE_FUNC To float8_base<Derived>::ConvertTo(const Derived& from) {
|
| 1261 |
+
return ConvertImpl<Derived, To, kSaturate, kTruncate>::run(from);
|
| 1262 |
+
}
|
| 1263 |
+
|
| 1264 |
+
} // namespace float8_internal
|
| 1265 |
+
|
| 1266 |
+
// Exported types.
|
| 1267 |
+
using float8_e4m3fn = float8_internal::float8_e4m3fn;
|
| 1268 |
+
using float8_e4m3fnuz = float8_internal::float8_e4m3fnuz;
|
| 1269 |
+
using float8_e4m3b11fnuz = float8_internal::float8_e4m3b11fnuz;
|
| 1270 |
+
using float8_e5m2 = float8_internal::float8_e5m2;
|
| 1271 |
+
using float8_e5m2fnuz = float8_internal::float8_e5m2fnuz;
|
| 1272 |
+
|
| 1273 |
+
} // namespace ml_dtypes
|
| 1274 |
+
|
| 1275 |
+
// Eigen-specific overrides.
|
| 1276 |
+
namespace Eigen {
|
| 1277 |
+
namespace numext {
|
| 1278 |
+
|
| 1279 |
+
template <>
|
| 1280 |
+
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC ml_dtypes::float8_e4m3fn
|
| 1281 |
+
bit_cast<ml_dtypes::float8_e4m3fn, uint8_t>(const uint8_t& src) {
|
| 1282 |
+
return ml_dtypes::float8_e4m3fn::FromRep(src);
|
| 1283 |
+
}
|
| 1284 |
+
|
| 1285 |
+
template <>
|
| 1286 |
+
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC uint8_t
|
| 1287 |
+
bit_cast<uint8_t, ml_dtypes::float8_e4m3fn>(
|
| 1288 |
+
const ml_dtypes::float8_e4m3fn& src) {
|
| 1289 |
+
return src.rep();
|
| 1290 |
+
}
|
| 1291 |
+
|
| 1292 |
+
template <>
|
| 1293 |
+
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC ml_dtypes::float8_e5m2
|
| 1294 |
+
bit_cast<ml_dtypes::float8_e5m2, uint8_t>(const uint8_t& src) {
|
| 1295 |
+
return ml_dtypes::float8_e5m2::FromRep(src);
|
| 1296 |
+
}
|
| 1297 |
+
|
| 1298 |
+
template <>
|
| 1299 |
+
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC uint8_t
|
| 1300 |
+
bit_cast<uint8_t, ml_dtypes::float8_e5m2>(const ml_dtypes::float8_e5m2& src) {
|
| 1301 |
+
return src.rep();
|
| 1302 |
+
}
|
| 1303 |
+
|
| 1304 |
+
} // namespace numext
|
| 1305 |
+
|
| 1306 |
+
// Work-around for isinf/isnan/isfinite issue on aarch64.
|
| 1307 |
+
namespace internal {
|
| 1308 |
+
template <>
|
| 1309 |
+
EIGEN_DEVICE_FUNC inline bool isinf_impl<ml_dtypes::float8_e4m3fn>(
|
| 1310 |
+
const ml_dtypes::float8_e4m3fn& x) {
|
| 1311 |
+
return ml_dtypes::float8_internal::isinf(x);
|
| 1312 |
+
}
|
| 1313 |
+
|
| 1314 |
+
template <>
|
| 1315 |
+
EIGEN_DEVICE_FUNC inline bool isinf_impl<ml_dtypes::float8_e4m3b11fnuz>(
|
| 1316 |
+
const ml_dtypes::float8_e4m3b11fnuz& x) {
|
| 1317 |
+
return ml_dtypes::float8_internal::isinf(x);
|
| 1318 |
+
}
|
| 1319 |
+
|
| 1320 |
+
template <>
|
| 1321 |
+
EIGEN_DEVICE_FUNC inline bool isinf_impl<ml_dtypes::float8_e4m3fnuz>(
|
| 1322 |
+
const ml_dtypes::float8_e4m3fnuz& x) {
|
| 1323 |
+
return ml_dtypes::float8_internal::isinf(x);
|
| 1324 |
+
}
|
| 1325 |
+
|
| 1326 |
+
template <>
|
| 1327 |
+
EIGEN_DEVICE_FUNC inline bool isinf_impl<ml_dtypes::float8_e5m2>(
|
| 1328 |
+
const ml_dtypes::float8_e5m2& x) {
|
| 1329 |
+
return ml_dtypes::float8_internal::isinf(x);
|
| 1330 |
+
}
|
| 1331 |
+
|
| 1332 |
+
template <>
|
| 1333 |
+
EIGEN_DEVICE_FUNC inline bool isinf_impl<ml_dtypes::float8_e5m2fnuz>(
|
| 1334 |
+
const ml_dtypes::float8_e5m2fnuz& x) {
|
| 1335 |
+
return ml_dtypes::float8_internal::isinf(x);
|
| 1336 |
+
}
|
| 1337 |
+
|
| 1338 |
+
template <>
|
| 1339 |
+
EIGEN_DEVICE_FUNC inline bool isnan_impl<ml_dtypes::float8_e4m3fn>(
|
| 1340 |
+
const ml_dtypes::float8_e4m3fn& x) {
|
| 1341 |
+
return ml_dtypes::float8_internal::isnan(x);
|
| 1342 |
+
}
|
| 1343 |
+
|
| 1344 |
+
template <>
|
| 1345 |
+
EIGEN_DEVICE_FUNC inline bool isnan_impl<ml_dtypes::float8_e4m3b11fnuz>(
|
| 1346 |
+
const ml_dtypes::float8_e4m3b11fnuz& x) {
|
| 1347 |
+
return ml_dtypes::float8_internal::isnan(x);
|
| 1348 |
+
}
|
| 1349 |
+
|
| 1350 |
+
template <>
|
| 1351 |
+
EIGEN_DEVICE_FUNC inline bool isnan_impl<ml_dtypes::float8_e4m3fnuz>(
|
| 1352 |
+
const ml_dtypes::float8_e4m3fnuz& x) {
|
| 1353 |
+
return ml_dtypes::float8_internal::isnan(x);
|
| 1354 |
+
}
|
| 1355 |
+
|
| 1356 |
+
template <>
|
| 1357 |
+
EIGEN_DEVICE_FUNC inline bool isnan_impl<ml_dtypes::float8_e5m2>(
|
| 1358 |
+
const ml_dtypes::float8_e5m2& x) {
|
| 1359 |
+
return ml_dtypes::float8_internal::isnan(x);
|
| 1360 |
+
}
|
| 1361 |
+
|
| 1362 |
+
template <>
|
| 1363 |
+
EIGEN_DEVICE_FUNC inline bool isnan_impl<ml_dtypes::float8_e5m2fnuz>(
|
| 1364 |
+
const ml_dtypes::float8_e5m2fnuz& x) {
|
| 1365 |
+
return ml_dtypes::float8_internal::isnan(x);
|
| 1366 |
+
}
|
| 1367 |
+
|
| 1368 |
+
template <>
|
| 1369 |
+
EIGEN_DEVICE_FUNC inline bool isfinite_impl<ml_dtypes::float8_e4m3fn>(
|
| 1370 |
+
const ml_dtypes::float8_e4m3fn& x) {
|
| 1371 |
+
return ml_dtypes::float8_internal::isfinite(x);
|
| 1372 |
+
}
|
| 1373 |
+
|
| 1374 |
+
template <>
|
| 1375 |
+
EIGEN_DEVICE_FUNC inline bool isfinite_impl<ml_dtypes::float8_e4m3b11fnuz>(
|
| 1376 |
+
const ml_dtypes::float8_e4m3b11fnuz& x) {
|
| 1377 |
+
return ml_dtypes::float8_internal::isfinite(x);
|
| 1378 |
+
}
|
| 1379 |
+
|
| 1380 |
+
template <>
|
| 1381 |
+
EIGEN_DEVICE_FUNC inline bool isfinite_impl<ml_dtypes::float8_e4m3fnuz>(
|
| 1382 |
+
const ml_dtypes::float8_e4m3fnuz& x) {
|
| 1383 |
+
return ml_dtypes::float8_internal::isfinite(x);
|
| 1384 |
+
}
|
| 1385 |
+
|
| 1386 |
+
template <>
|
| 1387 |
+
EIGEN_DEVICE_FUNC inline bool isfinite_impl<ml_dtypes::float8_e5m2>(
|
| 1388 |
+
const ml_dtypes::float8_e5m2& x) {
|
| 1389 |
+
return ml_dtypes::float8_internal::isfinite(x);
|
| 1390 |
+
}
|
| 1391 |
+
|
| 1392 |
+
template <>
|
| 1393 |
+
EIGEN_DEVICE_FUNC inline bool isfinite_impl<ml_dtypes::float8_e5m2fnuz>(
|
| 1394 |
+
const ml_dtypes::float8_e5m2fnuz& x) {
|
| 1395 |
+
return ml_dtypes::float8_internal::isfinite(x);
|
| 1396 |
+
}
|
| 1397 |
+
|
| 1398 |
+
} // namespace internal
|
| 1399 |
+
} // namespace Eigen
|
| 1400 |
+
|
| 1401 |
+
#endif // ML_DTYPES_FLOAT8_H_
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/include/external/ml_dtypes/_virtual_includes/int4/ml_dtypes/include/int4.h
ADDED
|
@@ -0,0 +1,262 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2023 The ml_dtypes Authors
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
|
| 16 |
+
#ifndef ML_DTYPES_INT4_H_
|
| 17 |
+
#define ML_DTYPES_INT4_H_
|
| 18 |
+
|
| 19 |
+
#include <cstdint>
|
| 20 |
+
#include <limits>
|
| 21 |
+
#include <optional>
|
| 22 |
+
#include <ostream>
|
| 23 |
+
#include <sstream>
|
| 24 |
+
#include <string>
|
| 25 |
+
|
| 26 |
+
namespace ml_dtypes {
|
| 27 |
+
|
| 28 |
+
template <typename UnderlyingTy>
|
| 29 |
+
struct i4 {
|
| 30 |
+
private:
|
| 31 |
+
UnderlyingTy v : 4;
|
| 32 |
+
|
| 33 |
+
public:
|
| 34 |
+
constexpr i4() : v(0) {}
|
| 35 |
+
constexpr i4(const i4& other) = default;
|
| 36 |
+
constexpr i4(i4&& other) = default;
|
| 37 |
+
constexpr i4& operator=(const i4& other) = default;
|
| 38 |
+
constexpr i4& operator=(i4&&) = default;
|
| 39 |
+
|
| 40 |
+
explicit constexpr i4(UnderlyingTy val) : v(val & 0x0F) {}
|
| 41 |
+
template <typename T>
|
| 42 |
+
explicit constexpr i4(T t) : i4(static_cast<UnderlyingTy>(t)) {}
|
| 43 |
+
|
| 44 |
+
static constexpr i4 lowest() {
|
| 45 |
+
return std::is_signed<UnderlyingTy>::value ? i4(-8) : i4(0);
|
| 46 |
+
}
|
| 47 |
+
static constexpr i4 highest() {
|
| 48 |
+
return std::is_signed<UnderlyingTy>::value ? i4(7) : i4(15);
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
template <typename T>
|
| 52 |
+
explicit constexpr operator T() const {
|
| 53 |
+
return static_cast<T>(v);
|
| 54 |
+
}
|
| 55 |
+
// NOLINTNEXTLINE(google-explicit-constructor)
|
| 56 |
+
constexpr operator std::optional<int64_t>() const {
|
| 57 |
+
return static_cast<int64_t>(v);
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
constexpr i4 operator-() const { return i4(-v); }
|
| 61 |
+
constexpr i4 operator+(const i4& other) const { return i4((v + other.v)); }
|
| 62 |
+
constexpr i4 operator-(const i4& other) const { return i4((v - other.v)); }
|
| 63 |
+
constexpr i4 operator*(const i4& other) const { return i4((v * other.v)); }
|
| 64 |
+
constexpr i4 operator/(const i4& other) const { return i4((v / other.v)); }
|
| 65 |
+
constexpr i4 operator%(const i4& other) const { return i4((v % other.v)); }
|
| 66 |
+
|
| 67 |
+
constexpr i4 operator&(const i4& other) const { return i4((v & other.v)); }
|
| 68 |
+
constexpr i4 operator|(const i4& other) const { return i4((v | other.v)); }
|
| 69 |
+
constexpr i4 operator^(const i4& other) const { return i4((v ^ other.v)); }
|
| 70 |
+
constexpr i4 operator~() const { return i4(~v); }
|
| 71 |
+
constexpr i4 operator>>(int amount) const { return i4((v >> amount)); }
|
| 72 |
+
constexpr i4 operator<<(int amount) const { return i4((v << amount)); }
|
| 73 |
+
|
| 74 |
+
constexpr bool operator==(const i4& other) const { return v == other.v; }
|
| 75 |
+
constexpr bool operator!=(const i4& other) const { return v != other.v; }
|
| 76 |
+
constexpr bool operator<(const i4& other) const { return v < other.v; }
|
| 77 |
+
constexpr bool operator>(const i4& other) const { return v > other.v; }
|
| 78 |
+
constexpr bool operator<=(const i4& other) const { return v <= other.v; }
|
| 79 |
+
constexpr bool operator>=(const i4& other) const { return v >= other.v; }
|
| 80 |
+
|
| 81 |
+
constexpr bool operator==(int64_t other) const { return v == other; }
|
| 82 |
+
constexpr bool operator!=(int64_t other) const { return v != other; }
|
| 83 |
+
constexpr bool operator<(int64_t other) const { return v < other; }
|
| 84 |
+
constexpr bool operator>(int64_t other) const { return v > other; }
|
| 85 |
+
constexpr bool operator<=(int64_t other) const { return v <= other; }
|
| 86 |
+
constexpr bool operator>=(int64_t other) const { return v >= other; }
|
| 87 |
+
|
| 88 |
+
friend constexpr bool operator==(int64_t a, const i4& b) { return a == b.v; }
|
| 89 |
+
friend constexpr bool operator!=(int64_t a, const i4& b) { return a != b.v; }
|
| 90 |
+
friend constexpr bool operator<(int64_t a, const i4& b) { return a < b.v; }
|
| 91 |
+
friend constexpr bool operator>(int64_t a, const i4& b) { return a > b.v; }
|
| 92 |
+
friend constexpr bool operator<=(int64_t a, const i4& b) { return a <= b.v; }
|
| 93 |
+
friend constexpr bool operator>=(int64_t a, const i4& b) { return a >= b.v; }
|
| 94 |
+
|
| 95 |
+
constexpr i4& operator++() {
|
| 96 |
+
v = (v + 1) & 0x0F;
|
| 97 |
+
return *this;
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
constexpr i4 operator++(int) {
|
| 101 |
+
i4 orig = *this;
|
| 102 |
+
this->operator++();
|
| 103 |
+
return orig;
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
constexpr i4& operator--() {
|
| 107 |
+
v = (v - 1) & 0x0F;
|
| 108 |
+
return *this;
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
constexpr i4 operator--(int) {
|
| 112 |
+
i4 orig = *this;
|
| 113 |
+
this->operator--();
|
| 114 |
+
return orig;
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
constexpr i4& operator+=(const i4& other) {
|
| 118 |
+
*this = *this + other;
|
| 119 |
+
return *this;
|
| 120 |
+
}
|
| 121 |
+
constexpr i4& operator-=(const i4& other) {
|
| 122 |
+
*this = *this - other;
|
| 123 |
+
return *this;
|
| 124 |
+
}
|
| 125 |
+
constexpr i4& operator*=(const i4& other) {
|
| 126 |
+
*this = *this * other;
|
| 127 |
+
return *this;
|
| 128 |
+
}
|
| 129 |
+
constexpr i4& operator/=(const i4& other) {
|
| 130 |
+
*this = *this / other;
|
| 131 |
+
return *this;
|
| 132 |
+
}
|
| 133 |
+
constexpr i4& operator%=(const i4& other) {
|
| 134 |
+
*this = *this % other;
|
| 135 |
+
return *this;
|
| 136 |
+
}
|
| 137 |
+
constexpr i4& operator&=(const i4& other) {
|
| 138 |
+
*this = *this & other;
|
| 139 |
+
return *this;
|
| 140 |
+
}
|
| 141 |
+
constexpr i4& operator|=(const i4& other) {
|
| 142 |
+
*this = *this | other;
|
| 143 |
+
return *this;
|
| 144 |
+
}
|
| 145 |
+
constexpr i4& operator^=(const i4& other) {
|
| 146 |
+
*this = *this ^ other;
|
| 147 |
+
return *this;
|
| 148 |
+
}
|
| 149 |
+
constexpr i4& operator>>=(int amount) {
|
| 150 |
+
*this = *this >> amount;
|
| 151 |
+
return *this;
|
| 152 |
+
}
|
| 153 |
+
constexpr i4& operator<<=(int amount) {
|
| 154 |
+
*this = *this << amount;
|
| 155 |
+
return *this;
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
friend ::std::ostream& operator<<(::std::ostream& os, const i4& num) {
|
| 159 |
+
os << static_cast<int16_t>(num.v);
|
| 160 |
+
return os;
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
std::string ToString() const {
|
| 164 |
+
std::ostringstream os;
|
| 165 |
+
os << static_cast<int16_t>(v);
|
| 166 |
+
return os.str();
|
| 167 |
+
}
|
| 168 |
+
};
|
| 169 |
+
|
| 170 |
+
using int4 = i4<int8_t>;
|
| 171 |
+
using uint4 = i4<uint8_t>;
|
| 172 |
+
|
| 173 |
+
namespace internal {
|
| 174 |
+
|
| 175 |
+
struct int4_numeric_limits_base {
|
| 176 |
+
static inline constexpr const bool is_specialized = true;
|
| 177 |
+
static inline constexpr const bool is_integer = true;
|
| 178 |
+
static inline constexpr const bool is_exact = true;
|
| 179 |
+
static inline constexpr const bool has_infinity = false;
|
| 180 |
+
static inline constexpr const bool has_quiet_NaN = false;
|
| 181 |
+
static inline constexpr const bool has_signaling_NaN = false;
|
| 182 |
+
static inline constexpr const std::float_denorm_style has_denorm =
|
| 183 |
+
std::denorm_absent;
|
| 184 |
+
static inline constexpr const bool has_denorm_loss = false;
|
| 185 |
+
static inline constexpr const std::float_round_style round_style =
|
| 186 |
+
std::round_toward_zero;
|
| 187 |
+
static inline constexpr const bool is_iec559 = false;
|
| 188 |
+
static inline constexpr const bool is_bounded = true;
|
| 189 |
+
static inline constexpr const int max_digits10 = 0; // Not used for integers.
|
| 190 |
+
static inline constexpr const int radix = 2;
|
| 191 |
+
static inline constexpr const int min_exponent = 0;
|
| 192 |
+
static inline constexpr const int min_exponent10 = 0;
|
| 193 |
+
static inline constexpr const int max_exponent = 0;
|
| 194 |
+
static inline constexpr const int max_exponent10 = 0;
|
| 195 |
+
static inline constexpr const bool traps = true;
|
| 196 |
+
static inline constexpr const bool tinyness_before = false;
|
| 197 |
+
|
| 198 |
+
static constexpr ml_dtypes::int4 epsilon() noexcept {
|
| 199 |
+
return ml_dtypes::int4(0);
|
| 200 |
+
}
|
| 201 |
+
static constexpr ml_dtypes::int4 round_error() noexcept {
|
| 202 |
+
return ml_dtypes::int4(0);
|
| 203 |
+
}
|
| 204 |
+
static constexpr ml_dtypes::int4 infinity() noexcept {
|
| 205 |
+
return ml_dtypes::int4(0);
|
| 206 |
+
}
|
| 207 |
+
static constexpr ml_dtypes::int4 quiet_NaN() noexcept {
|
| 208 |
+
return ml_dtypes::int4(0);
|
| 209 |
+
}
|
| 210 |
+
static constexpr ml_dtypes::int4 signaling_NaN() noexcept {
|
| 211 |
+
return ml_dtypes::int4(0);
|
| 212 |
+
}
|
| 213 |
+
static constexpr ml_dtypes::int4 denorm_min() noexcept {
|
| 214 |
+
return ml_dtypes::int4(0);
|
| 215 |
+
}
|
| 216 |
+
};
|
| 217 |
+
|
| 218 |
+
} // namespace internal
|
| 219 |
+
|
| 220 |
+
} // namespace ml_dtypes
|
| 221 |
+
|
| 222 |
+
namespace std {
|
| 223 |
+
|
| 224 |
+
template <>
|
| 225 |
+
struct numeric_limits<ml_dtypes::int4>
|
| 226 |
+
: public ml_dtypes::internal::int4_numeric_limits_base {
|
| 227 |
+
static inline constexpr const bool is_signed = true;
|
| 228 |
+
static inline constexpr const bool is_modulo = false;
|
| 229 |
+
static inline constexpr const int digits = 3;
|
| 230 |
+
static inline constexpr const int digits10 = 0; // floor(3 * log10(2))
|
| 231 |
+
static constexpr ml_dtypes::int4 min() noexcept {
|
| 232 |
+
return ml_dtypes::int4::lowest();
|
| 233 |
+
}
|
| 234 |
+
static constexpr ml_dtypes::int4 lowest() noexcept {
|
| 235 |
+
return ml_dtypes::int4::lowest();
|
| 236 |
+
}
|
| 237 |
+
static constexpr ml_dtypes::int4 max() noexcept {
|
| 238 |
+
return ml_dtypes::int4::highest();
|
| 239 |
+
}
|
| 240 |
+
};
|
| 241 |
+
|
| 242 |
+
template <>
|
| 243 |
+
struct numeric_limits<ml_dtypes::uint4>
|
| 244 |
+
: public ml_dtypes::internal::int4_numeric_limits_base {
|
| 245 |
+
static inline constexpr const bool is_signed = false;
|
| 246 |
+
static inline constexpr const bool is_modulo = true;
|
| 247 |
+
static inline constexpr const int digits = 4;
|
| 248 |
+
static inline constexpr const int digits10 = 1; // floor(4 * log10(2))
|
| 249 |
+
static constexpr ml_dtypes::uint4 min() noexcept {
|
| 250 |
+
return ml_dtypes::uint4::lowest();
|
| 251 |
+
}
|
| 252 |
+
static constexpr ml_dtypes::uint4 lowest() noexcept {
|
| 253 |
+
return ml_dtypes::uint4::lowest();
|
| 254 |
+
}
|
| 255 |
+
static constexpr ml_dtypes::uint4 max() noexcept {
|
| 256 |
+
return ml_dtypes::uint4::highest();
|
| 257 |
+
}
|
| 258 |
+
};
|
| 259 |
+
|
| 260 |
+
} // namespace std
|
| 261 |
+
|
| 262 |
+
#endif // ML_DTYPES_INT4_H_
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/include/external/ml_dtypes/include/float8.h
ADDED
|
@@ -0,0 +1,1401 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2022 The TensorFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
|
| 16 |
+
#ifndef ML_DTYPES_FLOAT8_H_
|
| 17 |
+
#define ML_DTYPES_FLOAT8_H_
|
| 18 |
+
|
| 19 |
+
// 8-bit Floating Point Interchange Format, as described by
|
| 20 |
+
// https://arxiv.org/abs/2209.05433
|
| 21 |
+
|
| 22 |
+
#include <algorithm>
|
| 23 |
+
#include <cmath>
|
| 24 |
+
#include <cstdint>
|
| 25 |
+
#include <limits>
|
| 26 |
+
#include <ostream>
|
| 27 |
+
#include <type_traits>
|
| 28 |
+
#include <utility>
|
| 29 |
+
|
| 30 |
+
#ifdef __has_include
|
| 31 |
+
# if __has_include(<version>)
|
| 32 |
+
# include <version>
|
| 33 |
+
# endif
|
| 34 |
+
#endif
|
| 35 |
+
|
| 36 |
+
#if (defined(__cpp_lib_bitops) && __cpp_lib_bitops >= 201907L)
|
| 37 |
+
#include <bit>
|
| 38 |
+
#endif
|
| 39 |
+
|
| 40 |
+
#include "Eigen/Core"
|
| 41 |
+
|
| 42 |
+
namespace ml_dtypes {
|
| 43 |
+
namespace float8_internal {
|
| 44 |
+
|
| 45 |
+
// Forward-declarations of classes.
|
| 46 |
+
class float8_e4m3fn;
|
| 47 |
+
class float8_e4m3fnuz;
|
| 48 |
+
class float8_e4m3b11fnuz;
|
| 49 |
+
class float8_e5m2;
|
| 50 |
+
class float8_e5m2fnuz;
|
| 51 |
+
|
| 52 |
+
template <typename Derived>
|
| 53 |
+
class float8_base {
|
| 54 |
+
protected:
|
| 55 |
+
// Constructor tag to allow constexpr construction from bit representation.
|
| 56 |
+
struct ConstructFromRepTag {};
|
| 57 |
+
constexpr float8_base(uint8_t rep, ConstructFromRepTag) : rep_{rep} {}
|
| 58 |
+
|
| 59 |
+
public:
|
| 60 |
+
constexpr float8_base() : rep_(0) {}
|
| 61 |
+
|
| 62 |
+
template <typename T,
|
| 63 |
+
typename EnableIf = std::enable_if<std::is_arithmetic_v<T>>>
|
| 64 |
+
explicit EIGEN_DEVICE_FUNC float8_base(T f)
|
| 65 |
+
: float8_base(ConvertFrom(static_cast<float>(f)).rep(),
|
| 66 |
+
ConstructFromRepTag{}) {}
|
| 67 |
+
explicit EIGEN_DEVICE_FUNC float8_base(double f64)
|
| 68 |
+
: float8_base(ConvertFrom(f64).rep(), ConstructFromRepTag{}) {}
|
| 69 |
+
explicit EIGEN_DEVICE_FUNC float8_base(float f32)
|
| 70 |
+
: float8_base(ConvertFrom(f32).rep(), ConstructFromRepTag{}) {}
|
| 71 |
+
explicit EIGEN_DEVICE_FUNC float8_base(Eigen::bfloat16 bf16)
|
| 72 |
+
: float8_base(ConvertFrom(bf16).rep(), ConstructFromRepTag{}) {}
|
| 73 |
+
explicit EIGEN_DEVICE_FUNC float8_base(Eigen::half f16)
|
| 74 |
+
: float8_base(ConvertFrom(f16).rep(), ConstructFromRepTag{}) {}
|
| 75 |
+
|
| 76 |
+
constexpr uint8_t rep() const { return rep_; }
|
| 77 |
+
|
| 78 |
+
template <typename T,
|
| 79 |
+
typename EnableIf = std::enable_if<std::is_arithmetic_v<T>>>
|
| 80 |
+
explicit EIGEN_DEVICE_FUNC operator T() const {
|
| 81 |
+
return static_cast<T>(static_cast<float>(derived()));
|
| 82 |
+
}
|
| 83 |
+
explicit EIGEN_DEVICE_FUNC operator double() const {
|
| 84 |
+
return ConvertTo<double>(derived());
|
| 85 |
+
}
|
| 86 |
+
explicit EIGEN_DEVICE_FUNC operator float() const {
|
| 87 |
+
return ConvertTo<float>(derived());
|
| 88 |
+
}
|
| 89 |
+
explicit EIGEN_DEVICE_FUNC operator Eigen::bfloat16() const {
|
| 90 |
+
return ConvertTo<Eigen::bfloat16>(derived());
|
| 91 |
+
}
|
| 92 |
+
explicit EIGEN_DEVICE_FUNC operator Eigen::half() const {
|
| 93 |
+
return ConvertTo<Eigen::half>(derived());
|
| 94 |
+
}
|
| 95 |
+
explicit EIGEN_DEVICE_FUNC operator bool() const {
|
| 96 |
+
return (rep() & 0x7F) != 0;
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
constexpr Derived operator-() const {
|
| 100 |
+
return Derived(static_cast<uint8_t>(rep() ^ 0x80), ConstructFromRepTag{});
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
constexpr const Derived& derived() const {
|
| 104 |
+
return *static_cast<const Derived*>(this);
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
constexpr Derived& derived() { return *static_cast<Derived*>(this); }
|
| 108 |
+
|
| 109 |
+
static constexpr Derived FromRep(uint8_t rep) {
|
| 110 |
+
return Derived(rep, ConstructFromRepTag{});
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
// Conversions allowing saturation and truncation.
|
| 114 |
+
template <bool kSaturate = false, bool kTruncate = false, typename From>
|
| 115 |
+
static inline EIGEN_DEVICE_FUNC Derived ConvertFrom(const From& from);
|
| 116 |
+
|
| 117 |
+
template <typename To, bool kSaturate = false, bool kTruncate = false>
|
| 118 |
+
static inline EIGEN_DEVICE_FUNC To ConvertTo(const Derived& from);
|
| 119 |
+
|
| 120 |
+
// Operators via float32.
|
| 121 |
+
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Derived
|
| 122 |
+
operator+(const Derived& other) const {
|
| 123 |
+
return Derived{float{derived()} + float{other}};
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Derived
|
| 127 |
+
operator-(const Derived& other) const {
|
| 128 |
+
return Derived{float{derived()} - float{other}};
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Derived
|
| 132 |
+
operator*(const Derived& other) const {
|
| 133 |
+
return Derived{float{derived()} * float{other}};
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Derived
|
| 137 |
+
operator/(const Derived& other) const {
|
| 138 |
+
return Derived{float{derived()} / float{other}};
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
constexpr bool operator==(const Derived& other) const {
|
| 142 |
+
return Compare(derived(), other) == Ordering::kEquivalent;
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
constexpr bool operator!=(const Derived& other) const {
|
| 146 |
+
return Compare(derived(), other) != Ordering::kEquivalent;
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator<(
|
| 150 |
+
const Derived& other) const {
|
| 151 |
+
return Compare(derived(), other) == Ordering::kLess;
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator<=(
|
| 155 |
+
const Derived& other) const {
|
| 156 |
+
return Compare(derived(), other) <= Ordering::kEquivalent;
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
+
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator>(
|
| 160 |
+
const Derived& other) const {
|
| 161 |
+
return Compare(derived(), other) == Ordering::kGreater;
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator>=(
|
| 165 |
+
const Derived& other) const {
|
| 166 |
+
Ordering ordering = Compare(derived(), other);
|
| 167 |
+
return ordering == Ordering::kGreater || ordering == Ordering::kEquivalent;
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
// Compound assignment.
|
| 171 |
+
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Derived& operator+=(
|
| 172 |
+
const Derived& other) {
|
| 173 |
+
derived() = derived() + other;
|
| 174 |
+
return derived();
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Derived& operator-=(
|
| 178 |
+
const Derived& other) {
|
| 179 |
+
derived() = derived() - other;
|
| 180 |
+
return derived();
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Derived& operator*=(
|
| 184 |
+
const Derived& other) {
|
| 185 |
+
derived() = derived() * other;
|
| 186 |
+
return derived();
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
+
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Derived& operator/=(
|
| 190 |
+
const Derived& other) {
|
| 191 |
+
derived() = derived() / other;
|
| 192 |
+
return derived();
|
| 193 |
+
}
|
| 194 |
+
|
| 195 |
+
private:
|
| 196 |
+
static EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC std::pair<uint8_t, uint8_t>
|
| 197 |
+
SignAndMagnitude(Derived x) {
|
| 198 |
+
const uint8_t x_abs_bits =
|
| 199 |
+
Eigen::numext::bit_cast<uint8_t>(Eigen::numext::abs(x));
|
| 200 |
+
const uint8_t x_bits = Eigen::numext::bit_cast<uint8_t>(x);
|
| 201 |
+
const uint8_t x_sign = x_bits ^ x_abs_bits;
|
| 202 |
+
return {x_sign, x_abs_bits};
|
| 203 |
+
}
|
| 204 |
+
static EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC int8_t
|
| 205 |
+
SignAndMagnitudeToTwosComplement(uint8_t sign, uint8_t magnitude) {
|
| 206 |
+
return magnitude ^ (static_cast<int8_t>(sign) < 0 ? -1 : 0);
|
| 207 |
+
}
|
| 208 |
+
|
| 209 |
+
enum Ordering : int8_t {
|
| 210 |
+
kLess = -1,
|
| 211 |
+
kEquivalent = 0,
|
| 212 |
+
kGreater = 1,
|
| 213 |
+
kUnordered = 2,
|
| 214 |
+
};
|
| 215 |
+
|
| 216 |
+
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC friend Ordering Compare(
|
| 217 |
+
const Derived& lhs, const Derived& rhs) {
|
| 218 |
+
if (Eigen::numext::isnan(lhs) || Eigen::numext::isnan(rhs)) {
|
| 219 |
+
return Ordering::kUnordered;
|
| 220 |
+
}
|
| 221 |
+
auto [lhs_sign, lhs_mag] = SignAndMagnitude(lhs);
|
| 222 |
+
auto [rhs_sign, rhs_mag] = SignAndMagnitude(rhs);
|
| 223 |
+
if (lhs_mag == 0 && rhs_mag == 0) {
|
| 224 |
+
return Ordering::kEquivalent;
|
| 225 |
+
}
|
| 226 |
+
int8_t lhs_twos_complement =
|
| 227 |
+
SignAndMagnitudeToTwosComplement(lhs_sign, lhs_mag);
|
| 228 |
+
int8_t rhs_twos_complement =
|
| 229 |
+
SignAndMagnitudeToTwosComplement(rhs_sign, rhs_mag);
|
| 230 |
+
if (lhs_twos_complement < rhs_twos_complement) {
|
| 231 |
+
return Ordering::kLess;
|
| 232 |
+
}
|
| 233 |
+
if (lhs_twos_complement > rhs_twos_complement) {
|
| 234 |
+
return Ordering::kGreater;
|
| 235 |
+
}
|
| 236 |
+
return Ordering::kEquivalent;
|
| 237 |
+
}
|
| 238 |
+
|
| 239 |
+
uint8_t rep_;
|
| 240 |
+
};
|
| 241 |
+
|
| 242 |
+
class float8_e4m3fn : public float8_base<float8_e4m3fn> {
|
| 243 |
+
// Exponent: 4, Mantissa: 3, bias: 7.
|
| 244 |
+
// Extended range: no inf, NaN represented by 0bS111'1111.
|
| 245 |
+
// The "fn" suffix is for consistency with the corresponding LLVM/MLIR type,
|
| 246 |
+
// signaling this type is not consistent with IEEE-754. The "f" indicates
|
| 247 |
+
// it is finite values only. The "n" indicates it includes NaNs, but only
|
| 248 |
+
// at the outer range.
|
| 249 |
+
private:
|
| 250 |
+
using Base = float8_base<float8_e4m3fn>;
|
| 251 |
+
friend class float8_base<float8_e4m3fn>;
|
| 252 |
+
using Base::Base;
|
| 253 |
+
|
| 254 |
+
public:
|
| 255 |
+
explicit EIGEN_DEVICE_FUNC float8_e4m3fn(const float8_e5m2& f8)
|
| 256 |
+
: float8_e4m3fn(ConvertFrom(f8)) {}
|
| 257 |
+
explicit EIGEN_DEVICE_FUNC float8_e4m3fn(const float8_e4m3b11fnuz& f8)
|
| 258 |
+
: float8_e4m3fn(ConvertFrom(f8)) {}
|
| 259 |
+
};
|
| 260 |
+
|
| 261 |
+
class float8_e4m3b11fnuz : public float8_base<float8_e4m3b11fnuz> {
|
| 262 |
+
// Exponent: 4, Mantissa: 3, bias: 11.
|
| 263 |
+
// Extended range: no inf, NaN represented by 0b1000'0000.
|
| 264 |
+
private:
|
| 265 |
+
using Base = float8_base<float8_e4m3b11fnuz>;
|
| 266 |
+
friend class float8_base<float8_e4m3b11fnuz>;
|
| 267 |
+
using Base::Base;
|
| 268 |
+
|
| 269 |
+
public:
|
| 270 |
+
explicit EIGEN_DEVICE_FUNC float8_e4m3b11fnuz(const float8_e5m2& f8)
|
| 271 |
+
: float8_e4m3b11fnuz(ConvertFrom(f8)) {}
|
| 272 |
+
explicit EIGEN_DEVICE_FUNC float8_e4m3b11fnuz(const float8_e5m2fnuz& f8)
|
| 273 |
+
: float8_e4m3b11fnuz(ConvertFrom(f8)) {}
|
| 274 |
+
explicit EIGEN_DEVICE_FUNC float8_e4m3b11fnuz(const float8_e4m3fn& f8)
|
| 275 |
+
: float8_e4m3b11fnuz(ConvertFrom(f8)) {}
|
| 276 |
+
explicit EIGEN_DEVICE_FUNC float8_e4m3b11fnuz(const float8_e4m3fnuz& f8)
|
| 277 |
+
: float8_e4m3b11fnuz(ConvertFrom(f8)) {}
|
| 278 |
+
|
| 279 |
+
constexpr float8_e4m3b11fnuz operator-() const {
|
| 280 |
+
if ((rep() & 0x7f) == 0x00) {
|
| 281 |
+
return *this;
|
| 282 |
+
}
|
| 283 |
+
return Base::operator-();
|
| 284 |
+
}
|
| 285 |
+
|
| 286 |
+
float8_e4m3b11fnuz operator-(const float8_e4m3b11fnuz& other) const {
|
| 287 |
+
return Base::operator-(other);
|
| 288 |
+
}
|
| 289 |
+
|
| 290 |
+
explicit EIGEN_DEVICE_FUNC operator bool() const { return rep() != 0; }
|
| 291 |
+
};
|
| 292 |
+
|
| 293 |
+
// Legacy name used in XLA (TODO(jewillco): remove).
|
| 294 |
+
using float8_e4m3b11 = float8_e4m3b11fnuz;
|
| 295 |
+
|
| 296 |
+
class float8_e4m3fnuz : public float8_base<float8_e4m3fnuz> {
|
| 297 |
+
// 8-bit floating point with 3 bit mantissa.
|
| 298 |
+
//
|
| 299 |
+
// An 8-bit floating point type with 1 sign bit, 4 bits exponent and 3 bits
|
| 300 |
+
// mantissa. The suffix "fnuz" is consistent with LLVM/MLIR naming and is
|
| 301 |
+
// derived from the differences to IEEE floating point conventions. `F` is
|
| 302 |
+
// for "finite" (no infinities), `N` for with special NaN encoding, `UZ` for
|
| 303 |
+
// unsigned zero.
|
| 304 |
+
//
|
| 305 |
+
// This type has the following characteristics:
|
| 306 |
+
// * bit encoding: S1E4M3 - `0bSEEEEMMM`
|
| 307 |
+
// * exponent bias: 8
|
| 308 |
+
// * infinities: Not supported
|
| 309 |
+
// * NaNs: Supported with sign bit set to 1, exponent bits and mantissa bits
|
| 310 |
+
// set to all 0s - `0b10000000`
|
| 311 |
+
// * denormals when exponent is 0
|
| 312 |
+
private:
|
| 313 |
+
using Base = float8_base<float8_e4m3fnuz>;
|
| 314 |
+
friend class float8_base<float8_e4m3fnuz>;
|
| 315 |
+
using Base::Base;
|
| 316 |
+
|
| 317 |
+
public:
|
| 318 |
+
explicit EIGEN_DEVICE_FUNC float8_e4m3fnuz(const float8_e5m2& f8)
|
| 319 |
+
: float8_e4m3fnuz(ConvertFrom(f8)) {}
|
| 320 |
+
explicit EIGEN_DEVICE_FUNC float8_e4m3fnuz(const float8_e5m2fnuz& f8)
|
| 321 |
+
: float8_e4m3fnuz(ConvertFrom(f8)) {}
|
| 322 |
+
explicit EIGEN_DEVICE_FUNC float8_e4m3fnuz(const float8_e4m3b11fnuz& f8)
|
| 323 |
+
: float8_e4m3fnuz(ConvertFrom(f8)) {}
|
| 324 |
+
explicit EIGEN_DEVICE_FUNC float8_e4m3fnuz(const float8_e4m3fn& f8)
|
| 325 |
+
: float8_e4m3fnuz(ConvertFrom(f8)) {}
|
| 326 |
+
|
| 327 |
+
constexpr float8_e4m3fnuz operator-() const {
|
| 328 |
+
if ((rep() & 0x7f) == 0x00) {
|
| 329 |
+
return *this;
|
| 330 |
+
}
|
| 331 |
+
return Base::operator-();
|
| 332 |
+
}
|
| 333 |
+
|
| 334 |
+
float8_e4m3fnuz operator-(const float8_e4m3fnuz& other) const {
|
| 335 |
+
return Base::operator-(other);
|
| 336 |
+
}
|
| 337 |
+
|
| 338 |
+
explicit EIGEN_DEVICE_FUNC operator bool() const { return rep() != 0; }
|
| 339 |
+
};
|
| 340 |
+
|
| 341 |
+
class float8_e5m2 : public float8_base<float8_e5m2> {
|
| 342 |
+
// Exponent: 5, Mantissa: 2, bias: 15.
|
| 343 |
+
// IEEE 754.
|
| 344 |
+
private:
|
| 345 |
+
using Base = float8_base<float8_e5m2>;
|
| 346 |
+
friend class float8_base<float8_e5m2>;
|
| 347 |
+
using Base::Base;
|
| 348 |
+
|
| 349 |
+
public:
|
| 350 |
+
explicit EIGEN_DEVICE_FUNC float8_e5m2(float8_e4m3fn f8)
|
| 351 |
+
: float8_e5m2(ConvertFrom(f8)) {}
|
| 352 |
+
explicit EIGEN_DEVICE_FUNC float8_e5m2(float8_e4m3fnuz f8)
|
| 353 |
+
: float8_e5m2(ConvertFrom(f8)) {}
|
| 354 |
+
explicit EIGEN_DEVICE_FUNC float8_e5m2(float8_e4m3b11fnuz f8)
|
| 355 |
+
: float8_e5m2(ConvertFrom(f8)) {}
|
| 356 |
+
explicit EIGEN_DEVICE_FUNC float8_e5m2(float8_e5m2fnuz& f8)
|
| 357 |
+
: float8_e5m2(ConvertFrom(f8)) {}
|
| 358 |
+
};
|
| 359 |
+
|
| 360 |
+
class float8_e5m2fnuz : public float8_base<float8_e5m2fnuz> {
|
| 361 |
+
// 8-bit floating point with 2 bit mantissa.
|
| 362 |
+
//
|
| 363 |
+
// An 8-bit floating point type with 1 sign bit, 5 bits exponent and 2 bits
|
| 364 |
+
// mantissa. The suffix "fnuz" is consistent with LLVM/MLIR naming and is
|
| 365 |
+
// derived from the differences to IEEE floating point conventions. `F` is
|
| 366 |
+
// for "finite" (no infinities), `N` for with special NaN encoding, `UZ` for
|
| 367 |
+
// unsigned zero.
|
| 368 |
+
//
|
| 369 |
+
// This type has the following characteristics:
|
| 370 |
+
// * bit encoding: S1E5M2 - `0bSEEEEEMM`
|
| 371 |
+
// * exponent bias: 16
|
| 372 |
+
// * infinities: Not supported
|
| 373 |
+
// * NaNs: Supported with sign bit set to 1, exponent bits and mantissa bits
|
| 374 |
+
// set to all 0s - `0b10000000`
|
| 375 |
+
// * denormals when exponent is 0
|
| 376 |
+
private:
|
| 377 |
+
using Base = float8_base<float8_e5m2fnuz>;
|
| 378 |
+
friend class float8_base<float8_e5m2fnuz>;
|
| 379 |
+
using Base::Base;
|
| 380 |
+
|
| 381 |
+
public:
|
| 382 |
+
explicit EIGEN_DEVICE_FUNC float8_e5m2fnuz(const float8_e5m2& f8)
|
| 383 |
+
: float8_e5m2fnuz(ConvertFrom(f8)) {}
|
| 384 |
+
explicit EIGEN_DEVICE_FUNC float8_e5m2fnuz(const float8_e4m3b11fnuz& f8)
|
| 385 |
+
: float8_e5m2fnuz(ConvertFrom(f8)) {}
|
| 386 |
+
explicit EIGEN_DEVICE_FUNC float8_e5m2fnuz(const float8_e4m3fn& f8)
|
| 387 |
+
: float8_e5m2fnuz(ConvertFrom(f8)) {}
|
| 388 |
+
explicit EIGEN_DEVICE_FUNC float8_e5m2fnuz(const float8_e4m3fnuz& f8)
|
| 389 |
+
: float8_e5m2fnuz(ConvertFrom(f8)) {}
|
| 390 |
+
|
| 391 |
+
constexpr float8_e5m2fnuz operator-() const {
|
| 392 |
+
if ((rep() & 0x7f) == 0x00) {
|
| 393 |
+
return *this;
|
| 394 |
+
}
|
| 395 |
+
return Base::operator-();
|
| 396 |
+
}
|
| 397 |
+
|
| 398 |
+
float8_e5m2fnuz operator-(const float8_e5m2fnuz& other) const {
|
| 399 |
+
return Base::operator-(other);
|
| 400 |
+
}
|
| 401 |
+
|
| 402 |
+
explicit EIGEN_DEVICE_FUNC operator bool() const { return rep() != 0; }
|
| 403 |
+
};
|
| 404 |
+
|
| 405 |
+
constexpr double ConstexprAbs(double x) { return x < 0.0 ? -x : x; }
|
| 406 |
+
|
| 407 |
+
constexpr double ConstexprCeil(double x) {
|
| 408 |
+
constexpr double kIntegerThreshold =
|
| 409 |
+
uint64_t{1} << (std::numeric_limits<double>::digits - 1);
|
| 410 |
+
// Too big or NaN inputs get returned unchanged.
|
| 411 |
+
if (!(ConstexprAbs(x) < kIntegerThreshold)) {
|
| 412 |
+
return x;
|
| 413 |
+
}
|
| 414 |
+
const double x_trunc = static_cast<double>(static_cast<int64_t>(x));
|
| 415 |
+
return x_trunc < x ? x_trunc + 1.0 : x_trunc;
|
| 416 |
+
}
|
| 417 |
+
|
| 418 |
+
constexpr double ConstexprFloor(double x) { return -ConstexprCeil(-x); }
|
| 419 |
+
|
| 420 |
+
constexpr double kLog10Of2 = 0.3010299956639812;
|
| 421 |
+
// C17 5.2.4.2.2p11:
|
| 422 |
+
// "number of decimal digits, q, such that any floating-point number with q
|
| 423 |
+
// decimal digits can be rounded into a floating-point number with p radix b
|
| 424 |
+
// digits and back again without change to the q decimal digits"
|
| 425 |
+
// floor((p - 1) * log10(2));
|
| 426 |
+
constexpr int Digits10FromDigits(int digits) {
|
| 427 |
+
return static_cast<int>(ConstexprFloor((digits - 1) * kLog10Of2));
|
| 428 |
+
}
|
| 429 |
+
|
| 430 |
+
// C17 5.2.4.2.2p11:
|
| 431 |
+
// "number of decimal digits, n, such that any floating-point number with p
|
| 432 |
+
// radix b digits can be rounded to a floating-point number with n decimal
|
| 433 |
+
// digits and back again without change to the value"
|
| 434 |
+
// ceil(1 + p * log10(2));
|
| 435 |
+
constexpr int MaxDigits10FromDigits(int digits) {
|
| 436 |
+
return static_cast<int>(ConstexprCeil(1.0 + (digits * kLog10Of2)));
|
| 437 |
+
}
|
| 438 |
+
|
| 439 |
+
// C17 5.2.4.2.2p11:
|
| 440 |
+
// "minimum negative integer such that 10 raised to that power is in the range
|
| 441 |
+
// of normalized floating-point numbers"
|
| 442 |
+
// ceil(log10(2**(emin - 1))) == ceil((emin - 1) * log10(2));
|
| 443 |
+
constexpr int MinExponent10FromMinExponent(int min_exponent) {
|
| 444 |
+
return static_cast<int>(ConstexprCeil((min_exponent - 1) * kLog10Of2));
|
| 445 |
+
}
|
| 446 |
+
|
| 447 |
+
// C17 5.2.4.2.2p11:
|
| 448 |
+
// "maximum integer such that 10 raised to that power is in the range of
|
| 449 |
+
// representable finite floating-point numbers"
|
| 450 |
+
// floor(log10((1 - 2**-p) * 2**emax)) == floor(log10(1 - 2**-p) +
|
| 451 |
+
// emax * log10(2))
|
| 452 |
+
constexpr int MaxExponent10FromMaxExponentAndDigits(int max_exponent,
|
| 453 |
+
int digits) {
|
| 454 |
+
// We only support digits in {3,4}. This table would grow if we wanted to
|
| 455 |
+
// handle more values.
|
| 456 |
+
constexpr double kLog10OfOnePredecessor[] = {
|
| 457 |
+
// log10(1 - 2**-3)
|
| 458 |
+
-0.057991946977686754,
|
| 459 |
+
// log10(1 - 2**-4)
|
| 460 |
+
-0.028028723600243537,
|
| 461 |
+
};
|
| 462 |
+
return static_cast<int>(ConstexprFloor(kLog10OfOnePredecessor[digits - 3] +
|
| 463 |
+
max_exponent * kLog10Of2));
|
| 464 |
+
}
|
| 465 |
+
|
| 466 |
+
// Structures for use in specializing std::numeric_limits.
|
| 467 |
+
struct numeric_limits_float8_base {
|
| 468 |
+
// NOLINTBEGIN: these names must match std::numeric_limits.
|
| 469 |
+
static inline constexpr const bool is_specialized = true;
|
| 470 |
+
static inline constexpr const bool is_signed = true;
|
| 471 |
+
static inline constexpr const bool is_integer = false;
|
| 472 |
+
static inline constexpr const bool is_exact = false;
|
| 473 |
+
static inline constexpr const bool has_quiet_NaN = true;
|
| 474 |
+
static inline constexpr const std::float_denorm_style has_denorm =
|
| 475 |
+
std::denorm_present;
|
| 476 |
+
static inline constexpr const bool has_denorm_loss = false;
|
| 477 |
+
static inline constexpr const std::float_round_style round_style =
|
| 478 |
+
std::round_to_nearest;
|
| 479 |
+
static inline constexpr const bool is_bounded = true;
|
| 480 |
+
static inline constexpr const bool is_modulo = false;
|
| 481 |
+
static inline constexpr const int radix = std::numeric_limits<float>::radix;
|
| 482 |
+
static inline constexpr const bool traps = std::numeric_limits<float>::traps;
|
| 483 |
+
static inline constexpr const bool tinyness_before =
|
| 484 |
+
std::numeric_limits<float>::tinyness_before;
|
| 485 |
+
// NOLINTEND
|
| 486 |
+
};
|
| 487 |
+
|
| 488 |
+
struct numeric_limits_float8_e4m3fn : public numeric_limits_float8_base {
|
| 489 |
+
private:
|
| 490 |
+
static inline constexpr const int kExponentBias = 7;
|
| 491 |
+
static inline constexpr const int kMantissaBits = 3;
|
| 492 |
+
|
| 493 |
+
public:
|
| 494 |
+
// NOLINTBEGIN: these names must match std::numeric_limits.
|
| 495 |
+
static inline constexpr const int digits = kMantissaBits + 1;
|
| 496 |
+
static inline constexpr const int digits10 = Digits10FromDigits(digits);
|
| 497 |
+
static inline constexpr const int max_digits10 =
|
| 498 |
+
MaxDigits10FromDigits(digits);
|
| 499 |
+
static inline constexpr const int min_exponent = (1 - kExponentBias) + 1;
|
| 500 |
+
static inline constexpr const int min_exponent10 =
|
| 501 |
+
MinExponent10FromMinExponent(min_exponent);
|
| 502 |
+
static inline constexpr const int max_exponent =
|
| 503 |
+
(0b1111 - 7) + 1; // Extended format.
|
| 504 |
+
static inline constexpr const int max_exponent10 =
|
| 505 |
+
MaxExponent10FromMaxExponentAndDigits(max_exponent, digits);
|
| 506 |
+
static inline constexpr const bool is_iec559 = false;
|
| 507 |
+
static inline constexpr const bool has_infinity = false;
|
| 508 |
+
static inline constexpr const bool has_signaling_NaN = false;
|
| 509 |
+
// NOLINTEND
|
| 510 |
+
|
| 511 |
+
// 1.0 * 2^(0b0001 - 7) = 1.0 * 2^-6 = 0.015625
|
| 512 |
+
static constexpr float8_e4m3fn min() {
|
| 513 |
+
return float8_e4m3fn::FromRep(0b0'0001 << kMantissaBits);
|
| 514 |
+
}
|
| 515 |
+
// -(1 + 0b110 * 2^-3) * 2^(0b1111 - 7) = -1.75 * 2^8 = 448
|
| 516 |
+
static constexpr float8_e4m3fn lowest() {
|
| 517 |
+
return float8_e4m3fn::FromRep(0b1'1111'110);
|
| 518 |
+
}
|
| 519 |
+
// (1 + 0b110 * 2^-3) * 2**(0b1111 - 7) = 1.75 * 2^8 = 448
|
| 520 |
+
static constexpr float8_e4m3fn max() {
|
| 521 |
+
return float8_e4m3fn::FromRep(0b0'1111'110);
|
| 522 |
+
}
|
| 523 |
+
// 1.0 * 2^-3 = 0.125
|
| 524 |
+
static constexpr float8_e4m3fn epsilon() {
|
| 525 |
+
return float8_e4m3fn::FromRep((-kMantissaBits + kExponentBias)
|
| 526 |
+
<< kMantissaBits);
|
| 527 |
+
}
|
| 528 |
+
// 1.0 * 2^-1 = 0.5
|
| 529 |
+
static constexpr float8_e4m3fn round_error() {
|
| 530 |
+
return float8_e4m3fn::FromRep((-1 + kExponentBias) << kMantissaBits);
|
| 531 |
+
}
|
| 532 |
+
static constexpr float8_e4m3fn infinity() {
|
| 533 |
+
return float8_e4m3fn::FromRep(0b0'1111'111);
|
| 534 |
+
}
|
| 535 |
+
// NaN.
|
| 536 |
+
static constexpr float8_e4m3fn quiet_NaN() {
|
| 537 |
+
return float8_e4m3fn::FromRep(0b0'1111'111);
|
| 538 |
+
}
|
| 539 |
+
static constexpr float8_e4m3fn signaling_NaN() {
|
| 540 |
+
return float8_e4m3fn::FromRep(0b0'1111'111);
|
| 541 |
+
}
|
| 542 |
+
// 1.0 * 2^(-7 - 3 + 1) = 1.0 * 2^-9 = 0.001953125
|
| 543 |
+
static constexpr float8_e4m3fn denorm_min() {
|
| 544 |
+
return float8_e4m3fn::FromRep(0b0'0000'001);
|
| 545 |
+
}
|
| 546 |
+
};
|
| 547 |
+
|
| 548 |
+
struct numeric_limits_float8_e4m3b11fnuz : public numeric_limits_float8_base {
|
| 549 |
+
private:
|
| 550 |
+
static inline constexpr const int kExponentBias = 11;
|
| 551 |
+
static inline constexpr const int kMantissaBits = 3;
|
| 552 |
+
|
| 553 |
+
public:
|
| 554 |
+
// NOLINTBEGIN: these names must match std::numeric_limits.
|
| 555 |
+
static inline constexpr const int digits = kMantissaBits + 1;
|
| 556 |
+
static inline constexpr const int digits10 = Digits10FromDigits(digits);
|
| 557 |
+
static inline constexpr const int max_digits10 =
|
| 558 |
+
MaxDigits10FromDigits(digits);
|
| 559 |
+
static inline constexpr const int min_exponent = (1 - kExponentBias) + 1;
|
| 560 |
+
static inline constexpr const int min_exponent10 =
|
| 561 |
+
MinExponent10FromMinExponent(min_exponent);
|
| 562 |
+
static inline constexpr const int max_exponent =
|
| 563 |
+
(0b1111 - kExponentBias) + 1; // Extended format.
|
| 564 |
+
static inline constexpr const int max_exponent10 =
|
| 565 |
+
MaxExponent10FromMaxExponentAndDigits(max_exponent, digits);
|
| 566 |
+
static inline constexpr const bool is_iec559 = false;
|
| 567 |
+
static inline constexpr const bool has_infinity = false;
|
| 568 |
+
static inline constexpr const bool has_signaling_NaN = false;
|
| 569 |
+
// NOLINTEND
|
| 570 |
+
|
| 571 |
+
// 1.0 * 2^(0b0001 - 11) = 1.0 * 2^-10 = 0.0009765625
|
| 572 |
+
static constexpr float8_e4m3b11fnuz min() {
|
| 573 |
+
return float8_e4m3b11fnuz::FromRep(1 << kMantissaBits);
|
| 574 |
+
}
|
| 575 |
+
// -(1 + 0b111 * 2^-3) * 2^(0b1111 - 11) = -1.875 * 2^4 = -30
|
| 576 |
+
static constexpr float8_e4m3b11fnuz lowest() {
|
| 577 |
+
return float8_e4m3b11fnuz::FromRep(0b1'1111'111);
|
| 578 |
+
}
|
| 579 |
+
// (1 + 0b111 * 2^-3) * 2^(0b1111 - 11) = 1.875 * 2^4 = 30
|
| 580 |
+
static constexpr float8_e4m3b11fnuz max() {
|
| 581 |
+
return float8_e4m3b11fnuz::FromRep(0b0'1111'111);
|
| 582 |
+
}
|
| 583 |
+
// 1.0 * 2^-3 = 0.125
|
| 584 |
+
static constexpr float8_e4m3b11fnuz epsilon() {
|
| 585 |
+
return float8_e4m3b11fnuz::FromRep((-kMantissaBits + kExponentBias)
|
| 586 |
+
<< kMantissaBits);
|
| 587 |
+
}
|
| 588 |
+
// 1.0 * 2^-1 = 0.5
|
| 589 |
+
static constexpr float8_e4m3b11fnuz round_error() {
|
| 590 |
+
return float8_e4m3b11fnuz::FromRep((-1 + kExponentBias) << kMantissaBits);
|
| 591 |
+
}
|
| 592 |
+
static constexpr float8_e4m3b11fnuz infinity() {
|
| 593 |
+
return float8_e4m3b11fnuz::FromRep(0b1'0000'000);
|
| 594 |
+
}
|
| 595 |
+
// NaN.
|
| 596 |
+
static constexpr float8_e4m3b11fnuz quiet_NaN() {
|
| 597 |
+
return float8_e4m3b11fnuz::FromRep(0b1'0000'000);
|
| 598 |
+
}
|
| 599 |
+
static constexpr float8_e4m3b11fnuz signaling_NaN() {
|
| 600 |
+
return float8_e4m3b11fnuz::FromRep(0b1'0000'000);
|
| 601 |
+
}
|
| 602 |
+
// 1.0 * 2^(-11 - 3 + 1) = 1.0 * 2^-13 = 0.0001220703125
|
| 603 |
+
static constexpr float8_e4m3b11fnuz denorm_min() {
|
| 604 |
+
return float8_e4m3b11fnuz::FromRep(0b0'0000'001);
|
| 605 |
+
}
|
| 606 |
+
};
|
| 607 |
+
|
| 608 |
+
struct numeric_limits_float8_e4m3fnuz : public numeric_limits_float8_base {
|
| 609 |
+
private:
|
| 610 |
+
static inline constexpr const int kExponentBias = 8;
|
| 611 |
+
static inline constexpr const int kMantissaBits = 3;
|
| 612 |
+
|
| 613 |
+
public:
|
| 614 |
+
// NOLINTBEGIN: these names must match std::numeric_limits.
|
| 615 |
+
static inline constexpr const int digits = kMantissaBits + 1;
|
| 616 |
+
static inline constexpr const int digits10 = Digits10FromDigits(digits);
|
| 617 |
+
static inline constexpr const int max_digits10 =
|
| 618 |
+
MaxDigits10FromDigits(digits);
|
| 619 |
+
static inline constexpr const int min_exponent = (1 - kExponentBias) + 1;
|
| 620 |
+
static inline constexpr const int min_exponent10 =
|
| 621 |
+
MinExponent10FromMinExponent(min_exponent);
|
| 622 |
+
static inline constexpr const int max_exponent =
|
| 623 |
+
(0b1111 - kExponentBias) + 1; // Extended format.
|
| 624 |
+
static inline constexpr const int max_exponent10 =
|
| 625 |
+
MaxExponent10FromMaxExponentAndDigits(max_exponent, digits);
|
| 626 |
+
static inline constexpr const bool is_iec559 = false;
|
| 627 |
+
static inline constexpr const bool has_infinity = false;
|
| 628 |
+
static inline constexpr const bool has_signaling_NaN = false;
|
| 629 |
+
// NOLINTEND
|
| 630 |
+
|
| 631 |
+
static constexpr float8_e4m3fnuz min() {
|
| 632 |
+
return float8_e4m3fnuz::FromRep(0x08);
|
| 633 |
+
}
|
| 634 |
+
static constexpr float8_e4m3fnuz lowest() {
|
| 635 |
+
return float8_e4m3fnuz::FromRep(0xFF);
|
| 636 |
+
}
|
| 637 |
+
static constexpr float8_e4m3fnuz max() {
|
| 638 |
+
return float8_e4m3fnuz::FromRep(0x7F);
|
| 639 |
+
}
|
| 640 |
+
static constexpr float8_e4m3fnuz epsilon() {
|
| 641 |
+
return float8_e4m3fnuz::FromRep((-kMantissaBits + kExponentBias)
|
| 642 |
+
<< kMantissaBits);
|
| 643 |
+
}
|
| 644 |
+
static constexpr float8_e4m3fnuz round_error() {
|
| 645 |
+
return float8_e4m3fnuz::FromRep((-1 + kExponentBias) << kMantissaBits);
|
| 646 |
+
}
|
| 647 |
+
static constexpr float8_e4m3fnuz infinity() {
|
| 648 |
+
return float8_e4m3fnuz::FromRep(0x80);
|
| 649 |
+
} // NaN.
|
| 650 |
+
static constexpr float8_e4m3fnuz quiet_NaN() {
|
| 651 |
+
return float8_e4m3fnuz::FromRep(0x80);
|
| 652 |
+
}
|
| 653 |
+
static constexpr float8_e4m3fnuz signaling_NaN() {
|
| 654 |
+
return float8_e4m3fnuz::FromRep(0x80);
|
| 655 |
+
}
|
| 656 |
+
static constexpr float8_e4m3fnuz denorm_min() {
|
| 657 |
+
return float8_e4m3fnuz::FromRep(0x01);
|
| 658 |
+
}
|
| 659 |
+
};
|
| 660 |
+
|
| 661 |
+
struct numeric_limits_float8_e5m2 : public numeric_limits_float8_base {
|
| 662 |
+
private:
|
| 663 |
+
static inline constexpr const int kExponentBias = 15;
|
| 664 |
+
static inline constexpr const int kMantissaBits = 2;
|
| 665 |
+
|
| 666 |
+
public:
|
| 667 |
+
// NOLINTBEGIN: these names must match std::numeric_limits.
|
| 668 |
+
static inline constexpr const int digits = kMantissaBits + 1;
|
| 669 |
+
static inline constexpr const int digits10 = Digits10FromDigits(digits);
|
| 670 |
+
static inline constexpr const int max_digits10 =
|
| 671 |
+
MaxDigits10FromDigits(digits);
|
| 672 |
+
static inline constexpr const int min_exponent = (1 - kExponentBias) + 1;
|
| 673 |
+
static inline constexpr const int min_exponent10 =
|
| 674 |
+
MinExponent10FromMinExponent(min_exponent);
|
| 675 |
+
static inline constexpr const int max_exponent = 0b11111 - kExponentBias;
|
| 676 |
+
static inline constexpr const int max_exponent10 =
|
| 677 |
+
MaxExponent10FromMaxExponentAndDigits(max_exponent, digits);
|
| 678 |
+
static inline constexpr const bool is_iec559 = true;
|
| 679 |
+
static inline constexpr const bool has_infinity = true;
|
| 680 |
+
static inline constexpr const bool has_signaling_NaN = true;
|
| 681 |
+
// NOLINTEND
|
| 682 |
+
|
| 683 |
+
// 1.0 * 2^(0b00001 - 15) = 1.0 * 2^-14 = 0.00006103515625
|
| 684 |
+
static constexpr float8_e5m2 min() {
|
| 685 |
+
return float8_e5m2::FromRep(1 << kMantissaBits);
|
| 686 |
+
}
|
| 687 |
+
// -(1 + 0b11 * 2^-2) * 2^(0b11110 - 15) = -1.75 * 2^15 = -57344
|
| 688 |
+
static constexpr float8_e5m2 lowest() {
|
| 689 |
+
return float8_e5m2::FromRep(0b1'11110'11);
|
| 690 |
+
}
|
| 691 |
+
// (1 + 0b11 * 2^-2) * 2^(0b11110 - 15) = 1.75 * 2^15 = 57344
|
| 692 |
+
static constexpr float8_e5m2 max() {
|
| 693 |
+
return float8_e5m2::FromRep(0b0'11110'11);
|
| 694 |
+
}
|
| 695 |
+
// 1.0 * 2^-2 = 0.25
|
| 696 |
+
static constexpr float8_e5m2 epsilon() {
|
| 697 |
+
return float8_e5m2::FromRep((-kMantissaBits + kExponentBias)
|
| 698 |
+
<< kMantissaBits);
|
| 699 |
+
}
|
| 700 |
+
// 1.0 * 2^-1 = 0.5
|
| 701 |
+
static constexpr float8_e5m2 round_error() {
|
| 702 |
+
return float8_e5m2::FromRep((-1 + kExponentBias) << kMantissaBits);
|
| 703 |
+
}
|
| 704 |
+
static constexpr float8_e5m2 infinity() {
|
| 705 |
+
return float8_e5m2::FromRep(0b0'11111'00);
|
| 706 |
+
}
|
| 707 |
+
static constexpr float8_e5m2 quiet_NaN() {
|
| 708 |
+
// IEEE 754-2019 6.2.1: "All binary NaN bit strings have the sign bit S set
|
| 709 |
+
// to 0 or 1 and all the bits of the biased exponent field E set to 1
|
| 710 |
+
// (see 3.4). A quiet NaN bit string should be encoded with the first bit
|
| 711 |
+
// (d1) of the trailing significand field T being 1."
|
| 712 |
+
return float8_e5m2::FromRep(0b0'11111'10);
|
| 713 |
+
}
|
| 714 |
+
static constexpr float8_e5m2 signaling_NaN() {
|
| 715 |
+
// IEEE 754-2019 6.2.1: "A signaling NaN bit string should be encoded with
|
| 716 |
+
// the first bit of the trailing significand field being 0."
|
| 717 |
+
return float8_e5m2::FromRep(0b0'11111'01);
|
| 718 |
+
}
|
| 719 |
+
// 1.0 * 2^(-15 - 2 + 1) = 1.0 * 2^-16 = 0.0000152587890625
|
| 720 |
+
static constexpr float8_e5m2 denorm_min() {
|
| 721 |
+
return float8_e5m2::FromRep(0b0'00000'01);
|
| 722 |
+
}
|
| 723 |
+
};
|
| 724 |
+
|
| 725 |
+
struct numeric_limits_float8_e5m2fnuz : public numeric_limits_float8_base {
|
| 726 |
+
private:
|
| 727 |
+
static inline constexpr const int kExponentBias = 16;
|
| 728 |
+
static inline constexpr const int kMantissaBits = 2;
|
| 729 |
+
|
| 730 |
+
public:
|
| 731 |
+
// NOLINTBEGIN: these names must match std::numeric_limits.
|
| 732 |
+
static inline constexpr const int digits = kMantissaBits + 1;
|
| 733 |
+
static inline constexpr const int digits10 = Digits10FromDigits(digits);
|
| 734 |
+
static inline constexpr const int max_digits10 =
|
| 735 |
+
MaxDigits10FromDigits(digits);
|
| 736 |
+
static inline constexpr const int min_exponent = (1 - kExponentBias) + 1;
|
| 737 |
+
static inline constexpr const int min_exponent10 =
|
| 738 |
+
MinExponent10FromMinExponent(min_exponent);
|
| 739 |
+
static inline constexpr const int max_exponent =
|
| 740 |
+
(0b11111 - kExponentBias) + 1;
|
| 741 |
+
static inline constexpr const int max_exponent10 =
|
| 742 |
+
MaxExponent10FromMaxExponentAndDigits(max_exponent, digits);
|
| 743 |
+
static inline constexpr const bool is_iec559 = false;
|
| 744 |
+
static inline constexpr const bool has_infinity = false;
|
| 745 |
+
static inline constexpr const bool has_signaling_NaN = false;
|
| 746 |
+
// NOLINTEND
|
| 747 |
+
|
| 748 |
+
static constexpr float8_e5m2fnuz min() {
|
| 749 |
+
return float8_e5m2fnuz::FromRep(0x04);
|
| 750 |
+
}
|
| 751 |
+
static constexpr float8_e5m2fnuz lowest() {
|
| 752 |
+
return float8_e5m2fnuz::FromRep(0xFF);
|
| 753 |
+
}
|
| 754 |
+
static constexpr float8_e5m2fnuz max() {
|
| 755 |
+
return float8_e5m2fnuz::FromRep(0x7F);
|
| 756 |
+
}
|
| 757 |
+
static constexpr float8_e5m2fnuz epsilon() {
|
| 758 |
+
return float8_e5m2fnuz::FromRep((-kMantissaBits + kExponentBias)
|
| 759 |
+
<< kMantissaBits);
|
| 760 |
+
}
|
| 761 |
+
static constexpr float8_e5m2fnuz round_error() {
|
| 762 |
+
return float8_e5m2fnuz::FromRep((-1 + kExponentBias) << kMantissaBits);
|
| 763 |
+
}
|
| 764 |
+
static constexpr float8_e5m2fnuz infinity() {
|
| 765 |
+
return float8_e5m2fnuz::FromRep(0x80);
|
| 766 |
+
} // NaN.
|
| 767 |
+
static constexpr float8_e5m2fnuz quiet_NaN() {
|
| 768 |
+
return float8_e5m2fnuz::FromRep(0x80);
|
| 769 |
+
}
|
| 770 |
+
static constexpr float8_e5m2fnuz signaling_NaN() {
|
| 771 |
+
return float8_e5m2fnuz::FromRep(0x80);
|
| 772 |
+
}
|
| 773 |
+
static constexpr float8_e5m2fnuz denorm_min() {
|
| 774 |
+
return float8_e5m2fnuz::FromRep(0x01);
|
| 775 |
+
}
|
| 776 |
+
};
|
| 777 |
+
|
| 778 |
+
} // namespace float8_internal
|
| 779 |
+
} // namespace ml_dtypes
|
| 780 |
+
|
| 781 |
+
namespace std {
|
| 782 |
+
// Standard-library overrides. Note that these are picked up by Eigen as well.
|
| 783 |
+
template <>
|
| 784 |
+
struct numeric_limits<ml_dtypes::float8_internal::float8_e4m3fn>
|
| 785 |
+
: public ml_dtypes::float8_internal::numeric_limits_float8_e4m3fn {};
|
| 786 |
+
|
| 787 |
+
template <>
|
| 788 |
+
struct numeric_limits<ml_dtypes::float8_internal::float8_e4m3b11fnuz>
|
| 789 |
+
: public ml_dtypes::float8_internal::numeric_limits_float8_e4m3b11fnuz {};
|
| 790 |
+
|
| 791 |
+
template <>
|
| 792 |
+
struct numeric_limits<ml_dtypes::float8_internal::float8_e4m3fnuz>
|
| 793 |
+
: public ml_dtypes::float8_internal::numeric_limits_float8_e4m3fnuz {};
|
| 794 |
+
|
| 795 |
+
template <>
|
| 796 |
+
struct numeric_limits<ml_dtypes::float8_internal::float8_e5m2>
|
| 797 |
+
: public ml_dtypes::float8_internal::numeric_limits_float8_e5m2 {};
|
| 798 |
+
|
| 799 |
+
template <>
|
| 800 |
+
struct numeric_limits<ml_dtypes::float8_internal::float8_e5m2fnuz>
|
| 801 |
+
: public ml_dtypes::float8_internal::numeric_limits_float8_e5m2fnuz {};
|
| 802 |
+
} // namespace std
|
| 803 |
+
|
| 804 |
+
namespace ml_dtypes {
|
| 805 |
+
namespace float8_internal {
|
| 806 |
+
|
| 807 |
+
// Free-functions for use with ADL and in Eigen.
|
| 808 |
+
constexpr inline float8_e4m3fn abs(const float8_e4m3fn& a) {
|
| 809 |
+
return float8_e4m3fn::FromRep(a.rep() & 0b0'1111'111);
|
| 810 |
+
}
|
| 811 |
+
|
| 812 |
+
constexpr inline bool(isnan)(const float8_e4m3fn& a) {
|
| 813 |
+
return abs(a).rep() == std::numeric_limits<float8_e4m3fn>::quiet_NaN().rep();
|
| 814 |
+
}
|
| 815 |
+
|
| 816 |
+
constexpr inline float8_e4m3b11fnuz abs(const float8_e4m3b11fnuz& a) {
|
| 817 |
+
return (a.rep() & 0b0'1111'111) == 0
|
| 818 |
+
? float8_e4m3b11fnuz::FromRep(a.rep())
|
| 819 |
+
: float8_e4m3b11fnuz::FromRep(a.rep() & 0b0'1111'111);
|
| 820 |
+
}
|
| 821 |
+
|
| 822 |
+
constexpr inline bool(isnan)(const float8_e4m3b11fnuz& a) {
|
| 823 |
+
return a.rep() == std::numeric_limits<float8_e4m3b11fnuz>::quiet_NaN().rep();
|
| 824 |
+
}
|
| 825 |
+
|
| 826 |
+
constexpr inline float8_e4m3fnuz abs(const float8_e4m3fnuz& a) {
|
| 827 |
+
return (a.rep() & 0x7F) == 0 ? float8_e4m3fnuz::FromRep(a.rep())
|
| 828 |
+
: float8_e4m3fnuz::FromRep(a.rep() & 0x7F);
|
| 829 |
+
}
|
| 830 |
+
|
| 831 |
+
constexpr inline bool(isnan)(const float8_e4m3fnuz& a) {
|
| 832 |
+
return abs(a).rep() ==
|
| 833 |
+
std::numeric_limits<float8_e4m3fnuz>::quiet_NaN().rep();
|
| 834 |
+
}
|
| 835 |
+
|
| 836 |
+
constexpr inline float8_e5m2 abs(const float8_e5m2& a) {
|
| 837 |
+
return float8_e5m2::FromRep(a.rep() & 0b0'11111'11);
|
| 838 |
+
}
|
| 839 |
+
|
| 840 |
+
constexpr inline bool(isnan)(const float8_e5m2& a) {
|
| 841 |
+
return abs(a).rep() > std::numeric_limits<float8_e5m2>::infinity().rep();
|
| 842 |
+
}
|
| 843 |
+
|
| 844 |
+
constexpr inline float8_e5m2fnuz abs(const float8_e5m2fnuz& a) {
|
| 845 |
+
return (a.rep() & 0x7F) == 0 ? float8_e5m2fnuz::FromRep(a.rep())
|
| 846 |
+
: float8_e5m2fnuz::FromRep(a.rep() & 0x7F);
|
| 847 |
+
}
|
| 848 |
+
|
| 849 |
+
constexpr inline bool isnan(const float8_e5m2fnuz& a) {
|
| 850 |
+
return a.rep() == 0x80;
|
| 851 |
+
}
|
| 852 |
+
|
| 853 |
+
template <typename Float8>
|
| 854 |
+
constexpr inline bool(isinf)(const float8_base<Float8>& a) {
|
| 855 |
+
return std::numeric_limits<Float8>::has_infinity
|
| 856 |
+
? abs(a.derived()).rep() ==
|
| 857 |
+
std::numeric_limits<Float8>::infinity().rep()
|
| 858 |
+
: false; // No inf representation.
|
| 859 |
+
}
|
| 860 |
+
|
| 861 |
+
template <typename Float8>
|
| 862 |
+
constexpr inline bool(isfinite)(const float8_base<Float8>& a) {
|
| 863 |
+
return !isnan(a.derived()) && !isinf(a.derived());
|
| 864 |
+
}
|
| 865 |
+
|
| 866 |
+
template <typename Float8>
|
| 867 |
+
std::ostream& operator<<(std::ostream& os, const float8_base<Float8>& f8) {
|
| 868 |
+
os << static_cast<float>(f8.derived());
|
| 869 |
+
return os;
|
| 870 |
+
}
|
| 871 |
+
|
| 872 |
+
//==============================================================================
|
| 873 |
+
// Inline conversion routines between float8 and other types.
|
| 874 |
+
//==============================================================================
|
| 875 |
+
|
| 876 |
+
// Helper for getting a bit representation provided a byte size.
|
| 877 |
+
template <int kNumBytes>
|
| 878 |
+
using GetUnsignedInteger =
|
| 879 |
+
typename Eigen::numext::get_integer_by_size<kNumBytes>::unsigned_type;
|
| 880 |
+
|
| 881 |
+
// Converts between two floating-point types.
|
| 882 |
+
template <typename From, typename To, bool kSaturate, bool kTruncate,
|
| 883 |
+
typename EnableIf = void>
|
| 884 |
+
struct ConvertImpl;
|
| 885 |
+
|
| 886 |
+
// Convert to same type. We need explicit specializations for all combinations
|
| 887 |
+
// of template parameters to avoid ambiguities.
|
| 888 |
+
template <typename Scalar>
|
| 889 |
+
struct IdentityConversion {
|
| 890 |
+
static EIGEN_DEVICE_FUNC inline Scalar run(const Scalar& from) {
|
| 891 |
+
return from;
|
| 892 |
+
}
|
| 893 |
+
};
|
| 894 |
+
|
| 895 |
+
template <typename Scalar>
|
| 896 |
+
struct ConvertImpl<Scalar, Scalar, /*kSaturate=*/false, /*kTruncate=*/false,
|
| 897 |
+
/*EnableIf=*/void> : public IdentityConversion<Scalar> {};
|
| 898 |
+
template <typename Scalar>
|
| 899 |
+
struct ConvertImpl<Scalar, Scalar, /*kSaturate=*/false, /*kTruncate=*/true,
|
| 900 |
+
/*EnableIf=*/void> : public IdentityConversion<Scalar> {};
|
| 901 |
+
template <typename Scalar>
|
| 902 |
+
struct ConvertImpl<Scalar, Scalar, /*kSaturate=*/true, /*kTruncate=*/false,
|
| 903 |
+
/*EnableIf=*/void> : public IdentityConversion<Scalar> {};
|
| 904 |
+
template <typename Scalar>
|
| 905 |
+
struct ConvertImpl<Scalar, Scalar, /*kSaturate=*/true, /*kTruncate=*/true,
|
| 906 |
+
/*EnableIf=*/void> : public IdentityConversion<Scalar> {};
|
| 907 |
+
|
| 908 |
+
template <typename Float>
|
| 909 |
+
struct TraitsBase {
|
| 910 |
+
using BitsType = GetUnsignedInteger<sizeof(Float)>;
|
| 911 |
+
static constexpr int kBits = sizeof(Float) * CHAR_BIT;
|
| 912 |
+
static constexpr int kMantissaBits = Eigen::NumTraits<Float>::digits() - 1;
|
| 913 |
+
static constexpr int kExponentBits = kBits - kMantissaBits - 1;
|
| 914 |
+
static constexpr BitsType kExponentMask = ((BitsType{1} << kExponentBits) - 1)
|
| 915 |
+
<< kMantissaBits;
|
| 916 |
+
static constexpr BitsType kMantissaMask = (BitsType{1} << kMantissaBits) - 1;
|
| 917 |
+
static constexpr int kExponentBias = (1 << (kExponentBits - 1)) - 1;
|
| 918 |
+
};
|
| 919 |
+
|
| 920 |
+
template <typename Float>
|
| 921 |
+
struct Traits : public TraitsBase<Float> {};
|
| 922 |
+
|
| 923 |
+
template <>
|
| 924 |
+
struct Traits<float8_e4m3b11fnuz> : public TraitsBase<float8_e4m3b11fnuz> {
|
| 925 |
+
static constexpr int kExponentBias = 11;
|
| 926 |
+
};
|
| 927 |
+
|
| 928 |
+
template <>
|
| 929 |
+
struct Traits<float8_e4m3fnuz> : public TraitsBase<float8_e4m3fnuz> {
|
| 930 |
+
using Base = TraitsBase<float8_e4m3fnuz>;
|
| 931 |
+
static constexpr int kExponentBias = Base::kExponentBias + 1;
|
| 932 |
+
};
|
| 933 |
+
|
| 934 |
+
template <>
|
| 935 |
+
struct Traits<float8_e5m2fnuz> : public TraitsBase<float8_e5m2fnuz> {
|
| 936 |
+
using Base = TraitsBase<float8_e5m2fnuz>;
|
| 937 |
+
static constexpr int kExponentBias = Base::kExponentBias + 1;
|
| 938 |
+
};
|
| 939 |
+
|
| 940 |
+
template <typename Bits>
|
| 941 |
+
constexpr inline Bits RoundBitsToNearestEven(Bits bits, int roundoff) {
|
| 942 |
+
// Round to nearest even by adding a bias term.
|
| 943 |
+
// Consider a bit pattern
|
| 944 |
+
// FFF...FLRTT...T,
|
| 945 |
+
// where bits RTT...T need to be rounded-off. We add a bias term to the
|
| 946 |
+
// bit pattern s.t. a carry is introduced to round up only if
|
| 947 |
+
// - L is 1, R is 1, OR
|
| 948 |
+
// - L is 0, R is 1, any T is one.
|
| 949 |
+
// We do this by adding L to a bit pattern consisting of all T = 1.
|
| 950 |
+
Bits bias = roundoff == 0
|
| 951 |
+
? 0
|
| 952 |
+
: ((bits >> roundoff) & 1) + (Bits{1} << (roundoff - 1)) - 1;
|
| 953 |
+
return bits + bias;
|
| 954 |
+
}
|
| 955 |
+
|
| 956 |
+
#if (defined(__cpp_lib_bitops) && __cpp_lib_bitops >= 201907L)
|
| 957 |
+
using std::countl_zero;
|
| 958 |
+
#else
|
| 959 |
+
static constexpr inline int countl_zero(uint64_t x) {
|
| 960 |
+
int zeroes = 60;
|
| 961 |
+
if (x >> 32) {
|
| 962 |
+
zeroes -= 32;
|
| 963 |
+
x >>= 32;
|
| 964 |
+
}
|
| 965 |
+
if (x >> 16) {
|
| 966 |
+
zeroes -= 16;
|
| 967 |
+
x >>= 16;
|
| 968 |
+
}
|
| 969 |
+
if (x >> 8) {
|
| 970 |
+
zeroes -= 8;
|
| 971 |
+
x >>= 8;
|
| 972 |
+
}
|
| 973 |
+
if (x >> 4) {
|
| 974 |
+
zeroes -= 4;
|
| 975 |
+
x >>= 4;
|
| 976 |
+
}
|
| 977 |
+
return "\4\3\2\2\1\1\1\1\0\0\0\0\0\0\0"[x] + zeroes;
|
| 978 |
+
}
|
| 979 |
+
static constexpr inline int countl_zero(uint32_t x) {
|
| 980 |
+
int zeroes = 28;
|
| 981 |
+
if (x >> 16) {
|
| 982 |
+
zeroes -= 16;
|
| 983 |
+
x >>= 16;
|
| 984 |
+
}
|
| 985 |
+
if (x >> 8) {
|
| 986 |
+
zeroes -= 8;
|
| 987 |
+
x >>= 8;
|
| 988 |
+
}
|
| 989 |
+
if (x >> 4) {
|
| 990 |
+
zeroes -= 4;
|
| 991 |
+
x >>= 4;
|
| 992 |
+
}
|
| 993 |
+
return "\4\3\2\2\1\1\1\1\0\0\0\0\0\0\0"[x] + zeroes;
|
| 994 |
+
}
|
| 995 |
+
static constexpr inline int countl_zero(uint16_t x) {
|
| 996 |
+
int zeroes = 12;
|
| 997 |
+
if (x >> 8) {
|
| 998 |
+
zeroes -= 8;
|
| 999 |
+
x >>= 8;
|
| 1000 |
+
}
|
| 1001 |
+
if (x >> 4) {
|
| 1002 |
+
zeroes -= 4;
|
| 1003 |
+
x >>= 4;
|
| 1004 |
+
}
|
| 1005 |
+
return "\4\3\2\2\1\1\1\1\0\0\0\0\0\0\0"[x] + zeroes;
|
| 1006 |
+
}
|
| 1007 |
+
static constexpr inline int countl_zero(uint8_t x) {
|
| 1008 |
+
int zeroes = 4;
|
| 1009 |
+
if (x >> 4) {
|
| 1010 |
+
zeroes -= 4;
|
| 1011 |
+
x >>= 4;
|
| 1012 |
+
}
|
| 1013 |
+
return "\4\3\2\2\1\1\1\1\0\0\0\0\0\0\0"[x] + zeroes;
|
| 1014 |
+
}
|
| 1015 |
+
#endif
|
| 1016 |
+
|
| 1017 |
+
template <typename From, typename To, bool kSaturate, bool kTruncate>
|
| 1018 |
+
struct ConvertImpl<From, To, kSaturate, kTruncate,
|
| 1019 |
+
std::enable_if_t<!std::is_same_v<From, To>>> {
|
| 1020 |
+
using FromTraits = Traits<From>;
|
| 1021 |
+
using FromBits = typename FromTraits::BitsType;
|
| 1022 |
+
static constexpr int kFromBits = FromTraits::kBits;
|
| 1023 |
+
static constexpr int kFromMantissaBits = FromTraits::kMantissaBits;
|
| 1024 |
+
static constexpr int kFromExponentBits = FromTraits::kExponentBits;
|
| 1025 |
+
static constexpr int kFromExponentBias = FromTraits::kExponentBias;
|
| 1026 |
+
static constexpr FromBits kFromExponentMask = FromTraits::kExponentMask;
|
| 1027 |
+
|
| 1028 |
+
using ToTraits = Traits<To>;
|
| 1029 |
+
using ToBits = typename ToTraits::BitsType;
|
| 1030 |
+
static constexpr int kToBits = ToTraits::kBits;
|
| 1031 |
+
static constexpr int kToMantissaBits = ToTraits::kMantissaBits;
|
| 1032 |
+
static constexpr int kToExponentBits = ToTraits::kExponentBits;
|
| 1033 |
+
static constexpr int kToExponentBias = ToTraits::kExponentBias;
|
| 1034 |
+
static constexpr ToBits kToExponentMask = ToTraits::kExponentMask;
|
| 1035 |
+
|
| 1036 |
+
// `WideBits` is wide enough to accommodate the largest exponent and mantissa
|
| 1037 |
+
// in either `From` or `To`.
|
| 1038 |
+
static constexpr int kWideBits =
|
| 1039 |
+
(std::max(kToMantissaBits, kFromMantissaBits)) + // Max significand.
|
| 1040 |
+
(std::max(kToExponentBits, kFromExponentBits)); // Max exponent.
|
| 1041 |
+
static constexpr int kWideBytes = (kWideBits + (CHAR_BIT - 1)) / CHAR_BIT;
|
| 1042 |
+
using WideBits = GetUnsignedInteger<kWideBytes>;
|
| 1043 |
+
static constexpr int kExponentOffset = kToExponentBias - kFromExponentBias;
|
| 1044 |
+
static constexpr int kDigitShift = kToMantissaBits - kFromMantissaBits;
|
| 1045 |
+
|
| 1046 |
+
static EIGEN_DEVICE_FUNC inline To run(const From& from) {
|
| 1047 |
+
// Shift bits to destination type, without sign bit.
|
| 1048 |
+
const bool from_sign_bit =
|
| 1049 |
+
Eigen::numext::bit_cast<FromBits>(from) >> (kFromBits - 1);
|
| 1050 |
+
const FromBits from_bits =
|
| 1051 |
+
Eigen::numext::bit_cast<FromBits>(Eigen::numext::abs(from));
|
| 1052 |
+
|
| 1053 |
+
// Special values, preserving sign.
|
| 1054 |
+
if (Eigen::numext::isinf(from)) {
|
| 1055 |
+
return from_sign_bit ? -Eigen::NumTraits<To>::infinity()
|
| 1056 |
+
: Eigen::NumTraits<To>::infinity();
|
| 1057 |
+
}
|
| 1058 |
+
if (Eigen::numext::isnan(from)) {
|
| 1059 |
+
return from_sign_bit ? -Eigen::NumTraits<To>::quiet_NaN()
|
| 1060 |
+
: Eigen::NumTraits<To>::quiet_NaN();
|
| 1061 |
+
}
|
| 1062 |
+
if (from_bits == 0) {
|
| 1063 |
+
return from_sign_bit ? -To{} : To{};
|
| 1064 |
+
}
|
| 1065 |
+
|
| 1066 |
+
const int biased_from_exponent = from_bits >> kFromMantissaBits;
|
| 1067 |
+
|
| 1068 |
+
// `To` supports more exponents near zero which means that some subnormal
|
| 1069 |
+
// values in `From` may become normal.
|
| 1070 |
+
if constexpr (std::numeric_limits<To>::min_exponent <
|
| 1071 |
+
std::numeric_limits<From>::min_exponent) {
|
| 1072 |
+
if (biased_from_exponent == 0) {
|
| 1073 |
+
// Subnormals.
|
| 1074 |
+
WideBits bits = from_bits;
|
| 1075 |
+
|
| 1076 |
+
// Determine exponent in target type.
|
| 1077 |
+
const int normalization_factor =
|
| 1078 |
+
countl_zero(from_bits) - (kFromBits - kFromMantissaBits) + 1;
|
| 1079 |
+
const int biased_exponent = kExponentOffset - normalization_factor + 1;
|
| 1080 |
+
if (biased_exponent <= 0) {
|
| 1081 |
+
// Result is subnormal. Adjust the subnormal bits to account for
|
| 1082 |
+
// the difference in exponent bias.
|
| 1083 |
+
if constexpr (kExponentOffset < sizeof(WideBits) * CHAR_BIT) {
|
| 1084 |
+
bits <<= kExponentOffset;
|
| 1085 |
+
}
|
| 1086 |
+
} else {
|
| 1087 |
+
// Result is normal. Shift the mantissa to account for the number of
|
| 1088 |
+
// leading zero digits, and clear the hidden bit.
|
| 1089 |
+
bits <<= normalization_factor;
|
| 1090 |
+
bits &= ~(WideBits{1} << kFromMantissaBits);
|
| 1091 |
+
// Insert the exponent bits.
|
| 1092 |
+
bits |= static_cast<WideBits>(biased_exponent) << kFromMantissaBits;
|
| 1093 |
+
}
|
| 1094 |
+
|
| 1095 |
+
// Truncate/round mantissa if necessary.
|
| 1096 |
+
if constexpr (kDigitShift > 0) {
|
| 1097 |
+
bits <<= kDigitShift;
|
| 1098 |
+
} else {
|
| 1099 |
+
if constexpr (!kTruncate) {
|
| 1100 |
+
bits = RoundBitsToNearestEven(bits, -kDigitShift);
|
| 1101 |
+
}
|
| 1102 |
+
bits >>= -kDigitShift;
|
| 1103 |
+
}
|
| 1104 |
+
To to = Eigen::numext::bit_cast<To>(static_cast<ToBits>(bits));
|
| 1105 |
+
return from_sign_bit ? -to : to;
|
| 1106 |
+
}
|
| 1107 |
+
}
|
| 1108 |
+
// `To` supports fewer exponents near zero which means that some values in
|
| 1109 |
+
// `From` may become subnormal.
|
| 1110 |
+
if constexpr (std::numeric_limits<To>::min_exponent >
|
| 1111 |
+
std::numeric_limits<From>::min_exponent) {
|
| 1112 |
+
const int unbiased_exponent = biased_from_exponent - kFromExponentBias;
|
| 1113 |
+
const int biased_to_exponent = unbiased_exponent + kToExponentBias;
|
| 1114 |
+
// Subnormals and zero.
|
| 1115 |
+
if (biased_to_exponent <= 0) {
|
| 1116 |
+
// Round and shift mantissa down.
|
| 1117 |
+
FromBits from_has_leading_one = (biased_from_exponent > 0 ? 1 : 0);
|
| 1118 |
+
int exponent_shift =
|
| 1119 |
+
-kDigitShift - biased_to_exponent + from_has_leading_one;
|
| 1120 |
+
// Insert the implicit leading 1 bit on the mantissa for normalized
|
| 1121 |
+
// inputs.
|
| 1122 |
+
FromBits rounded_from_bits =
|
| 1123 |
+
(from_bits & FromTraits::kMantissaMask) |
|
| 1124 |
+
(from_has_leading_one << kFromMantissaBits);
|
| 1125 |
+
ToBits bits = 0;
|
| 1126 |
+
// To avoid UB, limit rounding and shifting to the full mantissa plus
|
| 1127 |
+
// leading 1.
|
| 1128 |
+
if (exponent_shift <= kFromMantissaBits + 1) {
|
| 1129 |
+
if constexpr (!kTruncate) {
|
| 1130 |
+
// NOTE: we need to round again from the original from_bits,
|
| 1131 |
+
// otherwise the lower precision bits may already be lost. There is
|
| 1132 |
+
// an edge-case where rounding to a normalized value would normally
|
| 1133 |
+
// round down, but for a subnormal, we need to round up.
|
| 1134 |
+
rounded_from_bits =
|
| 1135 |
+
RoundBitsToNearestEven(rounded_from_bits, exponent_shift);
|
| 1136 |
+
}
|
| 1137 |
+
bits = (rounded_from_bits >> exponent_shift);
|
| 1138 |
+
}
|
| 1139 |
+
// Insert sign and return.
|
| 1140 |
+
To to = Eigen::numext::bit_cast<To>(bits);
|
| 1141 |
+
return from_sign_bit ? -to : to;
|
| 1142 |
+
}
|
| 1143 |
+
}
|
| 1144 |
+
|
| 1145 |
+
// Round the mantissa if it is shrinking.
|
| 1146 |
+
WideBits rounded_from_bits = from_bits;
|
| 1147 |
+
if constexpr (kDigitShift < 0) {
|
| 1148 |
+
if constexpr (!kTruncate) {
|
| 1149 |
+
rounded_from_bits = RoundBitsToNearestEven(from_bits, -kDigitShift);
|
| 1150 |
+
}
|
| 1151 |
+
// Zero-out tail bits.
|
| 1152 |
+
rounded_from_bits &= ~((WideBits{1} << (-kDigitShift)) - 1);
|
| 1153 |
+
}
|
| 1154 |
+
|
| 1155 |
+
// Re-bias the exponent.
|
| 1156 |
+
rounded_from_bits += static_cast<WideBits>(kExponentOffset)
|
| 1157 |
+
<< kFromMantissaBits;
|
| 1158 |
+
|
| 1159 |
+
ToBits bits;
|
| 1160 |
+
// Check for overflows by aligning the significands. We always align the
|
| 1161 |
+
// narrower significand to the wider significand.
|
| 1162 |
+
const WideBits kToHighestRep =
|
| 1163 |
+
Eigen::numext::bit_cast<ToBits>(Eigen::NumTraits<To>::highest());
|
| 1164 |
+
WideBits aligned_highest{kToHighestRep};
|
| 1165 |
+
if constexpr (kDigitShift < 0) {
|
| 1166 |
+
aligned_highest <<= -kDigitShift;
|
| 1167 |
+
// Shift down, all dropped bits should already be zero.
|
| 1168 |
+
bits = static_cast<ToBits>(rounded_from_bits >> -kDigitShift);
|
| 1169 |
+
} else if constexpr (kDigitShift >= 0) {
|
| 1170 |
+
// Shift up, inserting zeros in the newly created digits.
|
| 1171 |
+
rounded_from_bits <<= kDigitShift;
|
| 1172 |
+
bits = ToBits{rounded_from_bits};
|
| 1173 |
+
}
|
| 1174 |
+
|
| 1175 |
+
To to = Eigen::numext::bit_cast<To>(bits);
|
| 1176 |
+
// `From` supports larger values than `To`, we may overflow.
|
| 1177 |
+
if constexpr (std::make_pair(std::numeric_limits<To>::max_exponent,
|
| 1178 |
+
std::numeric_limits<To>::digits) <
|
| 1179 |
+
std::make_pair(std::numeric_limits<From>::max_exponent,
|
| 1180 |
+
std::numeric_limits<From>::digits)) {
|
| 1181 |
+
if (rounded_from_bits > aligned_highest) {
|
| 1182 |
+
// Overflowed values map to highest or infinity depending on kSaturate.
|
| 1183 |
+
to = kSaturate ? Eigen::NumTraits<To>::highest()
|
| 1184 |
+
: Eigen::NumTraits<To>::infinity();
|
| 1185 |
+
}
|
| 1186 |
+
}
|
| 1187 |
+
// Insert sign bit.
|
| 1188 |
+
return from_sign_bit ? -to : to;
|
| 1189 |
+
}
|
| 1190 |
+
};
|
| 1191 |
+
|
| 1192 |
+
// Saturation has no impact when casting e4m3 to e5m2.
|
| 1193 |
+
template <bool kTruncate>
|
| 1194 |
+
struct ConvertImpl<float8_e4m3fn, float8_e5m2, true, kTruncate> {
|
| 1195 |
+
static EIGEN_DEVICE_FUNC inline float8_e5m2 run(const float8_e4m3fn& from) {
|
| 1196 |
+
return ConvertImpl<float8_e4m3fn, float8_e5m2, false, kTruncate>::run(from);
|
| 1197 |
+
}
|
| 1198 |
+
};
|
| 1199 |
+
|
| 1200 |
+
template <bool kSaturate, bool kTruncate>
|
| 1201 |
+
struct ConvertImpl<Eigen::half, float8_e5m2, kSaturate, kTruncate> {
|
| 1202 |
+
static EIGEN_DEVICE_FUNC inline float8_e5m2 run(const Eigen::half& from) {
|
| 1203 |
+
uint16_t from_bits = Eigen::numext::bit_cast<uint16_t>(from);
|
| 1204 |
+
|
| 1205 |
+
// Special values (Inf or NaN).
|
| 1206 |
+
uint16_t abs_bits = from_bits & 0x7FFF;
|
| 1207 |
+
if (abs_bits == 0x7C00) {
|
| 1208 |
+
return float8_e5m2::FromRep(from_bits >> 8);
|
| 1209 |
+
} else if (abs_bits > 0x7C00) {
|
| 1210 |
+
// IEEE 754-2019 6.2.1: "A quiet NaN bit string should be encoded with the
|
| 1211 |
+
// first bit (d1) of the trailing significand field T being 1."
|
| 1212 |
+
// IEEE 754-2019 6.2.3: "Conversion of a quiet NaN to a floating-point
|
| 1213 |
+
// format of the same or a different radix that does not allow the payload
|
| 1214 |
+
// to be preserved, shall return a quiet NaN [...]"
|
| 1215 |
+
return float8_e5m2::FromRep((from_bits >> 8) | 0b0'00000'10);
|
| 1216 |
+
}
|
| 1217 |
+
|
| 1218 |
+
if constexpr (!kTruncate) {
|
| 1219 |
+
from_bits = RoundBitsToNearestEven(from_bits, 8);
|
| 1220 |
+
// Rounding can cause an overflow to infinity. Clamp to the largest finite
|
| 1221 |
+
// value if saturation is requested.
|
| 1222 |
+
if constexpr (kSaturate) {
|
| 1223 |
+
const float8_e5m2 kHighest = Eigen::NumTraits<float8_e5m2>::highest();
|
| 1224 |
+
if ((from_bits & 0x7F00) > static_cast<uint16_t>(kHighest.rep()) << 8) {
|
| 1225 |
+
const bool from_sign_bit = from_bits >> 15;
|
| 1226 |
+
return from_sign_bit ? -kHighest : kHighest;
|
| 1227 |
+
}
|
| 1228 |
+
}
|
| 1229 |
+
}
|
| 1230 |
+
return float8_e5m2::FromRep(from_bits >> 8);
|
| 1231 |
+
}
|
| 1232 |
+
};
|
| 1233 |
+
|
| 1234 |
+
template <>
|
| 1235 |
+
struct ConvertImpl<float8_e5m2, Eigen::half, /*kSaturate=*/false,
|
| 1236 |
+
/*kTruncate=*/false> {
|
| 1237 |
+
static EIGEN_DEVICE_FUNC inline Eigen::half run(const float8_e5m2& from) {
|
| 1238 |
+
return Eigen::numext::bit_cast<Eigen::half>(
|
| 1239 |
+
static_cast<uint16_t>(static_cast<uint16_t>(from.rep()) << 8));
|
| 1240 |
+
}
|
| 1241 |
+
};
|
| 1242 |
+
|
| 1243 |
+
// Direct casts of e5m2 to Eigen::half simply shifts bits over.
|
| 1244 |
+
template <bool kSaturate, bool kTruncate>
|
| 1245 |
+
struct ConvertImpl<float8_e5m2, Eigen::half, kSaturate, kTruncate> {
|
| 1246 |
+
static EIGEN_DEVICE_FUNC inline Eigen::half run(const float8_e5m2& from) {
|
| 1247 |
+
return Eigen::numext::bit_cast<Eigen::half>(
|
| 1248 |
+
static_cast<uint16_t>(static_cast<uint16_t>(from.rep()) << 8));
|
| 1249 |
+
}
|
| 1250 |
+
};
|
| 1251 |
+
|
| 1252 |
+
template <typename Derived>
|
| 1253 |
+
template <bool kSaturate, bool kTruncate, typename From>
|
| 1254 |
+
EIGEN_DEVICE_FUNC Derived float8_base<Derived>::ConvertFrom(const From& from) {
|
| 1255 |
+
return ConvertImpl<From, Derived, kSaturate, kTruncate>::run(from);
|
| 1256 |
+
}
|
| 1257 |
+
|
| 1258 |
+
template <typename Derived>
|
| 1259 |
+
template <typename To, bool kSaturate, bool kTruncate>
|
| 1260 |
+
EIGEN_DEVICE_FUNC To float8_base<Derived>::ConvertTo(const Derived& from) {
|
| 1261 |
+
return ConvertImpl<Derived, To, kSaturate, kTruncate>::run(from);
|
| 1262 |
+
}
|
| 1263 |
+
|
| 1264 |
+
} // namespace float8_internal
|
| 1265 |
+
|
| 1266 |
+
// Exported types.
|
| 1267 |
+
using float8_e4m3fn = float8_internal::float8_e4m3fn;
|
| 1268 |
+
using float8_e4m3fnuz = float8_internal::float8_e4m3fnuz;
|
| 1269 |
+
using float8_e4m3b11fnuz = float8_internal::float8_e4m3b11fnuz;
|
| 1270 |
+
using float8_e5m2 = float8_internal::float8_e5m2;
|
| 1271 |
+
using float8_e5m2fnuz = float8_internal::float8_e5m2fnuz;
|
| 1272 |
+
|
| 1273 |
+
} // namespace ml_dtypes
|
| 1274 |
+
|
| 1275 |
+
// Eigen-specific overrides.
|
| 1276 |
+
namespace Eigen {
|
| 1277 |
+
namespace numext {
|
| 1278 |
+
|
| 1279 |
+
template <>
|
| 1280 |
+
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC ml_dtypes::float8_e4m3fn
|
| 1281 |
+
bit_cast<ml_dtypes::float8_e4m3fn, uint8_t>(const uint8_t& src) {
|
| 1282 |
+
return ml_dtypes::float8_e4m3fn::FromRep(src);
|
| 1283 |
+
}
|
| 1284 |
+
|
| 1285 |
+
template <>
|
| 1286 |
+
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC uint8_t
|
| 1287 |
+
bit_cast<uint8_t, ml_dtypes::float8_e4m3fn>(
|
| 1288 |
+
const ml_dtypes::float8_e4m3fn& src) {
|
| 1289 |
+
return src.rep();
|
| 1290 |
+
}
|
| 1291 |
+
|
| 1292 |
+
template <>
|
| 1293 |
+
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC ml_dtypes::float8_e5m2
|
| 1294 |
+
bit_cast<ml_dtypes::float8_e5m2, uint8_t>(const uint8_t& src) {
|
| 1295 |
+
return ml_dtypes::float8_e5m2::FromRep(src);
|
| 1296 |
+
}
|
| 1297 |
+
|
| 1298 |
+
template <>
|
| 1299 |
+
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC uint8_t
|
| 1300 |
+
bit_cast<uint8_t, ml_dtypes::float8_e5m2>(const ml_dtypes::float8_e5m2& src) {
|
| 1301 |
+
return src.rep();
|
| 1302 |
+
}
|
| 1303 |
+
|
| 1304 |
+
} // namespace numext
|
| 1305 |
+
|
| 1306 |
+
// Work-around for isinf/isnan/isfinite issue on aarch64.
|
| 1307 |
+
namespace internal {
|
| 1308 |
+
template <>
|
| 1309 |
+
EIGEN_DEVICE_FUNC inline bool isinf_impl<ml_dtypes::float8_e4m3fn>(
|
| 1310 |
+
const ml_dtypes::float8_e4m3fn& x) {
|
| 1311 |
+
return ml_dtypes::float8_internal::isinf(x);
|
| 1312 |
+
}
|
| 1313 |
+
|
| 1314 |
+
template <>
|
| 1315 |
+
EIGEN_DEVICE_FUNC inline bool isinf_impl<ml_dtypes::float8_e4m3b11fnuz>(
|
| 1316 |
+
const ml_dtypes::float8_e4m3b11fnuz& x) {
|
| 1317 |
+
return ml_dtypes::float8_internal::isinf(x);
|
| 1318 |
+
}
|
| 1319 |
+
|
| 1320 |
+
template <>
|
| 1321 |
+
EIGEN_DEVICE_FUNC inline bool isinf_impl<ml_dtypes::float8_e4m3fnuz>(
|
| 1322 |
+
const ml_dtypes::float8_e4m3fnuz& x) {
|
| 1323 |
+
return ml_dtypes::float8_internal::isinf(x);
|
| 1324 |
+
}
|
| 1325 |
+
|
| 1326 |
+
template <>
|
| 1327 |
+
EIGEN_DEVICE_FUNC inline bool isinf_impl<ml_dtypes::float8_e5m2>(
|
| 1328 |
+
const ml_dtypes::float8_e5m2& x) {
|
| 1329 |
+
return ml_dtypes::float8_internal::isinf(x);
|
| 1330 |
+
}
|
| 1331 |
+
|
| 1332 |
+
template <>
|
| 1333 |
+
EIGEN_DEVICE_FUNC inline bool isinf_impl<ml_dtypes::float8_e5m2fnuz>(
|
| 1334 |
+
const ml_dtypes::float8_e5m2fnuz& x) {
|
| 1335 |
+
return ml_dtypes::float8_internal::isinf(x);
|
| 1336 |
+
}
|
| 1337 |
+
|
| 1338 |
+
template <>
|
| 1339 |
+
EIGEN_DEVICE_FUNC inline bool isnan_impl<ml_dtypes::float8_e4m3fn>(
|
| 1340 |
+
const ml_dtypes::float8_e4m3fn& x) {
|
| 1341 |
+
return ml_dtypes::float8_internal::isnan(x);
|
| 1342 |
+
}
|
| 1343 |
+
|
| 1344 |
+
template <>
|
| 1345 |
+
EIGEN_DEVICE_FUNC inline bool isnan_impl<ml_dtypes::float8_e4m3b11fnuz>(
|
| 1346 |
+
const ml_dtypes::float8_e4m3b11fnuz& x) {
|
| 1347 |
+
return ml_dtypes::float8_internal::isnan(x);
|
| 1348 |
+
}
|
| 1349 |
+
|
| 1350 |
+
template <>
|
| 1351 |
+
EIGEN_DEVICE_FUNC inline bool isnan_impl<ml_dtypes::float8_e4m3fnuz>(
|
| 1352 |
+
const ml_dtypes::float8_e4m3fnuz& x) {
|
| 1353 |
+
return ml_dtypes::float8_internal::isnan(x);
|
| 1354 |
+
}
|
| 1355 |
+
|
| 1356 |
+
template <>
|
| 1357 |
+
EIGEN_DEVICE_FUNC inline bool isnan_impl<ml_dtypes::float8_e5m2>(
|
| 1358 |
+
const ml_dtypes::float8_e5m2& x) {
|
| 1359 |
+
return ml_dtypes::float8_internal::isnan(x);
|
| 1360 |
+
}
|
| 1361 |
+
|
| 1362 |
+
template <>
|
| 1363 |
+
EIGEN_DEVICE_FUNC inline bool isnan_impl<ml_dtypes::float8_e5m2fnuz>(
|
| 1364 |
+
const ml_dtypes::float8_e5m2fnuz& x) {
|
| 1365 |
+
return ml_dtypes::float8_internal::isnan(x);
|
| 1366 |
+
}
|
| 1367 |
+
|
| 1368 |
+
template <>
|
| 1369 |
+
EIGEN_DEVICE_FUNC inline bool isfinite_impl<ml_dtypes::float8_e4m3fn>(
|
| 1370 |
+
const ml_dtypes::float8_e4m3fn& x) {
|
| 1371 |
+
return ml_dtypes::float8_internal::isfinite(x);
|
| 1372 |
+
}
|
| 1373 |
+
|
| 1374 |
+
template <>
|
| 1375 |
+
EIGEN_DEVICE_FUNC inline bool isfinite_impl<ml_dtypes::float8_e4m3b11fnuz>(
|
| 1376 |
+
const ml_dtypes::float8_e4m3b11fnuz& x) {
|
| 1377 |
+
return ml_dtypes::float8_internal::isfinite(x);
|
| 1378 |
+
}
|
| 1379 |
+
|
| 1380 |
+
template <>
|
| 1381 |
+
EIGEN_DEVICE_FUNC inline bool isfinite_impl<ml_dtypes::float8_e4m3fnuz>(
|
| 1382 |
+
const ml_dtypes::float8_e4m3fnuz& x) {
|
| 1383 |
+
return ml_dtypes::float8_internal::isfinite(x);
|
| 1384 |
+
}
|
| 1385 |
+
|
| 1386 |
+
template <>
|
| 1387 |
+
EIGEN_DEVICE_FUNC inline bool isfinite_impl<ml_dtypes::float8_e5m2>(
|
| 1388 |
+
const ml_dtypes::float8_e5m2& x) {
|
| 1389 |
+
return ml_dtypes::float8_internal::isfinite(x);
|
| 1390 |
+
}
|
| 1391 |
+
|
| 1392 |
+
template <>
|
| 1393 |
+
EIGEN_DEVICE_FUNC inline bool isfinite_impl<ml_dtypes::float8_e5m2fnuz>(
|
| 1394 |
+
const ml_dtypes::float8_e5m2fnuz& x) {
|
| 1395 |
+
return ml_dtypes::float8_internal::isfinite(x);
|
| 1396 |
+
}
|
| 1397 |
+
|
| 1398 |
+
} // namespace internal
|
| 1399 |
+
} // namespace Eigen
|
| 1400 |
+
|
| 1401 |
+
#endif // ML_DTYPES_FLOAT8_H_
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/include/external/ml_dtypes/include/int4.h
ADDED
|
@@ -0,0 +1,262 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2023 The ml_dtypes Authors
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
|
| 16 |
+
#ifndef ML_DTYPES_INT4_H_
|
| 17 |
+
#define ML_DTYPES_INT4_H_
|
| 18 |
+
|
| 19 |
+
#include <cstdint>
|
| 20 |
+
#include <limits>
|
| 21 |
+
#include <optional>
|
| 22 |
+
#include <ostream>
|
| 23 |
+
#include <sstream>
|
| 24 |
+
#include <string>
|
| 25 |
+
|
| 26 |
+
namespace ml_dtypes {
|
| 27 |
+
|
| 28 |
+
template <typename UnderlyingTy>
|
| 29 |
+
struct i4 {
|
| 30 |
+
private:
|
| 31 |
+
UnderlyingTy v : 4;
|
| 32 |
+
|
| 33 |
+
public:
|
| 34 |
+
constexpr i4() : v(0) {}
|
| 35 |
+
constexpr i4(const i4& other) = default;
|
| 36 |
+
constexpr i4(i4&& other) = default;
|
| 37 |
+
constexpr i4& operator=(const i4& other) = default;
|
| 38 |
+
constexpr i4& operator=(i4&&) = default;
|
| 39 |
+
|
| 40 |
+
explicit constexpr i4(UnderlyingTy val) : v(val & 0x0F) {}
|
| 41 |
+
template <typename T>
|
| 42 |
+
explicit constexpr i4(T t) : i4(static_cast<UnderlyingTy>(t)) {}
|
| 43 |
+
|
| 44 |
+
static constexpr i4 lowest() {
|
| 45 |
+
return std::is_signed<UnderlyingTy>::value ? i4(-8) : i4(0);
|
| 46 |
+
}
|
| 47 |
+
static constexpr i4 highest() {
|
| 48 |
+
return std::is_signed<UnderlyingTy>::value ? i4(7) : i4(15);
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
template <typename T>
|
| 52 |
+
explicit constexpr operator T() const {
|
| 53 |
+
return static_cast<T>(v);
|
| 54 |
+
}
|
| 55 |
+
// NOLINTNEXTLINE(google-explicit-constructor)
|
| 56 |
+
constexpr operator std::optional<int64_t>() const {
|
| 57 |
+
return static_cast<int64_t>(v);
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
constexpr i4 operator-() const { return i4(-v); }
|
| 61 |
+
constexpr i4 operator+(const i4& other) const { return i4((v + other.v)); }
|
| 62 |
+
constexpr i4 operator-(const i4& other) const { return i4((v - other.v)); }
|
| 63 |
+
constexpr i4 operator*(const i4& other) const { return i4((v * other.v)); }
|
| 64 |
+
constexpr i4 operator/(const i4& other) const { return i4((v / other.v)); }
|
| 65 |
+
constexpr i4 operator%(const i4& other) const { return i4((v % other.v)); }
|
| 66 |
+
|
| 67 |
+
constexpr i4 operator&(const i4& other) const { return i4((v & other.v)); }
|
| 68 |
+
constexpr i4 operator|(const i4& other) const { return i4((v | other.v)); }
|
| 69 |
+
constexpr i4 operator^(const i4& other) const { return i4((v ^ other.v)); }
|
| 70 |
+
constexpr i4 operator~() const { return i4(~v); }
|
| 71 |
+
constexpr i4 operator>>(int amount) const { return i4((v >> amount)); }
|
| 72 |
+
constexpr i4 operator<<(int amount) const { return i4((v << amount)); }
|
| 73 |
+
|
| 74 |
+
constexpr bool operator==(const i4& other) const { return v == other.v; }
|
| 75 |
+
constexpr bool operator!=(const i4& other) const { return v != other.v; }
|
| 76 |
+
constexpr bool operator<(const i4& other) const { return v < other.v; }
|
| 77 |
+
constexpr bool operator>(const i4& other) const { return v > other.v; }
|
| 78 |
+
constexpr bool operator<=(const i4& other) const { return v <= other.v; }
|
| 79 |
+
constexpr bool operator>=(const i4& other) const { return v >= other.v; }
|
| 80 |
+
|
| 81 |
+
constexpr bool operator==(int64_t other) const { return v == other; }
|
| 82 |
+
constexpr bool operator!=(int64_t other) const { return v != other; }
|
| 83 |
+
constexpr bool operator<(int64_t other) const { return v < other; }
|
| 84 |
+
constexpr bool operator>(int64_t other) const { return v > other; }
|
| 85 |
+
constexpr bool operator<=(int64_t other) const { return v <= other; }
|
| 86 |
+
constexpr bool operator>=(int64_t other) const { return v >= other; }
|
| 87 |
+
|
| 88 |
+
friend constexpr bool operator==(int64_t a, const i4& b) { return a == b.v; }
|
| 89 |
+
friend constexpr bool operator!=(int64_t a, const i4& b) { return a != b.v; }
|
| 90 |
+
friend constexpr bool operator<(int64_t a, const i4& b) { return a < b.v; }
|
| 91 |
+
friend constexpr bool operator>(int64_t a, const i4& b) { return a > b.v; }
|
| 92 |
+
friend constexpr bool operator<=(int64_t a, const i4& b) { return a <= b.v; }
|
| 93 |
+
friend constexpr bool operator>=(int64_t a, const i4& b) { return a >= b.v; }
|
| 94 |
+
|
| 95 |
+
constexpr i4& operator++() {
|
| 96 |
+
v = (v + 1) & 0x0F;
|
| 97 |
+
return *this;
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
constexpr i4 operator++(int) {
|
| 101 |
+
i4 orig = *this;
|
| 102 |
+
this->operator++();
|
| 103 |
+
return orig;
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
constexpr i4& operator--() {
|
| 107 |
+
v = (v - 1) & 0x0F;
|
| 108 |
+
return *this;
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
constexpr i4 operator--(int) {
|
| 112 |
+
i4 orig = *this;
|
| 113 |
+
this->operator--();
|
| 114 |
+
return orig;
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
constexpr i4& operator+=(const i4& other) {
|
| 118 |
+
*this = *this + other;
|
| 119 |
+
return *this;
|
| 120 |
+
}
|
| 121 |
+
constexpr i4& operator-=(const i4& other) {
|
| 122 |
+
*this = *this - other;
|
| 123 |
+
return *this;
|
| 124 |
+
}
|
| 125 |
+
constexpr i4& operator*=(const i4& other) {
|
| 126 |
+
*this = *this * other;
|
| 127 |
+
return *this;
|
| 128 |
+
}
|
| 129 |
+
constexpr i4& operator/=(const i4& other) {
|
| 130 |
+
*this = *this / other;
|
| 131 |
+
return *this;
|
| 132 |
+
}
|
| 133 |
+
constexpr i4& operator%=(const i4& other) {
|
| 134 |
+
*this = *this % other;
|
| 135 |
+
return *this;
|
| 136 |
+
}
|
| 137 |
+
constexpr i4& operator&=(const i4& other) {
|
| 138 |
+
*this = *this & other;
|
| 139 |
+
return *this;
|
| 140 |
+
}
|
| 141 |
+
constexpr i4& operator|=(const i4& other) {
|
| 142 |
+
*this = *this | other;
|
| 143 |
+
return *this;
|
| 144 |
+
}
|
| 145 |
+
constexpr i4& operator^=(const i4& other) {
|
| 146 |
+
*this = *this ^ other;
|
| 147 |
+
return *this;
|
| 148 |
+
}
|
| 149 |
+
constexpr i4& operator>>=(int amount) {
|
| 150 |
+
*this = *this >> amount;
|
| 151 |
+
return *this;
|
| 152 |
+
}
|
| 153 |
+
constexpr i4& operator<<=(int amount) {
|
| 154 |
+
*this = *this << amount;
|
| 155 |
+
return *this;
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
friend ::std::ostream& operator<<(::std::ostream& os, const i4& num) {
|
| 159 |
+
os << static_cast<int16_t>(num.v);
|
| 160 |
+
return os;
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
std::string ToString() const {
|
| 164 |
+
std::ostringstream os;
|
| 165 |
+
os << static_cast<int16_t>(v);
|
| 166 |
+
return os.str();
|
| 167 |
+
}
|
| 168 |
+
};
|
| 169 |
+
|
| 170 |
+
using int4 = i4<int8_t>;
|
| 171 |
+
using uint4 = i4<uint8_t>;
|
| 172 |
+
|
| 173 |
+
namespace internal {
|
| 174 |
+
|
| 175 |
+
struct int4_numeric_limits_base {
|
| 176 |
+
static inline constexpr const bool is_specialized = true;
|
| 177 |
+
static inline constexpr const bool is_integer = true;
|
| 178 |
+
static inline constexpr const bool is_exact = true;
|
| 179 |
+
static inline constexpr const bool has_infinity = false;
|
| 180 |
+
static inline constexpr const bool has_quiet_NaN = false;
|
| 181 |
+
static inline constexpr const bool has_signaling_NaN = false;
|
| 182 |
+
static inline constexpr const std::float_denorm_style has_denorm =
|
| 183 |
+
std::denorm_absent;
|
| 184 |
+
static inline constexpr const bool has_denorm_loss = false;
|
| 185 |
+
static inline constexpr const std::float_round_style round_style =
|
| 186 |
+
std::round_toward_zero;
|
| 187 |
+
static inline constexpr const bool is_iec559 = false;
|
| 188 |
+
static inline constexpr const bool is_bounded = true;
|
| 189 |
+
static inline constexpr const int max_digits10 = 0; // Not used for integers.
|
| 190 |
+
static inline constexpr const int radix = 2;
|
| 191 |
+
static inline constexpr const int min_exponent = 0;
|
| 192 |
+
static inline constexpr const int min_exponent10 = 0;
|
| 193 |
+
static inline constexpr const int max_exponent = 0;
|
| 194 |
+
static inline constexpr const int max_exponent10 = 0;
|
| 195 |
+
static inline constexpr const bool traps = true;
|
| 196 |
+
static inline constexpr const bool tinyness_before = false;
|
| 197 |
+
|
| 198 |
+
static constexpr ml_dtypes::int4 epsilon() noexcept {
|
| 199 |
+
return ml_dtypes::int4(0);
|
| 200 |
+
}
|
| 201 |
+
static constexpr ml_dtypes::int4 round_error() noexcept {
|
| 202 |
+
return ml_dtypes::int4(0);
|
| 203 |
+
}
|
| 204 |
+
static constexpr ml_dtypes::int4 infinity() noexcept {
|
| 205 |
+
return ml_dtypes::int4(0);
|
| 206 |
+
}
|
| 207 |
+
static constexpr ml_dtypes::int4 quiet_NaN() noexcept {
|
| 208 |
+
return ml_dtypes::int4(0);
|
| 209 |
+
}
|
| 210 |
+
static constexpr ml_dtypes::int4 signaling_NaN() noexcept {
|
| 211 |
+
return ml_dtypes::int4(0);
|
| 212 |
+
}
|
| 213 |
+
static constexpr ml_dtypes::int4 denorm_min() noexcept {
|
| 214 |
+
return ml_dtypes::int4(0);
|
| 215 |
+
}
|
| 216 |
+
};
|
| 217 |
+
|
| 218 |
+
} // namespace internal
|
| 219 |
+
|
| 220 |
+
} // namespace ml_dtypes
|
| 221 |
+
|
| 222 |
+
namespace std {
|
| 223 |
+
|
| 224 |
+
template <>
|
| 225 |
+
struct numeric_limits<ml_dtypes::int4>
|
| 226 |
+
: public ml_dtypes::internal::int4_numeric_limits_base {
|
| 227 |
+
static inline constexpr const bool is_signed = true;
|
| 228 |
+
static inline constexpr const bool is_modulo = false;
|
| 229 |
+
static inline constexpr const int digits = 3;
|
| 230 |
+
static inline constexpr const int digits10 = 0; // floor(3 * log10(2))
|
| 231 |
+
static constexpr ml_dtypes::int4 min() noexcept {
|
| 232 |
+
return ml_dtypes::int4::lowest();
|
| 233 |
+
}
|
| 234 |
+
static constexpr ml_dtypes::int4 lowest() noexcept {
|
| 235 |
+
return ml_dtypes::int4::lowest();
|
| 236 |
+
}
|
| 237 |
+
static constexpr ml_dtypes::int4 max() noexcept {
|
| 238 |
+
return ml_dtypes::int4::highest();
|
| 239 |
+
}
|
| 240 |
+
};
|
| 241 |
+
|
| 242 |
+
template <>
|
| 243 |
+
struct numeric_limits<ml_dtypes::uint4>
|
| 244 |
+
: public ml_dtypes::internal::int4_numeric_limits_base {
|
| 245 |
+
static inline constexpr const bool is_signed = false;
|
| 246 |
+
static inline constexpr const bool is_modulo = true;
|
| 247 |
+
static inline constexpr const int digits = 4;
|
| 248 |
+
static inline constexpr const int digits10 = 1; // floor(4 * log10(2))
|
| 249 |
+
static constexpr ml_dtypes::uint4 min() noexcept {
|
| 250 |
+
return ml_dtypes::uint4::lowest();
|
| 251 |
+
}
|
| 252 |
+
static constexpr ml_dtypes::uint4 lowest() noexcept {
|
| 253 |
+
return ml_dtypes::uint4::lowest();
|
| 254 |
+
}
|
| 255 |
+
static constexpr ml_dtypes::uint4 max() noexcept {
|
| 256 |
+
return ml_dtypes::uint4::highest();
|
| 257 |
+
}
|
| 258 |
+
};
|
| 259 |
+
|
| 260 |
+
} // nam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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
#ifndef TENSORFLOW_LITE_KERNELS_SHIM_OP_KERNEL_H_
|
| 16 |
+
#define TENSORFLOW_LITE_KERNELS_SHIM_OP_KERNEL_H_
|
| 17 |
+
|
| 18 |
+
// This file defines a shim layer on top of TF and TFLite custom op APIs.
|
| 19 |
+
// The goal is for a custom op to be written once and used for both runtimes
|
| 20 |
+
//
|
| 21 |
+
// It consists of two pieces:
|
| 22 |
+
// * A set of *context* interfaces:
|
| 23 |
+
// ** InvokeContext, InitContext, ShapeInferenceContext
|
| 24 |
+
// These are passed on to the custom op implementation to read/write
|
| 25 |
+
// tensors, etc.
|
| 26 |
+
//
|
| 27 |
+
// * An OpKernelShim interface:
|
| 28 |
+
// This is what a custom op needs to implement. By using that interface the
|
| 29 |
+
// custom op can then be easily converted to both a TF op kernel and a
|
| 30 |
+
// TFLite op kernel.
|
| 31 |
+
|
| 32 |
+
#include <cstdint>
|
| 33 |
+
#include <memory>
|
| 34 |
+
#include <string>
|
| 35 |
+
|
| 36 |
+
#include "absl/status/status.h"
|
| 37 |
+
#include "absl/status/statusor.h"
|
| 38 |
+
#include "absl/strings/string_view.h"
|
| 39 |
+
#include "absl/types/variant.h"
|
| 40 |
+
#include "tensorflow/lite/kernels/shim/shape.h"
|
| 41 |
+
#include "tensorflow/lite/kernels/shim/status_macros.h"
|
| 42 |
+
#include "tensorflow/lite/kernels/shim/tensor_view.h"
|
| 43 |
+
|
| 44 |
+
namespace tflite {
|
| 45 |
+
namespace shim {
|
| 46 |
+
|
| 47 |
+
// List of the TF custom op APIs this shim library is abstracting away.
|
| 48 |
+
//
|
| 49 |
+
// This enum is used as the template parameter in various places in
|
| 50 |
+
// order to pick the correct set of types (eg. TfInvokeContext vs.
|
| 51 |
+
// TfLiteInvokeContext) in the op implementation.
|
| 52 |
+
enum class Runtime { kTf, kTfLite };
|
| 53 |
+
|
| 54 |
+
// TensorView or error
|
| 55 |
+
using TensorViewOr = absl::StatusOr<std::unique_ptr<TensorView>>;
|
| 56 |
+
using ConstTensorViewOr = absl::StatusOr<std::unique_ptr<const TensorView>>;
|
| 57 |
+
|
| 58 |
+
// Below are the interfaces for various "Context" objects to abstract away the
|
| 59 |
+
// TF and TFLite differences.
|
| 60 |
+
//
|
| 61 |
+
// The interfaces are static and use the CRTP pattern instead of virtual
|
| 62 |
+
// methods.
|
| 63 |
+
|
| 64 |
+
// The attribute dictionary passed to the op
|
| 65 |
+
using AttrValue = absl::variant<bool, int64_t, float, absl::string_view>;
|
| 66 |
+
|
| 67 |
+
// The interface for available methods during an op kernel initialization
|
| 68 |
+
template <typename SubType>
|
| 69 |
+
class InitContext {
|
| 70 |
+
public:
|
| 71 |
+
// Read the given attribute and populate the given value.
|
| 72 |
+
template <typename AttrType>
|
| 73 |
+
absl::Status GetAttr(const std::string& attr_name, AttrType* value) const;
|
| 74 |
+
|
| 75 |
+
protected:
|
| 76 |
+
// Read a given attribute or return error
|
| 77 |
+
absl::StatusOr<AttrValue> GetAttr(const std::string& attr_name) const {
|
| 78 |
+
return static_cast<const SubType&>(*this).GetAttr(attr_name);
|
| 79 |
+
}
|
| 80 |
+
};
|
| 81 |
+
|
| 82 |
+
// The interface for available methods during an op kernel invocation
|
| 83 |
+
template <typename SubType>
|
| 84 |
+
class InvokeContext {
|
| 85 |
+
public:
|
| 86 |
+
// Read an input tensor
|
| 87 |
+
ConstTensorViewOr GetInput(const int idx) const {
|
| 88 |
+
return static_cast<const SubType&>(*this).GetInput(idx);
|
| 89 |
+
}
|
| 90 |
+
// Get a mutable output tensor
|
| 91 |
+
TensorViewOr GetOutput(const int idx, const Shape& shape) const {
|
| 92 |
+
return static_cast<const SubType&>(*this).GetOutput(idx, shape);
|
| 93 |
+
}
|
| 94 |
+
// Number of input tensors
|
| 95 |
+
int NumInputs() const {
|
| 96 |
+
return static_cast<const SubType&>(*this).NumInputs();
|
| 97 |
+
}
|
| 98 |
+
// Number of output tensors
|
| 99 |
+
int NumOutputs() const {
|
| 100 |
+
return static_cast<const SubType&>(*this).NumOutputs();
|
| 101 |
+
}
|
| 102 |
+
};
|
| 103 |
+
|
| 104 |
+
// The interface for available methods during shape inference
|
| 105 |
+
template <typename SubType>
|
| 106 |
+
class ShapeInferenceContext {
|
| 107 |
+
public:
|
| 108 |
+
// Read an input tensor shape
|
| 109 |
+
ShapeOr GetInputShape(const int idx) const {
|
| 110 |
+
return static_cast<const SubType&>(*this).GetInputShape(idx);
|
| 111 |
+
}
|
| 112 |
+
// Set an output tensor shape
|
| 113 |
+
absl::Status SetOutputShape(const int idx, const Shape& shape) {
|
| 114 |
+
return static_cast<SubType&>(*this).SetOutputShape(idx, shape);
|
| 115 |
+
}
|
| 116 |
+
// Read an input tensor during shape inference
|
| 117 |
+
ConstTensorViewOr GetInputTensor(const int idx) const {
|
| 118 |
+
return static_cast<const SubType&>(*this).GetInputTensor(idx);
|
| 119 |
+
}
|
| 120 |
+
// Number of input tensors
|
| 121 |
+
int NumInputs() const {
|
| 122 |
+
return static_cast<const SubType&>(*this).NumInputs();
|
| 123 |
+
}
|
| 124 |
+
// Number of output tensors
|
| 125 |
+
int NumOutputs() const {
|
| 126 |
+
return static_cast<const SubType&>(*this).NumOutputs();
|
| 127 |
+
}
|
| 128 |
+
// Read the given attribute and populate the given value.
|
| 129 |
+
template <typename AttrType>
|
| 130 |
+
absl::Status GetAttr(const std::string& attr_name, AttrType* value) const;
|
| 131 |
+
|
| 132 |
+
protected:
|
| 133 |
+
// Read a given attribute or return error
|
| 134 |
+
absl::StatusOr<AttrValue> GetAttr(const std::string& attr_name) const {
|
| 135 |
+
return static_cast<const SubType&>(*this).GetAttr(attr_name);
|
| 136 |
+
}
|
| 137 |
+
};
|
| 138 |
+
|
| 139 |
+
// Maps the Runtime to the correct context types.
|
| 140 |
+
// eg. ContextTypeForRuntime<Runtime::Tf> -->
|
| 141 |
+
// { TfInitContext, TfInvokeContext, TfShapreInferenceContext }
|
| 142 |
+
template <Runtime Rt>
|
| 143 |
+
struct ContextTypeForRuntime {
|
| 144 |
+
// * Init
|
| 145 |
+
// * Invoke
|
| 146 |
+
// * ShapeInference
|
| 147 |
+
};
|
| 148 |
+
|
| 149 |
+
// A Tensorflow operation interface which is then adapted to both TF and TFLite
|
| 150 |
+
// runtimes.
|
| 151 |
+
//
|
| 152 |
+
// Example usage:
|
| 153 |
+
//
|
| 154 |
+
// template<Runtime R>
|
| 155 |
+
// class MyOp : public OpKernelShim<MyOp, R> {
|
| 156 |
+
//
|
| 157 |
+
// // Attributes declaration
|
| 158 |
+
// // (syntax: https://www.tensorflow.org/guide/create_op)
|
| 159 |
+
// static std::vector<std::string> Attrs();
|
| 160 |
+
//
|
| 161 |
+
// // Input tensors declaration
|
| 162 |
+
// // (syntax: https://www.tensorflow.org/guide/create_op)
|
| 163 |
+
// static std::vector<std::string> Inputs();
|
| 164 |
+
//
|
| 165 |
+
// // Output tensors declaration
|
| 166 |
+
// // (syntax: https://www.tensorflow.org/guide/create_op)
|
| 167 |
+
// static std::vector<std::string> Outputs();
|
| 168 |
+
//
|
| 169 |
+
// // Initializes the op
|
| 170 |
+
// absl::Status Init(InitContext* ctx);
|
| 171 |
+
//
|
| 172 |
+
// // Runs the operation
|
| 173 |
+
// absl::Status Invoke(InvokeContext* ctx);
|
| 174 |
+
//
|
| 175 |
+
// // Shape inference
|
| 176 |
+
// static absl::Status ShapeInference(ShapeInferenceContext* ctx);
|
| 177 |
+
//
|
| 178 |
+
// };
|
| 179 |
+
//
|
| 180 |
+
// WARNING: Experimental interface, subject to change
|
| 181 |
+
template <template <Runtime, typename...> typename SubType, Runtime Rt,
|
| 182 |
+
typename... Ts>
|
| 183 |
+
class OpKernelShim {
|
| 184 |
+
public:
|
| 185 |
+
// Some typedefs for convenience
|
| 186 |
+
using Shape = ::tflite::shim::Shape;
|
| 187 |
+
using InitContext =
|
| 188 |
+
::tflite::shim::InitContext<typename ContextTypeForRuntime<Rt>::Init>;
|
| 189 |
+
using InvokeContext =
|
| 190 |
+
::tflite::shim::InvokeContext<typename ContextTypeForRuntime<Rt>::Invoke>;
|
| 191 |
+
using ShapeInferenceContext = ::tflite::shim::ShapeInferenceContext<
|
| 192 |
+
typename ContextTypeForRuntime<Rt>::ShapeInference>;
|
| 193 |
+
|
| 194 |
+
// Needed because the pointer to this class is stored
|
| 195 |
+
virtual ~OpKernelShim() = default;
|
| 196 |
+
|
| 197 |
+
// If the operation has any attributes they are passed here.
|
| 198 |
+
absl::Status Init(InitContext* ctx) {
|
| 199 |
+
return static_cast<SubType<Rt, Ts...>&>(*this).Init(ctx);
|
| 200 |
+
}
|
| 201 |
+
|
| 202 |
+
// The actual computations of the operation
|
| 203 |
+
absl::Status Invoke(InvokeContext* ctx) {
|
| 204 |
+
return static_cast<SubType<Rt, Ts...>&>(*this).Invoke(ctx);
|
| 205 |
+
}
|
| 206 |
+
|
| 207 |
+
// Shape inference
|
| 208 |
+
static absl::Status ShapeInference(ShapeInferenceContext* ctx) {
|
| 209 |
+
return SubType<Rt, Ts...>::ShapeInference(ctx);
|
| 210 |
+
}
|
| 211 |
+
|
| 212 |
+
protected:
|
| 213 |
+
OpKernelShim() = default;
|
| 214 |
+
|
| 215 |
+
// Convience method for filling a single dimension output tensor.
|
| 216 |
+
template <typename BufferType, typename DType>
|
| 217 |
+
absl::Status FillOutputTensor(const std::vector<BufferType>& buffer,
|
| 218 |
+
int index, InvokeContext* context) const;
|
| 219 |
+
};
|
| 220 |
+
|
| 221 |
+
/////////////////////// Implementations
|
| 222 |
+
|
| 223 |
+
namespace internal {
|
| 224 |
+
// Extract the given AttrType from the AttrValue variant or returns error.
|
| 225 |
+
template <typename AttrType>
|
| 226 |
+
absl::Status GetAttr(const std::string& attr_name,
|
| 227 |
+
const absl::StatusOr<AttrValue> attr_value_or,
|
| 228 |
+
AttrType* value) {
|
| 229 |
+
if (!attr_value_or.ok()) return attr_value_or.status();
|
| 230 |
+
const AttrValue& attr_value = attr_value_or.value();
|
| 231 |
+
if (!absl::holds_alternative<AttrType>(attr_value)) {
|
| 232 |
+
return absl::InternalError(
|
| 233 |
+
absl::StrCat("The attribute type does not match the provided "
|
| 234 |
+
"type: attr_name: ",
|
| 235 |
+
attr_name));
|
| 236 |
+
}
|
| 237 |
+
*value = absl::get<AttrType>(attr_value);
|
| 238 |
+
return absl::OkStatus();
|
| 239 |
+
}
|
| 240 |
+
} // namespace internal
|
| 241 |
+
|
| 242 |
+
template <typename SubType>
|
| 243 |
+
template <typename AttrType>
|
| 244 |
+
absl::Status InitContext<SubType>::GetAttr(const std::string& attr_name,
|
| 245 |
+
AttrType* value) const {
|
| 246 |
+
const auto attr_value_or = GetAttr(attr_name);
|
| 247 |
+
return internal::GetAttr<AttrType>(attr_name, attr_value_or, value);
|
| 248 |
+
}
|
| 249 |
+
|
| 250 |
+
template <typename SubType>
|
| 251 |
+
template <typename AttrType>
|
| 252 |
+
absl::Status ShapeInferenceContext<SubType>::GetAttr(
|
| 253 |
+
const std::string& attr_name, AttrType* value) const {
|
| 254 |
+
const auto attr_value_or = GetAttr(attr_name);
|
| 255 |
+
return internal::GetAttr<AttrType>(attr_name, attr_value_or, value);
|
| 256 |
+
}
|
| 257 |
+
|
| 258 |
+
template <template <Runtime, typename...> typename SubType, Runtime Rt,
|
| 259 |
+
typename... Ts>
|
| 260 |
+
template <typename BufferType, typename DType>
|
| 261 |
+
absl::Status OpKernelShim<SubType, Rt, Ts...>::FillOutputTensor(
|
| 262 |
+
const std::vector<BufferType>& buffer, const int index,
|
| 263 |
+
tflite::shim::InvokeContext<typename ContextTypeForRuntime<Rt>::Invoke>*
|
| 264 |
+
context) const {
|
| 265 |
+
SH_ASSIGN_OR_RETURN(
|
| 266 |
+
const auto tensorview,
|
| 267 |
+
context->GetOutput(
|
| 268 |
+
index, tflite::shim::Shape({static_cast<int>(buffer.size())})));
|
| 269 |
+
auto data = tensorview->template As<DType, 1>();
|
| 270 |
+
for (int i = 0; i < buffer.size(); ++i) data(i) = buffer.at(i);
|
| 271 |
+
return absl::OkStatus();
|
| 272 |
+
}
|
| 273 |
+
|
| 274 |
+
} // namespace shim
|
| 275 |
+
} // namespace tflite
|
| 276 |
+
|
| 277 |
+
#endif // TENSORFLOW_LITE_KERNELS_SHIM_OP_KERNEL_H_
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/lite/kernels/shim/shape.h
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
#ifndef TENSORFLOW_LITE_KERNELS_SHIM_SHAPE_H_
|
| 16 |
+
#define TENSORFLOW_LITE_KERNELS_SHIM_SHAPE_H_
|
| 17 |
+
|
| 18 |
+
#include <initializer_list>
|
| 19 |
+
#include <string>
|
| 20 |
+
#include <vector>
|
| 21 |
+
|
| 22 |
+
#include "absl/status/statusor.h"
|
| 23 |
+
#include "absl/types/span.h"
|
| 24 |
+
|
| 25 |
+
namespace tflite {
|
| 26 |
+
namespace shim {
|
| 27 |
+
|
| 28 |
+
// Shape of a tensor. When unset it means the rank is unknown. Individual dims
|
| 29 |
+
// can also be unknown.
|
| 30 |
+
class Shape {
|
| 31 |
+
public:
|
| 32 |
+
using ValueType = std::vector<int>;
|
| 33 |
+
|
| 34 |
+
Shape() = default;
|
| 35 |
+
Shape(const Shape& o) = default;
|
| 36 |
+
Shape(Shape&& o) = default;
|
| 37 |
+
Shape& operator=(const Shape& o) = default;
|
| 38 |
+
Shape& operator=(Shape&& o) = default;
|
| 39 |
+
|
| 40 |
+
// Ctors
|
| 41 |
+
Shape(const std::initializer_list<int>& o) : value_(o), has_value_(true) {}
|
| 42 |
+
template <typename... Args>
|
| 43 |
+
explicit Shape(Args&&... args) // forward ctor args to that of std::vector
|
| 44 |
+
: value_(std::forward<Args>(args)...), has_value_(true) {}
|
| 45 |
+
explicit Shape(const absl::Span<int> value)
|
| 46 |
+
: value_(value.data(), value.data() + value.size()), has_value_(true) {}
|
| 47 |
+
|
| 48 |
+
// Accessors
|
| 49 |
+
inline bool has_value() const { return has_value_; }
|
| 50 |
+
inline ValueType& value() { return value_; }
|
| 51 |
+
inline const ValueType& value() const { return value_; }
|
| 52 |
+
ValueType* operator->() { return &value_; }
|
| 53 |
+
const ValueType* operator->() const { return &value_; }
|
| 54 |
+
ValueType& operator*() { return value_; }
|
| 55 |
+
const ValueType& operator*() const { return value_; }
|
| 56 |
+
// Get the specified dimension if known
|
| 57 |
+
int Dim(const int idx) const;
|
| 58 |
+
|
| 59 |
+
// Returns the rank of the shape
|
| 60 |
+
const int Rank() const { return has_value_ ? value_.size() : kUnknownRank; }
|
| 61 |
+
|
| 62 |
+
// Whether all the dimensions of the shape are known
|
| 63 |
+
bool FullyDefined() const;
|
| 64 |
+
|
| 65 |
+
// Pretty printer
|
| 66 |
+
std::string ToString() const;
|
| 67 |
+
|
| 68 |
+
// Adds two dimension taking into account unknown dims.
|
| 69 |
+
static int AddDims(const int dim1, const int dim2);
|
| 70 |
+
|
| 71 |
+
// Comparison
|
| 72 |
+
|
| 73 |
+
// Strict equality of the shapes. Unknown dims or rank on one side will
|
| 74 |
+
// result in false
|
| 75 |
+
bool operator==(const Shape& rhs) const;
|
| 76 |
+
bool operator!=(const Shape& rhs) const;
|
| 77 |
+
|
| 78 |
+
// Compatibility of the shapes. If there are two known and incompatible
|
| 79 |
+
// dimensions it returns false
|
| 80 |
+
bool Compatible(const Shape& rhs) const;
|
| 81 |
+
|
| 82 |
+
// The value for unknown dimensions and rank. There are static_asserts to
|
| 83 |
+
// ensure this matches the one defined in ::tensorflow namespace
|
| 84 |
+
static constexpr int kUnknownDim = -1;
|
| 85 |
+
static constexpr int kUnknownRank = -1;
|
| 86 |
+
|
| 87 |
+
private:
|
| 88 |
+
ValueType value_;
|
| 89 |
+
bool has_value_ = false;
|
| 90 |
+
};
|
| 91 |
+
using ShapeOr = absl::StatusOr<Shape>;
|
| 92 |
+
|
| 93 |
+
} // namespace shim
|
| 94 |
+
} // namespace tflite
|
| 95 |
+
|
| 96 |
+
#endif // TENSORFLOW_LITE_KERNELS_SHIM_SHAPE_H_
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/lite/kernels/shim/status_macros.h
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
#ifndef TENSORFLOW_LITE_KERNELS_SHIM_STATUS_MACROS_H_
|
| 16 |
+
#define TENSORFLOW_LITE_KERNELS_SHIM_STATUS_MACROS_H_
|
| 17 |
+
|
| 18 |
+
#include "absl/status/status.h"
|
| 19 |
+
|
| 20 |
+
// A few macros to help with propagating status.
|
| 21 |
+
// They are mainly adapted from the ones in tensorflow/core/platform/errors.h
|
| 22 |
+
// and tensorflow/core/platform/statusor.h but these files come with many
|
| 23 |
+
// transitive deps which can be too much for TFLite use cases. If these type of
|
| 24 |
+
// macros end up in `absl` we can replace them with those.
|
| 25 |
+
|
| 26 |
+
// The macros are prefixed with SH_ to avoid name collision.
|
| 27 |
+
|
| 28 |
+
namespace tflite {
|
| 29 |
+
namespace shim {
|
| 30 |
+
template <typename... Args>
|
| 31 |
+
void AppendToMessage(::absl::Status* status, Args... args) {
|
| 32 |
+
*status = ::absl::Status(status->code(),
|
| 33 |
+
::absl::StrCat(status->message(), "\n\t", args...));
|
| 34 |
+
}
|
| 35 |
+
} // namespace shim
|
| 36 |
+
} // namespace tflite
|
| 37 |
+
|
| 38 |
+
// Propagates error up the stack and appends to the error message.
|
| 39 |
+
#define SH_RETURN_WITH_CONTEXT_IF_ERROR(expr, ...) \
|
| 40 |
+
do { \
|
| 41 |
+
::absl::Status _status = (expr); \
|
| 42 |
+
if (!_status.ok()) { \
|
| 43 |
+
::tflite::shim::AppendToMessage(&_status, __VA_ARGS__); \
|
| 44 |
+
return _status; \
|
| 45 |
+
} \
|
| 46 |
+
} while (0)
|
| 47 |
+
|
| 48 |
+
// Propages the error up the stack.
|
| 49 |
+
// This can't be merged with the SH_RETURN_WITH_CONTEXT_IF_ERROR unless some
|
| 50 |
+
// overly clever/unreadable macro magic is used.
|
| 51 |
+
#define SH_RETURN_IF_ERROR(...) \
|
| 52 |
+
do { \
|
| 53 |
+
::absl::Status _status = (__VA_ARGS__); \
|
| 54 |
+
if (!_status.ok()) return _status; \
|
| 55 |
+
} while (0)
|
| 56 |
+
|
| 57 |
+
// Internal helper for concatenating macro values.
|
| 58 |
+
#define SH_STATUS_MACROS_CONCAT_NAME_INNER(x, y) x##y
|
| 59 |
+
#define SH_STATUS_MACROS_CONCAT_NAME(x, y) \
|
| 60 |
+
SH_STATUS_MACROS_CONCAT_NAME_INNER(x, y)
|
| 61 |
+
|
| 62 |
+
// Assigns an expression to lhs or propagates the error up.
|
| 63 |
+
#define SH_ASSIGN_OR_RETURN(lhs, rexpr) \
|
| 64 |
+
SH_ASSIGN_OR_RETURN_IMPL( \
|
| 65 |
+
SH_STATUS_MACROS_CONCAT_NAME(statusor, __COUNTER__), lhs, rexpr)
|
| 66 |
+
|
| 67 |
+
#define SH_ASSIGN_OR_RETURN_IMPL(statusor, lhs, rexpr) \
|
| 68 |
+
auto statusor = (rexpr); \
|
| 69 |
+
if (!statusor.ok()) return statusor.status(); \
|
| 70 |
+
lhs = std::move(statusor.value())
|
| 71 |
+
|
| 72 |
+
#endif // TENSORFLOW_LITE_KERNELS_SHIM_STATUS_MACROS_H_
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/lite/kernels/shim/tensor_view.h
ADDED
|
@@ -0,0 +1,256 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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orFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
#ifndef TENSORFLOW_LITE_KERNELS_SHIM_TENSOR_VIEW_H_
|
| 16 |
+
#define TENSORFLOW_LITE_KERNELS_SHIM_TENSOR_VIEW_H_
|
| 17 |
+
|
| 18 |
+
#include <variant>
|
| 19 |
+
|
| 20 |
+
#include "absl/status/statusor.h"
|
| 21 |
+
#include "absl/strings/string_view.h"
|
| 22 |
+
#include "absl/types/span.h"
|
| 23 |
+
#include "tensorflow/core/platform/logging.h"
|
| 24 |
+
#include "tensorflow/core/platform/tstring.h"
|
| 25 |
+
|
| 26 |
+
namespace tflite {
|
| 27 |
+
namespace shim {
|
| 28 |
+
|
| 29 |
+
// A type deduction template which is specialized for TF and TFLite.
|
| 30 |
+
// That is it maps
|
| 31 |
+
// ::tensorflow::Tensor -> tflite::shim::TfTensorView
|
| 32 |
+
// ::TfLiteTensor -> tflite::shim::TfLiteTensorView
|
| 33 |
+
template <typename W>
|
| 34 |
+
struct TensorViewSubType {};
|
| 35 |
+
|
| 36 |
+
// Common denominator for ::tflite::TfLiteTensor and ::tensorflow::Tensor.
|
| 37 |
+
// It is a "view" over the underlying tensor without taking ownership.
|
| 38 |
+
// Objects of this class can also mutate the underlying tensor depending on
|
| 39 |
+
// whether the underlying tensor is "const" qualified or not.
|
| 40 |
+
//
|
| 41 |
+
// Movable and copyable.
|
| 42 |
+
// It can be instantiated with the New() factory function. eg.
|
| 43 |
+
// TfTensorView t = TensorView::New(&tf_tensor);
|
| 44 |
+
// const TfTensorView t = TensorView::New(&const_tf_tensor);
|
| 45 |
+
// TfLiteTensorView t = TensorView::New(&tflite_tensor);
|
| 46 |
+
// const TfLiteTensorView t = TensorView::New(&const_tflite_tensor);
|
| 47 |
+
class TensorView {
|
| 48 |
+
protected:
|
| 49 |
+
// Union over all data types
|
| 50 |
+
using DataVariantType =
|
| 51 |
+
std::variant<absl::Span<bool>, absl::Span<int8_t>, absl::Span<uint8_t>,
|
| 52 |
+
absl::Span<int16_t>, absl::Span<uint16_t>,
|
| 53 |
+
absl::Span<int32_t>, absl::Span<uint32_t>,
|
| 54 |
+
absl::Span<int64_t>, absl::Span<uint64_t>, absl::Span<float>,
|
| 55 |
+
absl::Span<double>, absl::Span<::tensorflow::tstring>>;
|
| 56 |
+
|
| 57 |
+
// An interface while provides convenient row-major indexing over the
|
| 58 |
+
// underlying tensor.
|
| 59 |
+
// Example usage:
|
| 60 |
+
//
|
| 61 |
+
// // A scalar view
|
| 62 |
+
// const TensorView t_float
|
| 63 |
+
// float val = t_float.AsScalar<float>();
|
| 64 |
+
//
|
| 65 |
+
// // A vector view
|
| 66 |
+
// const TensorView t_int;
|
| 67 |
+
// auto t_int_vec = t_int.As<int32_t, /*RANK=*/ 1>();
|
| 68 |
+
// int sum = t_int_vec(0) + t_int_vec(1);
|
| 69 |
+
//
|
| 70 |
+
// // A matrix view
|
| 71 |
+
// TensorView t_str;
|
| 72 |
+
// auto t_str_mat = t_str.As<tensorflow::tstring, /*RANK=*/ 2>();
|
| 73 |
+
// t_str_mat(0, 0) = "abc";
|
| 74 |
+
// t_str_mat(2, 3) = "def";
|
| 75 |
+
template <typename DType, int RANK>
|
| 76 |
+
class Tensor {
|
| 77 |
+
public:
|
| 78 |
+
explicit Tensor(TensorView *t)
|
| 79 |
+
: data_(t->Data<DType>()), shape_(t->Shape()) {
|
| 80 |
+
DCHECK_EQ(RANK, shape_.size());
|
| 81 |
+
ComputeRowSizes();
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
explicit Tensor(const TensorView *t)
|
| 85 |
+
: data_(t->Data<DType>()), shape_(t->Shape()) {
|
| 86 |
+
DCHECK_EQ(RANK, shape_.size());
|
| 87 |
+
ComputeRowSizes();
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
// indexing operator
|
| 91 |
+
template <typename... IndexTypes>
|
| 92 |
+
inline DType &operator()(IndexTypes... indices) {
|
| 93 |
+
const auto idx = RowMajorIndex(std::array<int, RANK>{{indices...}});
|
| 94 |
+
return data_[idx];
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
// const indexing operator
|
| 98 |
+
template <typename... IndexTypes>
|
| 99 |
+
inline const DType &operator()(IndexTypes... indices) const {
|
| 100 |
+
const auto idx = RowMajorIndex(std::array<int, RANK>{{indices...}});
|
| 101 |
+
return data_.at(idx);
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
// Pointer accessor
|
| 105 |
+
typename absl::Span<DType>::pointer Ptr() { return data_.data(); }
|
| 106 |
+
constexpr typename absl::Span<DType>::const_pointer Ptr() const {
|
| 107 |
+
return data_.data();
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
// Size of the given dimension
|
| 111 |
+
inline int Dim(int dim_i) const {
|
| 112 |
+
DCHECK(RANK > 0 && dim_i < RANK) << "dim: " << dim_i << " rank:" << RANK;
|
| 113 |
+
// Handle negative indices
|
| 114 |
+
if (dim_i < 0) dim_i = ((dim_i % RANK) + RANK) % RANK;
|
| 115 |
+
return shape_[dim_i];
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
// The tensor's rank: number of dimensions
|
| 119 |
+
/*[[nodiscard]]*/ constexpr std::size_t Rank() const { return RANK; }
|
| 120 |
+
|
| 121 |
+
private:
|
| 122 |
+
// Computes the row-major index
|
| 123 |
+
inline std::size_t RowMajorIndex(
|
| 124 |
+
const std::array<int, RANK> &indices) const {
|
| 125 |
+
std::size_t ret = 0;
|
| 126 |
+
for (int i = 0; i < RANK; ++i) ret += indices[i] * row_sizes_[i];
|
| 127 |
+
return ret;
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
// Pre computes row sizes to convert multi dim indices into a row major
|
| 131 |
+
// index
|
| 132 |
+
void ComputeRowSizes() {
|
| 133 |
+
// Precompute row sizes for row major index computation
|
| 134 |
+
if (RANK > 0) {
|
| 135 |
+
row_sizes_[RANK - 1] = 1;
|
| 136 |
+
for (int i = RANK - 2; i >= 0; --i) {
|
| 137 |
+
row_sizes_[i] = row_sizes_[i + 1] * shape_[i + 1];
|
| 138 |
+
}
|
| 139 |
+
}
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
absl::Span<DType> data_;
|
| 143 |
+
const absl::Span<int> shape_;
|
| 144 |
+
std::size_t row_sizes_[RANK]{};
|
| 145 |
+
};
|
| 146 |
+
|
| 147 |
+
public:
|
| 148 |
+
// Factory which gets specialized for different wrapped tensor types.
|
| 149 |
+
template <typename W>
|
| 150 |
+
static absl::StatusOr<typename TensorViewSubType<W>::Type> New(
|
| 151 |
+
W *wrapped_tensor);
|
| 152 |
+
|
| 153 |
+
protected:
|
| 154 |
+
// Move constructor
|
| 155 |
+
TensorView(TensorView &&o) = default;
|
| 156 |
+
// Copy constructor
|
| 157 |
+
TensorView(const TensorView &o) = default;
|
| 158 |
+
// Move assignment operator
|
| 159 |
+
TensorView &operator=(TensorView &&o) = default;
|
| 160 |
+
// Copy assignment operator
|
| 161 |
+
TensorView &operator=(const TensorView &) = default;
|
| 162 |
+
|
| 163 |
+
public:
|
| 164 |
+
// Dtor
|
| 165 |
+
virtual ~TensorView() = default;
|
| 166 |
+
|
| 167 |
+
// Accessors
|
| 168 |
+
|
| 169 |
+
// Shape
|
| 170 |
+
absl::Span<int> Shape() { return shape_; }
|
| 171 |
+
/*[[nodiscard]]*/ const absl::Span<int> Shape() const { return shape_; }
|
| 172 |
+
|
| 173 |
+
// Data
|
| 174 |
+
template <typename DType>
|
| 175 |
+
absl::Span<DType> &Data() {
|
| 176 |
+
return std::get<absl::Span<DType>>(data_);
|
| 177 |
+
}
|
| 178 |
+
template <typename DType>
|
| 179 |
+
constexpr absl::Span<DType> Data() const {
|
| 180 |
+
return std::get<absl::Span<DType>>(data_);
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
// Reads the tensor given the dtype and its rank and provides an indexing
|
| 184 |
+
// operator.
|
| 185 |
+
template <typename DType, int RANK>
|
| 186 |
+
Tensor<DType, RANK> As() {
|
| 187 |
+
return Tensor<DType, RANK>(this);
|
| 188 |
+
}
|
| 189 |
+
|
| 190 |
+
// Const version of As()
|
| 191 |
+
template <typename DType, int RANK>
|
| 192 |
+
const Tensor<DType, RANK> As() const {
|
| 193 |
+
return Tensor<DType, RANK>(this);
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
// Read the given tensor as a scalar or return error if it isn't
|
| 197 |
+
template <typename DType>
|
| 198 |
+
DType &AsScalar();
|
| 199 |
+
|
| 200 |
+
template <typename DType>
|
| 201 |
+
const DType &AsScalar() const;
|
| 202 |
+
|
| 203 |
+
protected:
|
| 204 |
+
// Templated constructor. Since it's not possible to specify the template
|
| 205 |
+
// argument directly we place a dummy argument of that type so compiler
|
| 206 |
+
// can deduce the right template parameter
|
| 207 |
+
template <typename DType>
|
| 208 |
+
TensorView(const absl::Span<int> shape, void *data,
|
| 209 |
+
const std::size_t data_size, const DType &)
|
| 210 |
+
: shape_(shape),
|
| 211 |
+
data_(absl::Span<DType>(reinterpret_cast<DType *>(data),
|
| 212 |
+
data_size / sizeof(DType))) {}
|
| 213 |
+
|
| 214 |
+
// Return the total number of elements given the shape.
|
| 215 |
+
static constexpr std::size_t NumElements(const absl::Span<int> shape) {
|
| 216 |
+
std::size_t ret = 1;
|
| 217 |
+
for (const auto dim : shape) ret *= dim;
|
| 218 |
+
return ret;
|
| 219 |
+
}
|
| 220 |
+
|
| 221 |
+
// Tensor shape
|
| 222 |
+
// Note: using int rather than size_t to avoid conversion to from TfLite shape
|
| 223 |
+
absl::Span<int> shape_;
|
| 224 |
+
// Tensor data
|
| 225 |
+
DataVariantType data_;
|
| 226 |
+
};
|
| 227 |
+
|
| 228 |
+
// Add or remove const qualifier to O based on whether it is in I.
|
| 229 |
+
// For example
|
| 230 |
+
// MatchConstNess<const TfLiteTensor, TensorView>::Type == const TensorView
|
| 231 |
+
// MatchConstNess<TfLiteTensor, TensorView>::Type == TensorView
|
| 232 |
+
// MatchConstNess<TfLiteTensor, const TensorView>::Type == TensorView
|
| 233 |
+
template <typename I, typename O>
|
| 234 |
+
struct MatchConstNess {
|
| 235 |
+
using Type = std::conditional_t<std::is_const<I>::value, std::add_const_t<O>,
|
| 236 |
+
std::remove_const_t<O>>;
|
| 237 |
+
};
|
| 238 |
+
|
| 239 |
+
///////////////////////////// Implementation
|
| 240 |
+
|
| 241 |
+
template <typename DType>
|
| 242 |
+
DType &TensorView::AsScalar() {
|
| 243 |
+
DCHECK_EQ(shape_.size(), 0) << "Tensor is not a scalar";
|
| 244 |
+
return Data<DType>()[0];
|
| 245 |
+
}
|
| 246 |
+
|
| 247 |
+
template <typename DType>
|
| 248 |
+
const DType &TensorView::AsScalar() const {
|
| 249 |
+
DCHECK_EQ(shape_.size(), 0) << "Tensor is not a scalar";
|
| 250 |
+
return Data<DType>().at(0);
|
| 251 |
+
}
|
| 252 |
+
|
| 253 |
+
} // namespace shim
|
| 254 |
+
} // namespace tflite
|
| 255 |
+
|
| 256 |
+
#endif // TENSORFLOW_LITE_KERNELS_SHIM_TENSOR_VIEW_H_
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/lite/kernels/shim/tf_op_shim.h
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
#ifndef TENSORFLOW_LITE_KERNELS_SHIM_TF_OP_SHIM_H_
|
| 16 |
+
#define TENSORFLOW_LITE_KERNELS_SHIM_TF_OP_SHIM_H_
|
| 17 |
+
|
| 18 |
+
#include <memory>
|
| 19 |
+
#include <string>
|
| 20 |
+
|
| 21 |
+
#include "absl/status/status.h"
|
| 22 |
+
#include "absl/status/statusor.h"
|
| 23 |
+
#include "tensorflow/core/framework/attr_value.pb.h"
|
| 24 |
+
#include "tensorflow/core/framework/op_kernel.h"
|
| 25 |
+
#include "tensorflow/core/framework/registration/registration.h"
|
| 26 |
+
#include "tensorflow/core/framework/shape_inference.h"
|
| 27 |
+
#include "tensorflow/core/platform/errors.h"
|
| 28 |
+
#include "tensorflow/core/platform/status.h"
|
| 29 |
+
#include "tensorflow/lite/kernels/shim/op_kernel.h"
|
| 30 |
+
#include "tensorflow/lite/kernels/shim/shape.h"
|
| 31 |
+
|
| 32 |
+
// This file contains the TF adapter. That is, it takes a `OpKernelShim`
|
| 33 |
+
// class and provides a TF kernel out of it.
|
| 34 |
+
|
| 35 |
+
namespace tflite {
|
| 36 |
+
namespace shim {
|
| 37 |
+
|
| 38 |
+
// TF implementation of the methods during an op kernel initialization
|
| 39 |
+
class TfInitContext : public InitContext<TfInitContext> {
|
| 40 |
+
public:
|
| 41 |
+
explicit TfInitContext(const ::tensorflow::OpKernelConstruction* context);
|
| 42 |
+
// Read a given attribute
|
| 43 |
+
absl::StatusOr<AttrValue> GetAttr(const std::string& attr_name) const;
|
| 44 |
+
|
| 45 |
+
private:
|
| 46 |
+
const ::tensorflow::OpKernelConstruction* context_;
|
| 47 |
+
};
|
| 48 |
+
|
| 49 |
+
// TF implementation of the methods during an op kernel invocation
|
| 50 |
+
class TfInvokeContext : public InvokeContext<TfInvokeContext> {
|
| 51 |
+
public:
|
| 52 |
+
explicit TfInvokeContext(::tensorflow::OpKernelContext* context);
|
| 53 |
+
// Read an input tensor
|
| 54 |
+
ConstTensorViewOr GetInput(const int idx) const;
|
| 55 |
+
// Get a mutable output tensor
|
| 56 |
+
TensorViewOr GetOutput(const int idx, const Shape& shape) const;
|
| 57 |
+
// Number of input tensors
|
| 58 |
+
int NumInputs() const;
|
| 59 |
+
// Number of output tensors
|
| 60 |
+
int NumOutputs() const;
|
| 61 |
+
|
| 62 |
+
private:
|
| 63 |
+
::tensorflow::OpKernelContext* context_;
|
| 64 |
+
};
|
| 65 |
+
|
| 66 |
+
// TF implementation of the methods during shape inference
|
| 67 |
+
class TfShapeInferenceContext
|
| 68 |
+
: public ShapeInferenceContext<TfShapeInferenceContext> {
|
| 69 |
+
public:
|
| 70 |
+
explicit TfShapeInferenceContext(
|
| 71 |
+
::tensorflow::shape_inference::InferenceContext* context);
|
| 72 |
+
// Read an input tensor shape
|
| 73 |
+
ShapeOr GetInputShape(const int idx) const;
|
| 74 |
+
// Set an output tensor shape
|
| 75 |
+
absl::Status SetOutputShape(const int idx, const Shape& shape);
|
| 76 |
+
// Read an input tensor during shape inference
|
| 77 |
+
ConstTensorViewOr GetInputTensor(const int idx) const;
|
| 78 |
+
// Read a given attribute
|
| 79 |
+
absl::StatusOr<AttrValue> GetAttr(const std::string& attr_name) const;
|
| 80 |
+
// Number of input tensors
|
| 81 |
+
int NumInputs() const;
|
| 82 |
+
// Number of output tensors
|
| 83 |
+
int NumOutputs() const;
|
| 84 |
+
|
| 85 |
+
private:
|
| 86 |
+
::tensorflow::shape_inference::InferenceContext* context_;
|
| 87 |
+
};
|
| 88 |
+
|
| 89 |
+
// The adaptor between an op implementation (OpKernelShim subclass) and TF
|
| 90 |
+
// runtime
|
| 91 |
+
template <template <Runtime, typename...> typename Impl, typename... Ts>
|
| 92 |
+
class TfOpKernel : public ::tensorflow::OpKernel {
|
| 93 |
+
public:
|
| 94 |
+
using ImplType = Impl<Runtime::kTf, Ts...>;
|
| 95 |
+
|
| 96 |
+
explicit TfOpKernel(::tensorflow::OpKernelConstruction* c)
|
| 97 |
+
: OpKernel(c), impl_(std::make_unique<ImplType>()) {
|
| 98 |
+
TfInitContext ctx(c);
|
| 99 |
+
c->SetStatus(impl_->Init(&ctx));
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
// The main computation of the op
|
| 103 |
+
void Compute(::tensorflow::OpKernelContext* c) override {
|
| 104 |
+
TfInvokeContext ctx(c);
|
| 105 |
+
OP_REQUIRES_OK(c, impl_->Invoke(&ctx));
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
// Shape inference for the op.
|
| 109 |
+
static tensorflow::Status ShapeInference(
|
| 110 |
+
::tensorflow::shape_inference::InferenceContext* c) {
|
| 111 |
+
TfShapeInferenceContext ctx(c);
|
| 112 |
+
return ImplType::ShapeInference(&ctx);
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
// The operation name
|
| 116 |
+
static const char* OpName() { return ImplType::OpName(); }
|
| 117 |
+
|
| 118 |
+
protected:
|
| 119 |
+
std::unique_ptr<OpKernelShim<Impl, Runtime::kTf, Ts...>> impl_;
|
| 120 |
+
};
|
| 121 |
+
|
| 122 |
+
static_assert(::tensorflow::shape_inference::InferenceContext::kUnknownDim ==
|
| 123 |
+
Shape::kUnknownDim,
|
| 124 |
+
"The values must match.");
|
| 125 |
+
static_assert(::tensorflow::shape_inference::InferenceContext::kUnknownRank ==
|
| 126 |
+
Shape::kUnknownRank,
|
| 127 |
+
"The values must match.");
|
| 128 |
+
|
| 129 |
+
// Builds the OpDef to register the op with the TF runtime
|
| 130 |
+
template <typename Kernel>
|
| 131 |
+
::tensorflow::register_op::OpDefBuilderWrapper CreateOpDefBuilderWrapper() {
|
| 132 |
+
auto ret = ::tensorflow::register_op::OpDefBuilderWrapper(
|
| 133 |
+
Kernel::ImplType::OpName());
|
| 134 |
+
for (const auto& input : Kernel::ImplType::Inputs()) ret = ret.Input(input);
|
| 135 |
+
for (const auto& output : Kernel::ImplType::Outputs())
|
| 136 |
+
ret = ret.Output(output);
|
| 137 |
+
for (const auto& attr : Kernel::ImplType::Attrs()) ret = ret.Attr(attr);
|
| 138 |
+
ret.SetShapeFn(Kernel::ShapeInference).Doc(Kernel::ImplType::kDoc);
|
| 139 |
+
return ret;
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
template <>
|
| 143 |
+
struct ContextTypeForRuntime<Runtime::kTf> {
|
| 144 |
+
using Init = TfInitContext;
|
| 145 |
+
using Invoke = TfInvokeContext;
|
| 146 |
+
using ShapeInference = TfShapeInferenceContext;
|
| 147 |
+
};
|
| 148 |
+
|
| 149 |
+
// Macros for defining an op. These are taken from op.h because they need to be
|
| 150 |
+
// slightly modified here.
|
| 151 |
+
#define REGISTER_OP_SHIM_IMPL(ctr, op_kernel_cls) \
|
| 152 |
+
static ::tensorflow::InitOnStartupMarker const register_op##ctr \
|
| 153 |
+
TF_ATTRIBUTE_UNUSED = \
|
| 154 |
+
TF_INIT_ON_STARTUP_IF(SHOULD_REGISTER_OP(op_kernel_cls::OpName())) \
|
| 155 |
+
<< ::tflite::shim::CreateOpDefBuilderWrapper<op_kernel_cls>()
|
| 156 |
+
|
| 157 |
+
#define REGISTER_TF_OP_SHIM(...) \
|
| 158 |
+
TF_ATTRIBUTE_ANNOTATE("tf:op") \
|
| 159 |
+
TF_NEW_ID_FOR_INIT(REGISTER_OP_SHIM_IMPL, __VA_ARGS__)
|
| 160 |
+
|
| 161 |
+
} // namespace shim
|
| 162 |
+
} // namespace tflite
|
| 163 |
+
|
| 164 |
+
#endif // TENSORFLOW_LITE_KERNELS_SHIM_TF_OP_SHIM_H_
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/lite/kernels/shim/tf_tensor_view.h
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
#ifndef TENSORFLOW_LITE_KERNELS_SHIM_TF_TENSOR_VIEW_H_
|
| 16 |
+
#define TENSORFLOW_LITE_KERNELS_SHIM_TF_TENSOR_VIEW_H_
|
| 17 |
+
|
| 18 |
+
#include <vector>
|
| 19 |
+
|
| 20 |
+
#include "absl/status/statusor.h"
|
| 21 |
+
#include "absl/strings/string_view.h"
|
| 22 |
+
#include "tensorflow/core/framework/tensor.h"
|
| 23 |
+
#include "tensorflow/lite/kernels/shim/tensor_view.h"
|
| 24 |
+
|
| 25 |
+
namespace tflite {
|
| 26 |
+
namespace shim {
|
| 27 |
+
|
| 28 |
+
// A view over TF Tensor without taking ownership. It can be either mutable or
|
| 29 |
+
// immutable.
|
| 30 |
+
class TfTensorView : public TensorView {
|
| 31 |
+
public:
|
| 32 |
+
// Move constructor
|
| 33 |
+
TfTensorView(TfTensorView &&o) noexcept;
|
| 34 |
+
// Copy constructor
|
| 35 |
+
TfTensorView(const TfTensorView &o);
|
| 36 |
+
// Move assignment operator
|
| 37 |
+
TfTensorView &operator=(TfTensorView &&o) noexcept;
|
| 38 |
+
// Copy assignment operator
|
| 39 |
+
TfTensorView &operator=(const TfTensorView &);
|
| 40 |
+
|
| 41 |
+
protected:
|
| 42 |
+
// Templated constructor. Since it's not possible to specify the template
|
| 43 |
+
// argument directly we place a dummy argument of that type so compiler can
|
| 44 |
+
// deduce the right template parameter
|
| 45 |
+
template <typename DType>
|
| 46 |
+
TfTensorView(const ::tensorflow::Tensor *wrapped_tensor, const DType &dtype);
|
| 47 |
+
|
| 48 |
+
// Let the factory implementation use private constructors
|
| 49 |
+
template <typename TfTensorType>
|
| 50 |
+
friend absl::StatusOr<
|
| 51 |
+
typename MatchConstNess<TfTensorType, TfTensorView>::Type>
|
| 52 |
+
TfTensorViewTemplatizedNew(TfTensorType *wrapped_tensor);
|
| 53 |
+
|
| 54 |
+
// Stores the shape read from the TensorShape object
|
| 55 |
+
std::vector<int> shape_data_;
|
| 56 |
+
};
|
| 57 |
+
|
| 58 |
+
// Map ::tensorflow::Tensor -> TfTensorView
|
| 59 |
+
template <>
|
| 60 |
+
struct TensorViewSubType<::tensorflow::Tensor> {
|
| 61 |
+
using Type = TfTensorView;
|
| 62 |
+
};
|
| 63 |
+
|
| 64 |
+
// Map const ::tensorflow::Tensor -> const TfTensorView
|
| 65 |
+
template <>
|
| 66 |
+
struct TensorViewSubType<const ::tensorflow::Tensor> {
|
| 67 |
+
using Type = const TfTensorView;
|
| 68 |
+
};
|
| 69 |
+
|
| 70 |
+
// Specialization of New() factory
|
| 71 |
+
template <>
|
| 72 |
+
absl::StatusOr<TfTensorView> TensorView::New<::tensorflow::Tensor>(
|
| 73 |
+
::tensorflow::Tensor *wrapped_tensor);
|
| 74 |
+
|
| 75 |
+
// Specialization of New() factory
|
| 76 |
+
template <>
|
| 77 |
+
absl::StatusOr<const TfTensorView> TensorView::New<const ::tensorflow::Tensor>(
|
| 78 |
+
const ::tensorflow::Tensor *wrapped_tensor);
|
| 79 |
+
|
| 80 |
+
/////////////////////// Implementation
|
| 81 |
+
///////////////////////
|
| 82 |
+
|
| 83 |
+
// Templated ctor
|
| 84 |
+
template <typename DType>
|
| 85 |
+
TfTensorView::TfTensorView(const ::tensorflow::Tensor *wrapped_tensor,
|
| 86 |
+
const DType &dtype)
|
| 87 |
+
: TensorView({}, wrapped_tensor->data(),
|
| 88 |
+
wrapped_tensor->tensor_data().size(), dtype) {
|
| 89 |
+
shape_data_.resize(wrapped_tensor->shape().dims());
|
| 90 |
+
for (int dim = 0; dim < wrapped_tensor->shape().dims(); ++dim) {
|
| 91 |
+
shape_data_[dim] = wrapped_tensor->shape().dim_size(dim);
|
| 92 |
+
}
|
| 93 |
+
shape_ = absl::Span<int>(shape_data_);
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
} // namespace shim
|
| 97 |
+
} // namespace tflite
|
| 98 |
+
|
| 99 |
+
#endif // TENSORFLOW_LITE_KERNELS_SHIM_TF_TENSOR_VIEW_H_
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/python/client/session_ref.h
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
#ifndef TENSORFLOW_PYTHON_CLIENT_SESSION_REF_H_
|
| 16 |
+
#define TENSORFLOW_PYTHON_CLIENT_SESSION_REF_H_
|
| 17 |
+
|
| 18 |
+
#include <memory>
|
| 19 |
+
|
| 20 |
+
#include "tensorflow/core/platform/mutex.h"
|
| 21 |
+
#include "tensorflow/core/public/session.h"
|
| 22 |
+
|
| 23 |
+
namespace tensorflow {
|
| 24 |
+
|
| 25 |
+
class SessionLogger;
|
| 26 |
+
|
| 27 |
+
// A `SessionRef` manages the lifetime of a wrapped `Session` pointer.
|
| 28 |
+
//
|
| 29 |
+
// SessionRef blocks the return of Close() until all pending operations have
|
| 30 |
+
// been completed or cancelled and underlying session has been freed. Any
|
| 31 |
+
// subsequent operations on the SessionRef object will return errors::Cancelled.
|
| 32 |
+
class SessionRef : public Session {
|
| 33 |
+
public:
|
| 34 |
+
explicit SessionRef(Session* session);
|
| 35 |
+
~SessionRef() override;
|
| 36 |
+
|
| 37 |
+
Status Create(const GraphDef& graph) override;
|
| 38 |
+
Status Extend(const GraphDef& graph) override;
|
| 39 |
+
Status Create(const RunOptions& run_options, const GraphDef& graph) override;
|
| 40 |
+
Status Extend(const RunOptions& run_options, const GraphDef& graph) override;
|
| 41 |
+
Status Run(const std::vector<std::pair<string, Tensor> >& inputs,
|
| 42 |
+
const std::vector<string>& output_tensor_names,
|
| 43 |
+
const std::vector<string>& target_node_names,
|
| 44 |
+
std::vector<Tensor>* outputs) override;
|
| 45 |
+
|
| 46 |
+
Status ListDevices(std::vector<DeviceAttributes>* response) override;
|
| 47 |
+
|
| 48 |
+
Status Close() override;
|
| 49 |
+
Status Close(const RunOptions& run_options) override;
|
| 50 |
+
|
| 51 |
+
Status Run(const RunOptions& run_options,
|
| 52 |
+
const std::vector<std::pair<string, Tensor> >& inputs,
|
| 53 |
+
const std::vector<string>& output_tensor_names,
|
| 54 |
+
const std::vector<string>& target_node_names,
|
| 55 |
+
std::vector<Tensor>* outputs, RunMetadata* run_metadata) override;
|
| 56 |
+
|
| 57 |
+
Status PRunSetup(const std::vector<string>& input_names,
|
| 58 |
+
const std::vector<string>& output_names,
|
| 59 |
+
const std::vector<string>& target_nodes,
|
| 60 |
+
string* handle) override;
|
| 61 |
+
|
| 62 |
+
Status PRun(const string& handle,
|
| 63 |
+
const std::vector<std::pair<string, Tensor> >& inputs,
|
| 64 |
+
const std::vector<string>& output_names,
|
| 65 |
+
std::vector<Tensor>* outputs) override;
|
| 66 |
+
|
| 67 |
+
Status MakeCallable(const CallableOptions& callable_options,
|
| 68 |
+
CallableHandle* out_handle) override;
|
| 69 |
+
|
| 70 |
+
Status RunCallable(CallableHandle handle,
|
| 71 |
+
const std::vector<Tensor>& feed_tensors,
|
| 72 |
+
std::vector<Tensor>* fetch_tensors,
|
| 73 |
+
RunMetadata* run_metadata) override;
|
| 74 |
+
|
| 75 |
+
Status ReleaseCallable(CallableHandle handle) override;
|
| 76 |
+
|
| 77 |
+
private:
|
| 78 |
+
mutex run_lock_;
|
| 79 |
+
condition_variable run_finished_;
|
| 80 |
+
uint64 run_count_ TF_GUARDED_BY(run_lock_) = {0};
|
| 81 |
+
std::shared_ptr<Session> session_;
|
| 82 |
+
|
| 83 |
+
// Borrowed reference to global session logger.
|
| 84 |
+
SessionLogger* logger_;
|
| 85 |
+
|
| 86 |
+
Status CheckNotClosed();
|
| 87 |
+
};
|
| 88 |
+
|
| 89 |
+
} // namespace tensorflow
|
| 90 |
+
|
| 91 |
+
#endif // TENSORFLOW_PYTHON_CLIENT_SESSION_REF_H_
|
miniconda3/envs/active_proaction/lib/python3.10/site-packages/tensorflow/include/tensorflow/python/client/tf_session_helper.h
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
| 14 |
+
==============================================================================*/
|
| 15 |
+
|
| 16 |
+
#ifndef TENSORFLOW_PYTHON_CLIENT_TF_SESSION_HELPER_H_
|
| 17 |
+
#define TENSORFLOW_PYTHON_CLIENT_TF_SESSION_HELPER_H_
|
| 18 |
+
|
| 19 |
+
// Must be included first
|
| 20 |
+
// clang-format off
|
| 21 |
+
#include "tsl/python/lib/core/numpy.h" //NOLINT
|
| 22 |
+
// clang-format on
|
| 23 |
+
|
| 24 |
+
#include "tensorflow/c/c_api.h"
|
| 25 |
+
#include "tensorflow/core/framework/graph.pb.h"
|
| 26 |
+
#include "tensorflow/core/lib/core/errors.h"
|
| 27 |
+
#include "tensorflow/core/lib/core/status.h"
|
| 28 |
+
#include "tensorflow/core/lib/gtl/inlined_vector.h"
|
| 29 |
+
|
| 30 |
+
namespace tensorflow {
|
| 31 |
+
|
| 32 |
+
// Container types for the various arguments and temporary values used
|
| 33 |
+
// in the wrapper.
|
| 34 |
+
|
| 35 |
+
// A NameVector is a vector of tensor or operation names, as borrowed
|
| 36 |
+
// C strings.
|
| 37 |
+
typedef tensorflow::gtl::InlinedVector<const char*, 8> NameVector;
|
| 38 |
+
|
| 39 |
+
// A PyObjectVector is a vector of borrowed pointers to PyObjects.
|
| 40 |
+
typedef tensorflow::gtl::InlinedVector<PyObject*, 8> PyObjectVector;
|
| 41 |
+
|
| 42 |
+
// A TF_TensorVector is a vector of borrowed pointers to TF_Tensors.
|
| 43 |
+
typedef gtl::InlinedVector<TF_Tensor*, 8> TF_TensorVector;
|
| 44 |
+
|
| 45 |
+
TF_Session* TF_NewSessionRef(TF_Graph* graph, const TF_SessionOptions* opts,
|
| 46 |
+
TF_Status* status);
|
| 47 |
+
|
| 48 |
+
// Run the graph associated with the session starting with the
|
| 49 |
+
// supplied inputs[]. Regardless of success or failure, inputs[] are
|
| 50 |
+
// stolen by the implementation (i.e. the implementation will
|
| 51 |
+
// eventually call Py_DECREF on each array input).
|
| 52 |
+
//
|
| 53 |
+
// The PyObject* feed_dict must be a dictionary mapping strings to
|
| 54 |
+
// NumPy arrays. This function does not modify its reference count.
|
| 55 |
+
//
|
| 56 |
+
// On success, the tensors corresponding to output_names[0,noutputs-1]
|
| 57 |
+
// are placed in out_values[], and these outputs[] become the property
|
| 58 |
+
// of the caller (the caller must eventually call Py_DECREF on them).
|
| 59 |
+
//
|
| 60 |
+
// On failure, out_status contains a tensorflow::Status with an error
|
| 61 |
+
// message.
|
| 62 |
+
void TF_Run_wrapper(TF_DeprecatedSession* session, const TF_Buffer* run_options,
|
| 63 |
+
PyObject* feed_dict, const NameVector& output_names,
|
| 64 |
+
const NameVector& target_nodes, TF_Status* out_status,
|
| 65 |
+
PyObjectVector* out_values, TF_Buffer* run_outputs);
|
| 66 |
+
|
| 67 |
+
// Python wrappers for the `Session::MakeCallable()` API.
|
| 68 |
+
void TF_DeprecatedSessionMakeCallable(TF_DeprecatedSession* session,
|
| 69 |
+
const TF_Buffer* callable_options,
|
| 70 |
+
int64_t* out_handle, TF_Status* status);
|
| 71 |
+
void TF_SessionMakeCallable(TF_Session* session,
|
| 72 |
+
const TF_Buffer* callable_options,
|
| 73 |
+
int64_t* out_handle, TF_Status* status);
|
| 74 |
+
|
| 75 |
+
// Python wrappers for the `Session::RunCallable()` API.
|
| 76 |
+
void TF_DeprecatedSessionRunCallable(TF_DeprecatedSession* session,
|
| 77 |
+
int64_t handle, PyObject* feed_values,
|
| 78 |
+
PyObjectVector* out_values,
|
| 79 |
+
TF_Buffer* run_metadata,
|
| 80 |
+
TF_Status* status);
|
| 81 |
+
void TF_SessionRunCallable(TF_Session* session, int64_t handle,
|
| 82 |
+
PyObject* feed_values, PyObjectVector* out_values,
|
| 83 |
+
TF_Buffer* run_metadata, TF_Status* status);
|
| 84 |
+
|
| 85 |
+
// Python wrappers for the `Session::ReleaseCallable()` API.
|
| 86 |
+
void TF_DeprecatedSessionReleaseCallable(TF_DeprecatedSession* session,
|
| 87 |
+
int64_t handle, TF_Status* status);
|
| 88 |
+
void TF_SessionReleaseCallable(TF_Session* session, int64_t handle,
|
| 89 |
+
TF_Status* status);
|
| 90 |
+
|
| 91 |
+
// Set up the graph with the intended feeds and fetches for partial run.
|
| 92 |
+
// *out_handle is owned by the caller.
|
| 93 |
+
//
|
| 94 |
+
// On success, returns a handle that is used for subsequent PRun calls.
|
| 95 |
+
//
|
| 96 |
+
// On failure, out_status contains a tensorflow::Status with an error
|
| 97 |
+
// message.
|
| 98 |
+
void TF_PRunSetup_wrapper(TF_DeprecatedSession* session,
|
| 99 |
+
const NameVector& input_names,
|
| 100 |
+
const NameVector& output_names,
|
| 101 |
+
const NameVector& target_nodes, TF_Status* out_status,
|
| 102 |
+
const char** out_handle);
|
| 103 |
+
|
| 104 |
+
// Continue to run the graph with additional feeds and fetches. The
|
| 105 |
+
// execution state is uniquely identified by the handle.
|
| 106 |
+
//
|
| 107 |
+
// The PyObject* feed_dict must be a dictionary mapping strings to
|
| 108 |
+
// NumPy arrays. This function does not modify its reference count.
|
| 109 |
+
//
|
| 110 |
+
// On success, the tensors corresponding to output_names[0,noutputs-1]
|
| 111 |
+
// are placed in out_values[], and these outputs[] become the property
|
| 112 |
+
// of the caller (the caller must eventually call Py_DECREF on them).
|
| 113 |
+
//
|
| 114 |
+
// On failure, out_status contains a tensorflow::Status with an error
|
| 115 |
+
// message.
|
| 116 |
+
void TF_PRun_wrapper(TF_DeprecatedSession* session, const char* handle,
|
| 117 |
+
PyObject* feed_dict, const NameVector& output_names,
|
| 118 |
+
TF_Status* out_status, PyObjectVector* out_values);
|
| 119 |
+
|
| 120 |
+
// Wrapper for TF_Reset that converts the string vectors to character arrays.
|
| 121 |
+
void TF_Reset_wrapper(const TF_SessionOptions* opt,
|
| 122 |
+
const NameVector& containers, TF_Status* status);
|
| 123 |
+
|
| 124 |
+
// Convenience wrapper around EqualGraphDef to make it easier to wrap.
|
| 125 |
+
// Returns an explanation if a difference is found, or the empty string
|
| 126 |
+
// for no difference.
|
| 127 |
+
string EqualGraphDefWrapper(const string& actual, const string& expected);
|
| 128 |
+
|
| 129 |
+
// Convenience wrapper around AreAttrValuesEqual to make it easier to wrap.
|
| 130 |
+
// The actual and expected strings must correspond to a serialized binary
|
| 131 |
+
// representation of two AttrValue proto instances.
|
| 132 |
+
// Returns an explanation if a difference is found, or the empty string
|
| 133 |
+
// for no difference.
|
| 134 |
+
string EqualAttrValueWrapper(const string& actual, const string& expected);
|
| 135 |
+
|
| 136 |
+
// Gets shape from C API Graph object.
|
| 137 |
+
//
|
| 138 |
+
// If shape is known, returns shape vector where -1 means "unknown
|
| 139 |
+
// dimension". Sets unknown_shape to false.
|
| 140 |
+
//
|
| 141 |
+
// If shape is unknown, sets unknown_shape to true.
|
| 142 |
+
tensorflow::gtl::InlinedVector<int64_t, 6> TF_GraphGetTensorShapeHelper(
|
| 143 |
+
TF_Graph* graph, TF_Output output, TF_Status* status, bool* unknown_shape);
|
| 144 |
+
|
| 145 |
+
// Runs the graph associated with the session starting with the supplied inputs.
|
| 146 |
+
// On success, `py_outputs` is populated with a numpy ndarray for each output
|
| 147 |
+
// (the caller must decref these ndarrays, although this will likely be handled
|
| 148 |
+
// by the Python gc). `session`, `out_status`, and `py_outputs` must be
|
| 149 |
+
// non-null. `py_outputs` should be empty.
|
| 150 |
+
void TF_SessionRun_wrapper(TF_Session* session, const TF_Buffer* run_options,
|
| 151 |
+
const std::vector<TF_Output>& inputs,
|
| 152 |
+
const std::vector<PyObject*>& input_ndarrays,
|
| 153 |
+
const std::vector<TF_Output>& outputs,
|
| 154 |
+
const std::vector<TF_Operation*>& targets,
|
| 155 |
+
TF_Buffer* run_metadata, TF_Status* status,
|
| 156 |
+
std::vector<PyObject*>* py_outputs);
|
| 157 |
+
|
| 158 |
+
// Set up the graph with the intended feeds (inputs) and fetches (output) for
|
| 159 |
+
// a sequence of partial run calls.
|
| 160 |
+
//
|
| 161 |
+
// On success, returns a handle that can be used for subsequent PRun calls. The
|
| 162 |
+
// handle is owned by the caller and should be deleted with TF_DeletePRunHandle
|
| 163 |
+
// when it is no longer needed.
|
| 164 |
+
//
|
| 165 |
+
// On failure, out_status contains a tensorflow::Status with an error
|
| 166 |
+
// message.
|
| 167 |
+
void TF_SessionPRunSetup_wrapper(TF_Session* session,
|
| 168 |
+
const std::vector<TF_Output>& inputs,
|
| 169 |
+
const std::vector<TF_Output>& outputs,
|
| 170 |
+
const std::vector<TF_Operation*>& targets,
|
| 171 |
+
const char** out_handle, TF_Status* status);
|
| 172 |
+
|
| 173 |
+
// Continue to run the graph with additional feeds and fetches. The
|
| 174 |
+
// execution state is uniquely identified by the handle.
|
| 175 |
+
//
|
| 176 |
+
// On success, `py_outputs` is populated with a numpy ndarray for each output
|
| 177 |
+
// (the caller must decref these ndarrays, although this will likely be handled
|
| 178 |
+
// by the Python gc). `session`, `handle`, `out_status`, and `py_outputs` must
|
| 179 |
+
// be non-null. `py_outputs` should be empty.
|
| 180 |
+
//
|
| 181 |
+
// On failure, out_status contains a tensorflow::Status with an error
|
| 182 |
+
// message.
|
| 183 |
+
void TF_SessionPRun_wrapper(TF_Session* session, const char* handle,
|
| 184 |
+
const std::vector<TF_Output>& inputs,
|
| 185 |
+
const std::vector<PyObject*>& input_ndarrays,
|
| 186 |
+
const std::vector<TF_Output>& outputs,
|
| 187 |
+
TF_Status* status,
|
| 188 |
+
std::vector<PyObject*>* py_outputs);
|
| 189 |
+
|
| 190 |
+
// Retrieves the inputs of this operation.
|
| 191 |
+
std::vector<TF_Output> GetOperationInputs(TF_Operation* oper);
|
| 192 |
+
|
| 193 |
+
// Retrieves the control inputs of this operation.
|
| 194 |
+
std::vector<TF_Operation*> TF_OperationGetControlInputs_wrapper(
|
| 195 |
+
TF_Operation* oper);
|
| 196 |
+
|
| 197 |
+
// Retrieves the control outputs of this operation.
|
| 198 |
+
std::vector<TF_Operation*> TF_OperationGetControlOutputs_wrapper(
|
| 199 |
+
TF_Operation* oper);
|
| 200 |
+
|
| 201 |
+
// Retrieves the op names of the consumers of `oper_out`. The returned strings
|
| 202 |
+
// have the lifetime of the underlying TF_Graph.
|
| 203 |
+
std::vector<const char*> TF_OperationOutputConsumers_wrapper(
|
| 204 |
+
TF_Output oper_out);
|
| 205 |
+
|
| 206 |
+
// `opers` equaling NULL are converted to `nopers = -1`.
|
| 207 |
+
// `output_names` must be empty or have the same length as `outputs`.
|
| 208 |
+
TF_Function* TF_GraphToFunction_wrapper(
|
| 209 |
+
const TF_Graph* fn_body, const char* fn_name, bool append_hash_to_fn_name,
|
| 210 |
+
const std::vector<TF_Operation*>* opers,
|
| 211 |
+
const std::vector<TF_Output>& inputs, const std::vector<TF_Output>& outputs,
|
| 212 |
+
const NameVector& output_names,
|
| 213 |
+
const std::vector<TF_Operation*>* control_outputs,
|
| 214 |
+
const NameVector& control_output_names, const TF_FunctionOptions* opts,
|
| 215 |
+
const char* description, TF_Status* status);
|
| 216 |
+
|
| 217 |
+
// Set the shapes and types for the output's handle.
|
| 218 |
+
//
|
| 219 |
+
// The sizes of 'shapes', 'ranks', and 'types' must be equal; `shapes[i]`
|
| 220 |
+
// contains the shape of the handle's i-th value, `ranks[i]` contains the i-th
|
| 221 |
+
// shape's rank, and `types[i]` contains the i-th value's dtype. If the i-th
|
| 222 |
+
// shape is unknown, then `ranks[i]` must be equal to -1.
|
| 223 |
+
//
|
| 224 |
+
// The space between the double angle brackets below looks extraneous, but
|
| 225 |
+
// our version of SWIG cannot parse ">>".
|
| 226 |
+
void TF_GraphSetOutputHandleShapesAndTypes_wrapper(
|
| 227 |
+
TF_Graph* graph, TF_Output output,
|
| 228 |
+
const std::vector<std::vector<int64_t> >& shapes,
|
| 229 |
+
const std::vector<int>& ranks, const std::vector<TF_DataType>& types,
|
| 230 |
+
TF_Status* status);
|
| 231 |
+
|
| 232 |
+
// Creates Placeholders with specified types in the Graph.
|
| 233 |
+
//
|
| 234 |
+
// This is an internal API used to speed up creation of unused placeholders
|
| 235 |
+
// in while_v2 cond graph and is subject to change/removal.
|
| 236 |
+
std::vector<TF_Output> TF_CreatePlaceholders(TF_Graph* graph, PyObject* dtypes,
|
| 237 |
+
const char* prefix,
|
| 238 |
+
TF_Status* status);
|
| 239 |
+
|
| 240 |
+
// Set the shape of output. If unknown is true, `num_dims` must be set to
|
| 241 |
+
// -1 and `dims` is set to nullptr.
|
| 242 |
+
void TF_GraphSetTensorShape_wrapper(TF_Graph* graph, TF_Output output,
|
| 243 |
+
const std::vector<int64_t>& dims,
|
| 244 |
+
bool unknown_shape, TF_Status* status);
|
| 245 |
+
|
| 246 |
+
// Returns the string representations of the missing unused input mappings.
|
| 247 |
+
std::vector<string> TF_ImportGraphDefResultsMissingUnusedInputMappings_wrapper(
|
| 248 |
+
TF_ImportGraphDefResults* results);
|
| 249 |
+
|
| 250 |
+
// If evaluation was possible, returns the numpy ndarray of the evaluated
|
| 251 |
+
// result. Otherwise returns None.
|
| 252 |
+
PyObject* TF_TryEvaluateConstant_wrapper(TF_Graph* graph, TF_Output output,
|
| 253 |
+
TF_Status* status);
|
| 254 |
+
|
| 255 |
+
} // namespace tensorflow
|
| 256 |
+
|
| 257 |
+
#endif // TENSORFLOW_PYTHON_CLIENT_TF_SESSION_HELPER_H_
|