converter

Concepts

This module defines the interface of a converter.

A converter is responsible for converting tensorflow/pytorch types to or from generic types defined in utensor_cgen

GenericConverter

Any subclass of GenericConverter should overwrite following methods/attributes:

  1. GenericConverter.get_generic_value

    • should reture value of type as __utensor_generic_type__

  2. GenericConverter.__utensor_generic_type__

    • defines the return type of GenericConverter.get_generic_value

TFConverterMixin

Any subclass of TFConverterMixin should overwrite following methods/attributes:

  1. TFConverterMixin.get_tf_value

    • should return a value of type as __tfproto_type__

  2. TFConverterMixin.__tfproto_type__

A Qualified Converter in utensor_cgen

A qualified converter type should be subclass of both GenericConverter and TFConverterMixin

That is, a qualified converter should be able to convert a tensorflow protobuf type to a utensor_cgen generic data type.

Such convertion is defined by its __utensor_generic_type__ and __tfproto_type__ attributes.

Given a value of type __tfproto_type__, get_generic_value will convert it to an equivalent value of type __utensor_generic_type__. get_tf_value is hence an inverse function of get_generic_value

Module Members

class utensor_cgen.ir.converter.ConverterDispatcher

Converter Dispatcher

dispatch converter according the type of given value

classmethod all_generic_types(cls)

return a list of all generic types available in utensor_cgen

Return type

list

classmethod register(cls, converter_cls)

register decorator for a converter

Example

@ConverterDispatcher.register
class MyConverter(GenericConverterMixin, TFConverterMixin):
  __utensor_generic_type__ = <a generic type>
  __tfproto_type__ = <a tensorflow protobuf type>

  @classmethod
  def get_generic_value(cls, tf_value):
    # implement the convertion

  @classmethod
  def get_tf_value(cls, generic):
    # implement the convertion
class utensor_cgen.ir.converter.GenericConverterMixin

Abstract class for generic data type converter

a generic data type converter will convert a given value to a generic data type which is all internal data types defined in utensor_cgen

All subclass of GenericConverterMixin should

  1. overwrite __utensor_generic_type__

  • this is the data type the converter converting to

  1. overwrite classmethod get_generic_value

  • should be a classmethod which takes a value and reutrn a new value of type __utensor_generic_type__

classmethod get_generic_value(cls, value)

convert a given value to generic type

Return type

cls.__utensor_generic_type__ (polymorphism)

class utensor_cgen.ir.converter.TFConverterMixin

Abstract class for tensorflow protobuf data converter

a tensorflow protobuf data type converter will convert a given generic value in utensor_cgen to a tensorflow protobuf data type, defined with .proto files at tensorflow repo

classmethod get_tf_value(cls, value)

convert a given value to tensorflow protobuf type

Return type

cls.__tfproto_type__ (polymorphism)