|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0 and the following additional limitation. Functionality enabled by the |
| 5 | + * files subject to the Elastic License 2.0 may only be used in production when |
| 6 | + * invoked by an Elasticsearch process with a license key installed that permits |
| 7 | + * use of machine learning features. You may not use this file except in |
| 8 | + * compliance with the Elastic License 2.0 and the foregoing additional |
| 9 | + * limitation. |
| 10 | + */ |
| 11 | + |
| 12 | +#ifndef INCLUDED_ml_torch_CResultWriter_h |
| 13 | +#define INCLUDED_ml_torch_CResultWriter_h |
| 14 | + |
| 15 | +#include <core/CJsonOutputStreamWrapper.h> |
| 16 | +#include <core/CRapidJsonLineWriter.h> |
| 17 | + |
| 18 | +#include <rapidjson/stringbuffer.h> |
| 19 | +#include <torch/csrc/api/include/torch/types.h> |
| 20 | + |
| 21 | +#include <cstdint> |
| 22 | +#include <iosfwd> |
| 23 | +#include <sstream> |
| 24 | +#include <string> |
| 25 | + |
| 26 | +namespace ml { |
| 27 | +namespace torch { |
| 28 | +class CThreadSettings; |
| 29 | + |
| 30 | +//! \brief |
| 31 | +//! Formats and writes results for PyTorch inference. |
| 32 | +//! |
| 33 | +//! DESCRIPTION:\n |
| 34 | +//! There are four types of result: |
| 35 | +//! |
| 36 | +//! 1. Inference results |
| 37 | +//! 2. Thread settings |
| 38 | +//! 3. Acknowledgements |
| 39 | +//! 4. Errors |
| 40 | +//! |
| 41 | +//! IMPLEMENTATION DECISIONS:\n |
| 42 | +//! We can cache inference results and errors, but when we reply with a |
| 43 | +//! cached value we still need to change the request ID, time taken, and |
| 44 | +//! cache hit indicator. Therefore this class contains functionality for |
| 45 | +//! building the invariant portion of results to be cached and later |
| 46 | +//! spliced into a complete response. |
| 47 | +//! |
| 48 | +class CResultWriter { |
| 49 | +public: |
| 50 | + using TRapidJsonLineWriter = core::CRapidJsonLineWriter<rapidjson::StringBuffer>; |
| 51 | + |
| 52 | +public: |
| 53 | + explicit CResultWriter(std::ostream& strmOut); |
| 54 | + |
| 55 | + //! No copying |
| 56 | + CResultWriter(const CResultWriter&) = delete; |
| 57 | + CResultWriter& operator=(const CResultWriter&) = delete; |
| 58 | + |
| 59 | + //! Write an error directly to the output stream. |
| 60 | + void writeError(const std::string& requestId, const std::string& message); |
| 61 | + |
| 62 | + //! Write thread settings to the output stream. |
| 63 | + void writeThreadSettings(const std::string& requestId, const CThreadSettings& threadSettings); |
| 64 | + |
| 65 | + //! Write a simple acknowledgement to the output stream. |
| 66 | + void writeSimpleAck(const std::string& requestId); |
| 67 | + |
| 68 | + //! Wrap the invariant portion of a cached result with request ID, |
| 69 | + //! cache hit indicator and time taken. Then write the full document |
| 70 | + //! to the output stream. |
| 71 | + void wrapAndWriteInnerResponse(const std::string& innerResponse, |
| 72 | + const std::string& requestId, |
| 73 | + bool isCacheHit, |
| 74 | + std::uint64_t timeMs); |
| 75 | + |
| 76 | + //! Write the prediction portion of an inference result. |
| 77 | + template<std::size_t N> |
| 78 | + void writePrediction(const ::torch::Tensor& prediction, TRapidJsonLineWriter& jsonWriter) { |
| 79 | + |
| 80 | + // Creating the accessor will throw if the tensor does not have exactly |
| 81 | + // N dimensions. Do this before writing any output so the error message |
| 82 | + // isn't mingled with a partial result. |
| 83 | + |
| 84 | + if (prediction.dtype() == ::torch::kFloat32) { |
| 85 | + auto accessor = prediction.accessor<float, N>(); |
| 86 | + this->writeInferenceResults(accessor, jsonWriter); |
| 87 | + |
| 88 | + } else if (prediction.dtype() == ::torch::kFloat64) { |
| 89 | + auto accessor = prediction.accessor<double, N>(); |
| 90 | + this->writeInferenceResults(accessor, jsonWriter); |
| 91 | + |
| 92 | + } else { |
| 93 | + std::ostringstream ss; |
| 94 | + ss << "Cannot process result tensor of type [" << prediction.dtype() << ']'; |
| 95 | + writeInnerError(ss.str(), jsonWriter); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + //! Create the invariant portion of an inference result, suitable for |
| 100 | + //! caching and later splicing into a full result. |
| 101 | + std::string createInnerResult(const ::torch::Tensor& results); |
| 102 | + |
| 103 | +private: |
| 104 | + //! Field names. |
| 105 | + static const std::string RESULT; |
| 106 | + static const std::string INFERENCE; |
| 107 | + static const std::string ERROR; |
| 108 | + static const std::string TIME_MS; |
| 109 | + static const std::string CACHE_HIT; |
| 110 | + static const std::string THREAD_SETTINGS; |
| 111 | + static const std::string ACK; |
| 112 | + static const std::string ACKNOWLEDGED; |
| 113 | + static const std::string NUM_ALLOCATIONS; |
| 114 | + static const std::string NUM_THREADS_PER_ALLOCATION; |
| 115 | + |
| 116 | +private: |
| 117 | + //! Create the invariant portion of an error result, suitable for |
| 118 | + //! caching and later splicing into a full result. |
| 119 | + static void writeInnerError(const std::string& message, TRapidJsonLineWriter& jsonWriter); |
| 120 | + |
| 121 | + //! Write a one dimensional tensor. |
| 122 | + template<typename T> |
| 123 | + void writeTensor(const ::torch::TensorAccessor<T, 1UL>& accessor, |
| 124 | + TRapidJsonLineWriter& jsonWriter) { |
| 125 | + jsonWriter.StartArray(); |
| 126 | + for (int i = 0; i < accessor.size(0); ++i) { |
| 127 | + jsonWriter.Double(static_cast<double>(accessor[i])); |
| 128 | + } |
| 129 | + jsonWriter.EndArray(); |
| 130 | + } |
| 131 | + |
| 132 | + //! Write an N dimensional tensor for N > 1. |
| 133 | + template<typename T, std::size_t N_DIMS> |
| 134 | + void writeTensor(const ::torch::TensorAccessor<T, N_DIMS>& accessor, |
| 135 | + TRapidJsonLineWriter& jsonWriter) { |
| 136 | + jsonWriter.StartArray(); |
| 137 | + for (int i = 0; i < accessor.size(0); ++i) { |
| 138 | + this->writeTensor(accessor[i], jsonWriter); |
| 139 | + } |
| 140 | + jsonWriter.EndArray(); |
| 141 | + } |
| 142 | + |
| 143 | + //! Write a 3D inference result |
| 144 | + template<typename T> |
| 145 | + void writeInferenceResults(const ::torch::TensorAccessor<T, 3UL>& accessor, |
| 146 | + TRapidJsonLineWriter& jsonWriter) { |
| 147 | + |
| 148 | + jsonWriter.Key(RESULT); |
| 149 | + jsonWriter.StartObject(); |
| 150 | + jsonWriter.Key(INFERENCE); |
| 151 | + this->writeTensor(accessor, jsonWriter); |
| 152 | + jsonWriter.EndObject(); |
| 153 | + } |
| 154 | + |
| 155 | + //! Write a 2D inference result |
| 156 | + template<typename T> |
| 157 | + void writeInferenceResults(const ::torch::TensorAccessor<T, 2UL>& accessor, |
| 158 | + TRapidJsonLineWriter& jsonWriter) { |
| 159 | + |
| 160 | + jsonWriter.Key(RESULT); |
| 161 | + jsonWriter.StartObject(); |
| 162 | + jsonWriter.Key(INFERENCE); |
| 163 | + // The Java side requires a 3D array, so wrap the 2D result in an |
| 164 | + // extra outer array. |
| 165 | + jsonWriter.StartArray(); |
| 166 | + this->writeTensor(accessor, jsonWriter); |
| 167 | + jsonWriter.EndArray(); |
| 168 | + jsonWriter.EndObject(); |
| 169 | + } |
| 170 | + |
| 171 | +private: |
| 172 | + core::CJsonOutputStreamWrapper m_WrappedOutputStream; |
| 173 | +}; |
| 174 | +} |
| 175 | +} |
| 176 | + |
| 177 | +#endif // INCLUDED_ml_torch_CResultWriter_h |
0 commit comments