GithubHelp home page GithubHelp logo

adam-vandervorst / pybhv Goto Github PK

View Code? Open in Web Editor NEW
17.0 4.0 5.0 508 KB

Boolean Hypervectors with various operators for experiments in hyperdimensional computing (HDC).

License: GNU General Public License v3.0

Python 90.42% C++ 9.58%
computational hyperdimensional-computing machine-learning majority permutation reasoning vector-symbolic-architecture winnow hypervector binary-spatter-code

pybhv's Introduction

Python Boolean Hyper-Vectors

A rich research framework for hyperdimensional computing on large boolean vectors supporting program transformation and multiple backends for computation (plain Python, C++, NumPy, PyTorch). Many metrics and utility functions aim to aid the intuitive understanding of this new paradigm, and there are multiple levels of functionality available from the data marshalling and the basic (XOR, MAJ, PERMUTE)-algebra to cryptography support. All vector operations are implemented in C(++) and make use of bit-packing and SIMD, subprograms can be optimized and compiled to these operations in Python or C, and parallelization and pipelining are planned.

If your application is a relatively direct pipeline, take a look at HDCC. If you want a more stable library, or want to work with another base field than the booleans, use torch-hd. For C/C++, please see CBHV.

Overview

The fundamental research includes finding algebras with interesting properties on top of large boolean vectors. To this extent the library has laws used for testing and an expansive set of operators including:

  • Multiple types of fast random vector generation
  • Random and indexed select between vectors
  • Ability to slightly modify a vector, for example by flipping a fraction of its bits
  • Permutation, roll, and swapping with multiple interfaces
  • Hashing and encoding
  • Majority with multiple implementation
  • Sample, a cheap alternative to Majority
  • AND, OR, XOR, and NOT operators
  • Composite operations like SELECT (or MUX) and FLIP-FRAC (flipping a fraction of the bits)
  • Hamming, jaccard, cosine, bit-error-rate, tversky, and mutual-information metrics
  • A system for relatedness, unrelatedness, and standard deviations apart
  • zscore and pvalue

Additionally, provided are

Installation

Make sure you have a recent Python version, 3.10 is recommended.

pip install bhv

If you want a specific definition of "hyper" (the default is 8192) you can specify that as follows:

DIMENSION=512 pip install bhv Note, use multiples of 512 and preferably powers of 2.

If you only want to work with plain Python, you're good to go with from bhv.vanilla VanillaBHV as BHV.

For the native option, you need a modern C++ compiler and use from bhv.native import NativePackedBHV as BHV. The setup process should attempt to install this by default.

For interop with (the Python interface of) NumPy and PyTorch, you'll need pip install numpy or pip install torch with respectively from bhv.np import NumPyPacked64BHV as BHV or from bhv.np import TorchBoolBHV as BHV.

Getting started

Some resources to get started with the library, if you're looking for a broader intro, please take a look at hd-computing.com.

New to Hyperdimensional Computing

Basic uses (in the context of neo-GOFAI) are given in my presentation with a installation-free notebook.

The fundamental angle is to start is with Kanerva's initial paper together with the library. For that, multiple resources are provided:

As for a Machine Learning angle, you may enjoy:

Evaluating the library

If you like to dive into the code directly, I suggest the following entrypoints:

Example exploratory usages of the library:

Note

This repository is (highly) active development, and a work-in-progress. Do expect changes to the naming, and even features to be swapped for more elegant alternatives.

The codebase also works with PyPy. Use the vanilla Python implementation. The numeric operations are slower than on CPython, but the symbolic ones are way faster.

If you have any feedback, raise an informal issue, or email me at [email protected]

If the library is not as fast as possible, that's a bug, please report.

pybhv's People

Contributors

adam-vandervorst avatar annelined avatar luketpeterson avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

pybhv's Issues

Optimization Opportunity: Single-Pass window() Function

The window() function performs 2 threshold operations (high and low). But the expensive part is performing the counting, and that only needs to be done once.

The threshold counting functions can be re-used by the window() function for a single-pass implementation.

Feature request: Synthesize the exact ternary formulas

All the best XOR/AND/OR formulas for three-argument boolean functions are known; let's use these in synthesis.
Specifically for the cellular automata example, this now shows a rather ugly expression with select (or ITE), which can be elided.

Optimization Opportunity: Logic Ops Threshold

The Threshold function isn't very well optimized (anti-optimized in fact. Slower than naive.) for very low threshold values.

Any threshold value under ~10 could likely be done more efficiently with a template-generated decision tree. See logic_majority() for an example.

Optimization Opportunity: PSADBW and VPDPBUSD

A good amount of silicon goes to video processing compression and more recently to neural network inference: this gives us very fast compound instructions.
An example of the former is https://www.felixcloutier.com/x86/psadbw which computes the L1 norm between two byte-vectors.
The latter, part of VNNI, is https://www.felixcloutier.com/x86/vpdpbusd which is a convolution of a byte vector with an extra addition.

They're similar in that they compute similarity, and I believe this can be exploited in the window threshold-generalization and metrics like jaccard, cosine, and mutual_information.

SymbolicBHV.synth or simplify sometimes gives the wrong Boolean form

For some ECA rules

formula = SymbolicBHV.synth([Var("left"), Var("center"), Var("right")], mask)
formula = formula.simplify()

Results in the wrong formula

Rule 90 works, but many other rules do not work like 30, 110 or 204 as examples.

Trivial example Rule 204: (~center), but should be just (center)
examples
Boolean form

less trivial example Rule 30: (left ^ (center & right)) but (i think) should be (left ^ ( center | right))
examples
Boolean form

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.