-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d85abc0
Showing
21 changed files
with
983 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
name: CI Tests | ||
|
||
on: [push] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
max-parallel: 5 | ||
matrix: | ||
python-version: [3.6, 3.7, 3.8, 3.9] | ||
|
||
steps: | ||
- uses: actions/checkout@v1 | ||
- name: Set up Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install tox tox-gh-actions | ||
- name: Test with tox | ||
run: tox |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# general things to ignore | ||
build/ | ||
dist/ | ||
*.egg-info/ | ||
*.egg | ||
*.py[cod] | ||
__pycache__/ | ||
*.so | ||
*~ | ||
|
||
# due to using tox and pytest | ||
.tox | ||
.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) 2021 Ruby Nealon | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
include pyproject.toml | ||
include *.md | ||
include LICENSE | ||
recursive-include src *.py | ||
recursive-include example_outputs *.py | ||
recursive-include example_outputs *.vcl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# m2vcl | ||
|
||
![CI Tests status badge](https://github.com/rubyroobs/m2vcl/workflows/CI%20Tests/badge.svg) | ||
|
||
Experimental extension of [m2cgen](https://github.com/BayesWitnesses/m2cgen) to export statistical models to [Varnish Configuration Language](https://varnish-cache.org/docs/trunk/users-guide/vcl.html), for use in the Varnish cache. Right now only Fastly-flavored VCL is the only target supported, though this could theoretically partially target core Varnish in the future. | ||
|
||
## Examples | ||
|
||
For code examples and their generated VCL outputs, see the [example_outputs](https://github.com/rubyroobs/m2vcl/tree/master/example_outputs) directory. | ||
|
||
## Usage | ||
|
||
Use `export_to_fastly_vcl` to export to Fastly-flavored VCL. The `export_to_fasty_vcl` function takes arguemnts `indent` (defaults to 4, indent size in the generated VCL) and `sub_name` (defaults to `score`, the prefix for the generated subroutine and input/output header names). Inputs for the subroutine can be set on the headers `req.http.<prefix>_input_<index>` and outputs will be set on the header `req.http.<prefix>_output_<index>`. | ||
|
||
A working demo is available in [this Fastly fiddle](https://fiddle.fastlydemo.net/fiddle/754b1898), with the source provided below: | ||
|
||
### Generating Python code | ||
|
||
``` | ||
from sklearn.model_selection import train_test_split | ||
from sklearn.datasets import load_iris | ||
from sklearn.tree import DecisionTreeClassifier | ||
import m2vcl | ||
iris = load_iris() | ||
X = iris.data | ||
y = iris.target | ||
X_train, X_test, y_train, y_test = train_test_split( | ||
X, y, random_state=0) | ||
clf = DecisionTreeClassifier(max_leaf_nodes=3, random_state=0) | ||
clf.fit(X_train, y_train) | ||
print(m2vcl.export_to_vcl(clf)) | ||
``` | ||
|
||
### Output VCL | ||
|
||
``` | ||
sub score { | ||
declare local var.input_3 FLOAT; | ||
set var.input_3 = std.atof(req.http.score_input_3); | ||
declare local var.input_2 FLOAT; | ||
set var.input_2 = std.atof(req.http.score_input_2); | ||
declare local var.var0_0 FLOAT; | ||
declare local var.var0_1 FLOAT; | ||
declare local var.var0_2 FLOAT; | ||
if (var.input_3 <= 0.800000011920929) { | ||
set var.var0_0 = 1.0; | ||
set var.var0_1 = 0.0; | ||
set var.var0_2 = 0.0; | ||
} else { | ||
if (var.input_2 <= 4.950000047683716) { | ||
set var.var0_0 = 0.0; | ||
set var.var0_1 = 0.9166666666666666; | ||
set var.var0_2 = 0.08333333333333333; | ||
} else { | ||
set var.var0_0 = 0.0; | ||
set var.var0_1 = 0.02564102564102564; | ||
set var.var0_2 = 0.9743589743589743; | ||
} | ||
} | ||
set req.http.score_output_0 = var.var0_0; | ||
set req.http.score_output_1 = var.var0_1; | ||
set req.http.score_output_2 = var.var0_2; | ||
return; | ||
} | ||
``` | ||
|
||
### VCL Usage | ||
|
||
``` | ||
# VCL_DELIVER | ||
set req.http.score_input_2 = "1.23456789"; | ||
set req.http.score_input_3 = "9.87654321"; | ||
call score; | ||
set resp.http.Score-Result-0 = req.http.score_output_0; | ||
set resp.http.Score-Result-1 = req.http.score_output_1; | ||
set resp.http.Score-Result-2 = req.http.score_output_2; | ||
``` | ||
|
||
## Known limitations | ||
|
||
* Precision is limited due to limitations of Fastly, and will be lost for each subroutine the AST is broken down into due to the required float -> string -> float conversion. | ||
* Only tested with a small subset of models i.e. highly experimental - make sure to sanity check outputs | ||
|
||
## Todo | ||
|
||
* Improve test coverage by performing end to end testing on Fastly | ||
* Create tests for more models | ||
* Support core Varnish (may require a VMOD to provide equivalent functionality of [Fastly's math trig](https://developer.fastly.com/reference/vcl/functions/math-trig/)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
sub score { | ||
declare local var.input_3 FLOAT; | ||
set var.input_3 = std.atof(req.http.score_input_3); | ||
|
||
declare local var.input_2 FLOAT; | ||
set var.input_2 = std.atof(req.http.score_input_2); | ||
|
||
declare local var.var0_0 FLOAT; | ||
declare local var.var0_1 FLOAT; | ||
declare local var.var0_2 FLOAT; | ||
if (var.input_3 <= 0.800000011920929) { | ||
set var.var0_0 = 1.0; | ||
set var.var0_1 = 0.0; | ||
set var.var0_2 = 0.0; | ||
} else { | ||
if (var.input_2 <= 4.950000047683716) { | ||
set var.var0_0 = 0.0; | ||
set var.var0_1 = 0.9166666666666666; | ||
set var.var0_2 = 0.08333333333333333; | ||
} else { | ||
set var.var0_0 = 0.0; | ||
set var.var0_1 = 0.02564102564102564; | ||
set var.var0_2 = 0.9743589743589743; | ||
} | ||
} | ||
set req.http.score_output_0 = var.var0_0; | ||
set req.http.score_output_1 = var.var0_1; | ||
set req.http.score_output_2 = var.var0_2; | ||
return; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from sklearn.model_selection import train_test_split | ||
from sklearn.datasets import load_iris | ||
from sklearn.tree import DecisionTreeClassifier | ||
|
||
import m2vcl | ||
|
||
iris = load_iris() | ||
X = iris.data | ||
y = iris.target | ||
X_train, X_test, y_train, y_test = train_test_split( | ||
X, y, random_state=0) | ||
|
||
clf = DecisionTreeClassifier(max_leaf_nodes=3, random_state=0) | ||
clf.fit(X_train, y_train) | ||
print(m2vcl.export_to_vcl(clf)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
sub score { | ||
declare local var.input_0 FLOAT; | ||
set var.input_0 = std.atof(req.http.score_input_0); | ||
|
||
declare local var.input_1 FLOAT; | ||
set var.input_1 = std.atof(req.http.score_input_1); | ||
|
||
declare local var.input_2 FLOAT; | ||
set var.input_2 = std.atof(req.http.score_input_2); | ||
|
||
declare local var.input_3 FLOAT; | ||
set var.input_3 = std.atof(req.http.score_input_3); | ||
|
||
declare local var.input_4 FLOAT; | ||
set var.input_4 = std.atof(req.http.score_input_4); | ||
|
||
declare local var.input_5 FLOAT; | ||
set var.input_5 = std.atof(req.http.score_input_5); | ||
|
||
declare local var.input_6 FLOAT; | ||
set var.input_6 = std.atof(req.http.score_input_6); | ||
|
||
declare local var.input_7 FLOAT; | ||
set var.input_7 = std.atof(req.http.score_input_7); | ||
|
||
declare local var.input_8 FLOAT; | ||
set var.input_8 = std.atof(req.http.score_input_8); | ||
|
||
declare local var.input_9 FLOAT; | ||
set var.input_9 = std.atof(req.http.score_input_9); | ||
|
||
declare local var.input_10 FLOAT; | ||
set var.input_10 = std.atof(req.http.score_input_10); | ||
|
||
declare local var.input_11 FLOAT; | ||
set var.input_11 = std.atof(req.http.score_input_11); | ||
|
||
declare local var.input_12 FLOAT; | ||
set var.input_12 = std.atof(req.http.score_input_12); | ||
|
||
declare local var.var0_0 FLOAT; | ||
set var.var0_0 = var.input_0; | ||
set var.var0_0 *= -0.10801135783679545; | ||
declare local var.var1_0 FLOAT; | ||
set var.var1_0 = var.var0_0; | ||
declare local var.var2_0 FLOAT; | ||
set var.var2_0 = 36.459488385090125; | ||
set var.var2_0 += var.var1_0; | ||
declare local var.var3_0 FLOAT; | ||
set var.var3_0 = var.var2_0; | ||
declare local var.var4_0 FLOAT; | ||
set var.var4_0 = var.input_1; | ||
set var.var4_0 *= 0.04642045836688176; | ||
declare local var.var5_0 FLOAT; | ||
set var.var5_0 = var.var4_0; | ||
declare local var.var6_0 FLOAT; | ||
set var.var6_0 = var.var3_0; | ||
set var.var6_0 += var.var5_0; | ||
declare local var.var7_0 FLOAT; | ||
set var.var7_0 = var.var6_0; | ||
declare local var.var8_0 FLOAT; | ||
set var.var8_0 = var.input_2; | ||
set var.var8_0 *= 0.02055862636707862; | ||
declare local var.var9_0 FLOAT; | ||
set var.var9_0 = var.var8_0; | ||
declare local var.var10_0 FLOAT; | ||
set var.var10_0 = var.var7_0; | ||
set var.var10_0 += var.var9_0; | ||
declare local var.var11_0 FLOAT; | ||
set var.var11_0 = var.var10_0; | ||
declare local var.var12_0 FLOAT; | ||
set var.var12_0 = var.input_3; | ||
set var.var12_0 *= 2.6867338193448966; | ||
declare local var.var13_0 FLOAT; | ||
set var.var13_0 = var.var12_0; | ||
declare local var.var14_0 FLOAT; | ||
set var.var14_0 = var.var11_0; | ||
set var.var14_0 += var.var13_0; | ||
declare local var.var15_0 FLOAT; | ||
set var.var15_0 = var.var14_0; | ||
declare local var.var16_0 FLOAT; | ||
set var.var16_0 = var.input_4; | ||
set var.var16_0 *= -17.766611228300167; | ||
declare local var.var17_0 FLOAT; | ||
set var.var17_0 = var.var16_0; | ||
declare local var.var18_0 FLOAT; | ||
set var.var18_0 = var.var15_0; | ||
set var.var18_0 += var.var17_0; | ||
declare local var.var19_0 FLOAT; | ||
set var.var19_0 = var.var18_0; | ||
declare local var.var20_0 FLOAT; | ||
set var.var20_0 = var.input_5; | ||
set var.var20_0 *= 3.809865206809212; | ||
declare local var.var21_0 FLOAT; | ||
set var.var21_0 = var.var20_0; | ||
declare local var.var22_0 FLOAT; | ||
set var.var22_0 = var.var19_0; | ||
set var.var22_0 += var.var21_0; | ||
declare local var.var23_0 FLOAT; | ||
set var.var23_0 = var.var22_0; | ||
declare local var.var24_0 FLOAT; | ||
set var.var24_0 = var.input_6; | ||
set var.var24_0 *= 0.0006922246403425021; | ||
declare local var.var25_0 FLOAT; | ||
set var.var25_0 = var.var24_0; | ||
declare local var.var26_0 FLOAT; | ||
set var.var26_0 = var.var23_0; | ||
set var.var26_0 += var.var25_0; | ||
declare local var.var27_0 FLOAT; | ||
set var.var27_0 = var.var26_0; | ||
declare local var.var28_0 FLOAT; | ||
set var.var28_0 = var.input_7; | ||
set var.var28_0 *= -1.475566845600255; | ||
declare local var.var29_0 FLOAT; | ||
set var.var29_0 = var.var28_0; | ||
declare local var.var30_0 FLOAT; | ||
set var.var30_0 = var.var27_0; | ||
set var.var30_0 += var.var29_0; | ||
declare local var.var31_0 FLOAT; | ||
set var.var31_0 = var.var30_0; | ||
declare local var.var32_0 FLOAT; | ||
set var.var32_0 = var.input_8; | ||
set var.var32_0 *= 0.30604947898517226; | ||
declare local var.var33_0 FLOAT; | ||
set var.var33_0 = var.var32_0; | ||
declare local var.var34_0 FLOAT; | ||
set var.var34_0 = var.var31_0; | ||
set var.var34_0 += var.var33_0; | ||
declare local var.var35_0 FLOAT; | ||
set var.var35_0 = var.var34_0; | ||
declare local var.var36_0 FLOAT; | ||
set var.var36_0 = var.input_9; | ||
set var.var36_0 *= -0.01233459391657437; | ||
declare local var.var37_0 FLOAT; | ||
set var.var37_0 = var.var36_0; | ||
declare local var.var38_0 FLOAT; | ||
set var.var38_0 = var.var35_0; | ||
set var.var38_0 += var.var37_0; | ||
declare local var.var39_0 FLOAT; | ||
set var.var39_0 = var.var38_0; | ||
declare local var.var40_0 FLOAT; | ||
set var.var40_0 = var.input_10; | ||
set var.var40_0 *= -0.9527472317072923; | ||
declare local var.var41_0 FLOAT; | ||
set var.var41_0 = var.var40_0; | ||
declare local var.var42_0 FLOAT; | ||
set var.var42_0 = var.var39_0; | ||
set var.var42_0 += var.var41_0; | ||
declare local var.var43_0 FLOAT; | ||
set var.var43_0 = var.var42_0; | ||
declare local var.var44_0 FLOAT; | ||
set var.var44_0 = var.input_11; | ||
set var.var44_0 *= 0.009311683273793711; | ||
declare local var.var45_0 FLOAT; | ||
set var.var45_0 = var.var44_0; | ||
declare local var.var46_0 FLOAT; | ||
set var.var46_0 = var.var43_0; | ||
set var.var46_0 += var.var45_0; | ||
declare local var.var47_0 FLOAT; | ||
set var.var47_0 = var.var46_0; | ||
declare local var.var48_0 FLOAT; | ||
set var.var48_0 = var.input_12; | ||
set var.var48_0 *= -0.5247583778554923; | ||
declare local var.var49_0 FLOAT; | ||
set var.var49_0 = var.var48_0; | ||
declare local var.var50_0 FLOAT; | ||
set var.var50_0 = var.var47_0; | ||
set var.var50_0 += var.var49_0; | ||
set req.http.score_output_0 = var.var50_0; | ||
return; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from sklearn.datasets import load_boston | ||
from sklearn.linear_model import LinearRegression | ||
|
||
import m2vcl | ||
|
||
boston = load_boston() | ||
X, y = boston.data, boston.target | ||
estimator = LinearRegression() | ||
estimator.fit(X, y) | ||
print(m2vcl.export_to_vcl(estimator)) |
Oops, something went wrong.