diff --git a/packages/react-native-executorch/cpp/core/conversions.h b/packages/react-native-executorch/cpp/core/conversions.h index d55b995dc7..7a6903c2df 100644 --- a/packages/react-native-executorch/cpp/core/conversions.h +++ b/packages/react-native-executorch/cpp/core/conversions.h @@ -173,6 +173,7 @@ jsi::Array toJsiArray(jsi::Runtime &rt, const std::vector &vec) { return arr; } +/** Helper constant for static_assert in dependent template contexts. */ template inline constexpr bool kAlwaysFalse = false; diff --git a/packages/react-native-executorch/cpp/core/dtype.h b/packages/react-native-executorch/cpp/core/dtype.h index fafa0a8429..479f1015e6 100644 --- a/packages/react-native-executorch/cpp/core/dtype.h +++ b/packages/react-native-executorch/cpp/core/dtype.h @@ -5,6 +5,10 @@ #include namespace rnexecutorch::core::types { + +/** + * Supported tensor data types across the native runtime and JavaScript interface. + */ enum class DType { uint8, int32, @@ -12,12 +16,46 @@ enum class DType { float32 }; +/** + * Parses a string representation into a DType enum value. + * + * @param s The string name of the data type (e.g. "uint8", "int32", "int64", "float32"). + * @return The corresponding DType enum value. + * @throws std::invalid_argument If the string does not match any known DType. + */ DType parseDType(const std::string &s); + +/** + * Converts a DType enum value to its string representation. + * + * @param dtype The DType enum value to convert. + * @return The string representation of the data type. + */ std::string toString(DType dtype); +/** + * Converts a DType enum value to the corresponding ExecuTorch ScalarType. + * + * @param dtype The DType enum value to convert. + * @return The corresponding ExecuTorch ScalarType. + */ executorch::aten::ScalarType toScalarType(DType dtype); + +/** + * Converts an ExecuTorch ScalarType to the corresponding DType enum value. + * + * @param st The ExecuTorch ScalarType to convert. + * @return The corresponding DType enum value. + * @throws std::invalid_argument If the ScalarType is not supported. + */ DType fromScalarType(executorch::aten::ScalarType st); +/** + * Returns the byte size of a single element for the specified DType. + * + * @param dtype The DType enum value. + * @return The size in bytes of a single element of that data type. + */ size_t elementSize(DType dtype); } // namespace rnexecutorch::core::types diff --git a/packages/react-native-executorch/cpp/core/tensor.h b/packages/react-native-executorch/cpp/core/tensor.h index b679e767d9..541606fc3d 100644 --- a/packages/react-native-executorch/cpp/core/tensor.h +++ b/packages/react-native-executorch/cpp/core/tensor.h @@ -26,17 +26,31 @@ namespace types = rnexecutorch::core::types; class TensorHostObject : public jsi::HostObject, public std::enable_shared_from_this { public: + /** Data type of the tensor elements. */ const types::DType dtype_; + /** Dimensions (shape) of the tensor. */ const std::vector shape_; + /** Total number of elements contained in the tensor. */ const size_t numel_; + /** Total memory size of the tensor data buffer in bytes. */ const size_t size_; - // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays): owning runtime-sized byte buffer - std::unique_ptr data_; + /** Owning byte buffer holding the raw tensor data. */ + std::unique_ptr data_; // NOLINT(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays): owning runtime-sized byte buffer + /** ExecuTorch TensorPtr instance wrapping the data buffer. */ executorch::extension::TensorPtr tensor_; + /** Shared mutex guarding concurrent read/write access to the tensor data. */ std::shared_mutex mutex_; + /** + * Constructs a TensorHostObject with the specified shape and data type. + * + * Allocates and zero-initializes the underlying memory buffer for the tensor data. + * + * @param shape The dimensions of the tensor. + * @param dtype The data type of the tensor elements. + */ TensorHostObject(const std::vector &shape, types::DType dtype); jsi::Value get(jsi::Runtime &rt, const jsi::PropNameID &name) override; diff --git a/packages/react-native-executorch/cpp/extensions/cv/utils.h b/packages/react-native-executorch/cpp/extensions/cv/utils.h index 710124b1d9..132663a0a3 100644 --- a/packages/react-native-executorch/cpp/extensions/cv/utils.h +++ b/packages/react-native-executorch/cpp/extensions/cv/utils.h @@ -6,6 +6,13 @@ namespace rnexecutorch::extensions::cv { +/** + * Converts an ExecuTorch DType enum value to the corresponding OpenCV matrix depth constant. + * + * @param dtype The input tensor data type. + * @return The corresponding OpenCV depth constant (e.g. CV_8U, CV_32S, CV_32F). + * @throws std::invalid_argument If the data type is not supported by OpenCV depth representation. + */ inline int dtypeToCvDepth(rnexecutorch::core::types::DType dtype) { switch (dtype) { case rnexecutorch::core::types::DType::uint8: diff --git a/packages/react-native-executorch/cpp/extensions/nlp/tokenizer.h b/packages/react-native-executorch/cpp/extensions/nlp/tokenizer.h index 77a4b9d638..3e05e6f47a 100644 --- a/packages/react-native-executorch/cpp/extensions/nlp/tokenizer.h +++ b/packages/react-native-executorch/cpp/extensions/nlp/tokenizer.h @@ -11,21 +11,44 @@ #include namespace rnexecutorch::extensions::nlp::tokenizer { + +/** + * JSI HostObject wrapping a HuggingFace Tokenizer instance (`tokenizers::HFTokenizer`). + * + * Exposes methods to JavaScript for encoding text to token IDs, decoding token IDs + * to text, and managing tokenizer resources. + */ class TokenizerHostObject : public facebook::jsi::HostObject, public std::enable_shared_from_this { public: - // Loads the tokenizer from `tokenizerPath`; throws std::runtime_error on failure. + /** + * Constructs a TokenizerHostObject by loading a HuggingFace tokenizer configuration file. + * + * @param tokenizerPath File system path to the tokenizer configuration file. + * @throws std::runtime_error If loading the tokenizer fails. + */ explicit TokenizerHostObject(std::string tokenizerPath); facebook::jsi::Value get(facebook::jsi::Runtime &rt, const facebook::jsi::PropNameID &name) override; std::vector getPropertyNames(facebook::jsi::Runtime &rt) override; private: + /** + * Tries to acquire a unique lock on the tokenizer's mutex. + * Throws a facebook::jsi::JSError with contextual error info if the lock cannot be acquired. + * + * @param rt The JSI runtime instance. + * @param context Context description used to generate helpful error messages. + * @return A unique lock protecting the tokenizer. + */ [[nodiscard]] std::unique_lock tryLockUnique(facebook::jsi::Runtime &rt, std::string_view context); + /** File path to the HuggingFace tokenizer JSON configuration file. */ std::string tokenizerPath_; + /** Owning pointer to the underlying HuggingFace tokenizer instance. */ std::unique_ptr tokenizer_; + /** Mutex guarding concurrent access to the tokenizer. */ std::mutex mutex_; };