Instructions to use Zipeng365/WISP with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Scikit-learn
How to use Zipeng365/WISP with Scikit-learn:
from huggingface_hub import hf_hub_download import joblib model = joblib.load( hf_hub_download("Zipeng365/WISP", "sklearn_model.joblib") ) # only load pickle files from sources you trust # read more about it here https://skops.readthedocs.io/en/stable/persistence.html - Notebooks
- Google Colab
- Kaggle
Download src/wisp_fast/forest.py from Zipeng365/WISP: direct link, hf CLI and curl.
- Browser
- Download file 2.59 kB
-
https://huggingface.co/Zipeng365/WISP/resolve/main/src/wisp_fast/forest.py
- Command line
-
hf download hf://Zipeng365/WISP/src/wisp_fast/forest.py
-
curl -L -o forest.py https://huggingface.co/Zipeng365/WISP/resolve/main/src/wisp_fast/forest.py
2.59 kB
| """Optional zero-copy C traversal over sklearn's fitted single-output forests.""" | |
| import ctypes | |
| from functools import lru_cache | |
| from .native import lookup | |
| import numpy as np | |
| def _native(): | |
| fn = lookup("wisp_forest") | |
| if fn is None: | |
| return None | |
| arr = np.ctypeslib.ndpointer | |
| size = ctypes.c_ssize_t | |
| fn.argtypes = [arr(dtype=np.float32, ndim=2, flags="C_CONTIGUOUS"), size, size, | |
| arr(dtype=np.uintp, ndim=2, flags="C_CONTIGUOUS"), size, size, | |
| arr(dtype=np.float64, ndim=2, flags="C_CONTIGUOUS")] | |
| fn.restype = None | |
| return fn | |
| class ForestPlan: | |
| """Transient table of eight machine words per tree. The tree objects own the buffers.""" | |
| def __init__(self, head): | |
| self.trees = [e.tree_ for e in head.estimators_] | |
| self.table = None | |
| self._views = [] | |
| if _native() is None or head.n_outputs_ != 1 or len(self.trees) > 32768: | |
| return | |
| width = len(head.classes_) | |
| rows = [] | |
| for t in self.trees: | |
| fields = [t.children_left, t.children_right, t.feature, t.threshold, | |
| t.value, getattr(t, "missing_go_to_left", None)] | |
| left, right, feat, thresh, value, missing = fields | |
| if (missing is None or missing.dtype != np.uint8 or thresh.dtype != np.float64 | |
| or any(a.dtype != np.intp for a in [left, right, feat]) | |
| or value.dtype != np.float64 or value.shape != (t.node_count, 1, width) | |
| or value.strides[0] < width * 8 or value.strides[-1] != 8 | |
| or not value.flags.aligned or left.strides[0] <= 0 | |
| or any(a.shape != (t.node_count,) or a.strides != left.strides | |
| for a in (right, feat, thresh, missing))): | |
| return | |
| self._views.append(fields) # keep every buffer alive for the plan's lifetime | |
| rows.append([a.ctypes.data for a in fields] + [left.strides[0], value.strides[0]]) | |
| self.table = np.asarray(rows, dtype=np.uintp) | |
| def local_proba(head, features, plan): | |
| # sklearn performs its normal feature-count/dtype and finite input checks. | |
| X = head._validate_X_predict(features) | |
| k = len(head.classes_) | |
| out = np.zeros((len(X), k), dtype=np.float64) | |
| if plan.table is not None: | |
| _native()(np.ascontiguousarray(X), len(X), X.shape[1], plan.table, len(plan.trees), k, out) | |
| else: | |
| for tree in plan.trees: | |
| out += tree.predict(X)[:, :k] | |
| out /= len(plan.trees) | |
| return out | |