1414
1515import typing as tp
1616from collections .abc import Hashable
17- from typing import Dict , List , Tuple , Union
17+ from typing import Dict , List , Tuple
1818
1919import numpy as np
2020import torch
2121from pytorch_lightning import Trainer
22- from pytorch_lightning .accelerators import Accelerator
2322
2423from .item_net import CatFeaturesItemNet , IdEmbeddingsItemNet , ItemNetBase
2524from .transformer_base import (
2625 PADDING_VALUE ,
26+ SessionEncoderDataPreparatorType ,
2727 SessionEncoderLightningModule ,
2828 SessionEncoderLightningModuleBase ,
2929 TransformerModelBase ,
30+ TransformerModelConfig ,
3031)
3132from .transformer_data_preparator import SessionEncoderDataPreparatorBase
3233from .transformer_net_blocks import (
@@ -144,7 +145,15 @@ def _collate_fn_recommend(self, batch: List[Tuple[List[int], List[float]]]) -> D
144145 return {"x" : torch .LongTensor (x )}
145146
146147
147- class BERT4RecModel (TransformerModelBase ):
148+ class BERT4RecModelConfig (TransformerModelConfig ):
149+ """BERT4RecModel config."""
150+
151+ data_preparator_type : SessionEncoderDataPreparatorType = BERT4RecDataPreparator
152+ use_key_padding_mask : bool = True
153+ mask_prob : float = 0.15
154+
155+
156+ class BERT4RecModel (TransformerModelBase [BERT4RecModelConfig ]):
148157 """
149158 BERT4Rec model.
150159
@@ -190,10 +199,21 @@ class BERT4RecModel(TransformerModelBase):
190199 deterministic : bool, default ``False``
191200 If ``True``, set deterministic algorithms for PyTorch operations.
192201 Use `pytorch_lightning.seed_everything` together with this parameter to fix the random state.
193- recommend_device : {"cpu", "gpu", "tpu", "hpu", "mps", "auto"} or Accelerator, default "auto"
194- Device for recommend. Used at predict_step of lightning module.
202+ recommend_batch_size : int, default 256
203+ How many samples per batch to load during `recommend`.
204+ If you want to change this parameter after model is initialized,
205+ you can manually assign new value to model `recommend_batch_size` attribute.
206+ recommend_accelerator : {"cpu", "gpu", "tpu", "hpu", "mps", "auto"}, default "auto"
207+ Accelerator type for `recommend`. Used at predict_step of lightning module.
208+ If you want to change this parameter after model is initialized,
209+ you can manually assign new value to model `recommend_accelerator` attribute.
210+ recommend_devices : int | List[int], default 1
211+ Devices for `recommend`. Please note that multi-device inference is not supported!
212+ Do not specify more then one device. For ``gpu`` accelerator you can pass which device to
213+ use, e.g. ``[1]``.
214+ Used at predict_step of lightning module.
215+ Multi-device recommendations are not supported.
195216 If you want to change this parameter after model is initialized,
196- you can manually assign new value to model `recommend_device` attribute.
197217 recommend_n_threads : int, default 0
198218 Number of threads to use in ranker if GPU ranking is turned off or unavailable.
199219 If you want to change this parameter after model is initialized,
@@ -222,38 +242,44 @@ class BERT4RecModel(TransformerModelBase):
222242 Function to get validation mask.
223243 """
224244
245+ config_class = BERT4RecModelConfig
246+
225247 def __init__ ( # pylint: disable=too-many-arguments, too-many-locals
226248 self ,
227- n_blocks : int = 1 ,
228- n_heads : int = 1 ,
229- n_factors : int = 128 ,
249+ n_blocks : int = 2 ,
250+ n_heads : int = 4 ,
251+ n_factors : int = 256 ,
230252 use_pos_emb : bool = True ,
231253 use_causal_attn : bool = False ,
232254 use_key_padding_mask : bool = True ,
233255 dropout_rate : float = 0.2 ,
234256 epochs : int = 3 ,
235257 verbose : int = 0 ,
236258 deterministic : bool = False ,
237- recommend_device : Union [str , Accelerator ] = "auto" ,
259+ recommend_batch_size : int = 256 ,
260+ recommend_accelerator : str = "auto" ,
261+ recommend_devices : tp .Union [int , tp .List [int ]] = 1 ,
238262 recommend_n_threads : int = 0 ,
239263 recommend_use_gpu_ranking : bool = True ,
240- session_max_len : int = 32 ,
264+ session_max_len : int = 100 ,
241265 n_negatives : int = 1 ,
242266 batch_size : int = 128 ,
243267 loss : str = "softmax" ,
244268 gbce_t : float = 0.2 ,
245- lr : float = 0.01 ,
269+ lr : float = 0.001 ,
246270 dataloader_num_workers : int = 0 ,
247271 train_min_user_interactions : int = 2 ,
248272 mask_prob : float = 0.15 ,
249273 trainer : tp .Optional [Trainer ] = None ,
250274 item_net_block_types : tp .Sequence [tp .Type [ItemNetBase ]] = (IdEmbeddingsItemNet , CatFeaturesItemNet ),
251275 pos_encoding_type : tp .Type [PositionalEncodingBase ] = LearnableInversePositionalEncoding ,
252276 transformer_layers_type : tp .Type [TransformerLayersBase ] = PreLNTransformerLayers ,
253- data_preparator_type : tp .Type [BERT4RecDataPreparator ] = BERT4RecDataPreparator ,
277+ data_preparator_type : tp .Type [SessionEncoderDataPreparatorBase ] = BERT4RecDataPreparator ,
254278 lightning_module_type : tp .Type [SessionEncoderLightningModuleBase ] = SessionEncoderLightningModule ,
255279 get_val_mask_func : tp .Optional [tp .Callable ] = None ,
256280 ):
281+ self .mask_prob = mask_prob
282+
257283 super ().__init__ (
258284 transformer_layers_type = transformer_layers_type ,
259285 data_preparator_type = data_preparator_type ,
@@ -264,28 +290,37 @@ def __init__( # pylint: disable=too-many-arguments, too-many-locals
264290 use_causal_attn = use_causal_attn ,
265291 use_key_padding_mask = use_key_padding_mask ,
266292 dropout_rate = dropout_rate ,
293+ session_max_len = session_max_len ,
294+ dataloader_num_workers = dataloader_num_workers ,
295+ batch_size = batch_size ,
296+ loss = loss ,
297+ n_negatives = n_negatives ,
298+ gbce_t = gbce_t ,
299+ lr = lr ,
267300 epochs = epochs ,
268301 verbose = verbose ,
269302 deterministic = deterministic ,
270- recommend_device = recommend_device ,
303+ recommend_batch_size = recommend_batch_size ,
304+ recommend_accelerator = recommend_accelerator ,
305+ recommend_devices = recommend_devices ,
271306 recommend_n_threads = recommend_n_threads ,
272307 recommend_use_gpu_ranking = recommend_use_gpu_ranking ,
273- loss = loss ,
274- gbce_t = gbce_t ,
275- lr = lr ,
276- session_max_len = session_max_len + 1 ,
308+ train_min_user_interactions = train_min_user_interactions ,
277309 trainer = trainer ,
278310 item_net_block_types = item_net_block_types ,
279311 pos_encoding_type = pos_encoding_type ,
280312 lightning_module_type = lightning_module_type ,
313+ get_val_mask_func = get_val_mask_func ,
281314 )
282- self .data_preparator = data_preparator_type (
283- session_max_len = session_max_len ,
284- n_negatives = n_negatives if loss != "softmax" else None ,
285- batch_size = batch_size ,
286- dataloader_num_workers = dataloader_num_workers ,
287- train_min_user_interactions = train_min_user_interactions ,
315+
316+ def _init_data_preparator (self ) -> None :
317+ self .data_preparator : SessionEncoderDataPreparatorBase = self .data_preparator_type (
318+ session_max_len = self .session_max_len - 1 , # TODO: remove `-1`
319+ n_negatives = self .n_negatives if self .loss != "softmax" else None ,
320+ batch_size = self .batch_size ,
321+ dataloader_num_workers = self .dataloader_num_workers ,
322+ train_min_user_interactions = self .train_min_user_interactions ,
288323 item_extra_tokens = (PADDING_VALUE , MASKING_VALUE ),
289- mask_prob = mask_prob ,
290- get_val_mask_func = get_val_mask_func ,
324+ mask_prob = self . mask_prob ,
325+ get_val_mask_func = self . get_val_mask_func ,
291326 )
0 commit comments