Inference of ML Models in SQL Server via External Languages

Published 04-26-2021 11:34 AM 1,992 Views
Microsoft

In this blog post, we describe our work on enabling machine learning (ML) inference (aka scoring) of previously trained ML models using the newly introduced language extensions of SQL Server 2019. By implementing a set of APIs, users can interface SQL Server with an external process (such as an ML runtime in our scenario) in order to move data and results between the main execution engine and the external process (which will perform the model scoring). As we will show in this post, this method is more efficient and more intuitive than performing model scoring using SQL Server's ML Services capability via Python scripts.

 

To ground the description of such an external language implementation for model scoring with a concrete example, we will discuss scoring for ONNX models, as well as models that can be converted to ONNX (e.g., TensorFlow, PyTorch, scikit-learn). However, the technique described here is generic and can be used to invoke any other ML runtime.

 

Implementing ONNX as an external language allows users of any existing SQL Server 2019 installation to score ONNX models. There are several benefits to this approach:
  1. Users can take advantage of faster times for scoring ONNX models over data in SQL Server compared to implementations using SQL Server’s Python capabilities. As we will show, example prediction queries (invoking logistic regressions and decision trees) are about 1.3--2x faster when compared to the Python implementation. They are also much easier to express in a few lines of code, as opposed to a few tens of lines for the Python case.
  2. Following an implementation as the one we describe in this blog post for ONNX, users can create other external languages for efficiently invoking any other ML runtime of their choice, such as PyTorch or TensorFlow. Note that we will not release our implementation at this point, but interested users can perform a similar implementation following the design in this blog post.
  3. Given the extensibility framework has a C++ interface, users can directly embed any custom C/C++ code for model scoring just by implementing the API methods we describe below.

Note that another alternative is SQL Server’s native scoring capability using the PREDICT keyword for scoring ONNX models. Although this capability is available on Azure SQL Edge and Azure SQL Managed Instance (in preview), it is not yet present in all SQL Server flavors, including on-premises versions.

 

Example Usage

1. Language Registration

The first step to use ONNX as an external language is to register that language in SQL Server with the following command.

 

CREATE EXTERNAL LANGUAGE ONNX 
FROM (CONTENT=N'onnxextension.zip', FILE_NAME=N'onnxextension.dll')

 

To perform the language registration, we point to the onnxextension.zip file that contains the onnxextension.dll. This dll includes our implementation of the extensibility framework C++ APIs, as we will describe later.

 

2. Stored Procedure Creation

Next, the user can create a stored procedure that uses the ONNX external language we registered above to perform model scoring of a model over some data using the sp_execute_external_script capability. The procedure takes as input the ID of the model to be used for scoring (the model is stored as a binary object in a SQL Server table and is identified by that ID) and a SQL query that defines the data to be used.

 

CREATE OR ALTER PROCEDURE dbo.sp_onnx_scoring (@model_id int, @input_query nvarchar(max))
AS
BEGIN
DECLARE @model_binary varbinary(max) = (SELECT Data FROM models WHERE id = @model_id);
EXECUTE sp_execute_external_script
    @language = N'ONNX', 
    @script = N'', 
    @input_data_1=@input_query,
    @parallel = 1,
    @params = N'@model varbinary(max)',
    @model = @model_binary
WITH RESULT SETS (("length of stay" bigint))
END;

 

The example stored procedure declares (by setting the @language variable) that it is using the ONNX external language extension (created earlier). The input data is read into the @input_data_1 variable and the model is read into the @model_binary and @model variables.

The ‘EXECUTE sp_execute_external_script’ invokes execution of the language extension. The setting of @parallel to 1 enables parallel (multi-threaded) execution of the language extension.

The ‘WITH RESULT SETS’ directive specifies the return types of the results. In this case, the model returns an integer. In general, the type of the result returned by the model should match or be able to be implicitly converted to the type specified in the ‘RESULT SETS’ clause.

 

3. Model Scoring

The stored procedure needs to be saved once and can then be invoked later for scoring different models with input data as follows.

 

EXEC sp_onnx_scoring <model num>, “SELECT <feature list> FROM dbo.patients_table"

 

The ‘EXEC’ command in the example invokes the stored procedure with a model number and a T-SQL query string that specifies the input data consisting of all rows and a subset of columns (used as features for the model) from a database table (patients_table). The model number identifies a model that is stored as a binary string (along with other models) in another table in the database.

 

High-Level Architecture

 

rathijit_0-1615965347898.png

 

Fig. 1. High-level architecture diagram of ONNX extension for SQL Server

 

Figure 1 shows a high-level architecture of the implementation of ONNX as external language in SQL Server. The steps/components marked with a numbered circle are briefly described below.

  1. User registers an external language named ONNX with SQL Server. We describe the creation of this language extension in the Extension Implementation section later in this blog post. Our implementation is packaged in the file ‘onnxextension.zip’ shown in Figure 1.
  2. User executes a T-SQL query that invokes sp_execute_external_script with ONNX as the language.
  3. SQL Server communicates with the Launchpad service to launch the exthost external process (4).
  4. The exthost process loads the extension dll (5) that was registered for ONNX in step 1.
  5. The extension dll loads the ONNX runtime dll (6).
  6. The ONNX runtime dll provides the functionality for scoring the ONNX models.

 

Extension Implementation

 As we mentioned earlier, SQL Server 2019 introduced the extensibility framework that users can exploit to create their own language extension and invoke execution in that language through T-SQL queries. This framework consists of a set of C++ APIs that users can implement to copy data and parameters from SQL Server, perform operations on that data, and return results and performance telemetry to SQL Server. Reference implementations of Java, R, and Python using this extensibility framework have been open-sourced

 

We now briefly discuss how some of the APIs of the SQL Server language extensibility framework can be implemented to support ONNX as an external language. The implementation is compiled into a dll that can then be registered with SQL Server for this external language (step 1 in the architecture description shown earlier).

The Windows and SQL Server data types used in the code snippets below for the implementation are the same as those used in the other open-sourced reference implementations for the extensibility framework, e.g., in Java as an external language.

  • Init: This is called by exthost when the dll associated with the language extension is loaded. Our implementation of ONNX as an external language loads the ONNX runtime (ORT for short) dll in Init. ORT provides the functionality to score ONNX models. The code snipped below shows how the ORT dll can be loaded.

 

	LPCWSTR onnxDllPath = L"onnxruntime.dll";
	std::wstring fullPath = wstr.get();
	fullPath += L"\\onnxruntime.dll";
	auto load_result = LoadLibraryExW(fullPath.c_str(), NULL, 0);
	const OrtApi *api = OrtGetApiBase()->GetApi(ORT_API_VERSION);
	……	
	return SQL_SUCCESS;

 

