8000 GitHub - coderonion/ZEIN: Zig-based implementation of tensors
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

coderonion/ZEIN

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

91 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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!

About

Zig-based implementation of tensors

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Zig 100.0%
0