// Copyright 2023 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef SERVICES_ON_DEVICE_MODEL_ML_CHROME_ML_API_H_ #define SERVICES_ON_DEVICE_MODEL_ML_CHROME_ML_API_H_ #include <cstdint> #include <functional> #include <string> #include "third_party/dawn/include/dawn/dawn_proc_table.h" #include "third_party/dawn/include/dawn/webgpu.h" // This header defines the public interface to the ChromeML shared library. extern "C" { // A function used to handle fatal errors. ChromeMLFatalErrorFn; // A scheduling function used to run arbitrary async tasks. Given to // CreateModelExecutor() and called into by ChromeML as needed. When called, the // value of `context` is the same value given to CreateModelExecutor(). ChromeMLScheduleFn; #if defined(_WIN32) using PlatformFile = void*; #else PlatformFile; #endif // Opaque handle to an instance of a ChromeML model. ChromeMLModel; // Opaque handle to an instance of a ChromeML session. ChromeMLSession; // Opaque handle to an object that allows canceling operations. ChromeMLCancel; // Opaque handle to an instance of a ChromeMLTS model. ChromeMLTSModel; // Opaque handle to a video-frame-specific ML inference engine. ChromeMLInferenceEngine; // Type of the backend to run the model. enum ModelBackendType { … }; // A contiguous byte span. struct ChromeMLByteSpan { … }; // Describes a ChromeML model's underlying tensors. struct ChromeMLModelData { … }; // Describes a model to use with ChromeML. struct ChromeMLModelDescriptor { … }; // Describes an adaptation for a model. struct ChromeMLAdaptationDescriptor { … }; // A status value included with each output chunk. enum class ChromeMLExecutionStatus { … }; // Structure conveying sequential output from an in-progress model execution. struct ChromeMLExecutionOutput { … }; struct ChromeMLTSModelDescriptor { … }; // Status value indicating the result of ad hoc safety classification. enum class ChromeMLSafetyResult { … }; // Function provided from the library that will cancel the corresponding input // and output when called. This is safe to call on any thread. ChromeMLCancelFn; // Receives tokens an other information from a call to ExecuteModel(). This will // be called on the internal thread executing the model. May be multiple times, // and the final invocation will be indicated by the `status` field within // `output`. Note that `output` and any pointer fields therein are only valid // through the extent of the function invocation and must not be retained by // the callee. ChromeMLExecutionOutputFn; // Called with the number of tokens processed after a call to RunModel() // which has the kSave ContextMode set. This will be called on the internal // thread executing the model. ChromeMLContextSavedFn; // Called with the number of tokens after a call to SizeInTokens(). // This will be called on the internal thread executing the model. ChromeMLSizeInTokensFn; // Called with a probability score after a call to Score(). // This will be called on the internal thread executing the model. ChromeMLScoreFn; struct ChromeMLExecuteOptions { … }; // Performance data filled out by GetEstimatedPerformance(). struct ChromeMLPerformanceInfo { … }; // Structure needed to determine if the gpu is blockedlisted. Fields correspond // to that in gpu::WebGpuBlockListParams. struct GpuConfig { … }; struct ChromeMLMetricsFns { … }; struct ChromeMLTSAPI { … }; // IMPORTANT: All functions that call ChromeMLAPI should be annotated with // DISABLE_CFI_DLSYM. // Table of C API functions defined within the library. struct ChromeMLAPI { … }; // Signature of the GetChromeMLAPI() function which the shared library exports. ChromeMLAPIGetter; } // extern "C" #endif // SERVICES_ON_DEVICE_MODEL_ML_CHROME_ML_API_H_