Giter Site home page Giter Site logo

torchffi's Introduction

TorchFFI

This package exposes Torch's Tensor and Storage data structures, through LuaJIT's FFI. This allows extremely fast access to Tensors and Storages, all from Lua.

The original neural network from Torch7, 'nn', contains stable and widely used modules. 'nnx' contains more experimental, unproven modules, and optimizations. Eventually, modules that become stable enough will make their way into 'nn' (some already have).

Disclaimer: DONT USE THIS PACKAGE WITHOUT FIRST CHECKING ITS MODULES !!!

Installation

  • Install Torch7 (refer to its own documentation). It is necessary to build it with LuaJIT, to enable FFI:
$ cmake . -D WITH_LUA_JIT=1
$ make install
  • Install torchffi:
$ torch-pkg -local install torchffi

Usage

Simply require torchffi, and you'll have access to a new function in torch, called torch.data(obj):

> t = torch.randn(3,2)
> print(t)
 0.8008 -0.6103
 0.6473 -0.1870
-0.0023 -0.4902
[torch.DoubleTensor of dimension 3x2]

> t_data = torch.data(t)
> for i = 0,t:nElement()-1 do t_data[i] = 0 end
> print(t)
0 0
0 0
0 0
[torch.DoubleTensor of dimension 3x2]

Accessing the raw data of a Tensor like this is extremely efficient, in fact, it's almost as fast as C in lots of cases.

WARNING: bear in mind that accessing the raw data like this is dangerous, and should only be done on contiguous tensors (if a tensor is not contiguous, then you have to use it size and stride information). Making sure a tensor is contiguous is easy:

> t = torch.randn(3,2)
> t_noncontiguous = t:transpose(1,2)

-- it would be unsafe to work with torch.data(t_noncontiguous)

> t_transposed_and_contiguous = t:noncontiguous:contiguous()

-- it is now safe to work with the raw pointer

> data = torch.data(t_contiguous)

torchffi's People

Contributors

clementfarabet avatar

Watchers

Ivo Danihelka avatar James Cloos 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.