Note that the ONNX extension dll needs to be built with the delay load dlls option set for linker input properties, with the ORT dll specified as the dll to be delay loaded.

 

The OrtApi type definition is provided by the ORT C runtime API. The ORT_API_VERSION is set to the value corresponding to the ORT version, e.g., 4 for ORT v1.4.

We will call methods provided by the ORT by dereferencing the ‘api’ pointer. In Init, we call the api->CreateEnv(…) method provided by the ORT to create the runtime environment.

The call to Init and its execution corresponds to steps 4 and 5 of the architecture description shown earlier.

 

  • GetInterfaceVersion: This is called by exthost to get the version number of the extensibility API implemented by the extension. For example, the value returned would be 2 for Version 2 of the API.

 

	return EXTERNAL_LANGUAGE_EXTENSION_API;

 

  • InitSession: This is called by exthost once per inference session. The ONNX extension should copy the values of the function arguments to variables in the extension that can be retrieved by the other functions implemented by the extension. For example, the variables can be part of a class, an object of which is allocated on the heap during Init, and then this object is used by the rest of the functions.
  • InitColumn: This is called by exthost once per column (attribute) of the input data on which the model scoring will be done in this inference session. The ONNX extension should copy and save the values of the function arguments.
  • InitParam: This is called by exthost to pass other arguments of the sp_execute_external_script command for this inference session. One of the arguments is the “@model” string along with its value, which was set by the user script to the binary string of the model.  The ONNX extension should copy and save this model, e.g., using memcpy as shown in the code snippet below.

 

	if (!strcmp(reinterpret_cast<const char *>(ParamName), "@model")) {
		SQLPOINTER m_modelPtr = new unsigned char[StrLen_or_Ind];
		std::memcpy(m_modelPtr, ArgValue, StrLen_or_Ind);
	}
	……	
	return SQL_SUCCESS;

 

  • Execute: This is called by exthost to score the model on the input data for this inference session. There are several steps that are executed in the function.
    1. Map Types: First, the ONNX extension maps the SQL types (enums) of the input data to the types understood by the ORT. This can be done by looking up a mapping table that tells how to do the translation.
    2. Create Context: Then, it creates a context for scoring the model by calling ORT methods, e.g., api->CreateSessionOptions(…), api->SetIntraOpNumThreads(…), api-> SetInterOpNumThreads(…), api->SetSessionGraphOptimizationLevel(…), api->CreateSessionFromArray(…), etc.
    3.  Score Model: Then it scores the model by calling ORT methods, e.g., api->CreateTensorWithDataAsOrtValue(…),  api->Run(…)
    4. Save Results: It then saves the predictions returned by the ORT.
    5. Return number of output columns: Finally, it returns the number of columns in the output of the predictions by setting the ‘*OutputSchemaColumnsNumber’ parameter of the Execute function. The return value of the Execute function is SQL_SUCCESS.
  • GetResultColumn: This is called by exthost for each of the columns for the prediction results. The number of columns has been returned by the Execute function as described above. The ONNX extension returns the data type and size for the corresponding column.
  • GetResults: This is called by exthost once for the inference session to get the results of the model predictions. The code snippet below shows that the number of rows in the results as well as the results are returned. Here variable DataBuffer  is of type BYTE* and it holds the prediction results obtained during the Execute function call as described above.

 

	*RowsNumber = elemCount;
	*Data = reinterpret_cast<SQLPOINTER *>(&(DataBuffer));
	……	
	return SQL_SUCCESS;

 

  • CleanupSession: This is called by exthost once for the inference session. The ONNX extension deallocates variables that is allocated for this inference session.

 

Performance Comparisons

We evaluated the performance of the following three different ways of scoring ONNX models with SQL Server. We used a SQL Server 2019 installation.

  1. PREDICT: This uses SQL Server’s built-in PREDICT keyword for scoring ONNX models using the ONNX Runtime. It is currently publicly available only in Azure SQL Edge and Azure SQL Managed Instance (in preview).
  2. Ext. C++: This uses the ONNX as external language capability described in this blog post.
  3. Ext. Python: This uses SQL Server’s capability to invoke Python as an external process for invoking the ONNX Runtime to score ONNX models.

We used an input dataset ranging in size from 10K rows to approx. 3M rows. We measure end-to-end query runtimes, that include the total prediction time over all the rows in the particular table, with scoring for two ML models, namely a Decision Tree and a Logistic Regression. We average the runtimes over five executions of the same query.

 

rathijit_2-1615963858565.png

Fig. 2. Query execution times with inference using different input sizes and models.

 

Figure 2 shows the average query run times as a function of the number of rows in the input data set, which is also equal to the number of predictions done with the models. 

 

We make the following two observations from these experimental results.

  1. Ext. Python is significantly slower (2.7x--30.5x, depending on the model and number of input rows) than PREDICT. As an example, for 3M rows, Ext. Python takes about 2.9x (Decision Tree) to 2.7x (Logistic Regression) as much time as PREDICT.
  2. Ext. C++ results in query run times that are in between PREDICT (1.9x--22.4x slower than PREDICT) and Ext. Python (which is 2.7x--30.5x slower than PREDICT), leading to 1.3--2.1x speedups when compared to Ext. Python. For example, for 3M rows, Ext. C++ takes about 1.9x as much time as PREDICT in contrast to the 2.7x--2.9x time taken by Ext. Python. For approximately 0.3M rows and Decision Tree model, Ext. C++ takes about 2.6x whereas Ext. Python takes about 5.4x as much time as PREDICT, resulting in Ext. C++ being about 2x faster than Ext. Python for scoring the given model on the same input dataset.
 

rathijit_1-1615963235649.png

Fig. 3. Query slowdowns, relative to in-process PREDICT, for different input sizes and models.

 

Figure 3 shows the average query run times relative to that with PREDICT with the same number of input rows. At small number of input rows, the computation for the model scoring is small. So, the overheads of setting up the external execution environment dominates. At larger input sizes, the slowdown values become smaller. In all cases, the slowdowns with Ext. C++ are smaller than with Ext. Python.

  

Summary

SQL Server 2019’s C++ extensibility framework offers a powerful capability to users for calling a wide range of ML model scoring engines to score corresponding ML models on tabular data provided by SQL Server. Our implementation enables users to perform scoring of ONNX models faster and more intuitively than with the existing Python execution. This blog post also provided an overview of how users can write their own ML engine interfacing extensions with the new extensibility framework.

 

Acknowledgments

