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

Uploaded Source

Built Distribution

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

Uploaded Python 2 Python 3

File details

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

File metadata

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

File hashes

Hashes for tfopgen-0.2.3.tar.gz
Algorithm Hash digest
SHA256 3b2db5ef19ea75f72ffb851d50b63c2976fa76ad7f2157a87c3f02fd5bff81bc
MD5 61bbcb3210ec89ee5aa5632bb05fb611
BLAKE2b-256 7ce567aaa4b87a6e4a6f1af5aa0108a824c1325571ebb1ac17a31aa5b1e1a0b3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tfopgen-0.2.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 121bbae6d324909319cce3225139d7f1782385773e70db7c05952f0cf1742242
MD5 9e989503f9bff00249ce8e9b7dd89c5a
BLAKE2b-256 800cbd9acec0f2bef614289cefdb1eb7990bf960bba5cfcce4296dc3819f355e

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