SolidML Data Preprocessing
Overview
This guide describes how to use OpenGradient's data preprocessing capabilities from smart contracts. Data preprocessing can be useful for transforming or aggregating input data for a specific model in the expected format. For example, if your smart contract contains the price history of a token, but your model expects the standard deviation as input, you can use the data preprocessing precompile to calculate it.
The data preprocessing precompile offers efficient transformations on top of your data, and can be used to push compute-intensive operations on-chain, making them verifiable and secure.
Precompile Interface
OpenGradient provides a precompile for data preprocessing. This precompile is used to perform various statistical and analytical operations on data.
TIP
The precompile is accessible at address 0x00000000000000000000000000000000000000f6
.
The precompile exposes the following functions:
interface OGPreprocessing {
function min(TensorLib.Number[] calldata data) external returns (TensorLib.Number memory);
function max(TensorLib.Number[] calldata data) external returns (TensorLib.Number memory);
function mean(TensorLib.Number[] calldata data) external returns (TensorLib.Number memory);
function stdDev(TensorLib.Number[] calldata data) external returns (TensorLib.Number memory);
function variance(TensorLib.Number[] calldata data) external returns (TensorLib.Number memory);
function median(TensorLib.Number[] calldata data) external returns (TensorLib.Number memory);
function standardize(TensorLib.Number[] calldata data) external returns (TensorLib.Number[] memory);
function normalize(TensorLib.Number[] calldata data) external returns (TensorLib.Number[] memory);
function correlation(
TensorLib.Number[] calldata dataX,
TensorLib.Number[] calldata dataY
) external returns (TensorLib.Number memory);
}
Usage
Example Data Preprocessing:
contract DataPreprocessing {
address public precompileAddress = 0x00000000000000000000000000000000000000f6; // Address of the precompile
OGPreprocessing preprocessor = OGPreprocessing(precompileAddress);
function calculateMean(int256[] memory values) public returns (int256) {
TensorLib.Number[] memory data = new TensorLib.Number[](3);
data[0] = TensorLib.Number({value: 10, decimals: 1}); // Represents 1.0
data[1] = TensorLib.Number({value: 20, decimals: 1}); // Represents 2.0
data[2] = TensorLib.Number({value: 30, decimals: 1}); // Represents 3.0
// Call the mean function from the precompile
TensorLib.Number memory meanResult = preprocessor.mean(data);
// Return the calculated mean
return meanResult.value;
}
}