Skip to content

Deprecated tensorflow.contrib.training.HParams Dependency #380

@Meeex2

Description

@Meeex2

Update Deprecated tensorflow.contrib.training.HParams Dependency

Problem Description

The current implementation uses the deprecated tensorflow.contrib.training.HParams which:

  1. Was removed in TensorFlow 2.x (as tensorflow.contrib was fully deprecated)
  2. Causes import errors for users with modern TensorFlow installations
  3. Forces users to downgrade to TF 1.x or install compatibility packages

Affected Code

In model.py:

from tensorflow.contrib.training import HParams  # Line causing the error

def default_hparams():
    return HParams(
        n_vocab=0,
        n_ctx=1024,
        n_embd=768,
        n_head=12,
        n_layer=12,
    )

Suggested Solutions

Option 1: Native Python Implementation

class HParams:
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)
    
    def override_from_dict(self, params_dict):
        self.__dict__.update(params_dict)

def default_hparams():
    return HParams(
        n_vocab=0,
        n_ctx=1024,
        n_embd=768,
        n_head=12,
        n_layer=12,
    )

Option 2: Use Dataclasses (Python 3.7+)

from dataclasses import dataclass

@dataclass
class HParams:
    n_vocab: int = 0
    n_ctx: int = 1024
    n_embd: int = 768
    n_head: int = 12
    n_layer: int = 12
    
    def override_from_dict(self, params_dict):
        self.__dict__.update(params_dict)

def default_hparams():
    return HParams()

Option 3: Compatibility Backport

try:
    from tensorflow.contrib.training import HParams  # TF 1.x
except ImportError:
    from hparams import HParams  # Requires: pip install tensorflow-hparams

def default_hparams():
    return HParams(
        n_vocab=0,
        n_ctx=1024,
        n_embd=768,
        n_head=12,
        n_layer=12,
    )

Why This Change Matters

  1. Future Compatibility: TF 2.x is now standard and contrib was officially removed
  2. Easier Installation: Removes need for workarounds/downgrades
  3. Maintenance: Follows modern Python practices
  4. Performance: Native Python implementation has no TensorFlow dependency overhead

Workarounds (For Users)

Until this is merged, users can:

# Temporary solution 1: Downgrade TF
pip install tensorflow==1.15

# Temporary solution 2: Use backport
pip install tensorflow-hparams

References

Additional Notes

The change is backward-compatible as:

  1. The interface remains identical (__dict__-based)
  2. All existing hparams.json loading continues working
  3. No changes needed in other files that use HParams

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions