import argparse

import torch
from torch import nn
import torch.nn.functional as F


class Model(nn.Module):
    def __init__(self):
        super().__init__()
        self._is_generation_fast = False

    @staticmethod
    def add_options(parser):
        """Add model-specific arguments to the parser."""
        pass

    @staticmethod
    def map_options(args):
        """Map command-line options to constructor parameters"""
        return argparse.Namespace()

    @classmethod
    def build_model(cls, args):
        """Build a new model instance."""
        return cls(**cls.map_options(args).__dict__)

    def get_normalized_probs(self, net_output, *args, log_probs=False, **kwargs):
        """Get normalized probabilities (or log probs) from a net's output."""
        if torch.is_tensor(net_output):
            logits = net_output.float()
            if log_probs:
                return F.log_softmax(logits, dim=-1)
            else:
                return F.softmax(logits, dim=-1)
        raise NotImplementedError

    def make_generation_fast_(self, **kwargs):
        """Optimize model for faster generation."""
        if self._is_generation_fast:
            return  # only apply once
        self._is_generation_fast = True

        # remove weight norm from all modules in the network
        def apply_remove_weight_norm(module):
            try:
                nn.utils.remove_weight_norm(module)
            except ValueError:  # this module didn't have weight norm
                return

        self.apply(apply_remove_weight_norm)

        seen = set()

        def apply_make_generation_fast_(module):
            if module != self and hasattr(module, 'make_generation_fast_') \
                    and module not in seen:
                seen.add(module)
                module.make_generation_fast_(**kwargs)

        self.apply(apply_make_generation_fast_)

        def train(mode=True):
            if mode:
                raise RuntimeError('cannot train after make_generation_fast')

        # this model should no longer be used for training
        self.eval()
        self.train = train

    @staticmethod
    def upgrade_args(args):
        pass

import argparse

import torch
from torch import nn
import torch.nn.functional as F


class Model(nn.Module):
    def __init__(self):
        super().__init__()
        self._is_generation_fast = False

    @staticmethod
    def add_options(parser):
        """Add model-specific arguments to the parser."""
        pass

    @staticmethod
    def map_options(args):
        """Map command-line options to constructor parameters"""
        return argparse.Namespace()

    @classmethod
    def build_model(cls, args):
        """Build a new model instance."""
        return cls(**cls.map_options(args).__dict__)

    def get_normalized_probs(self, net_output, *args, log_probs=False, **kwargs):
        """Get normalized probabilities (or log probs) from a net's output."""
        if torch.is_tensor(net_output):
            logits = net_output.float()
            if log_probs:
                return F.log_softmax(logits, dim=-1)
            else:
                return F.softmax(logits, dim=-1)
        raise NotImplementedError

    def make_generation_fast_(self, **kwargs):
        """Optimize model for faster generation."""
        if self._is_generation_fast:
            return  # only apply once
        self._is_generation_fast = True

        # remove weight norm from all modules in the network
        def apply_remove_weight_norm(module):
            try:
                nn.utils.remove_weight_norm(module)
            except ValueError:  # this module didn't have weight norm
                return

        self.apply(apply_remove_weight_norm)

        seen = set()

        def apply_make_generation_fast_(module):
            if module != self and hasattr(module, 'make_generation_fast_') \
                    and module not in seen:
                seen.add(module)
                module.make_generation_fast_(**kwargs)

        self.apply(apply_make_generation_fast_)

        def train(mode=True):
            if mode:
                raise RuntimeError('cannot train after make_generation_fast')

        # this model should no longer be used for training
        self.eval()
        self.train = train

    @staticmethod
    def upgrade_args(args):
        pass

Add model-specific arguments to the parser. Map command-line options to constructor parameters Build a new model instance. Get normalized probabilities (or log probs) from a net's output. Optimize model for faster generation. # only apply once # remove weight norm from all modules in the network # this module didn't have weight norm # this model should no longer be used for training