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

Uploaded Source

Built Distribution

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

Uploaded Python 2 Python 3

File details

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

File metadata

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

File hashes

Hashes for tfopgen-0.2.1.tar.gz
Algorithm Hash digest
SHA256 f43a51f63cd6aa0b47572c82fa4c4ebb3f5d509b94bde0694b6235ae1eedcb66
MD5 76d000c7bc971f6e8adc860e92cb4d77
BLAKE2b-256 56ad897152263034aed1d25875d6d6dfb3490f07cb2346fa707298a46135fd61

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tfopgen-0.2.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b233bdbb133f32462ca98e604a2a154361015051a6631143a01b2034fd69b7aa
MD5 b3806e69e49b0af3304f716533262620
BLAKE2b-256 9c3263dd0543bf3fbce8d184591410a1f41ac2e73895e69250bf911b1fa501e0

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