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.2.tar.gz (14.3 kB view details)

Uploaded Source

Built Distribution

tfopgen-0.2.2-py2.py3-none-any.whl (20.2 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

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

File hashes

Hashes for tfopgen-0.2.2.tar.gz
Algorithm Hash digest
SHA256 d127f05cf8b7748d334cc1159b41413184caaf6067c95536ce824cfd197a4eb7
MD5 1b035d2d56c52f7a490d8f4845f05011
BLAKE2b-256 c760320c3005cc61210f7e6314bfe53c76b3ac4dbca8a6f602e9fbc4a6d68c9f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tfopgen-0.2.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 ac9c789570b46ddc4b4dda90525275e96171f7c63b1b37f55caf1a054a3a4be3
MD5 af76f47c19698a9876ebc2dc2c50a4a8
BLAKE2b-256 de40c2001f87fdd7a7a7be42800bd1150e222106078677380bfb4e5290eb0a55

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