ncf_test.py 4 KB
Newer Older
Frederick Liu's avatar
Frederick Liu committed
1
# Copyright 2021 The TensorFlow Authors. All Rights Reserved.
2
3
4
5
6
7
8
9
10
11
12
13
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Frederick Liu's avatar
Frederick Liu committed
14

15
16
17
18
19
20
"""Tests NCF."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

21
import unittest
22
23

import tensorflow as tf
Hongkun Yu's avatar
Hongkun Yu committed
24
from tensorflow.python.eager import context  # pylint: disable=ungrouped-imports
25
from official.recommendation import constants as rconst
Shining Sun's avatar
Shining Sun committed
26
from official.recommendation import ncf_common
27
28
from official.recommendation import ncf_keras_main
from official.utils.testing import integration
29

30
NUM_TRAIN_NEG = 4
31
32
33


class NcfTest(tf.test.TestCase):
Reed's avatar
Reed committed
34
35
36
37

  @classmethod
  def setUpClass(cls):  # pylint: disable=invalid-name
    super(NcfTest, cls).setUpClass()
Shining Sun's avatar
Shining Sun committed
38
    ncf_common.define_ncf_flags()
Reed's avatar
Reed committed
39

40
41
42
43
44
45
46
47
48
  def setUp(self):
    self.top_k_old = rconst.TOP_K
    self.num_eval_negatives_old = rconst.NUM_EVAL_NEGATIVES
    rconst.NUM_EVAL_NEGATIVES = 2

  def tearDown(self):
    rconst.NUM_EVAL_NEGATIVES = self.num_eval_negatives_old
    rconst.TOP_K = self.top_k_old

49
  _BASE_END_TO_END_FLAGS = ['-batch_size', '1044', '-train_epochs', '1']
50

Hongkun Yu's avatar
Hongkun Yu committed
51
  @unittest.mock.patch.object(rconst, 'SYNTHETIC_BATCHES_PER_EPOCH', 100)
52
  def test_end_to_end_keras_no_dist_strat(self):
53
    integration.run_synthetic(
Hongkun Yu's avatar
Hongkun Yu committed
54
55
        ncf_keras_main.main,
        tmp_root=self.get_temp_dir(),
56
        extra_flags=self._BASE_END_TO_END_FLAGS +
57
        ['-distribution_strategy', 'off'])
Reed's avatar
Reed committed
58

Hongkun Yu's avatar
Hongkun Yu committed
59
  @unittest.mock.patch.object(rconst, 'SYNTHETIC_BATCHES_PER_EPOCH', 100)
60
  def test_end_to_end_keras_dist_strat(self):
61
    integration.run_synthetic(
Hongkun Yu's avatar
Hongkun Yu committed
62
63
        ncf_keras_main.main,
        tmp_root=self.get_temp_dir(),
64
65
        extra_flags=self._BASE_END_TO_END_FLAGS + ['-num_gpus', '0'])

Hongkun Yu's avatar
Hongkun Yu committed
66
  @unittest.mock.patch.object(rconst, 'SYNTHETIC_BATCHES_PER_EPOCH', 100)
67
  def test_end_to_end_keras_dist_strat_ctl(self):
Hongkun Yu's avatar
Hongkun Yu committed
68
69
70
    flags = (
        self._BASE_END_TO_END_FLAGS + ['-num_gpus', '0'] +
        ['-keras_use_ctl', 'True'])
71
    integration.run_synthetic(
Hongkun Yu's avatar
Hongkun Yu committed
72
        ncf_keras_main.main, tmp_root=self.get_temp_dir(), extra_flags=flags)
Reed's avatar
Reed committed
73

Hongkun Yu's avatar
Hongkun Yu committed
74
  @unittest.mock.patch.object(rconst, 'SYNTHETIC_BATCHES_PER_EPOCH', 100)
75
  def test_end_to_end_keras_1_gpu_dist_strat_fp16(self):
76
77
    if context.num_gpus() < 1:
      self.skipTest(
Hongkun Yu's avatar
Hongkun Yu committed
78
79
          '{} GPUs are not available for this test. {} GPUs are available'
          .format(1, context.num_gpus()))
80
81

    integration.run_synthetic(
Hongkun Yu's avatar
Hongkun Yu committed
82
83
84
85
        ncf_keras_main.main,
        tmp_root=self.get_temp_dir(),
        extra_flags=self._BASE_END_TO_END_FLAGS +
        ['-num_gpus', '1', '--dtype', 'fp16'])
86

Hongkun Yu's avatar
Hongkun Yu committed
87
  @unittest.mock.patch.object(rconst, 'SYNTHETIC_BATCHES_PER_EPOCH', 100)
88
89
90
  def test_end_to_end_keras_1_gpu_dist_strat_ctl_fp16(self):
    if context.num_gpus() < 1:
      self.skipTest(
Hongkun Yu's avatar
Hongkun Yu committed
91
92
          '{} GPUs are not available for this test. {} GPUs are available'
          .format(1, context.num_gpus()))
93
94

    integration.run_synthetic(
Hongkun Yu's avatar
Hongkun Yu committed
95
96
97
98
        ncf_keras_main.main,
        tmp_root=self.get_temp_dir(),
        extra_flags=self._BASE_END_TO_END_FLAGS +
        ['-num_gpus', '1', '--dtype', 'fp16', '--keras_use_ctl'])
99

100
  @unittest.mock.patch.object(rconst, 'SYNTHETIC_BATCHES_PER_EPOCH', 100)
101
  def test_end_to_end_keras_2_gpu_fp16(self):
102
103
    if context.num_gpus() < 2:
      self.skipTest(
Hongkun Yu's avatar
Hongkun Yu committed
104
105
          '{} GPUs are not available for this test. {} GPUs are available'
          .format(2, context.num_gpus()))
106
107

    integration.run_synthetic(
Hongkun Yu's avatar
Hongkun Yu committed
108
109
110
111
112
        ncf_keras_main.main,
        tmp_root=self.get_temp_dir(),
        extra_flags=self._BASE_END_TO_END_FLAGS +
        ['-num_gpus', '2', '--dtype', 'fp16'])

113

Hongkun Yu's avatar
Hongkun Yu committed
114
if __name__ == '__main__':
115
  tf.test.main()