Giter VIP home page Giter VIP logo

zein's Introduction

ZEIN

Zig-based implementation of general-rank tensors! [1, 64)

Using Tensor Objects

Tensors can be created in the following way:

// initialize underlying tensor memory:
var data = [9]i32{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };

// create a rank 2, 3x3, Rowwise tensor of i32 from data:
var X = zein.Tensor(i32, 2, Rowwise).init(&data, .{ 3, 3 });    

const x = X.getValue(.{0, 2}); // access value 3...

Allocating Tensor Data

The TensorFactory offers the ability to track and free allocations:

// null will automatically use the GPA allocator:
var factory = zein.TensorFactory(f32).init(null);

// Begin tracking tensor allocations (default is no-tracking):
factory.tracking(.start);

// Stop tracking tensor allocations (does not free tensors):
factory.tracking(.stop);

// Free tracked tensor allocations (no-op if no tensors are tracked):
factory.tracking(.free);

// Deinit will free the allocator and currently tracked tensors:
factory.deinit();
// Assign a new tensor from allocator:
var Y = try factory.allocTensor(2, Rowwise, .{ 10, 10 });
// Assign memory into existing tensor:
var X = Tensor(f32, 2, Rowwise).init(null, .{ 10, 10 });

try factory.allocToTensor(&X); // alloc 100 elements...

Tensor Operations

Tensor operations are are in the form of either Free Functions or Factory Functions:

    - Free Functions require operands and the destination tensor.

    - Factory Functions use operands to create the destination tensor.

The operations use compile time strings as einsum notation:

// Collapse tensor values using contraction:

try zein.contraction("ijk->ji", &x, &y); // free function - assign to existing memory

var y = factory.contraction("ijk->ji", &x); // factory function - allocate new memory
// Elementary binary functions (add, multiply):

try zein.add(&x, &y, &z); // free function - assign to existing memory

var x = factory.add(&x, &y); // factory function - allocate new memory
// Transpose/permutate tensor views (does not modify underlying data).

var y = try x.permutate("ijk->kji");
// Elementary vectorized reduction functions (sum, product, min, max):
const a = try zein.sum(&x);
const b = try zein.product(&x);
const c = try zein.max(&x);
const d = try zein.min(&x);

Using the Zein library

The main ZEIN/Zein.zig file provides an interface for the library implementation.

The implementations are in the Core folder. They will be labled as "VX" where X is the verion number.

Most functions in the Zein library will have default and "unchecked" versions.

Default functions ensure operational correctness while unchecked versions do not.

Unchecked versions are provided for api-level optimizations where performance matters.

Memory Ownership and Viewership

Currently, tensor permutations only change the indexing of a tensor - they do not invalidate underlying memory. If the user chooses to use the TensorFactory, it will track allocations and delete them automatically when calling deinit. V1 is only tested on single thread environments - thread safety with allocators will be coming in a later version!

Additonal functionality coming soon.

This library is still in the beginning phases. If you want to contribute, please contact me! This is a big job and I'll take the help!

zein's People

Contributors

andrewcodedev avatar doomwhite avatar

Stargazers

 avatar

Watchers

 avatar

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.