We thank Ivan Popivanov, Jasraj Dange, Michelle Wallig, Carlo Curino, and members of the SQL Server extensibility team for their help and feedback on this work.

 

References

  1. SQL Server 2019
  2. Native scoring using the PREDICT T-SQL function with SQL machine learning
  3. Machine learning and AI with ONNX in SQL Edge
  4. Machine Learning Services in Azure SQL Managed Instance (preview)
  5. Extensibility architecture in SQL Server Machine Learning Services
  6. Python language extension in SQL Server Machine Learning Services
  7. R language extension in SQL Server Machine Learning Services
  8. sp_execute_external_script
  9. Extending Relational Query Processing with ML Inference
  10. SQL Server Language Extensions
  11. CREATE EXTERNAL LANGUAGE (Transact-SQL)
  12. Extensibility architecture in SQL Server Language Extensions
  13. Extensibility Framework API for SQL Server
  14. SQL Server Language Extensions project
  15. ONNX Runtime
  16. ONNX Runtime C API
%3CLINGO-SUB%20id%3D%22lingo-sub-2216226%22%20slang%3D%22en-US%22%3EInference%20of%20ML%20Models%20in%20SQL%20Server%20via%20External%20Languages%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2216226%22%20slang%3D%22en-US%22%3E%3CP%3EIn%20this%20blog%20post%2C%20we%20describe%20our%20work%20on%20enabling%20machine%20learning%20(ML)%20inference%20(aka%20scoring)%20of%20previously%20trained%20ML%20models%20using%20the%20newly%20introduced%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Flanguage-extensions%2Fconcepts%2Fextensibility-framework%3Fview%3Dsql-server-ver15%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3Elanguage%20extensions%3C%2FA%3E%26nbsp%3Bof%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fwww.microsoft.com%2Fen-us%2Fsql-server%2Fsql-server-2019%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3ESQL%20Server%202019%3C%2FA%3E.%20By%20implementing%20a%20set%20of%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Flanguage-extensions%2Freference%2Fextensibility-framework-api%3Fview%3Dsql-server-ver15%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EAPIs%3C%2FA%3E%2C%20users%20can%20interface%20SQL%20Server%20with%20an%20external%20process%20(such%20as%20an%20ML%20runtime%20in%20our%20scenario)%20in%20order%20to%20move%20data%20and%20results%20between%20the%20main%20execution%20engine%20and%20the%20external%20process%20(which%20will%20perform%20the%20model%20scoring).%20As%20we%20will%20show%20in%20this%20post%2C%20t%3CSPAN%20style%3D%22font-family%3A%20inherit%3B%22%3Ehis%20method%20is%20%3C%2FSPAN%3E%3CEM%20style%3D%22font-family%3A%20inherit%3B%22%3Emore%20efficient%3C%2FEM%3E%3CSPAN%20style%3D%22font-family%3A%20inherit%3B%22%3E%20and%20%3C%2FSPAN%3E%3CEM%20style%3D%22font-family%3A%20inherit%3B%22%3Emore%20intuitive%3C%2FEM%3E%3CSPAN%20style%3D%22font-family%3A%20inherit%3B%22%3E%20than%20performing%20model%20scoring%20using%20SQL%20Server's%26nbsp%3B%3C%2FSPAN%3E%3CA%20style%3D%22font-family%3A%20inherit%3B%20background-color%3A%20%23ffffff%3B%22%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Fmachine-learning%2Fconcepts%2Fextensibility-framework%3Fview%3Dsql-server-ver15%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EML%20Services%3C%2FA%3E%3CSPAN%20style%3D%22font-family%3A%20inherit%3B%22%3E%26nbsp%3Bcapability%3C%2FSPAN%3E%3CSPAN%20style%3D%22font-family%3A%20inherit%3B%22%3E%20via%26nbsp%3B%3C%2FSPAN%3E%3CA%20style%3D%22font-family%3A%20inherit%3B%20background-color%3A%20%23ffffff%3B%22%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Fmachine-learning%2Fconcepts%2Fextension-python%3Fview%3Dsql-server-ver15%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EPython%3C%2FA%3E%3CSPAN%20style%3D%22font-family%3A%20inherit%3B%22%3E%20scripts.%20%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20style%3D%22font-family%3A%20inherit%3B%22%3ETo%20ground%20the%20description%20of%20such%20an%20external%20language%20implementation%20for%20model%20scoring%20with%20a%20concrete%20example%2C%20we%20will%20discuss%20scoring%20for%26nbsp%3B%3C%2FSPAN%3E%3CA%20style%3D%22font-family%3A%20inherit%3B%20background-color%3A%20%23ffffff%3B%22%20href%3D%22https%3A%2F%2Fonnx.ai%2F%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3EONNX%3C%2FA%3E%3CSPAN%20style%3D%22font-family%3A%20inherit%3B%22%3E%20models%2C%20as%20well%20as%20models%20that%20can%20be%20converted%20to%20ONNX%20(e.g.%2C%20TensorFlow%2C%20PyTorch%2C%20scikit-learn)%3C%2FSPAN%3E%3CSPAN%20style%3D%22font-family%3A%20inherit%3B%22%3E.%20However%2C%20the%20technique%20described%20here%20is%20generic%20and%20can%20be%20used%20to%20invoke%20any%20other%20ML%20runtime.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%3EImplementing%20ONNX%20as%20an%20external%20language%20allows%20users%20of%20any%20existing%20SQL%20Server%202019%20installation%20to%20score%20ONNX%20models.%20There%20are%20several%20benefits%20to%20this%20approach%3A%3C%2FDIV%3E%0A%3COL%3E%0A%3CLI%3EUsers%20can%20take%20advantage%20of%20faster%20times%20for%20scoring%20ONNX%20models%20over%20data%20in%20SQL%20Server%20compared%20to%20implementations%20using%20SQL%20Server%E2%80%99s%20Python%20capabilities.%20As%20we%20will%20show%2C%20example%20prediction%20queries%20(invoking%20logistic%20regressions%20and%20decision%20trees)%20are%20about%201.3%3CFONT%20style%3D%22font-family%3A%20inherit%3B%22%3E--2x%3C%2FFONT%3E%3CSPAN%20style%3D%22font-family%3A%20inherit%3B%22%3E%20faster%26nbsp%3B%3C%2FSPAN%3Ewhen%20compared%20to%20the%20Python%20implementation.%20They%20are%20also%20much%20easier%20to%20express%20in%20a%20few%20lines%20of%20code%2C%20as%20opposed%20to%20a%20few%20tens%20of%20lines%20for%20the%20Python%20case.%3C%2FLI%3E%0A%3CLI%3EFollowing%20an%20implementation%20as%20the%20one%20we%20describe%20in%20this%20blog%20post%20for%20ONNX%2C%20users%20can%20create%20other%20external%20languages%20for%20efficiently%20invoking%20any%20other%20ML%20runtime%20of%20their%20choice%2C%20such%20as%20PyTorch%20or%20TensorFlow.%20Note%20that%20we%20will%20not%20release%20our%20implementation%20at%20this%20point%2C%20but%20interested%20users%20can%20perform%20a%20similar%20implementation%20following%20the%20design%20in%20this%20blog%20post.%3C%2FLI%3E%0A%3CLI%3EGiven%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Flanguage-extensions%2Fconcepts%2Fextensibility-framework%3Fview%3Dsql-server-ver15%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3Eextensibility%20framework%3C%2FA%3E%20has%20a%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Flanguage-extensions%2Freference%2Fextensibility-framework-api%3Fview%3Dsql-server-ver15%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3EC%2B%2B%20interface%3C%2FA%3E%2C%20users%20can%20directly%20embed%20any%20custom%20C%2FC%2B%2B%20code%20for%20model%20scoring%20just%20by%20implementing%20the%20API%20methods%20we%20describe%20below.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3ENote%20that%20another%20alternative%20is%20SQL%20Server%E2%80%99s%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Fmachine-learning%2Fpredictions%2Fnative-scoring-predict-transact-sql%3Fview%3Dsql-server-ver15%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Enative%20scoring%20capability%3C%2FA%3E%20using%20the%20PREDICT%20keyword%20for%26nbsp%3B%3CA%20href%3D%22http%3A%2F%2Fcidrdb.org%2Fcidr2020%2Fpapers%2Fp24-karanasos-cidr20.pdf%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Escoring%20ONNX%20models%3C%2FA%3E.%20Although%20this%20capability%20is%20available%20on%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fazure-sql-edge%2Fonnx-overview%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EAzure%20SQL%20Edge%3C%2FA%3E%20and%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fazure-sql%2Fmanaged-instance%2Fmachine-learning-services-overview%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EAzure%20SQL%20Managed%20Instance%20(in%20preview)%3C%2FA%3E%2C%20it%20is%20not%20yet%20present%20in%20all%20SQL%20Server%20flavors%2C%20including%20on-premises%20versions.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--499229265%22%20id%3D%22toc-hId--499229295%22%3E%3CSTRONG%3EExample%20Usage%3C%2FSTRONG%3E%3C%2FH2%3E%0A%3CH4%20id%3D%22toc-hId--1605619150%22%20id%3D%22toc-hId--1605619180%22%3E1.%20Language%20Registration%3C%2FH4%3E%0A%3CP%3EThe%20first%20step%20to%20use%20ONNX%20as%20an%20external%20language%20is%20to%20register%20that%20language%20in%20SQL%20Server%20with%20the%20following%20command.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ECREATE%20EXTERNAL%20LANGUAGE%20ONNX%20%0AFROM%20(CONTENT%3DN'onnxextension.zip'%2C%20FILE_NAME%3DN'onnxextension.dll')%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20perform%20the%20language%20registration%2C%20we%20point%20to%20the%20onnxextension.zip%20file%20that%20contains%20the%20onnxextension.dll.%20This%20dll%20includes%20our%20implementation%20of%20the%20extensibility%20framework%20C%2B%2B%20APIs%2C%20as%20we%20will%20describe%20later.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH4%20id%3D%22toc-hId-881893683%22%20id%3D%22toc-hId-881893653%22%3E2.%20Stored%20Procedure%20Creation%3C%2FH4%3E%0A%3CP%3ENext%2C%20the%20user%20can%20create%20a%20stored%20procedure%20that%20uses%20the%20ONNX%20external%20language%20we%20registered%20above%20to%20perform%20model%20scoring%20of%20a%20model%20over%20some%20data%20using%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Fsystem-stored-procedures%2Fsp-execute-external-script-transact-sql%3Fview%3Dsql-server-ver15%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3Esp_execute_external_script%3C%2FA%3E%26nbsp%3Bcapability.%20The%20procedure%20takes%20as%20input%20the%20ID%20of%20the%20model%20to%20be%20used%20for%20scoring%20(the%20model%20is%20stored%20as%20a%20binary%20object%20in%20a%20SQL%20Server%20table%20and%20is%20identified%20by%20that%20ID)%20and%20a%20SQL%20query%20that%20defines%20the%20data%20to%20be%20used.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ECREATE%20OR%20ALTER%20PROCEDURE%20dbo.sp_onnx_scoring%20(%40model_id%20int%2C%20%40input_query%20nvarchar(max))%0AAS%0ABEGIN%0ADECLARE%20%40model_binary%20varbinary(max)%20%3D%20(SELECT%20Data%20FROM%20models%20WHERE%20id%20%3D%20%40model_id)%3B%0AEXECUTE%20sp_execute_external_script%0A%20%20%20%20%40language%20%3D%20N'ONNX'%2C%20%0A%20%20%20%20%40script%20%3D%20N''%2C%20%0A%20%20%20%20%40input_data_1%3D%40input_query%2C%0A%20%20%20%20%40parallel%20%3D%201%2C%0A%20%20%20%20%40params%20%3D%20N'%40model%20varbinary(max)'%2C%0A%20%20%20%20%40model%20%3D%20%40model_binary%0AWITH%20RESULT%20SETS%20((%22length%20of%20stay%22%20bigint))%0AEND%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20example%20stored%20procedure%20declares%20(by%20setting%20the%20%40language%20variable)%20that%20it%20is%20using%20the%20ONNX%20external%20language%20extension%20(created%20earlier).%20The%20input%20data%20is%20read%20into%20the%20%40input_data_1%20variable%20and%20the%20model%20is%20read%20into%20the%20%40model_binary%20and%20%40model%20variables.%3C%2FP%3E%0A%3CP%3EThe%20%E2%80%98EXECUTE%20sp_execute_external_script%E2%80%99%20invokes%20execution%20of%20the%20language%20extension.%20The%20setting%20of%20%40parallel%20to%201%20enables%20parallel%20(multi-threaded)%20execution%20of%20the%20language%20extension.%3C%2FP%3E%0A%3CP%3EThe%20%E2%80%98WITH%20RESULT%20SETS%E2%80%99%20directive%20specifies%20the%20return%20types%20of%20the%20results.%20In%20this%20case%2C%20the%20model%20returns%20an%20integer.%20In%20general%2C%20the%20type%20of%20the%20result%20returned%20by%20the%20model%20should%20match%20or%20be%20able%20to%20be%20implicitly%20converted%20to%20the%20type%20specified%20in%20the%20%E2%80%98RESULT%20SETS%E2%80%99%20clause.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH4%20id%3D%22toc-hId--925560780%22%20id%3D%22toc-hId--925560810%22%3E3.%20Model%20Scoring%3C%2FH4%3E%0A%3CP%3EThe%20stored%20procedure%20needs%20to%20be%20saved%20once%20and%20can%20then%20be%20invoked%20later%20for%20scoring%20different%20models%20with%20input%20data%20as%20follows.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3EEXEC%20sp_onnx_scoring%20%3CMODEL%20num%3D%22%22%3E%2C%20%E2%80%9CSELECT%20%3CFEATURE%20list%3D%22%22%3E%20FROM%20dbo.patients_table%22%3C%2FFEATURE%3E%3C%2FMODEL%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20%E2%80%98EXEC%E2%80%99%20command%20in%20the%20example%20invokes%20the%20stored%20procedure%20with%20a%20model%20number%20and%20a%20T-SQL%20query%20string%20that%20specifies%20the%20input%20data%20consisting%20of%20all%20rows%20and%20a%20subset%20of%20columns%20(used%20as%20features%20for%20the%20model)%20from%20a%20database%20table%20(patients_table).%20The%20model%20number%20identifies%20a%20model%20that%20is%20stored%20as%20a%20binary%20string%20(along%20with%20other%20models)%20in%20another%20table%20in%20the%20database.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-860887475%22%20id%3D%22toc-hId-860887445%22%3E%3CSTRONG%3EHigh-Level%20Architecture%3C%2FSTRONG%3E%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22System%20Architecture%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F264748iC04AD9F4DA14F1B8%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22rathijit_0-1615965347898.png%22%20alt%3D%22rathijit_0-1615965347898.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20align%3D%22center%22%3E%3CEM%3EFig.%201.%20High-level%20architecture%20diagram%20of%20ONNX%20extension%20for%20SQL%20Server%3C%2FEM%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFigure%201%20shows%20a%20high-level%20architecture%20of%20the%20implementation%20of%20ONNX%20as%20external%20language%20in%20SQL%20Server.%20The%20steps%2Fcomponents%20marked%20with%20a%20numbered%20circle%20are%20briefly%20described%20below.%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EUser%20registers%20an%20external%20language%20named%20ONNX%20with%20SQL%20Server.%20We%20describe%20the%20creation%20of%20this%20language%20extension%20in%20the%20Extension%20Implementation%20section%20later%20in%20this%20blog%20post.%20Our%20implementation%20is%20packaged%20in%20the%20file%20%E2%80%98onnxextension.zip%E2%80%99%20shown%20in%20Figure%201.%3C%2FLI%3E%0A%3CLI%3EUser%20executes%20a%20T-SQL%20query%20that%20invokes%20sp_execute_external_script%20with%20ONNX%20as%20the%20language.%3C%2FLI%3E%0A%3CLI%3ESQL%20Server%20communicates%20with%20the%20Launchpad%20service%20to%20launch%20the%20exthost%20external%20process%20(4).%3C%2FLI%3E%0A%3CLI%3EThe%20exthost%20process%20loads%20the%20extension%20dll%20(5)%20that%20was%20registered%20for%20ONNX%20in%20step%201.%3C%2FLI%3E%0A%3CLI%3EThe%20extension%20dll%20loads%20the%20ONNX%20runtime%20dll%20(6).%3C%2FLI%3E%0A%3CLI%3EThe%20ONNX%20runtime%20dll%20provides%20the%20functionality%20for%20scoring%20the%20ONNX%20models.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--946566988%22%20id%3D%22toc-hId--946567018%22%3E%3CSTRONG%3EExtension%20Implementation%3C%2FSTRONG%3E%3C%2FH2%3E%0A%3CP%3E%26nbsp%3BAs%20we%20mentioned%20earlier%2C%20SQL%20Server%202019%20introduced%20the%20extensibility%20framework%26nbsp%3Bthat%20users%20can%20exploit%20to%20create%20their%20own%20language%20extension%26nbsp%3Band%20invoke%20execution%20in%20that%20language%20through%20T-SQL%20queries.%20This%20framework%20consists%20of%20a%20set%20of%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Flanguage-extensions%2Freference%2Fextensibility-framework-api%3Fview%3Dsql-server-ver15%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EC%2B%2B%20APIs%3C%2FA%3E%20that%20users%20can%20implement%20to%20copy%20data%20and%20parameters%20from%20SQL%20Server%2C%20perform%20operations%20on%20that%20data%2C%20and%20return%20results%20and%20performance%20telemetry%20to%20SQL%20Server.%26nbsp%3BReference%20implementations%20of%20Java%2C%20R%2C%20and%20Python%20using%20this%20extensibility%20framework%20have%20been%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fmicrosoft%2Fsql-server-language-extensions%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Eopen-sourced%3C%2FA%3E.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWe%20now%20briefly%20discuss%20how%20some%20of%20the%20APIs%20of%20the%20SQL%20Server%20language%20extensibility%20framework%20can%20be%20implemented%20to%20support%20ONNX%20as%20an%20external%20language.%20The%20implementation%20is%20compiled%20into%20a%20dll%20that%20can%20then%20be%20registered%20with%20SQL%20Server%20for%20this%20external%20language%20(step%201%20in%20the%20architecture%20description%20shown%20earlier).%3C%2FP%3E%0A%3CP%3EThe%20Windows%20and%20SQL%20Server%20data%20types%20used%20in%20the%20code%20snippets%20below%20for%20the%20implementation%20are%20the%20same%20as%20those%20used%20in%20the%20other%20open-sourced%20reference%20implementations%20for%20the%20extensibility%20framework%2C%20e.g.%2C%20in%20Java%20as%20an%20external%20language.%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3EInit%3C%2FSTRONG%3E%3A%20This%20is%20called%20by%20exthost%20when%20the%20dll%20associated%20with%20the%20language%20extension%20is%20loaded.%20Our%20implementation%20of%20ONNX%20as%20an%20external%20language%20loads%20the%20ONNX%20runtime%20(ORT%20for%20short)%20dll%20in%20Init.%20ORT%20provides%20the%20functionality%20to%20score%20ONNX%20models.%20The%20code%20snipped%20below%20shows%20how%20the%20ORT%20dll%20can%20be%20loaded.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-cpp%22%3E%3CCODE%3E%20LPCWSTR%20onnxDllPath%20%3D%20L%22onnxruntime.dll%22%3B%0A%20std%3A%3Awstring%20fullPath%20%3D%20wstr.get()%3B%0A%20fullPath%20%2B%3D%20L%22%5C%5Connxruntime.dll%22%3B%0A%20auto%20load_result%20%3D%20LoadLibraryExW(fullPath.c_str()%2C%20NULL%2C%200)%3B%0A%20const%20OrtApi%20*api%20%3D%20OrtGetApiBase()-%26gt%3BGetApi(ORT_API_VERSION)%3B%0A%20%E2%80%A6%E2%80%A6%20%0A%20return%20SQL_SUCCESS%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-30px%22%3ENote%20that%20the%20ONNX%20extension%20dll%20needs%20to%20be%20built%20with%20the%20delay%20load%20dlls%20option%20set%20for%20linker%20input%20properties%2C%20with%20the%20ORT%20dll%20specified%20as%20the%20dll%20to%20be%20delay%20loaded.%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-30px%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-30px%22%3EThe%20OrtApi%20type%20definition%20is%20provided%20by%20the%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fmicrosoft%2Fonnxruntime%2Fblob%2Fmaster%2Fdocs%2FC_API.md%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EORT%20C%20runtime%20API%3C%2FA%3E.%20The%20ORT_API_VERSION%20is%20set%20to%20the%20value%20corresponding%20to%20the%20ORT%20version%2C%20e.g.%2C%204%20for%20ORT%20v1.4.%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-30px%22%3EWe%20will%20call%20methods%20provided%20by%20the%20ORT%20by%20dereferencing%20the%20%E2%80%98api%E2%80%99%20pointer.%20In%20Init%2C%20we%20call%20the%20api-%26gt%3BCreateEnv(%E2%80%A6)%20method%20provided%20by%20the%20ORT%20to%20create%20the%20runtime%20environment.%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-30px%22%3EThe%20call%20to%20Init%20and%20its%20execution%20corresponds%20to%20steps%204%20and%205%20of%20the%20architecture%20description%20shown%20earlier.%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-30px%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3EGetInterfaceVersion%3C%2FSTRONG%3E%3A%20This%20is%20called%20by%20exthost%20to%20get%20the%20version%20number%20of%20the%20extensibility%20API%20implemented%20by%20the%20extension.%20For%20example%2C%20the%20value%20returned%20would%20be%202%20for%20Version%202%20of%20the%20API.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-cpp%22%3E%3CCODE%3E%20return%20EXTERNAL_LANGUAGE_EXTENSION_API%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3EInitSession%3C%2FSTRONG%3E%3A%20This%20is%20called%20by%20exthost%20once%20per%20inference%20session.%20The%20ONNX%20extension%20should%20copy%20the%20values%20of%20the%20function%20arguments%20to%20variables%20in%20the%20extension%20that%20can%20be%20retrieved%20by%20the%20other%20functions%20implemented%20by%20the%20extension.%20For%20example%2C%20the%20variables%20can%20be%20part%20of%20a%20class%2C%20an%20object%20of%20which%20is%20allocated%20on%20the%20heap%20during%20Init%2C%20and%20then%20this%20object%20is%20used%20by%20the%20rest%20of%20the%20functions.%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3EInitColumn%3C%2FSTRONG%3E%3A%20This%20is%20called%20by%20exthost%20once%20per%20column%20(attribute)%20of%20the%20input%20data%20on%20which%20the%20model%20scoring%20will%20be%20done%20in%20this%20inference%20session.%20The%20ONNX%20extension%20should%20copy%20and%20save%20the%20values%20of%20the%20function%20arguments.%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3EInitParam%3C%2FSTRONG%3E%3A%20This%20is%20called%20by%20exthost%20to%20pass%20other%20arguments%20of%20the%20sp_execute_external_script%20command%20for%20this%20inference%20session.%20One%20of%20the%20arguments%20is%20the%20%E2%80%9C%40model%E2%80%9D%20string%20along%20with%20its%20value%2C%20which%20was%20set%20by%20the%20user%20script%20to%20the%20binary%20string%20of%20the%20model.%26nbsp%3B%20The%20ONNX%20extension%20should%20copy%20and%20save%20this%20model%2C%20e.g.%2C%20using%20memcpy%20as%20shown%20in%20the%20code%20snippet%20below.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-cpp%22%3E%3CCODE%3E%20if%20(!strcmp(reinterpret_cast%3CCONST%20char%3D%22%22%3E(ParamName)%2C%20%22%40model%22))%20%7B%0A%20%20SQLPOINTER%20m_modelPtr%20%3D%20new%20unsigned%20char%5BStrLen_or_Ind%5D%3B%0A%20%20std%3A%3Amemcpy(m_modelPtr%2C%20ArgValue%2C%20StrLen_or_Ind)%3B%0A%20%7D%0A%20%E2%80%A6%E2%80%A6%20%0A%20return%20SQL_SUCCESS%3B%0A%3C%2FCONST%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3EExecute%3C%2FSTRONG%3E%3A%20This%20is%20called%20by%20exthost%20to%20score%20the%20model%20on%20the%20input%20data%20for%20this%20inference%20session.%20There%20are%20several%20steps%20that%20are%20executed%20in%20the%20function.%3COL%3E%0A%3CLI%3E%3CEM%3EMap%20Types%3C%2FEM%3E%3A%20First%2C%20the%20ONNX%20extension%20maps%20the%20SQL%20types%20(enums)%20of%20the%20input%20data%20to%20the%20types%20understood%20by%20the%20ORT.%20This%20can%20be%20done%20by%20looking%20up%20a%20mapping%20table%20that%20tells%20how%20to%20do%20the%20translation.%3C%2FLI%3E%0A%3CLI%3E%3CEM%3ECreate%20Context%3C%2FEM%3E%3A%20Then%2C%20it%20creates%20a%20context%20for%20scoring%20the%20model%20by%20calling%20ORT%20methods%2C%20e.g.%2C%20api-%26gt%3BCreateSessionOptions(%E2%80%A6)%2C%20api-%26gt%3BSetIntraOpNumThreads(%E2%80%A6)%2C%20api-%26gt%3B%20SetInterOpNumThreads(%E2%80%A6)%2C%20api-%26gt%3BSetSessionGraphOptimizationLevel(%E2%80%A6)%2C%20api-%26gt%3BCreateSessionFromArray(%E2%80%A6)%2C%20etc.%3C%2FLI%3E%0A%3CLI%3E%26nbsp%3B%3CEM%3EScore%20Model%3C%2FEM%3E%3A%20Then%20it%20scores%20the%20model%20by%20calling%20ORT%20methods%2C%20e.g.%2C%20api-%26gt%3BCreateTensorWithDataAsOrtValue(%E2%80%A6)%2C%20%26nbsp%3Bapi-%26gt%3BRun(%E2%80%A6)%3C%2FLI%3E%0A%3CLI%3E%3CEM%3ESave%20Results%3C%2FEM%3E%3A%20It%20then%20saves%20the%20predictions%20returned%20by%20the%20ORT.%3C%2FLI%3E%0A%3CLI%3E%3CEM%3EReturn%20number%20of%20output%20columns%3C%2FEM%3E%3A%20Finally%2C%20it%20returns%20the%20number%20of%20columns%20in%20the%20output%20of%20the%20predictions%20by%20setting%20the%20%E2%80%98*OutputSchemaColumnsNumber%E2%80%99%20parameter%20of%20the%20Execute%20function.%20The%20return%20value%20of%20the%20Execute%20function%20is%20SQL_SUCCESS.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3EGetResultColumn%3C%2FSTRONG%3E%3A%20This%20is%20called%20by%20exthost%20for%20each%20of%20the%20columns%20for%20the%20prediction%20results.%20The%20number%20of%20columns%20has%20been%20returned%20by%20the%20Execute%20function%20as%20described%20above.%20The%20ONNX%20extension%20returns%20the%20data%20type%20and%20size%20for%20the%20corresponding%20column.%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3EGetResults%3C%2FSTRONG%3E%3A%20This%20is%20called%20by%20exthost%20once%20for%20the%20inference%20session%20to%20get%20the%20results%20of%20the%20model%20predictions.%20The%20code%20snippet%20below%20shows%20that%20the%20number%20of%20rows%20in%20the%20results%20as%20well%20as%20the%20results%20are%20returned.%20Here%20variable%20DataBuffer%26nbsp%3B%20is%20of%20type%20BYTE*%20and%20it%20holds%20the%20prediction%20results%20obtained%20during%20the%20Execute%20function%20call%20as%20described%20above.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-cpp%22%3E%3CCODE%3E%20*RowsNumber%20%3D%20elemCount%3B%0A%20*Data%20%3D%20reinterpret_cast%3CSQLPOINTER%3E(%26amp%3B(DataBuffer))%3B%0A%20%E2%80%A6%E2%80%A6%20%0A%20return%20SQL_SUCCESS%3B%3C%2FSQLPOINTER%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3ECleanupSession%3C%2FSTRONG%3E%3A%20This%20is%20called%20by%20exthost%20once%20for%20the%20inference%20session.%20The%20ONNX%20extension%20deallocates%20variables%20that%20is%20allocated%20for%20this%20inference%20session.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1540945845%22%20id%3D%22toc-hId-1540945815%22%3E%3CSTRONG%3EPerformance%20Comparisons%3C%2FSTRONG%3E%3C%2FH2%3E%0A%3CP%3EWe%20evaluated%20the%20performance%20of%20the%20following%20three%20different%20ways%20of%20scoring%20ONNX%20models%20with%20SQL%20Server.%20We%20used%20a%20SQL%20Server%202019%20installation.%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3E%3CSTRONG%3EPREDICT%3C%2FSTRONG%3E%3A%20This%20uses%20SQL%20Server%E2%80%99s%20built-in%20PREDICT%20keyword%20for%26nbsp%3Bscoring%20ONNX%20models%20using%20the%20ONNX%20Runtime.%20It%20is%20currently%20publicly%20available%20only%20in%26nbsp%3BAzure%20SQL%20Edge%26nbsp%3Band%26nbsp%3BAzure%20SQL%20Managed%20Instance%20(in%20preview).%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3EExt.%20C%2B%2B%3C%2FSTRONG%3E%3A%20This%20uses%20the%20ONNX%20as%20external%20language%20capability%20described%20in%20this%20blog%20post.%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3EExt%3C%2FSTRONG%3E.%20%3CSTRONG%3EPython%3C%2FSTRONG%3E%3A%20This%20uses%20SQL%20Server%E2%80%99s%20capability%20to%20invoke%20Python%20as%20an%20external%20process%20for%20invoking%20the%20ONNX%20Runtime%20to%20score%20ONNX%20models.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3EWe%20used%20an%20input%20dataset%20ranging%20in%20size%20from%2010K%20rows%20to%20approx.%203M%20rows.%20We%20measure%20end-to-end%20query%20runtimes%2C%26nbsp%3Bthat%20include%20the%20total%20prediction%20time%20over%20all%20the%20rows%20in%20the%20particular%20table%2C%20with%20scoring%20for%20two%20ML%20models%2C%20namely%20a%20Decision%20Tree%20and%20a%20Logistic%20Regression.%20We%20average%20the%20runtimes%20over%20five%20executions%20of%20the%20same%20query.%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorrathijit_12%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22Query%20execution%20times%20with%20Decision%20Tree%20and%20Logistic%20Regression%20models%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F264746iF5FD2D3F0F8882CE%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22rathijit_2-1615963858565.png%22%20alt%3D%22rathijit_2-1615963858565.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20align%3D%22center%22%3E%3CEM%3EFig.%202.%20Query%20execution%20times%20with%20inference%20using%20different%20input%20sizes%20and%20models.%3C%2FEM%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFigure%202%20shows%20the%20average%20query%20run%20times%20as%20a%20function%20of%20the%20number%20of%20rows%20in%20the%20input%20data%20set%2C%20which%20is%20also%20equal%20to%20the%20number%20of%20predictions%20done%20with%20the%20models.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWe%20make%20the%20following%20two%20observations%20from%20these%20experimental%20results.%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EExt.%20Python%20is%20significantly%20slower%20(2.7x--30.5x%2C%20depending%20on%20the%20model%20and%20number%20of%20input%20rows)%20than%20PREDICT.%20As%20an%20example%2C%20for%203M%20rows%2C%20Ext.%20Python%20takes%20about%202.9x%20(Decision%20Tree)%20to%202.7x%20(Logistic%20Regression)%20as%20much%20time%20as%20PREDICT.%3C%2FLI%3E%0A%3CLI%3EExt.%20C%2B%2B%20results%20in%20query%20run%20times%20that%20are%20in%20between%20PREDICT%20(1.9x--22.4x%20slower%20than%20PREDICT)%20and%20Ext.%20Python%20(which%20is%202.7x--30.5x%20slower%20than%20PREDICT)%2C%20leading%20to%201.3--2.1x%20speedups%20when%20compared%20to%20Ext.%20Python.%20For%20example%2C%20for%203M%20rows%2C%20Ext.%20C%2B%2B%20takes%20about%201.9x%20as%20much%20time%20as%20PREDICT%20in%20contrast%20to%20the%202.7x--2.9x%20time%20taken%20by%20Ext.%20Python.%20For%20approximately%200.3M%20rows%20and%20Decision%20Tree%20model%2C%20Ext.%20C%2B%2B%26nbsp%3Btakes%20about%202.6x%20whereas%20Ext.%20Python%20takes%20about%205.4x%20as%20much%20time%20as%20PREDICT%2C%20resulting%20in%26nbsp%3BExt.%20C%2B%2B%20being%20about%202x%20faster%20than%26nbsp%3BExt.%20Python%20for%20scoring%20the%20given%20model%20on%20the%20same%20input%20dataset.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CDIV%20id%3D%22tinyMceEditorrathijit_15%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22Query%20slowdowns%20relative%20to%20in-process%20PREDICT%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F264745i4F082D2CAA535FF5%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22rathijit_1-1615963235649.png%22%20alt%3D%22rathijit_1-1615963235649.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20align%3D%22center%22%3E%3CEM%3EFig.%203.%20Query%20slowdowns%2C%20relative%20to%20in-process%20PREDICT%2C%20for%20different%20input%20sizes%20and%20models.%3C%2FEM%3E%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorrathijit_16%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3EFigure%203%20shows%20the%20average%20query%20run%20times%20relative%20to%20that%20with%20PREDICT%20with%20the%20same%20number%20of%20input%20rows.%20At%20small%20number%20of%20input%20rows%2C%20the%20computation%20for%20the%20model%20scoring%20is%20small.%20So%2C%20the%20overheads%20of%20setting%20up%20the%20external%20execution%20environment%20dominates.%20At%20larger%20input%20sizes%2C%20the%20slowdown%20values%20become%20smaller.%20In%20all%20cases%2C%20the%20slowdowns%20with%20Ext.%20C%2B%2B%20are%20smaller%20than%20with%20Ext.%20Python.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--266508618%22%20id%3D%22toc-hId--266508648%22%3E%3CSTRONG%3ESummary%3C%2FSTRONG%3E%3C%2FH2%3E%0A%3CP%3ESQL%20Server%202019%E2%80%99s%20C%2B%2B%20extensibility%20framework%20offers%20a%20powerful%20capability%20to%20users%20for%20calling%20a%20wide%20range%20of%20ML%20model%20scoring%20engines%20to%20score%20corresponding%20ML%20models%20on%20tabular%20data%20provided%20by%20SQL%20Server.%20Our%20implementation%20enables%20users%20to%20perform%20scoring%20of%20ONNX%20models%20faster%20and%20more%20intuitively%20than%20with%20the%20existing%20Python%20execution.%20This%20blog%20post%20also%20provided%20an%20overview%20of%20how%20users%20can%20write%20their%20own%20ML%20engine%20interfacing%20extensions%20with%20the%20new%20extensibility%20framework.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--2073963081%22%20id%3D%22toc-hId--2073963111%22%3E%3CSTRONG%3EAcknowledgments%3C%2FSTRONG%3E%3C%2FH2%3E%0A%3CP%3EWe%20thank%20Ivan%20Popivanov%2C%20Jasraj%20Dange%2C%20Michelle%20Wallig%2C%20Carlo%20Curino%2C%20and%20members%20of%20the%20SQL%20Server%20extensibility%20team%20for%20their%20help%20and%20feedback%20on%20this%20work.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-413549752%22%20id%3D%22toc-hId-413549722%22%3E%3CSTRONG%3EReferences%3C%2FSTRONG%3E%3C%2FH2%3E%0A%3COL%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fwww.microsoft.com%2Fen-us%2Fsql-server%2Fsql-server-2019%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3ESQL%20Server%202019%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Fmachine-learning%2Fpredictions%2Fnative-scoring-predict-transact-sql%3Fview%3Dsql-server-ver15%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3ENative%20scoring%20using%20the%20PREDICT%20T-SQL%20function%20with%20SQL%20machine%20learning%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fazure-sql-edge%2Fonnx-overview%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EMachine%20learning%20and%20AI%20with%20ONNX%20in%20SQL%20Edge%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fazure-sql%2Fmanaged-instance%2Fmachine-learning-services-overview%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EMachine%20Learning%20Services%20in%20Azure%20SQL%20Managed%20Instance%20(preview)%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Fmachine-learning%2Fconcepts%2Fextensibility-framework%3Fview%3Dsql-server-ver15%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EExtensibility%20architecture%20in%20SQL%20Server%20Machine%20Learning%20Services%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Fmachine-learning%2Fconcepts%2Fextension-python%3Fview%3Dsql-server-ver15%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EPython%20language%20extension%20in%20SQL%20Server%20Machine%20Learning%20Services%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Fmachine-learning%2Fconcepts%2Fextension-r%3Fview%3Dsql-server-ver15%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3ER%20language%20extension%20in%20SQL%20Server%20Machine%20Learning%20Services%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Fsystem-stored-procedures%2Fsp-execute-external-script-transact-sql%3Fview%3Dsql-server-ver15%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Esp_execute_external_script%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22http%3A%2F%2Fcidrdb.org%2Fcidr2020%2Fpapers%2Fp24-karanasos-cidr20.pdf%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3EExtending%20Relational%20Query%20Processing%20with%20ML%20Inference%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Flanguage-extensions%2Flanguage-extensions-overview%3Fview%3Dsql-server-ver15%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3ESQL%20Server%20Language%20Extensions%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Ft-sql%2Fstatements%2Fcreate-external-language-transact-sql%3Fview%3Dsql-server-ver15%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3ECREATE%20EXTERNAL%20LANGUAGE%20(Transact-SQL)%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Flanguage-extensions%2Fconcepts%2Fextensibility-framework%3Fview%3Dsql-server-ver15%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EExtensibility%20architecture%20in%20SQL%20Server%20Language%20Extensions%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Flanguage-extensions%2Freference%2Fextensibility-framework-api%3Fview%3Dsql-server-ver15%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EExtensibility%20Framework%20API%20for%20SQL%20Server%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fmicrosoft%2Fsql-server-language-extensions%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3ESQL%20Server%20Language%20Extensions%20project%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fmicrosoft%2Fonnxruntime%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EONNX%20Runtime%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fmicrosoft%2Fonnxruntime%2Fblob%2Fmaster%2Fdocs%2FC_API.md%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EONNX%20Runtime%20C%20API%3C%2FA%3E%3C%2FLI%3E%0A%3C%2FOL%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2216226%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESQLServerMachineLearning%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Version history
Last update:
‎Apr 13 2021 03:24 PM
Updated by: