Skip to main content

Generates tensorflow custom operator boilerplate

Project description

https://travis-ci.org/sjperkins/tfopgen.svg?branch=master

Writing a tensorflow operator requires writing fair amounts of boilerplate C++ and CUDA code. This script generates code for the CPU and GPU version of a tensorflow operator. More specifically, given tensorflow inputs, outputs and attributes, it generates:

  • C++ Header file that defines the operator class, templated on Device.

  • C++ Header file that defines the CPU implementation of the operator.

  • C++ Source file with Shape Function, REGISTER_OP and REGISTER_KERNEL_BUILDER constructs.

  • Cuda Header that defines the GPU implementation of the operator, including a CUDA kernel.

  • Cuda Source file with GPU REGISTER_KERNEL_BUILDER’s for the operator.

  • python unit test case, which constructs random input data, and calls the operator.

  • Makefile for compiling the operator into a shared library, using g++ and nvcc.

Requirements

A tensorflow installation, required for building the operator.

pip install tensorflow

Installation

pip install tfopgen

Usage

The user should provide a YAML configuration file defining the operator:

  • inputs and optionally, their shapes.

  • outputs and optionally, their outputs.

  • polymorphic type attributes.

  • other attributes.

  • documentation.

For example, we can define the outline for a ComplexPhase operator in the complex_phase.yml file.

---
project: astronomy
library: fourier
name: ComplexPhase
type_attrs:
  - "FT: {float, double} = DT_FLOAT"
  - "CT: {complex64, complex128} = DT_COMPLEX64"
inputs:
  - ["uvw: FT", [null, null, 3]]   # (ntime, nbl, 3)
  - ["frequency: FT", [null]]      # (nchan, )
  - ["lm: FT", [null, 2]]          # (nsrc, 2)
outputs:
  - ["complex_phase: CT", [null, null, null, null]]
doc: >
  Given tensors
    (1) of (U, V, W) baseline coordinates with shape (ntime, nbl, 3)
    (2) of (L, M) sky coordinates with shape (nsrc, 2)
    (3) of frequencies,
  compute the complex phase with shape (nsrc, ntime, nbl, nchan)

We can then run:

$ tfopgen complex_phase.yml

to create the following directory structure and files:

$ tree fourier/
fourier/
├── complex_phase_op_cpu.cpp
├── complex_phase_op_cpu.h
├── complex_phase_op_gpu.cu
├── complex_phase_op_gpu.cuh
├── complex_phase_op.h
├── Makefile
└── test_complex_phase.py

The project and library options specify C++ namespaces within which the operator is created. Additionally, the Makefile will create a fourier.so shared library that can be loaded with tf.load_op_library('fourier.so').

Any polymorphic type attributes should be supplied. The generator will template the operators on type attributes. It will also generate concrete permutations of REGISTER_KERNEL_BUILDER for both the CPU and GPU op using the actual types supplied in the type attributes (float, double, complex64 and complex128) below:

type_attrs:
  - "FT: {float, double} = DT_FLOAT"
  - "CT: {complex64, complex128} = DT_COMPLEX64"

The operator inputs and their optional shapes should be specified as a list containing a string defining the .Input directive, and a list describing the shape of the input tensor. A null value in the shape will be translated into a python None. If concrete dimensions are specified, corresponding checks will be generated in the Shape Function associated with the operator.

inputs:
  - ["uvw: FT", [null, null, 3]]   # (ntime, nbl, 3)
  - ["frequency: FT", [null]]      # (nchan, )
  - ["lm: FT", [null, 2]]          # (nsrc, 2)

The operator outputs should similarly defined.

outputs:
  - ["complex_phase: CT", [null, null, null, null]]

Given these inputs and outputs, CPU and GPU operators are created with named variables corresponding to the inputs and outputs. Additionally, a CUDA kernel with the given inputs and outputs is created, as well as a shape function checking the rank and dimensions of the supplied inputs.

Other attributes may be specified (and will be output in the REGISTER_OP) directive, but are not catered for automatically by the generator code as the range of attribute behaviour is complex.

op_other_attrs:
    - "iterations: int32 >= 2",

Finally operator documentation may also be supplied.

doc: >
  Given tensors
    (1) of (U, V, W) baseline coordinates with shape (ntime, nbl, 3)
    (2) of (L, M) sky coordinates with shape (nsrc, 2)
    (3) of frequencies,
  compute the complex phase with shape (nsrc, ntime, nbl, nchan)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tfopgen-0.2.0.tar.gz (14.3 kB view details)

Uploaded Source

Built Distribution

tfopgen-0.2.0-py2.py3-none-any.whl (19.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file tfopgen-0.2.0.tar.gz.

File metadata

  • Download URL: tfopgen-0.2.0.tar.gz
  • Upload date:
  • Size: 14.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for tfopgen-0.2.0.tar.gz
Algorithm Hash digest
SHA256 0e094b96b681933f9b372e57925c3a1d386bd35d80513984521ff98c02d5e6a9
MD5 6378fab3222190eb0dc0319947e71d5d
BLAKE2b-256 c41fafa39ffeef9e8a3884832f78fe138979ee53a2c4e4b3246457a9586d111c

See more details on using hashes here.

File details

Details for the file tfopgen-0.2.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for tfopgen-0.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 43dcaf5d1401bc8030206f825bcc173748e83f448eaa54b05332403f5cebe3c8
MD5 602b4191c270e766807891b303c9759e
BLAKE2b-256 c2e8f628895bbe26a53fc279dfd90af11629a28020c6382997257c7321946719

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page