code: def models_trained_for_one_step(
      request,
      train_val_implicit_sample_data,
      movielens_metadata_df,
      user_metadata_df,
      movielens_implicit_df,
      train_val_implicit_pandas_data,
      gpu_count,
  ):
      train, val = train_val_implicit_sample_data
  
      genres = (
          torch.tensor(
              movielens_metadata_df[
                  [
                      c
                      for c in movielens_metadata_df.columns
                      if "genre" in c
                  ]
              ].values
          )
          .topk(1)
          .indices.view(-1)
      )
  
      number_of_stages = None
  
      if request.param == "mf_hdf5":
          # create, fit, and return the model all at once so we can close the HDF5 file
          (
              train_pandas_df,
              val_pandas_df,
          ) = train_val_implicit_pandas_data
  
          with tempfile.TemporaryDirectory() as temp_dir:
              pandas_df_to_hdf5(
                  df=train_pandas_df,
                  out_path=os.path.join(temp_dir, "train.h5"),
                  key="interactions",
              )
              pandas_df_to_hdf5(
                  df=val_pandas_df,
                  out_path=os.path.join(temp_dir, "val.h5"),
                  key="interactions",
              )
  
              train_loader = HDF5InteractionsDataLoader(
                  hdf5_path=os.path.join(temp_dir, "train.h5"),
                  user_col="user_id",
                  item_col="item_id",
                  num_users=train.num_users,
                  num_items=train.num_items,
                  batch_size=1024,
                  shuffle=True,
              )
              val_loader = HDF5InteractionsDataLoader(
                  hdf5_path=os.path.join(temp_dir, "val.h5"),
                  user_col="user_id",
                  item_col="item_id",
                  num_users=val.num_users,
                  num_items=val.num_items,
                  batch_size=1024,
                  shuffle=False,
              )
  
              model = MatrixFactorizationModel(
                  train=train_loader,
                  val=val_loader,
                  embedding_dim=15,
                  dropout_p=0.1,
                  lr=1e-1,
                  bias_lr=1e-2,
                  optimizer="adam",
                  bias_optimizer="sgd",
                  weight_decay=1e-7,
                  loss="bpr",
                  sparse=False,
              )
  
              model_trainer = CollieTrainer(
                  model=model,
                  gpus=gpu_count,
                  max_epochs=1,
                  deterministic=True,
                  logger=False,
                  enable_checkpointing=False,
              )
  
              model_trainer.fit(model)
              model.eval()
  
              return model
  
      elif request.param == "sparse_mf":
          model = MatrixFactorizationModel(
              train=train,
              val=val,
              embedding_dim=15,
              dropout_p=0.1,
              lr=1e-1,
              bias_lr=1e-2,
              optimizer="sparse_adam",
              bias_optimizer="sgd",
              weight_decay=0,
              loss="hinge",
              sparse=True,
          )
      elif request.param == "mf_no_val":
          model = MatrixFactorizationModel(train=train, val=None)
      elif (
          request.param == "mf_non_approximate"
          or request.param == "mf_approximate"
      ):
          if request.param == "mf_non_approximate":
              train_loader = InteractionsDataLoader(
                  interactions=train, batch_size=1024, shuffle=True
              )
              val_loader = InteractionsDataLoader(
                  interactions=val, batch_size=1024, shuffle=False
              )
          else:
              train_loader = (
                  ApproximateNegativeSamplingInteractionsDataLoader(
                      interactions=train, batch_size=1024, shuffle=True
                  )
              )
              val_loader = (
                  ApproximateNegativeSamplingInteractionsDataLoader(
                      interactions=val, batch_size=1024, shuffle=False
                  )
              )
  
          model = MatrixFactorizationModel(
              train=train_loader,
              val=val_loader,
              embedding_dim=15,
              dropout_p=0.1,
              lr=1e-1,
              bias_lr=1e-2,
              optimizer="adam",
              bias_optimizer="sgd",
              weight_decay=1e-7,
              loss="bpr",
              sparse=False,
          )
      elif request.param == "mf_with_y_range":
          model = MatrixFactorizationModel(
              train=train, val=val, y_range=(0, 4)
          )
      elif request.param == "collaborative_metric_learning":
          model = CollaborativeMetricLearningModel(
              train=train,
              val=val,
              embedding_dim=15,
              lr=1e-1,
              optimizer="adam",
              loss="hinge",
          )
      elif request.param == "sparse_collaborative_metric_learning":
          model = CollaborativeMetricLearningModel(
              train=train,
              val=val,
              embedding_dim=15,
              lr=1e-1,
              optimizer="sparse_adam",
              loss="hinge",
              sparse=True,
          )
      elif request.param == "mlp_mf":
          model = MLPMatrixFactorizationModel(
              train=train,
              val=val,
              embedding_dim=15,
              num_layers=3,
              dropout_p=0.1,
              lr=1e-1,
              bias_lr=1e-2,
              optimizer="adam",
              bias_optimizer="sgd",
              weight_decay=1e-7,
              loss="hinge",
          )
      elif request.param == "mlp_mf_with_y_range":
          model = MLPMatrixFactorizationModel(
              train=train, val=val, y_range=(0, 2)
          )
      elif request.param == "nonlinear_mf":
          model = NonlinearMatrixFactorizationModel(
              train=train,
              val=val,
              user_embedding_dim=15,
              item_embedding_dim=15,
              user_dense_layers_dims=[15, 10],
              item_dense_layers_dims=[15, 10],
              embedding_dropout_p=0.05,
              dense_dropout_p=0.1,
              lr=1e-2,
              bias_lr=1e-1,
              optimizer="adam",
              bias_optimizer="sgd",
              weight_decay=1e-7,
              loss="bpr",
          )
      elif request.param == "nonlinear_mf_with_y_range":
          model = NonlinearMatrixFactorizationModel(
              train=train, val=val, y_range=(0, 4)
          )
      elif request.param == "neucf":
          model = NeuralCollaborativeFiltering(
              train=train,
              val=val,
              embedding_dim=10,
              num_layers=1,
              dropout_p=0.1,
              lr=1e-4,
              weight_decay=0.0,
              optimizer="adam",
              loss="adaptive",
          )
      elif request.param == "neucf_sigmoid":
          model = NeuralCollaborativeFiltering(
              train=train, val=val, lr=1e-4, final_layer="sigmoid"
          )
      elif request.param == "neucf_relu":
          model = NeuralCollaborativeFiltering(
              train=train, val=val, lr=1e-4, final_layer="relu"
          )
      elif request.param == "neucf_leaky_rulu":
          model = NeuralCollaborativeFiltering(
              train=train, val=val, lr=1e-4, final_layer="leaky_relu"
          )
      elif request.param == "neucf_custom":
          model = NeuralCollaborativeFiltering(
              train=train, val=val, lr=1e-4, final_layer=torch.tanh
          )
      elif request.param == "deep_fm":
          model = DeepFM(
              train=train,
              val=val,
              embedding_dim=10,
              num_layers=1,
              dropout_p=0.1,
              lr=1e-3,
              weight_decay=0.0,
              optimizer="adam",
              bias_optimizer=None,
              loss="hinge",
          )
      elif request.param == "deep_fm_sigmoid":
          model = DeepFM(train=train, val=val, final_layer="sigmoid")
      elif request.param == "deep_fm_relu":
          model = DeepFM(train=train, val=val, final_layer="relu")
      elif request.param == "deep_fm_leaky_rulu":
          model = DeepFM(train=train, val=val, final_layer="leaky_relu")
      elif request.param == "deep_fm_custom":
          model = DeepFM(train=train, val=val, final_layer=torch.tanh)
      elif (
          request.param == "hybrid_pretrained"
          or request.param == "hybrid_pretrained_metadata_layers"
      ):
          implicit_model = MatrixFactorizationModel(
              train=train,
              val=val,
              embedding_dim=10,
              lr=1e-1,
              optimizer="adam",
          )
          implicit_model_trainer = CollieTrainer(
              model=implicit_model,
              gpus=gpu_count,
              max_epochs=1,
              deterministic=True,
              logger=False,
              enable_checkpointing=False,
          )
          implicit_model_trainer.fit(implicit_model)
          implicit_model.eval()
  
          metadata_layers_dims = None
          if request.param == "hybrid_pretrained_metadata_layers":
              metadata_layers_dims = [16, 12]
  
          model_frozen = HybridPretrainedModel(
              train=train,
              val=val,
              item_metadata=movielens_metadata_df,
              trained_model=implicit_model,
              item_metadata_layers_dims=metadata_layers_dims,
              user_metadata=user_metadata_df,
              user_metadata_layers_dims=metadata_layers_dims,
              freeze_embeddings=True,
              dropout_p=0.15,
              loss="warp",
              lr=0.01,
              optimizer=torch.optim.Adam,
              weight_decay=0.0,
          )
          model_frozen_trainer = CollieTrainer(
              model=model_frozen,
              gpus=gpu_count,
              max_epochs=1,
              deterministic=True,
              logger=False,
              enable_checkpointing=False,
          )
          model_frozen_trainer.fit(model_frozen)
  
          model = HybridPretrainedModel(
              train=train,
              val=val,
              item_metadata=movielens_metadata_df,
              trained_model=implicit_model,
              item_metadata_layers_dims=metadata_layers_dims,
              user_metadata=user_metadata_df,
              user_metadata_layers_dims=metadata_layers_dims,
              freeze_embeddings=False,
              dropout_p=0.15,
              loss="bpr",
              lr=1e-4,
              optimizer=torch.optim.Adam,
              metadata_for_loss={"genre": genres},
              metadata_for_loss_weights={"genre": 0.4},
              weight_decay=0.0,
          )
          model.load_from_hybrid_model(model_frozen)
      elif (
          request.param == "hybrid_mf"
          or request.param == "hybrid_mf_metadata_layers"
          or request.param
          == "hybrid_mf_metadata_layers_metadata_for_loss"
          or request.param
          == "hybrid_mf_metadata_layers_no_bias_optimizer"
      ):
          number_of_stages = 3
  
          metadata_layers_dims = None
          if request.param == "hybrid_mf_metadata_layers":
              metadata_layers_dims = [16, 12]
  
          additional_kwargs = {}
          if (
              request.param
              == "hybrid_mf_metadata_layers_metadata_for_loss"
          ):
              additional_kwargs = {
                  "metadata_for_loss": {"genre": genres},
                  "metadata_for_loss_weights": {"genre": 0.4},
              }
          elif (
              request.param
              == "hybrid_mf_metadata_layers_no_bias_optimizer"
          ):
              additional_kwargs = {"bias_optimizer": None}
  
          model = HybridModel(
              train=train,
              val=val,
              item_metadata=movielens_metadata_df,
              embedding_dim=10,
              item_metadata_layers_dims=metadata_layers_dims,
              user_metadata=user_metadata_df,
              user_metadata_layers_dims=metadata_layers_dims,
              lr=1e-1,
              optimizer="adam",
              **additional_kwargs,
          )
      elif (
          request.param == "cold_start"
          or request.param == "cold_start_metadata_for_loss"
          or request.param == "cold_start_no_val"
          or request.param == "cold_start_no_lr_scheduler"
      ):
          number_of_stages = 2
  
          additional_kwargs = {}
          if request.param == "cold_start_metadata_for_loss":
              additional_kwargs = {
                  "metadata_for_loss": {"genre": genres},
                  "metadata_for_loss_weights": {"genre": 0.4},
              }
          elif request.param == "cold_start_no_lr_scheduler":
              additional_kwargs = {"lr_scheduler_func": None}
  
          model = ColdStartModel(
              train=train,
              val=val if request.param != "cold_start_no_val" else None,
              item_buckets=genres[: train.num_items],
              embedding_dim=10,
              item_buckets_stage_lr=1e-2,
              no_buckets_stage_lr=1e-2,
              item_buckets_stage_optimizer="sgd",
              **additional_kwargs,
          )
  
      model_trainer = CollieTrainer(
          model=model,
          gpus=gpu_count,
          max_epochs=1,
          deterministic=True,
          logger=False,
          enable_checkpointing=False,
      )
  
      if number_of_stages is None:
          model_trainer.fit(model)
      else:
          for idx in range(number_of_stages):
              model_trainer.fit(model)
  
              if idx < (number_of_stages - 1):
                  model_trainer.max_epochs += 1
                  model.advance_stage()
  
      model.eval()
  
      return model
  
file_path: collie/tests/fixtures/model_fixtures.py
imports: import os
  import tempfile
  import pytest
  import torch
  from collie.interactions import ApproximateNegativeSamplingInteractionsDataLoader, HDF5InteractionsDataLoader, InteractionsDataLoader
  from collie.model import ColdStartModel, CollaborativeMetricLearningModel, CollieMinimalTrainer, CollieTrainer, DeepFM, HybridModel, HybridPretrainedModel, MatrixFactorizationModel, MLPMatrixFactorizationModel, NeuralCollaborativeFiltering, NonlinearMatrixFactorizationModel
  from collie.utils import pandas_df_to_hdf5
label_desc: Use image node_id 7 for calling a global function with example usage: models_trained_for_one_step(request, train_val_implicit_sample_data, movielens_metadata_df, user_metadata_df, movielens_implicit_df, train_val_implicit_pandas_data, gpu_count) and returns: model, model